rendering optimizations
authoradam <adam@megacz.com>
Sat, 10 Apr 2004 07:51:56 +0000 (07:51 +0000)
committeradam <adam@megacz.com>
Sat, 10 Apr 2004 07:51:56 +0000 (07:51 +0000)
darcs-hash:20040410075156-5007d-d37e99b6f58674268511562d8ff5e7d6751b37e3.gz

src/org/ibex/Box.java

index bbfad25..b3f5fd4 100644 (file)
@@ -116,13 +116,14 @@ public final class Box extends JSScope implements Scheduler.Task {
     static final int CURSOR       = 0x00010000;  // if true, this box has a cursor in the cursor hash; FEATURE: GC issues?
     static final int CLIP         = 0x00020000;
     static final int STOP_UPWARD_PROPAGATION    = 0x00040000;
+    static final int MOVED         = 0x00080000;
 
 
     // Instance Data //////////////////////////////////////////////////////////////////////
 
     Box parent = null;
     Box redirect = this;
-    int flags = VISIBLE | PACKED | REPACK | REFLOW | RESIZE | FIXED /* ROWS */ | STOP_UPWARD_PROPAGATION | CLIP;
+    int flags = VISIBLE | PACKED | REPACK | REFLOW | RESIZE | FIXED /* ROWS */ | STOP_UPWARD_PROPAGATION | CLIP | MOVED;
 
     private String text = null;
     private Font font = DEFAULT_FONT; 
@@ -168,19 +169,9 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     /** invoked when a resource needed to render ourselves finishes loading */
     public void perform() throws JSExn {
-
-        // FIXME; we can't assume that just because we were performed the image is loaded.
-        // as external events have occured, check the state of box
-        if (texture != null) {
-            if (texture.isLoaded) { minwidth = min(texture.width, maxwidth); minheight = min(texture.height, maxheight); }
-            else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res.unclone()); }
-        } else {
-            Log.warn(Box.class, "perform() called with null texture");
-        }
-        MARK_REPACK;
-        MARK_REFLOW;
-        MARK_RESIZE;
-        dirty();
+        if (texture == null) { Log.warn(Box.class, "perform() called with null texture"); return; }
+        if (texture.isLoaded) { setMinWidth(max(texture.width, maxwidth)); setMinHeight(max(texture.height, maxheight)); }
+        else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res.unclone()); }
     }
 
     // FEATURE: use cx2/cy2 format
@@ -205,20 +196,18 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     // Reflow ////////////////////////////////////////////////////////////////////////////////////////
 
