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" |
| 10 |
| 11 // We scrunch the bounds in just a little to make the right and bottom edges |
| 12 // exclusive. We want bounds of exactly one tile to hit exactly one tile. |
| 13 static const Sk4f kScrunch = { 0, 0, SK_ScalarNearlyZero, SK_ScalarNearlyZero }; |
9 | 14 |
10 SkTileGrid::SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGrid
Info& info) | 15 SkTileGrid::SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGrid
Info& info) |
11 : fXTiles(xTiles) | 16 : fXTiles(xTiles) |
12 , fYTiles(yTiles) | 17 , fNumTiles(xTiles * yTiles) |
13 , fInvWidth( SkScalarInvert(info.fTileInterval.width())) | 18 , fGridBounds(SkLoad4f(0, |
14 , fInvHeight(SkScalarInvert(info.fTileInterval.height())) | 19 0, |
15 , fMarginWidth (info.fMargin.fWidth +1) // Margin is offset by 1 as a provi
sion for AA and | 20 xTiles * info.fTileInterval.width(), |
16 , fMarginHeight(info.fMargin.fHeight+1) // to cancel the outset applied by
getClipDeviceBounds. | 21 yTiles * info.fTileInterval.height())) |
17 , fOffset(SkPoint::Make(info.fOffset.fX, info.fOffset.fY)) | 22 , fMargin(SkLoad4f(-info.fMargin.fWidth - 1, // Margin is increased by 1 a
s a provision for AA |
18 , fGridBounds(SkRect::MakeWH(xTiles * info.fTileInterval.width(), | 23 -info.fMargin.fHeight - 1, // and to cancel the outset a
pplied by |
19 yTiles * info.fTileInterval.height())) | 24 +info.fMargin.fWidth + 1, // getClipDeviceBounds(). |
| 25 +info.fMargin.fHeight + 1)) |
| 26 , fOffset(SkLoad4f(info.fOffset.fX, info.fOffset.fY, info.fOffset.fX, info.f
Offset.fY) |
| 27 - kScrunch) |
| 28 , fScale(SkLoad4f(SkScalarInvert(info.fTileInterval.width()), |
| 29 SkScalarInvert(info.fTileInterval.height()), |
| 30 SkScalarInvert(info.fTileInterval.width()), |
| 31 SkScalarInvert(info.fTileInterval.height()))) |
| 32 , fHigh(SkLoad4f(fXTiles -1, yTiles - 1, fXTiles - 1, yTiles - 1)) |
20 , fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) {} | 33 , fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) {} |
21 | 34 |
22 SkTileGrid::~SkTileGrid() { | 35 SkTileGrid::~SkTileGrid() { |
23 SkDELETE_ARRAY(fTiles); | 36 SkDELETE_ARRAY(fTiles); |
24 } | 37 } |
25 | 38 |
26 void SkTileGrid::reserve(unsigned opCount) { | 39 void SkTileGrid::reserve(unsigned opCount) { |
27 if (fXTiles * fYTiles == 0) { | 40 if (fNumTiles == 0) { |
28 return; // A tileless tile grid is nonsensical, but happens in at least
cc_unittests. | 41 return; // A tileless tile grid is nonsensical, but happens in at least
cc_unittests. |
29 } | 42 } |
30 | 43 |
31 // If we assume every op we're about to try to insert() falls within our gri
d bounds, | 44 // 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 | 45 // 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. | 46 // SKP set shows that in the average SKP, each op hits two 256x256 tiles. |
34 | 47 |
35 // If we take those observations and further assume the ops are distributed
evenly | 48 // 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: | 49 // across the picture, we get this guess for number of ops per tile: |
37 const int opsPerTileGuess = (2 * opCount) / (fXTiles * fYTiles); | 50 const int opsPerTileGuess = (2 * opCount) / fNumTiles; |
38 | 51 |
39 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles
); tile++) { | 52 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + fNumTiles; tile++)
{ |
40 tile->setReserve(opsPerTileGuess); | 53 tile->setReserve(opsPerTileGuess); |
41 } | 54 } |
42 | 55 |
43 // In practice, this heuristic means we'll temporarily allocate about 30% mo
re bytes | 56 // 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%. | 57 // than if we made no setReserve() calls, but time spent in insert() drops b
y about 50%. |
45 } | 58 } |
46 | 59 |
47 void SkTileGrid::flushDeferredInserts() { | 60 void SkTileGrid::flushDeferredInserts() { |
48 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles
); tile++) { | 61 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + fNumTiles; tile++)
{ |
49 tile->shrinkToFit(); | 62 tile->shrinkToFit(); |
50 } | 63 } |
51 } | 64 } |
52 | 65 |
53 // Adjustments to user-provided bounds common to both insert() and search(). | 66 // Convert user-space bounds to grid tiles they cover (LT+RB both inclusive). |
54 // Call this after making insert- or search- specific adjustments. | 67 // Out of bounds queries are clamped to the single nearest tile. |
55 void SkTileGrid::commonAdjust(SkRect* rect) const { | 68 void SkTileGrid::userToGrid(const Sk4f& user, SkIRect* grid) const { |
56 // Apply our offset. | 69 static const Sk4f kZero = { 0, 0, 0, 0 }; |
57 rect->offset(fOffset); | |
58 | 70 |
59 // Scrunch the bounds in just a little to make the right and bottom edges | 71 Sk4f scaled = user * fScale; |
60 // exclusive. We want bounds of exactly one tile to hit exactly one tile. | 72 scaled = Sk4Max(scaled, kZero); |
61 rect->fRight -= SK_ScalarNearlyZero; | 73 scaled = Sk4Min(scaled, fHigh); |
62 rect->fBottom -= SK_ScalarNearlyZero; | 74 |
| 75 SkStore4(Sk4Convert<Sk4i>(scaled), &grid->fLeft); |
63 } | 76 } |
64 | 77 |
65 // Convert user-space bounds to grid tiles they cover (LT inclusive, RB exclusiv
e). | 78 // If the rect is inverted, sort it. |
66 void SkTileGrid::userToGrid(const SkRect& user, SkIRect* grid) const { | 79 static Sk4f sorted(const Sk4f& ltrb) { |
67 grid->fLeft = SkPin32(user.left() * fInvWidth , 0, fXTiles - 1); | 80 Sk4f rblt = Sk4Shuffle(ltrb, ltrb, 2, 3, 0, 1), |
68 grid->fTop = SkPin32(user.top() * fInvHeight, 0, fYTiles - 1); | 81 mins = Sk4Min(ltrb, rblt), |
69 grid->fRight = SkPin32(user.right() * fInvWidth , 0, fXTiles - 1) + 1; | 82 maxs = Sk4Max(ltrb, rblt), |
70 grid->fBottom = SkPin32(user.bottom() * fInvHeight, 0, fYTiles - 1) + 1; | 83 sort = Sk4Shuffle(mins, maxs, 0, 1, 4, 5); |
| 84 return sort; |
| 85 } |
| 86 |
| 87 static bool intersects(const Sk4f& a, const Sk4f& b) { |
| 88 // Two rects intersect if lefts are less than the |
| 89 // opposite rights and tops less than opposite bottoms. |
| 90 Sk4f lt = Sk4Shuffle(a, b, 0, 4, 1, 5), // a.L b.L a.T b.T < |
| 91 rb = Sk4Shuffle(a, b, 6, 2, 7, 3); // b.R a.R b.B a.B ? |
| 92 return Sk4All(lt < rb); |
71 } | 93 } |
72 | 94 |
73 void SkTileGrid::insert(unsigned opIndex, const SkRect& originalBounds, bool) { | 95 void SkTileGrid::insert(unsigned opIndex, const SkRect& originalBounds, bool) { |
74 SkRect bounds = originalBounds; | 96 Sk4f bounds = SkLoad4f(&originalBounds.fLeft) + fMargin + fOffset; |
75 bounds.outset(fMarginWidth, fMarginHeight); | 97 SkASSERT(Sk4All(sorted(bounds) == bounds)); |
76 this->commonAdjust(&bounds); | |
77 | 98 |
78 // TODO(mtklein): can we assert this instead to save an intersection in Rele
ase mode, | 99 // 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)? | 100 if (!intersects(bounds, fGridBounds)) { |
80 if (!SkRect::Intersects(bounds, fGridBounds)) { | |
81 return; | 101 return; |
82 } | 102 } |
83 | 103 |
84 SkIRect grid; | 104 SkIRect grid; |
85 this->userToGrid(bounds, &grid); | 105 this->userToGrid(bounds, &grid); |
86 | 106 |
87 for (int y = grid.fTop; y < grid.fBottom; y++) { | 107 SkTDArray<unsigned>* row = &fTiles[grid.fTop * fXTiles + grid.fLeft]; |
88 for (int x = grid.fLeft; x < grid.fRight; x++) { | 108 for (int y = 0; y <= grid.fBottom - grid.fTop; y++) { |
89 fTiles[y * fXTiles + x].push(opIndex); | 109 SkTDArray<unsigned>* tile = row; |
| 110 for (int x = 0; x <= grid.fRight - grid.fLeft; x++) { |
| 111 (tile++)->push(opIndex); |
90 } | 112 } |
| 113 row += fXTiles; |
91 } | 114 } |
92 } | 115 } |
93 | 116 |
94 // Number of tiles for which data is allocated on the stack in | 117 // Number of tiles for which data is allocated on the stack in |
95 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider | 118 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider |
96 // increasing this number. Typical large web page, say 2k x 16k, would | 119 // increasing this number. Typical large web page, say 2k x 16k, would |
97 // require 512 tiles of size 256 x 256 pixels. | 120 // require 512 tiles of size 256 x 256 pixels. |
98 static const int kStackAllocationTileCount = 1024; | 121 static const int kStackAllocationTileCount = 1024; |
99 | 122 |
| 123 |
100 void SkTileGrid::search(const SkRect& originalQuery, SkTDArray<unsigned>* result
s) const { | 124 void SkTileGrid::search(const SkRect& originalQuery, SkTDArray<unsigned>* result
s) const { |
101 // The inset counteracts the outset that applied in 'insert', which optimize
s | 125 // The "- fMargin" counteracts the "+ fMargin" that applied in insert(), whi
ch optimizes |
102 // for lookups of size 'tileInterval + 2 * margin' (aligned with the tile gr
id). | 126 // for lookups of size tileInterval + 2 * margin (aligned with the tile grid
). |
103 SkRect query = originalQuery; | 127 Sk4f query = sorted(SkLoad4f(&originalQuery.fLeft) - fMargin + fOffset); |
104 query.inset(fMarginWidth, fMarginHeight); | |
105 this->commonAdjust(&query); | |
106 | 128 |
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; | 129 SkIRect grid; |
116 this->userToGrid(query, &grid); | 130 this->userToGrid(query, &grid); |
117 | 131 |
118 const int tilesHit = (grid.fRight - grid.fLeft) * (grid.fBottom - grid.fTop)
; | 132 const int tilesHit = (grid.fRight - grid.fLeft + 1) * (grid.fBottom - grid.f
Top + 1); |
119 SkASSERT(tilesHit > 0); | 133 SkASSERT(tilesHit > 0); |
120 | 134 |
121 if (tilesHit == 1) { | 135 if (tilesHit == 1) { |
122 // A performance shortcut. The merging code below would work fine here
too. | 136 // A performance shortcut. The merging code below would work fine here
too. |
123 *results = fTiles[grid.fTop * fXTiles + grid.fLeft]; | 137 *results = fTiles[grid.fTop * fXTiles + grid.fLeft]; |
124 return; | 138 return; |
125 } | 139 } |
126 | 140 |
127 // We've got to merge the data in many tiles into a single sorted and dedupl
icated stream. | 141 // 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. | 142 // We do a simple k-way merge based on the value of opIndex. |
129 | 143 |
130 // Gather pointers to the starts and ends of the tiles to merge. | 144 // Gather pointers to the starts and ends of the tiles to merge. |
131 SkAutoSTArray<kStackAllocationTileCount, const unsigned*> starts(tilesHit),
ends(tilesHit); | 145 SkAutoSTArray<kStackAllocationTileCount, const unsigned*> starts(tilesHit),
ends(tilesHit); |
132 int i = 0; | 146 int i = 0; |
133 for (int y = grid.fTop; y < grid.fBottom; y++) { | 147 for (int y = grid.fTop; y <= grid.fBottom; y++) { |
134 for (int x = grid.fLeft; x < grid.fRight; x++) { | 148 for (int x = grid.fLeft; x <= grid.fRight; x++) { |
135 starts[i] = fTiles[y * fXTiles + x].begin(); | 149 starts[i] = fTiles[y * fXTiles + x].begin(); |
136 ends[i] = fTiles[y * fXTiles + x].end(); | 150 ends[i] = fTiles[y * fXTiles + x].end(); |
137 i++; | 151 i++; |
138 } | 152 } |
139 } | 153 } |
140 | 154 |
141 // Merge tiles into results until they're fully consumed. | 155 // Merge tiles into results until they're fully consumed. |
142 results->reset(); | 156 results->reset(); |
143 while (true) { | 157 while (true) { |
144 // The tiles themselves are already ordered, so the earliest op is at th
e front of some | 158 // The tiles themselves are already ordered, so the earliest op is at th
e front of some |
145 // tile. It may be at the front of several, even all, tiles. | 159 // tile. It may be at the front of several, even all, tiles. |
146 unsigned earliest = SK_MaxU32; | 160 unsigned earliest = SK_MaxU32; |
147 for (int i = 0; i < starts.count(); i++) { | 161 for (int i = 0; i < starts.count(); i++) { |
148 if (starts[i] < ends[i]) { | 162 if (starts[i] < ends[i]) { |
149 earliest = SkTMin(earliest, *starts[i]); | 163 earliest = SkTMin(earliest, *starts[i]); |
150 } | 164 } |
151 } | 165 } |
152 | 166 |
153 // If we didn't find an earliest op, there isn't anything left to merge. | 167 // If we didn't find an earliest op, there isn't anything left to merge. |
154 if (SK_MaxU32 == earliest) { | 168 if (SK_MaxU32 == earliest) { |
155 return; | 169 return; |
156 } | 170 } |
157 | 171 |
158 // We did find an earliest op. Output it, and step forward every tile th
at contains it. | 172 // We did find an earliest op. Output it, and step forward every tile th
at contains it. |
159 results->push(earliest); | 173 results->push(earliest); |
160 for (int i = 0; i < starts.count(); i++) { | 174 for (int i = 0; i < starts.count(); i++) { |
161 if (starts[i] < ends[i] && *starts[i] == earliest) { | 175 if (starts[i] < ends[i] && *starts[i] == earliest) { |
162 starts[i]++; | 176 starts[i]++; |
163 } | 177 } |
164 } | 178 } |
165 } | 179 } |
166 } | 180 } |
167 | 181 |
OLD | NEW |