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 | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 11 | #include "SkData.h" |
| 12 | #include "SkPaint.h" |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 13 | #include "SkShader.h" |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 14 | #include "SkSurface.h" |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
| 16 | #include "SkMatrixUtils.h" |
| 17 | |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 18 | #include "SkLazyPixelRef.h" |
| 19 | #include "SkLruImageCache.h" |
| 20 | |
| 21 | // A BitmapFactory that always fails when asked to return pixels. |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 22 | static bool FailureDecoder(const void* data, size_t length, SkImageInfo* info, |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 23 | const SkBitmapFactory::Target* target) { |
| 24 | if (info) { |
| 25 | info->fWidth = info->fHeight = 100; |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 26 | info->fColorType = kRGBA_8888_SkColorType; |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 27 | info->fAlphaType = kPremul_SkAlphaType; |
| 28 | } |
| 29 | // this will deliberately return false if they are asking us to decode |
| 30 | // into pixels. |
| 31 | return NULL == target; |
| 32 | } |
| 33 | |
| 34 | // crbug.com/295895 |
| 35 | // Crashing in skia when a pixelref fails in lockPixels |
| 36 | // |
| 37 | static void test_faulty_pixelref(skiatest::Reporter* reporter) { |
| 38 | // need a cache, but don't expect to use it, so the budget is not critical |
| 39 | SkLruImageCache cache(10 * 1000); |
| 40 | // construct a garbage data represent "bad" encoded data. |
| 41 | SkAutoDataUnref data(SkData::NewFromMalloc(malloc(1000), 1000)); |
| 42 | SkAutoTUnref<SkPixelRef> pr(new SkLazyPixelRef(data, FailureDecoder, &cache)); |
skia.committer@gmail.com | 4726291 | 2013-10-16 07:02:24 +0000 | [diff] [blame] | 43 | |
reed@google.com | cef5bb4 | 2013-10-15 20:05:24 +0000 | [diff] [blame] | 44 | SkBitmap bm; |
| 45 | bm.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 46 | bm.setPixelRef(pr); |
| 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 | |
| 123 | const SkScalar tooMuchSubpixel = SkFloatToScalar(100.1f); |
| 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 | |
| 129 | const SkScalar tinySubPixel = SkFloatToScalar(100.02f); |
| 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; |
| 177 | dev.setConfig(SkBitmap::kARGB_8888_Config, 0x56F, 0x4f6); |
| 178 | dev.allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 179 | 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] | 180 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 181 | SkMatrix 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 | SkCanvas c(dev); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 184 | matrix.setAll(SkFloatToScalar(-119.34097f), |
| 185 | SkFloatToScalar(-43.436558f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 186 | SkFloatToScalar(93489.945f), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 187 | SkFloatToScalar(43.436558f), |
| 188 | SkFloatToScalar(-119.34097f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 189 | SkFloatToScalar(123.98426f), |
| 190 | 0, 0, SK_Scalar1); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 191 | c.concat(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 192 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 193 | SkBitmap bm; |
| 194 | bm.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 195 | bm.allocPixels(); |
| 196 | bm.eraseColor(SK_ColorRED); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 197 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 198 | SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, |
| 199 | SkShader::kRepeat_TileMode); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 200 | matrix.setAll(SkFloatToScalar(0.0078740157f), |
| 201 | 0, |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 202 | SkIntToScalar(249), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 203 | 0, |
| 204 | SkFloatToScalar(0.0078740157f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 205 | SkIntToScalar(239), |
| 206 | 0, 0, SK_Scalar1); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 207 | s->setLocalMatrix(matrix); |
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 | SkPaint paint; |
| 210 | paint.setShader(s)->unref(); |
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 | SkRect r = SkRect::MakeXYWH(681, 239, 695, 253); |
| 213 | c.drawRect(r, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 214 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 215 | assert_ifDrawnTo(reporter, dev, shouldBeDrawn); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Original bug was asserting that the matrix-proc had generated a (Y) value |
| 220 | * that was out of range. This led (in the release build) to the sampler-proc |
| 221 | * reading memory out-of-bounds of the original bitmap. |
| 222 | * |
| 223 | * We were numerically overflowing our 16bit coordinates that we communicate |
| 224 | * between these two procs. The fixes was in two parts: |
| 225 | * |
| 226 | * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we |
| 227 | * can't represent those coordinates in our transport format (yet). |
| 228 | * 2. Perform an unsigned shift during the calculation, so we don't get |
| 229 | * sign-extension bleed when packing the two values (X,Y) into our 32bit |
| 230 | * slot. |
| 231 | * |
| 232 | * This tests exercises the original setup, plus 3 more to ensure that we can, |
| 233 | * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total |
| 234 | * memory allocation limit). |
| 235 | */ |
| 236 | static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) { |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 237 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 238 | static const struct { |
| 239 | int fWidth; |
| 240 | int fHeight; |
| 241 | bool fExpectedToDraw; |
| 242 | } gTests[] = { |
| 243 | { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K) |
| 244 | { 0xFFFF, 0x7f, true }, // should draw, test max width |
| 245 | { 0x7f, 0xFFFF, true }, // should draw, test max height |
| 246 | { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM) |
| 247 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 248 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 249 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) { |
| 250 | test_wacky_bitmapshader(reporter, |
| 251 | gTests[i].fWidth, gTests[i].fHeight, |
| 252 | gTests[i].fExpectedToDraw); |
| 253 | } |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 254 | #endif |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 255 | } |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 256 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 257 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 258 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 259 | static void test_nan_antihair() { |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 260 | SkBitmap bm; |
| 261 | bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20); |
| 262 | bm.allocPixels(); |
| 263 | |
| 264 | SkCanvas canvas(bm); |
| 265 | |
| 266 | SkPath path; |
| 267 | path.moveTo(0, 0); |
| 268 | path.lineTo(10, SK_ScalarNaN); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 269 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 270 | SkPaint paint; |
| 271 | paint.setAntiAlias(true); |
| 272 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 273 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 274 | // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...) |
| 275 | // this would trigger an assert/crash. |
| 276 | // |
| 277 | // see rev. 3558 |
| 278 | canvas.drawPath(path, paint); |
| 279 | } |
| 280 | |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 281 | static bool check_for_all_zeros(const SkBitmap& bm) { |
| 282 | SkAutoLockPixels alp(bm); |
| 283 | |
| 284 | size_t count = bm.width() * bm.bytesPerPixel(); |
| 285 | for (int y = 0; y < bm.height(); y++) { |
| 286 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y)); |
| 287 | for (size_t i = 0; i < count; i++) { |
| 288 | if (ptr[i]) { |
| 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | static const int gWidth = 256; |
| 297 | static const int gHeight = 256; |
| 298 | |
| 299 | static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { |
| 300 | bm->setConfig(config, gWidth, gHeight); |
| 301 | bm->allocPixels(); |
| 302 | bm->eraseColor(color); |
| 303 | } |
| 304 | |
| 305 | static void TestDrawBitmapRect(skiatest::Reporter* reporter) { |
| 306 | SkBitmap src, dst; |
| 307 | |
| 308 | create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 309 | create(&dst, SkBitmap::kARGB_8888_Config, 0); |
| 310 | |
| 311 | SkCanvas canvas(dst); |
| 312 | |
| 313 | SkIRect srcR = { gWidth, 0, gWidth + 16, 16 }; |
| 314 | SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) }; |
| 315 | |
| 316 | canvas.drawBitmapRect(src, &srcR, dstR, NULL); |
| 317 | |
| 318 | // ensure that we draw nothing if srcR does not intersect the bitmap |
| 319 | REPORTER_ASSERT(reporter, check_for_all_zeros(dst)); |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 320 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 321 | test_nan_antihair(); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 322 | test_giantrepeat_crbug118018(reporter); |
reed@google.com | ad51430 | 2013-01-02 20:19:45 +0000 | [diff] [blame] | 323 | |
| 324 | test_treatAsSprite(reporter); |
reed@google.com | 23d0ab7 | 2013-10-15 20:14:00 +0000 | [diff] [blame] | 325 | test_faulty_pixelref(reporter); |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | #include "TestClassDef.h" |
| 329 | DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect) |