blob: 5cffd3aefeab7c5ed9a01763bd9392dabc283607 [file] [log] [blame]
junov@chromium.orgadc58e42012-11-07 17:38:38 +00001/*
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 "Test.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00009#include "SkBitmapDevice.h"
10#include "SkCanvas.h"
junov@chromium.orgadc58e42012-11-07 17:38:38 +000011#include "SkTileGrid.h"
junov@chromium.org3cb834b2012-12-13 16:39:53 +000012#include "SkTileGridPicture.h"
junov@chromium.orgadc58e42012-11-07 17:38:38 +000013
14enum Tile {
15 kTopLeft_Tile = 0x1,
16 kTopRight_Tile = 0x2,
17 kBottomLeft_Tile = 0x4,
18 kBottomRight_Tile = 0x8,
19
20 kAll_Tile = kTopLeft_Tile | kTopRight_Tile | kBottomLeft_Tile | kBottomRight_Tile,
21};
22
junov@chromium.org3cb834b2012-12-13 16:39:53 +000023class MockCanvas : public SkCanvas {
24public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000025 MockCanvas(SkBaseDevice* device) : SkCanvas(device)
junov@chromium.org3cb834b2012-12-13 16:39:53 +000026 {}
27
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000028 virtual void drawRect(const SkRect& rect, const SkPaint&)
junov@chromium.org3cb834b2012-12-13 16:39:53 +000029 {
30 // This capture occurs before quick reject.
31 fRects.push(rect);
32 }
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000033
34 SkTDArray<SkRect> fRects;
junov@chromium.org3cb834b2012-12-13 16:39:53 +000035};
junov@chromium.org3cb834b2012-12-13 16:39:53 +000036
junov@chromium.orgadc58e42012-11-07 17:38:38 +000037class TileGridTest {
38public:
skia.committer@gmail.com86267192013-02-09 07:05:02 +000039 static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, uint32_t tileMask,
junov@chromium.orgf507c412013-02-08 21:03:30 +000040 int borderPixels = 0) {
junov@chromium.org29b19e52013-02-27 18:35:16 +000041 SkTileGridPicture::TileGridInfo info;
42 info.fMargin.set(borderPixels, borderPixels);
43 info.fOffset.setZero();
44 info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels);
45 SkTileGrid grid(2, 2, info, NULL);
junov@chromium.orgadc58e42012-11-07 17:38:38 +000046 grid.insert(NULL, rect, false);
47 REPORTER_ASSERT(reporter, grid.tile(0,0).count() ==
48 ((tileMask & kTopLeft_Tile)? 1 : 0));
49 REPORTER_ASSERT(reporter, grid.tile(1,0).count() ==
50 ((tileMask & kTopRight_Tile)? 1 : 0));
51 REPORTER_ASSERT(reporter, grid.tile(0,1).count() ==
52 ((tileMask & kBottomLeft_Tile)? 1 : 0));
53 REPORTER_ASSERT(reporter, grid.tile(1,1).count() ==
54 ((tileMask & kBottomRight_Tile)? 1 : 0));
55 }
56
junov@chromium.org3cb834b2012-12-13 16:39:53 +000057 static void TestUnalignedQuery(skiatest::Reporter* reporter) {
58 // Use SkTileGridPicture to generate a SkTileGrid with a helper
junov@chromium.org29b19e52013-02-27 18:35:16 +000059 SkTileGridPicture::TileGridInfo info;
60 info.fMargin.setEmpty();
61 info.fOffset.setZero();
62 info.fTileInterval.set(10, 10);
63 SkTileGridPicture picture(20, 20, info);
skia.committer@gmail.com61b05dc2012-12-14 02:02:06 +000064 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
junov@chromium.org3cb834b2012-12-13 16:39:53 +000065 SkIntToScalar(8), SkIntToScalar(8));
66 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(11), SkIntToScalar(11),
67 SkIntToScalar(1), SkIntToScalar(1));
68 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForClippedPlayback_RecordingFlag);
69 SkPaint paint;
70 canvas->drawRect(rect1, paint);
71 canvas->drawRect(rect2, paint);
72 picture.endRecording();
73
74 SkBitmap store;
75 store.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
76 store.allocPixels();
77
78 // Test parts of top-left tile
79 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000080 SkBitmapDevice device(store);
junov@chromium.org3cb834b2012-12-13 16:39:53 +000081 MockCanvas mockCanvas(&device);
82 picture.draw(&mockCanvas);
83 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
84 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
85 }
86 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000087 SkBitmapDevice device(store);
junov@chromium.org3cb834b2012-12-13 16:39:53 +000088 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000089 mockCanvas.translate(-7.99f, -7.99f);
junov@chromium.org3cb834b2012-12-13 16:39:53 +000090 picture.draw(&mockCanvas);
91 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
92 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
93 }
94 // Corner overlap
95 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000096 SkBitmapDevice device(store);
junov@chromium.org3cb834b2012-12-13 16:39:53 +000097 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000098 mockCanvas.translate(-9.5f, -9.5f);
junov@chromium.org3cb834b2012-12-13 16:39:53 +000099 picture.draw(&mockCanvas);
100 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
101 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
102 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
103 }
104 // Intersect bottom right tile, but does not overlap rect 2
105 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000106 SkBitmapDevice device(store);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000107 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000108 mockCanvas.translate(-16.0f, -16.0f);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000109 picture.draw(&mockCanvas);
110 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
111 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
112 }
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000113 // Out of bounds queries, snap to border tiles
114 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000115 SkBitmapDevice device(store);
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000116 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000117 mockCanvas.translate(2.0f, 0.0f);
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000118 picture.draw(&mockCanvas);
119 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
120 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
121 }
122 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000123 SkBitmapDevice device(store);
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000124 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000125 mockCanvas.translate(0.0f, 2.0f);
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000126 picture.draw(&mockCanvas);
127 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
128 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
129 }
130 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000131 SkBitmapDevice device(store);
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000132 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000133 mockCanvas.translate(-22.0f, -16.0f);
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000134 picture.draw(&mockCanvas);
135 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
136 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
137 }
138 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000139 SkBitmapDevice device(store);
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000140 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000141 mockCanvas.translate(-16.0f, -22.0f);
junov@chromium.orgef5b8112013-04-09 16:13:09 +0000142 picture.draw(&mockCanvas);
143 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
144 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
145 }
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000146 }
147
junov@chromium.org29b19e52013-02-27 18:35:16 +0000148 static void TestOverlapOffsetQueryAlignment(skiatest::Reporter* reporter) {
149 // Use SkTileGridPicture to generate a SkTileGrid with a helper
150 SkTileGridPicture::TileGridInfo info;
151 info.fMargin.set(1, 1);
152 info.fOffset.set(-1, -1);
153 info.fTileInterval.set(8, 8);
154 SkTileGridPicture picture(20, 20, info);
155
156 // rect landing entirely in top left tile
157 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
158 SkIntToScalar(1), SkIntToScalar(1));
159 // rect landing entirely in center tile
160 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(12), SkIntToScalar(12),
161 SkIntToScalar(1), SkIntToScalar(1));
162 // rect landing entirely in bottomright tile
163 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(19), SkIntToScalar(19),
164 SkIntToScalar(1), SkIntToScalar(1));
165 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForClippedPlayback_RecordingFlag);
166 SkPaint paint;
167 canvas->drawRect(rect1, paint);
168 canvas->drawRect(rect2, paint);
169 canvas->drawRect(rect3, paint);
170 picture.endRecording();
171
172 SkBitmap tileBitmap;
173 tileBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
174 tileBitmap.allocPixels();
175 SkBitmap moreThanATileBitmap;
176 moreThanATileBitmap.setConfig(SkBitmap::kARGB_8888_Config, 11, 11);
177 moreThanATileBitmap.allocPixels();
junov@chromium.orgd5cfdff2013-04-24 15:39:11 +0000178 SkBitmap tinyBitmap;
179 tinyBitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
180 tinyBitmap.allocPixels();
junov@chromium.org29b19e52013-02-27 18:35:16 +0000181 // Test parts of top-left tile
182 {
183 // The offset should cancel the top and left borders of the top left tile
184 // So a look-up at interval 0-10 should be grid aligned,
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000185 SkBitmapDevice device(tileBitmap);
junov@chromium.org29b19e52013-02-27 18:35:16 +0000186 MockCanvas mockCanvas(&device);
187 picture.draw(&mockCanvas);
188 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
189 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
190 }
191 {
192 // Encroaching border by one pixel
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000193 SkBitmapDevice device(moreThanATileBitmap);
junov@chromium.org29b19e52013-02-27 18:35:16 +0000194 MockCanvas mockCanvas(&device);
195 picture.draw(&mockCanvas);
196 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
197 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
198 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
199 }
200 {
201 // Tile stride is 8 (tileWidth - 2 * border pixels
202 // so translating by 8, should make query grid-aligned
203 // with middle tile.
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000204 SkBitmapDevice device(tileBitmap);
junov@chromium.org29b19e52013-02-27 18:35:16 +0000205 MockCanvas mockCanvas(&device);
206 mockCanvas.translate(SkIntToScalar(-8), SkIntToScalar(-8));
207 picture.draw(&mockCanvas);
208 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
209 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
210 }
211 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000212 SkBitmapDevice device(tileBitmap);
junov@chromium.org29b19e52013-02-27 18:35:16 +0000213 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000214 mockCanvas.translate(-7.9f, -7.9f);
junov@chromium.org29b19e52013-02-27 18:35:16 +0000215 picture.draw(&mockCanvas);
216 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
217 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
218 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
219 }
220 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000221 SkBitmapDevice device(tileBitmap);
junov@chromium.org29b19e52013-02-27 18:35:16 +0000222 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000223 mockCanvas.translate(-8.1f, -8.1f);
junov@chromium.org29b19e52013-02-27 18:35:16 +0000224 picture.draw(&mockCanvas);
225 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
226 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
227 REPORTER_ASSERT(reporter, rect3 == mockCanvas.fRects[1]);
228 }
junov@chromium.orgd5cfdff2013-04-24 15:39:11 +0000229 {
230 // Regression test for crbug.com/234688
231 // Once the 2x2 device region is inset by margin, it yields an empty
232 // adjusted region, sitting right on top of the tile boundary.
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000233 SkBitmapDevice device(tinyBitmap);
junov@chromium.orgd5cfdff2013-04-24 15:39:11 +0000234 MockCanvas mockCanvas(&device);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000235 mockCanvas.translate(-8.0f, -8.0f);
junov@chromium.orgd5cfdff2013-04-24 15:39:11 +0000236 picture.draw(&mockCanvas);
237 // This test passes by not asserting. We do not validate the rects recorded
skia.committer@gmail.com83f0d302013-04-25 07:01:04 +0000238 // because the result is numerically unstable (floating point equality).
junov@chromium.orgd5cfdff2013-04-24 15:39:11 +0000239 // The content of any one of the four tiles of the tilegrid would be a valid
240 // result since any bbox that covers the center point of the canvas will be
241 // recorded in all four tiles.
242 }
junov@chromium.org29b19e52013-02-27 18:35:16 +0000243 }
244
junov@chromium.orgadc58e42012-11-07 17:38:38 +0000245 static void Test(skiatest::Reporter* reporter) {
246 // Out of bounds
247 verifyTileHits(reporter, SkIRect::MakeXYWH(30, 0, 1, 1), 0);
248 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 30, 1, 1), 0);
249 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, 0, 1, 1), 0);
250 verifyTileHits(reporter, SkIRect::MakeXYWH(0, -10, 1, 1), 0);
251
252 // Dilation for AA consideration
junov@chromium.org29b19e52013-02-27 18:35:16 +0000253 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 9, 9), kTopLeft_Tile);
254 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 10, 10), kAll_Tile);
255 verifyTileHits(reporter, SkIRect::MakeXYWH(9, 9, 1, 1), kAll_Tile);
junov@chromium.orgadc58e42012-11-07 17:38:38 +0000256 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kAll_Tile);
257 verifyTileHits(reporter, SkIRect::MakeXYWH(11, 11, 1, 1), kBottomRight_Tile);
skia.committer@gmail.com631cdcb2013-03-01 12:12:55 +0000258
junov@chromium.orgf507c412013-02-08 21:03:30 +0000259 // BorderPixels
junov@chromium.org29b19e52013-02-27 18:35:16 +0000260 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 6, 6), kTopLeft_Tile, 1);
261 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 7, 7), kAll_Tile, 1);
262 verifyTileHits(reporter, SkIRect::MakeXYWH(9, 9, 1, 1), kAll_Tile, 1);
263 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile, 1);
264 verifyTileHits(reporter, SkIRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile, 1);
junov@chromium.orgf507c412013-02-08 21:03:30 +0000265
junov@chromium.orgadc58e42012-11-07 17:38:38 +0000266 // BBoxes that overlap tiles
267 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kTopRight_Tile);
268 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile |
269 kBottomLeft_Tile);
270 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile);
271 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000272
273 TestUnalignedQuery(reporter);
junov@chromium.org29b19e52013-02-27 18:35:16 +0000274 TestOverlapOffsetQueryAlignment(reporter);
junov@chromium.orgadc58e42012-11-07 17:38:38 +0000275 }
276};
277
junov@chromium.orgadc58e42012-11-07 17:38:38 +0000278#include "TestClassDef.h"
279DEFINE_TESTCLASS("TileGrid", TileGridTestClass, TileGridTest::Test)