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" |
| 12 | |
| 13 | static void assert_ifDrawnTo(skiatest::Reporter* reporter, |
| 14 | const SkBitmap& bm, bool shouldBeDrawn) { |
| 15 | for (int y = 0; y < bm.height(); ++y) { |
| 16 | for (int x = 0; x < bm.width(); ++x) { |
| 17 | if (shouldBeDrawn) { |
| 18 | if (0 == *bm.getAddr32(x, y)) { |
| 19 | REPORTER_ASSERT(reporter, false); |
| 20 | return; |
| 21 | } |
| 22 | } else { |
| 23 | // should not be drawn |
| 24 | if (*bm.getAddr32(x, y)) { |
| 25 | REPORTER_ASSERT(reporter, false); |
| 26 | return; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | static void test_wacky_bitmapshader(skiatest::Reporter* reporter, |
| 34 | int width, int height, bool shouldBeDrawn) { |
| 35 | SkBitmap dev; |
| 36 | dev.setConfig(SkBitmap::kARGB_8888_Config, 0x56F, 0x4f6); |
| 37 | dev.allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 38 | 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] | 39 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 40 | SkMatrix matrix; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 41 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 42 | SkCanvas c(dev); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 43 | matrix.setAll(SkFloatToScalar(-119.34097f), |
| 44 | SkFloatToScalar(-43.436558f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 45 | SkFloatToScalar(93489.945f), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 46 | SkFloatToScalar(43.436558f), |
| 47 | SkFloatToScalar(-119.34097f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 48 | SkFloatToScalar(123.98426f), |
| 49 | 0, 0, SK_Scalar1); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 50 | c.concat(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 51 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 52 | SkBitmap bm; |
| 53 | bm.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 54 | bm.allocPixels(); |
| 55 | bm.eraseColor(SK_ColorRED); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 56 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 57 | SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, |
| 58 | SkShader::kRepeat_TileMode); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 59 | matrix.setAll(SkFloatToScalar(0.0078740157f), |
| 60 | 0, |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 61 | SkIntToScalar(249), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 62 | 0, |
| 63 | SkFloatToScalar(0.0078740157f), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 64 | SkIntToScalar(239), |
| 65 | 0, 0, SK_Scalar1); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 66 | s->setLocalMatrix(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 67 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 68 | SkPaint paint; |
| 69 | paint.setShader(s)->unref(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 70 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 71 | SkRect r = SkRect::MakeXYWH(681, 239, 695, 253); |
| 72 | c.drawRect(r, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 73 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 74 | assert_ifDrawnTo(reporter, dev, shouldBeDrawn); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Original bug was asserting that the matrix-proc had generated a (Y) value |
| 79 | * that was out of range. This led (in the release build) to the sampler-proc |
| 80 | * reading memory out-of-bounds of the original bitmap. |
| 81 | * |
| 82 | * We were numerically overflowing our 16bit coordinates that we communicate |
| 83 | * between these two procs. The fixes was in two parts: |
| 84 | * |
| 85 | * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we |
| 86 | * can't represent those coordinates in our transport format (yet). |
| 87 | * 2. Perform an unsigned shift during the calculation, so we don't get |
| 88 | * sign-extension bleed when packing the two values (X,Y) into our 32bit |
| 89 | * slot. |
| 90 | * |
| 91 | * This tests exercises the original setup, plus 3 more to ensure that we can, |
| 92 | * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total |
| 93 | * memory allocation limit). |
| 94 | */ |
| 95 | static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) { |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 96 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 97 | static const struct { |
| 98 | int fWidth; |
| 99 | int fHeight; |
| 100 | bool fExpectedToDraw; |
| 101 | } gTests[] = { |
| 102 | { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K) |
| 103 | { 0xFFFF, 0x7f, true }, // should draw, test max width |
| 104 | { 0x7f, 0xFFFF, true }, // should draw, test max height |
| 105 | { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM) |
| 106 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 107 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 108 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) { |
| 109 | test_wacky_bitmapshader(reporter, |
| 110 | gTests[i].fWidth, gTests[i].fHeight, |
| 111 | gTests[i].fExpectedToDraw); |
| 112 | } |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 113 | #endif |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 114 | } |
reed@google.com | 0da0627 | 2012-05-03 20:26:06 +0000 | [diff] [blame] | 115 | |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 116 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 117 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 118 | static void test_nan_antihair(skiatest::Reporter* reporter) { |
| 119 | SkBitmap bm; |
| 120 | bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20); |
| 121 | bm.allocPixels(); |
| 122 | |
| 123 | SkCanvas canvas(bm); |
| 124 | |
| 125 | SkPath path; |
| 126 | path.moveTo(0, 0); |
| 127 | path.lineTo(10, SK_ScalarNaN); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 128 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 129 | SkPaint paint; |
| 130 | paint.setAntiAlias(true); |
| 131 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 132 | |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 133 | // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...) |
| 134 | // this would trigger an assert/crash. |
| 135 | // |
| 136 | // see rev. 3558 |
| 137 | canvas.drawPath(path, paint); |
| 138 | } |
| 139 | |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 140 | static bool check_for_all_zeros(const SkBitmap& bm) { |
| 141 | SkAutoLockPixels alp(bm); |
| 142 | |
| 143 | size_t count = bm.width() * bm.bytesPerPixel(); |
| 144 | for (int y = 0; y < bm.height(); y++) { |
| 145 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y)); |
| 146 | for (size_t i = 0; i < count; i++) { |
| 147 | if (ptr[i]) { |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | static const int gWidth = 256; |
| 156 | static const int gHeight = 256; |
| 157 | |
| 158 | static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { |
| 159 | bm->setConfig(config, gWidth, gHeight); |
| 160 | bm->allocPixels(); |
| 161 | bm->eraseColor(color); |
| 162 | } |
| 163 | |
| 164 | static void TestDrawBitmapRect(skiatest::Reporter* reporter) { |
| 165 | SkBitmap src, dst; |
| 166 | |
| 167 | create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 168 | create(&dst, SkBitmap::kARGB_8888_Config, 0); |
| 169 | |
| 170 | SkCanvas canvas(dst); |
| 171 | |
| 172 | SkIRect srcR = { gWidth, 0, gWidth + 16, 16 }; |
| 173 | SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) }; |
| 174 | |
| 175 | canvas.drawBitmapRect(src, &srcR, dstR, NULL); |
| 176 | |
| 177 | // ensure that we draw nothing if srcR does not intersect the bitmap |
| 178 | REPORTER_ASSERT(reporter, check_for_all_zeros(dst)); |
reed@google.com | 6de0bfc | 2012-03-30 17:43:33 +0000 | [diff] [blame] | 179 | |
| 180 | test_nan_antihair(reporter); |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 181 | test_giantrepeat_crbug118018(reporter); |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | #include "TestClassDef.h" |
| 185 | DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect) |