blob: cf1bcfc9efaba94cca53ec52343314fc175fc26c [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"
Brian Salomon5392c942021-03-30 16:14:37 -040017#include "src/gpu/GrColorInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/shaders/SkColorShader.h"
19#include "tests/Test.h"
reed@google.com83226972012-06-07 20:26:47 +000020
reed9d91eb32015-01-28 11:44:48 -080021// https://code.google.com/p/chromium/issues/detail?id=448299
22// Giant (inverse) matrix causes overflow when converting/computing using 32.32
23// Before the fix, we would assert (and then crash).
24static void test_big_grad(skiatest::Reporter* reporter) {
25 const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
26 const SkPoint pts[] = {{ 15, 14.7112684f }, { 0.709064007f, 12.6108112f }};
reed9d91eb32015-01-28 11:44:48 -080027 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -040028 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
reed9d91eb32015-01-28 11:44:48 -080029
30 SkBitmap bm;
31 bm.allocN32Pixels(2000, 1);
32 SkCanvas c(bm);
33
34 const SkScalar affine[] = {
35 1.06608627e-06f, 4.26434525e-07f, 6.2855f, 2.6611f, 273.4393f, 244.0046f
36 };
37 SkMatrix matrix;
38 matrix.setAffine(affine);
39 c.concat(matrix);
halcanary9d524f22016-03-29 09:03:52 -070040
reed9d91eb32015-01-28 11:44:48 -080041 c.drawPaint(paint);
42}
43
reed@google.com83226972012-06-07 20:26:47 +000044struct GradRec {
45 int fColorCount;
46 const SkColor* fColors;
47 const SkScalar* fPos;
48 const SkPoint* fPoint; // 2
49 const SkScalar* fRadius; // 2
Mike Reedfae8fce2019-04-03 10:27:45 -040050 SkTileMode fTileMode;
reed@google.com83226972012-06-07 20:26:47 +000051
reed1a9b9642016-03-13 14:13:58 -070052 void gradCheck(skiatest::Reporter* reporter, const sk_sp<SkShader>& shader,
reed@google.com83226972012-06-07 20:26:47 +000053 SkShader::GradientInfo* info,
54 SkShader::GradientType gt) const {
55 SkAutoTMalloc<SkColor> colorStorage(fColorCount);
56 SkAutoTMalloc<SkScalar> posStorage(fColorCount);
57
58 info->fColorCount = fColorCount;
59 info->fColors = colorStorage;
60 info->fColorOffsets = posStorage.get();
61 REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt);
62
63 REPORTER_ASSERT(reporter, info->fColorCount == fColorCount);
64 REPORTER_ASSERT(reporter,
65 !memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor)));
66 REPORTER_ASSERT(reporter,
67 !memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar)));
Mike Reedfae8fce2019-04-03 10:27:45 -040068 REPORTER_ASSERT(reporter, fTileMode == (SkTileMode)info->fTileMode);
reed@google.com83226972012-06-07 20:26:47 +000069 }
70};
71
72
fmalita51229672016-08-22 06:22:28 -070073static void none_gradproc(skiatest::Reporter* reporter, const GradRec&, const GradRec&) {
Mike Reedc8bea7d2019-04-09 13:55:36 -040074 sk_sp<SkShader> s(SkShaders::Empty());
halcanary96fcdcc2015-08-27 07:41:13 -070075 REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(nullptr));
reed@google.com83226972012-06-07 20:26:47 +000076}
77
fmalita51229672016-08-22 06:22:28 -070078static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec, const GradRec&) {
Hal Canary342b7ac2016-11-04 11:49:42 -040079 sk_sp<SkShader> s(new SkColorShader(rec.fColors[0]));
halcanary96fcdcc2015-08-27 07:41:13 -070080 REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(nullptr));
reed@google.com83226972012-06-07 20:26:47 +000081
82 SkShader::GradientInfo info;
halcanary96fcdcc2015-08-27 07:41:13 -070083 info.fColors = nullptr;
reed@google.com83226972012-06-07 20:26:47 +000084 info.fColorCount = 0;
85 s->asAGradient(&info);
86 REPORTER_ASSERT(reporter, 1 == info.fColorCount);
87}
88
fmalita51229672016-08-22 06:22:28 -070089static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
90 const GradRec& checkRec) {
91 sk_sp<SkShader> s(SkGradientShader::MakeLinear(buildRec.fPoint, buildRec.fColors, buildRec.fPos,
92 buildRec.fColorCount, buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +000093
reed@google.com83226972012-06-07 20:26:47 +000094 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -070095 checkRec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType);
96 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint)));
reed@google.com83226972012-06-07 20:26:47 +000097}
98
fmalita51229672016-08-22 06:22:28 -070099static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
100 const GradRec& checkRec) {
101 sk_sp<SkShader> s(SkGradientShader::MakeRadial(buildRec.fPoint[0], buildRec.fRadius[0],
102 buildRec.fColors, buildRec.fPos,
103 buildRec.fColorCount, buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104
reed@google.com83226972012-06-07 20:26:47 +0000105 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700106 checkRec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType);
107 REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]);
108 REPORTER_ASSERT(reporter, info.fRadius[0] == checkRec.fRadius[0]);
reed@google.com83226972012-06-07 20:26:47 +0000109}
110
fmalita51229672016-08-22 06:22:28 -0700111static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
112 const GradRec& checkRec) {
113 sk_sp<SkShader> s(SkGradientShader::MakeSweep(buildRec.fPoint[0].fX, buildRec.fPoint[0].fY,
114 buildRec.fColors, buildRec.fPos,
115 buildRec.fColorCount));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
reed@google.com83226972012-06-07 20:26:47 +0000117 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700118 checkRec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType);
119 REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]);
reed@google.com83226972012-06-07 20:26:47 +0000120}
121
fmalita51229672016-08-22 06:22:28 -0700122static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
123 const GradRec& checkRec) {
124 sk_sp<SkShader> s(SkGradientShader::MakeTwoPointConical(buildRec.fPoint[0],
125 buildRec.fRadius[0],
126 buildRec.fPoint[1],
127 buildRec.fRadius[1],
128 buildRec.fColors,
129 buildRec.fPos,
130 buildRec.fColorCount,
131 buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
reed@google.com83226972012-06-07 20:26:47 +0000133 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700134 checkRec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType);
135 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint)));
136 REPORTER_ASSERT(reporter, !memcmp(info.fRadius, checkRec.fRadius, 2 * sizeof(SkScalar)));
reed@google.com83226972012-06-07 20:26:47 +0000137}
138
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000139// Ensure that repeated color gradients behave like drawing a single color
sugoi@google.come0e385c2013-03-11 18:50:03 +0000140static void TestConstantGradient(skiatest::Reporter*) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000141 const SkPoint pts[] = {
142 { 0, 0 },
143 { SkIntToScalar(10), 0 }
144 };
145 SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE };
146 const SkScalar pos[] = { 0, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700147 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400148 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000149 SkBitmap outBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000150 outBitmap.allocN32Pixels(10, 1);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000151 SkCanvas canvas(outBitmap);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000152 canvas.drawPaint(paint);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000153 for (int i = 0; i < 10; i++) {
154 // The following is commented out because it currently fails
155 // Related bug: https://code.google.com/p/skia/issues/detail?id=1098
156
157 // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0));
158 }
159}
160
fmalita51229672016-08-22 06:22:28 -0700161typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&, const GradRec&);
reed@google.com83226972012-06-07 20:26:47 +0000162
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000163static void TestGradientShaders(skiatest::Reporter* reporter) {
reed@google.com83226972012-06-07 20:26:47 +0000164 static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
165 static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
166 static const SkPoint gPts[] = {
167 { 0, 0 },
168 { SkIntToScalar(10), SkIntToScalar(20) }
169 };
170 static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) };
171
172 GradRec rec;
173 rec.fColorCount = SK_ARRAY_COUNT(gColors);
174 rec.fColors = gColors;
175 rec.fPos = gPos;
176 rec.fPoint = gPts;
177 rec.fRadius = gRad;
Mike Reedfae8fce2019-04-03 10:27:45 -0400178 rec.fTileMode = SkTileMode::kClamp;
reed@google.com83226972012-06-07 20:26:47 +0000179
180 static const GradProc gProcs[] = {
181 none_gradproc,
182 color_gradproc,
183 linear_gradproc,
184 radial_gradproc,
reed@google.com83226972012-06-07 20:26:47 +0000185 sweep_gradproc,
186 conical_gradproc,
187 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188
reed@google.com83226972012-06-07 20:26:47 +0000189 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
fmalita51229672016-08-22 06:22:28 -0700190 gProcs[i](reporter, rec, rec);
191 }
192}
193
194static void TestGradientOptimization(skiatest::Reporter* reporter) {
195 static const struct {
196 GradProc fProc;
197 bool fIsClampRestricted;
198 } gProcInfo[] = {
199 { linear_gradproc , false },
200 { radial_gradproc , false },
201 { sweep_gradproc , true }, // sweep is funky in that it always pretends to be kClamp.
202 { conical_gradproc, false },
203 };
204
205 static const SkColor gC_00[] = { 0xff000000, 0xff000000 };
206 static const SkColor gC_01[] = { 0xff000000, 0xffffffff };
207 static const SkColor gC_11[] = { 0xffffffff, 0xffffffff };
208 static const SkColor gC_001[] = { 0xff000000, 0xff000000, 0xffffffff };
209 static const SkColor gC_011[] = { 0xff000000, 0xffffffff, 0xffffffff };
210 static const SkColor gC_0011[] = { 0xff000000, 0xff000000, 0xffffffff, 0xffffffff };
211
212 static const SkScalar gP_01[] = { 0, 1 };
213 static const SkScalar gP_001[] = { 0, 0, 1 };
214 static const SkScalar gP_011[] = { 0, 1, 1 };
215 static const SkScalar gP_0x1[] = { 0, .5f, 1 };
216 static const SkScalar gP_0011[] = { 0, 0, 1, 1 };
217
218 static const SkPoint gPts[] = { {0, 0}, {1, 1} };
219 static const SkScalar gRadii[] = { 1, 2 };
220
221 static const struct {
222 const SkColor* fCol;
223 const SkScalar* fPos;
224 int fCount;
225
226 const SkColor* fExpectedCol;
227 const SkScalar* fExpectedPos;
228 int fExpectedCount;
229 bool fRequiresNonClamp;
230 } gTests[] = {
231 { gC_001, gP_001, 3, gC_01, gP_01, 2, false },
232 { gC_001, gP_011, 3, gC_00, gP_01, 2, true },
233 { gC_001, gP_0x1, 3, gC_001, gP_0x1, 3, false },
234 { gC_001, nullptr, 3, gC_001, gP_0x1, 3, false },
235
236 { gC_011, gP_001, 3, gC_11, gP_01, 2, true },
237 { gC_011, gP_011, 3, gC_01, gP_01, 2, false },
238 { gC_011, gP_0x1, 3, gC_011, gP_0x1, 3, false },
239 { gC_011, nullptr, 3, gC_011, gP_0x1, 3, false },
240
241 { gC_0011, gP_0011, 4, gC_0011, gP_0011, 4, false },
242 };
243
Mike Reedfae8fce2019-04-03 10:27:45 -0400244 const SkTileMode modes[] = {
245 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror,
Mike Reeddfc0e912018-02-16 12:40:18 -0500246 // TODO: add kDecal_TileMode when it is implemented
247 };
fmalita51229672016-08-22 06:22:28 -0700248 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcInfo); ++i) {
Mike Reeddfc0e912018-02-16 12:40:18 -0500249 for (auto mode : modes) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400250 if (gProcInfo[i].fIsClampRestricted && mode != SkTileMode::kClamp) {
fmalita51229672016-08-22 06:22:28 -0700251 continue;
252 }
253
254 for (size_t t = 0; t < SK_ARRAY_COUNT(gTests); ++t) {
255 GradRec rec;
256 rec.fColorCount = gTests[t].fCount;
257 rec.fColors = gTests[t].fCol;
258 rec.fPos = gTests[t].fPos;
Mike Reedfae8fce2019-04-03 10:27:45 -0400259 rec.fTileMode = mode;
fmalita51229672016-08-22 06:22:28 -0700260 rec.fPoint = gPts;
261 rec.fRadius = gRadii;
262
263 GradRec expected = rec;
Mike Reedfae8fce2019-04-03 10:27:45 -0400264 if (!gTests[t].fRequiresNonClamp || mode != SkTileMode::kClamp) {
fmalita51229672016-08-22 06:22:28 -0700265 expected.fColorCount = gTests[t].fExpectedCount;
266 expected.fColors = gTests[t].fExpectedCol;
267 expected.fPos = gTests[t].fExpectedPos;
268 }
269
270 gProcInfo[i].fProc(reporter, rec, expected);
271 }
272 }
reed@google.com83226972012-06-07 20:26:47 +0000273 }
274}
275
fmalita8d381022015-11-19 10:35:34 -0800276static void test_nearly_vertical(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700277 auto surface(SkSurface::MakeRasterN32Premul(200, 200));
fmalita8d381022015-11-19 10:35:34 -0800278
279 const SkPoint pts[] = {{ 100, 50 }, { 100.0001f, 50000 }};
280 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
281 const SkScalar pos[] = { 0, 1 };
fmalita8d381022015-11-19 10:35:34 -0800282 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400283 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
fmalita8d381022015-11-19 10:35:34 -0800284
285 surface->getCanvas()->drawPaint(paint);
286}
287
James Zern44e91c92016-11-09 19:22:46 -0800288static void test_vertical(skiatest::Reporter* reporter) {
289 auto surface(SkSurface::MakeRasterN32Premul(200, 200));
290
291 const SkPoint pts[] = {{ 100, 50 }, { 100, 50 }};
292 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
293 const SkScalar pos[] = { 0, 1 };
294 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400295 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkTileMode::kClamp));
James Zern44e91c92016-11-09 19:22:46 -0800296
297 surface->getCanvas()->drawPaint(paint);
298}
299
reedaeab8ea2016-01-05 10:01:38 -0800300// A linear gradient interval can, due to numerical imprecision (likely in the divide)
301// finish an interval with the final fx not landing outside of [p0...p1].
302// The old code had an assert which this test triggered.
303// We now explicitly clamp the resulting fx value.
304static void test_linear_fuzz(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700305 auto surface(SkSurface::MakeRasterN32Premul(1300, 630));
reedaeab8ea2016-01-05 10:01:38 -0800306
307 const SkPoint pts[] = {{ 179.5f, -179.5f }, { 1074.5f, 715.5f }};
308 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE };
309 const SkScalar pos[] = {0, 0.200000003f, 0.800000012f, 1 };
310
reed9283d202016-03-13 13:01:57 -0700311 SkPaint paint;
Mike Reedfae8fce2019-04-03 10:27:45 -0400312 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 4, SkTileMode::kClamp));
reed1a9b9642016-03-13 14:13:58 -0700313
reedaeab8ea2016-01-05 10:01:38 -0800314 SkRect r = {0, 83, 1254, 620};
315 surface->getCanvas()->drawRect(r, paint);
316}
317
fmalita5edf82e2016-03-03 06:41:54 -0800318// https://bugs.chromium.org/p/skia/issues/detail?id=5023
319// We should still shade pixels for which the radius is exactly 0.
320static void test_two_point_conical_zero_radius(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700321 auto surface(SkSurface::MakeRasterN32Premul(5, 5));
fmalita5edf82e2016-03-03 06:41:54 -0800322 surface->getCanvas()->clear(SK_ColorRED);
323
324 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
reed1a9b9642016-03-13 14:13:58 -0700325 SkPaint p;
326 p.setShader(SkGradientShader::MakeTwoPointConical(
fmalita5edf82e2016-03-03 06:41:54 -0800327 SkPoint::Make(2.5f, 2.5f), 0,
328 SkPoint::Make(3.0f, 3.0f), 10,
Mike Reedfae8fce2019-04-03 10:27:45 -0400329 colors, nullptr, SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
fmalita5edf82e2016-03-03 06:41:54 -0800330 surface->getCanvas()->drawPaint(p);
331
332 // r == 0 for the center pixel.
333 // verify that we draw it (no red bleed)
334 SkPMColor centerPMColor;
335 surface->readPixels(SkImageInfo::MakeN32Premul(1, 1), &centerPMColor, sizeof(SkPMColor), 2, 2);
336 REPORTER_ASSERT(reporter, SkGetPackedR32(centerPMColor) == 0);
337}
338
fmalita7b38e3c2016-05-26 11:13:52 -0700339// http://crbug.com/599458
340static void test_clamping_overflow(skiatest::Reporter*) {
341 SkPaint p;
342 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
343 const SkPoint pts1[] = { SkPoint::Make(1001, 1000001), SkPoint::Make(1000.99f, 1000000) };
344
Mike Reedfae8fce2019-04-03 10:27:45 -0400345 p.setShader(SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkTileMode::kClamp));
fmalita7b38e3c2016-05-26 11:13:52 -0700346
347 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
348 surface->getCanvas()->scale(100, 100);
349 surface->getCanvas()->drawPaint(p);
350
351 const SkPoint pts2[] = { SkPoint::Make(10000.99f, 1000000), SkPoint::Make(10001, 1000001) };
Mike Reedfae8fce2019-04-03 10:27:45 -0400352 p.setShader(SkGradientShader::MakeLinear(pts2, colors, nullptr, 2, SkTileMode::kClamp));
fmalita7b38e3c2016-05-26 11:13:52 -0700353 surface->getCanvas()->drawPaint(p);
354
355 // Passes if we don't trigger asserts.
356}
357
fmalitac5231042016-08-10 05:45:50 -0700358// http://crbug.com/636194
Florin Malitae659c7f2017-02-09 13:46:55 -0500359static void test_degenerate_linear(skiatest::Reporter*) {
fmalitac5231042016-08-10 05:45:50 -0700360 SkPaint p;
361 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
362 const SkPoint pts[] = {
363 SkPoint::Make(-46058024627067344430605278824628224.0f, 0),
364 SkPoint::Make(SK_ScalarMax, 0)
365 };
366
Mike Reedfae8fce2019-04-03 10:27:45 -0400367 p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
fmalitac5231042016-08-10 05:45:50 -0700368 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
369 surface->getCanvas()->drawPaint(p);
370
371 // Passes if we don't trigger asserts.
372}
373
Michael Ludwigad5ec1a2020-11-19 14:15:05 -0500374// http://crbug.com/1149216
375static void test_unsorted_degenerate(skiatest::Reporter* r) {
376 // Passes if a valid solid color is computed for the degenerate gradient
377 // (unsorted positions are fixed during regular gradient construction, so this ensures the
378 // same fixing happens for degenerate gradients as well). If they aren't fixed, this test
379 // case produces a negative alpha, which asserts during SkPMColor4f::isOpaque().
380 const SkColor4f colors[] = { {0.f, 0.f, 0.f, 0.f},
381 {0.00784314f, 0.f, 0.f, 0.0627451f},
382 {0.f, 0.00392157f, 0.f, 0.f} };
383 const SkScalar positions[] = {0.00753367f, 8.54792e-44f, 1.46955e-39f};
384
385 const SkPoint points[] { { 0.f, 0.f }, { 1e-20f, -1e-8f }}; // must be degenerate
386 // Use kMirror to go through average color stop calculation, vs. kClamp which would pick a color
387 sk_sp<SkShader> gradient = SkGradientShader::MakeLinear(points, colors, nullptr, positions, 3,
388 SkTileMode::kMirror);
389
390 // The degenerate gradient shouldn't be null
391 REPORTER_ASSERT(r, SkToBool(gradient));
392 // And it shouldn't crash when creating a fragment processor
393
394 SkSimpleMatrixProvider provider(SkMatrix::I());
395 GrColorInfo dstColorInfo(GrColorType::kRGBA_8888, kPremul_SkAlphaType,
396 SkColorSpace::MakeSRGB());
397 GrMockOptions options;
398 auto context = GrDirectContext::MakeMock(&options);
399
Mike Reed12a75582021-03-20 10:49:02 -0400400 GrFPArgs args(context.get(), provider, &dstColorInfo);
Michael Ludwigad5ec1a2020-11-19 14:15:05 -0500401 as_SB(gradient)->asFragmentProcessor(args);
402}
403
Florin Malitae659c7f2017-02-09 13:46:55 -0500404// "Interesting" fuzzer values.
405static void test_linear_fuzzer(skiatest::Reporter*) {
406 static const SkColor gColors0[] = { 0x30303030, 0x30303030 };
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500407 static const SkColor gColors1[] = { 0x30303030, 0x30303030, 0x30303030 };
408
409 static const SkScalar gPos1[] = { 0, 0, 1 };
410
Florin Malitad9569662017-02-09 16:41:34 -0500411 static const SkScalar gMatrix0[9] = {
412 6.40969056e-10f, 0 , 6.40969056e-10f,
413 0 , 4.42539023e-39f, 6.40969056e-10f,
414 0 , 0 , 1
415 };
416 static const SkScalar gMatrix1[9] = {
417 -2.75294113f , 6.40969056e-10f, 6.40969056e-10f,
418 6.40969056e-10f, 6.40969056e-10f, -3.32810161e+24f,
419 6.40969056e-10f, 6.40969056e-10f, 0
420 };
421 static const SkScalar gMatrix2[9] = {
422 7.93481258e+17f, 6.40969056e-10f, 6.40969056e-10f,
423 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f,
424 6.40969056e-10f, 6.40969056e-10f, 0.688235283f
425 };
426 static const SkScalar gMatrix3[9] = {
427 1.89180674e+11f, 6.40969056e-10f, 6.40969056e-10f,
428 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f,
429 6.40969056e-10f, 11276.0469f , 8.12524808e+20f
430 };
Florin Malitae659c7f2017-02-09 13:46:55 -0500431
432 static const struct {
433 SkPoint fPts[2];
434 const SkColor* fColors;
435 const SkScalar* fPos;
436 int fCount;
Mike Reedfae8fce2019-04-03 10:27:45 -0400437 SkTileMode fTileMode;
Florin Malitae659c7f2017-02-09 13:46:55 -0500438 uint32_t fFlags;
439 const SkScalar* fLocalMatrix;
440 const SkScalar* fGlobalMatrix;
441 } gConfigs[] = {
442 {
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500443 {{0, -2.752941f}, {0, 0}},
444 gColors0,
445 nullptr,
446 SK_ARRAY_COUNT(gColors0),
Mike Reedfae8fce2019-04-03 10:27:45 -0400447 SkTileMode::kClamp,
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500448 0,
449 gMatrix0,
450 nullptr
451 },
452 {
453 {{4.42539023e-39f, -4.42539023e-39f}, {9.78041162e-15f, 4.42539023e-39f}},
454 gColors1,
455 gPos1,
456 SK_ARRAY_COUNT(gColors1),
Mike Reedfae8fce2019-04-03 10:27:45 -0400457 SkTileMode::kClamp,
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500458 0,
459 nullptr,
460 gMatrix1
Florin Malitae659c7f2017-02-09 13:46:55 -0500461 },
Florin Malitad9569662017-02-09 16:41:34 -0500462 {
463 {{4.42539023e-39f, 6.40969056e-10f}, {6.40969056e-10f, 1.49237238e-19f}},
464 gColors1,
465 gPos1,
466 SK_ARRAY_COUNT(gColors1),
Mike Reedfae8fce2019-04-03 10:27:45 -0400467 SkTileMode::kClamp,
Florin Malitad9569662017-02-09 16:41:34 -0500468 0,
469 nullptr,
470 gMatrix2
471 },
472 {
473 {{6.40969056e-10f, 6.40969056e-10f}, {6.40969056e-10f, -0.688235283f}},
474 gColors0,
475 nullptr,
476 SK_ARRAY_COUNT(gColors0),
Mike Reedfae8fce2019-04-03 10:27:45 -0400477 SkTileMode::kClamp,
Florin Malitad9569662017-02-09 16:41:34 -0500478 0,
479 gMatrix3,
480 nullptr
481 },
Florin Malitae659c7f2017-02-09 13:46:55 -0500482 };
483
Florin Malitad1aedde2017-06-07 15:03:38 -0400484 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
485 SkColorSpace* colorSpaces[] = {
486 nullptr, // hits the legacy gradient impl
487 srgb.get(), // triggers 4f/raster-pipeline
488 };
Florin Malitae659c7f2017-02-09 13:46:55 -0500489
Florin Malitae659c7f2017-02-09 13:46:55 -0500490 SkPaint paint;
491
John Stilesbd3ffa42020-07-30 20:24:57 -0400492 for (const SkColorSpace* colorSpace : colorSpaces) {
Florin Malitad1aedde2017-06-07 15:03:38 -0400493
494 sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::Make(100, 100,
495 kN32_SkColorType,
496 kPremul_SkAlphaType,
497 sk_ref_sp(colorSpace)));
498 SkCanvas* canvas = surface->getCanvas();
499
Florin Malitae659c7f2017-02-09 13:46:55 -0500500 for (const auto& config : gConfigs) {
501 SkAutoCanvasRestore acr(canvas, false);
502 SkTLazy<SkMatrix> localMatrix;
503 if (config.fLocalMatrix) {
504 localMatrix.init();
John Stilesa008b0f2020-08-16 08:48:02 -0400505 localMatrix->set9(config.fLocalMatrix);
Florin Malitae659c7f2017-02-09 13:46:55 -0500506 }
507
508 paint.setShader(SkGradientShader::MakeLinear(config.fPts,
509 config.fColors,
510 config.fPos,
511 config.fCount,
512 config.fTileMode,
Florin Malitad1aedde2017-06-07 15:03:38 -0400513 config.fFlags,
Florin Malitae659c7f2017-02-09 13:46:55 -0500514 localMatrix.getMaybeNull()));
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500515 if (config.fGlobalMatrix) {
516 SkMatrix m;
517 m.set9(config.fGlobalMatrix);
518 canvas->save();
519 canvas->concat(m);
520 }
521
Florin Malitae659c7f2017-02-09 13:46:55 -0500522 canvas->drawPaint(paint);
523 }
524 }
525}
526
Florin Malita03013082017-04-18 13:47:15 -0400527static void test_sweep_fuzzer(skiatest::Reporter*) {
528 static const SkColor gColors0[] = { 0x30303030, 0x30303030, 0x30303030 };
529 static const SkScalar gPos0[] = { -47919293023455565225163489280.0f, 0, 1 };
530 static const SkScalar gMatrix0[9] = {
531 1.12116716e-13f, 0 , 8.50489682e+16f,
532 4.1917041e-41f , 3.51369881e-23f, -2.54344271e-26f,
533 9.61111907e+17f, -3.35263808e-29f, -1.35659403e+14f
534 };
535 static const struct {
536 SkPoint fCenter;
537 const SkColor* fColors;
538 const SkScalar* fPos;
539 int fCount;
540 const SkScalar* fGlobalMatrix;
541 } gConfigs[] = {
542 {
543 { 0, 0 },
544 gColors0,
545 gPos0,
546 SK_ARRAY_COUNT(gColors0),
547 gMatrix0
548 },
549 };
550
551 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
552 SkCanvas* canvas = surface->getCanvas();
553 SkPaint paint;
554
555 for (const auto& config : gConfigs) {
556 paint.setShader(SkGradientShader::MakeSweep(config.fCenter.x(),
557 config.fCenter.y(),
558 config.fColors,
559 config.fPos,
560 config.fCount));
561
562 SkAutoCanvasRestore acr(canvas, false);
563 if (config.fGlobalMatrix) {
564 SkMatrix m;
565 m.set9(config.fGlobalMatrix);
566 canvas->save();
567 canvas->concat(m);
568 }
569 canvas->drawPaint(paint);
570 }
571}
Florin Malitae659c7f2017-02-09 13:46:55 -0500572
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000573DEF_TEST(Gradient, reporter) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000574 TestGradientShaders(reporter);
fmalita51229672016-08-22 06:22:28 -0700575 TestGradientOptimization(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000576 TestConstantGradient(reporter);
reed9d91eb32015-01-28 11:44:48 -0800577 test_big_grad(reporter);
fmalita8d381022015-11-19 10:35:34 -0800578 test_nearly_vertical(reporter);
James Zern44e91c92016-11-09 19:22:46 -0800579 test_vertical(reporter);
reedaeab8ea2016-01-05 10:01:38 -0800580 test_linear_fuzz(reporter);
fmalita5edf82e2016-03-03 06:41:54 -0800581 test_two_point_conical_zero_radius(reporter);
fmalita7b38e3c2016-05-26 11:13:52 -0700582 test_clamping_overflow(reporter);
Florin Malitae659c7f2017-02-09 13:46:55 -0500583 test_degenerate_linear(reporter);
584 test_linear_fuzzer(reporter);
Florin Malita03013082017-04-18 13:47:15 -0400585 test_sweep_fuzzer(reporter);
Michael Ludwigad5ec1a2020-11-19 14:15:05 -0500586 test_unsorted_degenerate(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000587}