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 , fInvWidth( SkScalarInvert(info.fTileInterval.width())) | 13 , fInvWidth( SkScalarInvert(info.fTileInterval.width())) |
14 , fInvHeight(SkScalarInvert(info.fTileInterval.height())) | 14 , fInvHeight(SkScalarInvert(info.fTileInterval.height())) |
15 , fMarginWidth (info.fMargin.fWidth +1) // Margin is offset by 1 as a provi
sion for AA and | 15 , fMarginWidth (info.fMargin.fWidth +1) // Margin is offset by 1 as a provi
sion for AA and |
16 , fMarginHeight(info.fMargin.fHeight+1) // to cancel the outset applied by
getClipDeviceBounds. | 16 , fMarginHeight(info.fMargin.fHeight+1) // to cancel the outset applied by
getClipDeviceBounds. |
17 , fOffset(SkPoint::Make(info.fOffset.fX, info.fOffset.fY)) | 17 , fOffset(SkPoint::Make(info.fOffset.fX, info.fOffset.fY)) |
18 , fGridBounds(SkRect::MakeWH(xTiles * info.fTileInterval.width(), | 18 , fGridBounds(SkRect::MakeWH(xTiles * info.fTileInterval.width(), |
19 yTiles * info.fTileInterval.height())) | 19 yTiles * info.fTileInterval.height())) |
20 , fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) { | 20 , fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) {} |
21 SkASSERT(fXTiles * fYTiles != 0); | |
22 } | |
23 | 21 |
24 SkTileGrid::~SkTileGrid() { | 22 SkTileGrid::~SkTileGrid() { |
25 SkDELETE_ARRAY(fTiles); | 23 SkDELETE_ARRAY(fTiles); |
26 } | 24 } |
27 | 25 |
28 void SkTileGrid::reserve(unsigned opCount) { | |
29 // If we assume every op we're about to try to insert() falls within our gri
d bounds, | |
30 // then every op has to hit at least one tile. In fact, a quick scan over o
ur small | |
31 // SKP set shows that in the average SKP, each op hits two 256x256 tiles. | |
32 | |
33 // If we take those observations and further assume the ops are distributed
evenly | |
34 // across the picture, we get this guess for number of ops per tile: | |
35 const int opsPerTileGuess = (2 * opCount) / (fXTiles * fYTiles); | |
36 | |
37 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles
); tile++) { | |
38 tile->setReserve(opsPerTileGuess); | |
39 } | |
40 | |
41 // In practice, this heuristic means we'll temporarily allocate about 30% mo
re bytes | |
42 // than if we made no setReserve() calls, but time spent in insert() drops b
y about 50%. | |
43 } | |
44 | |
45 void SkTileGrid::flushDeferredInserts() { | |
46 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles
); tile++) { | |
47 tile->shrinkToFit(); | |
48 } | |
49 } | |
50 | |
51 // Adjustments to user-provided bounds common to both insert() and search(). | 26 // Adjustments to user-provided bounds common to both insert() and search(). |
52 // Call this after making insert- or search- specific adjustments. | 27 // Call this after making insert- or search- specific adjustments. |
53 void SkTileGrid::commonAdjust(SkRect* rect) const { | 28 void SkTileGrid::commonAdjust(SkRect* rect) const { |
54 // Apply our offset. | 29 // Apply our offset. |
55 rect->offset(fOffset); | 30 rect->offset(fOffset); |
56 | 31 |
57 // Scrunch the bounds in just a little to make the right and bottom edges | 32 // Scrunch the bounds in just a little to make the right and bottom edges |
58 // exclusive. We want bounds of exactly one tile to hit exactly one tile. | 33 // exclusive. We want bounds of exactly one tile to hit exactly one tile. |
59 rect->fRight -= SK_ScalarNearlyZero; | 34 rect->fRight -= SK_ScalarNearlyZero; |
60 rect->fBottom -= SK_ScalarNearlyZero; | 35 rect->fBottom -= SK_ScalarNearlyZero; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 // We did find an earliest op. Output it, and step forward every tile th
at contains it. | 131 // We did find an earliest op. Output it, and step forward every tile th
at contains it. |
157 results->push(earliest); | 132 results->push(earliest); |
158 for (int i = 0; i < starts.count(); i++) { | 133 for (int i = 0; i < starts.count(); i++) { |
159 if (starts[i] < ends[i] && *starts[i] == earliest) { | 134 if (starts[i] < ends[i] && *starts[i] == earliest) { |
160 starts[i]++; | 135 starts[i]++; |
161 } | 136 } |
162 } | 137 } |
163 } | 138 } |
164 } | 139 } |
165 | 140 |
OLD | NEW |