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