epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 11 | #include "SkShader.h" |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
| 13 | #include "SkMatrixUtils.h" |
| 14 | |
| 15 | static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) { |
| 16 | mat->setIdentity(); |
| 17 | if (mask & SkMatrix::kTranslate_Mask) { |
| 18 | mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1()); |
| 19 | } |
| 20 | if (mask & SkMatrix::kScale_Mask) { |
| 21 | mat->postScale(rand.nextSScalar1(), rand.nextSScalar1()); |
| 22 | } |
| 23 | if (mask & SkMatrix::kAffine_Mask) { |
| 24 | mat->postRotate(rand.nextSScalar1() * 360); |
| 25 | } |
| 26 | if (mask & SkMatrix::kPerspective_Mask) { |
| 27 | mat->setPerspX(rand.nextSScalar1()); |
| 28 | mat->setPerspY(rand.nextSScalar1()); |
| 29 | } |
| 30 | } |
| 31 | |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 32 | static void rand_size(SkISize* size, SkRandom& rand) { |
| 33 | size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF); |
| 34 | } |
| 35 | |
| 36 | static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size, |
| 37 | unsigned bits) { |
| 38 | return SkTreatAsSprite(mat, size.width(), size.height(), bits); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static void test_treatAsSprite(skiatest::Reporter* reporter) { |
| 42 | const unsigned bilerBits = kSkSubPixelBitsForBilerp; |
| 43 | |
| 44 | SkMatrix mat; |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 45 | SkISize size; |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 46 | SkRandom rand; |
| 47 | |
| 48 | // assert: translate-only no-filter can always be treated as sprite |
| 49 | for (int i = 0; i < 1000; ++i) { |
| 50 | rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask); |
| 51 | for (int j = 0; j < 1000; ++j) { |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 52 | rand_size(&size, rand); |
| 53 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
skia.committer@gmail.com | 422188f | 2013-01-03 02:01:32 +0000 | [diff] [blame] | 56 | |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 57 | // assert: rotate/perspect is never treated as sprite |
| 58 | for (int i = 0; i < 1000; ++i) { |
| 59 | rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask); |
| 60 | for (int j = 0; j < 1000; ++j) { |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 61 | rand_size(&size, rand); |
| 62 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, 0)); |
| 63 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 67 | size.set(500, 600); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 68 | |
| 69 | const SkScalar tooMuchSubpixel = SkFloatToScalar(100.1f); |
| 70 | mat.setTranslate(tooMuchSubpixel, 0); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 72 | mat.setTranslate(0, tooMuchSubpixel); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 74 | |
| 75 | const SkScalar tinySubPixel = SkFloatToScalar(100.02f); |
| 76 | mat.setTranslate(tinySubPixel, 0); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 78 | mat.setTranslate(0, tinySubPixel); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
skia.committer@gmail.com | 422188f | 2013-01-03 02:01:32 +0000 | [diff] [blame] | 80 | |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 81 | const SkScalar twoThirds = SK_Scalar1 * 2 / 3; |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 82 | const SkScalar bigScale = SkScalarDiv(size.width() + twoThirds, size.width()); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 83 | mat.setScale(bigScale, bigScale); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, false)); |
| 85 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
skia.committer@gmail.com | 422188f | 2013-01-03 02:01:32 +0000 | [diff] [blame] | 86 | |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 87 | const SkScalar oneThird = SK_Scalar1 / 3; |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 88 | const SkScalar smallScale = SkScalarDiv(size.width() + oneThird, size.width()); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 89 | mat.setScale(smallScale, smallScale); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 90 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false)); |
| 91 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
skia.committer@gmail.com | 422188f | 2013-01-03 02:01:32 +0000 | [diff] [blame] | 92 | |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 93 | const SkScalar oneFortyth = SK_Scalar1 / 40; |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 94 | const SkScalar tinyScale = SkScalarDiv(size.width() + oneFortyth, size.width()); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 95 | mat.setScale(tinyScale, tinyScale); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 96 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false)); |
| 97 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 98 | } |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 99 | |
| 100 | static void assert_ifDrawnTo(skiatest::Reporter* reporter, |
| 101 | const SkBitmap& bm, bool shouldBeDrawn) { |
| 102 | for (int y = 0; y < bm.height(); ++y) { |
| 103 | for (int x = 0; x < bm.width(); ++x) { |
| 104 | if (shouldBeDrawn) { |
| 105 | if (0 == *bm.getAddr32(x, y)) { |
| 106 | REPORTER_ASSERT(reporter, false); |
| 107 | return; |
| 108 | } |
| 109 | } else { |
| 110 | // should not be drawn |
| 111 | if (*bm.getAddr32(x, y)) { |
| 112 | REPORTER_ASSERT(reporter, false); |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void test_wacky_bitmapshader(skiatest::Reporter* reporter, |
| 121 | int width, int height, bool shouldBeDrawn) { |
| 122 | SkBitmap dev; |
| 123 | dev.setConfig(SkBitmap::kARGB_8888_Config, 0x56F, 0x4f6); |
| 124 | dev.allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 125 | dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 126 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 127 | SkMatrix matrix; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 128 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 129 | SkCanvas c(dev); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 130 | matrix.setAll(SkFloatToScalar(-119.34097f), |
| 131 | SkFloatToScalar(-43.436558f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 132 | SkFloatToScalar(93489.945f), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 133 | SkFloatToScalar(43.436558f), |
| 134 | SkFloatToScalar(-119.34097f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 135 | SkFloatToScalar(123.98426f), |
| 136 | 0, 0, SK_Scalar1); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 137 | c.concat(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 138 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 139 | SkBitmap bm; |
| 140 | bm.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 141 | bm.allocPixels(); |
| 142 | bm.eraseColor(SK_ColorRED); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 143 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 144 | SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, |
| 145 | SkShader::kRepeat_TileMode); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 146 | matrix.setAll(SkFloatToScalar(0.0078740157f), |
| 147 | 0, |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 148 | SkIntToScalar(249), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 149 | 0, |
| 150 | SkFloatToScalar(0.0078740157f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 151 | SkIntToScalar(239), |
| 152 | 0, 0, SK_Scalar1); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 153 | s->setLocalMatrix(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 154 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 155 | SkPaint paint; |
| 156 | paint.setShader(s)->unref(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 157 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 158 | SkRect r = SkRect::MakeXYWH(681, 239, 695, 253); |
| 159 | c.drawRect(r, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 160 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 161 | assert_ifDrawnTo(reporter, dev, shouldBeDrawn); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Original bug was asserting that the matrix-proc had generated a (Y) value |
| 166 | * that was out of range. This led (in the release build) to the sampler-proc |
| 167 | * reading memory out-of-bounds of the original bitmap. |
| 168 | * |
| 169 | * We were numerically overflowing our 16bit coordinates that we communicate |
| 170 | * between these two procs. The fixes was in two parts: |
| 171 | * |
| 172 | * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we |
| 173 | * can't represent those coordinates in our transport format (yet). |
| 174 | * 2. Perform an unsigned shift during the calculation, so we don't get |
| 175 | * sign-extension bleed when packing the two values (X,Y) into our 32bit |
| 176 | * slot. |
| 177 | * |
| 178 | * This tests exercises the original setup, plus 3 more to ensure that we can, |
| 179 | * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total |
| 180 | * memory allocation limit). |
| 181 | */ |
| 182 | static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) { |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 183 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 184 | static const struct { |
| 185 | int fWidth; |
| 186 | int fHeight; |
| 187 | bool fExpectedToDraw; |
| 188 | } gTests[] = { |
| 189 | { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K) |
| 190 | { 0xFFFF, 0x7f, true }, // should draw, test max width |
| 191 | { 0x7f, 0xFFFF, true }, // should draw, test max height |
| 192 | { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM) |
| 193 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 194 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 195 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) { |
| 196 | test_wacky_bitmapshader(reporter, |
| 197 | gTests[i].fWidth, gTests[i].fHeight, |
| 198 | gTests[i].fExpectedToDraw); |
| 199 | } |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 200 | #endif |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 201 | } |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 202 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 203 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 204 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 205 | static void test_nan_antihair(skiatest::Reporter* reporter) { |
| 206 | SkBitmap bm; |
| 207 | bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20); |
| 208 | bm.allocPixels(); |
| 209 | |
| 210 | SkCanvas canvas(bm); |
| 211 | |
| 212 | SkPath path; |
| 213 | path.moveTo(0, 0); |
| 214 | path.lineTo(10, SK_ScalarNaN); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 215 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 216 | SkPaint paint; |
| 217 | paint.setAntiAlias(true); |
| 218 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 219 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 220 | // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...) |
| 221 | // this would trigger an assert/crash. |
| 222 | // |
| 223 | // see rev. 3558 |
| 224 | canvas.drawPath(path, paint); |
| 225 | } |
| 226 | |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 227 | static bool check_for_all_zeros(const SkBitmap& bm) { |
| 228 | SkAutoLockPixels alp(bm); |
| 229 | |
| 230 | size_t count = bm.width() * bm.bytesPerPixel(); |
| 231 | for (int y = 0; y < bm.height(); y++) { |
| 232 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y)); |
| 233 | for (size_t i = 0; i < count; i++) { |
| 234 | if (ptr[i]) { |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | static const int gWidth = 256; |
| 243 | static const int gHeight = 256; |
| 244 | |
| 245 | static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { |
| 246 | bm->setConfig(config, gWidth, gHeight); |
| 247 | bm->allocPixels(); |
| 248 | bm->eraseColor(color); |
| 249 | } |
| 250 | |
| 251 | static void TestDrawBitmapRect(skiatest::Reporter* reporter) { |
| 252 | SkBitmap src, dst; |
| 253 | |
| 254 | create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 255 | create(&dst, SkBitmap::kARGB_8888_Config, 0); |
| 256 | |
| 257 | SkCanvas canvas(dst); |
| 258 | |
| 259 | SkIRect srcR = { gWidth, 0, gWidth + 16, 16 }; |
| 260 | SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) }; |
| 261 | |
| 262 | canvas.drawBitmapRect(src, &srcR, dstR, NULL); |
| 263 | |
| 264 | // ensure that we draw nothing if srcR does not intersect the bitmap |
| 265 | REPORTER_ASSERT(reporter, check_for_all_zeros(dst)); |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 266 | |
| 267 | test_nan_antihair(reporter); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 268 | test_giantrepeat_crbug118018(reporter); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 269 | |
| 270 | test_treatAsSprite(reporter); |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | #include "TestClassDef.h" |
| 274 | DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect) |