blob: e43e0a779f2877d4923ad315640489a8a852c014 [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 Reedac9f0c92020-12-23 10:11:33 -05008#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
10#include "include/core/SkColorPriv.h"
11#include "include/core/SkShader.h"
12#include "include/core/SkSurface.h"
13#include "include/effects/SkGradientShader.h"
14#include "include/private/SkTemplates.h"
Michael Ludwigad5ec1a2020-11-19 14:15:05 -050015#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkTLazy.h"
17#include "src/shaders/SkColorShader.h"
18#include "tests/Test.h"
reed@google.com83226972012-06-07 20:26:47 +000019
reed9d91eb32015-01-28 11:44:48 -080020// https://code.google.com/p/chromium/issues/detail?id=448299
21// Giant (inverse) matrix causes overflow when converting/computing using 32.32
22// Before the fix, we would assert (and then crash).
23static void test_big_grad(skiatest::Reporter* reporter) {
24 const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
25 const SkPoint pts[] = {{ 15, 14.7112684f }, { 0.709064007f, 12.6108112f }};
reed9d91eb32015-01-28 11:44:48 -080026 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -040027 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
reed9d91eb32015-01-28 11:44:48 -080028
29 SkBitmap bm;
30 bm.allocN32Pixels(2000, 1);
31 SkCanvas c(bm);
32
33 const SkScalar affine[] = {
34 1.06608627e-06f, 4.26434525e-07f, 6.2855f, 2.6611f, 273.4393f, 244.0046f
35 };
36 SkMatrix matrix;
37 matrix.setAffine(affine);
38 c.concat(matrix);
halcanary9d524f22016-03-29 09:03:52 -070039
reed9d91eb32015-01-28 11:44:48 -080040 c.drawPaint(paint);
41}
42
reed@google.com83226972012-06-07 20:26:47 +000043struct GradRec {
44 int fColorCount;
45 const SkColor* fColors;
46 const SkScalar* fPos;
47 const SkPoint* fPoint; // 2
48 const SkScalar* fRadius; // 2
Mike Reedfae8fce2019-04-03 10:27:45 -040049 SkTileMode fTileMode;
reed@google.com83226972012-06-07 20:26:47 +000050
reed1a9b9642016-03-13 14:13:58 -070051 void gradCheck(skiatest::Reporter* reporter, const sk_sp<SkShader>& shader,
reed@google.com83226972012-06-07 20:26:47 +000052 SkShader::GradientInfo* info,
53 SkShader::GradientType gt) const {
54 SkAutoTMalloc<SkColor> colorStorage(fColorCount);
55 SkAutoTMalloc<SkScalar> posStorage(fColorCount);
56
57 info->fColorCount = fColorCount;
58 info->fColors = colorStorage;
59 info->fColorOffsets = posStorage.get();
60 REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt);
61
62 REPORTER_ASSERT(reporter, info->fColorCount == fColorCount);
63 REPORTER_ASSERT(reporter,
64 !memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor)));
65 REPORTER_ASSERT(reporter,
66 !memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar)));
Mike Reedfae8fce2019-04-03 10:27:45 -040067 REPORTER_ASSERT(reporter, fTileMode == (SkTileMode)info->fTileMode);
reed@google.com83226972012-06-07 20:26:47 +000068 }
69};
70
71
fmalita51229672016-08-22 06:22:28 -070072static void none_gradproc(skiatest::Reporter* reporter, const GradRec&, const GradRec&) {
Mike Reedc8bea7d2019-04-09 13:55:36 -040073 sk_sp<SkShader> s(SkShaders::Empty());
halcanary96fcdcc2015-08-27 07:41:13 -070074 REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(nullptr));
reed@google.com83226972012-06-07 20:26:47 +000075}
76
fmalita51229672016-08-22 06:22:28 -070077static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec, const GradRec&) {
Hal Canary342b7ac2016-11-04 11:49:42 -040078 sk_sp<SkShader> s(new SkColorShader(rec.fColors[0]));
halcanary96fcdcc2015-08-27 07:41:13 -070079 REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(nullptr));
reed@google.com83226972012-06-07 20:26:47 +000080
81 SkShader::GradientInfo info;
halcanary96fcdcc2015-08-27 07:41:13 -070082 info.fColors = nullptr;
reed@google.com83226972012-06-07 20:26:47 +000083 info.fColorCount = 0;
84 s->asAGradient(&info);
85 REPORTER_ASSERT(reporter, 1 == info.fColorCount);
86}
87
fmalita51229672016-08-22 06:22:28 -070088static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
89 const GradRec& checkRec) {
90 sk_sp<SkShader> s(SkGradientShader::MakeLinear(buildRec.fPoint, buildRec.fColors, buildRec.fPos,
91 buildRec.fColorCount, buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +000092
reed@google.com83226972012-06-07 20:26:47 +000093 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -070094 checkRec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType);
95 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint)));
reed@google.com83226972012-06-07 20:26:47 +000096}
97
fmalita51229672016-08-22 06:22:28 -070098static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
99 const GradRec& checkRec) {
100 sk_sp<SkShader> s(SkGradientShader::MakeRadial(buildRec.fPoint[0], buildRec.fRadius[0],
101 buildRec.fColors, buildRec.fPos,
102 buildRec.fColorCount, buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
reed@google.com83226972012-06-07 20:26:47 +0000104 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700105 checkRec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType);
106 REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]);
107 REPORTER_ASSERT(reporter, info.fRadius[0] == checkRec.fRadius[0]);
reed@google.com83226972012-06-07 20:26:47 +0000108}
109
fmalita51229672016-08-22 06:22:28 -0700110static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
111 const GradRec& checkRec) {
112 sk_sp<SkShader> s(SkGradientShader::MakeSweep(buildRec.fPoint[0].fX, buildRec.fPoint[0].fY,
113 buildRec.fColors, buildRec.fPos,
114 buildRec.fColorCount));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
reed@google.com83226972012-06-07 20:26:47 +0000116 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700117 checkRec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType);
118 REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]);
reed@google.com83226972012-06-07 20:26:47 +0000119}
120
fmalita51229672016-08-22 06:22:28 -0700121static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
122 const GradRec& checkRec) {
123 sk_sp<SkShader> s(SkGradientShader::MakeTwoPointConical(buildRec.fPoint[0],
124 buildRec.fRadius[0],
125 buildRec.fPoint[1],
126 buildRec.fRadius[1],
127 buildRec.fColors,
128 buildRec.fPos,
129 buildRec.fColorCount,
130 buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000131
reed@google.com83226972012-06-07 20:26:47 +0000132 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700133 checkRec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType);
134 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint)));
135 REPORTER_ASSERT(reporter, !memcmp(info.fRadius, checkRec.fRadius, 2 * sizeof(SkScalar)));
reed@google.com83226972012-06-07 20:26:47 +0000136}
137
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000138// Ensure that repeated color gradients behave like drawing a single color
sugoi@google.come0e385c2013-03-11 18:50:03 +0000139static void TestConstantGradient(skiatest::Reporter*) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000140 const SkPoint pts[] = {
141 { 0, 0 },
142 { SkIntToScalar(10), 0 }
143 };
144 SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE };
145 const SkScalar pos[] = { 0, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700146 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400147 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000148 SkBitmap outBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000149 outBitmap.allocN32Pixels(10, 1);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000150 SkCanvas canvas(outBitmap);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000151 canvas.drawPaint(paint);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000152 for (int i = 0; i < 10; i++) {
153 // The following is commented out because it currently fails
154 // Related bug: https://code.google.com/p/skia/issues/detail?id=1098
155
156 // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0));
157 }
158}
159
fmalita51229672016-08-22 06:22:28 -0700160typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&, const GradRec&);
reed@google.com83226972012-06-07 20:26:47 +0000161
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000162static void TestGradientShaders(skiatest::Reporter* reporter) {
reed@google.com83226972012-06-07 20:26:47 +0000163 static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
164 static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
165 static const SkPoint gPts[] = {
166 { 0, 0 },
167 { SkIntToScalar(10), SkIntToScalar(20) }
168 };
169 static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) };
170
171 GradRec rec;
172 rec.fColorCount = SK_ARRAY_COUNT(gColors);
173 rec.fColors = gColors;
174 rec.fPos = gPos;
175 rec.fPoint = gPts;
176 rec.fRadius = gRad;
Mike Reedfae8fce2019-04-03 10:27:45 -0400177 rec.fTileMode = SkTileMode::kClamp;
reed@google.com83226972012-06-07 20:26:47 +0000178
179 static const GradProc gProcs[] = {
180 none_gradproc,
181 color_gradproc,
182 linear_gradproc,
183 radial_gradproc,
reed@google.com83226972012-06-07 20:26:47 +0000184 sweep_gradproc,
185 conical_gradproc,
186 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
reed@google.com83226972012-06-07 20:26:47 +0000188 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
fmalita51229672016-08-22 06:22:28 -0700189 gProcs[i](reporter, rec, rec);
190 }
191}
192
193static void TestGradientOptimization(skiatest::Reporter* reporter) {
194 static const struct {
195 GradProc fProc;
196 bool fIsClampRestricted;
197 } gProcInfo[] = {
198 { linear_gradproc , false },
199 { radial_gradproc , false },
200 { sweep_gradproc , true }, // sweep is funky in that it always pretends to be kClamp.
201 { conical_gradproc, false },
202 };
203
204 static const SkColor gC_00[] = { 0xff000000, 0xff000000 };
205 static const SkColor gC_01[] = { 0xff000000, 0xffffffff };
206 static const SkColor gC_11[] = { 0xffffffff, 0xffffffff };
207 static const SkColor gC_001[] = { 0xff000000, 0xff000000, 0xffffffff };
208 static const SkColor gC_011[] = { 0xff000000, 0xffffffff, 0xffffffff };
209 static const SkColor gC_0011[] = { 0xff000000, 0xff000000, 0xffffffff, 0xffffffff };
210
211 static const SkScalar gP_01[] = { 0, 1 };
212 static const SkScalar gP_001[] = { 0, 0, 1 };
213 static const SkScalar gP_011[] = { 0, 1, 1 };
214 static const SkScalar gP_0x1[] = { 0, .5f, 1 };
215 static const SkScalar gP_0011[] = { 0, 0, 1, 1 };
216
217 static const SkPoint gPts[] = { {0, 0}, {1, 1} };
218 static const SkScalar gRadii[] = { 1, 2 };
219
220 static const struct {
221 const SkColor* fCol;
222 const SkScalar* fPos;
223 int fCount;
224
225 const SkColor* fExpectedCol;
226 const SkScalar* fExpectedPos;
227 int fExpectedCount;
228 bool fRequiresNonClamp;
229 } gTests[] = {
230 { gC_001, gP_001, 3, gC_01, gP_01, 2, false },
231 { gC_001, gP_011, 3, gC_00, gP_01, 2, true },
232 { gC_001, gP_0x1, 3, gC_001, gP_0x1, 3, false },
233 { gC_001, nullptr, 3, gC_001, gP_0x1, 3, false },
234
235 { gC_011, gP_001, 3, gC_11, gP_01, 2, true },
236 { gC_011, gP_011, 3, gC_01, gP_01, 2, false },
237 { gC_011, gP_0x1, 3, gC_011, gP_0x1, 3, false },
238 { gC_011, nullptr, 3, gC_011, gP_0x1, 3, false },
239
240 { gC_0011, gP_0011, 4, gC_0011, gP_0011, 4, false },
241 };
242
Mike Reedfae8fce2019-04-03 10:27:45 -0400243 const SkTileMode modes[] = {
244 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror,
Mike Reeddfc0e912018-02-16 12:40:18 -0500245 // TODO: add kDecal_TileMode when it is implemented
246 };
fmalita51229672016-08-22 06:22:28 -0700247 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcInfo); ++i) {
Mike Reeddfc0e912018-02-16 12:40:18 -0500248 for (auto mode : modes) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400249 if (gProcInfo[i].fIsClampRestricted && mode != SkTileMode::kClamp) {
fmalita51229672016-08-22 06:22:28 -0700250 continue;
251 }
252
253 for (size_t t = 0; t < SK_ARRAY_COUNT(gTests); ++t) {
254 GradRec rec;
255 rec.fColorCount = gTests[t].fCount;
256 rec.fColors = gTests[t].fCol;
257 rec.fPos = gTests[t].fPos;
Mike Reedfae8fce2019-04-03 10:27:45 -0400258 rec.fTileMode = mode;
fmalita51229672016-08-22 06:22:28 -0700259 rec.fPoint = gPts;
260 rec.fRadius = gRadii;
261
262 GradRec expected = rec;
Mike Reedfae8fce2019-04-03 10:27:45 -0400263 if (!gTests[t].fRequiresNonClamp || mode != SkTileMode::kClamp) {
fmalita51229672016-08-22 06:22:28 -0700264 expected.fColorCount = gTests[t].fExpectedCount;
265 expected.fColors = gTests[t].fExpectedCol;
266 expected.fPos = gTests[t].fExpectedPos;
267 }
268
269 gProcInfo[i].fProc(reporter, rec, expected);
270 }
271 }
reed@google.com83226972012-06-07 20:26:47 +0000272 }
273}
274
fmalita8d381022015-11-19 10:35:34 -0800275static void test_nearly_vertical(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700276 auto surface(SkSurface::MakeRasterN32Premul(200, 200));
fmalita8d381022015-11-19 10:35:34 -0800277
278 const SkPoint pts[] = {{ 100, 50 }, { 100.0001f, 50000 }};
279 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
280 const SkScalar pos[] = { 0, 1 };
fmalita8d381022015-11-19 10:35:34 -0800281 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400282 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
fmalita8d381022015-11-19 10:35:34 -0800283
284 surface->getCanvas()->drawPaint(paint);
285}
286
James Zern44e91c92016-11-09 19:22:46 -0800287static void test_vertical(skiatest::Reporter* reporter) {
288 auto surface(SkSurface::MakeRasterN32Premul(200, 200));
289
290 const SkPoint pts[] = {{ 100, 50 }, { 100, 50 }};
291 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
292 const SkScalar pos[] = { 0, 1 };
293 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400294 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
James Zern44e91c92016-11-09 19:22:46 -0800295
296 surface->getCanvas()->drawPaint(paint);
297}
298
reedaeab8ea2016-01-05 10:01:38 -0800299// A linear gradient interval can, due to numerical imprecision (likely in the divide)
300// finish an interval with the final fx not landing outside of [p0...p1].
301// The old code had an assert which this test triggered.
302// We now explicitly clamp the resulting fx value.
303static void test_linear_fuzz(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700304 auto surface(SkSurface::MakeRasterN32Premul(1300, 630));
reedaeab8ea2016-01-05 10:01:38 -0800305
306 const SkPoint pts[] = {{ 179.5f, -179.5f }, { 1074.5f, 715.5f }};
307 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE };
308 const SkScalar pos[] = {0, 0.200000003f, 0.800000012f, 1 };
309
reed9283d202016-03-13 13:01:57 -0700310 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400311 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 4, SkTileMode::kClamp));
reed1a9b9642016-03-13 14:13:58 -0700312
reedaeab8ea2016-01-05 10:01:38 -0800313 SkRect r = {0, 83, 1254, 620};
314 surface->getCanvas()->drawRect(r, paint);
315}
316
fmalita5edf82e2016-03-03 06:41:54 -0800317// https://bugs.chromium.org/p/skia/issues/detail?id=5023
318// We should still shade pixels for which the radius is exactly 0.
319static void test_two_point_conical_zero_radius(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700320 auto surface(SkSurface::MakeRasterN32Premul(5, 5));
fmalita5edf82e2016-03-03 06:41:54 -0800321 surface->getCanvas()->clear(SK_ColorRED);
322
323 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
reed1a9b9642016-03-13 14:13:58 -0700324 SkPaint p;
325 p.setShader(SkGradientShader::MakeTwoPointConical(
fmalita5edf82e2016-03-03 06:41:54 -0800326 SkPoint::Make(2.5f, 2.5f), 0,
327 SkPoint::Make(3.0f, 3.0f), 10,
Mike Reedfae8fce2019-04-03 10:27:45 -0400328 colors, nullptr, SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
fmalita5edf82e2016-03-03 06:41:54 -0800329 surface->getCanvas()->drawPaint(p);
330
331 // r == 0 for the center pixel.
332 // verify that we draw it (no red bleed)
333 SkPMColor centerPMColor;
334 surface->readPixels(SkImageInfo::MakeN32Premul(1, 1), &centerPMColor, sizeof(SkPMColor), 2, 2);
335 REPORTER_ASSERT(reporter, SkGetPackedR32(centerPMColor) == 0);
336}
337
fmalita7b38e3c2016-05-26 11:13:52 -0700338// http://crbug.com/599458
339static void test_clamping_overflow(skiatest::Reporter*) {
340 SkPaint p;
341 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
342 const SkPoint pts1[] = { SkPoint::Make(1001, 1000001), SkPoint::Make(1000.99f, 1000000) };
343
Mike Reedfae8fce2019-04-03 10:27:45 -0400344 p.setShader(SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkTileMode::kClamp));
fmalita7b38e3c2016-05-26 11:13:52 -0700345
346 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
347 surface->getCanvas()->scale(100, 100);
348 surface->getCanvas()->drawPaint(p);
349
350 const SkPoint pts2[] = { SkPoint::Make(10000.99f, 1000000), SkPoint::Make(10001, 1000001) };
Mike Reedfae8fce2019-04-03 10:27:45 -0400351 p.setShader(SkGradientShader::MakeLinear(pts2, colors, nullptr, 2, SkTileMode::kClamp));
fmalita7b38e3c2016-05-26 11:13:52 -0700352 surface->getCanvas()->drawPaint(p);
353
354 // Passes if we don't trigger asserts.
355}
356
fmalitac5231042016-08-10 05:45:50 -0700357// http://crbug.com/636194
Florin Malitae659c7f2017-02-09 13:46:55 -0500358static void test_degenerate_linear(skiatest::Reporter*) {
fmalitac5231042016-08-10 05:45:50 -0700359 SkPaint p;
360 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
361 const SkPoint pts[] = {
362 SkPoint::Make(-46058024627067344430605278824628224.0f, 0),
363 SkPoint::Make(SK_ScalarMax, 0)
364 };
365
Mike Reedfae8fce2019-04-03 10:27:45 -0400366 p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
fmalitac5231042016-08-10 05:45:50 -0700367 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
368 surface->getCanvas()->drawPaint(p);
369
370 // Passes if we don't trigger asserts.
371}
372
Michael Ludwigad5ec1a2020-11-19 14:15:05 -0500373// http://crbug.com/1149216
374static void test_unsorted_degenerate(skiatest::Reporter* r) {
375 // Passes if a valid solid color is computed for the degenerate gradient
376 // (unsorted positions are fixed during regular gradient construction, so this ensures the
377 // same fixing happens for degenerate gradients as well). If they aren't fixed, this test
378 // case produces a negative alpha, which asserts during SkPMColor4f::isOpaque().
379 const SkColor4f colors[] = { {0.f, 0.f, 0.f, 0.f},
380 {0.00784314f, 0.f, 0.f, 0.0627451f},
381 {0.f, 0.00392157f, 0.f, 0.f} };
382 const SkScalar positions[] = {0.00753367f, 8.54792e-44f, 1.46955e-39f};
383
384 const SkPoint points[] { { 0.f, 0.f }, { 1e-20f, -1e-8f }}; // must be degenerate
385 // Use kMirror to go through average color stop calculation, vs. kClamp which would pick a color
386 sk_sp<SkShader> gradient = SkGradientShader::MakeLinear(points, colors, nullptr, positions, 3,
387 SkTileMode::kMirror);
388
389 // The degenerate gradient shouldn't be null
390 REPORTER_ASSERT(r, SkToBool(gradient));
391 // And it shouldn't crash when creating a fragment processor
392
393 SkSimpleMatrixProvider provider(SkMatrix::I());
394 GrColorInfo dstColorInfo(GrColorType::kRGBA_8888, kPremul_SkAlphaType,
395 SkColorSpace::MakeSRGB());
396 GrMockOptions options;
397 auto context = GrDirectContext::MakeMock(&options);
398
Mike Reed12a75582021-03-20 10:49:02 -0400399 GrFPArgs args(context.get(), provider, &dstColorInfo);
Michael Ludwigad5ec1a2020-11-19 14:15:05 -0500400 as_SB(gradient)->asFragmentProcessor(args);
401}
402
Florin Malitae659c7f2017-02-09 13:46:55 -0500403// "Interesting" fuzzer values.
404static void test_linear_fuzzer(skiatest::Reporter*) {
405 static const SkColor gColors0[] = { 0x30303030, 0x30303030 };
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500406 static const SkColor gColors1[] = { 0x30303030, 0x30303030, 0x30303030 };
407
408 static const SkScalar gPos1[] = { 0, 0, 1 };
409
Florin Malitad9569662017-02-09 16:41:34 -0500410 static const SkScalar gMatrix0[9] = {
411 6.40969056e-10f, 0 , 6.40969056e-10f,
412 0 , 4.42539023e-39f, 6.40969056e-10f,
413 0 , 0 , 1
414 };
415 static const SkScalar gMatrix1[9] = {
416 -2.75294113f , 6.40969056e-10f, 6.40969056e-10f,
417 6.40969056e-10f, 6.40969056e-10f, -3.32810161e+24f,
418 6.40969056e-10f, 6.40969056e-10f, 0
419 };
420 static const SkScalar gMatrix2[9] = {
421 7.93481258e+17f, 6.40969056e-10f, 6.40969056e-10f,
422 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f,
423 6.40969056e-10f, 6.40969056e-10f, 0.688235283f
424 };
425 static const SkScalar gMatrix3[9] = {
426 1.89180674e+11f, 6.40969056e-10f, 6.40969056e-10f,
427 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f,
428 6.40969056e-10f, 11276.0469f , 8.12524808e+20f
429 };
Florin Malitae659c7f2017-02-09 13:46:55 -0500430
431 static const struct {
432 SkPoint fPts[2];
433 const SkColor* fColors;
434 const SkScalar* fPos;
435 int fCount;
Mike Reedfae8fce2019-04-03 10:27:45 -0400436 SkTileMode fTileMode;
Florin Malitae659c7f2017-02-09 13:46:55 -0500437 uint32_t fFlags;
438 const SkScalar* fLocalMatrix;
439 const SkScalar* fGlobalMatrix;
440 } gConfigs[] = {
441 {
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500442 {{0, -2.752941f}, {0, 0}},
443 gColors0,
444 nullptr,
445 SK_ARRAY_COUNT(gColors0),
Mike Reedfae8fce2019-04-03 10:27:45 -0400446 SkTileMode::kClamp,
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500447 0,
448 gMatrix0,
449 nullptr
450 },
451 {
452 {{4.42539023e-39f, -4.42539023e-39f}, {9.78041162e-15f, 4.42539023e-39f}},
453 gColors1,
454 gPos1,
455 SK_ARRAY_COUNT(gColors1),
Mike Reedfae8fce2019-04-03 10:27:45 -0400456 SkTileMode::kClamp,
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500457 0,
458 nullptr,
459 gMatrix1
Florin Malitae659c7f2017-02-09 13:46:55 -0500460 },
Florin Malitad9569662017-02-09 16:41:34 -0500461 {
462 {{4.42539023e-39f, 6.40969056e-10f}, {6.40969056e-10f, 1.49237238e-19f}},
463 gColors1,
464 gPos1,
465 SK_ARRAY_COUNT(gColors1),
Mike Reedfae8fce2019-04-03 10:27:45 -0400466 SkTileMode::kClamp,
Florin Malitad9569662017-02-09 16:41:34 -0500467 0,
468 nullptr,
469 gMatrix2
470 },
471 {
472 {{6.40969056e-10f, 6.40969056e-10f}, {6.40969056e-10f, -0.688235283f}},
473 gColors0,
474 nullptr,
475 SK_ARRAY_COUNT(gColors0),
Mike Reedfae8fce2019-04-03 10:27:45 -0400476 SkTileMode::kClamp,
Florin Malitad9569662017-02-09 16:41:34 -0500477 0,
478 gMatrix3,
479 nullptr
480 },
Florin Malitae659c7f2017-02-09 13:46:55 -0500481 };
482
Florin Malitad1aedde2017-06-07 15:03:38 -0400483 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
484 SkColorSpace* colorSpaces[] = {
485 nullptr, // hits the legacy gradient impl
486 srgb.get(), // triggers 4f/raster-pipeline
487 };
Florin Malitae659c7f2017-02-09 13:46:55 -0500488
Florin Malitae659c7f2017-02-09 13:46:55 -0500489 SkPaint paint;
490
John Stilesbd3ffa42020-07-30 20:24:57 -0400491 for (const SkColorSpace* colorSpace : colorSpaces) {
Florin Malitad1aedde2017-06-07 15:03:38 -0400492
493 sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::Make(100, 100,
494 kN32_SkColorType,
495 kPremul_SkAlphaType,
496 sk_ref_sp(colorSpace)));
497 SkCanvas* canvas = surface->getCanvas();
498
Florin Malitae659c7f2017-02-09 13:46:55 -0500499 for (const auto& config : gConfigs) {
500 SkAutoCanvasRestore acr(canvas, false);
501 SkTLazy<SkMatrix> localMatrix;
502 if (config.fLocalMatrix) {
503 localMatrix.init();
John Stilesa008b0f2020-08-16 08:48:02 -0400504 localMatrix->set9(config.fLocalMatrix);
Florin Malitae659c7f2017-02-09 13:46:55 -0500505 }
506
507 paint.setShader(SkGradientShader::MakeLinear(config.fPts,
508 config.fColors,
509 config.fPos,
510 config.fCount,
511 config.fTileMode,
Florin Malitad1aedde2017-06-07 15:03:38 -0400512 config.fFlags,
Florin Malitae659c7f2017-02-09 13:46:55 -0500513 localMatrix.getMaybeNull()));
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500514 if (config.fGlobalMatrix) {
515 SkMatrix m;
516 m.set9(config.fGlobalMatrix);
517 canvas->save();
518 canvas->concat(m);
519 }
520
Florin Malitae659c7f2017-02-09 13:46:55 -0500521 canvas->drawPaint(paint);
522 }
523 }
524}
525
Florin Malita03013082017-04-18 13:47:15 -0400526static void test_sweep_fuzzer(skiatest::Reporter*) {
527 static const SkColor gColors0[] = { 0x30303030, 0x30303030, 0x30303030 };
528 static const SkScalar gPos0[] = { -47919293023455565225163489280.0f, 0, 1 };
529 static const SkScalar gMatrix0[9] = {
530 1.12116716e-13f, 0 , 8.50489682e+16f,
531 4.1917041e-41f , 3.51369881e-23f, -2.54344271e-26f,
532 9.61111907e+17f, -3.35263808e-29f, -1.35659403e+14f
533 };
534 static const struct {
535 SkPoint fCenter;
536 const SkColor* fColors;
537 const SkScalar* fPos;
538 int fCount;
539 const SkScalar* fGlobalMatrix;
540 } gConfigs[] = {
541 {
542 { 0, 0 },
543 gColors0,
544 gPos0,
545 SK_ARRAY_COUNT(gColors0),
546 gMatrix0
547 },
548 };
549
550 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
551 SkCanvas* canvas = surface->getCanvas();
552 SkPaint paint;
553
554 for (const auto& config : gConfigs) {
555 paint.setShader(SkGradientShader::MakeSweep(config.fCenter.x(),
556 config.fCenter.y(),
557 config.fColors,
558 config.fPos,
559 config.fCount));
560
561 SkAutoCanvasRestore acr(canvas, false);
562 if (config.fGlobalMatrix) {
563 SkMatrix m;
564 m.set9(config.fGlobalMatrix);
565 canvas->save();
566 canvas->concat(m);
567 }
568 canvas->drawPaint(paint);
569 }
570}
Florin Malitae659c7f2017-02-09 13:46:55 -0500571
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000572DEF_TEST(Gradient, reporter) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000573 TestGradientShaders(reporter);
fmalita51229672016-08-22 06:22:28 -0700574 TestGradientOptimization(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000575 TestConstantGradient(reporter);
reed9d91eb32015-01-28 11:44:48 -0800576 test_big_grad(reporter);
fmalita8d381022015-11-19 10:35:34 -0800577 test_nearly_vertical(reporter);
James Zern44e91c92016-11-09 19:22:46 -0800578 test_vertical(reporter);
reedaeab8ea2016-01-05 10:01:38 -0800579 test_linear_fuzz(reporter);
fmalita5edf82e2016-03-03 06:41:54 -0800580 test_two_point_conical_zero_radius(reporter);
fmalita7b38e3c2016-05-26 11:13:52 -0700581 test_clamping_overflow(reporter);
Florin Malitae659c7f2017-02-09 13:46:55 -0500582 test_degenerate_linear(reporter);
583 test_linear_fuzzer(reporter);
Florin Malita03013082017-04-18 13:47:15 -0400584 test_sweep_fuzzer(reporter);
Michael Ludwigad5ec1a2020-11-19 14:15:05 -0500585 test_unsorted_degenerate(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000586}