-    // worst case runtime = O(numboxes * numboxes_in_widest_row)
-    
-    private static Box[] frontier = new Box[65535];  // FIXME: GC hazard
-
-    /** pack the boxes into rows and columns; also computes contentwidth */
+    private static Box[] frontier = new Box[65535];
+    /** pack the boxes into rows and columns, compute contentwidth */
     void pack() {
         for(Box child = getChild(0); child != null; child = child.nextSibling()) child.pack();
         int frontier_size = 0; contentwidth = 0; contentheight = 0;
         //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan \
         //        contentheight/contentwidth contentwidth/contentheight
-        if (treeSize() > 0 && test(FIXED) == COLS) {
+        if (test(FIXED) == COLS) {
             rows = 0;
             for(Box child = getChild(0); child != null; child = child.nextSibling()) {
                 if (!child.test(PACKED) || !child.test(VISIBLE)) continue;
+                if (cols == 1) { child.row = rows; rows += child.rowspan; child.col = 0; continue; }
                 child.col = (short)(frontier_size <= 0 ? 0 : (frontier[frontier_size-1].col + frontier[frontier_size-1].colspan));
                 child.row = (short)(frontier_size <= 0 ? 0 : frontier[frontier_size-1].row);
                 if (child.col + min(cols,child.colspan) > cols) { child.col = 0; child.row++; }
@@ -236,39 +225,37 @@ public final class Box extends JSScope implements Scheduler.Task {
                     } else break;
                 frontier[frontier_size++] = child;
             }
-            for(int i=0; i<frontier_size; i++){rows=(short)max(rows, frontier[i].row + frontier[i].rowspan); frontier[i] = null; }
+            for(int i=0; i<frontier_size; i++){ rows=(short)max(rows, frontier[i].row + frontier[i].rowspan); frontier[i] = null; }
         }
         //#end
         solve(true);
         //#repeat contentwidth/contentheight contentheight/contentwidth minwidth/minheight row/col col/row \
         //        textwidth/textheight maxwidth/maxheight cols/rows rows/cols colspan/rowspan rowspan/colspan
         contentwidth = bound(minwidth,
-                             max(contentwidth,
-                                 font == null || text == null ? 0 : font.textwidth(text)),
+                             max(contentwidth, font == null || text == null ? 0 : font.textwidth(text)),
                              maxwidth);
         //#end
     }
     
     void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
-        if (x != this.x || y != this.y || width != this.width || height != this.height) {
-            boolean sizechange = (this.width != width || this.height != height) && getTrap("SizeChange") != null;
-            do {
-                int thisx = parent == null ? 0 : this.x;
-                int thisy = parent == null ? 0 : this.y;
-                // we can't reenable this until we track surface-relative sizes; imagine clear surface with nonclear children
-                if (false /*texture == null && (text == null || text.equals(""))*/) {
-                    if ((fillcolor & 0xff000000) == 0) break;
-                    if (this.x == x && this.y == y) {
-                        Box who = (parent == null ? this : parent);
-                        who.dirty(thisx+min(this.width,width), thisy, Math.abs(width-this.width), max(this.height, height));
-                        who.dirty(thisx, thisy+min(this.height,height), min(this.width, width), Math.abs(height-this.height));
-                        break;
-                    }
+        if (x == this.x && y == this.y && width == this.width && height == this.height) return;
+        boolean sizechange = (this.width != width || this.height != height) && getTrap("SizeChange") != null;
+        try {
+            int thisx = parent == null ? 0 : this.x;
+            int thisy = parent == null ? 0 : this.y;
+            if (this.x != x || this.y != y) set(MOVED);
+            if (texture == null && (text == null || text.equals("")) && !test(MOVED)) {
+                if ((fillcolor & 0xff000000) != 0) {
+                    Box who = (parent == null ? this : parent);
+                    who.dirty(thisx+min(this.width,width), thisy, Math.abs(width-this.width), max(this.height, height));
+                    who.dirty(thisx, thisy+min(this.height,height), min(this.width, width), Math.abs(height-this.height));
                 }
-                (parent == null ? this : parent).dirty(thisx, thisy, this.width, this.height);
-                this.width = width; this.height = height; this.x = x; this.y = y;
-                dirty();
-            } while (false);
+                return;
+            }
+            (parent == null ? this : parent).dirty(thisx, thisy, this.width, this.height);
+            this.width = width; this.height = height; this.x = x; this.y = y;
+            dirty();
+        } finally {
             this.width = width; this.height = height; this.x = x; this.y = y;
             if (sizechange) putAndTriggerTrapsAndCatchExceptions("SizeChange", T);
         }
@@ -421,8 +408,10 @@ public final class Box extends JSScope implements Scheduler.Task {
                 child_width -= diff;
                 //#end
             }
+            if (test(MOVED)) child.set(MOVED);
             child.resize(child_x, child_y, child_width, child_height);
         }
+        clear(MOVED);
 
         for(Box child = getChild(0); child != null; child = child.nextSibling())
             if (child.test(VISIBLE) && child.treeSize() > 0)
@@ -576,6 +565,14 @@ public final class Box extends JSScope implements Scheduler.Task {
         }
     }
 
+    public void setMinWidth(int minwidth) {
+        if (this.minwidth == minwidth) return;
+        MARK_RESIZE; MARK_REFLOW; MARK_REPACK; this.minwidth = minwidth;
+    }
+    public void setMinHeight(int minheight) {
+        if (this.minheight == minheight) return;
+        MARK_RESIZE; MARK_REFLOW; MARK_REPACK; this.minheight = minheight;
+    }
     public void setMaxWidth(Object value) {
         do { CHECKSET_INT(maxwidth); MARK_RESIZE; } while(false);
         if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth;