aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/com/sun/opengl/impl/packrect/LevelSet.java
diff options
context:
space:
mode:
authorKenneth Russel <kbrussel@alum.mit.edu>2007-01-04 07:20:30 +0000
committerKenneth Russel <kbrussel@alum.mit.edu>2007-01-04 07:20:30 +0000
commite4daa5d745f3d084e0c7180a88597b2582131b63 (patch)
tree3e5b35a4b3ec92a74abaa11228fabca32e11eae0 /src/classes/com/sun/opengl/impl/packrect/LevelSet.java
parent10c76681d1507fa527b59cb28bcde5d1c6a4fe18 (diff)
With extensive help from Phil Race and Chris Campbell from the Java 2D
team, added a TextRenderer class which enables high-performance rendering of bitmapped Java 2D fonts, with full Unicode support, into arbitrary OpenGL drawables using a simple API. Builds on top of the TextureRenderer class and its associated functionality; added createAlphaOnlyRenderer() and other methods to enable the TextRenderer. Caches rendering results on a string-by-string basis in an OpenGL texture using a fully automatic least-recently-used algorithm for good efficiency when rendering the same string or strings multiple times. Uses a rectangle packing algorithm, currently housed in com.sun.opengl.impl.packrect, for managing the positions of cached strings on a larger OpenGL texture to avoid OpenGL pipeline state changes. Added a TestTextRenderer demo which simply adds a moving text string and frames-per-second counter to the Gears demo; more sophisticated examples to come. (Some commented-out debugging code is being temporarily left in the new demo, to be removed in the next checkin, in order to have it in the version history.) git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@1067 232f8b59-042b-4e1e-8c03-345bb8c30851
Diffstat (limited to 'src/classes/com/sun/opengl/impl/packrect/LevelSet.java')
-rwxr-xr-xsrc/classes/com/sun/opengl/impl/packrect/LevelSet.java174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/classes/com/sun/opengl/impl/packrect/LevelSet.java b/src/classes/com/sun/opengl/impl/packrect/LevelSet.java
new file mode 100755
index 000000000..561a3fe14
--- /dev/null
+++ b/src/classes/com/sun/opengl/impl/packrect/LevelSet.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * - Redistribution of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistribution in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any kind. ALL
+ * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
+ * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
+ * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
+ * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
+ * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
+ * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
+ * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
+ * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
+ * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
+ * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
+ * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that this software is not designed or intended for use
+ * in the design, construction, operation or maintenance of any nuclear
+ * facility.
+ *
+ * Sun gratefully acknowledges that this software was originally authored
+ * and developed by Kenneth Bradley Russell and Christopher John Kline.
+ */
+
+package com.sun.opengl.impl.packrect;
+
+import java.util.*;
+
+/** Manages a list of Levels; this is the core data structure
+ contained within the RectanglePacker and encompasses the storage
+ algorithm for the contained Rects. */
+
+public class LevelSet {
+ // Maintained in sorted order by increasing Y coordinate
+ private List/*<Level>*/ levels = new ArrayList/*<Level>*/();
+ private int nextAddY;
+ private int w;
+ private int h;
+
+ /** A LevelSet manages all of the backing store for a region of a
+ specified width and height. */
+ public LevelSet(int w, int h) {
+ this.w = w;
+ this.h = h;
+ }
+
+ public int w() { return w; }
+ public int h() { return h; }
+
+ /** Returns true if the given rectangle was successfully added to
+ the LevelSet given its current dimensions, false if not. Caller
+ is responsible for performing compaction, expansion, etc. as a
+ consequence. */
+ public boolean add(Rect rect) {
+ if (rect.w() > w)
+ return false;
+
+ // Go in reverse order through the levels seeing whether we can
+ // trivially satisfy the allocation request
+ for (int i = levels.size() - 1; i >= 0; --i) {
+ Level level = (Level) levels.get(i);
+ if (level.add(rect))
+ return true;
+ }
+
+ // See whether compaction could satisfy this allocation. This
+ // increases the computational complexity of the addition process,
+ // but prevents us from expanding unnecessarily.
+ for (int i = levels.size() - 1; i >= 0; --i) {
+ Level level = (Level) levels.get(i);
+ if (level.couldAllocateIfCompacted(rect))
+ return false;
+ }
+
+ // OK, we need to either add a new Level or expand the backing
+ // store. Try to add a new Level.
+ if (nextAddY + rect.h() > h)
+ return false;
+
+ Level newLevel = new Level(w, rect.h(), nextAddY, this);
+ levels.add(newLevel);
+ nextAddY += rect.h();
+ boolean res = newLevel.add(rect);
+ if (!res)
+ throw new RuntimeException("Unexpected failure in addition to new Level");
+ return true;
+ }
+
+ /** Removes the given Rect from this LevelSet. */
+ public boolean remove(Rect rect) {
+ for (int i = levels.size() - 1; i >= 0; --i) {
+ Level level = (Level) levels.get(i);
+ if (level.remove(rect))
+ return true;
+ }
+
+ return false;
+ }
+
+ /** Allocates the given Rectangle, performing compaction of a Level
+ if necessary. This is the correct fallback path to {@link
+ #add(Rect)} above. Returns true if allocated successfully, false
+ otherwise (indicating the need to expand the backing store). */
+ public boolean compactAndAdd(Rect rect,
+ Object backingStore,
+ BackingStoreManager manager) {
+ for (int i = levels.size() - 1; i >= 0; --i) {
+ Level level = (Level) levels.get(i);
+ if (level.couldAllocateIfCompacted(rect)) {
+ level.compact(backingStore, manager);
+ boolean res = level.add(rect);
+ if (!res)
+ throw new RuntimeException("Unexpected failure to add after compaction");
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /** Indicates whether it's legal to trivially increase the height of
+ the given Level. This is only possible if it's the last Level
+ added and there's enough room in the backing store. */
+ public boolean canExpand(Level level, int height) {
+ if (levels.isEmpty())
+ return false; // Should not happen
+ if (levels.get(levels.size() - 1) == level &&
+ (h - nextAddY >= height - level.h()))
+ return true;
+ return false;
+ }
+
+ public void expand(Level level, int oldHeight, int newHeight) {
+ nextAddY += (newHeight - oldHeight);
+ }
+
+ public Iterator iterator() {
+ return levels.iterator();
+ }
+
+ /** Visits all Rects contained in this LevelSet. */
+ public void visit(RectVisitor visitor) {
+ for (Iterator iter = levels.iterator(); iter.hasNext(); ) {
+ Level level = (Level) iter.next();
+ level.visit(visitor);
+ }
+ }
+
+ /** Updates the references to the Rect objects in this LevelSet with
+ the "next locations" of those Rects. This is actually used to
+ update the new Rects in a newly laid-out LevelSet with the
+ original Rects. */
+ public void updateRectangleReferences() {
+ for (Iterator iter = levels.iterator(); iter.hasNext(); ) {
+ Level level = (Level) iter.next();
+ level.updateRectangleReferences();
+ }
+ }
+}