| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "SkTileGrid.h" | 9 #include "SkTileGrid.h" |
| 10 #include "SkPictureStateTree.h" | 10 #include "SkPictureStateTree.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 fYTileCount -1), 0); | 61 fYTileCount -1), 0); |
| 62 | 62 |
| 63 for (int x = minTileX; x <= maxTileX; x++) { | 63 for (int x = minTileX; x <= maxTileX; x++) { |
| 64 for (int y = minTileY; y <= maxTileY; y++) { | 64 for (int y = minTileY; y <= maxTileY; y++) { |
| 65 this->tile(x, y).push(data); | 65 this->tile(x, y).push(data); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 fInsertionCount++; | 68 fInsertionCount++; |
| 69 } | 69 } |
| 70 | 70 |
| 71 static void* next_datum(const SkTDArray<void*>** tileData, | 71 static int divide_ceil(int x, int y) { |
| 72 SkAutoSTArray<SkTileGrid::kStackAllocationTileCount, int
>& tileIndices) { | 72 return (x + y - 1) / y; |
| 73 SkPictureStateTree::Draw* minVal = NULL; | 73 } |
| 74 int tileCount = tileIndices.count(); | 74 |
| 75 int minIndex = tileCount; | 75 // Number of tiles for which data is allocated on the stack in |
| 76 int maxIndex = 0; | 76 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider |
| 77 // Find the next Datum; track where it's found so we reduce the size of the
second loop. | 77 // increasing this number. Typical large web page, say 2k x 16k, would |
| 78 for (int tile = 0; tile < tileCount; ++tile) { | 78 // require 512 tiles of size 256 x 256 pixels. |
| 79 int pos = tileIndices[tile]; | 79 static const int kStackAllocationTileCount = 1024; |
| 80 if (pos != SkTileGrid::kTileFinished) { | 80 |
| 81 SkPictureStateTree::Draw* candidate = (SkPictureStateTree::Draw*)(*t
ileData[tile])[pos]; | 81 void SkTileGrid::search(const SkIRect& query, SkTDArray<void*>* results) const { |
| 82 if (NULL == minVal || (*candidate) < (*minVal)) { | 82 SkIRect adjusted = query; |
| 83 minVal = candidate; | 83 |
| 84 minIndex = tile; | 84 // The inset is to counteract the outset that was applied in 'insert' |
| 85 maxIndex = tile; | 85 // The outset/inset is to optimize for lookups of size |
| 86 } else if (!((*minVal) < (*candidate))) { | 86 // 'tileInterval + 2 * margin' that are aligned with the tile grid. |
| 87 // We don't require operator==; if !(candidate<minVal) && !(minV
al<candidate), | 87 adjusted.inset(fInfo.fMargin.width(), fInfo.fMargin.height()); |
| 88 // candidate==minVal and we have to add this tile to the range s
earched. | 88 adjusted.offset(fInfo.fOffset); |
| 89 maxIndex = tile; | 89 adjusted.sort(); // in case the inset inverted the rectangle |
| 90 |
| 91 // Convert the query rectangle from device coordinates to tile coordinates |
| 92 // by rounding outwards to the nearest tile boundary so that the resulting t
ile |
| 93 // region includes the query rectangle. |
| 94 int startX = adjusted.left() / fInfo.fTileInterval.width(), |
| 95 startY = adjusted.top() / fInfo.fTileInterval.height(); |
| 96 int endX = divide_ceil(adjusted.right(), fInfo.fTileInterval.width()), |
| 97 endY = divide_ceil(adjusted.bottom(), fInfo.fTileInterval.height()); |
| 98 |
| 99 // Logically, we could pin endX to [startX, fXTileCount], but we force it |
| 100 // up to (startX, fXTileCount] to make sure we hit at least one tile. |
| 101 // This snaps just-out-of-bounds queries to the neighboring border tile. |
| 102 // I don't know if this is an important feature outside of unit tests. |
| 103 startX = SkPin32(startX, 0, fXTileCount - 1); |
| 104 startY = SkPin32(startY, 0, fYTileCount - 1); |
| 105 endX = SkPin32(endX, startX + 1, fXTileCount); |
| 106 endY = SkPin32(endY, startY + 1, fYTileCount); |
| 107 |
| 108 const int tilesHit = (endX - startX) * (endY - startY); |
| 109 SkASSERT(tilesHit > 0); |
| 110 |
| 111 if (tilesHit == 1) { |
| 112 // A performance shortcut. The merging code below would work fine here
too. |
| 113 *results = this->tile(startX, startY); |
| 114 return; |
| 115 } |
| 116 |
| 117 // We've got to merge the data in many tiles into a single sorted and dedupl
icated stream. |
| 118 // Each tile itself is already sorted (TODO: assert this while building) so
we just need to do |
| 119 // a simple k-way merge. |
| 120 |
| 121 // Gather pointers to the starts and ends of the tiles to merge. |
| 122 SkAutoSTArray<kStackAllocationTileCount, void**> tiles(tilesHit), ends(tiles
Hit); |
| 123 int i = 0; |
| 124 for (int x = startX; x < endX; x++) { |
| 125 for (int y = startY; y < endY; y++) { |
| 126 tiles[i] = fTileData[y * fXTileCount + x].begin(); |
| 127 ends[i] = fTileData[y * fXTileCount + x].end(); |
| 128 i++; |
| 129 } |
| 130 } |
| 131 |
| 132 // Merge tiles into results until they're fully consumed. |
| 133 results->reset(); |
| 134 while (true) { |
| 135 // The tiles themselves are already sorted, so the smallest datum is the
front of some tile. |
| 136 // It may be at the front of several, even all, tiles. |
| 137 SkPictureStateTree::Draw* smallest = NULL; |
| 138 for (int i = 0; i < tiles.count(); i++) { |
| 139 if (tiles[i] < ends[i]) { |
| 140 SkPictureStateTree::Draw* candidate = |
| 141 static_cast<SkPictureStateTree::Draw*>(*tiles[i]); |
| 142 if (NULL == smallest || (*candidate) < (*smallest)) { |
| 143 smallest = candidate; |
| 144 } |
| 145 } |
| 146 } |
| 147 |
| 148 // If we didn't find a smallest datum, there's nothing left to merge. |
| 149 if (NULL == smallest) { |
| 150 return; |
| 151 } |
| 152 |
| 153 // We did find a smallest datum. Output it, and step forward in every ti
le that contains it. |
| 154 results->push(smallest); |
| 155 for (int i = 0; i < tiles.count(); i++) { |
| 156 if (tiles[i] < ends[i] && *tiles[i] == smallest) { |
| 157 tiles[i]++; |
| 90 } | 158 } |
| 91 } | 159 } |
| 92 } | 160 } |
| 93 // Increment indices past the next datum | |
| 94 if (minVal != NULL) { | |
| 95 for (int tile = minIndex; tile <= maxIndex; ++tile) { | |
| 96 int pos = tileIndices[tile]; | |
| 97 if (pos != SkTileGrid::kTileFinished && (*tileData[tile])[pos] == mi
nVal) { | |
| 98 if (++(tileIndices[tile]) >= tileData[tile]->count()) { | |
| 99 tileIndices[tile] = SkTileGrid::kTileFinished; | |
| 100 } | |
| 101 } | |
| 102 } | |
| 103 return minVal; | |
| 104 } | |
| 105 return NULL; | |
| 106 } | 161 } |
| 107 | |
| 108 void SkTileGrid::search(const SkIRect& query, SkTDArray<void*>* results) const { | |
| 109 SkIRect adjustedQuery = query; | |
| 110 // The inset is to counteract the outset that was applied in 'insert' | |
| 111 // The outset/inset is to optimize for lookups of size | |
| 112 // 'tileInterval + 2 * margin' that are aligned with the tile grid. | |
| 113 adjustedQuery.inset(fInfo.fMargin.width(), fInfo.fMargin.height()); | |
| 114 adjustedQuery.offset(fInfo.fOffset); | |
| 115 adjustedQuery.sort(); // in case the inset inverted the rectangle | |
| 116 // Convert the query rectangle from device coordinates to tile coordinates | |
| 117 // by rounding outwards to the nearest tile boundary so that the resulting t
ile | |
| 118 // region includes the query rectangle. (using truncating division to "floor
") | |
| 119 int tileStartX = adjustedQuery.left() / fInfo.fTileInterval.width(); | |
| 120 int tileEndX = (adjustedQuery.right() + fInfo.fTileInterval.width() - 1) / | |
| 121 fInfo.fTileInterval.width(); | |
| 122 int tileStartY = adjustedQuery.top() / fInfo.fTileInterval.height(); | |
| 123 int tileEndY = (adjustedQuery.bottom() + fInfo.fTileInterval.height() - 1) / | |
| 124 fInfo.fTileInterval.height(); | |
| 125 | |
| 126 tileStartX = SkPin32(tileStartX, 0, fXTileCount - 1); | |
| 127 tileEndX = SkPin32(tileEndX, tileStartX+1, fXTileCount); | |
| 128 tileStartY = SkPin32(tileStartY, 0, fYTileCount - 1); | |
| 129 tileEndY = SkPin32(tileEndY, tileStartY+1, fYTileCount); | |
| 130 | |
| 131 int queryTileCount = (tileEndX - tileStartX) * (tileEndY - tileStartY); | |
| 132 SkASSERT(queryTileCount); | |
| 133 if (queryTileCount == 1) { | |
| 134 *results = this->tile(tileStartX, tileStartY); | |
| 135 } else { | |
| 136 results->reset(); | |
| 137 SkAutoSTArray<kStackAllocationTileCount, int> curPositions(queryTileCoun
t); | |
| 138 SkAutoSTArray<kStackAllocationTileCount, SkTDArray<void *>*> storage(que
ryTileCount); | |
| 139 const SkTDArray<void *>** tileRange = const_cast<const SkTDArray<void*>*
*>(storage.get()); | |
| 140 int tile = 0; | |
| 141 for (int x = tileStartX; x < tileEndX; ++x) { | |
| 142 for (int y = tileStartY; y < tileEndY; ++y) { | |
| 143 tileRange[tile] = &this->tile(x, y); | |
| 144 curPositions[tile] = tileRange[tile]->count() ? 0 : kTileFinishe
d; | |
| 145 ++tile; | |
| 146 } | |
| 147 } | |
| 148 while(void* nextElement = next_datum(tileRange, curPositions)) { | |
| 149 results->push(nextElement); | |
| 150 } | |
| 151 } | |
| 152 } | |
| 153 | 162 |
| 154 void SkTileGrid::clear() { | 163 void SkTileGrid::clear() { |
| 155 for (int i = 0; i < fTileCount; i++) { | 164 for (int i = 0; i < fTileCount; i++) { |
| 156 fTileData[i].reset(); | 165 fTileData[i].reset(); |
| 157 } | 166 } |
| 158 } | 167 } |
| 159 | 168 |
| 160 int SkTileGrid::getCount() const { | 169 int SkTileGrid::getCount() const { |
| 161 return fInsertionCount; | 170 return fInsertionCount; |
| 162 } | 171 } |
| 163 | 172 |
| 164 void SkTileGrid::rewindInserts() { | 173 void SkTileGrid::rewindInserts() { |
| 165 SkASSERT(fClient); | 174 SkASSERT(fClient); |
| 166 for (int i = 0; i < fTileCount; ++i) { | 175 for (int i = 0; i < fTileCount; ++i) { |
| 167 while (!fTileData[i].isEmpty() && fClient->shouldRewind(fTileData[i].top
())) { | 176 while (!fTileData[i].isEmpty() && fClient->shouldRewind(fTileData[i].top
())) { |
| 168 fTileData[i].pop(); | 177 fTileData[i].pop(); |
| 169 } | 178 } |
| 170 } | 179 } |
| 171 } | 180 } |
| OLD | NEW |