Add new skyline Rectanizer
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 2 Oct 2013 17:37:59 +0000 (17:37 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 2 Oct 2013 17:37:59 +0000 (17:37 +0000)
The skyline Rectanizer tracks the leading silhouette of the
currently packed rects and tries to fit the next rectangle
on top of that. This gives slightly better packing behavior
than the current log2 scheme.

Also shrank the size of the plots, which increases their
number from 18 to 32 and gives better cache behavior (fewer
purges and uploads).

R=bsalomon@google.com

Author: jvanverth@google.com

Review URL: https://codereview.chromium.org/25584002

git-svn-id: http://skia.googlecode.com/svn/trunk@11577 2bbb7eff-a529-9590-31e7-b0007b416f81

gyp/gpu.gypi
src/gpu/GrAtlas.cpp
src/gpu/GrRectanizer.cpp
src/gpu/GrRectanizer_skyline.cpp [new file with mode: 0755]

index 80840a0..d8e2cc4 100644 (file)
@@ -95,6 +95,7 @@
       '<(skia_src_path)/gpu/GrPlotMgr.h',
       '<(skia_src_path)/gpu/GrRectanizer.cpp',
       '<(skia_src_path)/gpu/GrRectanizer.h',
+      '<(skia_src_path)/gpu/GrRectanizer_skyline.cpp',
       '<(skia_src_path)/gpu/GrRedBlackTree.h',
       '<(skia_src_path)/gpu/GrRenderTarget.cpp',
       '<(skia_src_path)/gpu/GrReducedClip.cpp',
index ff14983..70fe3e0 100644 (file)
@@ -25,8 +25,8 @@
 #define GR_ATLAS_TEXTURE_WIDTH  1024
 #define GR_ATLAS_TEXTURE_HEIGHT 2048
 
-#define GR_ATLAS_WIDTH  341
-#define GR_ATLAS_HEIGHT 341
+#define GR_ATLAS_WIDTH  256
+#define GR_ATLAS_HEIGHT 256
 
 #define GR_PLOT_WIDTH   (GR_ATLAS_TEXTURE_WIDTH / GR_ATLAS_WIDTH)
 #define GR_PLOT_HEIGHT  (GR_ATLAS_TEXTURE_HEIGHT / GR_ATLAS_HEIGHT)
index 00803fe..9f0d846 100644 (file)
@@ -122,6 +122,7 @@ bool GrRectanizerPow2::addRect(int width, int height, GrIPoint16* loc) {
 
 ///////////////////////////////////////////////////////////////////////////////
 
-GrRectanizer* GrRectanizer::Factory(int width, int height) {
-    return SkNEW_ARGS(GrRectanizerPow2, (width, height));
-}
+// factory is now in GrRectanizer_skyline.cpp
+//GrRectanizer* GrRectanizer::Factory(int width, int height) {
+//    return SkNEW_ARGS(GrRectanizerPow2, (width, height));
+//}
diff --git a/src/gpu/GrRectanizer_skyline.cpp b/src/gpu/GrRectanizer_skyline.cpp
new file mode 100755 (executable)
index 0000000..96de2d0
--- /dev/null
@@ -0,0 +1,166 @@
+
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "GrRectanizer.h"
+#include "SkTDArray.h"
+
+// Pack rectangles and track the current silhouette
+// Based in part on Jukka Jylänki's work at http://clb.demon.fi
+
+class GrRectanizerSkyline : public GrRectanizer {
+public:
+    GrRectanizerSkyline(int w, int h) : GrRectanizer(w, h) {
+        reset();
+    }
+
+    virtual ~GrRectanizerSkyline() {
+    }
+
+    virtual void reset() {
+        fAreaSoFar = 0;
+        fSkyline.reset();
+        SkylineSegment* seg = fSkyline.append(1);
+        seg->fX = 0;
+        seg->fY = 0;
+        seg->fWidth = width();
+    }
+
+    virtual bool addRect(int w, int h, GrIPoint16* loc);
+
+    virtual float percentFull() const {
+        return fAreaSoFar / ((float)this->width() * this->height());
+    }
+
+    virtual int stripToPurge(int height) const { return -1; }
+    virtual void purgeStripAtY(int yCoord) { }
+
+    ///////////////////////////////////////////////////////////////////////////
+
+    struct SkylineSegment {
+        int  fX;
+        int  fY;
+        int  fWidth;
+    };
+
+    SkTDArray<SkylineSegment> fSkyline;
+
+    int32_t fAreaSoFar;
+
+    bool rectangleFits(int skylineIndex, int width, int height, int* y) const;
+    void addSkylineLevel(int skylineIndex, int x, int y, int width, int height);
+};
+
+bool GrRectanizerSkyline::addRect(int width, int height, GrIPoint16* loc) {
+    if ((unsigned)width > (unsigned)this->width() ||
+        (unsigned)height > (unsigned)this->height()) {
+        return false;
+    }
+
+    // find position for new rectangle
+    int bestWidth = this->width() + 1;
+    int bestX;
+    int bestY = this->height() + 1;
+    int bestIndex = -1;
+    for (int i = 0; i < fSkyline.count(); ++i) {
+        int y;
+        if (this->rectangleFits(i, width, height, &y)) {
+            // minimize y position first, then width of skyline
+            if (y < bestY || (y == bestY && fSkyline[i].fWidth < bestWidth)) {
+                bestIndex = i;
+                bestWidth = fSkyline[i].fWidth;
+                bestX = fSkyline[i].fX;
+                bestY = y;
+            }
+        }
+    }
+
+    // add rectangle to skyline
+    if (-1 != bestIndex) {
+        this->addSkylineLevel(bestIndex, bestX, bestY, width, height);
+        loc->fX = bestX;
+        loc->fY = bestY;
+
+        fAreaSoFar += width*height;
+        return true;
+    }
+
+    loc->fX = 0;
+    loc->fY = 0;
+    return false;
+}
+
+bool GrRectanizerSkyline::rectangleFits(int skylineIndex, int width, int height, int* ypos) const {
+    int x = fSkyline[skylineIndex].fX;
+    if (x + width > this->width()) {
+        return false;
+    }
+
+    int widthLeft = width;
+    int i = skylineIndex;
+    int y = fSkyline[skylineIndex].fY;
+    while (widthLeft > 0) {
+       y = SkMax32(y, fSkyline[i].fY);
+        if (y + height > this->height()) {
+            return false;
+        }
+       widthLeft -= fSkyline[i].fWidth;
+       ++i;
+       SkASSERT(i < fSkyline.count() || widthLeft <= 0);
+    }
+
+    *ypos = y;
+    return true;
+}
+
+void GrRectanizerSkyline::addSkylineLevel(int skylineIndex, int x, int y, int width, int height) {
+    SkylineSegment newSegment;
+    newSegment.fX = x;
+    newSegment.fY = y + height;
+    newSegment.fWidth = width;
+    fSkyline.insert(skylineIndex, 1, &newSegment);
+
+    SkASSERT(newSegment.fX + newSegment.fWidth <= this->width());
+    SkASSERT(newSegment.fY <= this->height());
+
+    // delete width of this skyline segment from following ones
+    for (int i = skylineIndex+1; i < fSkyline.count(); ++i) {
+        SkASSERT(fSkyline[i-1].fX <= fSkyline[i].fX);
+
+        if (fSkyline[i].fX < fSkyline[i-1].fX + fSkyline[i-1].fWidth) {
+            int shrink = fSkyline[i-1].fX + fSkyline[i-1].fWidth - fSkyline[i].fX;
+
+            fSkyline[i].fX += shrink;
+            fSkyline[i].fWidth -= shrink;
+
+            if (fSkyline[i].fWidth <= 0) {
+                fSkyline.remove(i);
+                --i;
+            }
+            else
+                break;
+        }
+        else
+            break;
+    }
+
+    // merge fSkylines
+    for (int i = 0; i < fSkyline.count()-1; ++i) {
+        if (fSkyline[i].fY == fSkyline[i+1].fY) {
+            fSkyline[i].fWidth += fSkyline[i+1].fWidth;
+            fSkyline.remove(i+1);
+            --i;
+        }
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+GrRectanizer* GrRectanizer::Factory(int width, int height) {
+    return SkNEW_ARGS(GrRectanizerSkyline, (width, height));
+}
+