epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 11 | #include "SkDiscardableMemoryPool.h" |
commit-bot@chromium.org | 2d970b5 | 2014-05-27 14:14:22 +0000 | [diff] [blame] | 12 | #include "SkImageGeneratorPriv.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "SkMatrixUtils.h" |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 16 | #include "SkShader.h" |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 17 | #include "SkSurface.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 18 | #include "Test.h" |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 19 | |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 20 | // A BitmapFactory that always fails when asked to return pixels. |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 21 | class FailureImageGenerator : public SkImageGenerator { |
| 22 | public: |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 23 | FailureImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(100, 100)) {} |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 24 | protected: |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 25 | // default onGetPixels() returns kUnimplemented, which is what we want. |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 26 | }; |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 27 | |
| 28 | // crbug.com/295895 |
| 29 | // Crashing in skia when a pixelref fails in lockPixels |
| 30 | // |
| 31 | static void test_faulty_pixelref(skiatest::Reporter* reporter) { |
| 32 | // need a cache, but don't expect to use it, so the budget is not critical |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 33 | SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 34 | SkDiscardableMemoryPool::Create(10 * 1000, NULL)); |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 35 | SkBitmap bm; |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 36 | bool installSuccess = SkInstallDiscardablePixelRef(SkNEW(FailureImageGenerator), &bm, pool); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, installSuccess); |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 38 | // now our bitmap has a pixelref, but we know it will fail to lock |
| 39 | |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 40 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(200, 200)); |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 41 | SkCanvas* canvas = surface->getCanvas(); |
| 42 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 43 | const SkFilterQuality levels[] = { |
| 44 | kNone_SkFilterQuality, |
| 45 | kLow_SkFilterQuality, |
| 46 | kMedium_SkFilterQuality, |
| 47 | kHigh_SkFilterQuality, |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | SkPaint paint; |
| 51 | canvas->scale(2, 2); // need a scale, otherwise we may ignore filtering |
| 52 | for (size_t i = 0; i < SK_ARRAY_COUNT(levels); ++i) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 53 | paint.setFilterQuality(levels[i]); |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 54 | canvas->drawBitmap(bm, 0, 0, &paint); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 60 | static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) { |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 61 | mat->setIdentity(); |
| 62 | if (mask & SkMatrix::kTranslate_Mask) { |
| 63 | mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1()); |
| 64 | } |
| 65 | if (mask & SkMatrix::kScale_Mask) { |
| 66 | mat->postScale(rand.nextSScalar1(), rand.nextSScalar1()); |
| 67 | } |
| 68 | if (mask & SkMatrix::kAffine_Mask) { |
| 69 | mat->postRotate(rand.nextSScalar1() * 360); |
| 70 | } |
| 71 | if (mask & SkMatrix::kPerspective_Mask) { |
| 72 | mat->setPerspX(rand.nextSScalar1()); |
| 73 | mat->setPerspY(rand.nextSScalar1()); |
| 74 | } |
| 75 | } |
| 76 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 77 | static void rand_size(SkISize* size, SkRandom& rand) { |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 78 | size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF); |
| 79 | } |
| 80 | |
| 81 | static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size, |
| 82 | unsigned bits) { |
| 83 | return SkTreatAsSprite(mat, size.width(), size.height(), bits); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void test_treatAsSprite(skiatest::Reporter* reporter) { |
| 87 | const unsigned bilerBits = kSkSubPixelBitsForBilerp; |
| 88 | |
| 89 | SkMatrix mat; |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 90 | SkISize size; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 91 | SkRandom rand; |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 92 | |
| 93 | // assert: translate-only no-filter can always be treated as sprite |
| 94 | for (int i = 0; i < 1000; ++i) { |
| 95 | rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask); |
| 96 | for (int j = 0; j < 1000; ++j) { |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 97 | rand_size(&size, rand); |
| 98 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
skia.committer@gmail.com | 422188f | 2013-01-03 02:01:32 +0000 | [diff] [blame] | 101 | |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 102 | // assert: rotate/perspect is never treated as sprite |
| 103 | for (int i = 0; i < 1000; ++i) { |
| 104 | rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask); |
| 105 | for (int j = 0; j < 1000; ++j) { |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 106 | rand_size(&size, rand); |
| 107 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, 0)); |
| 108 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 112 | size.set(500, 600); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 114 | const SkScalar tooMuchSubpixel = 100.1f; |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 115 | mat.setTranslate(tooMuchSubpixel, 0); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 116 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 117 | mat.setTranslate(0, tooMuchSubpixel); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 119 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 120 | const SkScalar tinySubPixel = 100.02f; |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 121 | mat.setTranslate(tinySubPixel, 0); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 123 | mat.setTranslate(0, tinySubPixel); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
skia.committer@gmail.com | 422188f | 2013-01-03 02:01:32 +0000 | [diff] [blame] | 125 | |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 126 | const SkScalar twoThirds = SK_Scalar1 * 2 / 3; |
reed | 67d71c8 | 2015-05-12 09:47:22 -0700 | [diff] [blame^] | 127 | const SkScalar bigScale = (size.width() + twoThirds) / size.width(); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 128 | mat.setScale(bigScale, bigScale); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 129 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, false)); |
| 130 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
skia.committer@gmail.com | 422188f | 2013-01-03 02:01:32 +0000 | [diff] [blame] | 131 | |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 132 | const SkScalar oneThird = SK_Scalar1 / 3; |
reed | 67d71c8 | 2015-05-12 09:47:22 -0700 | [diff] [blame^] | 133 | const SkScalar smallScale = (size.width() + oneThird) / size.width(); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 134 | mat.setScale(smallScale, smallScale); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 135 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false)); |
| 136 | REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits)); |
skia.committer@gmail.com | 422188f | 2013-01-03 02:01:32 +0000 | [diff] [blame] | 137 | |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 138 | const SkScalar oneFortyth = SK_Scalar1 / 40; |
reed | 67d71c8 | 2015-05-12 09:47:22 -0700 | [diff] [blame^] | 139 | const SkScalar tinyScale = (size.width() + oneFortyth) / size.width(); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 140 | mat.setScale(tinyScale, tinyScale); |
reed@google.com | ae57358 | 2013-01-03 15:22:40 +0000 | [diff] [blame] | 141 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false)); |
| 142 | REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits)); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 143 | } |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 144 | |
| 145 | static void assert_ifDrawnTo(skiatest::Reporter* reporter, |
| 146 | const SkBitmap& bm, bool shouldBeDrawn) { |
| 147 | for (int y = 0; y < bm.height(); ++y) { |
| 148 | for (int x = 0; x < bm.width(); ++x) { |
| 149 | if (shouldBeDrawn) { |
commit-bot@chromium.org | c2050e3 | 2013-07-15 13:10:31 +0000 | [diff] [blame] | 150 | if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) { |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 151 | REPORTER_ASSERT(reporter, false); |
| 152 | return; |
| 153 | } |
| 154 | } else { |
| 155 | // should not be drawn |
commit-bot@chromium.org | c2050e3 | 2013-07-15 13:10:31 +0000 | [diff] [blame] | 156 | if (SK_ColorTRANSPARENT != *bm.getAddr32(x, y)) { |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, false); |
| 158 | return; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void test_wacky_bitmapshader(skiatest::Reporter* reporter, |
| 166 | int width, int height, bool shouldBeDrawn) { |
| 167 | SkBitmap dev; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 168 | dev.allocN32Pixels(0x56F, 0x4f6); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 169 | 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] | 170 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 171 | SkMatrix matrix; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 172 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 173 | SkCanvas c(dev); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 174 | matrix.setAll(-119.34097f, |
| 175 | -43.436558f, |
| 176 | 93489.945f, |
| 177 | 43.436558f, |
| 178 | -119.34097f, |
| 179 | 123.98426f, |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 180 | 0, 0, SK_Scalar1); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 181 | c.concat(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 182 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 183 | SkBitmap bm; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 184 | if (bm.tryAllocN32Pixels(width, height)) { |
| 185 | // allow this to fail silently, to test the code downstream |
| 186 | } |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 187 | bm.eraseColor(SK_ColorRED); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 188 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 189 | matrix.setAll(0.0078740157f, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 190 | 0, |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 191 | SkIntToScalar(249), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 192 | 0, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 193 | 0.0078740157f, |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 194 | SkIntToScalar(239), |
| 195 | 0, 0, SK_Scalar1); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 196 | SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, |
| 197 | SkShader::kRepeat_TileMode, &matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 198 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 199 | SkPaint paint; |
| 200 | paint.setShader(s)->unref(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 201 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 202 | SkRect r = SkRect::MakeXYWH(681, 239, 695, 253); |
| 203 | c.drawRect(r, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 204 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 205 | assert_ifDrawnTo(reporter, dev, shouldBeDrawn); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Original bug was asserting that the matrix-proc had generated a (Y) value |
| 210 | * that was out of range. This led (in the release build) to the sampler-proc |
| 211 | * reading memory out-of-bounds of the original bitmap. |
| 212 | * |
| 213 | * We were numerically overflowing our 16bit coordinates that we communicate |
| 214 | * between these two procs. The fixes was in two parts: |
| 215 | * |
| 216 | * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we |
| 217 | * can't represent those coordinates in our transport format (yet). |
| 218 | * 2. Perform an unsigned shift during the calculation, so we don't get |
| 219 | * sign-extension bleed when packing the two values (X,Y) into our 32bit |
| 220 | * slot. |
| 221 | * |
| 222 | * This tests exercises the original setup, plus 3 more to ensure that we can, |
| 223 | * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total |
| 224 | * memory allocation limit). |
| 225 | */ |
| 226 | static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) { |
| 227 | static const struct { |
| 228 | int fWidth; |
| 229 | int fHeight; |
| 230 | bool fExpectedToDraw; |
| 231 | } gTests[] = { |
| 232 | { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K) |
| 233 | { 0xFFFF, 0x7f, true }, // should draw, test max width |
| 234 | { 0x7f, 0xFFFF, true }, // should draw, test max height |
| 235 | { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM) |
| 236 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 237 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 238 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) { |
| 239 | test_wacky_bitmapshader(reporter, |
| 240 | gTests[i].fWidth, gTests[i].fHeight, |
| 241 | gTests[i].fExpectedToDraw); |
| 242 | } |
| 243 | } |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 244 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 245 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 246 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 247 | static void test_nan_antihair() { |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 248 | SkBitmap bm; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 249 | bm.allocN32Pixels(20, 20); |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 250 | |
| 251 | SkCanvas canvas(bm); |
| 252 | |
| 253 | SkPath path; |
| 254 | path.moveTo(0, 0); |
| 255 | path.lineTo(10, SK_ScalarNaN); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 256 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 257 | SkPaint paint; |
| 258 | paint.setAntiAlias(true); |
| 259 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 260 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 261 | // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...) |
| 262 | // this would trigger an assert/crash. |
| 263 | // |
| 264 | // see rev. 3558 |
| 265 | canvas.drawPath(path, paint); |
| 266 | } |
| 267 | |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 268 | static bool check_for_all_zeros(const SkBitmap& bm) { |
| 269 | SkAutoLockPixels alp(bm); |
| 270 | |
| 271 | size_t count = bm.width() * bm.bytesPerPixel(); |
| 272 | for (int y = 0; y < bm.height(); y++) { |
| 273 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y)); |
| 274 | for (size_t i = 0; i < count; i++) { |
| 275 | if (ptr[i]) { |
| 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | static const int gWidth = 256; |
| 284 | static const int gHeight = 256; |
| 285 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 286 | static void create(SkBitmap* bm, SkColor color) { |
| 287 | bm->allocN32Pixels(gWidth, gHeight); |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 288 | bm->eraseColor(color); |
| 289 | } |
| 290 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 291 | DEF_TEST(DrawBitmapRect, reporter) { |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 292 | SkBitmap src, dst; |
| 293 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 294 | create(&src, 0xFFFFFFFF); |
| 295 | create(&dst, 0); |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 296 | |
| 297 | SkCanvas canvas(dst); |
| 298 | |
| 299 | SkIRect srcR = { gWidth, 0, gWidth + 16, 16 }; |
| 300 | SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) }; |
| 301 | |
| 302 | canvas.drawBitmapRect(src, &srcR, dstR, NULL); |
| 303 | |
| 304 | // ensure that we draw nothing if srcR does not intersect the bitmap |
| 305 | REPORTER_ASSERT(reporter, check_for_all_zeros(dst)); |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 306 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 307 | test_nan_antihair(); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 308 | test_giantrepeat_crbug118018(reporter); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 309 | |
| 310 | test_treatAsSprite(reporter); |
reed@google.com | 23d0ab7 | 2013-10-15 20:14:00 +0000 | [diff] [blame] | 311 | test_faulty_pixelref(reporter); |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 312 | } |