blob: 7d2b5e5a81a5cffed58af7a11fed026eded0c80c [file] [log] [blame]
reed@google.com83226972012-06-07 20:26:47 +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/SkCanvas.h"
9#include "include/core/SkColorPriv.h"
10#include "include/core/SkShader.h"
11#include "include/core/SkSurface.h"
12#include "include/effects/SkGradientShader.h"
13#include "include/private/SkTemplates.h"
14#include "src/core/SkTLazy.h"
15#include "src/shaders/SkColorShader.h"
16#include "tests/Test.h"
reed@google.com83226972012-06-07 20:26:47 +000017
reed9d91eb32015-01-28 11:44:48 -080018// https://code.google.com/p/chromium/issues/detail?id=448299
19// Giant (inverse) matrix causes overflow when converting/computing using 32.32
20// Before the fix, we would assert (and then crash).
21static void test_big_grad(skiatest::Reporter* reporter) {
22 const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
23 const SkPoint pts[] = {{ 15, 14.7112684f }, { 0.709064007f, 12.6108112f }};
reed9d91eb32015-01-28 11:44:48 -080024 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -040025 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
reed9d91eb32015-01-28 11:44:48 -080026
27 SkBitmap bm;
28 bm.allocN32Pixels(2000, 1);
29 SkCanvas c(bm);
30
31 const SkScalar affine[] = {
32 1.06608627e-06f, 4.26434525e-07f, 6.2855f, 2.6611f, 273.4393f, 244.0046f
33 };
34 SkMatrix matrix;
35 matrix.setAffine(affine);
36 c.concat(matrix);
halcanary9d524f22016-03-29 09:03:52 -070037
reed9d91eb32015-01-28 11:44:48 -080038 c.drawPaint(paint);
39}
40
reed@google.com83226972012-06-07 20:26:47 +000041struct GradRec {
42 int fColorCount;
43 const SkColor* fColors;
44 const SkScalar* fPos;
45 const SkPoint* fPoint; // 2
46 const SkScalar* fRadius; // 2
Mike Reedfae8fce2019-04-03 10:27:45 -040047 SkTileMode fTileMode;
reed@google.com83226972012-06-07 20:26:47 +000048
reed1a9b9642016-03-13 14:13:58 -070049 void gradCheck(skiatest::Reporter* reporter, const sk_sp<SkShader>& shader,
reed@google.com83226972012-06-07 20:26:47 +000050 SkShader::GradientInfo* info,
51 SkShader::GradientType gt) const {
52 SkAutoTMalloc<SkColor> colorStorage(fColorCount);
53 SkAutoTMalloc<SkScalar> posStorage(fColorCount);
54
55 info->fColorCount = fColorCount;
56 info->fColors = colorStorage;
57 info->fColorOffsets = posStorage.get();
58 REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt);
59
60 REPORTER_ASSERT(reporter, info->fColorCount == fColorCount);
61 REPORTER_ASSERT(reporter,
62 !memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor)));
63 REPORTER_ASSERT(reporter,
64 !memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar)));
Mike Reedfae8fce2019-04-03 10:27:45 -040065 REPORTER_ASSERT(reporter, fTileMode == (SkTileMode)info->fTileMode);
reed@google.com83226972012-06-07 20:26:47 +000066 }
67};
68
69
fmalita51229672016-08-22 06:22:28 -070070static void none_gradproc(skiatest::Reporter* reporter, const GradRec&, const GradRec&) {
Mike Reedc8bea7d2019-04-09 13:55:36 -040071 sk_sp<SkShader> s(SkShaders::Empty());
halcanary96fcdcc2015-08-27 07:41:13 -070072 REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(nullptr));
reed@google.com83226972012-06-07 20:26:47 +000073}
74
fmalita51229672016-08-22 06:22:28 -070075static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec, const GradRec&) {
Hal Canary342b7ac2016-11-04 11:49:42 -040076 sk_sp<SkShader> s(new SkColorShader(rec.fColors[0]));
halcanary96fcdcc2015-08-27 07:41:13 -070077 REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(nullptr));
reed@google.com83226972012-06-07 20:26:47 +000078
79 SkShader::GradientInfo info;
halcanary96fcdcc2015-08-27 07:41:13 -070080 info.fColors = nullptr;
reed@google.com83226972012-06-07 20:26:47 +000081 info.fColorCount = 0;
82 s->asAGradient(&info);
83 REPORTER_ASSERT(reporter, 1 == info.fColorCount);
84}
85
fmalita51229672016-08-22 06:22:28 -070086static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
87 const GradRec& checkRec) {
88 sk_sp<SkShader> s(SkGradientShader::MakeLinear(buildRec.fPoint, buildRec.fColors, buildRec.fPos,
89 buildRec.fColorCount, buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +000090
reed@google.com83226972012-06-07 20:26:47 +000091 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -070092 checkRec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType);
93 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint)));
reed@google.com83226972012-06-07 20:26:47 +000094}
95
fmalita51229672016-08-22 06:22:28 -070096static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
97 const GradRec& checkRec) {
98 sk_sp<SkShader> s(SkGradientShader::MakeRadial(buildRec.fPoint[0], buildRec.fRadius[0],
99 buildRec.fColors, buildRec.fPos,
100 buildRec.fColorCount, buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
reed@google.com83226972012-06-07 20:26:47 +0000102 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700103 checkRec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType);
104 REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]);
105 REPORTER_ASSERT(reporter, info.fRadius[0] == checkRec.fRadius[0]);
reed@google.com83226972012-06-07 20:26:47 +0000106}
107
fmalita51229672016-08-22 06:22:28 -0700108static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
109 const GradRec& checkRec) {
110 sk_sp<SkShader> s(SkGradientShader::MakeSweep(buildRec.fPoint[0].fX, buildRec.fPoint[0].fY,
111 buildRec.fColors, buildRec.fPos,
112 buildRec.fColorCount));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113
reed@google.com83226972012-06-07 20:26:47 +0000114 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700115 checkRec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType);
116 REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]);
reed@google.com83226972012-06-07 20:26:47 +0000117}
118
fmalita51229672016-08-22 06:22:28 -0700119static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
120 const GradRec& checkRec) {
121 sk_sp<SkShader> s(SkGradientShader::MakeTwoPointConical(buildRec.fPoint[0],
122 buildRec.fRadius[0],
123 buildRec.fPoint[1],
124 buildRec.fRadius[1],
125 buildRec.fColors,
126 buildRec.fPos,
127 buildRec.fColorCount,
128 buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000129
reed@google.com83226972012-06-07 20:26:47 +0000130 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700131 checkRec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType);
132 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint)));
133 REPORTER_ASSERT(reporter, !memcmp(info.fRadius, checkRec.fRadius, 2 * sizeof(SkScalar)));
reed@google.com83226972012-06-07 20:26:47 +0000134}
135
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000136// Ensure that repeated color gradients behave like drawing a single color
sugoi@google.come0e385c2013-03-11 18:50:03 +0000137static void TestConstantGradient(skiatest::Reporter*) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000138 const SkPoint pts[] = {
139 { 0, 0 },
140 { SkIntToScalar(10), 0 }
141 };
142 SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE };
143 const SkScalar pos[] = { 0, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700144 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400145 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000146 SkBitmap outBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000147 outBitmap.allocN32Pixels(10, 1);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000148 SkCanvas canvas(outBitmap);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000149 canvas.drawPaint(paint);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000150 for (int i = 0; i < 10; i++) {
151 // The following is commented out because it currently fails
152 // Related bug: https://code.google.com/p/skia/issues/detail?id=1098
153
154 // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0));
155 }
156}
157
fmalita51229672016-08-22 06:22:28 -0700158typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&, const GradRec&);
reed@google.com83226972012-06-07 20:26:47 +0000159
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000160static void TestGradientShaders(skiatest::Reporter* reporter) {
reed@google.com83226972012-06-07 20:26:47 +0000161 static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
162 static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
163 static const SkPoint gPts[] = {
164 { 0, 0 },
165 { SkIntToScalar(10), SkIntToScalar(20) }
166 };
167 static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) };
168
169 GradRec rec;
170 rec.fColorCount = SK_ARRAY_COUNT(gColors);
171 rec.fColors = gColors;
172 rec.fPos = gPos;
173 rec.fPoint = gPts;
174 rec.fRadius = gRad;
Mike Reedfae8fce2019-04-03 10:27:45 -0400175 rec.fTileMode = SkTileMode::kClamp;
reed@google.com83226972012-06-07 20:26:47 +0000176
177 static const GradProc gProcs[] = {
178 none_gradproc,
179 color_gradproc,
180 linear_gradproc,
181 radial_gradproc,
reed@google.com83226972012-06-07 20:26:47 +0000182 sweep_gradproc,
183 conical_gradproc,
184 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000185
reed@google.com83226972012-06-07 20:26:47 +0000186 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
fmalita51229672016-08-22 06:22:28 -0700187 gProcs[i](reporter, rec, rec);
188 }
189}
190
191static void TestGradientOptimization(skiatest::Reporter* reporter) {
192 static const struct {
193 GradProc fProc;
194 bool fIsClampRestricted;
195 } gProcInfo[] = {
196 { linear_gradproc , false },
197 { radial_gradproc , false },
198 { sweep_gradproc , true }, // sweep is funky in that it always pretends to be kClamp.
199 { conical_gradproc, false },
200 };
201
202 static const SkColor gC_00[] = { 0xff000000, 0xff000000 };
203 static const SkColor gC_01[] = { 0xff000000, 0xffffffff };
204 static const SkColor gC_11[] = { 0xffffffff, 0xffffffff };
205 static const SkColor gC_001[] = { 0xff000000, 0xff000000, 0xffffffff };
206 static const SkColor gC_011[] = { 0xff000000, 0xffffffff, 0xffffffff };
207 static const SkColor gC_0011[] = { 0xff000000, 0xff000000, 0xffffffff, 0xffffffff };
208
209 static const SkScalar gP_01[] = { 0, 1 };
210 static const SkScalar gP_001[] = { 0, 0, 1 };
211 static const SkScalar gP_011[] = { 0, 1, 1 };
212 static const SkScalar gP_0x1[] = { 0, .5f, 1 };
213 static const SkScalar gP_0011[] = { 0, 0, 1, 1 };
214
215 static const SkPoint gPts[] = { {0, 0}, {1, 1} };
216 static const SkScalar gRadii[] = { 1, 2 };
217
218 static const struct {
219 const SkColor* fCol;
220 const SkScalar* fPos;
221 int fCount;
222
223 const SkColor* fExpectedCol;
224 const SkScalar* fExpectedPos;
225 int fExpectedCount;
226 bool fRequiresNonClamp;
227 } gTests[] = {
228 { gC_001, gP_001, 3, gC_01, gP_01, 2, false },
229 { gC_001, gP_011, 3, gC_00, gP_01, 2, true },
230 { gC_001, gP_0x1, 3, gC_001, gP_0x1, 3, false },
231 { gC_001, nullptr, 3, gC_001, gP_0x1, 3, false },
232
233 { gC_011, gP_001, 3, gC_11, gP_01, 2, true },
234 { gC_011, gP_011, 3, gC_01, gP_01, 2, false },
235 { gC_011, gP_0x1, 3, gC_011, gP_0x1, 3, false },
236 { gC_011, nullptr, 3, gC_011, gP_0x1, 3, false },
237
238 { gC_0011, gP_0011, 4, gC_0011, gP_0011, 4, false },
239 };
240
Mike Reedfae8fce2019-04-03 10:27:45 -0400241 const SkTileMode modes[] = {
242 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror,
Mike Reeddfc0e912018-02-16 12:40:18 -0500243 // TODO: add kDecal_TileMode when it is implemented
244 };
fmalita51229672016-08-22 06:22:28 -0700245 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcInfo); ++i) {
Mike Reeddfc0e912018-02-16 12:40:18 -0500246 for (auto mode : modes) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400247 if (gProcInfo[i].fIsClampRestricted && mode != SkTileMode::kClamp) {
fmalita51229672016-08-22 06:22:28 -0700248 continue;
249 }
250
251 for (size_t t = 0; t < SK_ARRAY_COUNT(gTests); ++t) {
252 GradRec rec;
253 rec.fColorCount = gTests[t].fCount;
254 rec.fColors = gTests[t].fCol;
255 rec.fPos = gTests[t].fPos;
Mike Reedfae8fce2019-04-03 10:27:45 -0400256 rec.fTileMode = mode;
fmalita51229672016-08-22 06:22:28 -0700257 rec.fPoint = gPts;
258 rec.fRadius = gRadii;
259
260 GradRec expected = rec;
Mike Reedfae8fce2019-04-03 10:27:45 -0400261 if (!gTests[t].fRequiresNonClamp || mode != SkTileMode::kClamp) {
fmalita51229672016-08-22 06:22:28 -0700262 expected.fColorCount = gTests[t].fExpectedCount;
263 expected.fColors = gTests[t].fExpectedCol;
264 expected.fPos = gTests[t].fExpectedPos;
265 }
266
267 gProcInfo[i].fProc(reporter, rec, expected);
268 }
269 }
reed@google.com83226972012-06-07 20:26:47 +0000270 }
271}
272
fmalita8d381022015-11-19 10:35:34 -0800273static void test_nearly_vertical(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700274 auto surface(SkSurface::MakeRasterN32Premul(200, 200));
fmalita8d381022015-11-19 10:35:34 -0800275
276 const SkPoint pts[] = {{ 100, 50 }, { 100.0001f, 50000 }};
277 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
278 const SkScalar pos[] = { 0, 1 };
fmalita8d381022015-11-19 10:35:34 -0800279 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400280 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
fmalita8d381022015-11-19 10:35:34 -0800281
282 surface->getCanvas()->drawPaint(paint);
283}
284
James Zern44e91c92016-11-09 19:22:46 -0800285static void test_vertical(skiatest::Reporter* reporter) {
286 auto surface(SkSurface::MakeRasterN32Premul(200, 200));
287
288 const SkPoint pts[] = {{ 100, 50 }, { 100, 50 }};
289 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
290 const SkScalar pos[] = { 0, 1 };
291 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400292 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
James Zern44e91c92016-11-09 19:22:46 -0800293
294 surface->getCanvas()->drawPaint(paint);
295}
296
reedaeab8ea2016-01-05 10:01:38 -0800297// A linear gradient interval can, due to numerical imprecision (likely in the divide)
298// finish an interval with the final fx not landing outside of [p0...p1].
299// The old code had an assert which this test triggered.
300// We now explicitly clamp the resulting fx value.
301static void test_linear_fuzz(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700302 auto surface(SkSurface::MakeRasterN32Premul(1300, 630));
reedaeab8ea2016-01-05 10:01:38 -0800303
304 const SkPoint pts[] = {{ 179.5f, -179.5f }, { 1074.5f, 715.5f }};
305 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE };
306 const SkScalar pos[] = {0, 0.200000003f, 0.800000012f, 1 };
307
reed9283d202016-03-13 13:01:57 -0700308 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400309 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 4, SkTileMode::kClamp));
reed1a9b9642016-03-13 14:13:58 -0700310
reedaeab8ea2016-01-05 10:01:38 -0800311 SkRect r = {0, 83, 1254, 620};
312 surface->getCanvas()->drawRect(r, paint);
313}
314
fmalita5edf82e2016-03-03 06:41:54 -0800315// https://bugs.chromium.org/p/skia/issues/detail?id=5023
316// We should still shade pixels for which the radius is exactly 0.
317static void test_two_point_conical_zero_radius(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700318 auto surface(SkSurface::MakeRasterN32Premul(5, 5));
fmalita5edf82e2016-03-03 06:41:54 -0800319 surface->getCanvas()->clear(SK_ColorRED);
320
321 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
reed1a9b9642016-03-13 14:13:58 -0700322 SkPaint p;
323 p.setShader(SkGradientShader::MakeTwoPointConical(
fmalita5edf82e2016-03-03 06:41:54 -0800324 SkPoint::Make(2.5f, 2.5f), 0,
325 SkPoint::Make(3.0f, 3.0f), 10,
Mike Reedfae8fce2019-04-03 10:27:45 -0400326 colors, nullptr, SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
fmalita5edf82e2016-03-03 06:41:54 -0800327 surface->getCanvas()->drawPaint(p);
328
329 // r == 0 for the center pixel.
330 // verify that we draw it (no red bleed)
331 SkPMColor centerPMColor;
332 surface->readPixels(SkImageInfo::MakeN32Premul(1, 1), &centerPMColor, sizeof(SkPMColor), 2, 2);
333 REPORTER_ASSERT(reporter, SkGetPackedR32(centerPMColor) == 0);
334}
335
fmalita7b38e3c2016-05-26 11:13:52 -0700336// http://crbug.com/599458
337static void test_clamping_overflow(skiatest::Reporter*) {
338 SkPaint p;
339 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
340 const SkPoint pts1[] = { SkPoint::Make(1001, 1000001), SkPoint::Make(1000.99f, 1000000) };
341
Mike Reedfae8fce2019-04-03 10:27:45 -0400342 p.setShader(SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkTileMode::kClamp));
fmalita7b38e3c2016-05-26 11:13:52 -0700343
344 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
345 surface->getCanvas()->scale(100, 100);
346 surface->getCanvas()->drawPaint(p);
347
348 const SkPoint pts2[] = { SkPoint::Make(10000.99f, 1000000), SkPoint::Make(10001, 1000001) };
Mike Reedfae8fce2019-04-03 10:27:45 -0400349 p.setShader(SkGradientShader::MakeLinear(pts2, colors, nullptr, 2, SkTileMode::kClamp));
fmalita7b38e3c2016-05-26 11:13:52 -0700350 surface->getCanvas()->drawPaint(p);
351
352 // Passes if we don't trigger asserts.
353}
354
fmalitac5231042016-08-10 05:45:50 -0700355// http://crbug.com/636194
Florin Malitae659c7f2017-02-09 13:46:55 -0500356static void test_degenerate_linear(skiatest::Reporter*) {
fmalitac5231042016-08-10 05:45:50 -0700357 SkPaint p;
358 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
359 const SkPoint pts[] = {
360 SkPoint::Make(-46058024627067344430605278824628224.0f, 0),
361 SkPoint::Make(SK_ScalarMax, 0)
362 };
363
Mike Reedfae8fce2019-04-03 10:27:45 -0400364 p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
fmalitac5231042016-08-10 05:45:50 -0700365 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
366 surface->getCanvas()->drawPaint(p);
367
368 // Passes if we don't trigger asserts.
369}
370
Florin Malitae659c7f2017-02-09 13:46:55 -0500371// "Interesting" fuzzer values.
372static void test_linear_fuzzer(skiatest::Reporter*) {
373 static const SkColor gColors0[] = { 0x30303030, 0x30303030 };
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500374 static const SkColor gColors1[] = { 0x30303030, 0x30303030, 0x30303030 };
375
376 static const SkScalar gPos1[] = { 0, 0, 1 };
377
Florin Malitad9569662017-02-09 16:41:34 -0500378 static const SkScalar gMatrix0[9] = {
379 6.40969056e-10f, 0 , 6.40969056e-10f,
380 0 , 4.42539023e-39f, 6.40969056e-10f,
381 0 , 0 , 1
382 };
383 static const SkScalar gMatrix1[9] = {
384 -2.75294113f , 6.40969056e-10f, 6.40969056e-10f,
385 6.40969056e-10f, 6.40969056e-10f, -3.32810161e+24f,
386 6.40969056e-10f, 6.40969056e-10f, 0
387 };
388 static const SkScalar gMatrix2[9] = {
389 7.93481258e+17f, 6.40969056e-10f, 6.40969056e-10f,
390 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f,
391 6.40969056e-10f, 6.40969056e-10f, 0.688235283f
392 };
393 static const SkScalar gMatrix3[9] = {
394 1.89180674e+11f, 6.40969056e-10f, 6.40969056e-10f,
395 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f,
396 6.40969056e-10f, 11276.0469f , 8.12524808e+20f
397 };
Florin Malitae659c7f2017-02-09 13:46:55 -0500398
399 static const struct {
400 SkPoint fPts[2];
401 const SkColor* fColors;
402 const SkScalar* fPos;
403 int fCount;
Mike Reedfae8fce2019-04-03 10:27:45 -0400404 SkTileMode fTileMode;
Florin Malitae659c7f2017-02-09 13:46:55 -0500405 uint32_t fFlags;
406 const SkScalar* fLocalMatrix;
407 const SkScalar* fGlobalMatrix;
408 } gConfigs[] = {
409 {
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500410 {{0, -2.752941f}, {0, 0}},
411 gColors0,
412 nullptr,
413 SK_ARRAY_COUNT(gColors0),
Mike Reedfae8fce2019-04-03 10:27:45 -0400414 SkTileMode::kClamp,
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500415 0,
416 gMatrix0,
417 nullptr
418 },
419 {
420 {{4.42539023e-39f, -4.42539023e-39f}, {9.78041162e-15f, 4.42539023e-39f}},
421 gColors1,
422 gPos1,
423 SK_ARRAY_COUNT(gColors1),
Mike Reedfae8fce2019-04-03 10:27:45 -0400424 SkTileMode::kClamp,
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500425 0,
426 nullptr,
427 gMatrix1
Florin Malitae659c7f2017-02-09 13:46:55 -0500428 },
Florin Malitad9569662017-02-09 16:41:34 -0500429 {
430 {{4.42539023e-39f, 6.40969056e-10f}, {6.40969056e-10f, 1.49237238e-19f}},
431 gColors1,
432 gPos1,
433 SK_ARRAY_COUNT(gColors1),
Mike Reedfae8fce2019-04-03 10:27:45 -0400434 SkTileMode::kClamp,
Florin Malitad9569662017-02-09 16:41:34 -0500435 0,
436 nullptr,
437 gMatrix2
438 },
439 {
440 {{6.40969056e-10f, 6.40969056e-10f}, {6.40969056e-10f, -0.688235283f}},
441 gColors0,
442 nullptr,
443 SK_ARRAY_COUNT(gColors0),
Mike Reedfae8fce2019-04-03 10:27:45 -0400444 SkTileMode::kClamp,
Florin Malitad9569662017-02-09 16:41:34 -0500445 0,
446 gMatrix3,
447 nullptr
448 },
Florin Malitae659c7f2017-02-09 13:46:55 -0500449 };
450
Florin Malitad1aedde2017-06-07 15:03:38 -0400451 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
452 SkColorSpace* colorSpaces[] = {
453 nullptr, // hits the legacy gradient impl
454 srgb.get(), // triggers 4f/raster-pipeline
455 };
Florin Malitae659c7f2017-02-09 13:46:55 -0500456
Florin Malitae659c7f2017-02-09 13:46:55 -0500457 SkPaint paint;
458
Florin Malitad1aedde2017-06-07 15:03:38 -0400459 for (auto colorSpace : colorSpaces) {
460
461 sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::Make(100, 100,
462 kN32_SkColorType,
463 kPremul_SkAlphaType,
464 sk_ref_sp(colorSpace)));
465 SkCanvas* canvas = surface->getCanvas();
466
Florin Malitae659c7f2017-02-09 13:46:55 -0500467 for (const auto& config : gConfigs) {
468 SkAutoCanvasRestore acr(canvas, false);
469 SkTLazy<SkMatrix> localMatrix;
470 if (config.fLocalMatrix) {
471 localMatrix.init();
472 localMatrix.get()->set9(config.fLocalMatrix);
473 }
474
475 paint.setShader(SkGradientShader::MakeLinear(config.fPts,
476 config.fColors,
477 config.fPos,
478 config.fCount,
479 config.fTileMode,
Florin Malitad1aedde2017-06-07 15:03:38 -0400480 config.fFlags,
Florin Malitae659c7f2017-02-09 13:46:55 -0500481 localMatrix.getMaybeNull()));
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500482 if (config.fGlobalMatrix) {
483 SkMatrix m;
484 m.set9(config.fGlobalMatrix);
485 canvas->save();
486 canvas->concat(m);
487 }
488
Florin Malitae659c7f2017-02-09 13:46:55 -0500489 canvas->drawPaint(paint);
490 }
491 }
492}
493
Florin Malita03013082017-04-18 13:47:15 -0400494static void test_sweep_fuzzer(skiatest::Reporter*) {
495 static const SkColor gColors0[] = { 0x30303030, 0x30303030, 0x30303030 };
496 static const SkScalar gPos0[] = { -47919293023455565225163489280.0f, 0, 1 };
497 static const SkScalar gMatrix0[9] = {
498 1.12116716e-13f, 0 , 8.50489682e+16f,
499 4.1917041e-41f , 3.51369881e-23f, -2.54344271e-26f,
500 9.61111907e+17f, -3.35263808e-29f, -1.35659403e+14f
501 };
502 static const struct {
503 SkPoint fCenter;
504 const SkColor* fColors;
505 const SkScalar* fPos;
506 int fCount;
507 const SkScalar* fGlobalMatrix;
508 } gConfigs[] = {
509 {
510 { 0, 0 },
511 gColors0,
512 gPos0,
513 SK_ARRAY_COUNT(gColors0),
514 gMatrix0
515 },
516 };
517
518 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
519 SkCanvas* canvas = surface->getCanvas();
520 SkPaint paint;
521
522 for (const auto& config : gConfigs) {
523 paint.setShader(SkGradientShader::MakeSweep(config.fCenter.x(),
524 config.fCenter.y(),
525 config.fColors,
526 config.fPos,
527 config.fCount));
528
529 SkAutoCanvasRestore acr(canvas, false);
530 if (config.fGlobalMatrix) {
531 SkMatrix m;
532 m.set9(config.fGlobalMatrix);
533 canvas->save();
534 canvas->concat(m);
535 }
536 canvas->drawPaint(paint);
537 }
538}
Florin Malitae659c7f2017-02-09 13:46:55 -0500539
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000540DEF_TEST(Gradient, reporter) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000541 TestGradientShaders(reporter);
fmalita51229672016-08-22 06:22:28 -0700542 TestGradientOptimization(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000543 TestConstantGradient(reporter);
reed9d91eb32015-01-28 11:44:48 -0800544 test_big_grad(reporter);
fmalita8d381022015-11-19 10:35:34 -0800545 test_nearly_vertical(reporter);
James Zern44e91c92016-11-09 19:22:46 -0800546 test_vertical(reporter);
reedaeab8ea2016-01-05 10:01:38 -0800547 test_linear_fuzz(reporter);
fmalita5edf82e2016-03-03 06:41:54 -0800548 test_two_point_conical_zero_radius(reporter);
fmalita7b38e3c2016-05-26 11:13:52 -0700549 test_clamping_overflow(reporter);
Florin Malitae659c7f2017-02-09 13:46:55 -0500550 test_degenerate_linear(reporter);
551 test_linear_fuzzer(reporter);
Florin Malita03013082017-04-18 13:47:15 -0400552 test_sweep_fuzzer(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000553}