blob: 88b9437935cebe326a2ce51b0ebe7c7ecdfc91b0 [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"
bungemand3ebb482015-08-05 13:57:49 -070015#include "SkPath.h"
reed74bd9532015-09-14 08:52:12 -070016#include "SkPixelRef.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "SkRandom.h"
reed@google.com99c114e2012-05-03 20:14:26 +000018#include "SkShader.h"
reed@google.comcef5bb42013-10-15 20:05:24 +000019#include "SkSurface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "Test.h"
reed@google.comad514302013-01-02 20:19:45 +000021
reed74bd9532015-09-14 08:52:12 -070022class FailurePixelRef : public SkPixelRef {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000023public:
reed74bd9532015-09-14 08:52:12 -070024 FailurePixelRef(const SkImageInfo& info) : SkPixelRef(info) {}
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000025protected:
reed74bd9532015-09-14 08:52:12 -070026 bool onNewLockPixels(LockRec*) override { return false; }
27 void onUnlockPixels() override {}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000028};
reed@google.comcef5bb42013-10-15 20:05:24 +000029
30// crbug.com/295895
31// Crashing in skia when a pixelref fails in lockPixels
32//
33static void test_faulty_pixelref(skiatest::Reporter* reporter) {
34 // 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 +000035 SkAutoTUnref<SkDiscardableMemoryPool> pool(
halcanary96fcdcc2015-08-27 07:41:13 -070036 SkDiscardableMemoryPool::Create(10 * 1000, nullptr));
reed74bd9532015-09-14 08:52:12 -070037
reed@google.comcef5bb42013-10-15 20:05:24 +000038 SkBitmap bm;
reed74bd9532015-09-14 08:52:12 -070039 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
40 bm.setInfo(info);
41 bm.setPixelRef(new FailurePixelRef(info), 0, 0)->unref();
reed@google.comcef5bb42013-10-15 20:05:24 +000042 // now our bitmap has a pixelref, but we know it will fail to lock
43
reed3054be12014-12-10 07:24:28 -080044 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(200, 200));
reed@google.comcef5bb42013-10-15 20:05:24 +000045 SkCanvas* canvas = surface->getCanvas();
46
reed93a12152015-03-16 10:08:34 -070047 const SkFilterQuality levels[] = {
48 kNone_SkFilterQuality,
49 kLow_SkFilterQuality,
50 kMedium_SkFilterQuality,
51 kHigh_SkFilterQuality,
reed@google.comcef5bb42013-10-15 20:05:24 +000052 };
53
54 SkPaint paint;
55 canvas->scale(2, 2); // need a scale, otherwise we may ignore filtering
56 for (size_t i = 0; i < SK_ARRAY_COUNT(levels); ++i) {
reed93a12152015-03-16 10:08:34 -070057 paint.setFilterQuality(levels[i]);
reed@google.comcef5bb42013-10-15 20:05:24 +000058 canvas->drawBitmap(bm, 0, 0, &paint);
59 }
60}
61
62///////////////////////////////////////////////////////////////////////////////
63
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000064static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) {
reed@google.comad514302013-01-02 20:19:45 +000065 mat->setIdentity();
66 if (mask & SkMatrix::kTranslate_Mask) {
67 mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1());
68 }
69 if (mask & SkMatrix::kScale_Mask) {
70 mat->postScale(rand.nextSScalar1(), rand.nextSScalar1());
71 }
72 if (mask & SkMatrix::kAffine_Mask) {
73 mat->postRotate(rand.nextSScalar1() * 360);
74 }
75 if (mask & SkMatrix::kPerspective_Mask) {
76 mat->setPerspX(rand.nextSScalar1());
77 mat->setPerspY(rand.nextSScalar1());
78 }
79}
80
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000081static void rand_size(SkISize* size, SkRandom& rand) {
reed@google.comae573582013-01-03 15:22:40 +000082 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF);
83}
84
reed@google.comad514302013-01-02 20:19:45 +000085static void test_treatAsSprite(skiatest::Reporter* reporter) {
reed@google.comad514302013-01-02 20:19:45 +000086
87 SkMatrix mat;
reed@google.comae573582013-01-03 15:22:40 +000088 SkISize size;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000089 SkRandom rand;
reed@google.comad514302013-01-02 20:19:45 +000090
fmalitac7e211a2016-01-07 10:34:46 -080091 SkPaint noaaPaint;
92 SkPaint aaPaint;
93 aaPaint.setAntiAlias(true);
94
95 // assert: translate-only no-aa can always be treated as sprite
reed@google.comad514302013-01-02 20:19:45 +000096 for (int i = 0; i < 1000; ++i) {
97 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask);
98 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +000099 rand_size(&size, rand);
fmalitac7e211a2016-01-07 10:34:46 -0800100 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
reed@google.comad514302013-01-02 20:19:45 +0000101 }
102 }
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000103
reed@google.comad514302013-01-02 20:19:45 +0000104 // assert: rotate/perspect is never treated as sprite
105 for (int i = 0; i < 1000; ++i) {
106 rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask);
107 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +0000108 rand_size(&size, rand);
fmalitac7e211a2016-01-07 10:34:46 -0800109 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, noaaPaint));
110 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +0000111 }
112 }
113
reed@google.comae573582013-01-03 15:22:40 +0000114 size.set(500, 600);
reed@google.comad514302013-01-02 20:19:45 +0000115
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000116 const SkScalar tooMuchSubpixel = 100.1f;
reed@google.comad514302013-01-02 20:19:45 +0000117 mat.setTranslate(tooMuchSubpixel, 0);
fmalitac7e211a2016-01-07 10:34:46 -0800118 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +0000119 mat.setTranslate(0, tooMuchSubpixel);
fmalitac7e211a2016-01-07 10:34:46 -0800120 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +0000121
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000122 const SkScalar tinySubPixel = 100.02f;
reed@google.comad514302013-01-02 20:19:45 +0000123 mat.setTranslate(tinySubPixel, 0);
fmalitac7e211a2016-01-07 10:34:46 -0800124 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +0000125 mat.setTranslate(0, tinySubPixel);
fmalitac7e211a2016-01-07 10:34:46 -0800126 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000127
reed@google.comad514302013-01-02 20:19:45 +0000128 const SkScalar twoThirds = SK_Scalar1 * 2 / 3;
reed80ea19c2015-05-12 10:37:34 -0700129 const SkScalar bigScale = (size.width() + twoThirds) / size.width();
reed@google.comad514302013-01-02 20:19:45 +0000130 mat.setScale(bigScale, bigScale);
fmalitac7e211a2016-01-07 10:34:46 -0800131 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, noaaPaint));
132 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000133
reed@google.comad514302013-01-02 20:19:45 +0000134 const SkScalar oneThird = SK_Scalar1 / 3;
reed80ea19c2015-05-12 10:37:34 -0700135 const SkScalar smallScale = (size.width() + oneThird) / size.width();
reed@google.comad514302013-01-02 20:19:45 +0000136 mat.setScale(smallScale, smallScale);
fmalitac7e211a2016-01-07 10:34:46 -0800137 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
138 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000139
reed@google.comad514302013-01-02 20:19:45 +0000140 const SkScalar oneFortyth = SK_Scalar1 / 40;
reed80ea19c2015-05-12 10:37:34 -0700141 const SkScalar tinyScale = (size.width() + oneFortyth) / size.width();
reed@google.comad514302013-01-02 20:19:45 +0000142 mat.setScale(tinyScale, tinyScale);
fmalitac7e211a2016-01-07 10:34:46 -0800143 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
144 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +0000145}
reed@google.com99c114e2012-05-03 20:14:26 +0000146
147static void assert_ifDrawnTo(skiatest::Reporter* reporter,
148 const SkBitmap& bm, bool shouldBeDrawn) {
149 for (int y = 0; y < bm.height(); ++y) {
150 for (int x = 0; x < bm.width(); ++x) {
151 if (shouldBeDrawn) {
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000152 if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000153 REPORTER_ASSERT(reporter, false);
154 return;
155 }
156 } else {
157 // should not be drawn
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000158 if (SK_ColorTRANSPARENT != *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000159 REPORTER_ASSERT(reporter, false);
160 return;
161 }
162 }
163 }
164 }
165}
166
167static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
168 int width, int height, bool shouldBeDrawn) {
169 SkBitmap dev;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000170 dev.allocN32Pixels(0x56F, 0x4f6);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000171 dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
rmistry@google.comd6176b02012-08-23 18:14:13 +0000172
reed@google.com99c114e2012-05-03 20:14:26 +0000173 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000174
reed@google.com99c114e2012-05-03 20:14:26 +0000175 SkCanvas c(dev);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000176 matrix.setAll(-119.34097f,
177 -43.436558f,
178 93489.945f,
179 43.436558f,
180 -119.34097f,
181 123.98426f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000182 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000183 c.concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000184
reed@google.com99c114e2012-05-03 20:14:26 +0000185 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700186 if (bm.tryAllocN32Pixels(width, height)) {
187 // allow this to fail silently, to test the code downstream
188 }
reed@google.com99c114e2012-05-03 20:14:26 +0000189 bm.eraseColor(SK_ColorRED);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000191 matrix.setAll(0.0078740157f,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000192 0,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000193 SkIntToScalar(249),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194 0,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000195 0.0078740157f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000196 SkIntToScalar(239),
197 0, 0, SK_Scalar1);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000198 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
199 SkShader::kRepeat_TileMode, &matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000200
reed@google.com99c114e2012-05-03 20:14:26 +0000201 SkPaint paint;
202 paint.setShader(s)->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000203
reed@google.com99c114e2012-05-03 20:14:26 +0000204 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
205 c.drawRect(r, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000206
reed@google.com99c114e2012-05-03 20:14:26 +0000207 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
208}
209
210/*
211 * Original bug was asserting that the matrix-proc had generated a (Y) value
212 * that was out of range. This led (in the release build) to the sampler-proc
213 * reading memory out-of-bounds of the original bitmap.
214 *
215 * We were numerically overflowing our 16bit coordinates that we communicate
216 * between these two procs. The fixes was in two parts:
217 *
218 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
219 * can't represent those coordinates in our transport format (yet).
220 * 2. Perform an unsigned shift during the calculation, so we don't get
221 * sign-extension bleed when packing the two values (X,Y) into our 32bit
222 * slot.
223 *
224 * This tests exercises the original setup, plus 3 more to ensure that we can,
225 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
226 * memory allocation limit).
227 */
228static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
229 static const struct {
230 int fWidth;
231 int fHeight;
232 bool fExpectedToDraw;
233 } gTests[] = {
234 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
235 { 0xFFFF, 0x7f, true }, // should draw, test max width
236 { 0x7f, 0xFFFF, true }, // should draw, test max height
237 { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
238 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000239
reed@google.com99c114e2012-05-03 20:14:26 +0000240 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
241 test_wacky_bitmapshader(reporter,
242 gTests[i].fWidth, gTests[i].fHeight,
243 gTests[i].fExpectedToDraw);
244 }
245}
reed@google.com0da06272012-05-03 20:26:06 +0000246
reed@google.com99c114e2012-05-03 20:14:26 +0000247///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000248
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000249static void test_nan_antihair() {
reed@google.com6de0bfc2012-03-30 17:43:33 +0000250 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000251 bm.allocN32Pixels(20, 20);
reed@google.com6de0bfc2012-03-30 17:43:33 +0000252
253 SkCanvas canvas(bm);
254
255 SkPath path;
256 path.moveTo(0, 0);
257 path.lineTo(10, SK_ScalarNaN);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258
reed@google.com6de0bfc2012-03-30 17:43:33 +0000259 SkPaint paint;
260 paint.setAntiAlias(true);
261 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262
reed@google.com6de0bfc2012-03-30 17:43:33 +0000263 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
264 // this would trigger an assert/crash.
265 //
266 // see rev. 3558
267 canvas.drawPath(path, paint);
268}
269
reed@google.com2ade0862011-03-17 17:48:04 +0000270static bool check_for_all_zeros(const SkBitmap& bm) {
271 SkAutoLockPixels alp(bm);
272
273 size_t count = bm.width() * bm.bytesPerPixel();
274 for (int y = 0; y < bm.height(); y++) {
275 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
276 for (size_t i = 0; i < count; i++) {
277 if (ptr[i]) {
278 return false;
279 }
280 }
281 }
282 return true;
283}
284
285static const int gWidth = 256;
286static const int gHeight = 256;
287
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000288static void create(SkBitmap* bm, SkColor color) {
289 bm->allocN32Pixels(gWidth, gHeight);
reed@google.com2ade0862011-03-17 17:48:04 +0000290 bm->eraseColor(color);
291}
292
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000293DEF_TEST(DrawBitmapRect, reporter) {
reed@google.com2ade0862011-03-17 17:48:04 +0000294 SkBitmap src, dst;
295
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000296 create(&src, 0xFFFFFFFF);
297 create(&dst, 0);
reed@google.com2ade0862011-03-17 17:48:04 +0000298
299 SkCanvas canvas(dst);
300
301 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
302 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
303
halcanary96fcdcc2015-08-27 07:41:13 -0700304 canvas.drawBitmapRect(src, srcR, dstR, nullptr);
reed@google.com2ade0862011-03-17 17:48:04 +0000305
306 // ensure that we draw nothing if srcR does not intersect the bitmap
307 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000308
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000309 test_nan_antihair();
reed@google.com99c114e2012-05-03 20:14:26 +0000310 test_giantrepeat_crbug118018(reporter);
reed@google.comad514302013-01-02 20:19:45 +0000311
312 test_treatAsSprite(reporter);
reed@google.com23d0ab72013-10-15 20:14:00 +0000313 test_faulty_pixelref(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000314}