blob: 5bedf9123ba1de81e4fae9d25f333bbbfc930a94 [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"
Mike Reed54518ac2017-07-22 08:29:48 -040021#include "SkPoint3.h"
Hal Canary5395c592017-03-08 16:52:18 -050022#include "SkRSXform.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050023#include "SkRegion.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050024#include "SkSurface.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050025#include "SkTypeface.h"
Kevin Lubickedef8ec2018-01-09 15:32:58 -050026#include "SkOSFile.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050027
28// EFFECTS
Hal Canary5395c592017-03-08 16:52:18 -050029#include "Sk1DPathEffect.h"
30#include "Sk2DPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050031#include "SkAlphaThresholdFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050032#include "SkArithmeticImageFilter.h"
Robert Phillips70e3e9a2017-06-26 14:22:01 -040033#include "SkBlurImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050034#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050035#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050036#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050037#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050038#include "SkCornerPathEffect.h"
39#include "SkDashPathEffect.h"
40#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050041#include "SkDisplacementMapEffect.h"
42#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050043#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050044#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050045#include "SkImageSource.h"
46#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050047#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050048#include "SkMagnifierImageFilter.h"
49#include "SkMatrixConvolutionImageFilter.h"
50#include "SkMergeImageFilter.h"
51#include "SkMorphologyImageFilter.h"
52#include "SkOffsetImageFilter.h"
53#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050054#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050055#include "SkPictureImageFilter.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050056#include "SkReadBuffer.h"
Hal Canary5395c592017-03-08 16:52:18 -050057#include "SkRRectsGaussianEdgeMaskFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050058#include "SkTableColorFilter.h"
Mike Reedc090c642017-05-16 10:39:06 -040059#include "SkTextBlob.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050060#include "SkTileImageFilter.h"
61#include "SkXfermodeImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050062
63// SRC
64#include "SkUtils.h"
65
Hal Canary44801ca2017-03-15 11:39:06 -040066#if SK_SUPPORT_GPU
67#include "GrContextFactory.h"
68#endif
69
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050070// MISC
71
72#include <iostream>
73
Hal Canary24ac42b2017-02-14 13:35:14 -050074// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050075// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040076// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050077
Hal Canaryf7005202017-03-10 08:48:28 -050078template <typename T, typename Min, typename Max>
79inline void fuzz_enum_range(Fuzz* fuzz, T* value, Min rmin, Max rmax) {
Hal Canary24ac42b2017-02-14 13:35:14 -050080 using U = skstd::underlying_type_t<T>;
Hal Canaryf7005202017-03-10 08:48:28 -050081 fuzz->nextRange((U*)value, (U)rmin, (U)rmax);
82}
83
Hal Canary1e0138b2017-03-10 13:56:08 -050084// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
85// In fact, all make_fuzz_foo() functions have this potential problem.
86// Use sequence points!
87template <typename T>
88inline T make_fuzz_t(Fuzz* fuzz) {
89 T t;
90 fuzz->next(&t);
91 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050092}
93
Hal Canary1e0138b2017-03-10 13:56:08 -050094// We don't always want to test NaNs and infinities.
Hal Canaryce540ea2017-03-06 08:30:44 -050095static void fuzz_nice_float(Fuzz* fuzz, float* f) {
Hal Canary1e0138b2017-03-10 13:56:08 -050096 float v;
97 fuzz->next(&v);
98 constexpr float kLimit = 1.0e35f; // FLT_MAX?
99 *f = (v == v && v <= kLimit && v >= -kLimit) ? v : 0.0f;
Hal Canaryce540ea2017-03-06 08:30:44 -0500100}
Hal Canary24ac42b2017-02-14 13:35:14 -0500101
Hal Canaryce540ea2017-03-06 08:30:44 -0500102template <typename... Args>
Hal Canary1e0138b2017-03-10 13:56:08 -0500103inline void fuzz_nice_float(Fuzz* fuzz, float* f, Args... rest) {
Hal Canaryce540ea2017-03-06 08:30:44 -0500104 fuzz_nice_float(fuzz, f);
105 fuzz_nice_float(fuzz, rest...);
106}
107
108static void fuzz_path(Fuzz* fuzz, SkPath* path, int maxOps) {
109 if (maxOps < 2) {
110 maxOps = 2;
111 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500112 uint8_t fillType;
113 fuzz->nextRange(&fillType, 0, (uint8_t)SkPath::kInverseEvenOdd_FillType);
114 path->setFillType((SkPath::FillType)fillType);
115 uint8_t numOps;
Hal Canaryce540ea2017-03-06 08:30:44 -0500116 fuzz->nextRange(&numOps, 2, maxOps);
Hal Canary24ac42b2017-02-14 13:35:14 -0500117 for (uint8_t i = 0; i < numOps; ++i) {
118 uint8_t op;
119 fuzz->nextRange(&op, 0, 6);
120 SkScalar a, b, c, d, e, f;
121 switch (op) {
122 case 0:
Hal Canaryce540ea2017-03-06 08:30:44 -0500123 fuzz_nice_float(fuzz, &a, &b);
Hal Canary24ac42b2017-02-14 13:35:14 -0500124 path->moveTo(a, b);
125 break;
126 case 1:
Hal Canaryce540ea2017-03-06 08:30:44 -0500127 fuzz_nice_float(fuzz, &a, &b);
Hal Canary24ac42b2017-02-14 13:35:14 -0500128 path->lineTo(a, b);
129 break;
130 case 2:
Hal Canaryce540ea2017-03-06 08:30:44 -0500131 fuzz_nice_float(fuzz, &a, &b, &c, &d);
Hal Canary24ac42b2017-02-14 13:35:14 -0500132 path->quadTo(a, b, c, d);
133 break;
134 case 3:
Hal Canaryce540ea2017-03-06 08:30:44 -0500135 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
Hal Canary24ac42b2017-02-14 13:35:14 -0500136 path->conicTo(a, b, c, d, e);
137 break;
138 case 4:
Hal Canaryce540ea2017-03-06 08:30:44 -0500139 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e, &f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500140 path->cubicTo(a, b, c, d, e, f);
141 break;
142 case 5:
Hal Canaryce540ea2017-03-06 08:30:44 -0500143 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
Hal Canary24ac42b2017-02-14 13:35:14 -0500144 path->arcTo(a, b, c, d, e);
145 break;
146 case 6:
147 path->close();
148 break;
149 default:
150 break;
151 }
152 }
153}
154
Hal Canarye03c3e52017-03-09 11:33:35 -0500155template <>
156inline void Fuzz::next(SkRegion* region) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500157 uint8_t N;
Hal Canarye03c3e52017-03-09 11:33:35 -0500158 this->nextRange(&N, 0, 10);
Hal Canary24ac42b2017-02-14 13:35:14 -0500159 for (uint8_t i = 0; i < N; ++i) {
160 SkIRect r;
161 uint8_t op;
Hal Canarye03c3e52017-03-09 11:33:35 -0500162 this->next(&r);
Hal Canary24ac42b2017-02-14 13:35:14 -0500163 r.sort();
Hal Canarye03c3e52017-03-09 11:33:35 -0500164 this->nextRange(&op, 0, (uint8_t)SkRegion::kLastOp);
Hal Canary24ac42b2017-02-14 13:35:14 -0500165 if (!region->op(r, (SkRegion::Op)op)) {
166 return;
167 }
168 }
169}
170
Hal Canaryb69c4b82017-03-08 11:02:40 -0500171template <>
172inline void Fuzz::next(SkShader::TileMode* m) {
Hal Canaryf7005202017-03-10 08:48:28 -0500173 fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500174}
175
Hal Canaryb69c4b82017-03-08 11:02:40 -0500176template <>
Hal Canarye03c3e52017-03-09 11:33:35 -0500177inline void Fuzz::next(SkFilterQuality* q) {
Hal Canaryf7005202017-03-10 08:48:28 -0500178 fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
179 SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500180}
181
182template <>
Hal Canaryb69c4b82017-03-08 11:02:40 -0500183inline void Fuzz::next(SkMatrix* m) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500184 constexpr int kArrayLength = 9;
185 SkScalar buffer[kArrayLength];
186 int matrixType;
187 this->nextRange(&matrixType, 0, 4);
188 switch (matrixType) {
189 case 0: // identity
190 *m = SkMatrix::I();
191 return;
192 case 1: // translate
193 this->nextRange(&buffer[0], -4000.0f, 4000.0f);
194 this->nextRange(&buffer[1], -4000.0f, 4000.0f);
195 *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
196 return;
197 case 2: // translate + scale
198 this->nextRange(&buffer[0], -400.0f, 400.0f);
199 this->nextRange(&buffer[1], -400.0f, 400.0f);
200 this->nextRange(&buffer[2], -4000.0f, 4000.0f);
201 this->nextRange(&buffer[3], -4000.0f, 4000.0f);
202 *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
203 m->postTranslate(buffer[2], buffer[3]);
204 return;
205 case 3: // affine
206 this->nextN(buffer, 6);
207 m->setAffine(buffer);
208 return;
209 case 4: // perspective
210 this->nextN(buffer, kArrayLength);
211 m->set9(buffer);
212 return;
213 default:
214 return;
215 }
216}
217
Hal Canaryb69c4b82017-03-08 11:02:40 -0500218template <>
219inline void Fuzz::next(SkRRect* rr) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500220 SkRect r;
221 SkVector radii[4];
222 this->next(&r);
Hal Canary27bece82017-03-07 16:23:20 -0500223 r.sort();
224 for (SkVector& vec : radii) {
225 this->nextRange(&vec.fX, 0.0f, 1.0f);
226 vec.fX *= 0.5f * r.width();
227 this->nextRange(&vec.fY, 0.0f, 1.0f);
228 vec.fY *= 0.5f * r.height();
229 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500230 rr->setRectRadii(r, radii);
Hal Canary24ac42b2017-02-14 13:35:14 -0500231}
232
Hal Canaryb69c4b82017-03-08 11:02:40 -0500233template <>
234inline void Fuzz::next(SkBlendMode* mode) {
Hal Canaryf7005202017-03-10 08:48:28 -0500235 fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500236}
237
Hal Canary1e0138b2017-03-10 13:56:08 -0500238static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -0500239
Hal Canary1e0138b2017-03-10 13:56:08 -0500240static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500241
Hal Canary1e0138b2017-03-10 13:56:08 -0500242static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500243
Hal Canary1e0138b2017-03-10 13:56:08 -0500244static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500245 if (depth <= 0) {
246 return nullptr;
247 }
248 int colorFilterType;
249 fuzz->nextRange(&colorFilterType, 0, 8);
250 switch (colorFilterType) {
251 case 0:
252 return nullptr;
253 case 1: {
254 SkColor color;
255 SkBlendMode mode;
256 fuzz->next(&color, &mode);
257 return SkColorFilter::MakeModeFilter(color, mode);
258 }
259 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500260 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
261 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary27bece82017-03-07 16:23:20 -0500262 return SkColorFilter::MakeComposeFilter(std::move(outer), std::move(inner));
263 }
264 case 3: {
265 SkScalar array[20];
266 fuzz->nextN(array, SK_ARRAY_COUNT(array));
267 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
268 }
269 case 4: {
270 SkColor mul, add;
271 fuzz->next(&mul, &add);
272 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
273 }
274 case 5: {
275 bool grayscale;
276 int invertStyle;
277 float contrast;
278 fuzz->next(&grayscale);
279 fuzz->nextRange(&invertStyle, 0, 2);
280 fuzz->nextRange(&contrast, -1.0f, 1.0f);
281 return SkHighContrastFilter::Make(SkHighContrastConfig(
282 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
283 }
284 case 6:
285 return SkLumaColorFilter::Make();
286 case 7: {
287 uint8_t table[256];
288 fuzz->nextN(table, SK_ARRAY_COUNT(table));
289 return SkTableColorFilter::Make(table);
290 }
291 case 8: {
292 uint8_t tableA[256];
293 uint8_t tableR[256];
294 uint8_t tableG[256];
295 uint8_t tableB[256];
296 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
297 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
298 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
299 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
300 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
301 }
302 }
303 return nullptr;
304}
Hal Canary24ac42b2017-02-14 13:35:14 -0500305
Hal Canary1e0138b2017-03-10 13:56:08 -0500306static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500307 SkScalar totalPos = 0;
308 for (int i = 0; i < colorCount; ++i) {
309 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
310 totalPos += pos[i];
311 }
312 totalPos = 1.0f / totalPos;
313 for (int i = 0; i < colorCount; ++i) {
314 pos[i] *= totalPos;
315 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500316 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500317 pos[colorCount - 1] = 1.0f;
318}
319
Hal Canary1e0138b2017-03-10 13:56:08 -0500320static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500321 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
322 sk_sp<SkColorFilter> colorFilter(nullptr);
323 SkBitmap bitmap;
324 sk_sp<SkImage> img;
325 SkShader::TileMode tmX, tmY;
326 bool useMatrix;
327 SkColor color;
328 SkMatrix matrix;
329 SkBlendMode blendMode;
330 int shaderType;
331 if (depth <= 0) {
332 return nullptr;
333 }
Hal Canary671e4422017-02-27 13:36:38 -0500334 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500335 switch (shaderType) {
336 case 0:
337 return nullptr;
338 case 1:
339 return SkShader::MakeEmptyShader();
340 case 2:
341 fuzz->next(&color);
342 return SkShader::MakeColorShader(color);
343 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500344 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500345 fuzz->next(&tmX, &tmY, &useMatrix);
346 if (useMatrix) {
347 fuzz->next(&matrix);
348 }
349 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
350 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500351 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500352 fuzz->next(&tmX, &tmY, &useMatrix);
353 if (useMatrix) {
354 fuzz->next(&matrix);
355 }
356 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
357 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500358 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500359 fuzz->next(&matrix);
360 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
361 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500362 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
363 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500364 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
365 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500366 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
367 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500368 fuzz->next(&blendMode);
369 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500370 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500371 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500372 bool useTile;
373 SkRect tile;
374 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
375 if (useMatrix) {
376 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500377 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500378 if (useTile) {
379 fuzz->next(&tile);
380 }
381 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
382 useMatrix ? &matrix : nullptr,
383 useTile ? &tile : nullptr);
384 }
Hal Canary671e4422017-02-27 13:36:38 -0500385 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500386 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000387 // Deprecated SkGaussianEdgeShader
388 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500389 case 10: {
390 constexpr int kMaxColors = 12;
391 SkPoint pts[2];
392 SkColor colors[kMaxColors];
393 SkScalar pos[kMaxColors];
394 int colorCount;
395 bool usePos;
396 fuzz->nextN(pts, 2);
397 fuzz->nextRange(&colorCount, 2, kMaxColors);
398 fuzz->nextN(colors, colorCount);
399 fuzz->next(&tmX, &useMatrix, &usePos);
400 if (useMatrix) {
401 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500402 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500403 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500404 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500405 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500406 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
407 tmX, 0, useMatrix ? &matrix : nullptr);
408 }
409 case 11: {
410 constexpr int kMaxColors = 12;
411 SkPoint center;
412 SkScalar radius;
413 int colorCount;
414 bool usePos;
415 SkColor colors[kMaxColors];
416 SkScalar pos[kMaxColors];
417 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
418 fuzz->nextRange(&colorCount, 2, kMaxColors);
419 fuzz->nextN(colors, colorCount);
420 if (useMatrix) {
421 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500422 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500423 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500424 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500425 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500426 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
427 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
428 }
429 case 12: {
430 constexpr int kMaxColors = 12;
431 SkPoint start, end;
432 SkScalar startRadius, endRadius;
433 int colorCount;
434 bool usePos;
435 SkColor colors[kMaxColors];
436 SkScalar pos[kMaxColors];
437 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
438 fuzz->nextRange(&colorCount, 2, kMaxColors);
439 fuzz->nextN(colors, colorCount);
440 if (useMatrix) {
441 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500442 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500443 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500444 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500445 }
446 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
447 usePos ? pos : nullptr, colorCount, tmX, 0,
448 useMatrix ? &matrix : nullptr);
449 }
450 case 13: {
451 constexpr int kMaxColors = 12;
452 SkScalar cx, cy;
453 int colorCount;
454 bool usePos;
455 SkColor colors[kMaxColors];
456 SkScalar pos[kMaxColors];
457 fuzz->next(&cx, &cy, &useMatrix, &usePos);
458 fuzz->nextRange(&colorCount, 2, kMaxColors);
459 fuzz->nextN(colors, colorCount);
460 if (useMatrix) {
461 fuzz->next(&matrix);
462 }
463 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500464 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500465 }
466 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
467 0, useMatrix ? &matrix : nullptr);
468 }
469 case 14: {
470 SkScalar baseFrequencyX, baseFrequencyY, seed;
471 int numOctaves;
472 SkISize tileSize;
473 bool useTileSize, turbulence;
474 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
475 if (useTileSize) {
476 fuzz->next(&tileSize);
477 }
478 fuzz->nextRange(&numOctaves, 2, 7);
479 if (turbulence) {
480 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
481 numOctaves, seed,
482 useTileSize ? &tileSize : nullptr);
483 } else {
484 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
485 numOctaves, seed,
486 useTileSize ? &tileSize : nullptr);
487 }
488 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500489 default:
490 break;
491 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500492 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500493}
494
Hal Canary1e0138b2017-03-10 13:56:08 -0500495static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500496 if (depth <= 0) {
497 return nullptr;
498 }
499 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400500 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500501 switch (pathEffectType) {
502 case 0: {
503 return nullptr;
504 }
505 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500506 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
507 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500508 return SkPathEffect::MakeSum(std::move(first), std::move(second));
509 }
510 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500511 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
512 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500513 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
514 }
515 case 3: {
516 SkPath path;
517 fuzz_path(fuzz, &path, 20);
518 SkScalar advance, phase;
519 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500520 SkPath1DPathEffect::Style style;
521 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
522 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500523 }
524 case 4: {
525 SkScalar width;
526 SkMatrix matrix;
527 fuzz->next(&width, &matrix);
528 return SkLine2DPathEffect::Make(width, matrix);
529 }
530 case 5: {
531 SkPath path;
532 fuzz_path(fuzz, &path, 20);
533 SkMatrix matrix;
534 fuzz->next(&matrix);
535 return SkPath2DPathEffect::Make(matrix, path);
536 }
537 case 6: {
538 SkScalar radius;
539 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500540 return SkCornerPathEffect::Make(radius);
541 }
Mike Reed40e7e652017-07-22 22:12:59 -0400542 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500543 SkScalar phase;
544 fuzz->next(&phase);
545 SkScalar intervals[20];
546 int count;
547 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
548 fuzz->nextN(intervals, count);
549 return SkDashPathEffect::Make(intervals, count, phase);
550 }
Mike Reed40e7e652017-07-22 22:12:59 -0400551 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500552 SkScalar segLength, dev;
553 uint32_t seed;
554 fuzz->next(&segLength, &dev, &seed);
555 return SkDiscretePathEffect::Make(segLength, dev, seed);
556 }
557 default:
558 SkASSERT(false);
559 return nullptr;
560 }
561}
Hal Canary24ac42b2017-02-14 13:35:14 -0500562
Hal Canary1e0138b2017-03-10 13:56:08 -0500563static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500564 int maskfilterType;
565 fuzz->nextRange(&maskfilterType, 0, 2);
566 switch (maskfilterType) {
567 case 0:
568 return nullptr;
569 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500570 SkBlurStyle blurStyle;
571 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500572 SkScalar sigma;
573 fuzz->next(&sigma);
574 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Hal Canary1e0138b2017-03-10 13:56:08 -0500575 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -0500576 fuzz->next(&occluder);
577 }
578 uint32_t flags;
579 fuzz->nextRange(&flags, 0, 3);
Hal Canaryf7005202017-03-10 08:48:28 -0500580 return SkBlurMaskFilter::Make(blurStyle, sigma, occluder, flags);
Hal Canary5395c592017-03-08 16:52:18 -0500581 }
582 case 2: {
583 SkRRect first, second;
584 SkScalar radius;
585 fuzz->next(&first, &second, &radius);
586 return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
587 }
588 default:
589 SkASSERT(false);
590 return nullptr;
591 }
592}
Hal Canary24ac42b2017-02-14 13:35:14 -0500593
Hal Canary1e0138b2017-03-10 13:56:08 -0500594static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
595 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500596 return nullptr;
597 }
598 auto fontMugger = SkFontMgr::RefDefault();
599 SkASSERT(fontMugger);
600 int familyCount = fontMugger->countFamilies();
601 int i, j;
602 fuzz->nextRange(&i, 0, familyCount - 1);
603 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
604 int styleCount = family->count();
605 fuzz->nextRange(&j, 0, styleCount - 1);
606 return sk_sp<SkTypeface>(family->createTypeface(j));
607}
Hal Canary24ac42b2017-02-14 13:35:14 -0500608
Hal Canarye03c3e52017-03-09 11:33:35 -0500609template <>
610inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
611 SkRect rect;
612 uint8_t flags;
613 this->next(&rect);
614 this->nextRange(&flags, 0, 0xF);
615 *cropRect = SkImageFilter::CropRect(rect, flags);
616}
617
Hal Canary1e0138b2017-03-10 13:56:08 -0500618static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500619
620static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
621 if (depth <= 0) {
622 return nullptr;
623 }
624 uint8_t imageFilterType;
625 fuzz->nextRange(&imageFilterType, 1, 6);
626 SkPoint3 p, q;
627 SkColor lightColor;
628 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
629 sk_sp<SkImageFilter> input;
630 SkImageFilter::CropRect cropRect;
631 bool useCropRect;
632 fuzz->next(&useCropRect);
633 if (useCropRect) {
634 fuzz->next(&cropRect);
635 }
636 switch (imageFilterType) {
637 case 1:
638 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500639 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500640 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
641 std::move(input),
642 useCropRect ? &cropRect : nullptr);
643 case 2:
644 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500645 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500646 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
647 std::move(input),
648 useCropRect ? &cropRect : nullptr);
649 case 3:
650 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500651 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500652 return SkLightingImageFilter::MakeSpotLitDiffuse(
653 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
654 std::move(input), useCropRect ? &cropRect : nullptr);
655 case 4:
656 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500657 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500658 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
659 shininess, std::move(input),
660 useCropRect ? &cropRect : nullptr);
661 case 5:
662 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500663 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500664 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
665 shininess, std::move(input),
666 useCropRect ? &cropRect : nullptr);
667 case 6:
668 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
669 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500670 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500671 return SkLightingImageFilter::MakeSpotLitSpecular(
672 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
673 std::move(input), useCropRect ? &cropRect : nullptr);
674 default:
675 SkASSERT(false);
676 return nullptr;
677 }
678}
679
Hal Canary1e0138b2017-03-10 13:56:08 -0500680static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500681
Hal Canary1e0138b2017-03-10 13:56:08 -0500682static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500683 if (depth <= 0) {
684 return nullptr;
685 }
686 uint8_t imageFilterType;
687 fuzz->nextRange(&imageFilterType, 0, 24);
688 switch (imageFilterType) {
689 case 0:
690 return nullptr;
691 case 1: {
692 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500693 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500694 bool useCropRect;
695 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400696 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500697 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400698 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500699 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400700 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500701 useCropRect ? &cropRect : nullptr);
702 }
703 case 2: {
704 SkMatrix matrix;
705 SkFilterQuality quality;
706 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500707 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500708 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
709 }
710 case 3: {
711 SkRegion region;
712 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500713 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500714 bool useCropRect;
715 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400716 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500717 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400718 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500719 }
720 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
721 useCropRect ? &cropRect : nullptr);
722 }
723 case 4: {
724 float k1, k2, k3, k4;
725 bool enforcePMColor;
726 bool useCropRect;
727 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500728 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
729 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500730 SkImageFilter::CropRect cropRect;
731 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400732 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500733 }
734 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
735 std::move(background), std::move(foreground),
736 useCropRect ? &cropRect : nullptr);
737 }
738 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500739 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
740 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500741 bool useCropRect;
742 SkImageFilter::CropRect cropRect;
743 fuzz->next(&useCropRect);
744 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400745 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500746 }
747 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
748 useCropRect ? &cropRect : nullptr);
749 }
750 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500751 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
752 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500753 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
754 }
755 case 7: {
756 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400757 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
758 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500759 SkScalar scale;
760 bool useCropRect;
761 fuzz->next(&scale, &useCropRect);
762 SkImageFilter::CropRect cropRect;
763 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400764 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500765 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500766 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
767 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500768 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
769 std::move(displacement), std::move(color),
770 useCropRect ? &cropRect : nullptr);
771 }
772 case 8: {
773 SkScalar dx, dy, sigmaX, sigmaY;
774 SkColor color;
775 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500776 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500777 bool useCropRect;
778 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
779 SkImageFilter::CropRect cropRect;
780 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400781 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500782 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500783 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500784 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
785 std::move(input),
786 useCropRect ? &cropRect : nullptr);
787 }
788 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500789 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500790 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500791 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500792 SkRect srcRect, dstRect;
793 SkFilterQuality filterQuality;
794 fuzz->next(&srcRect, &dstRect, &filterQuality);
795 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
796 }
797 case 11:
798 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
799 case 12: {
800 SkRect srcRect;
801 SkScalar inset;
802 bool useCropRect;
803 SkImageFilter::CropRect cropRect;
804 fuzz->next(&srcRect, &inset, &useCropRect);
805 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400806 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500807 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500808 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500809 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
810 useCropRect ? &cropRect : nullptr);
811 }
812 case 13: {
813 constexpr int kMaxKernelSize = 5;
814 int32_t n, m;
815 fuzz->nextRange(&n, 1, kMaxKernelSize);
816 fuzz->nextRange(&m, 1, kMaxKernelSize);
817 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
818 fuzz->nextN(kernel, n * m);
819 int32_t offsetX, offsetY;
820 fuzz->nextRange(&offsetX, 0, n - 1);
821 fuzz->nextRange(&offsetY, 0, m - 1);
822 SkScalar gain, bias;
823 bool convolveAlpha, useCropRect;
824 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
825 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500826 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500827 SkImageFilter::CropRect cropRect;
828 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400829 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500830 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500831 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500832 return SkMatrixConvolutionImageFilter::Make(
833 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
834 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
835 }
836 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500837 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
838 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500839 bool useCropRect;
840 fuzz->next(&useCropRect);
841 SkImageFilter::CropRect cropRect;
842 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400843 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500844 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400845 return SkMergeImageFilter::Make(std::move(first), std::move(second),
846 useCropRect ? &cropRect : nullptr);
847 }
848 case 15: {
849 constexpr int kMaxCount = 4;
850 sk_sp<SkImageFilter> ifs[kMaxCount];
851 int count;
852 fuzz->nextRange(&count, 1, kMaxCount);
853 for (int i = 0; i < count; ++i) {
854 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
855 }
856 bool useCropRect;
857 fuzz->next(&useCropRect);
858 SkImageFilter::CropRect cropRect;
859 if (useCropRect) {
860 fuzz->next(&cropRect);
861 }
862 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500863 }
864 case 16: {
865 int rx, ry;
866 fuzz->next(&rx, &ry);
867 bool useCropRect;
868 fuzz->next(&useCropRect);
869 SkImageFilter::CropRect cropRect;
870 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400871 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500872 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500873 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500874 return SkDilateImageFilter::Make(rx, ry, std::move(input),
875 useCropRect ? &cropRect : nullptr);
876 }
877 case 17: {
878 int rx, ry;
879 fuzz->next(&rx, &ry);
880 bool useCropRect;
881 fuzz->next(&useCropRect);
882 SkImageFilter::CropRect cropRect;
883 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400884 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500885 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500886 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500887 return SkErodeImageFilter::Make(rx, ry, std::move(input),
888 useCropRect ? &cropRect : nullptr);
889 }
890 case 18: {
891 SkScalar dx, dy;
892 fuzz->next(&dx, &dy);
893 bool useCropRect;
894 fuzz->next(&useCropRect);
895 SkImageFilter::CropRect cropRect;
896 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400897 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500898 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500899 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500900 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
901 useCropRect ? &cropRect : nullptr);
902 }
903 case 19: {
904 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500905 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500906 bool useCropRect;
907 fuzz->next(&useCropRect);
908 SkImageFilter::CropRect cropRect;
909 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400910 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500911 }
912 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
913 }
914 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500915 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500916 return SkPictureImageFilter::Make(std::move(picture));
917 }
918 case 21: {
919 SkRect cropRect;
920 fuzz->next(&cropRect);
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), cropRect);
923 }
924 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500925 SkRect src, dst;
926 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500927 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500928 return SkTileImageFilter::Make(src, dst, std::move(input));
929 }
Mike Reed77e487d2017-11-09 21:50:20 +0000930 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500931 SkBlendMode blendMode;
932 bool useCropRect;
933 fuzz->next(&useCropRect, &blendMode);
934 SkImageFilter::CropRect cropRect;
935 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400936 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500937 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500938 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
939 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500940 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
941 useCropRect ? &cropRect : nullptr);
942 }
943 default:
944 SkASSERT(false);
945 return nullptr;
946 }
947}
Hal Canary24ac42b2017-02-14 13:35:14 -0500948
Hal Canary1e0138b2017-03-10 13:56:08 -0500949static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500950 int w, h;
951 fuzz->nextRange(&w, 1, 1024);
952 fuzz->nextRange(&h, 1, 1024);
953 SkAutoTMalloc<SkPMColor> data(w * h);
954 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
955 int n = w * h;
956 for (int i = 0; i < n; ++i) {
957 SkColor c;
958 fuzz->next(&c);
959 data[i] = SkPreMultiplyColor(c);
960 }
961 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500962 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
963 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500964}
965
Hal Canary1e0138b2017-03-10 13:56:08 -0500966static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500967 SkBitmap bitmap;
968 int w, h;
969 fuzz->nextRange(&w, 1, 1024);
970 fuzz->nextRange(&h, 1, 1024);
971 bitmap.allocN32Pixels(w, h);
Hal Canary24ac42b2017-02-14 13:35:14 -0500972 for (int y = 0; y < h; ++y) {
973 for (int x = 0; x < w; ++x) {
974 SkColor c;
975 fuzz->next(&c);
976 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
977 }
978 }
979 return bitmap;
980}
981
Hal Canary1e0138b2017-03-10 13:56:08 -0500982template <typename T, typename Min, typename Max>
983inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
984 T value;
985 fuzz_enum_range(fuzz, &value, minv, maxv);
986 return value;
987}
988
989static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500990 if (!fuzz || !paint || depth <= 0) {
991 return;
992 }
993
Hal Canary1e0138b2017-03-10 13:56:08 -0500994 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
995 paint->setDither( make_fuzz_t<bool>(fuzz));
996 paint->setColor( make_fuzz_t<SkColor>(fuzz));
997 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
998 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
999 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
1000 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
1001 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
1002 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
1003 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
1004 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -05001005
1006 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001007 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
1008 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
1009 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
1010 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -05001011 }
1012}
1013
Hal Canary1e0138b2017-03-10 13:56:08 -05001014static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
1015 paint->setTypeface( make_fuzz_typeface(fuzz));
1016 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
1017 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
1018 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
1019 paint->setLinearText( make_fuzz_t<bool>(fuzz));
1020 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
1021 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
1022 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
1023 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
1024 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
1025 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
1026 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
1027 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
1028 SkPaint::kFull_Hinting));
1029 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -05001030}
1031
1032static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001033 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -05001034}
1035
Hal Canary5395c592017-03-08 16:52:18 -05001036constexpr int kMaxGlyphCount = 30;
1037
Hal Canary1e0138b2017-03-10 13:56:08 -05001038static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -05001039 SkTDArray<uint8_t> array;
1040 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001041 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
1042 : SkTypeface::MakeDefault()->countGlyphs();
Hal Canary671e4422017-02-27 13:36:38 -05001043 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -05001044 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001045 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
1046 for (int i = 0; i < glyphCount; ++i) {
1047 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
1048 }
1049 return array;
1050 }
1051 static const SkUnichar ranges[][2] = {
1052 {0x0020, 0x007F},
1053 {0x00A1, 0x0250},
1054 {0x0400, 0x0500},
1055 };
1056 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001057 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001058 count += (ranges[i][1] - ranges[i][0]);
1059 }
Hal Canary5395c592017-03-08 16:52:18 -05001060 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -05001061 SkUnichar buffer[kMaxLength];
1062 int length;
1063 fuzz->nextRange(&length, 1, kMaxLength);
1064 for (int j = 0; j < length; ++j) {
1065 int32_t value;
1066 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001067 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001068 if (value + ranges[i][0] < ranges[i][1]) {
1069 buffer[j] = value + ranges[i][0];
1070 break;
1071 } else {
1072 value -= (ranges[i][1] - ranges[i][0]);
1073 }
1074 }
1075 }
1076 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001077 case SkPaint::kUTF8_TextEncoding: {
1078 size_t utf8len = 0;
1079 for (int j = 0; j < length; ++j) {
1080 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001081 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001082 char* ptr = (char*)array.append(utf8len);
1083 for (int j = 0; j < length; ++j) {
1084 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001085 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001086 } break;
1087 case SkPaint::kUTF16_TextEncoding: {
1088 size_t utf16len = 0;
1089 for (int j = 0; j < length; ++j) {
1090 utf16len += SkUTF16_FromUnichar(buffer[j]);
1091 }
1092 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1093 for (int j = 0; j < length; ++j) {
1094 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1095 }
1096 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001097 case SkPaint::kUTF32_TextEncoding:
1098 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001099 break;
Hal Canary671e4422017-02-27 13:36:38 -05001100 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001101 SkASSERT(false);
Hal Canary671e4422017-02-27 13:36:38 -05001102 }
1103 return array;
1104}
1105
Hal Canary5395c592017-03-08 16:52:18 -05001106static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1107 SkTextBlobBuilder textBlobBuilder;
1108 int8_t runCount;
1109 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1110 while (runCount-- > 0) {
1111 SkPaint paint;
1112 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001113 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001114 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1115 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1116 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1117 SkASSERT(glyphCount <= kMaxGlyphCount);
1118 SkScalar x, y;
1119 const SkTextBlobBuilder::RunBuffer* buffer;
1120 uint8_t runType;
1121 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1122 switch (runType) {
1123 case 0:
1124 fuzz->next(&x, &y);
1125 // TODO: Test other variations of this.
1126 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1127 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1128 break;
1129 case 1:
1130 fuzz->next(&y);
1131 // TODO: Test other variations of this.
1132 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1133 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1134 fuzz->nextN(buffer->pos, glyphCount);
1135 break;
1136 case 2:
1137 // TODO: Test other variations of this.
1138 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1139 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1140 fuzz->nextN(buffer->pos, glyphCount * 2);
1141 break;
1142 default:
1143 SkASSERT(false);
1144 }
1145 }
1146 return textBlobBuilder.make();
1147}
1148
Hal Canary1e0138b2017-03-10 13:56:08 -05001149static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001150 if (!fuzz || !canvas || depth <= 0) {
1151 return;
1152 }
1153 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1154 unsigned N;
1155 fuzz->nextRange(&N, 0, 2000);
1156 for (unsigned i = 0; i < N; ++i) {
1157 if (fuzz->exhausted()) {
1158 return;
1159 }
1160 SkPaint paint;
1161 SkMatrix matrix;
1162 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001163 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001164 switch (drawCommand) {
1165 case 0:
1166 canvas->flush();
1167 break;
1168 case 1:
1169 canvas->save();
1170 break;
1171 case 2: {
1172 SkRect bounds;
1173 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001174 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001175 canvas->saveLayer(&bounds, &paint);
1176 break;
1177 }
1178 case 3: {
1179 SkRect bounds;
1180 fuzz->next(&bounds);
1181 canvas->saveLayer(&bounds, nullptr);
1182 break;
1183 }
1184 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001185 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001186 canvas->saveLayer(nullptr, &paint);
1187 break;
1188 case 5:
1189 canvas->saveLayer(nullptr, nullptr);
1190 break;
1191 case 6: {
1192 uint8_t alpha;
1193 fuzz->next(&alpha);
1194 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1195 break;
1196 }
1197 case 7: {
1198 SkRect bounds;
1199 uint8_t alpha;
1200 fuzz->next(&bounds, &alpha);
1201 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1202 break;
1203 }
1204 case 8: {
1205 SkCanvas::SaveLayerRec saveLayerRec;
1206 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001207 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001208 fuzz->next(&bounds);
1209 saveLayerRec.fBounds = &bounds;
1210 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001211 if (make_fuzz_t<bool>(fuzz)) {
1212 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001213 saveLayerRec.fPaint = &paint;
1214 }
1215 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001216 if (make_fuzz_t<bool>(fuzz)) {
1217 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001218 saveLayerRec.fBackdrop = imageFilter.get();
1219 }
Hal Canary5395c592017-03-08 16:52:18 -05001220 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001221 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001222 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kIsOpaque_SaveLayerFlag;
1223 // }
Hal Canary1e0138b2017-03-10 13:56:08 -05001224 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001225 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1226 // }
1227
Hal Canary24ac42b2017-02-14 13:35:14 -05001228 canvas->saveLayer(saveLayerRec);
1229 break;
1230 }
1231 case 9:
1232 canvas->restore();
1233 break;
1234 case 10: {
1235 int saveCount;
1236 fuzz->next(&saveCount);
1237 canvas->restoreToCount(saveCount);
1238 break;
1239 }
1240 case 11: {
1241 SkScalar x, y;
1242 fuzz->next(&x, &y);
1243 canvas->translate(x, y);
1244 break;
1245 }
1246 case 12: {
1247 SkScalar x, y;
1248 fuzz->next(&x, &y);
1249 canvas->scale(x, y);
1250 break;
1251 }
1252 case 13: {
1253 SkScalar v;
1254 fuzz->next(&v);
1255 canvas->rotate(v);
1256 break;
1257 }
1258 case 14: {
1259 SkScalar x, y, v;
1260 fuzz->next(&x, &y, &v);
1261 canvas->rotate(v, x, y);
1262 break;
1263 }
1264 case 15: {
1265 SkScalar x, y;
1266 fuzz->next(&x, &y);
1267 canvas->skew(x, y);
1268 break;
1269 }
1270 case 16: {
1271 SkMatrix mat;
1272 fuzz->next(&mat);
1273 canvas->concat(mat);
1274 break;
1275 }
1276 case 17: {
1277 SkMatrix mat;
1278 fuzz->next(&mat);
1279 canvas->setMatrix(mat);
1280 break;
1281 }
1282 case 18:
1283 canvas->resetMatrix();
1284 break;
1285 case 19: {
1286 SkRect r;
1287 int op;
1288 bool doAntiAlias;
1289 fuzz->next(&r, &doAntiAlias);
1290 fuzz->nextRange(&op, 0, 1);
1291 r.sort();
1292 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1293 break;
1294 }
1295 case 20: {
1296 SkRRect rr;
1297 int op;
1298 bool doAntiAlias;
1299 fuzz->next(&rr);
1300 fuzz->next(&doAntiAlias);
1301 fuzz->nextRange(&op, 0, 1);
1302 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1303 break;
1304 }
1305 case 21: {
1306 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001307 fuzz_path(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001308 int op;
1309 bool doAntiAlias;
1310 fuzz->next(&doAntiAlias);
1311 fuzz->nextRange(&op, 0, 1);
1312 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1313 break;
1314 }
1315 case 22: {
1316 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001317 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001318 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001319 fuzz->nextRange(&op, 0, 1);
1320 canvas->clipRegion(region, (SkClipOp)op);
1321 break;
1322 }
1323 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001324 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001325 canvas->drawPaint(paint);
1326 break;
1327 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001328 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001329 SkCanvas::PointMode pointMode;
1330 fuzz_enum_range(fuzz, &pointMode,
1331 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001332 size_t count;
1333 constexpr int kMaxCount = 30;
1334 fuzz->nextRange(&count, 0, kMaxCount);
1335 SkPoint pts[kMaxCount];
1336 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001337 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001338 break;
1339 }
1340 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001341 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001342 SkRect r;
1343 fuzz->next(&r);
1344 canvas->drawRect(r, paint);
1345 break;
1346 }
1347 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001348 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001349 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001350 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001351 canvas->drawRegion(region, paint);
1352 break;
1353 }
1354 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001356 SkRect r;
1357 fuzz->next(&r);
1358 canvas->drawOval(r, paint);
1359 break;
1360 }
1361 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001362 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001363 SkRRect rr;
1364 fuzz->next(&rr);
1365 canvas->drawRRect(rr, paint);
1366 break;
1367 }
1368 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001369 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001370 SkRRect orr, irr;
1371 fuzz->next(&orr);
1372 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001373 if (orr.getBounds().contains(irr.getBounds())) {
1374 canvas->drawDRRect(orr, irr, paint);
1375 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001376 break;
1377 }
1378 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001379 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001380 SkRect r;
1381 SkScalar start, sweep;
1382 bool useCenter;
1383 fuzz->next(&r, &start, &sweep, &useCenter);
1384 canvas->drawArc(r, start, sweep, useCenter, paint);
1385 break;
1386 }
1387 case 32: {
1388 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001389 fuzz_path(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001390 canvas->drawPath(path, paint);
1391 break;
1392 }
1393 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001394 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001395 SkScalar left, top;
1396 bool usePaint;
1397 fuzz->next(&left, &top, &usePaint);
1398 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001399 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001400 }
1401 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1402 break;
1403 }
1404 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001405 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001406 SkRect src, dst;
1407 bool usePaint;
1408 fuzz->next(&src, &dst, &usePaint);
1409 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001410 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001411 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001412 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001413 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1414 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001415 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1416 break;
1417 }
1418 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001419 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001420 SkIRect src;
1421 SkRect dst;
1422 bool usePaint;
1423 fuzz->next(&src, &dst, &usePaint);
1424 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001425 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001426 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001427 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001428 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1429 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001430 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1431 break;
1432 }
1433 case 36: {
1434 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001435 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001436 SkRect dst;
1437 fuzz->next(&dst, &usePaint);
1438 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001439 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001440 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001441 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001442 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1443 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001444 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1445 break;
1446 }
1447 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001448 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001449 SkIRect center;
1450 SkRect dst;
1451 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001452 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001453 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001454 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001455 }
Hal Canary0361d492017-03-15 12:58:15 -04001456 if (make_fuzz_t<bool>(fuzz)) {
1457 fuzz->next(&center);
1458 } else { // Make valid center, see SkLatticeIter::Valid().
1459 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1460 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1461 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1462 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1463 }
1464 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001465 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1466 break;
1467 }
1468 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001469 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001470 SkScalar left, top;
1471 bool usePaint;
1472 fuzz->next(&left, &top, &usePaint);
1473 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001474 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001475 }
1476 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1477 break;
1478 }
1479 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001480 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001481 SkRect src, dst;
1482 bool usePaint;
1483 fuzz->next(&src, &dst, &usePaint);
1484 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001485 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001486 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001487 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001488 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1489 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001490 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1491 break;
1492 }
1493 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001494 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001495 SkIRect src;
1496 SkRect dst;
1497 bool usePaint;
1498 fuzz->next(&src, &dst, &usePaint);
1499 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001500 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001501 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001502 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001503 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1504 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001505 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1506 break;
1507 }
1508 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001509 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001510 SkRect dst;
1511 bool usePaint;
1512 fuzz->next(&dst, &usePaint);
1513 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001514 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001515 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001516 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001517 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1518 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001519 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1520 break;
1521 }
1522 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001523 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001524 SkIRect center;
1525 SkRect dst;
1526 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001527 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001528 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001529 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001530 }
Hal Canary0361d492017-03-15 12:58:15 -04001531 if (make_fuzz_t<bool>(fuzz)) {
1532 fuzz->next(&center);
1533 } else { // Make valid center, see SkLatticeIter::Valid().
1534 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1535 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1536 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1537 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1538 }
1539 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001540 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1541 break;
1542 }
1543 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001544 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001545 bool usePaint;
1546 SkRect dst;
1547 fuzz->next(&usePaint, &dst);
1548 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001549 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001550 }
1551 constexpr int kMax = 6;
1552 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001553 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001554 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1555 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1556 fuzz->nextN(xDivs, lattice.fXCount);
1557 fuzz->nextN(yDivs, lattice.fYCount);
1558 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1559 break;
1560 }
1561 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001562 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001563 bool usePaint;
1564 SkRect dst;
1565 fuzz->next(&usePaint, &dst);
1566 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001567 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001568 }
1569 constexpr int kMax = 6;
1570 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001571 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001572 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1573 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1574 fuzz->nextN(xDivs, lattice.fXCount);
1575 fuzz->nextN(yDivs, lattice.fYCount);
1576 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1577 break;
1578 }
1579 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001580 fuzz_paint(fuzz, &paint, depth - 1);
1581 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001582 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001583 SkScalar x, y;
1584 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001585 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001586 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001587 break;
1588 }
1589 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001590 fuzz_paint(fuzz, &paint, depth - 1);
1591 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001592 fuzz_paint_text_encoding(fuzz, &paint);
1593 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001594 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001595 if (glyphCount < 1) {
1596 break;
1597 }
1598 SkAutoTMalloc<SkPoint> pos(glyphCount);
1599 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001600 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001601 pos[0] = {0, 0};
1602 for (int i = 1; i < glyphCount; ++i) {
1603 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001604 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001605 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1606 }
Hal Canary671e4422017-02-27 13:36:38 -05001607 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001608 break;
1609 }
1610 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001611 fuzz_paint(fuzz, &paint, depth - 1);
1612 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001613 fuzz_paint_text_encoding(fuzz, &paint);
1614 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001615 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001616 SkAutoTMalloc<SkScalar> widths(glyphCount);
1617 if (glyphCount < 1) {
1618 break;
1619 }
Hal Canary671e4422017-02-27 13:36:38 -05001620 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001621 SkScalar x = widths[0];
1622 for (int i = 0; i < glyphCount; ++i) {
1623 SkTSwap(x, widths[i]);
1624 x += widths[i];
1625 SkScalar offset;
1626 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001627 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001628 widths[i] += offset;
1629 }
1630 SkScalar y;
1631 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001632 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001633 break;
1634 }
1635 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001636 fuzz_paint(fuzz, &paint, depth - 1);
1637 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001638 fuzz_paint_text_encoding(fuzz, &paint);
1639 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001640 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001641 fuzz_path(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001642 SkScalar hOffset, vOffset;
1643 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001644 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1645 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001646 break;
1647 }
1648 case 49: {
1649 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001650 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001651 if (useMatrix) {
1652 fuzz->next(&matrix);
1653 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001654 fuzz_paint(fuzz, &paint, depth - 1);
1655 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001656 fuzz_paint_text_encoding(fuzz, &paint);
1657 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001658 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001659 fuzz_path(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001660 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001661 useMatrix ? &matrix : nullptr, paint);
1662 break;
1663 }
1664 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001665 fuzz_paint(fuzz, &paint, depth - 1);
1666 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001667 fuzz_paint_text_encoding(fuzz, &paint);
1668 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1669 SkRSXform rSXform[kMaxGlyphCount];
1670 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1671 SkASSERT(glyphCount <= kMaxGlyphCount);
1672 fuzz->nextN(rSXform, glyphCount);
1673 SkRect cullRect;
1674 bool useCullRect;
1675 fuzz->next(&useCullRect);
1676 if (useCullRect) {
1677 fuzz->next(&cullRect);
1678 }
1679 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1680 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001681 break;
1682 }
1683 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001684 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001685 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001686 SkScalar x, y;
1687 fuzz->next(&x, &y);
1688 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001689 break;
1690 }
1691 case 52: {
1692 bool usePaint, useMatrix;
1693 fuzz->next(&usePaint, &useMatrix);
1694 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001695 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001696 }
1697 if (useMatrix) {
1698 fuzz->next(&matrix);
1699 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001700 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001701 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1702 usePaint ? &paint : nullptr);
1703 break;
1704 }
1705 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001706 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001707 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001708 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001709 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001710 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001711 constexpr int kMaxCount = 100;
1712 int vertexCount;
1713 SkPoint vertices[kMaxCount];
1714 SkPoint texs[kMaxCount];
1715 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001716 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1717 fuzz->nextN(vertices, vertexCount);
1718 bool useTexs, useColors;
1719 fuzz->next(&useTexs, &useColors);
1720 if (useTexs) {
1721 fuzz->nextN(texs, vertexCount);
1722 }
1723 if (useColors) {
1724 fuzz->nextN(colors, vertexCount);
1725 }
1726 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001727 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001728 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001729 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1730 for (int i = 0; i < indexCount; ++i) {
1731 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1732 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001733 }
Mike Reed887cdf12017-04-03 11:11:09 -04001734 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1735 useTexs ? texs : nullptr,
1736 useColors ? colors : nullptr,
1737 indexCount, indices),
1738 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001739 break;
1740 }
1741 default:
1742 break;
1743 }
1744 }
1745}
1746
Hal Canary1e0138b2017-03-10 13:56:08 -05001747static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001748 SkScalar w, h;
1749 fuzz->next(&w, &h);
1750 SkPictureRecorder pictureRecorder;
1751 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1752 return pictureRecorder.finishRecordingAsPicture();
1753}
1754
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001755DEF_FUZZ(NullCanvas, fuzz) {
1756 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001757}
1758
Hal Canary44801ca2017-03-15 11:39:06 -04001759// 8.5x11 letter paper at 72ppi.
1760constexpr SkISize kCanvasSize = {612, 792};
1761
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001762DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001763 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001764 SkASSERT(surface && surface->getCanvas());
1765 fuzz_canvas(fuzz, surface->getCanvas());
1766}
1767
Hal Canarye2924432017-12-01 11:46:26 -05001768DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1769 SkPictureRecorder recorder;
1770 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1771 SkIntToScalar(kCanvasSize.height())));
1772 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1773 if (!pic) { fuzz->signalBug(); }
1774 sk_sp<SkData> data = pic->serialize();
1775 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001776 SkReadBuffer rb(data->data(), data->size());
1777 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001778 if (!deserialized) { fuzz->signalBug(); }
1779 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1780 SkASSERT(surface && surface->getCanvas());
1781 surface->getCanvas()->drawPicture(deserialized);
1782}
1783
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001784DEF_FUZZ(ImageFilter, fuzz) {
1785 auto fil = make_fuzz_imageFilter(fuzz, 20);
1786
1787 SkPaint paint;
1788 paint.setImageFilter(fil);
1789 SkBitmap bitmap;
1790 SkCanvas canvas(bitmap);
1791 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1792}
1793
1794
1795//SkRandom _rand;
1796#define SK_ADD_RANDOM_BIT_FLIPS
1797
1798DEF_FUZZ(SerializedImageFilter, fuzz) {
1799 auto filter = make_fuzz_imageFilter(fuzz, 20);
1800 auto data = filter->serialize();
1801 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1802 size_t len = data->size();
1803#ifdef SK_ADD_RANDOM_BIT_FLIPS
1804 unsigned char* p = const_cast<unsigned char*>(ptr);
1805 for (size_t i = 0; i < len; ++i, ++p) {
1806 uint8_t j;
1807 fuzz->nextRange(&j, 1, 250);
1808 if (j == 1) { // 0.4% of the time, flip a bit or byte
1809 uint8_t k;
1810 fuzz->nextRange(&k, 1, 10);
1811 if (k == 1) { // Then 10% of the time, change a whole byte
1812 uint8_t s;
1813 fuzz->nextRange(&s, 0, 2);
1814 switch(s) {
1815 case 0:
1816 *p ^= 0xFF; // Flip entire byte
1817 break;
1818 case 1:
1819 *p = 0xFF; // Set all bits to 1
1820 break;
1821 case 2:
1822 *p = 0x00; // Set all bits to 0
1823 break;
1824 }
1825 } else {
1826 uint8_t s;
1827 fuzz->nextRange(&s, 0, 7);
1828 *p ^= (1 << 7);
1829 }
1830 }
1831 }
1832#endif // SK_ADD_RANDOM_BIT_FLIPS
1833 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1834
1835 // uncomment below to write out a serialized image filter (to make corpus
1836 // for -t filter_fuzz)
1837 // SkString s("./serialized_filters/sf");
1838 // s.appendU32(_rand.nextU());
1839 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1840 // sk_fwrite(data->bytes(), data->size(), file);
1841 // sk_fclose(file);
1842
1843 SkPaint paint;
1844 paint.setImageFilter(deserializedFil);
1845 SkBitmap bitmap;
1846 SkCanvas canvas(bitmap);
1847 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1848}
1849
Hal Canary44801ca2017-03-15 11:39:06 -04001850#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001851static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1852 SkASSERT(context);
1853 auto surface = SkSurface::MakeRenderTarget(
1854 context,
1855 SkBudgeted::kNo,
1856 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1857 SkASSERT(surface && surface->getCanvas());
1858 fuzz_canvas(fuzz, surface->getCanvas());
1859}
1860
Hal Canary44801ca2017-03-15 11:39:06 -04001861DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001862 sk_gpu_test::GrContextFactory f;
1863 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001864 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001865 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001866 }
Hal Canary5aa91582017-03-21 11:11:44 -07001867 fuzz_ganesh(fuzz, context);
1868}
1869
1870DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001871 sk_gpu_test::GrContextFactory f;
1872 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001873}
1874
1875DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001876 sk_gpu_test::GrContextFactory f;
1877 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001878}
1879#endif
1880
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001881DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001882 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001883 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001884 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1885 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001886}
1887
1888// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1889DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001890 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001891 fuzz_canvas(fuzz, &debugCanvas);
1892 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1893 UrlDataManager dataManager(SkString("data"));
1894 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1895 Json::StyledStreamWriter(" ").write(std::cout, json);
1896}