blob: f530b3e263bb285d1b82b6ab0087d9b5a4ac4cad [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"
reed@google.com83226972012-06-07 20:26:47 +00009#include "SkColorShader.h"
reed@google.com83226972012-06-07 20:26:47 +000010#include "SkGradientShader.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000011#include "SkShader.h"
12#include "SkTemplates.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "Test.h"
reed@google.com83226972012-06-07 20:26:47 +000014
reed9d91eb32015-01-28 11:44:48 -080015// https://code.google.com/p/chromium/issues/detail?id=448299
16// Giant (inverse) matrix causes overflow when converting/computing using 32.32
17// Before the fix, we would assert (and then crash).
18static void test_big_grad(skiatest::Reporter* reporter) {
19 const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
20 const SkPoint pts[] = {{ 15, 14.7112684f }, { 0.709064007f, 12.6108112f }};
21 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
22 SkPaint paint;
23 paint.setShader(s)->unref();
24
25 SkBitmap bm;
26 bm.allocN32Pixels(2000, 1);
27 SkCanvas c(bm);
28
29 const SkScalar affine[] = {
30 1.06608627e-06f, 4.26434525e-07f, 6.2855f, 2.6611f, 273.4393f, 244.0046f
31 };
32 SkMatrix matrix;
33 matrix.setAffine(affine);
34 c.concat(matrix);
35
36 c.drawPaint(paint);
37}
38
reed@google.com83226972012-06-07 20:26:47 +000039struct GradRec {
40 int fColorCount;
41 const SkColor* fColors;
42 const SkScalar* fPos;
43 const SkPoint* fPoint; // 2
44 const SkScalar* fRadius; // 2
45 SkShader::TileMode fTileMode;
46
47 void gradCheck(skiatest::Reporter* reporter, SkShader* shader,
48 SkShader::GradientInfo* info,
49 SkShader::GradientType gt) const {
50 SkAutoTMalloc<SkColor> colorStorage(fColorCount);
51 SkAutoTMalloc<SkScalar> posStorage(fColorCount);
52
53 info->fColorCount = fColorCount;
54 info->fColors = colorStorage;
55 info->fColorOffsets = posStorage.get();
56 REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt);
57
58 REPORTER_ASSERT(reporter, info->fColorCount == fColorCount);
59 REPORTER_ASSERT(reporter,
60 !memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor)));
61 REPORTER_ASSERT(reporter,
62 !memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar)));
63 REPORTER_ASSERT(reporter, fTileMode == info->fTileMode);
64 }
65};
66
67
sugoi@google.come0e385c2013-03-11 18:50:03 +000068static void none_gradproc(skiatest::Reporter* reporter, const GradRec&) {
reede1085e02014-07-03 07:26:01 -070069 SkAutoTUnref<SkShader> s(SkShader::CreateEmptyShader());
reed@google.com83226972012-06-07 20:26:47 +000070 REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(NULL));
71}
72
73static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
74 SkAutoTUnref<SkShader> s(new SkColorShader(rec.fColors[0]));
75 REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(NULL));
76
77 SkShader::GradientInfo info;
robertphillips@google.com37ebe3f2013-03-04 20:03:44 +000078 info.fColors = NULL;
reed@google.com83226972012-06-07 20:26:47 +000079 info.fColorCount = 0;
80 s->asAGradient(&info);
81 REPORTER_ASSERT(reporter, 1 == info.fColorCount);
82}
83
84static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
85 SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(rec.fPoint,
86 rec.fColors,
87 rec.fPos,
88 rec.fColorCount,
89 rec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +000090
reed@google.com83226972012-06-07 20:26:47 +000091 SkShader::GradientInfo info;
92 rec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType);
93 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint)));
94}
95
96static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
97 SkAutoTUnref<SkShader> s(SkGradientShader::CreateRadial(rec.fPoint[0],
98 rec.fRadius[0],
99 rec.fColors,
100 rec.fPos,
101 rec.fColorCount,
102 rec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
reed@google.com83226972012-06-07 20:26:47 +0000104 SkShader::GradientInfo info;
105 rec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType);
106 REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]);
107 REPORTER_ASSERT(reporter, info.fRadius[0] == rec.fRadius[0]);
108}
109
110static void radial2_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
111 SkAutoTUnref<SkShader> s(SkGradientShader::CreateTwoPointRadial(rec.fPoint[0],
112 rec.fRadius[0],
113 rec.fPoint[1],
114 rec.fRadius[1],
115 rec.fColors,
116 rec.fPos,
117 rec.fColorCount,
118 rec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
reed@google.com83226972012-06-07 20:26:47 +0000120 SkShader::GradientInfo info;
121 rec.gradCheck(reporter, s, &info, SkShader::kRadial2_GradientType);
122 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint)));
123 REPORTER_ASSERT(reporter, !memcmp(info.fRadius, rec.fRadius, 2 * sizeof(SkScalar)));
124}
125
126static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
127 SkAutoTUnref<SkShader> s(SkGradientShader::CreateSweep(rec.fPoint[0].fX,
128 rec.fPoint[0].fY,
129 rec.fColors,
130 rec.fPos,
131 rec.fColorCount));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
reed@google.com83226972012-06-07 20:26:47 +0000133 SkShader::GradientInfo info;
134 rec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType);
135 REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]);
136}
137
138static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
139 SkAutoTUnref<SkShader> s(SkGradientShader::CreateTwoPointConical(rec.fPoint[0],
140 rec.fRadius[0],
141 rec.fPoint[1],
142 rec.fRadius[1],
143 rec.fColors,
144 rec.fPos,
145 rec.fColorCount,
146 rec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000147
reed@google.com83226972012-06-07 20:26:47 +0000148 SkShader::GradientInfo info;
149 rec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType);
150 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint)));
151 REPORTER_ASSERT(reporter, !memcmp(info.fRadius, rec.fRadius, 2 * sizeof(SkScalar)));
152}
153
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000154// Ensure that repeated color gradients behave like drawing a single color
sugoi@google.come0e385c2013-03-11 18:50:03 +0000155static void TestConstantGradient(skiatest::Reporter*) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000156 const SkPoint pts[] = {
157 { 0, 0 },
158 { SkIntToScalar(10), 0 }
159 };
160 SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE };
161 const SkScalar pos[] = { 0, SK_Scalar1 };
162 SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(pts,
163 colors,
164 pos,
165 2,
166 SkShader::kClamp_TileMode));
167 SkBitmap outBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000168 outBitmap.allocN32Pixels(10, 1);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000169 SkPaint paint;
170 paint.setShader(s.get());
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000171 SkCanvas canvas(outBitmap);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000172 canvas.drawPaint(paint);
173 SkAutoLockPixels alp(outBitmap);
174 for (int i = 0; i < 10; i++) {
175 // The following is commented out because it currently fails
176 // Related bug: https://code.google.com/p/skia/issues/detail?id=1098
177
178 // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0));
179 }
180}
181
reed@google.com83226972012-06-07 20:26:47 +0000182typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&);
183
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000184static void TestGradientShaders(skiatest::Reporter* reporter) {
reed@google.com83226972012-06-07 20:26:47 +0000185 static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
186 static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
187 static const SkPoint gPts[] = {
188 { 0, 0 },
189 { SkIntToScalar(10), SkIntToScalar(20) }
190 };
191 static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) };
192
193 GradRec rec;
194 rec.fColorCount = SK_ARRAY_COUNT(gColors);
195 rec.fColors = gColors;
196 rec.fPos = gPos;
197 rec.fPoint = gPts;
198 rec.fRadius = gRad;
199 rec.fTileMode = SkShader::kClamp_TileMode;
200
201 static const GradProc gProcs[] = {
202 none_gradproc,
203 color_gradproc,
204 linear_gradproc,
205 radial_gradproc,
206 radial2_gradproc,
207 sweep_gradproc,
208 conical_gradproc,
209 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000210
reed@google.com83226972012-06-07 20:26:47 +0000211 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
212 gProcs[i](reporter, rec);
213 }
214}
215
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000216DEF_TEST(Gradient, reporter) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000217 TestGradientShaders(reporter);
218 TestConstantGradient(reporter);
reed9d91eb32015-01-28 11:44:48 -0800219 test_big_grad(reporter);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000220}