blob: f523306b5d7ba60ab8c8eb2039f403203a719f7a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkMatrix.h"
12#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkSize.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040018#include "include/core/SkTileMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkTypes.h"
20#include "include/utils/SkRandom.h"
21#include "src/core/SkMatrixUtils.h"
22#include "tests/Test.h"
reed@google.comad514302013-01-02 20:19:45 +000023
reed@google.comcef5bb42013-10-15 20:05:24 +000024///////////////////////////////////////////////////////////////////////////////
25
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000026static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) {
reed@google.comad514302013-01-02 20:19:45 +000027 mat->setIdentity();
28 if (mask & SkMatrix::kTranslate_Mask) {
29 mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1());
30 }
31 if (mask & SkMatrix::kScale_Mask) {
32 mat->postScale(rand.nextSScalar1(), rand.nextSScalar1());
33 }
34 if (mask & SkMatrix::kAffine_Mask) {
35 mat->postRotate(rand.nextSScalar1() * 360);
36 }
37 if (mask & SkMatrix::kPerspective_Mask) {
38 mat->setPerspX(rand.nextSScalar1());
39 mat->setPerspY(rand.nextSScalar1());
40 }
41}
42
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000043static void rand_size(SkISize* size, SkRandom& rand) {
reed@google.comae573582013-01-03 15:22:40 +000044 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF);
45}
46
reed@google.comad514302013-01-02 20:19:45 +000047static void test_treatAsSprite(skiatest::Reporter* reporter) {
reed@google.comad514302013-01-02 20:19:45 +000048
49 SkMatrix mat;
reed@google.comae573582013-01-03 15:22:40 +000050 SkISize size;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000051 SkRandom rand;
reed@google.comad514302013-01-02 20:19:45 +000052
fmalitac7e211a2016-01-07 10:34:46 -080053 SkPaint noaaPaint;
54 SkPaint aaPaint;
55 aaPaint.setAntiAlias(true);
56
57 // assert: translate-only no-aa can always be treated as sprite
reed@google.comad514302013-01-02 20:19:45 +000058 for (int i = 0; i < 1000; ++i) {
59 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask);
60 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +000061 rand_size(&size, rand);
fmalitac7e211a2016-01-07 10:34:46 -080062 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
reed@google.comad514302013-01-02 20:19:45 +000063 }
64 }
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000065
reed@google.comad514302013-01-02 20:19:45 +000066 // assert: rotate/perspect is never treated as sprite
67 for (int i = 0; i < 1000; ++i) {
68 rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask);
69 for (int j = 0; j < 1000; ++j) {
reed@google.comae573582013-01-03 15:22:40 +000070 rand_size(&size, rand);
fmalitac7e211a2016-01-07 10:34:46 -080071 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, noaaPaint));
72 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +000073 }
74 }
75
reed@google.comae573582013-01-03 15:22:40 +000076 size.set(500, 600);
reed@google.comad514302013-01-02 20:19:45 +000077
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000078 const SkScalar tooMuchSubpixel = 100.1f;
reed@google.comad514302013-01-02 20:19:45 +000079 mat.setTranslate(tooMuchSubpixel, 0);
fmalitac7e211a2016-01-07 10:34:46 -080080 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +000081 mat.setTranslate(0, tooMuchSubpixel);
fmalitac7e211a2016-01-07 10:34:46 -080082 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +000083
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000084 const SkScalar tinySubPixel = 100.02f;
reed@google.comad514302013-01-02 20:19:45 +000085 mat.setTranslate(tinySubPixel, 0);
fmalitac7e211a2016-01-07 10:34:46 -080086 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +000087 mat.setTranslate(0, tinySubPixel);
fmalitac7e211a2016-01-07 10:34:46 -080088 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000089
reed@google.comad514302013-01-02 20:19:45 +000090 const SkScalar twoThirds = SK_Scalar1 * 2 / 3;
reed80ea19c2015-05-12 10:37:34 -070091 const SkScalar bigScale = (size.width() + twoThirds) / size.width();
reed@google.comad514302013-01-02 20:19:45 +000092 mat.setScale(bigScale, bigScale);
fmalitac7e211a2016-01-07 10:34:46 -080093 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, noaaPaint));
94 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +000095
reed@google.comad514302013-01-02 20:19:45 +000096 const SkScalar oneThird = SK_Scalar1 / 3;
reed80ea19c2015-05-12 10:37:34 -070097 const SkScalar smallScale = (size.width() + oneThird) / size.width();
reed@google.comad514302013-01-02 20:19:45 +000098 mat.setScale(smallScale, smallScale);
fmalitac7e211a2016-01-07 10:34:46 -080099 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
100 REPORTER_ASSERT(reporter, !SkTreatAsSprite(mat, size, aaPaint));
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000101
reed@google.comad514302013-01-02 20:19:45 +0000102 const SkScalar oneFortyth = SK_Scalar1 / 40;
reed80ea19c2015-05-12 10:37:34 -0700103 const SkScalar tinyScale = (size.width() + oneFortyth) / size.width();
reed@google.comad514302013-01-02 20:19:45 +0000104 mat.setScale(tinyScale, tinyScale);
fmalitac7e211a2016-01-07 10:34:46 -0800105 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, noaaPaint));
106 REPORTER_ASSERT(reporter, SkTreatAsSprite(mat, size, aaPaint));
reed@google.comad514302013-01-02 20:19:45 +0000107}
reed@google.com99c114e2012-05-03 20:14:26 +0000108
reed@google.com99c114e2012-05-03 20:14:26 +0000109static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
Mike Kleindac694d2018-12-18 10:13:52 -0500110 int width, int height) {
reed@google.com99c114e2012-05-03 20:14:26 +0000111 SkBitmap dev;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000112 dev.allocN32Pixels(0x56F, 0x4f6);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000113 dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114
reed@google.com99c114e2012-05-03 20:14:26 +0000115 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
reed@google.com99c114e2012-05-03 20:14:26 +0000117 SkCanvas c(dev);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000118 matrix.setAll(-119.34097f,
119 -43.436558f,
120 93489.945f,
121 43.436558f,
122 -119.34097f,
123 123.98426f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000124 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000125 c.concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126
reed@google.com99c114e2012-05-03 20:14:26 +0000127 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700128 if (bm.tryAllocN32Pixels(width, height)) {
Mike Reedf0ffb892017-10-03 14:47:21 -0400129 bm.eraseColor(SK_ColorRED);
130 } else {
Mike Kleindac694d2018-12-18 10:13:52 -0500131 SkASSERT(false);
132 return;
reed84825042014-09-02 12:50:45 -0700133 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000134
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000135 matrix.setAll(0.0078740157f,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000136 0,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000137 SkIntToScalar(249),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138 0,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000139 0.0078740157f,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000140 SkIntToScalar(239),
141 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +0000142 SkPaint paint;
Mike Reed50acf8f2019-04-08 13:20:23 -0400143 paint.setShader(bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &matrix));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000144
reed@google.com99c114e2012-05-03 20:14:26 +0000145 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
146 c.drawRect(r, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000147
Mike Kleindac694d2018-12-18 10:13:52 -0500148 for (int y = 0; y < dev.height(); ++y) {
149 for (int x = 0; x < dev.width(); ++x) {
150 if (SK_ColorTRANSPARENT == *dev.getAddr32(x, y)) {
151 REPORTER_ASSERT(reporter, false);
152 return;
153 }
154 }
155 }
reed@google.com99c114e2012-05-03 20:14:26 +0000156}
157
Mike Kleindac694d2018-12-18 10:13:52 -0500158// ATTENTION We should always draw each of these sizes safely now. ATTENTION
159// ATTENTION I'm leaving this next /*comment*/ for posterity. ATTENTION
160
reed@google.com99c114e2012-05-03 20:14:26 +0000161/*
162 * Original bug was asserting that the matrix-proc had generated a (Y) value
163 * that was out of range. This led (in the release build) to the sampler-proc
164 * reading memory out-of-bounds of the original bitmap.
165 *
166 * We were numerically overflowing our 16bit coordinates that we communicate
167 * between these two procs. The fixes was in two parts:
168 *
169 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
170 * can't represent those coordinates in our transport format (yet).
171 * 2. Perform an unsigned shift during the calculation, so we don't get
172 * sign-extension bleed when packing the two values (X,Y) into our 32bit
173 * slot.
174 *
Mike Reedf0ffb892017-10-03 14:47:21 -0400175 * This tests exercises the original setup, plus 2 more to ensure that we can,
reed@google.com99c114e2012-05-03 20:14:26 +0000176 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
177 * memory allocation limit).
178 */
179static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
180 static const struct {
181 int fWidth;
182 int fHeight;
reed@google.com99c114e2012-05-03 20:14:26 +0000183 } gTests[] = {
Mike Kleindac694d2018-12-18 10:13:52 -0500184 { 0x1b294, 0x7f}, // crbug 118018 (width exceeds 64K)... should draw safely now.
185 { 0xFFFF, 0x7f }, // should draw, test max width
186 { 0x7f, 0xFFFF }, // should draw, test max height
reed@google.com99c114e2012-05-03 20:14:26 +0000187 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188
reed@google.com99c114e2012-05-03 20:14:26 +0000189 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
190 test_wacky_bitmapshader(reporter,
Mike Kleindac694d2018-12-18 10:13:52 -0500191 gTests[i].fWidth, gTests[i].fHeight);
reed@google.com99c114e2012-05-03 20:14:26 +0000192 }
193}
reed@google.com0da06272012-05-03 20:26:06 +0000194
reed@google.com99c114e2012-05-03 20:14:26 +0000195///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000196
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000197static void test_nan_antihair() {
reed@google.com6de0bfc2012-03-30 17:43:33 +0000198 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000199 bm.allocN32Pixels(20, 20);
reed@google.com6de0bfc2012-03-30 17:43:33 +0000200
201 SkCanvas canvas(bm);
202
203 SkPath path;
204 path.moveTo(0, 0);
205 path.lineTo(10, SK_ScalarNaN);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000206
reed@google.com6de0bfc2012-03-30 17:43:33 +0000207 SkPaint paint;
208 paint.setAntiAlias(true);
209 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000210
reed@google.com6de0bfc2012-03-30 17:43:33 +0000211 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
212 // this would trigger an assert/crash.
213 //
214 // see rev. 3558
215 canvas.drawPath(path, paint);
216}
217
reed@google.com2ade0862011-03-17 17:48:04 +0000218static bool check_for_all_zeros(const SkBitmap& bm) {
reed@google.com2ade0862011-03-17 17:48:04 +0000219 size_t count = bm.width() * bm.bytesPerPixel();
220 for (int y = 0; y < bm.height(); y++) {
221 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
222 for (size_t i = 0; i < count; i++) {
223 if (ptr[i]) {
224 return false;
225 }
226 }
227 }
228 return true;
229}
230
231static const int gWidth = 256;
232static const int gHeight = 256;
233
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000234static void create(SkBitmap* bm, SkColor color) {
235 bm->allocN32Pixels(gWidth, gHeight);
reed@google.com2ade0862011-03-17 17:48:04 +0000236 bm->eraseColor(color);
237}
238
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000239DEF_TEST(DrawBitmapRect, reporter) {
reed@google.com2ade0862011-03-17 17:48:04 +0000240 SkBitmap src, dst;
241
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000242 create(&src, 0xFFFFFFFF);
243 create(&dst, 0);
reed@google.com2ade0862011-03-17 17:48:04 +0000244
245 SkCanvas canvas(dst);
246
247 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
248 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
249
halcanary96fcdcc2015-08-27 07:41:13 -0700250 canvas.drawBitmapRect(src, srcR, dstR, nullptr);
reed@google.com2ade0862011-03-17 17:48:04 +0000251
252 // ensure that we draw nothing if srcR does not intersect the bitmap
253 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000254
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000255 test_nan_antihair();
reed@google.com99c114e2012-05-03 20:14:26 +0000256 test_giantrepeat_crbug118018(reporter);
reed@google.comad514302013-01-02 20:19:45 +0000257
258 test_treatAsSprite(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000259}