Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: src/core/SkTileGrid.cpp

Issue 845623002: Remove SkTileGrid (except for TileGridInfo). (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: kill gridSupported Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkTileGrid.h ('k') | tests/PictureBBHTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkTileGrid.h"
9
10 SkTileGrid::SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGrid Info& info)
11 : fXTiles(xTiles)
12 , fYTiles(yTiles)
13 , fInvWidth( SkScalarInvert(info.fTileInterval.width()))
14 , fInvHeight(SkScalarInvert(info.fTileInterval.height()))
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.
17 , fOffset(SkPoint::Make(info.fOffset.fX, info.fOffset.fY))
18 , fGridBounds(SkRect::MakeWH(xTiles * info.fTileInterval.width(),
19 yTiles * info.fTileInterval.height()))
20 , fTiles(SkNEW_ARRAY(SkTDArray<unsigned>, xTiles * yTiles)) {}
21
22 SkTileGrid::~SkTileGrid() {
23 SkDELETE_ARRAY(fTiles);
24 }
25
26 void SkTileGrid::reserve(int opCount) {
27 if (fXTiles * fYTiles == 0) {
28 return; // A tileless tile grid is nonsensical, but happens in at least cc_unittests.
29 }
30
31 // If we assume every op we're about to try to insert() falls within our gri d bounds,
32 // then every op has to hit at least one tile. In fact, a quick scan over o ur small
33 // SKP set shows that in the average SKP, each op hits two 256x256 tiles.
34
35 // If we take those observations and further assume the ops are distributed evenly
36 // across the picture, we get this guess for number of ops per tile:
37 const int opsPerTileGuess = (2 * opCount) / (fXTiles * fYTiles);
38
39 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles ); tile++) {
40 tile->setReserve(opsPerTileGuess);
41 }
42
43 // In practice, this heuristic means we'll temporarily allocate about 30% mo re bytes
44 // than if we made no setReserve() calls, but time spent in insert() drops b y about 50%.
45 }
46
47 void SkTileGrid::shrinkToFit() {
48 for (SkTDArray<unsigned>* tile = fTiles; tile != fTiles + (fXTiles * fYTiles ); tile++) {
49 tile->shrinkToFit();
50 }
51 }
52
53 // Adjustments to user-provided bounds common to both insert() and search().
54 // Call this after making insert- or search- specific adjustments.
55 void SkTileGrid::commonAdjust(SkRect* rect) const {
56 // Apply our offset.
57 rect->offset(fOffset);
58
59 // Scrunch the bounds in just a little to make the right and bottom edges
60 // exclusive. We want bounds of exactly one tile to hit exactly one tile.
61 rect->fRight -= SK_ScalarNearlyZero;
62 rect->fBottom -= SK_ScalarNearlyZero;
63 }
64
65 // Convert user-space bounds to grid tiles they cover (LT and RB both inclusive) .
66 void SkTileGrid::userToGrid(const SkRect& user, SkIRect* grid) const {
67 grid->fLeft = SkPin32(user.left() * fInvWidth , 0, fXTiles - 1);
68 grid->fTop = SkPin32(user.top() * fInvHeight, 0, fYTiles - 1);
69 grid->fRight = SkPin32(user.right() * fInvWidth , 0, fXTiles - 1);
70 grid->fBottom = SkPin32(user.bottom() * fInvHeight, 0, fYTiles - 1);
71 }
72
73 void SkTileGrid::insert(SkAutoTMalloc<SkRect>* boundsArray, int N) {
74 this->reserve(N);
75
76 for (int i = 0; i < N; i++) {
77 SkRect bounds = (*boundsArray)[i];
78 bounds.outset(fMarginWidth, fMarginHeight);
79 this->commonAdjust(&bounds);
80
81 // TODO(mtklein): can we assert this instead to save an intersection in Release mode,
82 // or just allow out-of-bound insertions to insert anyway (clamped to ne arest tile)?
83 if (!SkRect::Intersects(bounds, fGridBounds)) {
84 continue;
85 }
86
87 SkIRect grid;
88 this->userToGrid(bounds, &grid);
89
90 // This is just a loop over y then x. This compiles to a slightly faste r and
91 // more compact loop than if we just did fTiles[y * fXTiles + x].push(i) .
92 SkTDArray<unsigned>* row = &fTiles[grid.fTop * fXTiles + grid.fLeft];
93 for (int y = 0; y <= grid.fBottom - grid.fTop; y++) {
94 SkTDArray<unsigned>* tile = row;
95 for (int x = 0; x <= grid.fRight - grid.fLeft; x++) {
96 (tile++)->push(i);
97 }
98 row += fXTiles;
99 }
100 }
101 this->shrinkToFit();
102 }
103
104 // Number of tiles for which data is allocated on the stack in
105 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider
106 // increasing this number. Typical large web page, say 2k x 16k, would
107 // require 512 tiles of size 256 x 256 pixels.
108 static const int kStackAllocationTileCount = 1024;
109
110 void SkTileGrid::search(const SkRect& originalQuery, SkTDArray<unsigned>* result s) const {
111 // The inset counteracts the outset that applied in 'insert', which optimize s
112 // for lookups of size 'tileInterval + 2 * margin' (aligned with the tile gr id).
113 SkRect query = originalQuery;
114 query.inset(fMarginWidth, fMarginHeight);
115 this->commonAdjust(&query);
116
117 // The inset may have inverted the rectangle, so sort().
118 // TODO(mtklein): It looks like we only end up with inverted bounds in unit tests
119 // that make explicitly inverted queries, not from insetting. If we can dro p support for
120 // unsorted bounds (i.e. we don't see them outside unit tests), I think we c an drop this.
121 query.sort();
122
123 // No intersection check. We optimize for queries that are in bounds.
124 // We're safe anyway: userToGrid() will clamp out-of-bounds queries to neare st tile.
125 SkIRect grid;
126 this->userToGrid(query, &grid);
127
128 const int tilesHit = (grid.fRight - grid.fLeft + 1) * (grid.fBottom - grid.f Top + 1);
129 SkASSERT(tilesHit > 0);
130
131 if (tilesHit == 1) {
132 // A performance shortcut. The merging code below would work fine here too.
133 *results = fTiles[grid.fTop * fXTiles + grid.fLeft];
134 return;
135 }
136
137 // We've got to merge the data in many tiles into a single sorted and dedupl icated stream.
138 // We do a simple k-way merge based on the value of opIndex.
139
140 // Gather pointers to the starts and ends of the tiles to merge.
141 SkAutoSTArray<kStackAllocationTileCount, const unsigned*> starts(tilesHit), ends(tilesHit);
142 int i = 0;
143 for (int y = grid.fTop; y <= grid.fBottom; y++) {
144 for (int x = grid.fLeft; x <= grid.fRight; x++) {
145 starts[i] = fTiles[y * fXTiles + x].begin();
146 ends[i] = fTiles[y * fXTiles + x].end();
147 i++;
148 }
149 }
150
151 // Merge tiles into results until they're fully consumed.
152 results->reset();
153 while (true) {
154 // The tiles themselves are already ordered, so the earliest op is at th e front of some
155 // tile. It may be at the front of several, even all, tiles.
156 unsigned earliest = SK_MaxU32;
157 for (int i = 0; i < starts.count(); i++) {
158 if (starts[i] < ends[i]) {
159 earliest = SkTMin(earliest, *starts[i]);
160 }
161 }
162
163 // If we didn't find an earliest op, there isn't anything left to merge.
164 if (SK_MaxU32 == earliest) {
165 return;
166 }
167
168 // We did find an earliest op. Output it, and step forward every tile th at contains it.
169 results->push(earliest);
170 for (int i = 0; i < starts.count(); i++) {
171 if (starts[i] < ends[i] && *starts[i] == earliest) {
172 starts[i]++;
173 }
174 }
175 }
176 }
177
178 size_t SkTileGrid::bytesUsed() const {
179 size_t byteCount = sizeof(SkTileGrid);
180
181 size_t opCount = 0;
182 for (int i = 0; i < fXTiles * fYTiles; i++) {
183 opCount += fTiles[i].reserved();
184 }
185 byteCount += opCount * sizeof(unsigned);
186
187 return byteCount;
188 }
OLDNEW
« no previous file with comments | « src/core/SkTileGrid.h ('k') | tests/PictureBBHTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698