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