Debian Codegraph @

fuzz/FuzzGradients.cpp

raw
1 /*
2 * Copyright 2016 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 */
7
8 #include "fuzz/Fuzz.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkSurface.h"
11 #include "include/effects/SkGradient.h"
12 #include "src/base/SkTLazy.h"
13 #include "src/core/SkDebugUtils.h"
14 #include "tools/flags/CommandLineFlags.h"
15
16 #include <algorithm>
17 #include <vector>
18
19 static DEFINE_bool2(verbose, v, false, "log verbose linear gradient description");
20
21 const int MAX_COUNT = 400;
22
23 void makeMatrix(Fuzz* fuzz, SkMatrix* m) {
24 SkScalar mat[9];
25 fuzz->nextN(mat, 9);
26 m->set9(mat);
27 }
28
29 void initGradientParams(Fuzz* fuzz, std::vector<SkColor4f>* colors,
30 std::vector<SkScalar>* pos, SkTileMode* mode) {
31 int count;
32 fuzz->nextRange(&count, 0, MAX_COUNT);
33
34 // Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint"
35 // smaller, which leads to more efficient fuzzing.
36 uint8_t m;
37 fuzz->nextRange(&m, 0, 2);
38 *mode = static_cast<SkTileMode>(m);
39
40 colors->clear();
41 pos ->clear();
42 for (int i = 0; i < count; i++) {
43 SkColor c;
44 SkScalar s;
45 fuzz->next(&c, &s);
46 colors->push_back(SkColor4f::FromColor(c));
47 pos ->push_back(s);
48 }
49 if (count) {
50 std::sort(pos->begin(), pos->end());
51 // The order matters. If count == 1, we want pos == 0.
52 (*pos)[count - 1] = 1;
53 (*pos)[0] = 0;
54 }
55 }
56
57 static void logOptionalMatrix(const char* label, const SkMatrix* m) {
58 if (!m) {
59 return;
60 }
61
62 SkDEBUGF(" %s: [ ", label);
63 for (int i = 0; i < 9; ++i) {
64 SkDEBUGF("%.9g ", m->get(i));
65 }
66 SkDEBUGF("]\n");
67 }
68
69 static void logLinearGradient(const SkPoint pts[2],
70 SkSpan<const SkColor4f> colors,
71 SkSpan<const float> pos,
72 SkTileMode mode,
73 SkGradient::Interpolation interp,
74 const SkMatrix* localMatrix,
75 const SkMatrix* globalMatrix) {
76 if (!FLAGS_verbose) {
77 return;
78 }
79
80 SkDebugf("--- fuzzLinearGradient ---\n");
81 SkDebugf(" pts:\t\t[ (%.9g %.9g) (%.9g %.9g) ]\n",
82 pts[0].x(), pts[0].y(), pts[1].x(), pts[1].y());
83 SkDebugf(" colors:\t[ ");
84 for (auto c : colors) {
85 SkDebugf("{%g %g %g %g} ", c.fR, c.fG, c.fB, c.fA);
86 }
87
88 SkDebugf("]\n pos:\t\t");
89 if (pos.empty()) {
90 SkDebugf("nullptr");
91 } else {
92 SkDebugf("[ ");
93 for (auto p : pos) {
94 SkDebugf("%f ", p);
95 }
96 }
97 SkDebugf("]\n");
98
99 SkDebugf(" mode:\t\t%s\n", SkTileModeToStr(mode));
100 SkDebugf(" inpremul:\t0x%x\n", (unsigned)interp.fInPremul);
101 logOptionalMatrix("local matrix", localMatrix);
102 logOptionalMatrix("global matrix", globalMatrix);
103 }
104
105 static SkGradient::Interpolation fuzz_interp(Fuzz* fuzz) {
106 uint32_t flags;
107 fuzz->next(&flags);
108
109 return {(flags & 1) ? SkGradient::Interpolation::InPremul::kYes
110 : SkGradient::Interpolation::InPremul::kNo};
111 }
112
113 void fuzzLinearGradient(Fuzz* fuzz) {
114 SkPoint pts[2];
115 fuzz->next(&pts[0].fX, &pts[0].fY, &pts[1].fX, &pts[1].fY);
116 bool useLocalMatrix, useGlobalMatrix;
117 fuzz->next(&useLocalMatrix, &useGlobalMatrix);
118
119 std::vector<SkColor4f> colors;
120 std::vector<SkScalar> pos;
121 SkTileMode mode;
122 initGradientParams(fuzz, &colors, &pos, &mode);
123
124 SkPaint p;
125 auto interp = fuzz_interp(fuzz);
126
127 SkTLazy<SkMatrix> localMatrix;
128 if (useLocalMatrix) {
129 makeMatrix(fuzz, localMatrix.init());
130 }
131 p.setShader(SkShaders::LinearGradient(pts, {{colors, pos, mode}, interp},
132 localMatrix.getMaybeNull()));
133
134 sk_sp<SkSurface> surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(50, 50)));
135 if (useGlobalMatrix) {
136 SkMatrix gm;
137 makeMatrix(fuzz, &gm);
138 logLinearGradient(pts, colors, pos, mode, interp, localMatrix.getMaybeNull(), &gm);
139 SkCanvas* c = surface->getCanvas();
140 c->setMatrix(gm);
141 c->drawPaint(p);
142 } else {
143 logLinearGradient(pts, colors, pos, mode, interp, localMatrix.getMaybeNull(), nullptr);
144 surface->getCanvas()->drawPaint(p);
145 }
146 }
147
148 void fuzzRadialGradient(Fuzz* fuzz) {
149 SkPoint center;
150 fuzz->next(&center.fX, &center.fY);
151 SkScalar radius;
152 bool useLocalMatrix, useGlobalMatrix;
153 fuzz->next(&radius, &useLocalMatrix, &useGlobalMatrix);
154
155
156 std::vector<SkColor4f> colors;
157 std::vector<SkScalar> pos;
158 SkTileMode mode;
159 initGradientParams(fuzz, &colors, &pos, &mode);
160
161 SkPaint p;
162 auto interp = fuzz_interp(fuzz);
163
164 SkTLazy<SkMatrix> localMatrix;
165 if (useLocalMatrix) {
166 makeMatrix(fuzz, localMatrix.init());
167 }
168 p.setShader(SkShaders::RadialGradient(center, radius, {{colors, pos, mode}, interp},
169 localMatrix.getMaybeNull()));
170
171 sk_sp<SkSurface> surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(50, 50)));
172 if (useGlobalMatrix) {
173 SkMatrix gm;
174 makeMatrix(fuzz, &gm);
175 SkCanvas* c = surface->getCanvas();
176 c->setMatrix(gm);
177 c->drawPaint(p);
178 } else {
179 surface->getCanvas()->drawPaint(p);
180 }
181 }
182
183 void fuzzTwoPointConicalGradient(Fuzz* fuzz) {
184 SkPoint start;
185 fuzz->next(&start.fX, &start.fY);
186 SkPoint end;
187 fuzz->next(&end.fX, &end.fY);
188 SkScalar startRadius, endRadius;
189 bool useLocalMatrix, useGlobalMatrix;
190 fuzz->next(&startRadius, &endRadius, &useLocalMatrix, &useGlobalMatrix);
191
192 std::vector<SkColor4f> colors;
193 std::vector<float> pos;
194 SkTileMode mode;
195 initGradientParams(fuzz, &colors, &pos, &mode);
196
197 SkPaint p;
198 auto interp = fuzz_interp(fuzz);
199
200 SkTLazy<SkMatrix> localMatrix;
201 if (useLocalMatrix) {
202 makeMatrix(fuzz, localMatrix.init());
203 }
204 p.setShader(SkShaders::TwoPointConicalGradient(start, startRadius, end, endRadius,
205 {{colors, pos, mode}, interp},
206 localMatrix.getMaybeNull()));
207
208 sk_sp<SkSurface> surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(50, 50)));
209 if (useGlobalMatrix) {
210 SkMatrix gm;
211 makeMatrix(fuzz, &gm);
212 SkCanvas* c = surface->getCanvas();
213 c->setMatrix(gm);
214 c->drawPaint(p);
215 } else {
216 surface->getCanvas()->drawPaint(p);
217 }
218 }
219
220 void fuzzSweepGradient(Fuzz* fuzz) {
221 SkScalar cx, cy;
222 bool useLocalMatrix, useGlobalMatrix;
223 fuzz->next(&cx, &cy, &useLocalMatrix, &useGlobalMatrix);
224
225 std::vector<SkColor4f> colors;
226 std::vector<SkScalar> pos;
227 SkTileMode mode;
228 initGradientParams(fuzz, &colors, &pos, &mode);
229
230 SkPaint p;
231 if (useLocalMatrix) {
232 SkMatrix m;
233 makeMatrix(fuzz, &m);
234 auto interp = fuzz_interp(fuzz);
235
236 p.setShader(SkShaders::SweepGradient({cx, cy},
237 {{colors, pos, SkTileMode::kClamp}, interp}, &m));
238 } else {
239 p.setShader(SkShaders::SweepGradient({cx, cy}, {{colors, pos, SkTileMode::kClamp}, {}}));
240 }
241
242 sk_sp<SkSurface> surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(50, 50)));
243 if (useGlobalMatrix) {
244 SkMatrix gm;
245 makeMatrix(fuzz, &gm);
246 SkCanvas* c = surface->getCanvas();
247 c->setMatrix(gm);
248 c->drawPaint(p);
249 } else {
250 surface->getCanvas()->drawPaint(p);
251 }
252 }
253
254 DEF_FUZZ(Gradients, fuzz) {
255 uint8_t i;
256 fuzz->next(&i);
257
258 switch(i) {
259 case 0:
260 SkDEBUGF("LinearGradient\n");
261 fuzzLinearGradient(fuzz);
262 return;
263 case 1:
264 SkDEBUGF("RadialGradient\n");
265 fuzzRadialGradient(fuzz);
266 return;
267 case 2:
268 SkDEBUGF("TwoPointConicalGradient\n");
269 fuzzTwoPointConicalGradient(fuzz);
270 return;
271 }
272 SkDEBUGF("SweepGradient\n");
273 fuzzSweepGradient(fuzz);
274 return;
275 }