Specialize SkRectanzier to SkRectanizerSkyline
It looks like the pow2 rectanizer has never been used. Remove
the unneeded abstraction for rectanizer everywhere.
Change-Id: Iba33f1c6faf37201d03928ce8409751c212480a0
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/265983
Commit-Queue: Herb Derby <herb@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/src/gpu/GrDrawOpAtlas.cpp b/src/gpu/GrDrawOpAtlas.cpp
index 8f15db9..6211b71 100644
--- a/src/gpu/GrDrawOpAtlas.cpp
+++ b/src/gpu/GrDrawOpAtlas.cpp
@@ -15,7 +15,6 @@
#include "src/gpu/GrOnFlushResourceProvider.h"
#include "src/gpu/GrOpFlushState.h"
#include "src/gpu/GrProxyProvider.h"
-#include "src/gpu/GrRectanizer.h"
#include "src/gpu/GrResourceProvider.h"
#include "src/gpu/GrResourceProviderPriv.h"
#include "src/gpu/GrSurfaceProxyPriv.h"
@@ -103,7 +102,7 @@
, fHeight(height)
, fX(offX)
, fY(offY)
- , fRects(nullptr)
+ , fRectanizer(width, height)
, fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
, fColorType(colorType)
, fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
@@ -120,17 +119,12 @@
GrDrawOpAtlas::Plot::~Plot() {
sk_free(fData);
- delete fRects;
}
bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
SkASSERT(width <= fWidth && height <= fHeight);
- if (!fRects) {
- fRects = GrRectanizer::Factory(fWidth, fHeight);
- }
-
- if (!fRects->addRect(width, height, loc)) {
+ if (!fRectanizer.addRect(width, height, loc)) {
return false;
}
@@ -192,9 +186,7 @@
}
void GrDrawOpAtlas::Plot::resetRects() {
- if (fRects) {
- fRects->reset();
- }
+ fRectanizer.reset();
fGenID++;
fID = CreateId(fPageIndex, fPlotIndex, fGenID);
diff --git a/src/gpu/GrDrawOpAtlas.h b/src/gpu/GrDrawOpAtlas.h
index 4829b47..ff9f688 100644
--- a/src/gpu/GrDrawOpAtlas.h
+++ b/src/gpu/GrDrawOpAtlas.h
@@ -16,10 +16,10 @@
#include "src/core/SkIPoint16.h"
#include "src/core/SkTInternalLList.h"
+#include "src/gpu/GrRectanizerSkyline.h"
#include "src/gpu/ops/GrDrawOp.h"
class GrOnFlushResourceProvider;
-class GrRectanizer;
/**
@@ -357,7 +357,7 @@
const int fHeight;
const int fX;
const int fY;
- GrRectanizer* fRects;
+ GrRectanizerSkyline fRectanizer;
const SkIPoint16 fOffset; // the offset of the plot in the backing texture
const GrColorType fColorType;
const size_t fBytesPerPixel;
diff --git a/src/gpu/GrRectanizer.h b/src/gpu/GrRectanizer.h
deleted file mode 100644
index 2e55896..0000000
--- a/src/gpu/GrRectanizer.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright 2010 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef GrRectanizer_DEFINED
-#define GrRectanizer_DEFINED
-
-#include "include/gpu/GrTypes.h"
-
-struct SkIPoint16;
-
-class GrRectanizer {
-public:
- GrRectanizer(int width, int height) : fWidth(width), fHeight(height) {
- SkASSERT(width >= 0);
- SkASSERT(height >= 0);
- }
-
- virtual ~GrRectanizer() {}
-
- virtual void reset() = 0;
-
- int width() const { return fWidth; }
- int height() const { return fHeight; }
-
- // Attempt to add a rect. Return true on success; false on failure. If
- // successful the position in the atlas is returned in 'loc'.
- virtual bool addRect(int width, int height, SkIPoint16* loc) = 0;
- virtual float percentFull() const = 0;
-
- /**
- * Our factory, which returns the subclass du jour
- */
- static GrRectanizer* Factory(int width, int height);
-
-private:
- int fWidth;
- int fHeight;
-};
-
-#endif
diff --git a/src/gpu/GrRectanizer_skyline.cpp b/src/gpu/GrRectanizerSkyline.cpp
similarity index 93%
rename from src/gpu/GrRectanizer_skyline.cpp
rename to src/gpu/GrRectanizerSkyline.cpp
index 250cfc5..21931ef 100644
--- a/src/gpu/GrRectanizer_skyline.cpp
+++ b/src/gpu/GrRectanizerSkyline.cpp
@@ -6,7 +6,7 @@
*/
#include "src/core/SkIPoint16.h"
-#include "src/gpu/GrRectanizer_skyline.h"
+#include "src/gpu/GrRectanizerSkyline.h"
bool GrRectanizerSkyline::addRect(int width, int height, SkIPoint16* loc) {
if ((unsigned)width > (unsigned)this->width() ||
@@ -114,8 +114,3 @@
}
}
-///////////////////////////////////////////////////////////////////////////////
-
-GrRectanizer* GrRectanizer::Factory(int width, int height) {
- return new GrRectanizerSkyline(width, height);
-}
diff --git a/src/gpu/GrRectanizer_skyline.h b/src/gpu/GrRectanizerSkyline.h
similarity index 70%
rename from src/gpu/GrRectanizer_skyline.h
rename to src/gpu/GrRectanizerSkyline.h
index 60663fd..638d893 100644
--- a/src/gpu/GrRectanizer_skyline.h
+++ b/src/gpu/GrRectanizerSkyline.h
@@ -5,23 +5,21 @@
* found in the LICENSE file.
*/
-#ifndef GrRectanizer_skyline_DEFINED
-#define GrRectanizer_skyline_DEFINED
+#ifndef GrRectanizerSkyline_DEFINED
+#define GrRectanizerSkyline_DEFINED
#include "include/private/SkTDArray.h"
-#include "src/gpu/GrRectanizer.h"
+#include "src/core/SkIPoint16.h"
// Pack rectangles and track the current silhouette
// Based, in part, on Jukka Jylanki's work at http://clb.demon.fi
-class GrRectanizerSkyline : public GrRectanizer {
+class GrRectanizerSkyline {
public:
- GrRectanizerSkyline(int w, int h) : INHERITED(w, h) {
+ GrRectanizerSkyline(int w, int h) : fWidth{w}, fHeight{h} {
this->reset();
}
- ~GrRectanizerSkyline() override { }
-
- void reset() override {
+ void reset() {
fAreaSoFar = 0;
fSkyline.reset();
SkylineSegment* seg = fSkyline.append(1);
@@ -30,11 +28,10 @@
seg->fWidth = this->width();
}
- bool addRect(int w, int h, SkIPoint16* loc) override;
+ bool addRect(int w, int h, SkIPoint16* loc);
- float percentFull() const override {
- return fAreaSoFar / ((float)this->width() * this->height());
- }
+ int width() const { return fWidth; }
+ int height() const { return fHeight; }
private:
struct SkylineSegment {
@@ -43,10 +40,6 @@
int fWidth;
};
- SkTDArray<SkylineSegment> fSkyline;
-
- int32_t fAreaSoFar;
-
// Can a width x height rectangle fit in the free space represented by
// the skyline segments >= 'skylineIndex'? If so, return true and fill in
// 'y' with the y-location at which it fits (the x location is pulled from
@@ -56,7 +49,10 @@
// at x,y.
void addSkylineLevel(int skylineIndex, int x, int y, int width, int height);
- typedef GrRectanizer INHERITED;
+ SkTDArray<SkylineSegment> fSkyline;
+ int32_t fAreaSoFar;
+ int fWidth;
+ int fHeight;
};
-#endif
+#endif // GrRectanizerSkyline_DEFINED
diff --git a/src/gpu/GrRectanizer_pow2.cpp b/src/gpu/GrRectanizer_pow2.cpp
deleted file mode 100644
index 1d0328a..0000000
--- a/src/gpu/GrRectanizer_pow2.cpp
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright 2010 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "src/gpu/GrRectanizer_pow2.h"
-
-bool GrRectanizerPow2::addRect(int width, int height, SkIPoint16* loc) {
- if ((unsigned)width > (unsigned)this->width() ||
- (unsigned)height > (unsigned)this->height()) {
- return false;
- }
-
- int32_t area = width * height; // computed here since height will be modified
-
- height = GrNextPow2(height);
- if (height < kMIN_HEIGHT_POW2) {
- height = kMIN_HEIGHT_POW2;
- }
-
- Row* row = &fRows[HeightToRowIndex(height)];
- SkASSERT(row->fRowHeight == 0 || row->fRowHeight == height);
-
- if (0 == row->fRowHeight) {
- if (!this->canAddStrip(height)) {
- return false;
- }
- this->initRow(row, height);
- } else {
- if (!row->canAddWidth(width, this->width())) {
- if (!this->canAddStrip(height)) {
- return false;
- }
- // that row is now "full", so retarget our Row record for
- // another one
- this->initRow(row, height);
- }
- }
-
- SkASSERT(row->fRowHeight == height);
- SkASSERT(row->canAddWidth(width, this->width()));
- *loc = row->fLoc;
- row->fLoc.fX += width;
-
- SkASSERT(row->fLoc.fX <= this->width());
- SkASSERT(row->fLoc.fY <= this->height());
- SkASSERT(fNextStripY <= this->height());
- fAreaSoFar += area;
- return true;
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-// factory is now in GrRectanizer_skyline.cpp
-//GrRectanizer* GrRectanizer::Factory(int width, int height) {
-// return new GrRectanizerPow2 (width, height);
-//}
diff --git a/src/gpu/GrRectanizer_pow2.h b/src/gpu/GrRectanizer_pow2.h
deleted file mode 100644
index 70e8399..0000000
--- a/src/gpu/GrRectanizer_pow2.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
-* Copyright 2014 Google Inc.
-*
-* Use of this source code is governed by a BSD-style license that can be
-* found in the LICENSE file.
-*/
-
-#ifndef GrRectanizer_pow2_DEFINED
-#define GrRectanizer_pow2_DEFINED
-
-#include "include/private/SkMalloc.h"
-#include "src/core/SkIPoint16.h"
-#include "src/core/SkMathPriv.h"
-#include "src/gpu/GrRectanizer.h"
-
-// This Rectanizer quantizes the incoming rects to powers of 2. Each power
-// of two can have, at most, one active row/shelf. Once a row/shelf for
-// a particular power of two gets full its fRows entry is recycled to point
-// to a new row.
-// The skyline algorithm almost always provides a better packing.
-class GrRectanizerPow2 : public GrRectanizer {
-public:
- GrRectanizerPow2(int w, int h) : INHERITED(w, h) {
- this->reset();
- }
-
- ~GrRectanizerPow2() override {}
-
- void reset() override {
- fNextStripY = 0;
- fAreaSoFar = 0;
- sk_bzero(fRows, sizeof(fRows));
- }
-
- bool addRect(int w, int h, SkIPoint16* loc) override;
-
- float percentFull() const override {
- return fAreaSoFar / ((float)this->width() * this->height());
- }
-
-private:
- static const int kMIN_HEIGHT_POW2 = 2;
- static const int kMaxExponent = 16;
-
- struct Row {
- SkIPoint16 fLoc;
- // fRowHeight is actually known by this struct's position in fRows
- // but it is used to signal if there exists an open row of this height
- int fRowHeight;
-
- bool canAddWidth(int width, int containerWidth) const {
- return fLoc.fX + width <= containerWidth;
- }
- };
-
- Row fRows[kMaxExponent]; // 0-th entry will be unused
-
- int fNextStripY;
- int32_t fAreaSoFar;
-
- static int HeightToRowIndex(int height) {
- SkASSERT(height >= kMIN_HEIGHT_POW2);
- int index = 32 - SkCLZ(height - 1);
- SkASSERT(index < kMaxExponent);
- return index;
- }
-
- bool canAddStrip(int height) const {
- return fNextStripY + height <= this->height();
- }
-
- void initRow(Row* row, int rowHeight) {
- row->fLoc.set(0, fNextStripY);
- row->fRowHeight = rowHeight;
- fNextStripY += rowHeight;
- }
-
- typedef GrRectanizer INHERITED;
-};
-
-#endif
diff --git a/src/gpu/ccpr/GrCCAtlas.cpp b/src/gpu/ccpr/GrCCAtlas.cpp
index 329a78c..8b4b14c 100644
--- a/src/gpu/ccpr/GrCCAtlas.cpp
+++ b/src/gpu/ccpr/GrCCAtlas.cpp
@@ -13,7 +13,7 @@
#include "src/gpu/GrCaps.h"
#include "src/gpu/GrOnFlushResourceProvider.h"
#include "src/gpu/GrProxyProvider.h"
-#include "src/gpu/GrRectanizer_skyline.h"
+#include "src/gpu/GrRectanizerSkyline.h"
#include "src/gpu/GrRenderTarget.h"
#include "src/gpu/GrRenderTargetContext.h"
#include "src/gpu/GrTextureProxy.h"