blob: ef2d796ecec99864b71ece6bad7c5b35aaf782af [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 "SkBitmapDevice.h"
9#include "SkCanvas.h"
junov@chromium.orgadc58e42012-11-07 17:38:38 +000010#include "SkTileGrid.h"
junov@chromium.org3cb834b2012-12-13 16:39:53 +000011#include "SkTileGridPicture.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.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
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000023static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect,
24 uint32_t tileMask, int borderPixels = 0) {
25 SkTileGridPicture::TileGridInfo info;
26 info.fMargin.set(borderPixels, borderPixels);
27 info.fOffset.setZero();
28 info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels);
29 SkTileGrid grid(2, 2, info, NULL);
30 grid.insert(NULL, rect, false);
31 REPORTER_ASSERT(reporter, grid.tileCount(0, 0) ==
32 ((tileMask & kTopLeft_Tile)? 1 : 0));
33 REPORTER_ASSERT(reporter, grid.tileCount(1, 0) ==
34 ((tileMask & kTopRight_Tile)? 1 : 0));
35 REPORTER_ASSERT(reporter, grid.tileCount(0, 1) ==
36 ((tileMask & kBottomLeft_Tile)? 1 : 0));
37 REPORTER_ASSERT(reporter, grid.tileCount(1, 1) ==
38 ((tileMask & kBottomRight_Tile)? 1 : 0));
39}
40
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +000041static 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
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000051DEF_TEST(TileGrid_UnalignedQuery, reporter) {
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000052 SkTileGridPicture::TileGridInfo info;
53 info.fMargin.setEmpty();
54 info.fOffset.setZero();
55 info.fTileInterval.set(10, 10);
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +000056 SkIRect rect1 = SkIRect::MakeXYWH(0, 0, 8, 8);
57 SkIRect rect2 = SkIRect::MakeXYWH(11, 11, 1, 1);
58 SkTileGrid grid(2, 2, info, SkTileGridNextDatum<SkPictureStateTree::Draw>);
59 grid.insert(&rect1, rect1, true);
60 grid.insert(&rect2, rect2, true);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000061
62 // Test parts of top-left tile
63 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +000064 SkTDArray<void*> rects;
65 grid.search(query(0.0f, 0.0f, 1.0f, 1.0f), &rects);
66 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +000067 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0);
junov@chromium.orgadc58e42012-11-07 17:38:38 +000068 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000069 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +000070 SkTDArray<void*> rects;
71 grid.search(query(7.99f, 7.99f, 1.0f, 1.0f), &rects);
72 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +000073 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0);
junov@chromium.org3cb834b2012-12-13 16:39:53 +000074 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000075 // Corner overlap
76 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +000077 SkTDArray<void*> rects;
78 grid.search(query(9.5f, 9.5f, 1.0f, 1.0f), &rects);
79 REPORTER_ASSERT(reporter, 2 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +000080 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0);
81 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0);
junov@chromium.org29b19e52013-02-27 18:35:16 +000082 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000083 // Intersect bottom right tile, but does not overlap rect 2
84 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +000085 SkTDArray<void*> rects;
86 grid.search(query(16.0f, 16.0f, 1.0f, 1.0f), &rects);
87 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +000088 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0);
junov@chromium.orgadc58e42012-11-07 17:38:38 +000089 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000090 // Out of bounds queries, snap to border tiles
91 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +000092 SkTDArray<void*> rects;
93 grid.search(query(-2.0f, 0.0f, 1.0f, 1.0f), &rects);
94 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +000095 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000096 }
97 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +000098 SkTDArray<void*> rects;
99 grid.search(query(0.0f, -2.0f, 1.0f, 1.0f), &rects);
100 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +0000101 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000102 }
103 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000104 SkTDArray<void*> rects;
105 grid.search(query(22.0f, 16.0f, 1.0f, 1.0f), &rects);
106 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +0000107 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000108 }
109 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000110 SkTDArray<void*> rects;
111 grid.search(query(16.0f, 22.0f, 1.0f, 1.0f), &rects);
112 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +0000113 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000114 }
115}
junov@chromium.orgadc58e42012-11-07 17:38:38 +0000116
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000117DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) {
118 // Use SkTileGridPicture to generate a SkTileGrid with a helper
119 SkTileGridPicture::TileGridInfo info;
120 info.fMargin.set(1, 1);
121 info.fOffset.set(-1, -1);
122 info.fTileInterval.set(8, 8);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000123
124 // rect landing entirely in top left tile
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000125 SkIRect rect1 = SkIRect::MakeXYWH(0, 0, 1, 1);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000126 // rect landing entirely in center tile
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000127 SkIRect rect2 = SkIRect::MakeXYWH(12, 12, 1, 1);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000128 // rect landing entirely in bottomright tile
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000129 SkIRect rect3 = SkIRect::MakeXYWH(19, 19, 1, 1);
130 SkTileGrid grid(3, 3, info, SkTileGridNextDatum<SkPictureStateTree::Draw>);
131 grid.insert(&rect1, rect1, true);
132 grid.insert(&rect2, rect2, true);
133 grid.insert(&rect3, rect3, true);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000134
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000135 // Test parts of top-left tile
136 {
137 // The offset should cancel the top and left borders of the top left tile
138 // So a look-up at interval 0-10 should be grid aligned,
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000139 SkTDArray<void*> rects;
140 grid.search(query(0.0f, 0.0f, 10.0f, 10.0f), &rects);
141 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +0000142 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000143 }
144 {
145 // Encroaching border by one pixel
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000146 SkTDArray<void*> rects;
147 grid.search(query(0.0f, 0.0f, 11.0f, 11.0f), &rects);
148 REPORTER_ASSERT(reporter, 2 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +0000149 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0);
150 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000151 }
152 {
153 // Tile stride is 8 (tileWidth - 2 * border pixels
154 // so translating by 8, should make query grid-aligned
155 // with middle tile.
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000156 SkTDArray<void*> rects;
157 grid.search(query(8.0f, 8.0f, 10.0f, 10.0f), &rects);
158 REPORTER_ASSERT(reporter, 1 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +0000159 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000160 }
161 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000162 SkTDArray<void*> rects;
163 grid.search(query(7.9f, 7.9f, 10.0f, 10.0f), &rects);
164 REPORTER_ASSERT(reporter, 2 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +0000165 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0);
166 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000167 }
168 {
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000169 SkTDArray<void*> rects;
170 grid.search(query(8.1f, 8.1f, 10.0f, 10.0f), &rects);
171 REPORTER_ASSERT(reporter, 2 == rects.count());
commit-bot@chromium.orgbe4825c2014-03-17 12:14:48 +0000172 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0);
173 REPORTER_ASSERT(reporter, rects.find(&rect3) >= 0);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000174 }
175 {
176 // Regression test for crbug.com/234688
177 // Once the 2x2 device region is inset by margin, it yields an empty
178 // adjusted region, sitting right on top of the tile boundary.
commit-bot@chromium.org7ae3bc72014-03-17 10:51:44 +0000179 SkTDArray<void*> rects;
180 grid.search(query(8.0f, 8.0f, 2.0f, 2.0f), &rects);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000181 // This test passes by not asserting. We do not validate the rects recorded
182 // because the result is numerically unstable (floating point equality).
183 // The content of any one of the four tiles of the tilegrid would be a valid
184 // result since any bbox that covers the center point of the canvas will be
185 // recorded in all four tiles.
186 }
187}
188
189DEF_TEST(TileGrid, reporter) {
190 // Out of bounds
191 verifyTileHits(reporter, SkIRect::MakeXYWH(30, 0, 1, 1), 0);
192 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 30, 1, 1), 0);
193 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, 0, 1, 1), 0);
194 verifyTileHits(reporter, SkIRect::MakeXYWH(0, -10, 1, 1), 0);
195
196 // Dilation for AA consideration
197 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 9, 9), kTopLeft_Tile);
198 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 10, 10), kAll_Tile);
199 verifyTileHits(reporter, SkIRect::MakeXYWH(9, 9, 1, 1), kAll_Tile);
200 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kAll_Tile);
201 verifyTileHits(reporter, SkIRect::MakeXYWH(11, 11, 1, 1), kBottomRight_Tile);
202
203 // BorderPixels
204 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 6, 6), kTopLeft_Tile, 1);
205 verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 7, 7), kAll_Tile, 1);
206 verifyTileHits(reporter, SkIRect::MakeXYWH(9, 9, 1, 1), kAll_Tile, 1);
207 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile, 1);
208 verifyTileHits(reporter, SkIRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile, 1);
209
210 // BBoxes that overlap tiles
211 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kTopRight_Tile);
212 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile |
213 kBottomLeft_Tile);
214 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile);
215 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile);
216}