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 { |
| 24 public: |
| 25 MockCanvas(const SkBitmap& bm) : SkCanvas(bm) {} |
| 26 |
| 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 |
23 static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, | 35 static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, |
24 uint32_t tileMask, int borderPixels = 0) { | 36 uint32_t tileMask, int borderPixels = 0) { |
25 SkTileGridPicture::TileGridInfo info; | 37 SkTileGridPicture::TileGridInfo info; |
26 info.fMargin.set(borderPixels, borderPixels); | 38 info.fMargin.set(borderPixels, borderPixels); |
27 info.fOffset.setZero(); | 39 info.fOffset.setZero(); |
28 info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels); | 40 info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels); |
29 SkTileGrid grid(2, 2, info, NULL); | 41 SkTileGrid grid(2, 2, info, NULL); |
30 grid.insert(NULL, rect, false); | 42 grid.insert(NULL, rect, false); |
31 REPORTER_ASSERT(reporter, grid.tileCount(0, 0) == | 43 REPORTER_ASSERT(reporter, grid.tileCount(0, 0) == |
32 ((tileMask & kTopLeft_Tile)? 1 : 0)); | 44 ((tileMask & kTopLeft_Tile)? 1 : 0)); |
33 REPORTER_ASSERT(reporter, grid.tileCount(1, 0) == | 45 REPORTER_ASSERT(reporter, grid.tileCount(1, 0) == |
34 ((tileMask & kTopRight_Tile)? 1 : 0)); | 46 ((tileMask & kTopRight_Tile)? 1 : 0)); |
35 REPORTER_ASSERT(reporter, grid.tileCount(0, 1) == | 47 REPORTER_ASSERT(reporter, grid.tileCount(0, 1) == |
36 ((tileMask & kBottomLeft_Tile)? 1 : 0)); | 48 ((tileMask & kBottomLeft_Tile)? 1 : 0)); |
37 REPORTER_ASSERT(reporter, grid.tileCount(1, 1) == | 49 REPORTER_ASSERT(reporter, grid.tileCount(1, 1) == |
38 ((tileMask & kBottomRight_Tile)? 1 : 0)); | 50 ((tileMask & kBottomRight_Tile)? 1 : 0)); |
39 } | 51 } |
40 | 52 |
41 static 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 | |
51 DEF_TEST(TileGrid_UnalignedQuery, reporter) { | 53 DEF_TEST(TileGrid_UnalignedQuery, reporter) { |
| 54 // Use SkTileGridPicture to generate a SkTileGrid with a helper |
52 SkTileGridPicture::TileGridInfo info; | 55 SkTileGridPicture::TileGridInfo info; |
53 info.fMargin.setEmpty(); | 56 info.fMargin.setEmpty(); |
54 info.fOffset.setZero(); | 57 info.fOffset.setZero(); |
55 info.fTileInterval.set(10, 10); | 58 info.fTileInterval.set(10, 10); |
56 SkIRect rect1 = SkIRect::MakeXYWH(0, 0, 8, 8); | 59 SkTileGridPicture picture(20, 20, info); |
57 SkIRect rect2 = SkIRect::MakeXYWH(11, 11, 1, 1); | 60 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
58 SkTileGrid grid(2, 2, info, SkTileGridNextDatum<SkPictureStateTree::Draw>); | 61 SkIntToScalar(8), SkIntToScalar(8)); |
59 grid.insert(&rect1, rect1, true); | 62 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(11), SkIntToScalar(11), |
60 grid.insert(&rect2, rect2, true); | 63 SkIntToScalar(1), SkIntToScalar(1)); |
| 64 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForCli
ppedPlayback_RecordingFlag); |
| 65 SkPaint paint; |
| 66 canvas->drawRect(rect1, paint); |
| 67 canvas->drawRect(rect2, paint); |
| 68 picture.endRecording(); |
| 69 |
| 70 SkBitmap store; |
| 71 store.allocN32Pixels(1, 1); |
61 | 72 |
62 // Test parts of top-left tile | 73 // Test parts of top-left tile |
63 { | 74 { |
64 SkTDArray<void*> rects; | 75 MockCanvas mockCanvas(store); |
65 grid.search(query(0.0f, 0.0f, 1.0f, 1.0f), &rects); | 76 picture.draw(&mockCanvas); |
66 REPORTER_ASSERT(reporter, 1 == rects.count()); | 77 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
67 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0); | 78 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
68 } | 79 } |
69 { | 80 { |
70 SkTDArray<void*> rects; | 81 MockCanvas mockCanvas(store); |
71 grid.search(query(7.99f, 7.99f, 1.0f, 1.0f), &rects); | 82 mockCanvas.translate(-7.99f, -7.99f); |
72 REPORTER_ASSERT(reporter, 1 == rects.count()); | 83 picture.draw(&mockCanvas); |
73 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0); | 84 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 85 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
74 } | 86 } |
75 // Corner overlap | 87 // Corner overlap |
76 { | 88 { |
77 SkTDArray<void*> rects; | 89 MockCanvas mockCanvas(store); |
78 grid.search(query(9.5f, 9.5f, 1.0f, 1.0f), &rects); | 90 mockCanvas.translate(-9.5f, -9.5f); |
79 REPORTER_ASSERT(reporter, 2 == rects.count()); | 91 picture.draw(&mockCanvas); |
80 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0); | 92 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
81 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0); | 93 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
| 94 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); |
82 } | 95 } |
83 // Intersect bottom right tile, but does not overlap rect 2 | 96 // Intersect bottom right tile, but does not overlap rect 2 |
84 { | 97 { |
85 SkTDArray<void*> rects; | 98 MockCanvas mockCanvas(store); |
86 grid.search(query(16.0f, 16.0f, 1.0f, 1.0f), &rects); | 99 mockCanvas.translate(-16.0f, -16.0f); |
87 REPORTER_ASSERT(reporter, 1 == rects.count()); | 100 picture.draw(&mockCanvas); |
88 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0); | 101 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 102 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
89 } | 103 } |
90 // Out of bounds queries, snap to border tiles | 104 // Out of bounds queries, snap to border tiles |
91 { | 105 { |
92 SkTDArray<void*> rects; | 106 MockCanvas mockCanvas(store); |
93 grid.search(query(-2.0f, 0.0f, 1.0f, 1.0f), &rects); | 107 mockCanvas.translate(2.0f, 0.0f); |
94 REPORTER_ASSERT(reporter, 1 == rects.count()); | 108 picture.draw(&mockCanvas); |
95 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0); | 109 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 110 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
96 } | 111 } |
97 { | 112 { |
98 SkTDArray<void*> rects; | 113 MockCanvas mockCanvas(store); |
99 grid.search(query(0.0f, -2.0f, 1.0f, 1.0f), &rects); | 114 mockCanvas.translate(0.0f, 2.0f); |
100 REPORTER_ASSERT(reporter, 1 == rects.count()); | 115 picture.draw(&mockCanvas); |
101 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0); | 116 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 117 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
102 } | 118 } |
103 { | 119 { |
104 SkTDArray<void*> rects; | 120 MockCanvas mockCanvas(store); |
105 grid.search(query(22.0f, 16.0f, 1.0f, 1.0f), &rects); | 121 mockCanvas.translate(-22.0f, -16.0f); |
106 REPORTER_ASSERT(reporter, 1 == rects.count()); | 122 picture.draw(&mockCanvas); |
107 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0); | 123 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 124 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
108 } | 125 } |
109 { | 126 { |
110 SkTDArray<void*> rects; | 127 MockCanvas mockCanvas(store); |
111 grid.search(query(16.0f, 22.0f, 1.0f, 1.0f), &rects); | 128 mockCanvas.translate(-16.0f, -22.0f); |
112 REPORTER_ASSERT(reporter, 1 == rects.count()); | 129 picture.draw(&mockCanvas); |
113 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0); | 130 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 131 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
114 } | 132 } |
115 } | 133 } |
116 | 134 |
117 DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) { | 135 DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) { |
118 // Use SkTileGridPicture to generate a SkTileGrid with a helper | 136 // Use SkTileGridPicture to generate a SkTileGrid with a helper |
119 SkTileGridPicture::TileGridInfo info; | 137 SkTileGridPicture::TileGridInfo info; |
120 info.fMargin.set(1, 1); | 138 info.fMargin.set(1, 1); |
121 info.fOffset.set(-1, -1); | 139 info.fOffset.set(-1, -1); |
122 info.fTileInterval.set(8, 8); | 140 info.fTileInterval.set(8, 8); |
| 141 SkTileGridPicture picture(20, 20, info); |
123 | 142 |
124 // rect landing entirely in top left tile | 143 // rect landing entirely in top left tile |
125 SkIRect rect1 = SkIRect::MakeXYWH(0, 0, 1, 1); | 144 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 145 SkIntToScalar(1), SkIntToScalar(1)); |
126 // rect landing entirely in center tile | 146 // rect landing entirely in center tile |
127 SkIRect rect2 = SkIRect::MakeXYWH(12, 12, 1, 1); | 147 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(12), SkIntToScalar(12), |
| 148 SkIntToScalar(1), SkIntToScalar(1)); |
128 // rect landing entirely in bottomright tile | 149 // rect landing entirely in bottomright tile |
129 SkIRect rect3 = SkIRect::MakeXYWH(19, 19, 1, 1); | 150 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(19), SkIntToScalar(19), |
130 SkTileGrid grid(3, 3, info, SkTileGridNextDatum<SkPictureStateTree::Draw>); | 151 SkIntToScalar(1), SkIntToScalar(1)); |
131 grid.insert(&rect1, rect1, true); | 152 SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForCli
ppedPlayback_RecordingFlag); |
132 grid.insert(&rect2, rect2, true); | 153 SkPaint paint; |
133 grid.insert(&rect3, rect3, true); | 154 canvas->drawRect(rect1, paint); |
| 155 canvas->drawRect(rect2, paint); |
| 156 canvas->drawRect(rect3, paint); |
| 157 picture.endRecording(); |
134 | 158 |
| 159 SkBitmap tileBitmap; |
| 160 tileBitmap.allocN32Pixels(10, 10); |
| 161 SkBitmap moreThanATileBitmap; |
| 162 moreThanATileBitmap.allocN32Pixels(11, 11); |
| 163 SkBitmap tinyBitmap; |
| 164 tinyBitmap.allocN32Pixels(2, 2); |
135 // Test parts of top-left tile | 165 // Test parts of top-left tile |
136 { | 166 { |
137 // 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 |
138 // 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, |
139 SkTDArray<void*> rects; | 169 MockCanvas mockCanvas(tileBitmap); |
140 grid.search(query(0.0f, 0.0f, 10.0f, 10.0f), &rects); | 170 picture.draw(&mockCanvas); |
141 REPORTER_ASSERT(reporter, 1 == rects.count()); | 171 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
142 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0); | 172 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
143 } | 173 } |
144 { | 174 { |
145 // Encroaching border by one pixel | 175 // Encroaching border by one pixel |
146 SkTDArray<void*> rects; | 176 MockCanvas mockCanvas(moreThanATileBitmap); |
147 grid.search(query(0.0f, 0.0f, 11.0f, 11.0f), &rects); | 177 picture.draw(&mockCanvas); |
148 REPORTER_ASSERT(reporter, 2 == rects.count()); | 178 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
149 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0); | 179 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
150 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0); | 180 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); |
151 } | 181 } |
152 { | 182 { |
153 // Tile stride is 8 (tileWidth - 2 * border pixels | 183 // Tile stride is 8 (tileWidth - 2 * border pixels |
154 // so translating by 8, should make query grid-aligned | 184 // so translating by 8, should make query grid-aligned |
155 // with middle tile. | 185 // with middle tile. |
156 SkTDArray<void*> rects; | 186 MockCanvas mockCanvas(tileBitmap); |
157 grid.search(query(8.0f, 8.0f, 10.0f, 10.0f), &rects); | 187 mockCanvas.translate(SkIntToScalar(-8), SkIntToScalar(-8)); |
158 REPORTER_ASSERT(reporter, 1 == rects.count()); | 188 picture.draw(&mockCanvas); |
159 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0); | 189 REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 190 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
160 } | 191 } |
161 { | 192 { |
162 SkTDArray<void*> rects; | 193 MockCanvas mockCanvas(tileBitmap); |
163 grid.search(query(7.9f, 7.9f, 10.0f, 10.0f), &rects); | 194 mockCanvas.translate(-7.9f, -7.9f); |
164 REPORTER_ASSERT(reporter, 2 == rects.count()); | 195 picture.draw(&mockCanvas); |
165 REPORTER_ASSERT(reporter, rects.find(&rect1) >= 0); | 196 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
166 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0); | 197 REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
| 198 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); |
167 } | 199 } |
168 { | 200 { |
169 SkTDArray<void*> rects; | 201 MockCanvas mockCanvas(tileBitmap); |
170 grid.search(query(8.1f, 8.1f, 10.0f, 10.0f), &rects); | 202 mockCanvas.translate(-8.1f, -8.1f); |
171 REPORTER_ASSERT(reporter, 2 == rects.count()); | 203 picture.draw(&mockCanvas); |
172 REPORTER_ASSERT(reporter, rects.find(&rect2) >= 0); | 204 REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
173 REPORTER_ASSERT(reporter, rects.find(&rect3) >= 0); | 205 REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
| 206 REPORTER_ASSERT(reporter, rect3 == mockCanvas.fRects[1]); |
174 } | 207 } |
175 { | 208 { |
176 // Regression test for crbug.com/234688 | 209 // Regression test for crbug.com/234688 |
177 // 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 |
178 // adjusted region, sitting right on top of the tile boundary. | 211 // adjusted region, sitting right on top of the tile boundary. |
179 SkTDArray<void*> rects; | 212 MockCanvas mockCanvas(tinyBitmap); |
180 grid.search(query(8.0f, 8.0f, 2.0f, 2.0f), &rects); | 213 mockCanvas.translate(-8.0f, -8.0f); |
| 214 picture.draw(&mockCanvas); |
181 // 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 |
182 // because the result is numerically unstable (floating point equality). | 216 // 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 v
alid | 217 // The content of any one of the four tiles of the tilegrid would be a v
alid |
184 // 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 |
185 // recorded in all four tiles. | 219 // recorded in all four tiles. |
186 } | 220 } |
187 } | 221 } |
188 | 222 |
189 DEF_TEST(TileGrid, reporter) { | 223 DEF_TEST(TileGrid, reporter) { |
190 // Out of bounds | 224 // Out of bounds |
(...skipping 16 matching lines...) Expand all Loading... |
207 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile
, 1); | 241 verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile
, 1); |
208 verifyTileHits(reporter, SkIRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile
, 1); | 242 verifyTileHits(reporter, SkIRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile
, 1); |
209 | 243 |
210 // BBoxes that overlap tiles | 244 // BBoxes that overlap tiles |
211 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); |
212 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile | | 246 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile | |
213 kBottomLeft_Tile); | 247 kBottomLeft_Tile); |
214 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile); | 248 verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile); |
215 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile); | 249 verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile); |
216 } | 250 } |
OLD | NEW |