blob: fb90c063053ad60afe038a9bb4f4a24bed403603 [file] [log] [blame]
Hal Canary24ac42b2017-02-14 13:35:14 -05001/*
2 * Copyright 2017 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.h"
Hal Canary24ac42b2017-02-14 13:35:14 -05009
10// CORE
11#include "SkCanvas.h"
12#include "SkColorFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050013#include "SkDebugCanvas.h"
14#include "SkDocument.h"
Hal Canary671e4422017-02-27 13:36:38 -050015#include "SkFontMgr.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050016#include "SkImageFilter.h"
17#include "SkMaskFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050018#include "SkNullCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050019#include "SkPathEffect.h"
20#include "SkPictureRecorder.h"
Hal Canary5395c592017-03-08 16:52:18 -050021#include "SkRSXform.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050022#include "SkRegion.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050023#include "SkSurface.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050024#include "SkTypeface.h"
25
26// EFFECTS
Hal Canary5395c592017-03-08 16:52:18 -050027#include "Sk1DPathEffect.h"
28#include "Sk2DPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050029#include "SkAlphaThresholdFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050030#include "SkArcToPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050031#include "SkArithmeticImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050032#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050033#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050034#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050035#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050036#include "SkCornerPathEffect.h"
37#include "SkDashPathEffect.h"
38#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050039#include "SkDisplacementMapEffect.h"
40#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050041#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050042#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050043#include "SkImageSource.h"
44#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050045#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050046#include "SkMagnifierImageFilter.h"
47#include "SkMatrixConvolutionImageFilter.h"
48#include "SkMergeImageFilter.h"
49#include "SkMorphologyImageFilter.h"
50#include "SkOffsetImageFilter.h"
51#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050052#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050053#include "SkPictureImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050054#include "SkRRectsGaussianEdgeMaskFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050055#include "SkTableColorFilter.h"
Mike Reedc090c642017-05-16 10:39:06 -040056#include "SkTextBlob.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050057#include "SkTileImageFilter.h"
58#include "SkXfermodeImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050059
60// SRC
61#include "SkUtils.h"
62
Hal Canary44801ca2017-03-15 11:39:06 -040063#if SK_SUPPORT_GPU
64#include "GrContextFactory.h"
65#endif
66
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050067// MISC
68
69#include <iostream>
70
Hal Canary24ac42b2017-02-14 13:35:14 -050071// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050072// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040073// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050074
Hal Canaryf7005202017-03-10 08:48:28 -050075template <typename T, typename Min, typename Max>
76inline void fuzz_enum_range(Fuzz* fuzz, T* value, Min rmin, Max rmax) {
Hal Canary24ac42b2017-02-14 13:35:14 -050077 using U = skstd::underlying_type_t<T>;
Hal Canaryf7005202017-03-10 08:48:28 -050078 fuzz->nextRange((U*)value, (U)rmin, (U)rmax);
79}
80
Hal Canary1e0138b2017-03-10 13:56:08 -050081// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
82// In fact, all make_fuzz_foo() functions have this potential problem.
83// Use sequence points!
84template <typename T>
85inline T make_fuzz_t(Fuzz* fuzz) {
86 T t;
87 fuzz->next(&t);
88 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050089}
90
Hal Canary1e0138b2017-03-10 13:56:08 -050091// We don't always want to test NaNs and infinities.
Hal Canaryce540ea2017-03-06 08:30:44 -050092static void fuzz_nice_float(Fuzz* fuzz, float* f) {
Hal Canary1e0138b2017-03-10 13:56:08 -050093 float v;
94 fuzz->next(&v);
95 constexpr float kLimit = 1.0e35f; // FLT_MAX?
96 *f = (v == v && v <= kLimit && v >= -kLimit) ? v : 0.0f;
Hal Canaryce540ea2017-03-06 08:30:44 -050097}
Hal Canary24ac42b2017-02-14 13:35:14 -050098
Hal Canaryce540ea2017-03-06 08:30:44 -050099template <typename... Args>
Hal Canary1e0138b2017-03-10 13:56:08 -0500100inline void fuzz_nice_float(Fuzz* fuzz, float* f, Args... rest) {
Hal Canaryce540ea2017-03-06 08:30:44 -0500101 fuzz_nice_float(fuzz, f);
102 fuzz_nice_float(fuzz, rest...);
103}
104
105static void fuzz_path(Fuzz* fuzz, SkPath* path, int maxOps) {
106 if (maxOps < 2) {
107 maxOps = 2;
108 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500109 uint8_t fillType;
110 fuzz->nextRange(&fillType, 0, (uint8_t)SkPath::kInverseEvenOdd_FillType);
111 path->setFillType((SkPath::FillType)fillType);
112 uint8_t numOps;
Hal Canaryce540ea2017-03-06 08:30:44 -0500113 fuzz->nextRange(&numOps, 2, maxOps);
Hal Canary24ac42b2017-02-14 13:35:14 -0500114 for (uint8_t i = 0; i < numOps; ++i) {
115 uint8_t op;
116 fuzz->nextRange(&op, 0, 6);
117 SkScalar a, b, c, d, e, f;
118 switch (op) {
119 case 0:
Hal Canaryce540ea2017-03-06 08:30:44 -0500120 fuzz_nice_float(fuzz, &a, &b);
Hal Canary24ac42b2017-02-14 13:35:14 -0500121 path->moveTo(a, b);
122 break;
123 case 1:
Hal Canaryce540ea2017-03-06 08:30:44 -0500124 fuzz_nice_float(fuzz, &a, &b);
Hal Canary24ac42b2017-02-14 13:35:14 -0500125 path->lineTo(a, b);
126 break;
127 case 2:
Hal Canaryce540ea2017-03-06 08:30:44 -0500128 fuzz_nice_float(fuzz, &a, &b, &c, &d);
Hal Canary24ac42b2017-02-14 13:35:14 -0500129 path->quadTo(a, b, c, d);
130 break;
131 case 3:
Hal Canaryce540ea2017-03-06 08:30:44 -0500132 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
Hal Canary24ac42b2017-02-14 13:35:14 -0500133 path->conicTo(a, b, c, d, e);
134 break;
135 case 4:
Hal Canaryce540ea2017-03-06 08:30:44 -0500136 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e, &f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500137 path->cubicTo(a, b, c, d, e, f);
138 break;
139 case 5:
Hal Canaryce540ea2017-03-06 08:30:44 -0500140 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
Hal Canary24ac42b2017-02-14 13:35:14 -0500141 path->arcTo(a, b, c, d, e);
142 break;
143 case 6:
144 path->close();
145 break;
146 default:
147 break;
148 }
149 }
150}
151
Hal Canarye03c3e52017-03-09 11:33:35 -0500152template <>
153inline void Fuzz::next(SkRegion* region) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500154 uint8_t N;
Hal Canarye03c3e52017-03-09 11:33:35 -0500155 this->nextRange(&N, 0, 10);
Hal Canary24ac42b2017-02-14 13:35:14 -0500156 for (uint8_t i = 0; i < N; ++i) {
157 SkIRect r;
158 uint8_t op;
Hal Canarye03c3e52017-03-09 11:33:35 -0500159 this->next(&r);
Hal Canary24ac42b2017-02-14 13:35:14 -0500160 r.sort();
Hal Canarye03c3e52017-03-09 11:33:35 -0500161 this->nextRange(&op, 0, (uint8_t)SkRegion::kLastOp);
Hal Canary24ac42b2017-02-14 13:35:14 -0500162 if (!region->op(r, (SkRegion::Op)op)) {
163 return;
164 }
165 }
166}
167
Hal Canaryb69c4b82017-03-08 11:02:40 -0500168template <>
169inline void Fuzz::next(SkShader::TileMode* m) {
Hal Canaryf7005202017-03-10 08:48:28 -0500170 fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500171}
172
Hal Canaryb69c4b82017-03-08 11:02:40 -0500173template <>
Hal Canarye03c3e52017-03-09 11:33:35 -0500174inline void Fuzz::next(SkFilterQuality* q) {
Hal Canaryf7005202017-03-10 08:48:28 -0500175 fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
176 SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500177}
178
179template <>
Hal Canaryb69c4b82017-03-08 11:02:40 -0500180inline void Fuzz::next(SkMatrix* m) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500181 constexpr int kArrayLength = 9;
182 SkScalar buffer[kArrayLength];
183 int matrixType;
184 this->nextRange(&matrixType, 0, 4);
185 switch (matrixType) {
186 case 0: // identity
187 *m = SkMatrix::I();
188 return;
189 case 1: // translate
190 this->nextRange(&buffer[0], -4000.0f, 4000.0f);
191 this->nextRange(&buffer[1], -4000.0f, 4000.0f);
192 *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
193 return;
194 case 2: // translate + scale
195 this->nextRange(&buffer[0], -400.0f, 400.0f);
196 this->nextRange(&buffer[1], -400.0f, 400.0f);
197 this->nextRange(&buffer[2], -4000.0f, 4000.0f);
198 this->nextRange(&buffer[3], -4000.0f, 4000.0f);
199 *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
200 m->postTranslate(buffer[2], buffer[3]);
201 return;
202 case 3: // affine
203 this->nextN(buffer, 6);
204 m->setAffine(buffer);
205 return;
206 case 4: // perspective
207 this->nextN(buffer, kArrayLength);
208 m->set9(buffer);
209 return;
210 default:
211 return;
212 }
213}
214
Hal Canaryb69c4b82017-03-08 11:02:40 -0500215template <>
216inline void Fuzz::next(SkRRect* rr) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500217 SkRect r;
218 SkVector radii[4];
219 this->next(&r);
Hal Canary27bece82017-03-07 16:23:20 -0500220 r.sort();
221 for (SkVector& vec : radii) {
222 this->nextRange(&vec.fX, 0.0f, 1.0f);
223 vec.fX *= 0.5f * r.width();
224 this->nextRange(&vec.fY, 0.0f, 1.0f);
225 vec.fY *= 0.5f * r.height();
226 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500227 rr->setRectRadii(r, radii);
Hal Canary24ac42b2017-02-14 13:35:14 -0500228}
229
Hal Canaryb69c4b82017-03-08 11:02:40 -0500230template <>
231inline void Fuzz::next(SkBlendMode* mode) {
Hal Canaryf7005202017-03-10 08:48:28 -0500232 fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500233}
234
Hal Canary1e0138b2017-03-10 13:56:08 -0500235static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -0500236
Hal Canary1e0138b2017-03-10 13:56:08 -0500237static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500238
Hal Canary1e0138b2017-03-10 13:56:08 -0500239static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500240
Hal Canary1e0138b2017-03-10 13:56:08 -0500241static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500242 if (depth <= 0) {
243 return nullptr;
244 }
245 int colorFilterType;
246 fuzz->nextRange(&colorFilterType, 0, 8);
247 switch (colorFilterType) {
248 case 0:
249 return nullptr;
250 case 1: {
251 SkColor color;
252 SkBlendMode mode;
253 fuzz->next(&color, &mode);
254 return SkColorFilter::MakeModeFilter(color, mode);
255 }
256 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500257 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
258 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary27bece82017-03-07 16:23:20 -0500259 return SkColorFilter::MakeComposeFilter(std::move(outer), std::move(inner));
260 }
261 case 3: {
262 SkScalar array[20];
263 fuzz->nextN(array, SK_ARRAY_COUNT(array));
264 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
265 }
266 case 4: {
267 SkColor mul, add;
268 fuzz->next(&mul, &add);
269 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
270 }
271 case 5: {
272 bool grayscale;
273 int invertStyle;
274 float contrast;
275 fuzz->next(&grayscale);
276 fuzz->nextRange(&invertStyle, 0, 2);
277 fuzz->nextRange(&contrast, -1.0f, 1.0f);
278 return SkHighContrastFilter::Make(SkHighContrastConfig(
279 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
280 }
281 case 6:
282 return SkLumaColorFilter::Make();
283 case 7: {
284 uint8_t table[256];
285 fuzz->nextN(table, SK_ARRAY_COUNT(table));
286 return SkTableColorFilter::Make(table);
287 }
288 case 8: {
289 uint8_t tableA[256];
290 uint8_t tableR[256];
291 uint8_t tableG[256];
292 uint8_t tableB[256];
293 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
294 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
295 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
296 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
297 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
298 }
299 }
300 return nullptr;
301}
Hal Canary24ac42b2017-02-14 13:35:14 -0500302
Hal Canary1e0138b2017-03-10 13:56:08 -0500303static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500304 SkScalar totalPos = 0;
305 for (int i = 0; i < colorCount; ++i) {
306 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
307 totalPos += pos[i];
308 }
309 totalPos = 1.0f / totalPos;
310 for (int i = 0; i < colorCount; ++i) {
311 pos[i] *= totalPos;
312 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500313 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500314 pos[colorCount - 1] = 1.0f;
315}
316
Hal Canary1e0138b2017-03-10 13:56:08 -0500317static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500318 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
319 sk_sp<SkColorFilter> colorFilter(nullptr);
320 SkBitmap bitmap;
321 sk_sp<SkImage> img;
322 SkShader::TileMode tmX, tmY;
323 bool useMatrix;
324 SkColor color;
325 SkMatrix matrix;
326 SkBlendMode blendMode;
327 int shaderType;
328 if (depth <= 0) {
329 return nullptr;
330 }
Hal Canary671e4422017-02-27 13:36:38 -0500331 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500332 switch (shaderType) {
333 case 0:
334 return nullptr;
335 case 1:
336 return SkShader::MakeEmptyShader();
337 case 2:
338 fuzz->next(&color);
339 return SkShader::MakeColorShader(color);
340 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500341 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500342 fuzz->next(&tmX, &tmY, &useMatrix);
343 if (useMatrix) {
344 fuzz->next(&matrix);
345 }
346 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
347 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500348 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500349 fuzz->next(&tmX, &tmY, &useMatrix);
350 if (useMatrix) {
351 fuzz->next(&matrix);
352 }
353 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
354 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500355 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500356 fuzz->next(&matrix);
357 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
358 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500359 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
360 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500361 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
362 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500363 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
364 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500365 fuzz->next(&blendMode);
366 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500367 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500368 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500369 bool useTile;
370 SkRect tile;
371 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
372 if (useMatrix) {
373 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500374 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500375 if (useTile) {
376 fuzz->next(&tile);
377 }
378 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
379 useMatrix ? &matrix : nullptr,
380 useTile ? &tile : nullptr);
381 }
Hal Canary671e4422017-02-27 13:36:38 -0500382 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500383 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000384 // Deprecated SkGaussianEdgeShader
385 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500386 case 10: {
387 constexpr int kMaxColors = 12;
388 SkPoint pts[2];
389 SkColor colors[kMaxColors];
390 SkScalar pos[kMaxColors];
391 int colorCount;
392 bool usePos;
393 fuzz->nextN(pts, 2);
394 fuzz->nextRange(&colorCount, 2, kMaxColors);
395 fuzz->nextN(colors, colorCount);
396 fuzz->next(&tmX, &useMatrix, &usePos);
397 if (useMatrix) {
398 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500399 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500400 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500401 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500402 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500403 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
404 tmX, 0, useMatrix ? &matrix : nullptr);
405 }
406 case 11: {
407 constexpr int kMaxColors = 12;
408 SkPoint center;
409 SkScalar radius;
410 int colorCount;
411 bool usePos;
412 SkColor colors[kMaxColors];
413 SkScalar pos[kMaxColors];
414 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
415 fuzz->nextRange(&colorCount, 2, kMaxColors);
416 fuzz->nextN(colors, colorCount);
417 if (useMatrix) {
418 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500419 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500420 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500421 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500422 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500423 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
424 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
425 }
426 case 12: {
427 constexpr int kMaxColors = 12;
428 SkPoint start, end;
429 SkScalar startRadius, endRadius;
430 int colorCount;
431 bool usePos;
432 SkColor colors[kMaxColors];
433 SkScalar pos[kMaxColors];
434 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
435 fuzz->nextRange(&colorCount, 2, kMaxColors);
436 fuzz->nextN(colors, colorCount);
437 if (useMatrix) {
438 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500439 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500440 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500441 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500442 }
443 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
444 usePos ? pos : nullptr, colorCount, tmX, 0,
445 useMatrix ? &matrix : nullptr);
446 }
447 case 13: {
448 constexpr int kMaxColors = 12;
449 SkScalar cx, cy;
450 int colorCount;
451 bool usePos;
452 SkColor colors[kMaxColors];
453 SkScalar pos[kMaxColors];
454 fuzz->next(&cx, &cy, &useMatrix, &usePos);
455 fuzz->nextRange(&colorCount, 2, kMaxColors);
456 fuzz->nextN(colors, colorCount);
457 if (useMatrix) {
458 fuzz->next(&matrix);
459 }
460 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500461 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500462 }
463 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
464 0, useMatrix ? &matrix : nullptr);
465 }
466 case 14: {
467 SkScalar baseFrequencyX, baseFrequencyY, seed;
468 int numOctaves;
469 SkISize tileSize;
470 bool useTileSize, turbulence;
471 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
472 if (useTileSize) {
473 fuzz->next(&tileSize);
474 }
475 fuzz->nextRange(&numOctaves, 2, 7);
476 if (turbulence) {
477 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
478 numOctaves, seed,
479 useTileSize ? &tileSize : nullptr);
480 } else {
481 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
482 numOctaves, seed,
483 useTileSize ? &tileSize : nullptr);
484 }
485 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500486 default:
487 break;
488 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500489 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500490}
491
Hal Canary1e0138b2017-03-10 13:56:08 -0500492static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500493 if (depth <= 0) {
494 return nullptr;
495 }
496 uint8_t pathEffectType;
497 fuzz->nextRange(&pathEffectType, 0, 9);
498 switch (pathEffectType) {
499 case 0: {
500 return nullptr;
501 }
502 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500503 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
504 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500505 return SkPathEffect::MakeSum(std::move(first), std::move(second));
506 }
507 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500508 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
509 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500510 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
511 }
512 case 3: {
513 SkPath path;
514 fuzz_path(fuzz, &path, 20);
515 SkScalar advance, phase;
516 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500517 SkPath1DPathEffect::Style style;
518 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
519 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500520 }
521 case 4: {
522 SkScalar width;
523 SkMatrix matrix;
524 fuzz->next(&width, &matrix);
525 return SkLine2DPathEffect::Make(width, matrix);
526 }
527 case 5: {
528 SkPath path;
529 fuzz_path(fuzz, &path, 20);
530 SkMatrix matrix;
531 fuzz->next(&matrix);
532 return SkPath2DPathEffect::Make(matrix, path);
533 }
534 case 6: {
535 SkScalar radius;
536 fuzz->next(&radius);
537 return SkArcToPathEffect::Make(radius);
538 }
539 case 7: {
540 SkScalar radius;
541 fuzz->next(&radius);
542 return SkCornerPathEffect::Make(radius);
543 }
544 case 8: {
545 SkScalar phase;
546 fuzz->next(&phase);
547 SkScalar intervals[20];
548 int count;
549 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
550 fuzz->nextN(intervals, count);
551 return SkDashPathEffect::Make(intervals, count, phase);
552 }
553 case 9: {
554 SkScalar segLength, dev;
555 uint32_t seed;
556 fuzz->next(&segLength, &dev, &seed);
557 return SkDiscretePathEffect::Make(segLength, dev, seed);
558 }
559 default:
560 SkASSERT(false);
561 return nullptr;
562 }
563}
Hal Canary24ac42b2017-02-14 13:35:14 -0500564
Hal Canary1e0138b2017-03-10 13:56:08 -0500565static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500566 int maskfilterType;
567 fuzz->nextRange(&maskfilterType, 0, 2);
568 switch (maskfilterType) {
569 case 0:
570 return nullptr;
571 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500572 SkBlurStyle blurStyle;
573 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500574 SkScalar sigma;
575 fuzz->next(&sigma);
576 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Hal Canary1e0138b2017-03-10 13:56:08 -0500577 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -0500578 fuzz->next(&occluder);
579 }
580 uint32_t flags;
581 fuzz->nextRange(&flags, 0, 3);
Hal Canaryf7005202017-03-10 08:48:28 -0500582 return SkBlurMaskFilter::Make(blurStyle, sigma, occluder, flags);
Hal Canary5395c592017-03-08 16:52:18 -0500583 }
584 case 2: {
585 SkRRect first, second;
586 SkScalar radius;
587 fuzz->next(&first, &second, &radius);
588 return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
589 }
590 default:
591 SkASSERT(false);
592 return nullptr;
593 }
594}
Hal Canary24ac42b2017-02-14 13:35:14 -0500595
Hal Canary1e0138b2017-03-10 13:56:08 -0500596static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
597 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500598 return nullptr;
599 }
600 auto fontMugger = SkFontMgr::RefDefault();
601 SkASSERT(fontMugger);
602 int familyCount = fontMugger->countFamilies();
603 int i, j;
604 fuzz->nextRange(&i, 0, familyCount - 1);
605 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
606 int styleCount = family->count();
607 fuzz->nextRange(&j, 0, styleCount - 1);
608 return sk_sp<SkTypeface>(family->createTypeface(j));
609}
Hal Canary24ac42b2017-02-14 13:35:14 -0500610
Hal Canarye03c3e52017-03-09 11:33:35 -0500611template <>
612inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
613 SkRect rect;
614 uint8_t flags;
615 this->next(&rect);
616 this->nextRange(&flags, 0, 0xF);
617 *cropRect = SkImageFilter::CropRect(rect, flags);
618}
619
Hal Canary1e0138b2017-03-10 13:56:08 -0500620static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500621
622static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
623 if (depth <= 0) {
624 return nullptr;
625 }
626 uint8_t imageFilterType;
627 fuzz->nextRange(&imageFilterType, 1, 6);
628 SkPoint3 p, q;
629 SkColor lightColor;
630 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
631 sk_sp<SkImageFilter> input;
632 SkImageFilter::CropRect cropRect;
633 bool useCropRect;
634 fuzz->next(&useCropRect);
635 if (useCropRect) {
636 fuzz->next(&cropRect);
637 }
638 switch (imageFilterType) {
639 case 1:
640 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500641 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500642 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
643 std::move(input),
644 useCropRect ? &cropRect : nullptr);
645 case 2:
646 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500647 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500648 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
649 std::move(input),
650 useCropRect ? &cropRect : nullptr);
651 case 3:
652 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500653 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500654 return SkLightingImageFilter::MakeSpotLitDiffuse(
655 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
656 std::move(input), useCropRect ? &cropRect : nullptr);
657 case 4:
658 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500659 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500660 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
661 shininess, std::move(input),
662 useCropRect ? &cropRect : nullptr);
663 case 5:
664 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500665 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500666 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
667 shininess, std::move(input),
668 useCropRect ? &cropRect : nullptr);
669 case 6:
670 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
671 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500672 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500673 return SkLightingImageFilter::MakeSpotLitSpecular(
674 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
675 std::move(input), useCropRect ? &cropRect : nullptr);
676 default:
677 SkASSERT(false);
678 return nullptr;
679 }
680}
681
Hal Canary1e0138b2017-03-10 13:56:08 -0500682static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500683
Hal Canary1e0138b2017-03-10 13:56:08 -0500684static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500685 if (depth <= 0) {
686 return nullptr;
687 }
688 uint8_t imageFilterType;
689 fuzz->nextRange(&imageFilterType, 0, 24);
690 switch (imageFilterType) {
691 case 0:
692 return nullptr;
693 case 1: {
694 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500695 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500696 bool useCropRect;
697 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400698 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500699 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400700 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500701 }
702 return SkImageFilter::MakeBlur(sigmaX, sigmaY, std::move(input),
703 useCropRect ? &cropRect : nullptr);
704 }
705 case 2: {
706 SkMatrix matrix;
707 SkFilterQuality quality;
708 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500709 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500710 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
711 }
712 case 3: {
713 SkRegion region;
714 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500715 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500716 bool useCropRect;
717 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400718 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500719 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400720 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500721 }
722 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
723 useCropRect ? &cropRect : nullptr);
724 }
725 case 4: {
726 float k1, k2, k3, k4;
727 bool enforcePMColor;
728 bool useCropRect;
729 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500730 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
731 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500732 SkImageFilter::CropRect cropRect;
733 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400734 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500735 }
736 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
737 std::move(background), std::move(foreground),
738 useCropRect ? &cropRect : nullptr);
739 }
740 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500741 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
742 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500743 bool useCropRect;
744 SkImageFilter::CropRect cropRect;
745 fuzz->next(&useCropRect);
746 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400747 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500748 }
749 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
750 useCropRect ? &cropRect : nullptr);
751 }
752 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500753 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
754 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500755 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
756 }
757 case 7: {
758 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400759 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
760 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500761 SkScalar scale;
762 bool useCropRect;
763 fuzz->next(&scale, &useCropRect);
764 SkImageFilter::CropRect cropRect;
765 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400766 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500767 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500768 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
769 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500770 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
771 std::move(displacement), std::move(color),
772 useCropRect ? &cropRect : nullptr);
773 }
774 case 8: {
775 SkScalar dx, dy, sigmaX, sigmaY;
776 SkColor color;
777 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500778 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500779 bool useCropRect;
780 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
781 SkImageFilter::CropRect cropRect;
782 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400783 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500784 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500785 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500786 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
787 std::move(input),
788 useCropRect ? &cropRect : nullptr);
789 }
790 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500791 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500792 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500793 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500794 SkRect srcRect, dstRect;
795 SkFilterQuality filterQuality;
796 fuzz->next(&srcRect, &dstRect, &filterQuality);
797 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
798 }
799 case 11:
800 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
801 case 12: {
802 SkRect srcRect;
803 SkScalar inset;
804 bool useCropRect;
805 SkImageFilter::CropRect cropRect;
806 fuzz->next(&srcRect, &inset, &useCropRect);
807 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400808 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500809 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500810 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500811 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
812 useCropRect ? &cropRect : nullptr);
813 }
814 case 13: {
815 constexpr int kMaxKernelSize = 5;
816 int32_t n, m;
817 fuzz->nextRange(&n, 1, kMaxKernelSize);
818 fuzz->nextRange(&m, 1, kMaxKernelSize);
819 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
820 fuzz->nextN(kernel, n * m);
821 int32_t offsetX, offsetY;
822 fuzz->nextRange(&offsetX, 0, n - 1);
823 fuzz->nextRange(&offsetY, 0, m - 1);
824 SkScalar gain, bias;
825 bool convolveAlpha, useCropRect;
826 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
827 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500828 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500829 SkImageFilter::CropRect cropRect;
830 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400831 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500832 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500833 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500834 return SkMatrixConvolutionImageFilter::Make(
835 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
836 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
837 }
838 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500839 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
840 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500841 SkBlendMode blendMode;
842 bool useCropRect;
843 fuzz->next(&useCropRect, &blendMode);
844 SkImageFilter::CropRect cropRect;
845 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400846 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500847 }
848 return SkMergeImageFilter::Make(std::move(first), std::move(second), blendMode,
849 useCropRect ? &cropRect : nullptr);
850 }
851 case 15: {
852 constexpr int kMaxCount = 4;
853 sk_sp<SkImageFilter> ifs[kMaxCount];
854 SkBlendMode blendModes[kMaxCount];
855 int count;
856 fuzz->nextRange(&count, 1, kMaxCount);
857 for (int i = 0; i < count; ++i) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500858 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500859 }
860 fuzz->nextN(blendModes, count);
861 bool useCropRect;
862 fuzz->next(&useCropRect);
863 SkImageFilter::CropRect cropRect;
864 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400865 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500866 }
867 return SkMergeImageFilter::MakeN(ifs, count, blendModes,
868 useCropRect ? &cropRect : nullptr);
869 }
870 case 16: {
871 int rx, ry;
872 fuzz->next(&rx, &ry);
873 bool useCropRect;
874 fuzz->next(&useCropRect);
875 SkImageFilter::CropRect cropRect;
876 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400877 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500878 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500879 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500880 return SkDilateImageFilter::Make(rx, ry, std::move(input),
881 useCropRect ? &cropRect : nullptr);
882 }
883 case 17: {
884 int rx, ry;
885 fuzz->next(&rx, &ry);
886 bool useCropRect;
887 fuzz->next(&useCropRect);
888 SkImageFilter::CropRect cropRect;
889 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400890 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500891 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500892 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500893 return SkErodeImageFilter::Make(rx, ry, std::move(input),
894 useCropRect ? &cropRect : nullptr);
895 }
896 case 18: {
897 SkScalar dx, dy;
898 fuzz->next(&dx, &dy);
899 bool useCropRect;
900 fuzz->next(&useCropRect);
901 SkImageFilter::CropRect cropRect;
902 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400903 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500904 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500905 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500906 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
907 useCropRect ? &cropRect : nullptr);
908 }
909 case 19: {
910 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500911 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500912 bool useCropRect;
913 fuzz->next(&useCropRect);
914 SkImageFilter::CropRect cropRect;
915 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400916 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500917 }
918 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
919 }
920 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500921 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500922 return SkPictureImageFilter::Make(std::move(picture));
923 }
924 case 21: {
925 SkRect cropRect;
926 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500927 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500928 return SkPictureImageFilter::Make(std::move(picture), cropRect);
929 }
930 case 22: {
931 SkRect cropRect;
932 SkFilterQuality filterQuality;
933 fuzz->next(&cropRect, &filterQuality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500934 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500935 return SkPictureImageFilter::MakeForLocalSpace(std::move(picture), cropRect,
936 filterQuality);
937 }
938 case 23: {
939 SkRect src, dst;
940 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500941 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500942 return SkTileImageFilter::Make(src, dst, std::move(input));
943 }
944 case 24: {
945 SkBlendMode blendMode;
946 bool useCropRect;
947 fuzz->next(&useCropRect, &blendMode);
948 SkImageFilter::CropRect cropRect;
949 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400950 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500951 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500952 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
953 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500954 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
955 useCropRect ? &cropRect : nullptr);
956 }
957 default:
958 SkASSERT(false);
959 return nullptr;
960 }
961}
Hal Canary24ac42b2017-02-14 13:35:14 -0500962
Hal Canary1e0138b2017-03-10 13:56:08 -0500963static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500964 int w, h;
965 fuzz->nextRange(&w, 1, 1024);
966 fuzz->nextRange(&h, 1, 1024);
967 SkAutoTMalloc<SkPMColor> data(w * h);
968 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
969 int n = w * h;
970 for (int i = 0; i < n; ++i) {
971 SkColor c;
972 fuzz->next(&c);
973 data[i] = SkPreMultiplyColor(c);
974 }
975 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500976 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
977 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500978}
979
Hal Canary1e0138b2017-03-10 13:56:08 -0500980static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500981 SkBitmap bitmap;
982 int w, h;
983 fuzz->nextRange(&w, 1, 1024);
984 fuzz->nextRange(&h, 1, 1024);
985 bitmap.allocN32Pixels(w, h);
Hal Canary24ac42b2017-02-14 13:35:14 -0500986 for (int y = 0; y < h; ++y) {
987 for (int x = 0; x < w; ++x) {
988 SkColor c;
989 fuzz->next(&c);
990 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
991 }
992 }
993 return bitmap;
994}
995
Hal Canary1e0138b2017-03-10 13:56:08 -0500996template <typename T, typename Min, typename Max>
997inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
998 T value;
999 fuzz_enum_range(fuzz, &value, minv, maxv);
1000 return value;
1001}
1002
1003static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001004 if (!fuzz || !paint || depth <= 0) {
1005 return;
1006 }
1007
Hal Canary1e0138b2017-03-10 13:56:08 -05001008 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
1009 paint->setDither( make_fuzz_t<bool>(fuzz));
1010 paint->setColor( make_fuzz_t<SkColor>(fuzz));
1011 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
1012 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
1013 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
1014 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
1015 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
1016 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
1017 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
1018 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -05001019
1020 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001021 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
1022 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
1023 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
1024 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -05001025 }
1026}
1027
Hal Canary1e0138b2017-03-10 13:56:08 -05001028static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
1029 paint->setTypeface( make_fuzz_typeface(fuzz));
1030 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
1031 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
1032 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
1033 paint->setLinearText( make_fuzz_t<bool>(fuzz));
1034 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
1035 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
1036 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
1037 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
1038 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
1039 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
1040 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
1041 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
1042 SkPaint::kFull_Hinting));
1043 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -05001044}
1045
1046static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001047 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -05001048}
1049
Hal Canary5395c592017-03-08 16:52:18 -05001050constexpr int kMaxGlyphCount = 30;
1051
Hal Canary1e0138b2017-03-10 13:56:08 -05001052static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -05001053 SkTDArray<uint8_t> array;
1054 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001055 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
1056 : SkTypeface::MakeDefault()->countGlyphs();
Hal Canary671e4422017-02-27 13:36:38 -05001057 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -05001058 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001059 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
1060 for (int i = 0; i < glyphCount; ++i) {
1061 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
1062 }
1063 return array;
1064 }
1065 static const SkUnichar ranges[][2] = {
1066 {0x0020, 0x007F},
1067 {0x00A1, 0x0250},
1068 {0x0400, 0x0500},
1069 };
1070 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001071 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001072 count += (ranges[i][1] - ranges[i][0]);
1073 }
Hal Canary5395c592017-03-08 16:52:18 -05001074 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -05001075 SkUnichar buffer[kMaxLength];
1076 int length;
1077 fuzz->nextRange(&length, 1, kMaxLength);
1078 for (int j = 0; j < length; ++j) {
1079 int32_t value;
1080 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001081 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001082 if (value + ranges[i][0] < ranges[i][1]) {
1083 buffer[j] = value + ranges[i][0];
1084 break;
1085 } else {
1086 value -= (ranges[i][1] - ranges[i][0]);
1087 }
1088 }
1089 }
1090 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001091 case SkPaint::kUTF8_TextEncoding: {
1092 size_t utf8len = 0;
1093 for (int j = 0; j < length; ++j) {
1094 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001095 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001096 char* ptr = (char*)array.append(utf8len);
1097 for (int j = 0; j < length; ++j) {
1098 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001099 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001100 } break;
1101 case SkPaint::kUTF16_TextEncoding: {
1102 size_t utf16len = 0;
1103 for (int j = 0; j < length; ++j) {
1104 utf16len += SkUTF16_FromUnichar(buffer[j]);
1105 }
1106 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1107 for (int j = 0; j < length; ++j) {
1108 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1109 }
1110 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001111 case SkPaint::kUTF32_TextEncoding:
1112 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001113 break;
Hal Canary671e4422017-02-27 13:36:38 -05001114 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001115 SkASSERT(false);
Hal Canary671e4422017-02-27 13:36:38 -05001116 }
1117 return array;
1118}
1119
Hal Canary5395c592017-03-08 16:52:18 -05001120static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1121 SkTextBlobBuilder textBlobBuilder;
1122 int8_t runCount;
1123 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1124 while (runCount-- > 0) {
1125 SkPaint paint;
1126 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001127 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001128 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1129 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1130 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1131 SkASSERT(glyphCount <= kMaxGlyphCount);
1132 SkScalar x, y;
1133 const SkTextBlobBuilder::RunBuffer* buffer;
1134 uint8_t runType;
1135 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1136 switch (runType) {
1137 case 0:
1138 fuzz->next(&x, &y);
1139 // TODO: Test other variations of this.
1140 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1141 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1142 break;
1143 case 1:
1144 fuzz->next(&y);
1145 // TODO: Test other variations of this.
1146 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1147 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1148 fuzz->nextN(buffer->pos, glyphCount);
1149 break;
1150 case 2:
1151 // TODO: Test other variations of this.
1152 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1153 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1154 fuzz->nextN(buffer->pos, glyphCount * 2);
1155 break;
1156 default:
1157 SkASSERT(false);
1158 }
1159 }
1160 return textBlobBuilder.make();
1161}
1162
Hal Canary1e0138b2017-03-10 13:56:08 -05001163static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001164 if (!fuzz || !canvas || depth <= 0) {
1165 return;
1166 }
1167 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1168 unsigned N;
1169 fuzz->nextRange(&N, 0, 2000);
1170 for (unsigned i = 0; i < N; ++i) {
1171 if (fuzz->exhausted()) {
1172 return;
1173 }
1174 SkPaint paint;
1175 SkMatrix matrix;
1176 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001177 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001178 switch (drawCommand) {
1179 case 0:
1180 canvas->flush();
1181 break;
1182 case 1:
1183 canvas->save();
1184 break;
1185 case 2: {
1186 SkRect bounds;
1187 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001188 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001189 canvas->saveLayer(&bounds, &paint);
1190 break;
1191 }
1192 case 3: {
1193 SkRect bounds;
1194 fuzz->next(&bounds);
1195 canvas->saveLayer(&bounds, nullptr);
1196 break;
1197 }
1198 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001199 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001200 canvas->saveLayer(nullptr, &paint);
1201 break;
1202 case 5:
1203 canvas->saveLayer(nullptr, nullptr);
1204 break;
1205 case 6: {
1206 uint8_t alpha;
1207 fuzz->next(&alpha);
1208 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1209 break;
1210 }
1211 case 7: {
1212 SkRect bounds;
1213 uint8_t alpha;
1214 fuzz->next(&bounds, &alpha);
1215 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1216 break;
1217 }
1218 case 8: {
1219 SkCanvas::SaveLayerRec saveLayerRec;
1220 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001221 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001222 fuzz->next(&bounds);
1223 saveLayerRec.fBounds = &bounds;
1224 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001225 if (make_fuzz_t<bool>(fuzz)) {
1226 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001227 saveLayerRec.fPaint = &paint;
1228 }
1229 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001230 if (make_fuzz_t<bool>(fuzz)) {
1231 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001232 saveLayerRec.fBackdrop = imageFilter.get();
1233 }
Hal Canary5395c592017-03-08 16:52:18 -05001234 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001235 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001236 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kIsOpaque_SaveLayerFlag;
1237 // }
Hal Canary1e0138b2017-03-10 13:56:08 -05001238 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001239 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1240 // }
1241
Hal Canary24ac42b2017-02-14 13:35:14 -05001242 canvas->saveLayer(saveLayerRec);
1243 break;
1244 }
1245 case 9:
1246 canvas->restore();
1247 break;
1248 case 10: {
1249 int saveCount;
1250 fuzz->next(&saveCount);
1251 canvas->restoreToCount(saveCount);
1252 break;
1253 }
1254 case 11: {
1255 SkScalar x, y;
1256 fuzz->next(&x, &y);
1257 canvas->translate(x, y);
1258 break;
1259 }
1260 case 12: {
1261 SkScalar x, y;
1262 fuzz->next(&x, &y);
1263 canvas->scale(x, y);
1264 break;
1265 }
1266 case 13: {
1267 SkScalar v;
1268 fuzz->next(&v);
1269 canvas->rotate(v);
1270 break;
1271 }
1272 case 14: {
1273 SkScalar x, y, v;
1274 fuzz->next(&x, &y, &v);
1275 canvas->rotate(v, x, y);
1276 break;
1277 }
1278 case 15: {
1279 SkScalar x, y;
1280 fuzz->next(&x, &y);
1281 canvas->skew(x, y);
1282 break;
1283 }
1284 case 16: {
1285 SkMatrix mat;
1286 fuzz->next(&mat);
1287 canvas->concat(mat);
1288 break;
1289 }
1290 case 17: {
1291 SkMatrix mat;
1292 fuzz->next(&mat);
1293 canvas->setMatrix(mat);
1294 break;
1295 }
1296 case 18:
1297 canvas->resetMatrix();
1298 break;
1299 case 19: {
1300 SkRect r;
1301 int op;
1302 bool doAntiAlias;
1303 fuzz->next(&r, &doAntiAlias);
1304 fuzz->nextRange(&op, 0, 1);
1305 r.sort();
1306 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1307 break;
1308 }
1309 case 20: {
1310 SkRRect rr;
1311 int op;
1312 bool doAntiAlias;
1313 fuzz->next(&rr);
1314 fuzz->next(&doAntiAlias);
1315 fuzz->nextRange(&op, 0, 1);
1316 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1317 break;
1318 }
1319 case 21: {
1320 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001321 fuzz_path(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001322 int op;
1323 bool doAntiAlias;
1324 fuzz->next(&doAntiAlias);
1325 fuzz->nextRange(&op, 0, 1);
1326 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1327 break;
1328 }
1329 case 22: {
1330 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001331 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001332 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001333 fuzz->nextRange(&op, 0, 1);
1334 canvas->clipRegion(region, (SkClipOp)op);
1335 break;
1336 }
1337 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001338 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001339 canvas->drawPaint(paint);
1340 break;
1341 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001342 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001343 uint8_t pointMode;
1344 fuzz->nextRange(&pointMode, 0, 3);
1345 size_t count;
1346 constexpr int kMaxCount = 30;
1347 fuzz->nextRange(&count, 0, kMaxCount);
1348 SkPoint pts[kMaxCount];
1349 fuzz->nextN(pts, count);
1350 canvas->drawPoints((SkCanvas::PointMode)pointMode, count, pts, paint);
1351 break;
1352 }
1353 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001354 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001355 SkRect r;
1356 fuzz->next(&r);
1357 canvas->drawRect(r, paint);
1358 break;
1359 }
1360 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001363 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001364 canvas->drawRegion(region, paint);
1365 break;
1366 }
1367 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001368 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001369 SkRect r;
1370 fuzz->next(&r);
1371 canvas->drawOval(r, paint);
1372 break;
1373 }
1374 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001375 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001376 SkRRect rr;
1377 fuzz->next(&rr);
1378 canvas->drawRRect(rr, paint);
1379 break;
1380 }
1381 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001382 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001383 SkRRect orr, irr;
1384 fuzz->next(&orr);
1385 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001386 if (orr.getBounds().contains(irr.getBounds())) {
1387 canvas->drawDRRect(orr, irr, paint);
1388 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001389 break;
1390 }
1391 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001392 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001393 SkRect r;
1394 SkScalar start, sweep;
1395 bool useCenter;
1396 fuzz->next(&r, &start, &sweep, &useCenter);
1397 canvas->drawArc(r, start, sweep, useCenter, paint);
1398 break;
1399 }
1400 case 32: {
1401 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001402 fuzz_path(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001403 canvas->drawPath(path, paint);
1404 break;
1405 }
1406 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001407 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001408 SkScalar left, top;
1409 bool usePaint;
1410 fuzz->next(&left, &top, &usePaint);
1411 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001412 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001413 }
1414 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1415 break;
1416 }
1417 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001418 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001419 SkRect src, dst;
1420 bool usePaint;
1421 fuzz->next(&src, &dst, &usePaint);
1422 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001423 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001424 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001425 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001426 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1427 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001428 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1429 break;
1430 }
1431 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001432 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001433 SkIRect src;
1434 SkRect dst;
1435 bool usePaint;
1436 fuzz->next(&src, &dst, &usePaint);
1437 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001438 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001439 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001440 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001441 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1442 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001443 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1444 break;
1445 }
1446 case 36: {
1447 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001448 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001449 SkRect dst;
1450 fuzz->next(&dst, &usePaint);
1451 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001452 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001453 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001454 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001455 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1456 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001457 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1458 break;
1459 }
1460 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001461 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001462 SkIRect center;
1463 SkRect dst;
1464 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001465 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001466 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001467 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001468 }
Hal Canary0361d492017-03-15 12:58:15 -04001469 if (make_fuzz_t<bool>(fuzz)) {
1470 fuzz->next(&center);
1471 } else { // Make valid center, see SkLatticeIter::Valid().
1472 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1473 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1474 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1475 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1476 }
1477 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001478 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1479 break;
1480 }
1481 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001482 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001483 SkScalar left, top;
1484 bool usePaint;
1485 fuzz->next(&left, &top, &usePaint);
1486 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001487 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001488 }
1489 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1490 break;
1491 }
1492 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001493 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001494 SkRect src, dst;
1495 bool usePaint;
1496 fuzz->next(&src, &dst, &usePaint);
1497 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001498 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001499 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001500 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001501 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1502 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001503 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1504 break;
1505 }
1506 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001507 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001508 SkIRect src;
1509 SkRect dst;
1510 bool usePaint;
1511 fuzz->next(&src, &dst, &usePaint);
1512 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001513 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001514 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001515 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001516 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1517 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001518 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1519 break;
1520 }
1521 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001522 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001523 SkRect dst;
1524 bool usePaint;
1525 fuzz->next(&dst, &usePaint);
1526 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001527 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001528 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001529 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001530 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1531 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001532 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1533 break;
1534 }
1535 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001536 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001537 SkIRect center;
1538 SkRect dst;
1539 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001540 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001541 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001542 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001543 }
Hal Canary0361d492017-03-15 12:58:15 -04001544 if (make_fuzz_t<bool>(fuzz)) {
1545 fuzz->next(&center);
1546 } else { // Make valid center, see SkLatticeIter::Valid().
1547 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1548 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1549 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1550 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1551 }
1552 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001553 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1554 break;
1555 }
1556 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001557 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001558 bool usePaint;
1559 SkRect dst;
1560 fuzz->next(&usePaint, &dst);
1561 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001562 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001563 }
1564 constexpr int kMax = 6;
1565 int xDivs[kMax], yDivs[kMax];
1566 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr};
1567 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1568 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1569 fuzz->nextN(xDivs, lattice.fXCount);
1570 fuzz->nextN(yDivs, lattice.fYCount);
1571 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1572 break;
1573 }
1574 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001575 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001576 bool usePaint;
1577 SkRect dst;
1578 fuzz->next(&usePaint, &dst);
1579 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001580 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001581 }
1582 constexpr int kMax = 6;
1583 int xDivs[kMax], yDivs[kMax];
1584 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr};
1585 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1586 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1587 fuzz->nextN(xDivs, lattice.fXCount);
1588 fuzz->nextN(yDivs, lattice.fYCount);
1589 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1590 break;
1591 }
1592 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001593 fuzz_paint(fuzz, &paint, depth - 1);
1594 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001595 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001596 SkScalar x, y;
1597 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001598 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001599 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001600 break;
1601 }
1602 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001603 fuzz_paint(fuzz, &paint, depth - 1);
1604 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001605 fuzz_paint_text_encoding(fuzz, &paint);
1606 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001607 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001608 if (glyphCount < 1) {
1609 break;
1610 }
1611 SkAutoTMalloc<SkPoint> pos(glyphCount);
1612 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001613 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001614 pos[0] = {0, 0};
1615 for (int i = 1; i < glyphCount; ++i) {
1616 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001617 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001618 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1619 }
Hal Canary671e4422017-02-27 13:36:38 -05001620 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001621 break;
1622 }
1623 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001624 fuzz_paint(fuzz, &paint, depth - 1);
1625 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001626 fuzz_paint_text_encoding(fuzz, &paint);
1627 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001628 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001629 SkAutoTMalloc<SkScalar> widths(glyphCount);
1630 if (glyphCount < 1) {
1631 break;
1632 }
Hal Canary671e4422017-02-27 13:36:38 -05001633 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001634 SkScalar x = widths[0];
1635 for (int i = 0; i < glyphCount; ++i) {
1636 SkTSwap(x, widths[i]);
1637 x += widths[i];
1638 SkScalar offset;
1639 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001640 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001641 widths[i] += offset;
1642 }
1643 SkScalar y;
1644 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001645 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001646 break;
1647 }
1648 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001649 fuzz_paint(fuzz, &paint, depth - 1);
1650 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001651 fuzz_paint_text_encoding(fuzz, &paint);
1652 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001653 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001654 fuzz_path(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001655 SkScalar hOffset, vOffset;
1656 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001657 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1658 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001659 break;
1660 }
1661 case 49: {
1662 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001663 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001664 if (useMatrix) {
1665 fuzz->next(&matrix);
1666 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001667 fuzz_paint(fuzz, &paint, depth - 1);
1668 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001669 fuzz_paint_text_encoding(fuzz, &paint);
1670 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001671 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001672 fuzz_path(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001673 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001674 useMatrix ? &matrix : nullptr, paint);
1675 break;
1676 }
1677 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001678 fuzz_paint(fuzz, &paint, depth - 1);
1679 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001680 fuzz_paint_text_encoding(fuzz, &paint);
1681 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1682 SkRSXform rSXform[kMaxGlyphCount];
1683 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1684 SkASSERT(glyphCount <= kMaxGlyphCount);
1685 fuzz->nextN(rSXform, glyphCount);
1686 SkRect cullRect;
1687 bool useCullRect;
1688 fuzz->next(&useCullRect);
1689 if (useCullRect) {
1690 fuzz->next(&cullRect);
1691 }
1692 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1693 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001694 break;
1695 }
1696 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001697 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001698 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001699 SkScalar x, y;
1700 fuzz->next(&x, &y);
1701 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001702 break;
1703 }
1704 case 52: {
1705 bool usePaint, useMatrix;
1706 fuzz->next(&usePaint, &useMatrix);
1707 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001708 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001709 }
1710 if (useMatrix) {
1711 fuzz->next(&matrix);
1712 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001713 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001714 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1715 usePaint ? &paint : nullptr);
1716 break;
1717 }
1718 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001719 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001720 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001721 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001722 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001723 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001724 constexpr int kMaxCount = 100;
1725 int vertexCount;
1726 SkPoint vertices[kMaxCount];
1727 SkPoint texs[kMaxCount];
1728 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001729 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1730 fuzz->nextN(vertices, vertexCount);
1731 bool useTexs, useColors;
1732 fuzz->next(&useTexs, &useColors);
1733 if (useTexs) {
1734 fuzz->nextN(texs, vertexCount);
1735 }
1736 if (useColors) {
1737 fuzz->nextN(colors, vertexCount);
1738 }
1739 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001740 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001741 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001742 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1743 for (int i = 0; i < indexCount; ++i) {
1744 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1745 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001746 }
Mike Reed887cdf12017-04-03 11:11:09 -04001747 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1748 useTexs ? texs : nullptr,
1749 useColors ? colors : nullptr,
1750 indexCount, indices),
1751 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001752 break;
1753 }
1754 default:
1755 break;
1756 }
1757 }
1758}
1759
Hal Canary1e0138b2017-03-10 13:56:08 -05001760static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001761 SkScalar w, h;
1762 fuzz->next(&w, &h);
1763 SkPictureRecorder pictureRecorder;
1764 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1765 return pictureRecorder.finishRecordingAsPicture();
1766}
1767
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001768DEF_FUZZ(NullCanvas, fuzz) {
1769 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001770}
1771
Hal Canary44801ca2017-03-15 11:39:06 -04001772// 8.5x11 letter paper at 72ppi.
1773constexpr SkISize kCanvasSize = {612, 792};
1774
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001775DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001776 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001777 SkASSERT(surface && surface->getCanvas());
1778 fuzz_canvas(fuzz, surface->getCanvas());
1779}
1780
Hal Canary44801ca2017-03-15 11:39:06 -04001781#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001782static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1783 SkASSERT(context);
1784 auto surface = SkSurface::MakeRenderTarget(
1785 context,
1786 SkBudgeted::kNo,
1787 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1788 SkASSERT(surface && surface->getCanvas());
1789 fuzz_canvas(fuzz, surface->getCanvas());
1790}
1791
Hal Canary44801ca2017-03-15 11:39:06 -04001792DEF_FUZZ(NativeGLCanvas, fuzz) {
Brian Salomon6405e712017-03-20 08:54:16 -04001793 GrContext* context = sk_gpu_test::GrContextFactory().get(
1794 sk_gpu_test::GrContextFactory::kGL_ContextType);
1795 if (!context) {
1796 context = sk_gpu_test::GrContextFactory().get(
1797 sk_gpu_test::GrContextFactory::kGLES_ContextType);
1798 }
Hal Canary5aa91582017-03-21 11:11:44 -07001799 fuzz_ganesh(fuzz, context);
1800}
1801
1802DEF_FUZZ(NullGLCanvas, fuzz) {
1803 fuzz_ganesh(fuzz, sk_gpu_test::GrContextFactory().get(
1804 sk_gpu_test::GrContextFactory::kNullGL_ContextType));
1805}
1806
1807DEF_FUZZ(DebugGLCanvas, fuzz) {
1808 fuzz_ganesh(fuzz, sk_gpu_test::GrContextFactory().get(
1809 sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001810}
1811#endif
1812
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001813DEF_FUZZ(PDFCanvas, fuzz) {
1814 struct final : public SkWStream {
1815 bool write(const void*, size_t n) override { fN += n; return true; }
1816 size_t bytesWritten() const override { return fN; }
1817 size_t fN = 0;
1818 } stream;
1819 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001820 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1821 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001822}
1823
1824// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1825DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001826 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001827 fuzz_canvas(fuzz, &debugCanvas);
1828 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1829 UrlDataManager dataManager(SkString("data"));
1830 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1831 Json::StyledStreamWriter(" ").write(std::cout, json);
1832}