blob: 43132e92f484420bf8c7802d4b574706492c0cf1 [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"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040010#include "SkColor.h"
11#include "SkMatrix.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "SkMatrixUtils.h"
reed@google.comcef5bb42013-10-15 20:05:24 +000013#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "SkRandom.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040016#include "SkRect.h"
17#include "SkScalar.h"
reed@google.com99c114e2012-05-03 20:14:26 +000018#include "SkShader.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040019#include "SkSize.h"
20#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "Test.h"
reed@google.comad514302013-01-02 20:19:45 +000022
reed@google.comcef5bb42013-10-15 20:05:24 +000023///////////////////////////////////////////////////////////////////////////////
24
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000025static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) {
reed@google.comad514302013-01-02 20:19:45 +000026 mat->setIdentity();
27 if (mask & SkMatrix::kTranslate_Mask) {
28 mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1());
29 }
30 if (mask & SkMatrix::kScale_Mask) {
31 mat->postScale(rand.nextSScalar1(), rand.nextSScalar1());
32 }
33 if (mask & SkMatrix::kAffine_Mask) {
34 mat->postRotate(rand.nextSScalar1() * 360);
35 }
36 if (mask & SkMatrix::kPerspective_Mask) {
37 mat->setPerspX(rand.nextSScalar1());
38 mat->setPerspY(rand.nextSScalar1());
39 }
40}
41
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000042static void rand_size(SkISize* size, SkRandom& rand) {
reed@google.comae573582013-01-03 15:22:40 +000043 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF);
44}
45
reed@google.comad514302013-01-02 20:19:45 +000046static void test_treatAsSprite(skiatest::Reporter* reporter) {
reed@google.comad514302013-01-02 20:19:45 +000047
48 SkMatrix mat;
reed@google.comae573582013-01-03 15:22:40 +000049 SkISize size;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000050 SkRandom rand;
reed@google.comad514302013-01-02 20:19:45 +000051
fmalitac7e211a2016-01-07 10:34:46 -080052 SkPaint noaaPaint;
53 SkPaint aaPaint;
54 aaPaint.setAntiAlias(true);
55
56 // assert: translate-only no-aa can always be treated as sprite
reed@google.comad514302013-01-02 20:19:45 +000057 for (int i = 0; i < 1000; ++i) {
58 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask);
59 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +000060 rand_size(&size, rand);
fmalitac7e211a2016-01-07 10:34:46 -080061 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
reed@google.comad514302013-01-02 20:19:45 +000062 }
63 }
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000064
reed@google.comad514302013-01-02 20:19:45 +000065 // assert: rotate/perspect is never treated as sprite
66 for (int i = 0; i < 1000; ++i) {
67 rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask);
68 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +000069 rand_size(&size, rand);
fmalitac7e211a2016-01-07 10:34:46 -080070 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, noaaPaint));
71 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +000072 }
73 }
74
reed@google.comae573582013-01-03 15:22:40 +000075 size.set(500, 600);
reed@google.comad514302013-01-02 20:19:45 +000076
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000077 const SkScalar tooMuchSubpixel = 100.1f;
reed@google.comad514302013-01-02 20:19:45 +000078 mat.setTranslate(tooMuchSubpixel, 0);
fmalitac7e211a2016-01-07 10:34:46 -080079 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +000080 mat.setTranslate(0, tooMuchSubpixel);
fmalitac7e211a2016-01-07 10:34:46 -080081 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +000082
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000083 const SkScalar tinySubPixel = 100.02f;
reed@google.comad514302013-01-02 20:19:45 +000084 mat.setTranslate(tinySubPixel, 0);
fmalitac7e211a2016-01-07 10:34:46 -080085 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +000086 mat.setTranslate(0, tinySubPixel);
fmalitac7e211a2016-01-07 10:34:46 -080087 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000088
reed@google.comad514302013-01-02 20:19:45 +000089 const SkScalar twoThirds = SK_Scalar1 * 2 / 3;
reed80ea19c2015-05-12 10:37:34 -070090 const SkScalar bigScale = (size.width() + twoThirds) / size.width();
reed@google.comad514302013-01-02 20:19:45 +000091 mat.setScale(bigScale, bigScale);
fmalitac7e211a2016-01-07 10:34:46 -080092 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, noaaPaint));
93 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000094
reed@google.comad514302013-01-02 20:19:45 +000095 const SkScalar oneThird = SK_Scalar1 / 3;
reed80ea19c2015-05-12 10:37:34 -070096 const SkScalar smallScale = (size.width() + oneThird) / size.width();
reed@google.comad514302013-01-02 20:19:45 +000097 mat.setScale(smallScale, smallScale);
fmalitac7e211a2016-01-07 10:34:46 -080098 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
99 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000100
reed@google.comad514302013-01-02 20:19:45 +0000101 const SkScalar oneFortyth = SK_Scalar1 / 40;
reed80ea19c2015-05-12 10:37:34 -0700102 const SkScalar tinyScale = (size.width() + oneFortyth) / size.width();
reed@google.comad514302013-01-02 20:19:45 +0000103 mat.setScale(tinyScale, tinyScale);
fmalitac7e211a2016-01-07 10:34:46 -0800104 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
105 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +0000106}
reed@google.com99c114e2012-05-03 20:14:26 +0000107
108static void assert_ifDrawnTo(skiatest::Reporter* reporter,
109 const SkBitmap& bm, bool shouldBeDrawn) {
110 for (int y = 0; y < bm.height(); ++y) {
111 for (int x = 0; x < bm.width(); ++x) {
112 if (shouldBeDrawn) {
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000113 if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000114 REPORTER_ASSERT(reporter, false);
115 return;
116 }
117 } else {
118 // should not be drawn
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000119 if (SK_ColorTRANSPARENT != *bm.getAddr32(x, y)) {
reed@google.com99c114e2012-05-03 20:14:26 +0000120 REPORTER_ASSERT(reporter, false);
121 return;
122 }
123 }
124 }
125 }
126}
127
128static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
129 int width, int height, bool shouldBeDrawn) {
130 SkBitmap dev;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000131 dev.allocN32Pixels(0x56F, 0x4f6);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000132 dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133
reed@google.com99c114e2012-05-03 20:14:26 +0000134 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000135
reed@google.com99c114e2012-05-03 20:14:26 +0000136 SkCanvas c(dev);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000137 matrix.setAll(-119.34097f,
138 -43.436558f,
139 93489.945f,
140 43.436558f,
141 -119.34097f,
142 123.98426f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000143 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000144 c.concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145
reed@google.com99c114e2012-05-03 20:14:26 +0000146 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700147 if (bm.tryAllocN32Pixels(width, height)) {
Mike Reedf0ffb892017-10-03 14:47:21 -0400148 bm.eraseColor(SK_ColorRED);
149 } else {
150 shouldBeDrawn = false;
reed84825042014-09-02 12:50:45 -0700151 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000152
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000153 matrix.setAll(0.0078740157f,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000154 0,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000155 SkIntToScalar(249),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000156 0,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000157 0.0078740157f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000158 SkIntToScalar(239),
159 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000160 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700161 paint.setShader(SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode,
162 SkShader::kRepeat_TileMode, &matrix));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163
reed@google.com99c114e2012-05-03 20:14:26 +0000164 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
165 c.drawRect(r, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166
reed@google.com99c114e2012-05-03 20:14:26 +0000167 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
168}
169
170/*
171 * Original bug was asserting that the matrix-proc had generated a (Y) value
172 * that was out of range. This led (in the release build) to the sampler-proc
173 * reading memory out-of-bounds of the original bitmap.
174 *
175 * We were numerically overflowing our 16bit coordinates that we communicate
176 * between these two procs. The fixes was in two parts:
177 *
178 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
179 * can't represent those coordinates in our transport format (yet).
180 * 2. Perform an unsigned shift during the calculation, so we don't get
181 * sign-extension bleed when packing the two values (X,Y) into our 32bit
182 * slot.
183 *
Mike Reedf0ffb892017-10-03 14:47:21 -0400184 * This tests exercises the original setup, plus 2 more to ensure that we can,
reed@google.com99c114e2012-05-03 20:14:26 +0000185 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
186 * memory allocation limit).
187 */
188static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
189 static const struct {
190 int fWidth;
191 int fHeight;
192 bool fExpectedToDraw;
193 } gTests[] = {
194 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
195 { 0xFFFF, 0x7f, true }, // should draw, test max width
196 { 0x7f, 0xFFFF, true }, // should draw, test max height
reed@google.com99c114e2012-05-03 20:14:26 +0000197 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198
reed@google.com99c114e2012-05-03 20:14:26 +0000199 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
200 test_wacky_bitmapshader(reporter,
201 gTests[i].fWidth, gTests[i].fHeight,
202 gTests[i].fExpectedToDraw);
203 }
204}
reed@google.com0da06272012-05-03 20:26:06 +0000205
reed@google.com99c114e2012-05-03 20:14:26 +0000206///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000207
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000208static void test_nan_antihair() {
reed@google.com6de0bfc2012-03-30 17:43:33 +0000209 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000210 bm.allocN32Pixels(20, 20);
reed@google.com6de0bfc2012-03-30 17:43:33 +0000211
212 SkCanvas canvas(bm);
213
214 SkPath path;
215 path.moveTo(0, 0);
216 path.lineTo(10, SK_ScalarNaN);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000217
reed@google.com6de0bfc2012-03-30 17:43:33 +0000218 SkPaint paint;
219 paint.setAntiAlias(true);
220 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
reed@google.com6de0bfc2012-03-30 17:43:33 +0000222 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
223 // this would trigger an assert/crash.
224 //
225 // see rev. 3558
226 canvas.drawPath(path, paint);
227}
228
reed@google.com2ade0862011-03-17 17:48:04 +0000229static bool check_for_all_zeros(const SkBitmap& bm) {
reed@google.com2ade0862011-03-17 17:48:04 +0000230 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
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000245static void create(SkBitmap* bm, SkColor color) {
246 bm->allocN32Pixels(gWidth, gHeight);
reed@google.com2ade0862011-03-17 17:48:04 +0000247 bm->eraseColor(color);
248}
249
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000250DEF_TEST(DrawBitmapRect, reporter) {
reed@google.com2ade0862011-03-17 17:48:04 +0000251 SkBitmap src, dst;
252
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000253 create(&src, 0xFFFFFFFF);
254 create(&dst, 0);
reed@google.com2ade0862011-03-17 17:48:04 +0000255
256 SkCanvas canvas(dst);
257
258 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
259 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
260
halcanary96fcdcc2015-08-27 07:41:13 -0700261 canvas.drawBitmapRect(src, srcR, dstR, nullptr);
reed@google.com2ade0862011-03-17 17:48:04 +0000262
263 // ensure that we draw nothing if srcR does not intersect the bitmap
264 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000265
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000266 test_nan_antihair();
reed@google.com99c114e2012-05-03 20:14:26 +0000267 test_giantrepeat_crbug118018(reporter);
reed@google.comad514302013-01-02 20:19:45 +0000268
269 test_treatAsSprite(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000270}