OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkTileGrid.h" | 8 #include "SkTileGrid.h" |
9 #include "Sk4x.h" | |
9 | 10 |
10 SkTileGrid::SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGrid Info& info) | 11 SkTileGrid::SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGrid Info& info) |
11 : fXTiles(xTiles) | 12 : fXTiles(xTiles) |
12 , fYTiles(yTiles) | 13 , fNumTiles(xTiles * yTiles) |
13 , fInvWidth( SkScalarInvert(info.fTileInterval.width())) | 14 , fGridBounds(0, 0, xTiles * info.fTileInterval.width(), yTiles * info.fTile Interval.height()) |
14 , fInvHeight(SkScalarInvert(info.fTileInterval.height())) | 15 , fMargin(-info.fMargin.fWidth - 1, // Margin is increased by 1 as a provi sion for AA |
15 , fMarginWidth (info.fMargin.fWidth +1) // Margin is offset by 1 as a provi sion for AA and | 16 -info.fMargin.fHeight - 1, // and to cancel the outset applied by |
16 , fMarginHeight(info.fMargin.fHeight+1) // to cancel the outset applied by getClipDeviceBounds. | 17 +info.fMargin.fWidth + 1, // getClipDeviceBounds(). |
17 , fOffset(SkPoint::Make(info.fOffset.fX, info.fOffset.fY)) | 18 +info.fMargin.fHeight + 1) |
18 , fGridBounds(SkRect::MakeWH(xTiles * info.fTileInterval.width(), | 19 , fOffset(info.fOffset.fX, |
19 yTiles * info.fTileInterval.height())) | 20 info.fOffset.fY, |
21 info.fOffset.fX - SK_ScalarNearlyZero, // We scrunch user-provide d bounds in a little | |
22 info.fOffset.fY - SK_ScalarNearlyZero) // to make right and botto m edges exclusive. | |
23 , fUserToGrid(SkScalarInvert(info.fTileInterval.width()), | |
24 SkScalarInvert(info.fTileInterval.height()), | |
25 SkScalarInvert(info.fTileInterval.width()), | |
26 SkScalarInvert(info.fTileInterval.height())) | |
27 , fGridHigh(fXTiles - 1, yTiles - 1, fXTiles - 1, yTiles - 1) | |
20 , fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) {} | 28 , fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) {} |
21 | 29 |
22 SkTileGrid::~SkTileGrid() { | 30 SkTileGrid::~SkTileGrid() { |
23 SkDELETE_ARRAY(fTiles); | 31 SkDELETE_ARRAY(fTiles); |
24 } | 32 } |
25 | 33 |
26 void SkTileGrid::reserve(unsigned opCount) { | 34 void SkTileGrid::reserve(unsigned opCount) { |
27 if (fXTiles * fYTiles == 0) { | 35 if (fNumTiles == 0) { |
28 return; // A tileless tile grid is nonsensical, but happens in at least cc_unittests. | 36 return; // A tileless tile grid is nonsensical, but happens in at least cc_unittests. |
29 } | 37 } |
30 | 38 |
31 // If we assume every op we're about to try to insert() falls within our gri d bounds, | 39 // If we assume every op we're about to try to insert() falls within our gri d bounds, |
32 // then every op has to hit at least one tile. In fact, a quick scan over o ur small | 40 // then every op has to hit at least one tile. In fact, a quick scan over o ur small |
33 // SKP set shows that in the average SKP, each op hits two 256x256 tiles. | 41 // SKP set shows that in the average SKP, each op hits two 256x256 tiles. |
34 | 42 |
35 // If we take those observations and further assume the ops are distributed evenly | 43 // If we take those observations and further assume the ops are distributed evenly |
36 // across the picture, we get this guess for number of ops per tile: | 44 // across the picture, we get this guess for number of ops per tile: |
37 const int opsPerTileGuess = (2 * opCount) / (fXTiles * fYTiles); | 45 const int opsPerTileGuess = (2 * opCount) / fNumTiles; |
38 | 46 |
39 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles ); tile++) { | 47 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + fNumTiles; tile++) { |
40 tile->setReserve(opsPerTileGuess); | 48 tile->setReserve(opsPerTileGuess); |
41 } | 49 } |
42 | 50 |
43 // In practice, this heuristic means we'll temporarily allocate about 30% mo re bytes | 51 // In practice, this heuristic means we'll temporarily allocate about 30% mo re bytes |
44 // than if we made no setReserve() calls, but time spent in insert() drops b y about 50%. | 52 // than if we made no setReserve() calls, but time spent in insert() drops b y about 50%. |
45 } | 53 } |
46 | 54 |
47 void SkTileGrid::flushDeferredInserts() { | 55 void SkTileGrid::flushDeferredInserts() { |
48 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles ); tile++) { | 56 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + fNumTiles; tile++) { |
49 tile->shrinkToFit(); | 57 tile->shrinkToFit(); |
50 } | 58 } |
51 } | 59 } |
52 | 60 |
53 // Adjustments to user-provided bounds common to both insert() and search(). | 61 // Convert user-space bounds to grid tiles they cover (LT+RB both inclusive). |
54 // Call this after making insert- or search- specific adjustments. | 62 // Out of bounds queries are clamped to the single nearest tile. |
55 void SkTileGrid::commonAdjust(SkRect* rect) const { | 63 void SkTileGrid::userToGrid(const Sk4f& user, SkIRect* out) const { |
56 // Apply our offset. | 64 // Map from user coordinates to grid tile coordinates. |
57 rect->offset(fOffset); | 65 Sk4f grid = user.multiply(fUserToGrid); |
58 | 66 |
59 // Scrunch the bounds in just a little to make the right and bottom edges | 67 // Now that we're in grid coordinates, clamp to the grid bounds. |
60 // exclusive. We want bounds of exactly one tile to hit exactly one tile. | 68 grid = Sk4f::Max(grid, Sk4f(0,0,0,0)); |
61 rect->fRight -= SK_ScalarNearlyZero; | 69 grid = Sk4f::Min(grid, fGridHigh); |
62 rect->fBottom -= SK_ScalarNearlyZero; | 70 |
71 // Truncate to integers. | |
72 grid.cast<Sk4i>().store(&out->fLeft); | |
63 } | 73 } |
64 | 74 |
65 // Convert user-space bounds to grid tiles they cover (LT inclusive, RB exclusiv e). | 75 // If the rect is inverted, sort it. |
66 void SkTileGrid::userToGrid(const SkRect& user, SkIRect* grid) const { | 76 static Sk4f sorted(const Sk4f& ltrb) { |
67 grid->fLeft = SkPin32(user.left() * fInvWidth , 0, fXTiles - 1); | 77 // To sort: |
68 grid->fTop = SkPin32(user.top() * fInvHeight, 0, fYTiles - 1); | 78 // left, right = minmax(left, right) |
69 grid->fRight = SkPin32(user.right() * fInvWidth , 0, fXTiles - 1) + 1; | 79 // top, bottom = minmax(top, bottom) |
70 grid->fBottom = SkPin32(user.bottom() * fInvHeight, 0, fYTiles - 1) + 1; | 80 Sk4f rblt = ltrb.zwxy(), |
81 ltlt = Sk4f::Min(ltrb, rblt), // Holds (2 copies of) new left and top. | |
82 rbrb = Sk4f::Max(ltrb, rblt), // Holds (2 copies of) new right and bot tom. | |
83 sort = Sk4f::XYAB(ltlt, rbrb); | |
84 return sort; | |
85 } | |
86 | |
87 // Do these two LTRB rectangles intersect? | |
88 static bool intersects(const Sk4f& r1, const Sk4f& r2) { | |
89 // Two rects intersect if lefts are less than the | |
90 // opposite rights and tops less than opposite bottoms. | |
91 Sk4f lt = Sk4f::XYAB(r1, r2), // r1.L r1.T r2.L r2.T < | |
92 rb = Sk4f::ZWCD(r2, r1); // r2.R r2.B r1.R r1.B ? | |
93 return lt.lessThan(rb).allTrue(); | |
71 } | 94 } |
72 | 95 |
73 void SkTileGrid::insert(unsigned opIndex, const SkRect& originalBounds, bool) { | 96 void SkTileGrid::insert(unsigned opIndex, const SkRect& originalBounds, bool) { |
74 SkRect bounds = originalBounds; | 97 Sk4f bounds = Sk4f(&originalBounds.fLeft).add(fMargin).add(fOffset); |
75 bounds.outset(fMarginWidth, fMarginHeight); | 98 SkASSERT(sorted(bounds).equal(bounds).allTrue()); |
76 this->commonAdjust(&bounds); | |
77 | 99 |
78 // TODO(mtklein): can we assert this instead to save an intersection in Rele ase mode, | 100 // TODO(mtklein): skip this check and just let out-of-bounds rects insert in to nearest tile? |
79 // or just allow out-of-bound insertions to insert anyway (clamped to neares t tile)? | 101 if (!intersects(bounds, fGridBounds)) { |
80 if (!SkRect::Intersects(bounds, fGridBounds)) { | |
81 return; | 102 return; |
82 } | 103 } |
83 | 104 |
84 SkIRect grid; | 105 SkIRect grid; |
85 this->userToGrid(bounds, &grid); | 106 this->userToGrid(bounds, &grid); |
86 | 107 |
87 for (int y = grid.fTop; y < grid.fBottom; y++) { | 108 // This is just a loop over y then x. This compiles to a slightly faster an d |
88 for (int x = grid.fLeft; x < grid.fRight; x++) { | 109 // more compact loop than if we just did fTiles[y * fXTiles + x].push(opInde x). |
89 fTiles[y * fXTiles + x].push(opIndex); | 110 SkTDArray<unsigned>* row = &fTiles[grid.fTop * fXTiles + grid.fLeft]; |
111 for (int y = 0; y <= grid.fBottom - grid.fTop; y++) { | |
112 SkTDArray<unsigned>* tile = row; | |
113 for (int x = 0; x <= grid.fRight - grid.fLeft; x++) { | |
114 (tile++)->push(opIndex); | |
90 } | 115 } |
116 row += fXTiles; | |
91 } | 117 } |
92 } | 118 } |
93 | 119 |
94 // Number of tiles for which data is allocated on the stack in | 120 // Number of tiles for which data is allocated on the stack in |
95 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider | 121 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider |
96 // increasing this number. Typical large web page, say 2k x 16k, would | 122 // increasing this number. Typical large web page, say 2k x 16k, would |
97 // require 512 tiles of size 256 x 256 pixels. | 123 // require 512 tiles of size 256 x 256 pixels. |
98 static const int kStackAllocationTileCount = 1024; | 124 static const int kStackAllocationTileCount = 1024; |
99 | 125 |
126 | |
100 void SkTileGrid::search(const SkRect& originalQuery, SkTDArray<unsigned>* result s) const { | 127 void SkTileGrid::search(const SkRect& originalQuery, SkTDArray<unsigned>* result s) const { |
101 // The inset counteracts the outset that applied in 'insert', which optimize s | 128 // The .subtract(fMargin) counteracts the .add(fMargin) applied in insert(), |
102 // for lookups of size 'tileInterval + 2 * margin' (aligned with the tile gr id). | 129 // which optimizes for lookups of size tileInterval + 2 * margin (aligned wi th the tile grid). |
103 SkRect query = originalQuery; | 130 Sk4f query = sorted(Sk4f(&originalQuery.fLeft).subtract(fMargin).add(fOffset )); |
104 query.inset(fMarginWidth, fMarginHeight); | |
105 this->commonAdjust(&query); | |
106 | 131 |
107 // The inset may have inverted the rectangle, so sort(). | |
108 // TODO(mtklein): It looks like we only end up with inverted bounds in unit tests | |
109 // that make explicitly inverted queries, not from insetting. If we can dro p support for | |
110 // unsorted bounds (i.e. we don't see them outside unit tests), I think we c an drop this. | |
111 query.sort(); | |
112 | |
113 // No intersection check. We optimize for queries that are in bounds. | |
114 // We're safe anyway: userToGrid() will clamp out-of-bounds queries to neare st tile. | |
115 SkIRect grid; | 132 SkIRect grid; |
116 this->userToGrid(query, &grid); | 133 this->userToGrid(query, &grid); |
117 | 134 |
118 const int tilesHit = (grid.fRight - grid.fLeft) * (grid.fBottom - grid.fTop) ; | 135 const int tilesHit = (grid.fRight - grid.fLeft + 1) * (grid.fBottom - grid.f Top + 1); |
reed1
2014/10/15 20:48:11
I see a +1 here, and a change from < to <= down be
mtklein
2014/10/15 20:55:28
Ah, yeah, you're watching work in progress. That
| |
119 SkASSERT(tilesHit > 0); | 136 SkASSERT(tilesHit > 0); |
120 | 137 |
121 if (tilesHit == 1) { | 138 if (tilesHit == 1) { |
122 // A performance shortcut. The merging code below would work fine here too. | 139 // A performance shortcut. The merging code below would work fine here too. |
123 *results = fTiles[grid.fTop * fXTiles + grid.fLeft]; | 140 *results = fTiles[grid.fTop * fXTiles + grid.fLeft]; |
124 return; | 141 return; |
125 } | 142 } |
126 | 143 |
127 // We've got to merge the data in many tiles into a single sorted and dedupl icated stream. | 144 // We've got to merge the data in many tiles into a single sorted and dedupl icated stream. |
128 // We do a simple k-way merge based on the value of opIndex. | 145 // We do a simple k-way merge based on the value of opIndex. |
129 | 146 |
130 // Gather pointers to the starts and ends of the tiles to merge. | 147 // Gather pointers to the starts and ends of the tiles to merge. |
131 SkAutoSTArray<kStackAllocationTileCount, const unsigned*> starts(tilesHit), ends(tilesHit); | 148 SkAutoSTArray<kStackAllocationTileCount, const unsigned*> starts(tilesHit), ends(tilesHit); |
132 int i = 0; | 149 int i = 0; |
133 for (int y = grid.fTop; y < grid.fBottom; y++) { | 150 for (int y = grid.fTop; y <= grid.fBottom; y++) { |
134 for (int x = grid.fLeft; x < grid.fRight; x++) { | 151 for (int x = grid.fLeft; x <= grid.fRight; x++) { |
135 starts[i] = fTiles[y * fXTiles + x].begin(); | 152 starts[i] = fTiles[y * fXTiles + x].begin(); |
136 ends[i] = fTiles[y * fXTiles + x].end(); | 153 ends[i] = fTiles[y * fXTiles + x].end(); |
137 i++; | 154 i++; |
138 } | 155 } |
139 } | 156 } |
140 | 157 |
141 // Merge tiles into results until they're fully consumed. | 158 // Merge tiles into results until they're fully consumed. |
142 results->reset(); | 159 results->reset(); |
143 while (true) { | 160 while (true) { |
144 // The tiles themselves are already ordered, so the earliest op is at th e front of some | 161 // The tiles themselves are already ordered, so the earliest op is at th e front of some |
(...skipping 13 matching lines...) Expand all Loading... | |
158 // We did find an earliest op. Output it, and step forward every tile th at contains it. | 175 // We did find an earliest op. Output it, and step forward every tile th at contains it. |
159 results->push(earliest); | 176 results->push(earliest); |
160 for (int i = 0; i < starts.count(); i++) { | 177 for (int i = 0; i < starts.count(); i++) { |
161 if (starts[i] < ends[i] && *starts[i] == earliest) { | 178 if (starts[i] < ends[i] && *starts[i] == earliest) { |
162 starts[i]++; | 179 starts[i]++; |
163 } | 180 } |
164 } | 181 } |
165 } | 182 } |
166 } | 183 } |
167 | 184 |
OLD | NEW |