OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF); | 87 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF); |
88 } | 88 } |
89 | 89 |
90 static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size, | 90 static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size, |
91 unsigned bits) { | 91 unsigned bits) { |
92 return SkTreatAsSprite(mat, size.width(), size.height(), bits); | 92 return SkTreatAsSprite(mat, size.width(), size.height(), bits); |
93 } | 93 } |
94 | 94 |
95 static void test_treatAsSprite(skiatest::Reporter* reporter) { | 95 static void test_treatAsSprite(skiatest::Reporter* reporter) { |
96 const unsigned bilerBits = kSkSubPixelBitsForBilerp; | 96 const unsigned bilerBits = kSkSubPixelBitsForBilerp; |
| 97 const unsigned nearestNeighborBits = 0; |
97 | 98 |
98 SkMatrix mat; | 99 SkMatrix mat; |
99 SkISize size; | 100 SkISize size; |
100 SkRandom rand; | 101 SkRandom rand; |
101 | 102 |
102 // assert: translate-only no-filter can always be treated as sprite | 103 // assert: translate-only no-filter can always be treated as sprite |
103 for (int i = 0; i < 1000; ++i) { | 104 for (int i = 0; i < 1000; ++i) { |
104 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask); | 105 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask); |
105 for (int j = 0; j < 1000; ++j) { | 106 for (int j = 0; j < 1000; ++j) { |
106 rand_size(&size, rand); | 107 rand_size(&size, rand); |
(...skipping 21 matching lines...) Expand all Loading... |
128 | 129 |
129 const SkScalar tinySubPixel = 100.02f; | 130 const SkScalar tinySubPixel = 100.02f; |
130 mat.setTranslate(tinySubPixel, 0); | 131 mat.setTranslate(tinySubPixel, 0); |
131 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); | 132 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
132 mat.setTranslate(0, tinySubPixel); | 133 mat.setTranslate(0, tinySubPixel); |
133 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); | 134 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
134 | 135 |
135 const SkScalar twoThirds = SK_Scalar1 * 2 / 3; | 136 const SkScalar twoThirds = SK_Scalar1 * 2 / 3; |
136 const SkScalar bigScale = SkScalarDiv(size.width() + twoThirds, size.width()
); | 137 const SkScalar bigScale = SkScalarDiv(size.width() + twoThirds, size.width()
); |
137 mat.setScale(bigScale, bigScale); | 138 mat.setScale(bigScale, bigScale); |
138 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, false)); | 139 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, nearestNeighborBits)); |
139 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); | 140 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
140 | 141 |
141 const SkScalar oneThird = SK_Scalar1 / 3; | 142 const SkScalar oneThird = SK_Scalar1 / 3; |
142 const SkScalar smallScale = SkScalarDiv(size.width() + oneThird, size.width(
)); | 143 const SkScalar smallScale = SkScalarDiv(size.width() + oneThird, size.width(
)); |
143 mat.setScale(smallScale, smallScale); | 144 mat.setScale(smallScale, smallScale); |
144 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false)); | 145 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, nearestNeighborBits)); |
145 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); | 146 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
146 | 147 |
147 const SkScalar oneFortyth = SK_Scalar1 / 40; | 148 const SkScalar oneFortyth = SK_Scalar1 / 40; |
148 const SkScalar tinyScale = SkScalarDiv(size.width() + oneFortyth, size.width
()); | 149 const SkScalar tinyScale = SkScalarDiv(size.width() + oneFortyth, size.width
()); |
149 mat.setScale(tinyScale, tinyScale); | 150 mat.setScale(tinyScale, tinyScale); |
150 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false)); | 151 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, nearestNeighborBits)); |
151 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); | 152 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
152 } | 153 } |
153 | 154 |
154 static void assert_ifDrawnTo(skiatest::Reporter* reporter, | 155 static void assert_ifDrawnTo(skiatest::Reporter* reporter, |
155 const SkBitmap& bm, bool shouldBeDrawn) { | 156 const SkBitmap& bm, bool shouldBeDrawn) { |
156 for (int y = 0; y < bm.height(); ++y) { | 157 for (int y = 0; y < bm.height(); ++y) { |
157 for (int x = 0; x < bm.width(); ++x) { | 158 for (int x = 0; x < bm.width(); ++x) { |
158 if (shouldBeDrawn) { | 159 if (shouldBeDrawn) { |
159 if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) { | 160 if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) { |
160 REPORTER_ASSERT(reporter, false); | 161 REPORTER_ASSERT(reporter, false); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y)); | 281 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y)); |
281 for (size_t i = 0; i < count; i++) { | 282 for (size_t i = 0; i < count; i++) { |
282 if (ptr[i]) { | 283 if (ptr[i]) { |
283 return false; | 284 return false; |
284 } | 285 } |
285 } | 286 } |
286 } | 287 } |
287 return true; | 288 return true; |
288 } | 289 } |
289 | 290 |
| 291 static void test_on_grid(skiatest::Reporter* reporter, int w, int h, const SkRec
t& r, |
| 292 SkScalar delta, int bits, bool expected) { |
| 293 REPORTER_ASSERT(reporter, SkRectIsOnGrid(w, h, r.makeInset(delta, 0), bits)
== expected); |
| 294 REPORTER_ASSERT(reporter, SkRectIsOnGrid(w, h, r.makeInset(-delta, 0), bits)
== expected); |
| 295 REPORTER_ASSERT(reporter, SkRectIsOnGrid(w, h, r.makeInset(0, delta), bits)
== expected); |
| 296 REPORTER_ASSERT(reporter, SkRectIsOnGrid(w, h, r.makeInset(0, -delta), bits)
== expected); |
| 297 |
| 298 if (0 == bits) { |
| 299 expected = true; |
| 300 } |
| 301 REPORTER_ASSERT(reporter, SkRectIsOnGrid(w, h, r.makeOffset(delta, 0), bits)
== expected); |
| 302 REPORTER_ASSERT(reporter, SkRectIsOnGrid(w, h, r.makeOffset(-delta, 0), bits
) == expected); |
| 303 REPORTER_ASSERT(reporter, SkRectIsOnGrid(w, h, r.makeOffset(0, delta), bits)
== expected); |
| 304 REPORTER_ASSERT(reporter, SkRectIsOnGrid(w, h, r.makeOffset(0, -delta), bits
) == expected); |
| 305 |
| 306 } |
| 307 |
| 308 static void test_on_grid(skiatest::Reporter* reporter, int bits, SkScalar cutOff
) { |
| 309 const int width = 100; |
| 310 const int height = 100; |
| 311 const SkScalar eps = 0.00001f; |
| 312 |
| 313 SkRect r; |
| 314 r.setWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 315 REPORTER_ASSERT(reporter, SkRectIsOnGrid(width, height, r, bits)); |
| 316 |
| 317 test_on_grid(reporter, width, height, r, cutOff - eps, bits, true); |
| 318 test_on_grid(reporter, width, height, r, cutOff + eps, bits, false); |
| 319 } |
| 320 |
| 321 static void test_on_grid(skiatest::Reporter* reporter) { |
| 322 for (int bits = 0; bits <= 8; ++bits) { |
| 323 // The cutoff for being on the grid is 0.5 * (1/grid_size) |
| 324 const SkScalar cutOff = SkScalarInvert(1 << (bits + 1)); |
| 325 test_on_grid(reporter, bits, cutOff); |
| 326 } |
| 327 } |
| 328 |
290 static const int gWidth = 256; | 329 static const int gWidth = 256; |
291 static const int gHeight = 256; | 330 static const int gHeight = 256; |
292 | 331 |
293 static void create(SkBitmap* bm, SkColor color) { | 332 static void create(SkBitmap* bm, SkColor color) { |
294 bm->allocN32Pixels(gWidth, gHeight); | 333 bm->allocN32Pixels(gWidth, gHeight); |
295 bm->eraseColor(color); | 334 bm->eraseColor(color); |
296 } | 335 } |
297 | 336 |
298 DEF_TEST(DrawBitmapRect, reporter) { | 337 DEF_TEST(DrawBitmapRect, reporter) { |
| 338 test_on_grid(reporter); |
| 339 |
299 SkBitmap src, dst; | 340 SkBitmap src, dst; |
300 | 341 |
301 create(&src, 0xFFFFFFFF); | 342 create(&src, 0xFFFFFFFF); |
302 create(&dst, 0); | 343 create(&dst, 0); |
303 | 344 |
304 SkCanvas canvas(dst); | 345 SkCanvas canvas(dst); |
305 | 346 |
306 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 }; | 347 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 }; |
307 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) }; | 348 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) }; |
308 | 349 |
309 canvas.drawBitmapRect(src, &srcR, dstR, NULL); | 350 canvas.drawBitmapRect(src, &srcR, dstR, NULL); |
310 | 351 |
311 // ensure that we draw nothing if srcR does not intersect the bitmap | 352 // ensure that we draw nothing if srcR does not intersect the bitmap |
312 REPORTER_ASSERT(reporter, check_for_all_zeros(dst)); | 353 REPORTER_ASSERT(reporter, check_for_all_zeros(dst)); |
313 | 354 |
314 test_nan_antihair(); | 355 test_nan_antihair(); |
315 test_giantrepeat_crbug118018(reporter); | 356 test_giantrepeat_crbug118018(reporter); |
316 | 357 |
317 test_treatAsSprite(reporter); | 358 test_treatAsSprite(reporter); |
318 test_faulty_pixelref(reporter); | 359 test_faulty_pixelref(reporter); |
319 } | 360 } |
OLD | NEW |