blob: 81f383d97ee3e98b4c97ed5e106b2ec8df94e294 [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
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00008#include "SkCanvas.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +00009#include "SkPictureRecorder.h"
junov@chromium.orgadc58e42012-11-07 17:38:38 +000010#include "SkTileGrid.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
junov@chromium.orgadc58e42012-11-07 17:38:38 +000012
13enum Tile {
14 kTopLeft_Tile = 0x1,
15 kTopRight_Tile = 0x2,
16 kBottomLeft_Tile = 0x4,
17 kBottomRight_Tile = 0x8,
18
19 kAll_Tile = kTopLeft_Tile | kTopRight_Tile | kBottomLeft_Tile | kBottomRight_Tile,
20};
21
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000022class MockCanvas : public SkCanvas {
23public:
24 MockCanvas(const SkBitmap& bm) : SkCanvas(bm) {}
25
26 virtual void drawRect(const SkRect& rect, const SkPaint&) {
27 // This capture occurs before quick reject.
28 fRects.push(rect);
29 }
30
31 SkTDArray<SkRect> fRects;
32};
33
mtklein533eb782014-08-27 10:39:42 -070034static void verifyTileHits(skiatest::Reporter* reporter, SkRect rect,
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000035 uint32_t tileMask, int borderPixels = 0) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000036 SkTileGridFactory::TileGridInfo info;
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000037 info.fMargin.set(borderPixels, borderPixels);
38 info.fOffset.setZero();
39 info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels);
mtklein534cc4c2014-08-11 10:09:34 -070040 SkTileGrid grid(2, 2, info);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000041 grid.insert(NULL, rect, false);
42 REPORTER_ASSERT(reporter, grid.tileCount(0, 0) ==
43 ((tileMask & kTopLeft_Tile)? 1 : 0));
44 REPORTER_ASSERT(reporter, grid.tileCount(1, 0) ==
45 ((tileMask & kTopRight_Tile)? 1 : 0));
46 REPORTER_ASSERT(reporter, grid.tileCount(0, 1) ==
47 ((tileMask & kBottomLeft_Tile)? 1 : 0));
48 REPORTER_ASSERT(reporter, grid.tileCount(1, 1) ==
49 ((tileMask & kBottomRight_Tile)? 1 : 0));
50}
51
52DEF_TEST(TileGrid_UnalignedQuery, reporter) {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000053 // Use SkTileGridPicture to generate a SkTileGrid with a helper
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000054 SkTileGridFactory::TileGridInfo info;
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000055 info.fMargin.setEmpty();
56 info.fOffset.setZero();
57 info.fTileInterval.set(10, 10);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000058 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
59 SkIntToScalar(8), SkIntToScalar(8));
60 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(11), SkIntToScalar(11),
61 SkIntToScalar(1), SkIntToScalar(1));
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000062 SkTileGridFactory factory(info);
63 SkPictureRecorder recorder;
64 SkCanvas* canvas = recorder.beginRecording(20, 20, &factory, 0);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000065 SkPaint paint;
66 canvas->drawRect(rect1, paint);
67 canvas->drawRect(rect2, paint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000068 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000069
70 SkBitmap store;
71 store.allocN32Pixels(1, 1);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000072
73 // Test parts of top-left tile
74 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000075 MockCanvas mockCanvas(store);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000076 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000077 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
78 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
junov@chromium.orgadc58e42012-11-07 17:38:38 +000079 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000080 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000081 MockCanvas mockCanvas(store);
82 mockCanvas.translate(-7.99f, -7.99f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000083 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000084 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
85 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
junov@chromium.org3cb834b2012-12-13 16:39:53 +000086 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000087 // Corner overlap
88 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000089 MockCanvas mockCanvas(store);
90 mockCanvas.translate(-9.5f, -9.5f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000091 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000092 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
93 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
94 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
junov@chromium.org29b19e52013-02-27 18:35:16 +000095 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000096 // Intersect bottom right tile, but does not overlap rect 2
97 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +000098 MockCanvas mockCanvas(store);
99 mockCanvas.translate(-16.0f, -16.0f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000100 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000101 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
102 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
junov@chromium.orgadc58e42012-11-07 17:38:38 +0000103 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000104 // Out of bounds queries, snap to border tiles
105 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000106 MockCanvas mockCanvas(store);
107 mockCanvas.translate(2.0f, 0.0f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000108 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000109 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
110 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000111 }
112 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000113 MockCanvas mockCanvas(store);
114 mockCanvas.translate(0.0f, 2.0f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000115 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000116 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
117 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000118 }
119 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000120 MockCanvas mockCanvas(store);
121 mockCanvas.translate(-22.0f, -16.0f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000122 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000123 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
124 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000125 }
126 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000127 MockCanvas mockCanvas(store);
128 mockCanvas.translate(-16.0f, -22.0f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000129 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000130 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
131 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000132 }
133}
junov@chromium.orgadc58e42012-11-07 17:38:38 +0000134
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000135DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) {
136 // Use SkTileGridPicture to generate a SkTileGrid with a helper
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000137 SkTileGridFactory::TileGridInfo info;
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000138 info.fMargin.set(1, 1);
139 info.fOffset.set(-1, -1);
140 info.fTileInterval.set(8, 8);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000141
142 // rect landing entirely in top left tile
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000143 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
144 SkIntToScalar(1), SkIntToScalar(1));
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000145 // rect landing entirely in center tile
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000146 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(12), SkIntToScalar(12),
147 SkIntToScalar(1), SkIntToScalar(1));
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000148 // rect landing entirely in bottomright tile
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000149 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(19), SkIntToScalar(19),
150 SkIntToScalar(1), SkIntToScalar(1));
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000151 SkTileGridFactory factory(info);
152 SkPictureRecorder recorder;
153 SkCanvas* canvas = recorder.beginRecording(20, 20, &factory, 0);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000154 SkPaint paint;
155 canvas->drawRect(rect1, paint);
156 canvas->drawRect(rect2, paint);
157 canvas->drawRect(rect3, paint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000158 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000159
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000160 SkBitmap tileBitmap;
161 tileBitmap.allocN32Pixels(10, 10);
162 SkBitmap moreThanATileBitmap;
163 moreThanATileBitmap.allocN32Pixels(11, 11);
164 SkBitmap tinyBitmap;
165 tinyBitmap.allocN32Pixels(2, 2);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000166 // Test parts of top-left tile
167 {
168 // The offset should cancel the top and left borders of the top left tile
169 // So a look-up at interval 0-10 should be grid aligned,
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000170 MockCanvas mockCanvas(tileBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000171 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000172 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
173 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000174 }
175 {
176 // Encroaching border by one pixel
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000177 MockCanvas mockCanvas(moreThanATileBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000178 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000179 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
180 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
181 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000182 }
183 {
184 // Tile stride is 8 (tileWidth - 2 * border pixels
185 // so translating by 8, should make query grid-aligned
186 // with middle tile.
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000187 MockCanvas mockCanvas(tileBitmap);
188 mockCanvas.translate(SkIntToScalar(-8), SkIntToScalar(-8));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000189 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000190 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
191 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000192 }
193 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000194 MockCanvas mockCanvas(tileBitmap);
195 mockCanvas.translate(-7.9f, -7.9f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000196 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000197 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
198 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
199 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000200 }
201 {
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000202 MockCanvas mockCanvas(tileBitmap);
203 mockCanvas.translate(-8.1f, -8.1f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000204 picture->draw(&mockCanvas);
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000205 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
206 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
207 REPORTER_ASSERT(reporter, rect3 == mockCanvas.fRects[1]);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000208 }
209 {
210 // Regression test for crbug.com/234688
211 // Once the 2x2 device region is inset by margin, it yields an empty
212 // adjusted region, sitting right on top of the tile boundary.
commit-bot@chromium.org0e9e6a32014-03-28 17:57:14 +0000213 MockCanvas mockCanvas(tinyBitmap);
214 mockCanvas.translate(-8.0f, -8.0f);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000215 picture->draw(&mockCanvas);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000216 // This test passes by not asserting. We do not validate the rects recorded
217 // because the result is numerically unstable (floating point equality).
218 // The content of any one of the four tiles of the tilegrid would be a valid
219 // result since any bbox that covers the center point of the canvas will be
220 // recorded in all four tiles.
221 }
222}
223
224DEF_TEST(TileGrid, reporter) {
225 // Out of bounds
mtklein533eb782014-08-27 10:39:42 -0700226 verifyTileHits(reporter, SkRect::MakeXYWH(30, 0, 1, 1), 0);
227 verifyTileHits(reporter, SkRect::MakeXYWH(0, 30, 1, 1), 0);
228 verifyTileHits(reporter, SkRect::MakeXYWH(-10, 0, 1, 1), 0);
229 verifyTileHits(reporter, SkRect::MakeXYWH(0, -10, 1, 1), 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000230
231 // Dilation for AA consideration
mtklein533eb782014-08-27 10:39:42 -0700232 verifyTileHits(reporter, SkRect::MakeXYWH(0, 0, 9, 9), kTopLeft_Tile);
233 verifyTileHits(reporter, SkRect::MakeXYWH(0, 0, 10, 10), kAll_Tile);
234 verifyTileHits(reporter, SkRect::MakeXYWH(9, 9, 1, 1), kAll_Tile);
235 verifyTileHits(reporter, SkRect::MakeXYWH(10, 10, 1, 1), kAll_Tile);
236 verifyTileHits(reporter, SkRect::MakeXYWH(11, 11, 1, 1), kBottomRight_Tile);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000237
238 // BorderPixels
mtklein533eb782014-08-27 10:39:42 -0700239 verifyTileHits(reporter, SkRect::MakeXYWH(0, 0, 6, 6), kTopLeft_Tile, 1);
240 verifyTileHits(reporter, SkRect::MakeXYWH(0, 0, 7, 7), kAll_Tile, 1);
241 verifyTileHits(reporter, SkRect::MakeXYWH(9, 9, 1, 1), kAll_Tile, 1);
242 verifyTileHits(reporter, SkRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile, 1);
243 verifyTileHits(reporter, SkRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile, 1);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000244
245 // BBoxes that overlap tiles
mtklein533eb782014-08-27 10:39:42 -0700246 verifyTileHits(reporter, SkRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kTopRight_Tile);
247 verifyTileHits(reporter, SkRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile |
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000248 kBottomLeft_Tile);
mtklein533eb782014-08-27 10:39:42 -0700249 verifyTileHits(reporter, SkRect::MakeXYWH(5, 5, 10, 10), kAll_Tile);
250 verifyTileHits(reporter, SkRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000251}