blob: 1ecc590e5367e6a2028858cd2e0e1de3b501ccf0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@google.com2ade0862011-03-17 17:48:04 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
reed@google.comcef5bb42013-10-15 20:05:24 +000010#include "SkData.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000011#include "SkDiscardableMemoryPool.h"
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000012#include "SkImageGeneratorPriv.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "SkMatrixUtils.h"
reed@google.comcef5bb42013-10-15 20:05:24 +000014#include "SkPaint.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "SkRandom.h"
reed@google.com99c114e2012-05-03 20:14:26 +000016#include "SkShader.h"
reed@google.comcef5bb42013-10-15 20:05:24 +000017#include "SkSurface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000018#include "Test.h"
reed@google.comad514302013-01-02 20:19:45 +000019
reed@google.comcef5bb42013-10-15 20:05:24 +000020// A BitmapFactory that always fails when asked to return pixels.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000021class FailureImageGenerator : public SkImageGenerator {
22public:
23 FailureImageGenerator() { }
24 virtual ~FailureImageGenerator() { }
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +000025
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000026protected:
mtklein72c9faa2015-01-09 10:06:39 -080027 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
reede5ea5002014-09-03 11:54:58 -070028 *info = SkImageInfo::MakeN32Premul(100, 100);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000029 return true;
reed@google.comcef5bb42013-10-15 20:05:24 +000030 }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000031 // default onGetPixels() returns false, which is what we want.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000032};
reed@google.comcef5bb42013-10-15 20:05:24 +000033
34// crbug.com/295895
35// Crashing in skia when a pixelref fails in lockPixels
36//
37static void test_faulty_pixelref(skiatest::Reporter* reporter) {
38 // need a cache, but don't expect to use it, so the budget is not critical
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000039 SkAutoTUnref<SkDiscardableMemoryPool> pool(
40 SkDiscardableMemoryPool::Create(10 * 1000, NULL));
reed@google.comcef5bb42013-10-15 20:05:24 +000041 SkBitmap bm;
halcanary@google.comedd370f2013-12-10 21:11:12 +000042 bool installSuccess = SkInstallDiscardablePixelRef(SkNEW(FailureImageGenerator), &bm, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000043 REPORTER_ASSERT(reporter, installSuccess);
reed@google.comcef5bb42013-10-15 20:05:24 +000044 // now our bitmap has a pixelref, but we know it will fail to lock
45
reed3054be12014-12-10 07:24:28 -080046 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(200, 200));
reed@google.comcef5bb42013-10-15 20:05:24 +000047 SkCanvas* canvas = surface->getCanvas();
48
49 const SkPaint::FilterLevel levels[] = {
50 SkPaint::kNone_FilterLevel,
51 SkPaint::kLow_FilterLevel,
52 SkPaint::kMedium_FilterLevel,
53 SkPaint::kHigh_FilterLevel,
54 };
55
56 SkPaint paint;
57 canvas->scale(2, 2); // need a scale, otherwise we may ignore filtering
58 for (size_t i = 0; i < SK_ARRAY_COUNT(levels); ++i) {
59 paint.setFilterLevel(levels[i]);
60 canvas->drawBitmap(bm, 0, 0, &paint);
61 }
62}
63
64///////////////////////////////////////////////////////////////////////////////
65
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000066static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) {
reed@google.comad514302013-01-02 20:19:45 +000067 mat->setIdentity();
68 if (mask & SkMatrix::kTranslate_Mask) {
69 mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1());
70 }
71 if (mask & SkMatrix::kScale_Mask) {
72 mat->postScale(rand.nextSScalar1(), rand.nextSScalar1());
73 }
74 if (mask & SkMatrix::kAffine_Mask) {
75 mat->postRotate(rand.nextSScalar1() * 360);
76 }
77 if (mask & SkMatrix::kPerspective_Mask) {
78 mat->setPerspX(rand.nextSScalar1());
79 mat->setPerspY(rand.nextSScalar1());
80 }
81}
82
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000083static void rand_size(SkISize* size, SkRandom& rand) {
reed@google.comae573582013-01-03 15:22:40 +000084 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF);
85}
86
87static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size,
88 unsigned bits) {
89 return SkTreatAsSprite(mat, size.width(), size.height(), bits);
reed@google.comad514302013-01-02 20:19:45 +000090}
91
92static void test_treatAsSprite(skiatest::Reporter* reporter) {
93 const unsigned bilerBits = kSkSubPixelBitsForBilerp;
94
95 SkMatrix mat;
reed@google.comae573582013-01-03 15:22:40 +000096 SkISize size;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000097 SkRandom rand;
reed@google.comad514302013-01-02 20:19:45 +000098
99 // assert: translate-only no-filter can always be treated as sprite
100 for (int i = 0; i < 1000; ++i) {
101 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask);
102 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +0000103 rand_size(&size, rand);
104 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0));
reed@google.comad514302013-01-02 20:19:45 +0000105 }
106 }
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000107
reed@google.comad514302013-01-02 20:19:45 +0000108 // assert: rotate/perspect is never treated as sprite
109 for (int i = 0; i < 1000; ++i) {
110 rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_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));
114 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000115 }
116 }
117
reed@google.comae573582013-01-03 15:22:40 +0000118 size.set(500, 600);
reed@google.comad514302013-01-02 20:19:45 +0000119
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000120 const SkScalar tooMuchSubpixel = 100.1f;
reed@google.comad514302013-01-02 20:19:45 +0000121 mat.setTranslate(tooMuchSubpixel, 0);
reed@google.comae573582013-01-03 15:22:40 +0000122 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000123 mat.setTranslate(0, tooMuchSubpixel);
reed@google.comae573582013-01-03 15:22:40 +0000124 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000125
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000126 const SkScalar tinySubPixel = 100.02f;
reed@google.comad514302013-01-02 20:19:45 +0000127 mat.setTranslate(tinySubPixel, 0);
reed@google.comae573582013-01-03 15:22:40 +0000128 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000129 mat.setTranslate(0, tinySubPixel);
reed@google.comae573582013-01-03 15:22:40 +0000130 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000131
reed@google.comad514302013-01-02 20:19:45 +0000132 const SkScalar twoThirds = SK_Scalar1 * 2 / 3;
reed@google.comae573582013-01-03 15:22:40 +0000133 const SkScalar bigScale = SkScalarDiv(size.width() + twoThirds, size.width());
reed@google.comad514302013-01-02 20:19:45 +0000134 mat.setScale(bigScale, bigScale);
reed@google.comae573582013-01-03 15:22:40 +0000135 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, false));
136 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000137
reed@google.comad514302013-01-02 20:19:45 +0000138 const SkScalar oneThird = SK_Scalar1 / 3;
reed@google.comae573582013-01-03 15:22:40 +0000139 const SkScalar smallScale = SkScalarDiv(size.width() + oneThird, size.width());
reed@google.comad514302013-01-02 20:19:45 +0000140 mat.setScale(smallScale, smallScale);
reed@google.comae573582013-01-03 15:22:40 +0000141 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
142 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000143
reed@google.comad514302013-01-02 20:19:45 +0000144 const SkScalar oneFortyth = SK_Scalar1 / 40;
reed@google.comae573582013-01-03 15:22:40 +0000145 const SkScalar tinyScale = SkScalarDiv(size.width() + oneFortyth, size.width());
reed@google.comad514302013-01-02 20:19:45 +0000146 mat.setScale(tinyScale, tinyScale);
reed@google.comae573582013-01-03 15:22:40 +0000147 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
148 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
reed@google.comad514302013-01-02 20:19:45 +0000149}
reed@google.com99c114e2012-05-03 20:14:26 +0000150
151static void assert_ifDrawnTo(skiatest::Reporter* reporter,
152 const SkBitmap& bm, bool shouldBeDrawn) {
153 for (int y = 0; y < bm.height(); ++y) {
154 for (int x = 0; x < bm.width(); ++x) {
155 if (shouldBeDrawn) {
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000156 if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000157 REPORTER_ASSERT(reporter, false);
158 return;
159 }
160 } else {
161 // should not be drawn
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000162 if (SK_ColorTRANSPARENT != *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000163 REPORTER_ASSERT(reporter, false);
164 return;
165 }
166 }
167 }
168 }
169}
170
171static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
172 int width, int height, bool shouldBeDrawn) {
173 SkBitmap dev;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000174 dev.allocN32Pixels(0x56F, 0x4f6);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000175 dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
rmistry@google.comd6176b02012-08-23 18:14:13 +0000176
reed@google.com99c114e2012-05-03 20:14:26 +0000177 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178
reed@google.com99c114e2012-05-03 20:14:26 +0000179 SkCanvas c(dev);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000180 matrix.setAll(-119.34097f,
181 -43.436558f,
182 93489.945f,
183 43.436558f,
184 -119.34097f,
185 123.98426f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000186 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000187 c.concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188
reed@google.com99c114e2012-05-03 20:14:26 +0000189 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700190 if (bm.tryAllocN32Pixels(width, height)) {
191 // allow this to fail silently, to test the code downstream
192 }
reed@google.com99c114e2012-05-03 20:14:26 +0000193 bm.eraseColor(SK_ColorRED);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000195 matrix.setAll(0.0078740157f,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000196 0,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000197 SkIntToScalar(249),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198 0,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000199 0.0078740157f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000200 SkIntToScalar(239),
201 0, 0, SK_Scalar1);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000202 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
203 SkShader::kRepeat_TileMode, &matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000204
reed@google.com99c114e2012-05-03 20:14:26 +0000205 SkPaint paint;
206 paint.setShader(s)->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000207
reed@google.com99c114e2012-05-03 20:14:26 +0000208 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
209 c.drawRect(r, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000210
reed@google.com99c114e2012-05-03 20:14:26 +0000211 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
212}
213
214/*
215 * Original bug was asserting that the matrix-proc had generated a (Y) value
216 * that was out of range. This led (in the release build) to the sampler-proc
217 * reading memory out-of-bounds of the original bitmap.
218 *
219 * We were numerically overflowing our 16bit coordinates that we communicate
220 * between these two procs. The fixes was in two parts:
221 *
222 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
223 * can't represent those coordinates in our transport format (yet).
224 * 2. Perform an unsigned shift during the calculation, so we don't get
225 * sign-extension bleed when packing the two values (X,Y) into our 32bit
226 * slot.
227 *
228 * This tests exercises the original setup, plus 3 more to ensure that we can,
229 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
230 * memory allocation limit).
231 */
232static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
233 static const struct {
234 int fWidth;
235 int fHeight;
236 bool fExpectedToDraw;
237 } gTests[] = {
238 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
239 { 0xFFFF, 0x7f, true }, // should draw, test max width
240 { 0x7f, 0xFFFF, true }, // should draw, test max height
241 { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
242 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000243
reed@google.com99c114e2012-05-03 20:14:26 +0000244 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
245 test_wacky_bitmapshader(reporter,
246 gTests[i].fWidth, gTests[i].fHeight,
247 gTests[i].fExpectedToDraw);
248 }
249}
reed@google.com0da06272012-05-03 20:26:06 +0000250
reed@google.com99c114e2012-05-03 20:14:26 +0000251///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000252
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000253static void test_nan_antihair() {
reed@google.com6de0bfc2012-03-30 17:43:33 +0000254 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000255 bm.allocN32Pixels(20, 20);
reed@google.com6de0bfc2012-03-30 17:43:33 +0000256
257 SkCanvas canvas(bm);
258
259 SkPath path;
260 path.moveTo(0, 0);
261 path.lineTo(10, SK_ScalarNaN);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262
reed@google.com6de0bfc2012-03-30 17:43:33 +0000263 SkPaint paint;
264 paint.setAntiAlias(true);
265 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000266
reed@google.com6de0bfc2012-03-30 17:43:33 +0000267 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
268 // this would trigger an assert/crash.
269 //
270 // see rev. 3558
271 canvas.drawPath(path, paint);
272}
273
reed@google.com2ade0862011-03-17 17:48:04 +0000274static bool check_for_all_zeros(const SkBitmap& bm) {
275 SkAutoLockPixels alp(bm);
276
277 size_t count = bm.width() * bm.bytesPerPixel();
278 for (int y = 0; y < bm.height(); y++) {
279 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
280 for (size_t i = 0; i < count; i++) {
281 if (ptr[i]) {
282 return false;
283 }
284 }
285 }
286 return true;
287}
288
289static const int gWidth = 256;
290static const int gHeight = 256;
291
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000292static void create(SkBitmap* bm, SkColor color) {
293 bm->allocN32Pixels(gWidth, gHeight);
reed@google.com2ade0862011-03-17 17:48:04 +0000294 bm->eraseColor(color);
295}
296
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000297DEF_TEST(DrawBitmapRect, reporter) {
reed@google.com2ade0862011-03-17 17:48:04 +0000298 SkBitmap src, dst;
299
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000300 create(&src, 0xFFFFFFFF);
301 create(&dst, 0);
reed@google.com2ade0862011-03-17 17:48:04 +0000302
303 SkCanvas canvas(dst);
304
305 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
306 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
307
308 canvas.drawBitmapRect(src, &srcR, dstR, NULL);
309
310 // ensure that we draw nothing if srcR does not intersect the bitmap
311 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000312
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000313 test_nan_antihair();
reed@google.com99c114e2012-05-03 20:14:26 +0000314 test_giantrepeat_crbug118018(reporter);
reed@google.comad514302013-01-02 20:19:45 +0000315
316 test_treatAsSprite(reporter);
reed@google.com23d0ab72013-10-15 20:14:00 +0000317 test_faulty_pixelref(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000318}