Index: src/core/SkTileGrid.cpp |
diff --git a/src/core/SkTileGrid.cpp b/src/core/SkTileGrid.cpp |
index 30ca4b9179b7e29d71425a65b0ad4d1dfd31fc8b..942d9db871442f9058e0680bc0fc0a082826451e 100644 |
--- a/src/core/SkTileGrid.cpp |
+++ b/src/core/SkTileGrid.cpp |
@@ -6,17 +6,30 @@ |
*/ |
#include "SkTileGrid.h" |
+#include "Sk4x.h" |
+ |
+// We scrunch the bounds in just a little to make the right and bottom edges |
+// exclusive. We want bounds of exactly one tile to hit exactly one tile. |
+static const Sk4f kScrunch = { 0, 0, SK_ScalarNearlyZero, SK_ScalarNearlyZero }; |
SkTileGrid::SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGridInfo& info) |
: fXTiles(xTiles) |
- , fYTiles(yTiles) |
- , fInvWidth( SkScalarInvert(info.fTileInterval.width())) |
- , fInvHeight(SkScalarInvert(info.fTileInterval.height())) |
- , fMarginWidth (info.fMargin.fWidth +1) // Margin is offset by 1 as a provision for AA and |
- , fMarginHeight(info.fMargin.fHeight+1) // to cancel the outset applied by getClipDeviceBounds. |
- , fOffset(SkPoint::Make(info.fOffset.fX, info.fOffset.fY)) |
- , fGridBounds(SkRect::MakeWH(xTiles * info.fTileInterval.width(), |
- yTiles * info.fTileInterval.height())) |
+ , fNumTiles(xTiles * yTiles) |
+ , fGridBounds(SkLoad4f(0, |
+ 0, |
+ xTiles * info.fTileInterval.width(), |
+ yTiles * info.fTileInterval.height())) |
+ , fMargin(SkLoad4f(-info.fMargin.fWidth - 1, // Margin is increased by 1 as a provision for AA |
+ -info.fMargin.fHeight - 1, // and to cancel the outset applied by |
+ +info.fMargin.fWidth + 1, // getClipDeviceBounds(). |
+ +info.fMargin.fHeight + 1)) |
+ , fOffset(SkLoad4f(info.fOffset.fX, info.fOffset.fY, info.fOffset.fX, info.fOffset.fY) |
+ - kScrunch) |
+ , fScale(SkLoad4f(SkScalarInvert(info.fTileInterval.width()), |
+ SkScalarInvert(info.fTileInterval.height()), |
+ SkScalarInvert(info.fTileInterval.width()), |
+ SkScalarInvert(info.fTileInterval.height()))) |
+ , fHigh(SkLoad4f(fXTiles -1, yTiles - 1, fXTiles - 1, yTiles - 1)) |
, fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) {} |
SkTileGrid::~SkTileGrid() { |
@@ -24,7 +37,7 @@ SkTileGrid::~SkTileGrid() { |
} |
void SkTileGrid::reserve(unsigned opCount) { |
- if (fXTiles * fYTiles == 0) { |
+ if (fNumTiles == 0) { |
return; // A tileless tile grid is nonsensical, but happens in at least cc_unittests. |
} |
@@ -34,9 +47,9 @@ void SkTileGrid::reserve(unsigned opCount) { |
// If we take those observations and further assume the ops are distributed evenly |
// across the picture, we get this guess for number of ops per tile: |
- const int opsPerTileGuess = (2 * opCount) / (fXTiles * fYTiles); |
+ const int opsPerTileGuess = (2 * opCount) / fNumTiles; |
- for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles); tile++) { |
+ for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + fNumTiles; tile++) { |
tile->setReserve(opsPerTileGuess); |
} |
@@ -45,49 +58,59 @@ void SkTileGrid::reserve(unsigned opCount) { |
} |
void SkTileGrid::flushDeferredInserts() { |
- for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles); tile++) { |
+ for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + fNumTiles; tile++) { |
tile->shrinkToFit(); |
} |
} |
-// Adjustments to user-provided bounds common to both insert() and search(). |
-// Call this after making insert- or search- specific adjustments. |
-void SkTileGrid::commonAdjust(SkRect* rect) const { |
- // Apply our offset. |
- rect->offset(fOffset); |
+// Convert user-space bounds to grid tiles they cover (LT+RB both inclusive). |
+// Out of bounds queries are clamped to the single nearest tile. |
+void SkTileGrid::userToGrid(const Sk4f& user, SkIRect* grid) const { |
+ static const Sk4f kZero = { 0, 0, 0, 0 }; |
+ |
+ Sk4f scaled = user * fScale; |
+ scaled = Sk4Max(scaled, kZero); |
+ scaled = Sk4Min(scaled, fHigh); |
+ |
+ SkStore4(Sk4Convert<Sk4i>(scaled), &grid->fLeft); |
+} |
- // Scrunch the bounds in just a little to make the right and bottom edges |
- // exclusive. We want bounds of exactly one tile to hit exactly one tile. |
- rect->fRight -= SK_ScalarNearlyZero; |
- rect->fBottom -= SK_ScalarNearlyZero; |
+// If the rect is inverted, sort it. |
+static Sk4f sorted(const Sk4f& ltrb) { |
+ Sk4f rblt = Sk4Shuffle(ltrb, ltrb, 2, 3, 0, 1), |
+ mins = Sk4Min(ltrb, rblt), |
+ maxs = Sk4Max(ltrb, rblt), |
+ sort = Sk4Shuffle(mins, maxs, 0, 1, 4, 5); |
+ return sort; |
} |
-// Convert user-space bounds to grid tiles they cover (LT inclusive, RB exclusive). |
-void SkTileGrid::userToGrid(const SkRect& user, SkIRect* grid) const { |
- grid->fLeft = SkPin32(user.left() * fInvWidth , 0, fXTiles - 1); |
- grid->fTop = SkPin32(user.top() * fInvHeight, 0, fYTiles - 1); |
- grid->fRight = SkPin32(user.right() * fInvWidth , 0, fXTiles - 1) + 1; |
- grid->fBottom = SkPin32(user.bottom() * fInvHeight, 0, fYTiles - 1) + 1; |
+static bool intersects(const Sk4f& a, const Sk4f& b) { |
+ // Two rects intersect if lefts are less than the |
+ // opposite rights and tops less than opposite bottoms. |
+ Sk4f lt = Sk4Shuffle(a, b, 0, 4, 1, 5), // a.L b.L a.T b.T < |
+ rb = Sk4Shuffle(a, b, 6, 2, 7, 3); // b.R a.R b.B a.B ? |
+ return Sk4All(lt < rb); |
} |
void SkTileGrid::insert(unsigned opIndex, const SkRect& originalBounds, bool) { |
- SkRect bounds = originalBounds; |
- bounds.outset(fMarginWidth, fMarginHeight); |
- this->commonAdjust(&bounds); |
+ Sk4f bounds = SkLoad4f(&originalBounds.fLeft) + fMargin + fOffset; |
+ SkASSERT(Sk4All(sorted(bounds) == bounds)); |
- // TODO(mtklein): can we assert this instead to save an intersection in Release mode, |
- // or just allow out-of-bound insertions to insert anyway (clamped to nearest tile)? |
- if (!SkRect::Intersects(bounds, fGridBounds)) { |
+ // TODO(mtklein): skip this check and just let out-of-bounds rects insert into nearest tile? |
+ if (!intersects(bounds, fGridBounds)) { |
return; |
} |
SkIRect grid; |
this->userToGrid(bounds, &grid); |
- for (int y = grid.fTop; y < grid.fBottom; y++) { |
- for (int x = grid.fLeft; x < grid.fRight; x++) { |
- fTiles[y * fXTiles + x].push(opIndex); |
+ SkTDArray<unsigned>* row = &fTiles[grid.fTop * fXTiles + grid.fLeft]; |
+ for (int y = 0; y <= grid.fBottom - grid.fTop; y++) { |
+ SkTDArray<unsigned>* tile = row; |
+ for (int x = 0; x <= grid.fRight - grid.fLeft; x++) { |
+ (tile++)->push(opIndex); |
} |
+ row += fXTiles; |
} |
} |
@@ -97,25 +120,16 @@ void SkTileGrid::insert(unsigned opIndex, const SkRect& originalBounds, bool) { |
// require 512 tiles of size 256 x 256 pixels. |
static const int kStackAllocationTileCount = 1024; |
+ |
void SkTileGrid::search(const SkRect& originalQuery, SkTDArray<unsigned>* results) const { |
- // The inset counteracts the outset that applied in 'insert', which optimizes |
- // for lookups of size 'tileInterval + 2 * margin' (aligned with the tile grid). |
- SkRect query = originalQuery; |
- query.inset(fMarginWidth, fMarginHeight); |
- this->commonAdjust(&query); |
- |
- // The inset may have inverted the rectangle, so sort(). |
- // TODO(mtklein): It looks like we only end up with inverted bounds in unit tests |
- // that make explicitly inverted queries, not from insetting. If we can drop support for |
- // unsorted bounds (i.e. we don't see them outside unit tests), I think we can drop this. |
- query.sort(); |
- |
- // No intersection check. We optimize for queries that are in bounds. |
- // We're safe anyway: userToGrid() will clamp out-of-bounds queries to nearest tile. |
+ // The "- fMargin" counteracts the "+ fMargin" that applied in insert(), which optimizes |
+ // for lookups of size tileInterval + 2 * margin (aligned with the tile grid). |
+ Sk4f query = sorted(SkLoad4f(&originalQuery.fLeft) - fMargin + fOffset); |
+ |
SkIRect grid; |
this->userToGrid(query, &grid); |
- const int tilesHit = (grid.fRight - grid.fLeft) * (grid.fBottom - grid.fTop); |
+ const int tilesHit = (grid.fRight - grid.fLeft + 1) * (grid.fBottom - grid.fTop + 1); |
SkASSERT(tilesHit > 0); |
if (tilesHit == 1) { |
@@ -130,12 +144,12 @@ void SkTileGrid::search(const SkRect& originalQuery, SkTDArray<unsigned>* result |
// Gather pointers to the starts and ends of the tiles to merge. |
SkAutoSTArray<kStackAllocationTileCount, const unsigned*> starts(tilesHit), ends(tilesHit); |
int i = 0; |
- for (int y = grid.fTop; y < grid.fBottom; y++) { |
- for (int x = grid.fLeft; x < grid.fRight; x++) { |
- starts[i] = fTiles[y * fXTiles + x].begin(); |
- ends[i] = fTiles[y * fXTiles + x].end(); |
- i++; |
- } |
+ for (int y = grid.fTop; y <= grid.fBottom; y++) { |
+ for (int x = grid.fLeft; x <= grid.fRight; x++) { |
+ starts[i] = fTiles[y * fXTiles + x].begin(); |
+ ends[i] = fTiles[y * fXTiles + x].end(); |
+ i++; |
+ } |
} |
// Merge tiles into results until they're fully consumed. |