blob: 9289e11ea8f6a6a9737275fab19dc4304d0e4b9f [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.comcef5bb42013-10-15 20:05:24 +000011#include "SkData.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000012#include "SkDiscardableMemoryPool.h"
13#include "SkDiscardablePixelRef.h"
reed@google.comcef5bb42013-10-15 20:05:24 +000014#include "SkPaint.h"
reed@google.com99c114e2012-05-03 20:14:26 +000015#include "SkShader.h"
reed@google.comcef5bb42013-10-15 20:05:24 +000016#include "SkSurface.h"
reed@google.comad514302013-01-02 20:19:45 +000017#include "SkRandom.h"
18#include "SkMatrixUtils.h"
19
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000020namespace {
reed@google.comcef5bb42013-10-15 20:05:24 +000021// A BitmapFactory that always fails when asked to return pixels.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000022class FailureImageGenerator : public SkImageGenerator {
23public:
24 FailureImageGenerator() { }
25 virtual ~FailureImageGenerator() { }
26 virtual bool getInfo(SkImageInfo* info) {
27 info->fWidth = 100;
28 info->fHeight = 100;
29 info->fColorType = kPMColor_SkColorType;
reed@google.comcef5bb42013-10-15 20:05:24 +000030 info->fAlphaType = kPremul_SkAlphaType;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000031 return true;
reed@google.comcef5bb42013-10-15 20:05:24 +000032 }
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000033 virtual bool getPixels(const SkImageInfo& info,
34 void* pixels,
35 size_t rowBytes) SK_OVERRIDE {
36 // this will deliberately return false if they are asking us
37 // to decode into pixels.
38 return false;
39 }
40};
41} // namespace
reed@google.comcef5bb42013-10-15 20:05:24 +000042
43// crbug.com/295895
44// Crashing in skia when a pixelref fails in lockPixels
45//
46static void test_faulty_pixelref(skiatest::Reporter* reporter) {
47 // need a cache, but don't expect to use it, so the budget is not critical
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000048 SkAutoTUnref<SkDiscardableMemoryPool> pool(SkNEW_ARGS(SkDiscardableMemoryPool,
49 (10 * 1000, NULL)));
reed@google.comcef5bb42013-10-15 20:05:24 +000050 SkBitmap bm;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000051 bool installSuccess = SkDiscardablePixelRef::Install(SkNEW(FailureImageGenerator), &bm, pool);
52 REPORTER_ASSERT(reporter, installSuccess);
reed@google.comcef5bb42013-10-15 20:05:24 +000053 // now our bitmap has a pixelref, but we know it will fail to lock
54
55 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(200, 200));
56 SkCanvas* canvas = surface->getCanvas();
57
58 const SkPaint::FilterLevel levels[] = {
59 SkPaint::kNone_FilterLevel,
60 SkPaint::kLow_FilterLevel,
61 SkPaint::kMedium_FilterLevel,
62 SkPaint::kHigh_FilterLevel,
63 };
64
65 SkPaint paint;
66 canvas->scale(2, 2); // need a scale, otherwise we may ignore filtering
67 for (size_t i = 0; i < SK_ARRAY_COUNT(levels); ++i) {
68 paint.setFilterLevel(levels[i]);
69 canvas->drawBitmap(bm, 0, 0, &paint);
70 }
71}
72
73///////////////////////////////////////////////////////////////////////////////
74
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000075static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) {
reed@google.comad514302013-01-02 20:19:45 +000076 mat->setIdentity();
77 if (mask & SkMatrix::kTranslate_Mask) {
78 mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1());
79 }
80 if (mask & SkMatrix::kScale_Mask) {
81 mat->postScale(rand.nextSScalar1(), rand.nextSScalar1());
82 }
83 if (mask & SkMatrix::kAffine_Mask) {
84 mat->postRotate(rand.nextSScalar1() * 360);
85 }
86 if (mask & SkMatrix::kPerspective_Mask) {
87 mat->setPerspX(rand.nextSScalar1());
88 mat->setPerspY(rand.nextSScalar1());
89 }
90}
91
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000092static void rand_size(SkISize* size, SkRandom& rand) {
reed@google.comae573582013-01-03 15:22:40 +000093 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF);
94}
95
96static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size,
97 unsigned bits) {
98 return SkTreatAsSprite(mat, size.width(), size.height(), bits);
reed@google.comad514302013-01-02 20:19:45 +000099}
100
101static void test_treatAsSprite(skiatest::Reporter* reporter) {
102 const unsigned bilerBits = kSkSubPixelBitsForBilerp;
103
104 SkMatrix mat;
reed@google.comae573582013-01-03 15:22:40 +0000105 SkISize size;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000106 SkRandom rand;
reed@google.comad514302013-01-02 20:19:45 +0000107
108 // assert: translate-only no-filter can always be treated as sprite
109 for (int i = 0; i < 1000; ++i) {
110 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask);
111 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +0000112 rand_size(&size, rand);
113 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0));
reed@google.comad514302013-01-02 20:19:45 +0000114 }
115 }
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000116
reed@google.comad514302013-01-02 20:19:45 +0000117 // assert: rotate/perspect is never treated as sprite
118 for (int i = 0; i < 1000; ++i) {
119 rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask);
120 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +0000121 rand_size(&size, rand);
122 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, 0));
123 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000124 }
125 }
126
reed@google.comae573582013-01-03 15:22:40 +0000127 size.set(500, 600);
reed@google.comad514302013-01-02 20:19:45 +0000128
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000129 const SkScalar tooMuchSubpixel = 100.1f;
reed@google.comad514302013-01-02 20:19:45 +0000130 mat.setTranslate(tooMuchSubpixel, 0);
reed@google.comae573582013-01-03 15:22:40 +0000131 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000132 mat.setTranslate(0, tooMuchSubpixel);
reed@google.comae573582013-01-03 15:22:40 +0000133 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000134
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000135 const SkScalar tinySubPixel = 100.02f;
reed@google.comad514302013-01-02 20:19:45 +0000136 mat.setTranslate(tinySubPixel, 0);
reed@google.comae573582013-01-03 15:22:40 +0000137 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000138 mat.setTranslate(0, tinySubPixel);
reed@google.comae573582013-01-03 15:22:40 +0000139 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000140
reed@google.comad514302013-01-02 20:19:45 +0000141 const SkScalar twoThirds = SK_Scalar1 * 2 / 3;
reed@google.comae573582013-01-03 15:22:40 +0000142 const SkScalar bigScale = SkScalarDiv(size.width() + twoThirds, size.width());
reed@google.comad514302013-01-02 20:19:45 +0000143 mat.setScale(bigScale, bigScale);
reed@google.comae573582013-01-03 15:22:40 +0000144 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, false));
145 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000146
reed@google.comad514302013-01-02 20:19:45 +0000147 const SkScalar oneThird = SK_Scalar1 / 3;
reed@google.comae573582013-01-03 15:22:40 +0000148 const SkScalar smallScale = SkScalarDiv(size.width() + oneThird, size.width());
reed@google.comad514302013-01-02 20:19:45 +0000149 mat.setScale(smallScale, smallScale);
reed@google.comae573582013-01-03 15:22:40 +0000150 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
151 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000152
reed@google.comad514302013-01-02 20:19:45 +0000153 const SkScalar oneFortyth = SK_Scalar1 / 40;
reed@google.comae573582013-01-03 15:22:40 +0000154 const SkScalar tinyScale = SkScalarDiv(size.width() + oneFortyth, size.width());
reed@google.comad514302013-01-02 20:19:45 +0000155 mat.setScale(tinyScale, tinyScale);
reed@google.comae573582013-01-03 15:22:40 +0000156 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
157 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000158}
reed@google.com99c114e2012-05-03 20:14:26 +0000159
160static void assert_ifDrawnTo(skiatest::Reporter* reporter,
161 const SkBitmap& bm, bool shouldBeDrawn) {
162 for (int y = 0; y < bm.height(); ++y) {
163 for (int x = 0; x < bm.width(); ++x) {
164 if (shouldBeDrawn) {
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000165 if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000166 REPORTER_ASSERT(reporter, false);
167 return;
168 }
169 } else {
170 // should not be drawn
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000171 if (SK_ColorTRANSPARENT != *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000172 REPORTER_ASSERT(reporter, false);
173 return;
174 }
175 }
176 }
177 }
178}
179
180static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
181 int width, int height, bool shouldBeDrawn) {
182 SkBitmap dev;
183 dev.setConfig(SkBitmap::kARGB_8888_Config, 0x56F, 0x4f6);
184 dev.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000185 dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
rmistry@google.comd6176b02012-08-23 18:14:13 +0000186
reed@google.com99c114e2012-05-03 20:14:26 +0000187 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188
reed@google.com99c114e2012-05-03 20:14:26 +0000189 SkCanvas c(dev);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000190 matrix.setAll(-119.34097f,
191 -43.436558f,
192 93489.945f,
193 43.436558f,
194 -119.34097f,
195 123.98426f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000196 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000197 c.concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198
reed@google.com99c114e2012-05-03 20:14:26 +0000199 SkBitmap bm;
200 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height);
201 bm.allocPixels();
202 bm.eraseColor(SK_ColorRED);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000203
reed@google.com99c114e2012-05-03 20:14:26 +0000204 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
205 SkShader::kRepeat_TileMode);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000206 matrix.setAll(0.0078740157f,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000207 0,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000208 SkIntToScalar(249),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000209 0,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000210 0.0078740157f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000211 SkIntToScalar(239),
212 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000213 s->setLocalMatrix(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000214
reed@google.com99c114e2012-05-03 20:14:26 +0000215 SkPaint paint;
216 paint.setShader(s)->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000217
reed@google.com99c114e2012-05-03 20:14:26 +0000218 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
219 c.drawRect(r, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000220
reed@google.com99c114e2012-05-03 20:14:26 +0000221 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
222}
223
224/*
225 * Original bug was asserting that the matrix-proc had generated a (Y) value
226 * that was out of range. This led (in the release build) to the sampler-proc
227 * reading memory out-of-bounds of the original bitmap.
228 *
229 * We were numerically overflowing our 16bit coordinates that we communicate
230 * between these two procs. The fixes was in two parts:
231 *
232 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
233 * can't represent those coordinates in our transport format (yet).
234 * 2. Perform an unsigned shift during the calculation, so we don't get
235 * sign-extension bleed when packing the two values (X,Y) into our 32bit
236 * slot.
237 *
238 * This tests exercises the original setup, plus 3 more to ensure that we can,
239 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
240 * memory allocation limit).
241 */
242static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
reed@google.com0da06272012-05-03 20:26:06 +0000243#ifdef SK_SCALAR_IS_FLOAT
reed@google.com99c114e2012-05-03 20:14:26 +0000244 static const struct {
245 int fWidth;
246 int fHeight;
247 bool fExpectedToDraw;
248 } gTests[] = {
249 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
250 { 0xFFFF, 0x7f, true }, // should draw, test max width
251 { 0x7f, 0xFFFF, true }, // should draw, test max height
252 { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
253 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000254
reed@google.com99c114e2012-05-03 20:14:26 +0000255 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
256 test_wacky_bitmapshader(reporter,
257 gTests[i].fWidth, gTests[i].fHeight,
258 gTests[i].fExpectedToDraw);
259 }
reed@google.com0da06272012-05-03 20:26:06 +0000260#endif
reed@google.com99c114e2012-05-03 20:14:26 +0000261}
reed@google.com0da06272012-05-03 20:26:06 +0000262
reed@google.com99c114e2012-05-03 20:14:26 +0000263///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000264
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000265static void test_nan_antihair() {
reed@google.com6de0bfc2012-03-30 17:43:33 +0000266 SkBitmap bm;
267 bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
268 bm.allocPixels();
269
270 SkCanvas canvas(bm);
271
272 SkPath path;
273 path.moveTo(0, 0);
274 path.lineTo(10, SK_ScalarNaN);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000275
reed@google.com6de0bfc2012-03-30 17:43:33 +0000276 SkPaint paint;
277 paint.setAntiAlias(true);
278 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000279
reed@google.com6de0bfc2012-03-30 17:43:33 +0000280 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
281 // this would trigger an assert/crash.
282 //
283 // see rev. 3558
284 canvas.drawPath(path, paint);
285}
286
reed@google.com2ade0862011-03-17 17:48:04 +0000287static bool check_for_all_zeros(const SkBitmap& bm) {
288 SkAutoLockPixels alp(bm);
289
290 size_t count = bm.width() * bm.bytesPerPixel();
291 for (int y = 0; y < bm.height(); y++) {
292 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
293 for (size_t i = 0; i < count; i++) {
294 if (ptr[i]) {
295 return false;
296 }
297 }
298 }
299 return true;
300}
301
302static const int gWidth = 256;
303static const int gHeight = 256;
304
305static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
306 bm->setConfig(config, gWidth, gHeight);
307 bm->allocPixels();
308 bm->eraseColor(color);
309}
310
311static void TestDrawBitmapRect(skiatest::Reporter* reporter) {
312 SkBitmap src, dst;
313
314 create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
315 create(&dst, SkBitmap::kARGB_8888_Config, 0);
316
317 SkCanvas canvas(dst);
318
319 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
320 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
321
322 canvas.drawBitmapRect(src, &srcR, dstR, NULL);
323
324 // ensure that we draw nothing if srcR does not intersect the bitmap
325 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000326
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000327 test_nan_antihair();
reed@google.com99c114e2012-05-03 20:14:26 +0000328 test_giantrepeat_crbug118018(reporter);
reed@google.comad514302013-01-02 20:19:45 +0000329
330 test_treatAsSprite(reporter);
reed@google.com23d0ab72013-10-15 20:14:00 +0000331 test_faulty_pixelref(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000332}
333
334#include "TestClassDef.h"
335DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect)