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