blob: 0538a12aad02bd8972454da690321debfddce48d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com2ade0862011-03-17 17:48:04 +00008#include "Test.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
reed@google.com99c114e2012-05-03 20:14:26 +000011#include "SkShader.h"
reed@google.comad514302013-01-02 20:19:45 +000012#include "SkRandom.h"
13#include "SkMatrixUtils.h"
14
jvanverth@google.comc490f802013-03-04 13:56:38 +000015static void rand_matrix(SkMatrix* mat, SkMWCRandom& rand, unsigned mask) {
reed@google.comad514302013-01-02 20:19:45 +000016 mat->setIdentity();
17 if (mask & SkMatrix::kTranslate_Mask) {
18 mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1());
19 }
20 if (mask & SkMatrix::kScale_Mask) {
21 mat->postScale(rand.nextSScalar1(), rand.nextSScalar1());
22 }
23 if (mask & SkMatrix::kAffine_Mask) {
24 mat->postRotate(rand.nextSScalar1() * 360);
25 }
26 if (mask & SkMatrix::kPerspective_Mask) {
27 mat->setPerspX(rand.nextSScalar1());
28 mat->setPerspY(rand.nextSScalar1());
29 }
30}
31
jvanverth@google.comc490f802013-03-04 13:56:38 +000032static void rand_size(SkISize* size, SkMWCRandom& rand) {
reed@google.comae573582013-01-03 15:22:40 +000033 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF);
34}
35
36static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size,
37 unsigned bits) {
38 return SkTreatAsSprite(mat, size.width(), size.height(), bits);
reed@google.comad514302013-01-02 20:19:45 +000039}
40
41static void test_treatAsSprite(skiatest::Reporter* reporter) {
42 const unsigned bilerBits = kSkSubPixelBitsForBilerp;
43
44 SkMatrix mat;
reed@google.comae573582013-01-03 15:22:40 +000045 SkISize size;
jvanverth@google.comc490f802013-03-04 13:56:38 +000046 SkMWCRandom rand;
reed@google.comad514302013-01-02 20:19:45 +000047
48 // assert: translate-only no-filter can always be treated as sprite
49 for (int i = 0; i < 1000; ++i) {
50 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask);
51 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +000052 rand_size(&size, rand);
53 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0));
reed@google.comad514302013-01-02 20:19:45 +000054 }
55 }
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000056
reed@google.comad514302013-01-02 20:19:45 +000057 // assert: rotate/perspect is never treated as sprite
58 for (int i = 0; i < 1000; ++i) {
59 rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask);
60 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +000061 rand_size(&size, rand);
62 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, 0));
63 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +000064 }
65 }
66
reed@google.comae573582013-01-03 15:22:40 +000067 size.set(500, 600);
reed@google.comad514302013-01-02 20:19:45 +000068
69 const SkScalar tooMuchSubpixel = SkFloatToScalar(100.1f);
70 mat.setTranslate(tooMuchSubpixel, 0);
reed@google.comae573582013-01-03 15:22:40 +000071 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +000072 mat.setTranslate(0, tooMuchSubpixel);
reed@google.comae573582013-01-03 15:22:40 +000073 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +000074
75 const SkScalar tinySubPixel = SkFloatToScalar(100.02f);
76 mat.setTranslate(tinySubPixel, 0);
reed@google.comae573582013-01-03 15:22:40 +000077 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +000078 mat.setTranslate(0, tinySubPixel);
reed@google.comae573582013-01-03 15:22:40 +000079 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000080
reed@google.comad514302013-01-02 20:19:45 +000081 const SkScalar twoThirds = SK_Scalar1 * 2 / 3;
reed@google.comae573582013-01-03 15:22:40 +000082 const SkScalar bigScale = SkScalarDiv(size.width() + twoThirds, size.width());
reed@google.comad514302013-01-02 20:19:45 +000083 mat.setScale(bigScale, bigScale);
reed@google.comae573582013-01-03 15:22:40 +000084 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, false));
85 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000086
reed@google.comad514302013-01-02 20:19:45 +000087 const SkScalar oneThird = SK_Scalar1 / 3;
reed@google.comae573582013-01-03 15:22:40 +000088 const SkScalar smallScale = SkScalarDiv(size.width() + oneThird, size.width());
reed@google.comad514302013-01-02 20:19:45 +000089 mat.setScale(smallScale, smallScale);
reed@google.comae573582013-01-03 15:22:40 +000090 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
91 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000092
reed@google.comad514302013-01-02 20:19:45 +000093 const SkScalar oneFortyth = SK_Scalar1 / 40;
reed@google.comae573582013-01-03 15:22:40 +000094 const SkScalar tinyScale = SkScalarDiv(size.width() + oneFortyth, size.width());
reed@google.comad514302013-01-02 20:19:45 +000095 mat.setScale(tinyScale, tinyScale);
reed@google.comae573582013-01-03 15:22:40 +000096 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
97 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +000098}
reed@google.com99c114e2012-05-03 20:14:26 +000099
100static void assert_ifDrawnTo(skiatest::Reporter* reporter,
101 const SkBitmap& bm, bool shouldBeDrawn) {
102 for (int y = 0; y < bm.height(); ++y) {
103 for (int x = 0; x < bm.width(); ++x) {
104 if (shouldBeDrawn) {
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000105 if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000106 REPORTER_ASSERT(reporter, false);
107 return;
108 }
109 } else {
110 // should not be drawn
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000111 if (SK_ColorTRANSPARENT != *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000112 REPORTER_ASSERT(reporter, false);
113 return;
114 }
115 }
116 }
117 }
118}
119
120static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
121 int width, int height, bool shouldBeDrawn) {
122 SkBitmap dev;
123 dev.setConfig(SkBitmap::kARGB_8888_Config, 0x56F, 0x4f6);
124 dev.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000125 dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126
reed@google.com99c114e2012-05-03 20:14:26 +0000127 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128
reed@google.com99c114e2012-05-03 20:14:26 +0000129 SkCanvas c(dev);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130 matrix.setAll(SkFloatToScalar(-119.34097f),
131 SkFloatToScalar(-43.436558f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000132 SkFloatToScalar(93489.945f),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133 SkFloatToScalar(43.436558f),
134 SkFloatToScalar(-119.34097f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000135 SkFloatToScalar(123.98426f),
136 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000137 c.concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138
reed@google.com99c114e2012-05-03 20:14:26 +0000139 SkBitmap bm;
140 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height);
141 bm.allocPixels();
142 bm.eraseColor(SK_ColorRED);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000143
reed@google.com99c114e2012-05-03 20:14:26 +0000144 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
145 SkShader::kRepeat_TileMode);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000146 matrix.setAll(SkFloatToScalar(0.0078740157f),
147 0,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000148 SkIntToScalar(249),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000149 0,
150 SkFloatToScalar(0.0078740157f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000151 SkIntToScalar(239),
152 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000153 s->setLocalMatrix(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000154
reed@google.com99c114e2012-05-03 20:14:26 +0000155 SkPaint paint;
156 paint.setShader(s)->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000157
reed@google.com99c114e2012-05-03 20:14:26 +0000158 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
159 c.drawRect(r, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000160
reed@google.com99c114e2012-05-03 20:14:26 +0000161 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
162}
163
164/*
165 * Original bug was asserting that the matrix-proc had generated a (Y) value
166 * that was out of range. This led (in the release build) to the sampler-proc
167 * reading memory out-of-bounds of the original bitmap.
168 *
169 * We were numerically overflowing our 16bit coordinates that we communicate
170 * between these two procs. The fixes was in two parts:
171 *
172 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
173 * can't represent those coordinates in our transport format (yet).
174 * 2. Perform an unsigned shift during the calculation, so we don't get
175 * sign-extension bleed when packing the two values (X,Y) into our 32bit
176 * slot.
177 *
178 * This tests exercises the original setup, plus 3 more to ensure that we can,
179 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
180 * memory allocation limit).
181 */
182static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
reed@google.com0da06272012-05-03 20:26:06 +0000183#ifdef SK_SCALAR_IS_FLOAT
reed@google.com99c114e2012-05-03 20:14:26 +0000184 static const struct {
185 int fWidth;
186 int fHeight;
187 bool fExpectedToDraw;
188 } gTests[] = {
189 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
190 { 0xFFFF, 0x7f, true }, // should draw, test max width
191 { 0x7f, 0xFFFF, true }, // should draw, test max height
192 { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
193 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
reed@google.com99c114e2012-05-03 20:14:26 +0000195 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
196 test_wacky_bitmapshader(reporter,
197 gTests[i].fWidth, gTests[i].fHeight,
198 gTests[i].fExpectedToDraw);
199 }
reed@google.com0da06272012-05-03 20:26:06 +0000200#endif
reed@google.com99c114e2012-05-03 20:14:26 +0000201}
reed@google.com0da06272012-05-03 20:26:06 +0000202
reed@google.com99c114e2012-05-03 20:14:26 +0000203///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000204
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000205static void test_nan_antihair() {
reed@google.com6de0bfc2012-03-30 17:43:33 +0000206 SkBitmap bm;
207 bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
208 bm.allocPixels();
209
210 SkCanvas canvas(bm);
211
212 SkPath path;
213 path.moveTo(0, 0);
214 path.lineTo(10, SK_ScalarNaN);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000215
reed@google.com6de0bfc2012-03-30 17:43:33 +0000216 SkPaint paint;
217 paint.setAntiAlias(true);
218 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000219
reed@google.com6de0bfc2012-03-30 17:43:33 +0000220 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
221 // this would trigger an assert/crash.
222 //
223 // see rev. 3558
224 canvas.drawPath(path, paint);
225}
226
reed@google.com2ade0862011-03-17 17:48:04 +0000227static bool check_for_all_zeros(const SkBitmap& bm) {
228 SkAutoLockPixels alp(bm);
229
230 size_t count = bm.width() * bm.bytesPerPixel();
231 for (int y = 0; y < bm.height(); y++) {
232 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
233 for (size_t i = 0; i < count; i++) {
234 if (ptr[i]) {
235 return false;
236 }
237 }
238 }
239 return true;
240}
241
242static const int gWidth = 256;
243static const int gHeight = 256;
244
245static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
246 bm->setConfig(config, gWidth, gHeight);
247 bm->allocPixels();
248 bm->eraseColor(color);
249}
250
251static void TestDrawBitmapRect(skiatest::Reporter* reporter) {
252 SkBitmap src, dst;
253
254 create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
255 create(&dst, SkBitmap::kARGB_8888_Config, 0);
256
257 SkCanvas canvas(dst);
258
259 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
260 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
261
262 canvas.drawBitmapRect(src, &srcR, dstR, NULL);
263
264 // ensure that we draw nothing if srcR does not intersect the bitmap
265 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000266
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000267 test_nan_antihair();
reed@google.com99c114e2012-05-03 20:14:26 +0000268 test_giantrepeat_crbug118018(reporter);
reed@google.comad514302013-01-02 20:19:45 +0000269
270 test_treatAsSprite(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000271}
272
273#include "TestClassDef.h"
274DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect)