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 { | 23 class MockCanvas : public SkCanvas { |
24 public: | 24 public: |
25 MockCanvas(SkBaseDevice* device) : SkCanvas(device) | 25 MockCanvas(const SkBitmap& bm) : SkCanvas(bm) {} |
26 {} | |
27 | 26 |
28 virtual void drawRect(const SkRect& rect, const SkPaint&) | 27 virtual void drawRect(const SkRect& rect, const SkPaint&) { |
29 { | |
30 // This capture occurs before quick reject. | 28 // This capture occurs before quick reject. |
31 fRects.push(rect); | 29 fRects.push(rect); |
32 } | 30 } |
33 | 31 |
34 SkTDArray<SkRect> fRects; | 32 SkTDArray<SkRect> fRects; |
35 }; | 33 }; |
36 | 34 |
37 static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, | 35 static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, |
38 uint32_t tileMask, int borderPixels = 0) { | 36 uint32_t tileMask, int borderPixels = 0) { |
39 SkTileGridPicture::TileGridInfo info; | 37 SkTileGridPicture::TileGridInfo info; |
(...skipping 23 matching lines...) Expand all Loading... |
63 SkIntToScalar(8), SkIntToScalar(8)); | 61 SkIntToScalar(8), SkIntToScalar(8)); |
64 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(11), SkIntToScalar(11), | 62 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(11), SkIntToScalar(11), |
65 SkIntToScalar(1), SkIntToScalar(1)); | 63 SkIntToScalar(1), SkIntToScalar(1)); |
66 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForCli
ppedPlayback_RecordingFlag); | 64 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForCli
ppedPlayback_RecordingFlag); |
67 SkPaint paint; | 65 SkPaint paint; |
68 canvas->drawRect(rect1, paint); | 66 canvas->drawRect(rect1, paint); |
69 canvas->drawRect(rect2, paint); | 67 canvas->drawRect(rect2, paint); |
70 picture.endRecording(); | 68 picture.endRecording(); |
71 | 69 |
72 SkBitmap store; | 70 SkBitmap store; |
73 store.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); | 71 store.allocN32Pixels(1, 1); |
74 store.allocPixels(); | |
75 | 72 |
76 // Test parts of top-left tile | 73 // Test parts of top-left tile |
77 { | 74 { |
78 SkBitmapDevice device(store); | 75 MockCanvas mockCanvas(store); |
79 MockCanvas mockCanvas(&device); | |
80 picture.draw(&mockCanvas); | 76 picture.draw(&mockCanvas); |
81 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 77 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
82 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 78 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
83 } | 79 } |
84 { | 80 { |
85 SkBitmapDevice device(store); | 81 MockCanvas mockCanvas(store); |
86 MockCanvas mockCanvas(&device); | |
87 mockCanvas.translate(-7.99f, -7.99f); | 82 mockCanvas.translate(-7.99f, -7.99f); |
88 picture.draw(&mockCanvas); | 83 picture.draw(&mockCanvas); |
89 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 84 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
90 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 85 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
91 } | 86 } |
92 // Corner overlap | 87 // Corner overlap |
93 { | 88 { |
94 SkBitmapDevice device(store); | 89 MockCanvas mockCanvas(store); |
95 MockCanvas mockCanvas(&device); | |
96 mockCanvas.translate(-9.5f, -9.5f); | 90 mockCanvas.translate(-9.5f, -9.5f); |
97 picture.draw(&mockCanvas); | 91 picture.draw(&mockCanvas); |
98 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); | 92 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
99 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 93 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
100 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); | 94 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); |
101 } | 95 } |
102 // Intersect bottom right tile, but does not overlap rect 2 | 96 // Intersect bottom right tile, but does not overlap rect 2 |
103 { | 97 { |
104 SkBitmapDevice device(store); | 98 MockCanvas mockCanvas(store); |
105 MockCanvas mockCanvas(&device); | |
106 mockCanvas.translate(-16.0f, -16.0f); | 99 mockCanvas.translate(-16.0f, -16.0f); |
107 picture.draw(&mockCanvas); | 100 picture.draw(&mockCanvas); |
108 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 101 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
109 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | 102 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
110 } | 103 } |
111 // Out of bounds queries, snap to border tiles | 104 // Out of bounds queries, snap to border tiles |
112 { | 105 { |
113 SkBitmapDevice device(store); | 106 MockCanvas mockCanvas(store); |
114 MockCanvas mockCanvas(&device); | |
115 mockCanvas.translate(2.0f, 0.0f); | 107 mockCanvas.translate(2.0f, 0.0f); |
116 picture.draw(&mockCanvas); | 108 picture.draw(&mockCanvas); |
117 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 109 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
118 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 110 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
119 } | 111 } |
120 { | 112 { |
121 SkBitmapDevice device(store); | 113 MockCanvas mockCanvas(store); |
122 MockCanvas mockCanvas(&device); | |
123 mockCanvas.translate(0.0f, 2.0f); | 114 mockCanvas.translate(0.0f, 2.0f); |
124 picture.draw(&mockCanvas); | 115 picture.draw(&mockCanvas); |
125 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 116 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
126 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 117 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
127 } | 118 } |
128 { | 119 { |
129 SkBitmapDevice device(store); | 120 MockCanvas mockCanvas(store); |
130 MockCanvas mockCanvas(&device); | |
131 mockCanvas.translate(-22.0f, -16.0f); | 121 mockCanvas.translate(-22.0f, -16.0f); |
132 picture.draw(&mockCanvas); | 122 picture.draw(&mockCanvas); |
133 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 123 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
134 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | 124 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
135 } | 125 } |
136 { | 126 { |
137 SkBitmapDevice device(store); | 127 MockCanvas mockCanvas(store); |
138 MockCanvas mockCanvas(&device); | |
139 mockCanvas.translate(-16.0f, -22.0f); | 128 mockCanvas.translate(-16.0f, -22.0f); |
140 picture.draw(&mockCanvas); | 129 picture.draw(&mockCanvas); |
141 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 130 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
142 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | 131 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
143 } | 132 } |
144 } | 133 } |
145 | 134 |
146 DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) { | 135 DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) { |
147 // Use SkTileGridPicture to generate a SkTileGrid with a helper | 136 // Use SkTileGridPicture to generate a SkTileGrid with a helper |
148 SkTileGridPicture::TileGridInfo info; | 137 SkTileGridPicture::TileGridInfo info; |
(...skipping 12 matching lines...) Expand all Loading... |
161 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(19), SkIntToScalar(19), | 150 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(19), SkIntToScalar(19), |
162 SkIntToScalar(1), SkIntToScalar(1)); | 151 SkIntToScalar(1), SkIntToScalar(1)); |
163 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForCli
ppedPlayback_RecordingFlag); | 152 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForCli
ppedPlayback_RecordingFlag); |
164 SkPaint paint; | 153 SkPaint paint; |
165 canvas->drawRect(rect1, paint); | 154 canvas->drawRect(rect1, paint); |
166 canvas->drawRect(rect2, paint); | 155 canvas->drawRect(rect2, paint); |
167 canvas->drawRect(rect3, paint); | 156 canvas->drawRect(rect3, paint); |
168 picture.endRecording(); | 157 picture.endRecording(); |
169 | 158 |
170 SkBitmap tileBitmap; | 159 SkBitmap tileBitmap; |
171 tileBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); | 160 tileBitmap.allocN32Pixels(10, 10); |
172 tileBitmap.allocPixels(); | |
173 SkBitmap moreThanATileBitmap; | 161 SkBitmap moreThanATileBitmap; |
174 moreThanATileBitmap.setConfig(SkBitmap::kARGB_8888_Config, 11, 11); | 162 moreThanATileBitmap.allocN32Pixels(11, 11); |
175 moreThanATileBitmap.allocPixels(); | |
176 SkBitmap tinyBitmap; | 163 SkBitmap tinyBitmap; |
177 tinyBitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | 164 tinyBitmap.allocN32Pixels(2, 2); |
178 tinyBitmap.allocPixels(); | |
179 // Test parts of top-left tile | 165 // Test parts of top-left tile |
180 { | 166 { |
181 // The offset should cancel the top and left borders of the top left til
e | 167 // The offset should cancel the top and left borders of the top left til
e |
182 // So a look-up at interval 0-10 should be grid aligned, | 168 // So a look-up at interval 0-10 should be grid aligned, |
183 SkBitmapDevice device(tileBitmap); | 169 MockCanvas mockCanvas(tileBitmap); |
184 MockCanvas mockCanvas(&device); | |
185 picture.draw(&mockCanvas); | 170 picture.draw(&mockCanvas); |
186 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 171 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
187 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 172 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
188 } | 173 } |
189 { | 174 { |
190 // Encroaching border by one pixel | 175 // Encroaching border by one pixel |
191 SkBitmapDevice device(moreThanATileBitmap); | 176 MockCanvas mockCanvas(moreThanATileBitmap); |
192 MockCanvas mockCanvas(&device); | |
193 picture.draw(&mockCanvas); | 177 picture.draw(&mockCanvas); |
194 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); | 178 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
195 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 179 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
196 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); | 180 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); |
197 } | 181 } |
198 { | 182 { |
199 // Tile stride is 8 (tileWidth - 2 * border pixels | 183 // Tile stride is 8 (tileWidth - 2 * border pixels |
200 // so translating by 8, should make query grid-aligned | 184 // so translating by 8, should make query grid-aligned |
201 // with middle tile. | 185 // with middle tile. |
202 SkBitmapDevice device(tileBitmap); | 186 MockCanvas mockCanvas(tileBitmap); |
203 MockCanvas mockCanvas(&device); | |
204 mockCanvas.translate(SkIntToScalar(-8), SkIntToScalar(-8)); | 187 mockCanvas.translate(SkIntToScalar(-8), SkIntToScalar(-8)); |
205 picture.draw(&mockCanvas); | 188 picture.draw(&mockCanvas); |
206 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); | 189 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
207 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | 190 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
208 } | 191 } |
209 { | 192 { |
210 SkBitmapDevice device(tileBitmap); | 193 MockCanvas mockCanvas(tileBitmap); |
211 MockCanvas mockCanvas(&device); | |
212 mockCanvas.translate(-7.9f, -7.9f); | 194 mockCanvas.translate(-7.9f, -7.9f); |
213 picture.draw(&mockCanvas); | 195 picture.draw(&mockCanvas); |
214 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); | 196 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
215 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); | 197 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
216 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); | 198 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); |
217 } | 199 } |
218 { | 200 { |
219 SkBitmapDevice device(tileBitmap); | 201 MockCanvas mockCanvas(tileBitmap); |
220 MockCanvas mockCanvas(&device); | |
221 mockCanvas.translate(-8.1f, -8.1f); | 202 mockCanvas.translate(-8.1f, -8.1f); |
222 picture.draw(&mockCanvas); | 203 picture.draw(&mockCanvas); |
223 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); | 204 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
224 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); | 205 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
225 REPORTER_ASSERT(reporter, rect3 == mockCanvas.fRects[1]); | 206 REPORTER_ASSERT(reporter, rect3 == mockCanvas.fRects[1]); |
226 } | 207 } |
227 { | 208 { |
228 // Regression test for crbug.com/234688 | 209 // Regression test for crbug.com/234688 |
229 // Once the 2x2 device region is inset by margin, it yields an empty | 210 // Once the 2x2 device region is inset by margin, it yields an empty |
230 // adjusted region, sitting right on top of the tile boundary. | 211 // adjusted region, sitting right on top of the tile boundary. |
231 SkBitmapDevice device(tinyBitmap); | 212 MockCanvas mockCanvas(tinyBitmap); |
232 MockCanvas mockCanvas(&device); | |
233 mockCanvas.translate(-8.0f, -8.0f); | 213 mockCanvas.translate(-8.0f, -8.0f); |
234 picture.draw(&mockCanvas); | 214 picture.draw(&mockCanvas); |
235 // This test passes by not asserting. We do not validate the rects recor
ded | 215 // This test passes by not asserting. We do not validate the rects recor
ded |
236 // because the result is numerically unstable (floating point equality). | 216 // because the result is numerically unstable (floating point equality). |
237 // The content of any one of the four tiles of the tilegrid would be a v
alid | 217 // The content of any one of the four tiles of the tilegrid would be a v
alid |
238 // result since any bbox that covers the center point of the canvas will
be | 218 // result since any bbox that covers the center point of the canvas will
be |
239 // recorded in all four tiles. | 219 // recorded in all four tiles. |
240 } | 220 } |
241 } | 221 } |
242 | 222 |
(...skipping 18 matching lines...) Expand all Loading... |
261 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile
, 1); | 241 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile
, 1); |
262 verifyTileHits(reporter, SkIRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile
, 1); | 242 verifyTileHits(reporter, SkIRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile
, 1); |
263 | 243 |
264 // BBoxes that overlap tiles | 244 // BBoxes that overlap tiles |
265 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kT
opRight_Tile); | 245 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kT
opRight_Tile); |
266 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile | | 246 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile | |
267 kBottomLeft_Tile); | 247 kBottomLeft_Tile); |
268 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile); | 248 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile); |
269 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile); | 249 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile); |
270 } | 250 } |
OLD | NEW |