blob: 3c62cb9d629e81f981516b201ca9530de93fe9e5 [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
reed8c0c7b02014-06-27 05:49:53 -07008#include "SkCanvas.h"
fmalita5edf82e2016-03-03 06:41:54 -08009#include "SkColorPriv.h"
reed@google.com83226972012-06-07 20:26:47 +000010#include "SkColorShader.h"
reed@google.com83226972012-06-07 20:26:47 +000011#include "SkGradientShader.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000012#include "SkShader.h"
fmalita8d381022015-11-19 10:35:34 -080013#include "SkSurface.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000014#include "SkTemplates.h"
Florin Malitae659c7f2017-02-09 13:46:55 -050015#include "SkTLazy.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "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;
reed1a9b9642016-03-13 14:13:58 -070025 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
26 SkShader::kClamp_TileMode));
reed9d91eb32015-01-28 11:44:48 -080027
28 SkBitmap bm;
29 bm.allocN32Pixels(2000, 1);
30 SkCanvas c(bm);
31
32 const SkScalar affine[] = {
33 1.06608627e-06f, 4.26434525e-07f, 6.2855f, 2.6611f, 273.4393f, 244.0046f
34 };
35 SkMatrix matrix;
36 matrix.setAffine(affine);
37 c.concat(matrix);
halcanary9d524f22016-03-29 09:03:52 -070038
reed9d91eb32015-01-28 11:44:48 -080039 c.drawPaint(paint);
40}
41
reed@google.com83226972012-06-07 20:26:47 +000042struct GradRec {
43 int fColorCount;
44 const SkColor* fColors;
45 const SkScalar* fPos;
46 const SkPoint* fPoint; // 2
47 const SkScalar* fRadius; // 2
48 SkShader::TileMode fTileMode;
49
reed1a9b9642016-03-13 14:13:58 -070050 void gradCheck(skiatest::Reporter* reporter, const sk_sp<SkShader>& shader,
reed@google.com83226972012-06-07 20:26:47 +000051 SkShader::GradientInfo* info,
52 SkShader::GradientType gt) const {
53 SkAutoTMalloc<SkColor> colorStorage(fColorCount);
54 SkAutoTMalloc<SkScalar> posStorage(fColorCount);
55
56 info->fColorCount = fColorCount;
57 info->fColors = colorStorage;
58 info->fColorOffsets = posStorage.get();
59 REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt);
60
61 REPORTER_ASSERT(reporter, info->fColorCount == fColorCount);
62 REPORTER_ASSERT(reporter,
63 !memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor)));
64 REPORTER_ASSERT(reporter,
65 !memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar)));
66 REPORTER_ASSERT(reporter, fTileMode == info->fTileMode);
67 }
68};
69
70
fmalita51229672016-08-22 06:22:28 -070071static void none_gradproc(skiatest::Reporter* reporter, const GradRec&, const GradRec&) {
reed1a9b9642016-03-13 14:13:58 -070072 sk_sp<SkShader> s(SkShader::MakeEmptyShader());
halcanary96fcdcc2015-08-27 07:41:13 -070073 REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(nullptr));
reed@google.com83226972012-06-07 20:26:47 +000074}
75
fmalita51229672016-08-22 06:22:28 -070076static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec, const GradRec&) {
Hal Canary342b7ac2016-11-04 11:49:42 -040077 sk_sp<SkShader> s(new SkColorShader(rec.fColors[0]));
halcanary96fcdcc2015-08-27 07:41:13 -070078 REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(nullptr));
reed@google.com83226972012-06-07 20:26:47 +000079
80 SkShader::GradientInfo info;
halcanary96fcdcc2015-08-27 07:41:13 -070081 info.fColors = nullptr;
reed@google.com83226972012-06-07 20:26:47 +000082 info.fColorCount = 0;
83 s->asAGradient(&info);
84 REPORTER_ASSERT(reporter, 1 == info.fColorCount);
85}
86
fmalita51229672016-08-22 06:22:28 -070087static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
88 const GradRec& checkRec) {
89 sk_sp<SkShader> s(SkGradientShader::MakeLinear(buildRec.fPoint, buildRec.fColors, buildRec.fPos,
90 buildRec.fColorCount, buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +000091
reed@google.com83226972012-06-07 20:26:47 +000092 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -070093 checkRec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType);
94 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint)));
reed@google.com83226972012-06-07 20:26:47 +000095}
96
fmalita51229672016-08-22 06:22:28 -070097static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
98 const GradRec& checkRec) {
99 sk_sp<SkShader> s(SkGradientShader::MakeRadial(buildRec.fPoint[0], buildRec.fRadius[0],
100 buildRec.fColors, buildRec.fPos,
101 buildRec.fColorCount, buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102
reed@google.com83226972012-06-07 20:26:47 +0000103 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700104 checkRec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType);
105 REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]);
106 REPORTER_ASSERT(reporter, info.fRadius[0] == checkRec.fRadius[0]);
reed@google.com83226972012-06-07 20:26:47 +0000107}
108
fmalita51229672016-08-22 06:22:28 -0700109static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
110 const GradRec& checkRec) {
111 sk_sp<SkShader> s(SkGradientShader::MakeSweep(buildRec.fPoint[0].fX, buildRec.fPoint[0].fY,
112 buildRec.fColors, buildRec.fPos,
113 buildRec.fColorCount));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114
reed@google.com83226972012-06-07 20:26:47 +0000115 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700116 checkRec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType);
117 REPORTER_ASSERT(reporter, info.fPoint[0] == checkRec.fPoint[0]);
reed@google.com83226972012-06-07 20:26:47 +0000118}
119
fmalita51229672016-08-22 06:22:28 -0700120static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& buildRec,
121 const GradRec& checkRec) {
122 sk_sp<SkShader> s(SkGradientShader::MakeTwoPointConical(buildRec.fPoint[0],
123 buildRec.fRadius[0],
124 buildRec.fPoint[1],
125 buildRec.fRadius[1],
126 buildRec.fColors,
127 buildRec.fPos,
128 buildRec.fColorCount,
129 buildRec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130
reed@google.com83226972012-06-07 20:26:47 +0000131 SkShader::GradientInfo info;
fmalita51229672016-08-22 06:22:28 -0700132 checkRec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType);
133 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, checkRec.fPoint, 2 * sizeof(SkPoint)));
134 REPORTER_ASSERT(reporter, !memcmp(info.fRadius, checkRec.fRadius, 2 * sizeof(SkScalar)));
reed@google.com83226972012-06-07 20:26:47 +0000135}
136
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000137// Ensure that repeated color gradients behave like drawing a single color
sugoi@google.come0e385c2013-03-11 18:50:03 +0000138static void TestConstantGradient(skiatest::Reporter*) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000139 const SkPoint pts[] = {
140 { 0, 0 },
141 { SkIntToScalar(10), 0 }
142 };
143 SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE };
144 const SkScalar pos[] = { 0, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700145 SkPaint paint;
146 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkShader::kClamp_TileMode));
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000147 SkBitmap outBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000148 outBitmap.allocN32Pixels(10, 1);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000149 SkCanvas canvas(outBitmap);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000150 canvas.drawPaint(paint);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000151 for (int i = 0; i < 10; i++) {
152 // The following is commented out because it currently fails
153 // Related bug: https://code.google.com/p/skia/issues/detail?id=1098
154
155 // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0));
156 }
157}
158
fmalita51229672016-08-22 06:22:28 -0700159typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&, const GradRec&);
reed@google.com83226972012-06-07 20:26:47 +0000160
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000161static void TestGradientShaders(skiatest::Reporter* reporter) {
reed@google.com83226972012-06-07 20:26:47 +0000162 static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
163 static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
164 static const SkPoint gPts[] = {
165 { 0, 0 },
166 { SkIntToScalar(10), SkIntToScalar(20) }
167 };
168 static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) };
169
170 GradRec rec;
171 rec.fColorCount = SK_ARRAY_COUNT(gColors);
172 rec.fColors = gColors;
173 rec.fPos = gPos;
174 rec.fPoint = gPts;
175 rec.fRadius = gRad;
176 rec.fTileMode = SkShader::kClamp_TileMode;
177
178 static const GradProc gProcs[] = {
179 none_gradproc,
180 color_gradproc,
181 linear_gradproc,
182 radial_gradproc,
reed@google.com83226972012-06-07 20:26:47 +0000183 sweep_gradproc,
184 conical_gradproc,
185 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000186
reed@google.com83226972012-06-07 20:26:47 +0000187 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
fmalita51229672016-08-22 06:22:28 -0700188 gProcs[i](reporter, rec, rec);
189 }
190}
191
192static void TestGradientOptimization(skiatest::Reporter* reporter) {
193 static const struct {
194 GradProc fProc;
195 bool fIsClampRestricted;
196 } gProcInfo[] = {
197 { linear_gradproc , false },
198 { radial_gradproc , false },
199 { sweep_gradproc , true }, // sweep is funky in that it always pretends to be kClamp.
200 { conical_gradproc, false },
201 };
202
203 static const SkColor gC_00[] = { 0xff000000, 0xff000000 };
204 static const SkColor gC_01[] = { 0xff000000, 0xffffffff };
205 static const SkColor gC_11[] = { 0xffffffff, 0xffffffff };
206 static const SkColor gC_001[] = { 0xff000000, 0xff000000, 0xffffffff };
207 static const SkColor gC_011[] = { 0xff000000, 0xffffffff, 0xffffffff };
208 static const SkColor gC_0011[] = { 0xff000000, 0xff000000, 0xffffffff, 0xffffffff };
209
210 static const SkScalar gP_01[] = { 0, 1 };
211 static const SkScalar gP_001[] = { 0, 0, 1 };
212 static const SkScalar gP_011[] = { 0, 1, 1 };
213 static const SkScalar gP_0x1[] = { 0, .5f, 1 };
214 static const SkScalar gP_0011[] = { 0, 0, 1, 1 };
215
216 static const SkPoint gPts[] = { {0, 0}, {1, 1} };
217 static const SkScalar gRadii[] = { 1, 2 };
218
219 static const struct {
220 const SkColor* fCol;
221 const SkScalar* fPos;
222 int fCount;
223
224 const SkColor* fExpectedCol;
225 const SkScalar* fExpectedPos;
226 int fExpectedCount;
227 bool fRequiresNonClamp;
228 } gTests[] = {
229 { gC_001, gP_001, 3, gC_01, gP_01, 2, false },
230 { gC_001, gP_011, 3, gC_00, gP_01, 2, true },
231 { gC_001, gP_0x1, 3, gC_001, gP_0x1, 3, false },
232 { gC_001, nullptr, 3, gC_001, gP_0x1, 3, false },
233
234 { gC_011, gP_001, 3, gC_11, gP_01, 2, true },
235 { gC_011, gP_011, 3, gC_01, gP_01, 2, false },
236 { gC_011, gP_0x1, 3, gC_011, gP_0x1, 3, false },
237 { gC_011, nullptr, 3, gC_011, gP_0x1, 3, false },
238
239 { gC_0011, gP_0011, 4, gC_0011, gP_0011, 4, false },
240 };
241
242 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcInfo); ++i) {
243 for (int mode = 0; mode < SkShader::kTileModeCount; ++mode) {
244 if (gProcInfo[i].fIsClampRestricted && mode != SkShader::kClamp_TileMode) {
245 continue;
246 }
247
248 for (size_t t = 0; t < SK_ARRAY_COUNT(gTests); ++t) {
249 GradRec rec;
250 rec.fColorCount = gTests[t].fCount;
251 rec.fColors = gTests[t].fCol;
252 rec.fPos = gTests[t].fPos;
253 rec.fTileMode = static_cast<SkShader::TileMode>(mode);
254 rec.fPoint = gPts;
255 rec.fRadius = gRadii;
256
257 GradRec expected = rec;
258 if (!gTests[t].fRequiresNonClamp || mode != SkShader::kClamp_TileMode) {
259 expected.fColorCount = gTests[t].fExpectedCount;
260 expected.fColors = gTests[t].fExpectedCol;
261 expected.fPos = gTests[t].fExpectedPos;
262 }
263
264 gProcInfo[i].fProc(reporter, rec, expected);
265 }
266 }
reed@google.com83226972012-06-07 20:26:47 +0000267 }
268}
269
fmalita8d381022015-11-19 10:35:34 -0800270static void test_nearly_vertical(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700271 auto surface(SkSurface::MakeRasterN32Premul(200, 200));
fmalita8d381022015-11-19 10:35:34 -0800272
273 const SkPoint pts[] = {{ 100, 50 }, { 100.0001f, 50000 }};
274 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
275 const SkScalar pos[] = { 0, 1 };
fmalita8d381022015-11-19 10:35:34 -0800276 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700277 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkShader::kClamp_TileMode));
fmalita8d381022015-11-19 10:35:34 -0800278
279 surface->getCanvas()->drawPaint(paint);
280}
281
James Zern44e91c92016-11-09 19:22:46 -0800282static void test_vertical(skiatest::Reporter* reporter) {
283 auto surface(SkSurface::MakeRasterN32Premul(200, 200));
284
285 const SkPoint pts[] = {{ 100, 50 }, { 100, 50 }};
286 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
287 const SkScalar pos[] = { 0, 1 };
288 SkPaint paint;
289 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, SkShader::kClamp_TileMode));
290
291 surface->getCanvas()->drawPaint(paint);
292}
293
reedaeab8ea2016-01-05 10:01:38 -0800294// A linear gradient interval can, due to numerical imprecision (likely in the divide)
295// finish an interval with the final fx not landing outside of [p0...p1].
296// The old code had an assert which this test triggered.
297// We now explicitly clamp the resulting fx value.
298static void test_linear_fuzz(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700299 auto surface(SkSurface::MakeRasterN32Premul(1300, 630));
reedaeab8ea2016-01-05 10:01:38 -0800300
301 const SkPoint pts[] = {{ 179.5f, -179.5f }, { 1074.5f, 715.5f }};
302 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE };
303 const SkScalar pos[] = {0, 0.200000003f, 0.800000012f, 1 };
304
reed9283d202016-03-13 13:01:57 -0700305 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700306 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 4, SkShader::kClamp_TileMode));
307
reedaeab8ea2016-01-05 10:01:38 -0800308 SkRect r = {0, 83, 1254, 620};
309 surface->getCanvas()->drawRect(r, paint);
310}
311
fmalita5edf82e2016-03-03 06:41:54 -0800312// https://bugs.chromium.org/p/skia/issues/detail?id=5023
313// We should still shade pixels for which the radius is exactly 0.
314static void test_two_point_conical_zero_radius(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700315 auto surface(SkSurface::MakeRasterN32Premul(5, 5));
fmalita5edf82e2016-03-03 06:41:54 -0800316 surface->getCanvas()->clear(SK_ColorRED);
317
318 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
reed1a9b9642016-03-13 14:13:58 -0700319 SkPaint p;
320 p.setShader(SkGradientShader::MakeTwoPointConical(
fmalita5edf82e2016-03-03 06:41:54 -0800321 SkPoint::Make(2.5f, 2.5f), 0,
322 SkPoint::Make(3.0f, 3.0f), 10,
323 colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
fmalita5edf82e2016-03-03 06:41:54 -0800324 surface->getCanvas()->drawPaint(p);
325
326 // r == 0 for the center pixel.
327 // verify that we draw it (no red bleed)
328 SkPMColor centerPMColor;
329 surface->readPixels(SkImageInfo::MakeN32Premul(1, 1), &centerPMColor, sizeof(SkPMColor), 2, 2);
330 REPORTER_ASSERT(reporter, SkGetPackedR32(centerPMColor) == 0);
331}
332
fmalita7b38e3c2016-05-26 11:13:52 -0700333// http://crbug.com/599458
334static void test_clamping_overflow(skiatest::Reporter*) {
335 SkPaint p;
336 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
337 const SkPoint pts1[] = { SkPoint::Make(1001, 1000001), SkPoint::Make(1000.99f, 1000000) };
338
339 p.setShader(SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkShader::kClamp_TileMode));
340
341 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
342 surface->getCanvas()->scale(100, 100);
343 surface->getCanvas()->drawPaint(p);
344
345 const SkPoint pts2[] = { SkPoint::Make(10000.99f, 1000000), SkPoint::Make(10001, 1000001) };
346 p.setShader(SkGradientShader::MakeLinear(pts2, colors, nullptr, 2, SkShader::kClamp_TileMode));
347 surface->getCanvas()->drawPaint(p);
348
349 // Passes if we don't trigger asserts.
350}
351
fmalitac5231042016-08-10 05:45:50 -0700352// http://crbug.com/636194
Florin Malitae659c7f2017-02-09 13:46:55 -0500353static void test_degenerate_linear(skiatest::Reporter*) {
fmalitac5231042016-08-10 05:45:50 -0700354 SkPaint p;
355 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
356 const SkPoint pts[] = {
357 SkPoint::Make(-46058024627067344430605278824628224.0f, 0),
358 SkPoint::Make(SK_ScalarMax, 0)
359 };
360
361 p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode));
362 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
363 surface->getCanvas()->drawPaint(p);
364
365 // Passes if we don't trigger asserts.
366}
367
Florin Malitae659c7f2017-02-09 13:46:55 -0500368// "Interesting" fuzzer values.
369static void test_linear_fuzzer(skiatest::Reporter*) {
370 static const SkColor gColors0[] = { 0x30303030, 0x30303030 };
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500371 static const SkColor gColors1[] = { 0x30303030, 0x30303030, 0x30303030 };
372
373 static const SkScalar gPos1[] = { 0, 0, 1 };
374
Florin Malitad9569662017-02-09 16:41:34 -0500375 static const SkScalar gMatrix0[9] = {
376 6.40969056e-10f, 0 , 6.40969056e-10f,
377 0 , 4.42539023e-39f, 6.40969056e-10f,
378 0 , 0 , 1
379 };
380 static const SkScalar gMatrix1[9] = {
381 -2.75294113f , 6.40969056e-10f, 6.40969056e-10f,
382 6.40969056e-10f, 6.40969056e-10f, -3.32810161e+24f,
383 6.40969056e-10f, 6.40969056e-10f, 0
384 };
385 static const SkScalar gMatrix2[9] = {
386 7.93481258e+17f, 6.40969056e-10f, 6.40969056e-10f,
387 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f,
388 6.40969056e-10f, 6.40969056e-10f, 0.688235283f
389 };
390 static const SkScalar gMatrix3[9] = {
391 1.89180674e+11f, 6.40969056e-10f, 6.40969056e-10f,
392 6.40969056e-10f, 6.40969056e-10f, 6.40969056e-10f,
393 6.40969056e-10f, 11276.0469f , 8.12524808e+20f
394 };
Florin Malitae659c7f2017-02-09 13:46:55 -0500395
396 static const struct {
397 SkPoint fPts[2];
398 const SkColor* fColors;
399 const SkScalar* fPos;
400 int fCount;
401 SkShader::TileMode fTileMode;
402 uint32_t fFlags;
403 const SkScalar* fLocalMatrix;
404 const SkScalar* fGlobalMatrix;
405 } gConfigs[] = {
406 {
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500407 {{0, -2.752941f}, {0, 0}},
408 gColors0,
409 nullptr,
410 SK_ARRAY_COUNT(gColors0),
411 SkShader::kClamp_TileMode,
412 0,
413 gMatrix0,
414 nullptr
415 },
416 {
417 {{4.42539023e-39f, -4.42539023e-39f}, {9.78041162e-15f, 4.42539023e-39f}},
418 gColors1,
419 gPos1,
420 SK_ARRAY_COUNT(gColors1),
421 SkShader::kClamp_TileMode,
422 0,
423 nullptr,
424 gMatrix1
Florin Malitae659c7f2017-02-09 13:46:55 -0500425 },
Florin Malitad9569662017-02-09 16:41:34 -0500426 {
427 {{4.42539023e-39f, 6.40969056e-10f}, {6.40969056e-10f, 1.49237238e-19f}},
428 gColors1,
429 gPos1,
430 SK_ARRAY_COUNT(gColors1),
431 SkShader::kClamp_TileMode,
432 0,
433 nullptr,
434 gMatrix2
435 },
436 {
437 {{6.40969056e-10f, 6.40969056e-10f}, {6.40969056e-10f, -0.688235283f}},
438 gColors0,
439 nullptr,
440 SK_ARRAY_COUNT(gColors0),
441 SkShader::kClamp_TileMode,
442 0,
443 gMatrix3,
444 nullptr
445 },
Florin Malitae659c7f2017-02-09 13:46:55 -0500446 };
447
Florin Malitad1aedde2017-06-07 15:03:38 -0400448 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
449 SkColorSpace* colorSpaces[] = {
450 nullptr, // hits the legacy gradient impl
451 srgb.get(), // triggers 4f/raster-pipeline
452 };
Florin Malitae659c7f2017-02-09 13:46:55 -0500453
Florin Malitae659c7f2017-02-09 13:46:55 -0500454 SkPaint paint;
455
Florin Malitad1aedde2017-06-07 15:03:38 -0400456 for (auto colorSpace : colorSpaces) {
457
458 sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::Make(100, 100,
459 kN32_SkColorType,
460 kPremul_SkAlphaType,
461 sk_ref_sp(colorSpace)));
462 SkCanvas* canvas = surface->getCanvas();
463
Florin Malitae659c7f2017-02-09 13:46:55 -0500464 for (const auto& config : gConfigs) {
465 SkAutoCanvasRestore acr(canvas, false);
466 SkTLazy<SkMatrix> localMatrix;
467 if (config.fLocalMatrix) {
468 localMatrix.init();
469 localMatrix.get()->set9(config.fLocalMatrix);
470 }
471
472 paint.setShader(SkGradientShader::MakeLinear(config.fPts,
473 config.fColors,
474 config.fPos,
475 config.fCount,
476 config.fTileMode,
Florin Malitad1aedde2017-06-07 15:03:38 -0400477 config.fFlags,
Florin Malitae659c7f2017-02-09 13:46:55 -0500478 localMatrix.getMaybeNull()));
Florin Malita3d1a6bc2017-02-09 15:05:15 -0500479 if (config.fGlobalMatrix) {
480 SkMatrix m;
481 m.set9(config.fGlobalMatrix);
482 canvas->save();
483 canvas->concat(m);
484 }
485
Florin Malitae659c7f2017-02-09 13:46:55 -0500486 canvas->drawPaint(paint);
487 }
488 }
489}
490
Florin Malita03013082017-04-18 13:47:15 -0400491static void test_sweep_fuzzer(skiatest::Reporter*) {
492 static const SkColor gColors0[] = { 0x30303030, 0x30303030, 0x30303030 };
493 static const SkScalar gPos0[] = { -47919293023455565225163489280.0f, 0, 1 };
494 static const SkScalar gMatrix0[9] = {
495 1.12116716e-13f, 0 , 8.50489682e+16f,
496 4.1917041e-41f , 3.51369881e-23f, -2.54344271e-26f,
497 9.61111907e+17f, -3.35263808e-29f, -1.35659403e+14f
498 };
499 static const struct {
500 SkPoint fCenter;
501 const SkColor* fColors;
502 const SkScalar* fPos;
503 int fCount;
504 const SkScalar* fGlobalMatrix;
505 } gConfigs[] = {
506 {
507 { 0, 0 },
508 gColors0,
509 gPos0,
510 SK_ARRAY_COUNT(gColors0),
511 gMatrix0
512 },
513 };
514
515 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
516 SkCanvas* canvas = surface->getCanvas();
517 SkPaint paint;
518
519 for (const auto& config : gConfigs) {
520 paint.setShader(SkGradientShader::MakeSweep(config.fCenter.x(),
521 config.fCenter.y(),
522 config.fColors,
523 config.fPos,
524 config.fCount));
525
526 SkAutoCanvasRestore acr(canvas, false);
527 if (config.fGlobalMatrix) {
528 SkMatrix m;
529 m.set9(config.fGlobalMatrix);
530 canvas->save();
531 canvas->concat(m);
532 }
533 canvas->drawPaint(paint);
534 }
535}
Florin Malitae659c7f2017-02-09 13:46:55 -0500536
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000537DEF_TEST(Gradient, reporter) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000538 TestGradientShaders(reporter);
fmalita51229672016-08-22 06:22:28 -0700539 TestGradientOptimization(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000540 TestConstantGradient(reporter);
reed9d91eb32015-01-28 11:44:48 -0800541 test_big_grad(reporter);
fmalita8d381022015-11-19 10:35:34 -0800542 test_nearly_vertical(reporter);
James Zern44e91c92016-11-09 19:22:46 -0800543 test_vertical(reporter);
reedaeab8ea2016-01-05 10:01:38 -0800544 test_linear_fuzz(reporter);
fmalita5edf82e2016-03-03 06:41:54 -0800545 test_two_point_conical_zero_radius(reporter);
fmalita7b38e3c2016-05-26 11:13:52 -0700546 test_clamping_overflow(reporter);
Florin Malitae659c7f2017-02-09 13:46:55 -0500547 test_degenerate_linear(reporter);
548 test_linear_fuzzer(reporter);
Florin Malita03013082017-04-18 13:47:15 -0400549 test_sweep_fuzzer(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000550}