Chromium Code Reviews| 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 |
|
robertphillips
2014/10/02 12:27:35
data -> opIndex everywhere ?
mtklein
2014/10/02 14:32:09
Done.
| |
| 26 void SkTileGrid::insert(void* data, const SkRect& fbounds, bool) { | 26 void SkTileGrid::insert(unsigned data, 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(data); |
| 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 const SkTDArray<unsigned>& tile = fTiles[startY * fXTiles + startX]; |
| 106 results->setCount(tile.count()); | 105 results->setCount(tile.count()); |
|
robertphillips
2014/10/02 12:27:35
*results = tile; ?
mtklein
2014/10/02 14:32:09
Done.
| |
| 107 for (int i = 0; i < tile.count(); i++) { | 106 memcpy(results->begin(), tile.begin(), tile.count() * sizeof(unsigned)); |
| 108 (*results)[i] = tile[i].data; | |
| 109 } | |
| 110 return; | 107 return; |
| 111 } | 108 } |
| 112 | 109 |
| 113 // We've got to merge the data in many tiles into a single sorted and dedupl icated stream. | 110 // 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. | 111 // We do a simple k-way merge based on the order the data was inserted. |
| 115 | 112 |
| 116 // Gather pointers to the starts and ends of the tiles to merge. | 113 // Gather pointers to the starts and ends of the tiles to merge. |
| 117 SkAutoSTArray<kStackAllocationTileCount, const Entry*> starts(tilesHit), end s(tilesHit); | 114 SkAutoSTArray<kStackAllocationTileCount, const unsigned*> starts(tilesHit), ends(tilesHit); |
| 118 int i = 0; | 115 int i = 0; |
| 119 for (int x = startX; x < endX; x++) { | 116 for (int x = startX; x < endX; x++) { |
| 120 for (int y = startY; y < endY; y++) { | 117 for (int y = startY; y < endY; y++) { |
| 121 starts[i] = fTiles[y * fXTiles + x].begin(); | 118 starts[i] = fTiles[y * fXTiles + x].begin(); |
| 122 ends[i] = fTiles[y * fXTiles + x].end(); | 119 ends[i] = fTiles[y * fXTiles + x].end(); |
| 123 i++; | 120 i++; |
| 124 } | 121 } |
| 125 } | 122 } |
| 126 | 123 |
| 127 // Merge tiles into results until they're fully consumed. | 124 // Merge tiles into results until they're fully consumed. |
| 128 results->reset(); | 125 results->reset(); |
| 129 while (true) { | 126 while (true) { |
| 130 // The tiles themselves are already ordered, so the earliest is at the f ront of some tile. | 127 // 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. | 128 // It may be at the front of several, even all, tiles. |
|
robertphillips
2014/10/02 12:27:35
It seems like earliest shouldn't be a pointer anym
mtklein
2014/10/02 14:32:09
Agreed, though I'm going to follow up on this one.
| |
| 132 const Entry* earliest = NULL; | 129 const unsigned* earliest = NULL; |
| 133 for (int i = 0; i < starts.count(); i++) { | 130 for (int i = 0; i < starts.count(); i++) { |
| 134 if (starts[i] < ends[i]) { | 131 if (starts[i] < ends[i]) { |
| 135 if (NULL == earliest || starts[i]->order < earliest->order) { | 132 if (NULL == earliest || *starts[i]< *earliest) { |
| 136 earliest = starts[i]; | 133 earliest = starts[i]; |
| 137 } | 134 } |
| 138 } | 135 } |
| 139 } | 136 } |
| 140 | 137 |
| 141 // If we didn't find an earliest entry, there isn't anything left to mer ge. | 138 // If we didn't find an earliest entry, there isn't anything left to mer ge. |
| 142 if (NULL == earliest) { | 139 if (NULL == earliest) { |
| 143 return; | 140 return; |
| 144 } | 141 } |
| 145 | 142 |
| 146 // We did find an earliest entry. Output it, and step forward every tile that contains it. | 143 // We did find an earliest entry. Output it, and step forward every tile that contains it. |
| 147 results->push(earliest->data); | 144 results->push(*earliest); |
| 148 for (int i = 0; i < starts.count(); i++) { | 145 for (int i = 0; i < starts.count(); i++) { |
| 149 if (starts[i] < ends[i] && starts[i]->order == earliest->order) { | 146 if (starts[i] < ends[i] && *starts[i] == *earliest) { |
| 150 starts[i]++; | 147 starts[i]++; |
| 151 } | 148 } |
| 152 } | 149 } |
| 153 } | 150 } |
| 154 } | 151 } |
| 155 | 152 |
| 156 void SkTileGrid::clear() { | 153 void SkTileGrid::clear() { |
| 157 for (int i = 0; i < fXTiles * fYTiles; i++) { | 154 for (int i = 0; i < fXTiles * fYTiles; i++) { |
| 158 fTiles[i].reset(); | 155 fTiles[i].reset(); |
| 159 } | 156 } |
| 160 } | 157 } |
| 161 | 158 |
| 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 |