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 | 9 |
10 SkTileGrid::SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGrid
Info& info) | 10 SkTileGrid::SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGrid
Info& info) |
11 : fXTiles(xTiles) | 11 : fXTiles(xTiles) |
12 , fYTiles(yTiles) | 12 , fYTiles(yTiles) |
13 , fInfo(info) | 13 , fInfo(info) |
14 , fCount(0) | 14 , fCount(0) |
15 , fTiles(SkNEW_ARRAY(SkTDArray<Entry>, xTiles * yTiles)) { | 15 , fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) { |
16 // Margin is offset by 1 as a provision for AA and | 16 // Margin is offset by 1 as a provision for AA and |
17 // to cancel-out the outset applied by getClipDeviceBounds. | 17 // to cancel-out the outset applied by getClipDeviceBounds. |
18 fInfo.fMargin.fHeight++; | 18 fInfo.fMargin.fHeight++; |
19 fInfo.fMargin.fWidth++; | 19 fInfo.fMargin.fWidth++; |
20 } | 20 } |
21 | 21 |
22 SkTileGrid::~SkTileGrid() { | 22 SkTileGrid::~SkTileGrid() { |
23 SkDELETE_ARRAY(fTiles); | 23 SkDELETE_ARRAY(fTiles); |
24 } | 24 } |
25 | 25 |
26 void SkTileGrid::insert(void* data, const SkRect& fbounds, bool) { | 26 void SkTileGrid::insert(unsigned opIndex, const SkRect& fbounds, bool) { |
27 SkASSERT(!fbounds.isEmpty()); | 27 SkASSERT(!fbounds.isEmpty()); |
28 SkIRect dilatedBounds; | 28 SkIRect dilatedBounds; |
29 if (fbounds.isLargest()) { | 29 if (fbounds.isLargest()) { |
30 // Dilating the largest SkIRect will overflow. Other nearly-largest rec
ts may overflow too, | 30 // Dilating the largest SkIRect will overflow. Other nearly-largest rec
ts may overflow too, |
31 // but we don't make active use of them like we do the largest. | 31 // but we don't make active use of them like we do the largest. |
32 dilatedBounds.setLargest(); | 32 dilatedBounds.setLargest(); |
33 } else { | 33 } else { |
34 fbounds.roundOut(&dilatedBounds); | 34 fbounds.roundOut(&dilatedBounds); |
35 dilatedBounds.outset(fInfo.fMargin.width(), fInfo.fMargin.height()); | 35 dilatedBounds.outset(fInfo.fMargin.width(), fInfo.fMargin.height()); |
36 dilatedBounds.offset(fInfo.fOffset); | 36 dilatedBounds.offset(fInfo.fOffset); |
37 } | 37 } |
38 | 38 |
39 const SkIRect gridBounds = | 39 const SkIRect gridBounds = |
40 { 0, 0, fInfo.fTileInterval.width() * fXTiles, fInfo.fTileInterval.heigh
t() * fYTiles }; | 40 { 0, 0, fInfo.fTileInterval.width() * fXTiles, fInfo.fTileInterval.heigh
t() * fYTiles }; |
41 if (!SkIRect::Intersects(dilatedBounds, gridBounds)) { | 41 if (!SkIRect::Intersects(dilatedBounds, gridBounds)) { |
42 return; | 42 return; |
43 } | 43 } |
44 | 44 |
45 // Note: SkIRects are non-inclusive of the right() column and bottom() row, | 45 // Note: SkIRects are non-inclusive of the right() column and bottom() row, |
46 // hence the "-1"s in the computations of maxX and maxY. | 46 // hence the "-1"s in the computations of maxX and maxY. |
47 int minX = SkMax32(0, SkMin32(dilatedBounds.left() / fInfo.fTileInterval.wid
th(), fXTiles - 1)); | 47 int minX = SkMax32(0, SkMin32(dilatedBounds.left() / fInfo.fTileInterval.wid
th(), fXTiles - 1)); |
48 int minY = SkMax32(0, SkMin32(dilatedBounds.top() / fInfo.fTileInterval.heig
ht(), fYTiles - 1)); | 48 int minY = SkMax32(0, SkMin32(dilatedBounds.top() / fInfo.fTileInterval.heig
ht(), fYTiles - 1)); |
49 int maxX = SkMax32(0, SkMin32((dilatedBounds.right() - 1) / fInfo.fTileInte
rval.width(), | 49 int maxX = SkMax32(0, SkMin32((dilatedBounds.right() - 1) / fInfo.fTileInte
rval.width(), |
50 fXTiles - 1)); | 50 fXTiles - 1)); |
51 int maxY = SkMax32(0, SkMin32((dilatedBounds.bottom() - 1) / fInfo.fTileInte
rval.height(), | 51 int maxY = SkMax32(0, SkMin32((dilatedBounds.bottom() - 1) / fInfo.fTileInte
rval.height(), |
52 fYTiles - 1)); | 52 fYTiles - 1)); |
53 | 53 |
54 Entry entry = { fCount++, data }; | |
55 for (int y = minY; y <= maxY; y++) { | 54 for (int y = minY; y <= maxY; y++) { |
56 for (int x = minX; x <= maxX; x++) { | 55 for (int x = minX; x <= maxX; x++) { |
57 fTiles[y * fXTiles + x].push(entry); | 56 fTiles[y * fXTiles + x].push(opIndex); |
58 } | 57 } |
59 } | 58 } |
60 } | 59 } |
61 | 60 |
62 static int divide_ceil(int x, int y) { | 61 static int divide_ceil(int x, int y) { |
63 return (x + y - 1) / y; | 62 return (x + y - 1) / y; |
64 } | 63 } |
65 | 64 |
66 // Number of tiles for which data is allocated on the stack in | 65 // Number of tiles for which data is allocated on the stack in |
67 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider | 66 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider |
68 // increasing this number. Typical large web page, say 2k x 16k, would | 67 // increasing this number. Typical large web page, say 2k x 16k, would |
69 // require 512 tiles of size 256 x 256 pixels. | 68 // require 512 tiles of size 256 x 256 pixels. |
70 static const int kStackAllocationTileCount = 1024; | 69 static const int kStackAllocationTileCount = 1024; |
71 | 70 |
72 void SkTileGrid::search(const SkRect& query, SkTDArray<void*>* results) const { | 71 void SkTileGrid::search(const SkRect& query, SkTDArray<unsigned>* results) const
{ |
73 SkIRect adjusted; | 72 SkIRect adjusted; |
74 query.roundOut(&adjusted); | 73 query.roundOut(&adjusted); |
75 | 74 |
76 // The inset is to counteract the outset that was applied in 'insert' | 75 // The inset is to counteract the outset that was applied in 'insert' |
77 // The outset/inset is to optimize for lookups of size | 76 // The outset/inset is to optimize for lookups of size |
78 // 'tileInterval + 2 * margin' that are aligned with the tile grid. | 77 // 'tileInterval + 2 * margin' that are aligned with the tile grid. |
79 adjusted.inset(fInfo.fMargin.width(), fInfo.fMargin.height()); | 78 adjusted.inset(fInfo.fMargin.width(), fInfo.fMargin.height()); |
80 adjusted.offset(fInfo.fOffset); | 79 adjusted.offset(fInfo.fOffset); |
81 adjusted.sort(); // in case the inset inverted the rectangle | 80 adjusted.sort(); // in case the inset inverted the rectangle |
82 | 81 |
(...skipping 12 matching lines...) Expand all Loading... |
95 startX = SkPin32(startX, 0, fXTiles - 1); | 94 startX = SkPin32(startX, 0, fXTiles - 1); |
96 startY = SkPin32(startY, 0, fYTiles - 1); | 95 startY = SkPin32(startY, 0, fYTiles - 1); |
97 endX = SkPin32(endX, startX + 1, fXTiles); | 96 endX = SkPin32(endX, startX + 1, fXTiles); |
98 endY = SkPin32(endY, startY + 1, fYTiles); | 97 endY = SkPin32(endY, startY + 1, fYTiles); |
99 | 98 |
100 const int tilesHit = (endX - startX) * (endY - startY); | 99 const int tilesHit = (endX - startX) * (endY - startY); |
101 SkASSERT(tilesHit > 0); | 100 SkASSERT(tilesHit > 0); |
102 | 101 |
103 if (tilesHit == 1) { | 102 if (tilesHit == 1) { |
104 // A performance shortcut. The merging code below would work fine here
too. | 103 // A performance shortcut. The merging code below would work fine here
too. |
105 const SkTDArray<Entry>& tile = fTiles[startY * fXTiles + startX]; | 104 *results = fTiles[startY * fXTiles + startX]; |
106 results->setCount(tile.count()); | |
107 for (int i = 0; i < tile.count(); i++) { | |
108 (*results)[i] = tile[i].data; | |
109 } | |
110 return; | 105 return; |
111 } | 106 } |
112 | 107 |
113 // We've got to merge the data in many tiles into a single sorted and dedupl
icated stream. | 108 // We've got to merge the data in many tiles into a single sorted and dedupl
icated stream. |
114 // We do a simple k-way merge based on the order the data was inserted. | 109 // We do a simple k-way merge based on the value of opIndex. |
115 | 110 |
116 // Gather pointers to the starts and ends of the tiles to merge. | 111 // Gather pointers to the starts and ends of the tiles to merge. |
117 SkAutoSTArray<kStackAllocationTileCount, const Entry*> starts(tilesHit), end
s(tilesHit); | 112 SkAutoSTArray<kStackAllocationTileCount, const unsigned*> starts(tilesHit),
ends(tilesHit); |
118 int i = 0; | 113 int i = 0; |
119 for (int x = startX; x < endX; x++) { | 114 for (int x = startX; x < endX; x++) { |
120 for (int y = startY; y < endY; y++) { | 115 for (int y = startY; y < endY; y++) { |
121 starts[i] = fTiles[y * fXTiles + x].begin(); | 116 starts[i] = fTiles[y * fXTiles + x].begin(); |
122 ends[i] = fTiles[y * fXTiles + x].end(); | 117 ends[i] = fTiles[y * fXTiles + x].end(); |
123 i++; | 118 i++; |
124 } | 119 } |
125 } | 120 } |
126 | 121 |
127 // Merge tiles into results until they're fully consumed. | 122 // Merge tiles into results until they're fully consumed. |
128 results->reset(); | 123 results->reset(); |
129 while (true) { | 124 while (true) { |
130 // The tiles themselves are already ordered, so the earliest is at the f
ront of some tile. | 125 // The tiles themselves are already ordered, so the earliest is at the f
ront of some tile. |
131 // It may be at the front of several, even all, tiles. | 126 // It may be at the front of several, even all, tiles. |
132 const Entry* earliest = NULL; | 127 const unsigned* earliest = NULL; |
133 for (int i = 0; i < starts.count(); i++) { | 128 for (int i = 0; i < starts.count(); i++) { |
134 if (starts[i] < ends[i]) { | 129 if (starts[i] < ends[i]) { |
135 if (NULL == earliest || starts[i]->order < earliest->order) { | 130 if (NULL == earliest || *starts[i]< *earliest) { |
136 earliest = starts[i]; | 131 earliest = starts[i]; |
137 } | 132 } |
138 } | 133 } |
139 } | 134 } |
140 | 135 |
141 // If we didn't find an earliest entry, there isn't anything left to mer
ge. | 136 // If we didn't find an earliest entry, there isn't anything left to mer
ge. |
142 if (NULL == earliest) { | 137 if (NULL == earliest) { |
143 return; | 138 return; |
144 } | 139 } |
145 | 140 |
146 // We did find an earliest entry. Output it, and step forward every tile
that contains it. | 141 // We did find an earliest entry. Output it, and step forward every tile
that contains it. |
147 results->push(earliest->data); | 142 results->push(*earliest); |
148 for (int i = 0; i < starts.count(); i++) { | 143 for (int i = 0; i < starts.count(); i++) { |
149 if (starts[i] < ends[i] && starts[i]->order == earliest->order) { | 144 if (starts[i] < ends[i] && *starts[i] == *earliest) { |
150 starts[i]++; | 145 starts[i]++; |
151 } | 146 } |
152 } | 147 } |
153 } | 148 } |
154 } | 149 } |
155 | 150 |
156 void SkTileGrid::clear() { | 151 void SkTileGrid::clear() { |
157 for (int i = 0; i < fXTiles * fYTiles; i++) { | 152 for (int i = 0; i < fXTiles * fYTiles; i++) { |
158 fTiles[i].reset(); | 153 fTiles[i].reset(); |
159 } | 154 } |
160 } | 155 } |
161 | 156 |
162 void SkTileGrid::rewindInserts() { | |
163 SkASSERT(fClient); | |
164 for (int i = 0; i < fXTiles * fYTiles; i++) { | |
165 while (!fTiles[i].isEmpty() && fClient->shouldRewind(fTiles[i].top().dat
a)) { | |
166 fTiles[i].pop(); | |
167 } | |
168 } | |
169 } | |
OLD | NEW |