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 "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkTileGrid.h" | 10 #include "SkTileGrid.h" |
11 #include "SkTileGridPicture.h" | 11 #include "SkTileGridPicture.h" |
12 #include "Test.h" | 12 #include "Test.h" |
13 | 13 |
14 enum Tile { | 14 enum Tile { |
15 kTopLeft_Tile = 0x1, | 15 kTopLeft_Tile = 0x1, |
16 kTopRight_Tile = 0x2, | 16 kTopRight_Tile = 0x2, |
17 kBottomLeft_Tile = 0x4, | 17 kBottomLeft_Tile = 0x4, |
18 kBottomRight_Tile = 0x8, | 18 kBottomRight_Tile = 0x8, |
19 | 19 |
20 kAll_Tile = kTopLeft_Tile | kTopRight_Tile | kBottomLeft_Tile | kBottomRight
_Tile, | 20 kAll_Tile = kTopLeft_Tile | kTopRight_Tile | kBottomLeft_Tile | kBottomRight
_Tile, |
21 }; | 21 }; |
22 | 22 |
23 class MockCanvas : public SkCanvas { | |
24 public: | |
25 MockCanvas(const SkBitmap& bm) : SkCanvas(bm) {} | |
26 | |
27 virtual void drawRect(const SkRect& rect, const SkPaint&) { | |
28 // This capture occurs before quick reject. | |
29 fRects.push(rect); | |
30 } | |
31 | |
32 SkTDArray<SkRect> fRects; | |
33 }; | |
34 | |
35 static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, | 23 static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, |
36 uint32_t tileMask, int borderPixels = 0) { | 24 uint32_t tileMask, int borderPixels = 0) { |
37 SkTileGridPicture::TileGridInfo info; | 25 SkTileGridPicture::TileGridInfo info; |
38 info.fMargin.set(borderPixels, borderPixels); | 26 info.fMargin.set(borderPixels, borderPixels); |
39 info.fOffset.setZero(); | 27 info.fOffset.setZero(); |
40 info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels); | 28 info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels); |
41 SkTileGrid grid(2, 2, info, NULL); | 29 SkTileGrid grid(2, 2, info, NULL); |
42 grid.insert(NULL, rect, false); | 30 grid.insert(NULL, rect, false); |
43 REPORTER_ASSERT(reporter, grid.tileCount(0, 0) == | 31 REPORTER_ASSERT(reporter, grid.tileCount(0, 0) == |
44 ((tileMask & kTopLeft_Tile)? 1 : 0)); | 32 ((tileMask & kTopLeft_Tile)? 1 : 0)); |
45 REPORTER_ASSERT(reporter, grid.tileCount(1, 0) == | 33 REPORTER_ASSERT(reporter, grid.tileCount(1, 0) == |
46 ((tileMask & kTopRight_Tile)? 1 : 0)); | 34 ((tileMask & kTopRight_Tile)? 1 : 0)); |
47 REPORTER_ASSERT(reporter, grid.tileCount(0, 1) == | 35 REPORTER_ASSERT(reporter, grid.tileCount(0, 1) == |
48 ((tileMask & kBottomLeft_Tile)? 1 : 0)); | 36 ((tileMask & kBottomLeft_Tile)? 1 : 0)); |
49 REPORTER_ASSERT(reporter, grid.tileCount(1, 1) == | 37 REPORTER_ASSERT(reporter, grid.tileCount(1, 1) == |
50 ((tileMask & kBottomRight_Tile)? 1 : 0)); | 38 ((tileMask & kBottomRight_Tile)? 1 : 0)); |
51 } | 39 } |
52 | 40 |
| 41 static SkIRect query(float x, float y, float w, float h) { |
| 42 // inflate for the margin++ in tilegrid |
| 43 SkRect bounds = SkRect::MakeXYWH(x, y, w, h); |
| 44 SkIRect r; |
| 45 bounds.roundOut(&r); |
| 46 r.outset(1, 1); // to counteract the inset in SkTileGrid::search |
| 47 return r; |
| 48 } |
| 49 |
| 50 |
53 DEF_TEST(TileGrid_UnalignedQuery, reporter) { | 51 DEF_TEST(TileGrid_UnalignedQuery, reporter) { |
54 // Use SkTileGridPicture to generate a SkTileGrid with a helper | |
55 SkTileGridPicture::TileGridInfo info; | 52 SkTileGridPicture::TileGridInfo info; |
56 info.fMargin.setEmpty(); | 53 info.fMargin.setEmpty(); |
57 info.fOffset.setZero(); | 54 info.fOffset.setZero(); |
58 info.fTileInterval.set(10, 10); | 55 info.fTileInterval.set(10, 10); |
59 SkTileGridPicture picture(20, 20, info); | 56 SkIRect rect1 = SkIRect::MakeXYWH(0, 0, 8, 8); |
60 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | 57 SkIRect rect2 = SkIRect::MakeXYWH(11, 11, 1, 1); |
61 SkIntToScalar(8), SkIntToScalar(8)); | 58 SkTileGrid grid(2, 2, info, SkTileGridNextDatum<SkPictureStateTree::Draw>); |
62 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(11), SkIntToScalar(11), | 59 grid.insert(&rect1, rect1, true); |
63 SkIntToScalar(1), SkIntToScalar(1)); | 60 grid.insert(&rect2, rect2, true); |
64 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForCli
ppedPlayback_RecordingFlag); | |
65 SkPaint paint; | |
66 canvas->drawRect(rect1, paint); | |
67 canvas->drawRect(rect2, paint); | |
68 picture.endRecording(); | |
69 | |
70 SkBitmap store; | |
71 store.allocN32Pixels(1, 1); | |
72 | 61 |
73 // Test parts of top-left tile | 62 // Test parts of top-left tile |
74 { | 63 { |
75 MockCanvas mockCanvas(store); | 64 SkTDArray<void*> rects; |
76 picture.draw(&mockCanvas); | 65 grid.search(query(0.0f, 0.0f, 1.0f, 1.0f), &rects); |
77 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 66 REPORTER_ASSERT(reporter, 1 == rects.count()); |
78 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 67 REPORTER_ASSERT(reporter, &rect1 == rects[0]); |
79 } | 68 } |
80 { | 69 { |
81 MockCanvas mockCanvas(store); | 70 SkTDArray<void*> rects; |
82 mockCanvas.translate(-7.99f, -7.99f); | 71 grid.search(query(7.99f, 7.99f, 1.0f, 1.0f), &rects); |
83 picture.draw(&mockCanvas); | 72 REPORTER_ASSERT(reporter, 1 == rects.count()); |
84 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 73 REPORTER_ASSERT(reporter, &rect1 == rects[0]); |
85 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | |
86 } | 74 } |
87 // Corner overlap | 75 // Corner overlap |
88 { | 76 { |
89 MockCanvas mockCanvas(store); | 77 SkTDArray<void*> rects; |
90 mockCanvas.translate(-9.5f, -9.5f); | 78 grid.search(query(9.5f, 9.5f, 1.0f, 1.0f), &rects); |
91 picture.draw(&mockCanvas); | 79 REPORTER_ASSERT(reporter, 2 == rects.count()); |
92 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); | 80 REPORTER_ASSERT(reporter, &rect1 == rects[0]); |
93 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 81 REPORTER_ASSERT(reporter, &rect2 == rects[1]); |
94 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); | |
95 } | 82 } |
96 // Intersect bottom right tile, but does not overlap rect 2 | 83 // Intersect bottom right tile, but does not overlap rect 2 |
97 { | 84 { |
98 MockCanvas mockCanvas(store); | 85 SkTDArray<void*> rects; |
99 mockCanvas.translate(-16.0f, -16.0f); | 86 grid.search(query(16.0f, 16.0f, 1.0f, 1.0f), &rects); |
100 picture.draw(&mockCanvas); | 87 REPORTER_ASSERT(reporter, 1 == rects.count()); |
101 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 88 REPORTER_ASSERT(reporter, &rect2 == rects[0]); |
102 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | |
103 } | 89 } |
104 // Out of bounds queries, snap to border tiles | 90 // Out of bounds queries, snap to border tiles |
105 { | 91 { |
106 MockCanvas mockCanvas(store); | 92 SkTDArray<void*> rects; |
107 mockCanvas.translate(2.0f, 0.0f); | 93 grid.search(query(-2.0f, 0.0f, 1.0f, 1.0f), &rects); |
108 picture.draw(&mockCanvas); | 94 REPORTER_ASSERT(reporter, 1 == rects.count()); |
109 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 95 REPORTER_ASSERT(reporter, &rect1 == rects[0]); |
110 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | |
111 } | 96 } |
112 { | 97 { |
113 MockCanvas mockCanvas(store); | 98 SkTDArray<void*> rects; |
114 mockCanvas.translate(0.0f, 2.0f); | 99 grid.search(query(0.0f, -2.0f, 1.0f, 1.0f), &rects); |
115 picture.draw(&mockCanvas); | 100 REPORTER_ASSERT(reporter, 1 == rects.count()); |
116 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 101 REPORTER_ASSERT(reporter, &rect1 == rects[0]); |
117 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | |
118 } | 102 } |
119 { | 103 { |
120 MockCanvas mockCanvas(store); | 104 SkTDArray<void*> rects; |
121 mockCanvas.translate(-22.0f, -16.0f); | 105 grid.search(query(22.0f, 16.0f, 1.0f, 1.0f), &rects); |
122 picture.draw(&mockCanvas); | 106 REPORTER_ASSERT(reporter, 1 == rects.count()); |
123 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 107 REPORTER_ASSERT(reporter, &rect2 == rects[0]); |
124 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | |
125 } | 108 } |
126 { | 109 { |
127 MockCanvas mockCanvas(store); | 110 SkTDArray<void*> rects; |
128 mockCanvas.translate(-16.0f, -22.0f); | 111 grid.search(query(16.0f, 22.0f, 1.0f, 1.0f), &rects); |
129 picture.draw(&mockCanvas); | 112 REPORTER_ASSERT(reporter, 1 == rects.count()); |
130 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 113 REPORTER_ASSERT(reporter, &rect2 == rects[0]); |
131 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | |
132 } | 114 } |
133 } | 115 } |
134 | 116 |
135 DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) { | 117 DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) { |
136 // Use SkTileGridPicture to generate a SkTileGrid with a helper | 118 // Use SkTileGridPicture to generate a SkTileGrid with a helper |
137 SkTileGridPicture::TileGridInfo info; | 119 SkTileGridPicture::TileGridInfo info; |
138 info.fMargin.set(1, 1); | 120 info.fMargin.set(1, 1); |
139 info.fOffset.set(-1, -1); | 121 info.fOffset.set(-1, -1); |
140 info.fTileInterval.set(8, 8); | 122 info.fTileInterval.set(8, 8); |
141 SkTileGridPicture picture(20, 20, info); | |
142 | 123 |
143 // rect landing entirely in top left tile | 124 // rect landing entirely in top left tile |
144 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | 125 SkIRect rect1 = SkIRect::MakeXYWH(0, 0, 1, 1); |
145 SkIntToScalar(1), SkIntToScalar(1)); | |
146 // rect landing entirely in center tile | 126 // rect landing entirely in center tile |
147 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(12), SkIntToScalar(12), | 127 SkIRect rect2 = SkIRect::MakeXYWH(12, 12, 1, 1); |
148 SkIntToScalar(1), SkIntToScalar(1)); | |
149 // rect landing entirely in bottomright tile | 128 // rect landing entirely in bottomright tile |
150 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(19), SkIntToScalar(19), | 129 SkIRect rect3 = SkIRect::MakeXYWH(19, 19, 1, 1); |
151 SkIntToScalar(1), SkIntToScalar(1)); | 130 SkTileGrid grid(3, 3, info, SkTileGridNextDatum<SkPictureStateTree::Draw>); |
152 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForCli
ppedPlayback_RecordingFlag); | 131 grid.insert(&rect1, rect1, true); |
153 SkPaint paint; | 132 grid.insert(&rect2, rect2, true); |
154 canvas->drawRect(rect1, paint); | 133 grid.insert(&rect3, rect3, true); |
155 canvas->drawRect(rect2, paint); | |
156 canvas->drawRect(rect3, paint); | |
157 picture.endRecording(); | |
158 | 134 |
159 SkBitmap tileBitmap; | |
160 tileBitmap.allocN32Pixels(10, 10); | |
161 SkBitmap moreThanATileBitmap; | |
162 moreThanATileBitmap.allocN32Pixels(11, 11); | |
163 SkBitmap tinyBitmap; | |
164 tinyBitmap.allocN32Pixels(2, 2); | |
165 // Test parts of top-left tile | 135 // Test parts of top-left tile |
166 { | 136 { |
167 // The offset should cancel the top and left borders of the top left til
e | 137 // The offset should cancel the top and left borders of the top left til
e |
168 // So a look-up at interval 0-10 should be grid aligned, | 138 // So a look-up at interval 0-10 should be grid aligned, |
169 MockCanvas mockCanvas(tileBitmap); | 139 SkTDArray<void*> rects; |
170 picture.draw(&mockCanvas); | 140 grid.search(query(0.0f, 0.0f, 10.0f, 10.0f), &rects); |
171 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 141 REPORTER_ASSERT(reporter, 1 == rects.count()); |
172 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 142 REPORTER_ASSERT(reporter, &rect1 == rects[0]); |
173 } | 143 } |
174 { | 144 { |
175 // Encroaching border by one pixel | 145 // Encroaching border by one pixel |
176 MockCanvas mockCanvas(moreThanATileBitmap); | 146 SkTDArray<void*> rects; |
177 picture.draw(&mockCanvas); | 147 grid.search(query(0.0f, 0.0f, 11.0f, 11.0f), &rects); |
178 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); | 148 REPORTER_ASSERT(reporter, 2 == rects.count()); |
179 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 149 REPORTER_ASSERT(reporter, &rect1 == rects[0]); |
180 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); | 150 REPORTER_ASSERT(reporter, &rect2 == rects[1]); |
181 } | 151 } |
182 { | 152 { |
183 // Tile stride is 8 (tileWidth - 2 * border pixels | 153 // Tile stride is 8 (tileWidth - 2 * border pixels |
184 // so translating by 8, should make query grid-aligned | 154 // so translating by 8, should make query grid-aligned |
185 // with middle tile. | 155 // with middle tile. |
186 MockCanvas mockCanvas(tileBitmap); | 156 SkTDArray<void*> rects; |
187 mockCanvas.translate(SkIntToScalar(-8), SkIntToScalar(-8)); | 157 grid.search(query(8.0f, 8.0f, 10.0f, 10.0f), &rects); |
188 picture.draw(&mockCanvas); | 158 REPORTER_ASSERT(reporter, 1 == rects.count()); |
189 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 159 REPORTER_ASSERT(reporter, &rect2 == rects[0]); |
190 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | |
191 } | 160 } |
192 { | 161 { |
193 MockCanvas mockCanvas(tileBitmap); | 162 SkTDArray<void*> rects; |
194 mockCanvas.translate(-7.9f, -7.9f); | 163 grid.search(query(7.9f, 7.9f, 10.0f, 10.0f), &rects); |
195 picture.draw(&mockCanvas); | 164 REPORTER_ASSERT(reporter, 2 == rects.count()); |
196 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); | 165 REPORTER_ASSERT(reporter, &rect1 == rects[0]); |
197 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 166 REPORTER_ASSERT(reporter, &rect2 == rects[1]); |
198 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); | |
199 } | 167 } |
200 { | 168 { |
201 MockCanvas mockCanvas(tileBitmap); | 169 SkTDArray<void*> rects; |
202 mockCanvas.translate(-8.1f, -8.1f); | 170 grid.search(query(8.1f, 8.1f, 10.0f, 10.0f), &rects); |
203 picture.draw(&mockCanvas); | 171 REPORTER_ASSERT(reporter, 2 == rects.count()); |
204 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); | 172 REPORTER_ASSERT(reporter, &rect2 == rects[0]); |
205 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | 173 REPORTER_ASSERT(reporter, &rect3 == rects[1]); |
206 REPORTER_ASSERT(reporter, rect3 == mockCanvas.fRects[1]); | |
207 } | 174 } |
208 { | 175 { |
209 // Regression test for crbug.com/234688 | 176 // Regression test for crbug.com/234688 |
210 // Once the 2x2 device region is inset by margin, it yields an empty | 177 // Once the 2x2 device region is inset by margin, it yields an empty |
211 // adjusted region, sitting right on top of the tile boundary. | 178 // adjusted region, sitting right on top of the tile boundary. |
212 MockCanvas mockCanvas(tinyBitmap); | 179 SkTDArray<void*> rects; |
213 mockCanvas.translate(-8.0f, -8.0f); | 180 grid.search(query(8.0f, 8.0f, 2.0f, 2.0f), &rects); |
214 picture.draw(&mockCanvas); | |
215 // This test passes by not asserting. We do not validate the rects recor
ded | 181 // This test passes by not asserting. We do not validate the rects recor
ded |
216 // because the result is numerically unstable (floating point equality). | 182 // because the result is numerically unstable (floating point equality). |
217 // The content of any one of the four tiles of the tilegrid would be a v
alid | 183 // The content of any one of the four tiles of the tilegrid would be a v
alid |
218 // result since any bbox that covers the center point of the canvas will
be | 184 // result since any bbox that covers the center point of the canvas will
be |
219 // recorded in all four tiles. | 185 // recorded in all four tiles. |
220 } | 186 } |
221 } | 187 } |
222 | 188 |
223 DEF_TEST(TileGrid, reporter) { | 189 DEF_TEST(TileGrid, reporter) { |
224 // Out of bounds | 190 // Out of bounds |
(...skipping 16 matching lines...) Expand all Loading... |
241 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile
, 1); | 207 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile
, 1); |
242 verifyTileHits(reporter, SkIRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile
, 1); | 208 verifyTileHits(reporter, SkIRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile
, 1); |
243 | 209 |
244 // BBoxes that overlap tiles | 210 // BBoxes that overlap tiles |
245 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kT
opRight_Tile); | 211 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kT
opRight_Tile); |
246 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile | | 212 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile | |
247 kBottomLeft_Tile); | 213 kBottomLeft_Tile); |
248 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile); | 214 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile); |
249 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile); | 215 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile); |
250 } | 216 } |
OLD | NEW |