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