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 | 11 |
11 SkTileGrid::SkTileGrid(int xTileCount, int yTileCount, const SkTileGridFactory::
TileGridInfo& info, | 12 SkTileGrid::SkTileGrid(int xTileCount, int yTileCount, const SkTileGridFactory::
TileGridInfo& info) { |
12 SkTileGridNextDatumFunctionPtr nextDatumFunction) { | |
13 fXTileCount = xTileCount; | 13 fXTileCount = xTileCount; |
14 fYTileCount = yTileCount; | 14 fYTileCount = yTileCount; |
15 fInfo = info; | 15 fInfo = info; |
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 fTileCount = fXTileCount * fYTileCount; | 20 fTileCount = fXTileCount * fYTileCount; |
21 fInsertionCount = 0; | 21 fInsertionCount = 0; |
22 fGridBounds = SkIRect::MakeXYWH(0, 0, fInfo.fTileInterval.width() * fXTileCo
unt, | 22 fGridBounds = SkIRect::MakeXYWH(0, 0, fInfo.fTileInterval.width() * fXTileCo
unt, |
23 fInfo.fTileInterval.height() * fYTileCount); | 23 fInfo.fTileInterval.height() * fYTileCount); |
24 fNextDatumFunction = nextDatumFunction; | |
25 fTileData = SkNEW_ARRAY(SkTDArray<void *>, fTileCount); | 24 fTileData = SkNEW_ARRAY(SkTDArray<void *>, fTileCount); |
26 } | 25 } |
27 | 26 |
28 SkTileGrid::~SkTileGrid() { | 27 SkTileGrid::~SkTileGrid() { |
29 SkDELETE_ARRAY(fTileData); | 28 SkDELETE_ARRAY(fTileData); |
30 } | 29 } |
31 | 30 |
32 int SkTileGrid::tileCount(int x, int y) { | 31 int SkTileGrid::tileCount(int x, int y) { |
33 return this->tile(x, y).count(); | 32 return this->tile(x, y).count(); |
34 } | 33 } |
(...skipping 27 matching lines...) Expand all Loading... |
62 fYTileCount -1), 0); | 61 fYTileCount -1), 0); |
63 | 62 |
64 for (int x = minTileX; x <= maxTileX; x++) { | 63 for (int x = minTileX; x <= maxTileX; x++) { |
65 for (int y = minTileY; y <= maxTileY; y++) { | 64 for (int y = minTileY; y <= maxTileY; y++) { |
66 this->tile(x, y).push(data); | 65 this->tile(x, y).push(data); |
67 } | 66 } |
68 } | 67 } |
69 fInsertionCount++; | 68 fInsertionCount++; |
70 } | 69 } |
71 | 70 |
| 71 static void* next_datum(const SkTDArray<void*>** tileData, |
| 72 SkAutoSTArray<SkTileGrid::kStackAllocationTileCount, int
>& tileIndices) { |
| 73 SkPictureStateTree::Draw* minVal = NULL; |
| 74 int tileCount = tileIndices.count(); |
| 75 int minIndex = tileCount; |
| 76 int maxIndex = 0; |
| 77 // Find the next Datum; track where it's found so we reduce the size of the
second loop. |
| 78 for (int tile = 0; tile < tileCount; ++tile) { |
| 79 int pos = tileIndices[tile]; |
| 80 if (pos != SkTileGrid::kTileFinished) { |
| 81 SkPictureStateTree::Draw* candidate = (SkPictureStateTree::Draw*)(*t
ileData[tile])[pos]; |
| 82 if (NULL == minVal || (*candidate) < (*minVal)) { |
| 83 minVal = candidate; |
| 84 minIndex = tile; |
| 85 maxIndex = tile; |
| 86 } else if (!((*minVal) < (*candidate))) { |
| 87 // We don't require operator==; if !(candidate<minVal) && !(minV
al<candidate), |
| 88 // candidate==minVal and we have to add this tile to the range s
earched. |
| 89 maxIndex = tile; |
| 90 } |
| 91 } |
| 92 } |
| 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 } |
| 107 |
72 void SkTileGrid::search(const SkIRect& query, SkTDArray<void*>* results) const { | 108 void SkTileGrid::search(const SkIRect& query, SkTDArray<void*>* results) const { |
73 SkIRect adjustedQuery = query; | 109 SkIRect adjustedQuery = query; |
74 // The inset is to counteract the outset that was applied in 'insert' | 110 // The inset is to counteract the outset that was applied in 'insert' |
75 // The outset/inset is to optimize for lookups of size | 111 // The outset/inset is to optimize for lookups of size |
76 // 'tileInterval + 2 * margin' that are aligned with the tile grid. | 112 // 'tileInterval + 2 * margin' that are aligned with the tile grid. |
77 adjustedQuery.inset(fInfo.fMargin.width(), fInfo.fMargin.height()); | 113 adjustedQuery.inset(fInfo.fMargin.width(), fInfo.fMargin.height()); |
78 adjustedQuery.offset(fInfo.fOffset); | 114 adjustedQuery.offset(fInfo.fOffset); |
79 adjustedQuery.sort(); // in case the inset inverted the rectangle | 115 adjustedQuery.sort(); // in case the inset inverted the rectangle |
80 // Convert the query rectangle from device coordinates to tile coordinates | 116 // Convert the query rectangle from device coordinates to tile coordinates |
81 // by rounding outwards to the nearest tile boundary so that the resulting t
ile | 117 // by rounding outwards to the nearest tile boundary so that the resulting t
ile |
(...skipping 20 matching lines...) Expand all Loading... |
102 SkAutoSTArray<kStackAllocationTileCount, SkTDArray<void *>*> storage(que
ryTileCount); | 138 SkAutoSTArray<kStackAllocationTileCount, SkTDArray<void *>*> storage(que
ryTileCount); |
103 const SkTDArray<void *>** tileRange = const_cast<const SkTDArray<void*>*
*>(storage.get()); | 139 const SkTDArray<void *>** tileRange = const_cast<const SkTDArray<void*>*
*>(storage.get()); |
104 int tile = 0; | 140 int tile = 0; |
105 for (int x = tileStartX; x < tileEndX; ++x) { | 141 for (int x = tileStartX; x < tileEndX; ++x) { |
106 for (int y = tileStartY; y < tileEndY; ++y) { | 142 for (int y = tileStartY; y < tileEndY; ++y) { |
107 tileRange[tile] = &this->tile(x, y); | 143 tileRange[tile] = &this->tile(x, y); |
108 curPositions[tile] = tileRange[tile]->count() ? 0 : kTileFinishe
d; | 144 curPositions[tile] = tileRange[tile]->count() ? 0 : kTileFinishe
d; |
109 ++tile; | 145 ++tile; |
110 } | 146 } |
111 } | 147 } |
112 void *nextElement; | 148 while(void* nextElement = next_datum(tileRange, curPositions)) { |
113 while(NULL != (nextElement = fNextDatumFunction(tileRange, curPositions)
)) { | |
114 results->push(nextElement); | 149 results->push(nextElement); |
115 } | 150 } |
116 } | 151 } |
117 } | 152 } |
118 | 153 |
119 void SkTileGrid::clear() { | 154 void SkTileGrid::clear() { |
120 for (int i = 0; i < fTileCount; i++) { | 155 for (int i = 0; i < fTileCount; i++) { |
121 fTileData[i].reset(); | 156 fTileData[i].reset(); |
122 } | 157 } |
123 } | 158 } |
124 | 159 |
125 int SkTileGrid::getCount() const { | 160 int SkTileGrid::getCount() const { |
126 return fInsertionCount; | 161 return fInsertionCount; |
127 } | 162 } |
128 | 163 |
129 void SkTileGrid::rewindInserts() { | 164 void SkTileGrid::rewindInserts() { |
130 SkASSERT(fClient); | 165 SkASSERT(fClient); |
131 for (int i = 0; i < fTileCount; ++i) { | 166 for (int i = 0; i < fTileCount; ++i) { |
132 while (!fTileData[i].isEmpty() && fClient->shouldRewind(fTileData[i].top
())) { | 167 while (!fTileData[i].isEmpty() && fClient->shouldRewind(fTileData[i].top
())) { |
133 fTileData[i].pop(); | 168 fTileData[i].pop(); |
134 } | 169 } |
135 } | 170 } |
136 } | 171 } |
OLD | NEW |