blob: 7cfcc132d54fcef44a9c494e975b6b094ee32aa4 [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"
Kevin Lubick2541edf2018-01-11 10:27:14 -05009#include "FuzzCommon.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050010
11// CORE
12#include "SkCanvas.h"
13#include "SkColorFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050014#include "SkDebugCanvas.h"
15#include "SkDocument.h"
Hal Canary671e4422017-02-27 13:36:38 -050016#include "SkFontMgr.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050017#include "SkImageFilter.h"
18#include "SkMaskFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050019#include "SkNullCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050020#include "SkPathEffect.h"
21#include "SkPictureRecorder.h"
Mike Reed54518ac2017-07-22 08:29:48 -040022#include "SkPoint3.h"
Hal Canary5395c592017-03-08 16:52:18 -050023#include "SkRSXform.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050024#include "SkRegion.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050025#include "SkSurface.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050026#include "SkTypeface.h"
Kevin Lubickedef8ec2018-01-09 15:32:58 -050027#include "SkOSFile.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050028
29// EFFECTS
Hal Canary5395c592017-03-08 16:52:18 -050030#include "Sk1DPathEffect.h"
31#include "Sk2DPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050032#include "SkAlphaThresholdFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050033#include "SkArithmeticImageFilter.h"
Robert Phillips70e3e9a2017-06-26 14:22:01 -040034#include "SkBlurImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050035#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050036#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050037#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050038#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050039#include "SkCornerPathEffect.h"
40#include "SkDashPathEffect.h"
41#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050042#include "SkDisplacementMapEffect.h"
43#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050044#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050045#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050046#include "SkImageSource.h"
47#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050048#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050049#include "SkMagnifierImageFilter.h"
50#include "SkMatrixConvolutionImageFilter.h"
51#include "SkMergeImageFilter.h"
52#include "SkMorphologyImageFilter.h"
53#include "SkOffsetImageFilter.h"
54#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050055#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050056#include "SkPictureImageFilter.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050057#include "SkReadBuffer.h"
Hal Canary5395c592017-03-08 16:52:18 -050058#include "SkRRectsGaussianEdgeMaskFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050059#include "SkTableColorFilter.h"
Mike Reedc090c642017-05-16 10:39:06 -040060#include "SkTextBlob.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050061#include "SkTileImageFilter.h"
62#include "SkXfermodeImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050063
64// SRC
65#include "SkUtils.h"
66
Hal Canary44801ca2017-03-15 11:39:06 -040067#if SK_SUPPORT_GPU
68#include "GrContextFactory.h"
69#endif
70
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050071// MISC
72
73#include <iostream>
74
Hal Canary24ac42b2017-02-14 13:35:14 -050075// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050076// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040077// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050078
Hal Canary1e0138b2017-03-10 13:56:08 -050079// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
80// In fact, all make_fuzz_foo() functions have this potential problem.
81// Use sequence points!
82template <typename T>
83inline T make_fuzz_t(Fuzz* fuzz) {
84 T t;
85 fuzz->next(&t);
86 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050087}
88
Hal Canaryb69c4b82017-03-08 11:02:40 -050089template <>
90inline void Fuzz::next(SkShader::TileMode* m) {
Hal Canaryf7005202017-03-10 08:48:28 -050091 fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -050092}
93
Hal Canaryb69c4b82017-03-08 11:02:40 -050094template <>
Hal Canarye03c3e52017-03-09 11:33:35 -050095inline void Fuzz::next(SkFilterQuality* q) {
Hal Canaryf7005202017-03-10 08:48:28 -050096 fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
97 SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -050098}
99
100template <>
Hal Canaryb69c4b82017-03-08 11:02:40 -0500101inline void Fuzz::next(SkMatrix* m) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500102 constexpr int kArrayLength = 9;
103 SkScalar buffer[kArrayLength];
104 int matrixType;
105 this->nextRange(&matrixType, 0, 4);
106 switch (matrixType) {
107 case 0: // identity
108 *m = SkMatrix::I();
109 return;
110 case 1: // translate
111 this->nextRange(&buffer[0], -4000.0f, 4000.0f);
112 this->nextRange(&buffer[1], -4000.0f, 4000.0f);
113 *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
114 return;
115 case 2: // translate + scale
116 this->nextRange(&buffer[0], -400.0f, 400.0f);
117 this->nextRange(&buffer[1], -400.0f, 400.0f);
118 this->nextRange(&buffer[2], -4000.0f, 4000.0f);
119 this->nextRange(&buffer[3], -4000.0f, 4000.0f);
120 *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
121 m->postTranslate(buffer[2], buffer[3]);
122 return;
123 case 3: // affine
124 this->nextN(buffer, 6);
125 m->setAffine(buffer);
126 return;
127 case 4: // perspective
128 this->nextN(buffer, kArrayLength);
129 m->set9(buffer);
130 return;
131 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500132 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500133 return;
134 }
135}
136
Hal Canaryb69c4b82017-03-08 11:02:40 -0500137template <>
138inline void Fuzz::next(SkRRect* rr) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500139 SkRect r;
140 SkVector radii[4];
141 this->next(&r);
Hal Canary27bece82017-03-07 16:23:20 -0500142 r.sort();
143 for (SkVector& vec : radii) {
144 this->nextRange(&vec.fX, 0.0f, 1.0f);
145 vec.fX *= 0.5f * r.width();
146 this->nextRange(&vec.fY, 0.0f, 1.0f);
147 vec.fY *= 0.5f * r.height();
148 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500149 rr->setRectRadii(r, radii);
Hal Canary24ac42b2017-02-14 13:35:14 -0500150}
151
Hal Canaryb69c4b82017-03-08 11:02:40 -0500152template <>
153inline void Fuzz::next(SkBlendMode* mode) {
Hal Canaryf7005202017-03-10 08:48:28 -0500154 fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500155}
156
Hal Canary1e0138b2017-03-10 13:56:08 -0500157static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -0500158
Hal Canary1e0138b2017-03-10 13:56:08 -0500159static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500160
Hal Canary1e0138b2017-03-10 13:56:08 -0500161static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500162
Hal Canary1e0138b2017-03-10 13:56:08 -0500163static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500164 if (depth <= 0) {
165 return nullptr;
166 }
167 int colorFilterType;
168 fuzz->nextRange(&colorFilterType, 0, 8);
169 switch (colorFilterType) {
170 case 0:
171 return nullptr;
172 case 1: {
173 SkColor color;
174 SkBlendMode mode;
175 fuzz->next(&color, &mode);
176 return SkColorFilter::MakeModeFilter(color, mode);
177 }
178 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500179 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500180 if (!outer) {
181 return nullptr;
182 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500183 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500184 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500185 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500186 }
187 case 3: {
188 SkScalar array[20];
189 fuzz->nextN(array, SK_ARRAY_COUNT(array));
190 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
191 }
192 case 4: {
193 SkColor mul, add;
194 fuzz->next(&mul, &add);
195 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
196 }
197 case 5: {
198 bool grayscale;
199 int invertStyle;
200 float contrast;
201 fuzz->next(&grayscale);
202 fuzz->nextRange(&invertStyle, 0, 2);
203 fuzz->nextRange(&contrast, -1.0f, 1.0f);
204 return SkHighContrastFilter::Make(SkHighContrastConfig(
205 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
206 }
207 case 6:
208 return SkLumaColorFilter::Make();
209 case 7: {
210 uint8_t table[256];
211 fuzz->nextN(table, SK_ARRAY_COUNT(table));
212 return SkTableColorFilter::Make(table);
213 }
214 case 8: {
215 uint8_t tableA[256];
216 uint8_t tableR[256];
217 uint8_t tableG[256];
218 uint8_t tableB[256];
219 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
220 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
221 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
222 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
223 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
224 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500225 default:
226 SkASSERT(false);
227 break;
Hal Canary27bece82017-03-07 16:23:20 -0500228 }
229 return nullptr;
230}
Hal Canary24ac42b2017-02-14 13:35:14 -0500231
Hal Canary1e0138b2017-03-10 13:56:08 -0500232static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500233 SkScalar totalPos = 0;
234 for (int i = 0; i < colorCount; ++i) {
235 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
236 totalPos += pos[i];
237 }
238 totalPos = 1.0f / totalPos;
239 for (int i = 0; i < colorCount; ++i) {
240 pos[i] *= totalPos;
241 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500242 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500243 pos[colorCount - 1] = 1.0f;
244}
245
Hal Canary1e0138b2017-03-10 13:56:08 -0500246static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500247 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
248 sk_sp<SkColorFilter> colorFilter(nullptr);
249 SkBitmap bitmap;
250 sk_sp<SkImage> img;
251 SkShader::TileMode tmX, tmY;
252 bool useMatrix;
253 SkColor color;
254 SkMatrix matrix;
255 SkBlendMode blendMode;
256 int shaderType;
257 if (depth <= 0) {
258 return nullptr;
259 }
Hal Canary671e4422017-02-27 13:36:38 -0500260 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500261 switch (shaderType) {
262 case 0:
263 return nullptr;
264 case 1:
265 return SkShader::MakeEmptyShader();
266 case 2:
267 fuzz->next(&color);
268 return SkShader::MakeColorShader(color);
269 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500270 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500271 fuzz->next(&tmX, &tmY, &useMatrix);
272 if (useMatrix) {
273 fuzz->next(&matrix);
274 }
275 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
276 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500277 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500278 fuzz->next(&tmX, &tmY, &useMatrix);
279 if (useMatrix) {
280 fuzz->next(&matrix);
281 }
282 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
283 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500284 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500285 fuzz->next(&matrix);
286 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
287 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500288 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
289 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500290 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
291 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500292 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
293 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500294 fuzz->next(&blendMode);
295 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500296 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500297 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500298 bool useTile;
299 SkRect tile;
300 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
301 if (useMatrix) {
302 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500303 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500304 if (useTile) {
305 fuzz->next(&tile);
306 }
307 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
308 useMatrix ? &matrix : nullptr,
309 useTile ? &tile : nullptr);
310 }
Hal Canary671e4422017-02-27 13:36:38 -0500311 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500312 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000313 // Deprecated SkGaussianEdgeShader
314 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500315 case 10: {
316 constexpr int kMaxColors = 12;
317 SkPoint pts[2];
318 SkColor colors[kMaxColors];
319 SkScalar pos[kMaxColors];
320 int colorCount;
321 bool usePos;
322 fuzz->nextN(pts, 2);
323 fuzz->nextRange(&colorCount, 2, kMaxColors);
324 fuzz->nextN(colors, colorCount);
325 fuzz->next(&tmX, &useMatrix, &usePos);
326 if (useMatrix) {
327 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500328 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500329 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500330 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500331 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500332 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
333 tmX, 0, useMatrix ? &matrix : nullptr);
334 }
335 case 11: {
336 constexpr int kMaxColors = 12;
337 SkPoint center;
338 SkScalar radius;
339 int colorCount;
340 bool usePos;
341 SkColor colors[kMaxColors];
342 SkScalar pos[kMaxColors];
343 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
344 fuzz->nextRange(&colorCount, 2, kMaxColors);
345 fuzz->nextN(colors, colorCount);
346 if (useMatrix) {
347 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500348 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500349 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500350 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500351 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500352 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
353 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
354 }
355 case 12: {
356 constexpr int kMaxColors = 12;
357 SkPoint start, end;
358 SkScalar startRadius, endRadius;
359 int colorCount;
360 bool usePos;
361 SkColor colors[kMaxColors];
362 SkScalar pos[kMaxColors];
363 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
364 fuzz->nextRange(&colorCount, 2, kMaxColors);
365 fuzz->nextN(colors, colorCount);
366 if (useMatrix) {
367 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500368 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500369 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500370 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500371 }
372 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
373 usePos ? pos : nullptr, colorCount, tmX, 0,
374 useMatrix ? &matrix : nullptr);
375 }
376 case 13: {
377 constexpr int kMaxColors = 12;
378 SkScalar cx, cy;
379 int colorCount;
380 bool usePos;
381 SkColor colors[kMaxColors];
382 SkScalar pos[kMaxColors];
383 fuzz->next(&cx, &cy, &useMatrix, &usePos);
384 fuzz->nextRange(&colorCount, 2, kMaxColors);
385 fuzz->nextN(colors, colorCount);
386 if (useMatrix) {
387 fuzz->next(&matrix);
388 }
389 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500390 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500391 }
392 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
393 0, useMatrix ? &matrix : nullptr);
394 }
395 case 14: {
396 SkScalar baseFrequencyX, baseFrequencyY, seed;
397 int numOctaves;
398 SkISize tileSize;
399 bool useTileSize, turbulence;
400 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
401 if (useTileSize) {
402 fuzz->next(&tileSize);
403 }
404 fuzz->nextRange(&numOctaves, 2, 7);
405 if (turbulence) {
406 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
407 numOctaves, seed,
408 useTileSize ? &tileSize : nullptr);
409 } else {
410 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
411 numOctaves, seed,
412 useTileSize ? &tileSize : nullptr);
413 }
414 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500415 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500416 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500417 break;
418 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500419 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500420}
421
Hal Canary1e0138b2017-03-10 13:56:08 -0500422static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500423 if (depth <= 0) {
424 return nullptr;
425 }
426 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400427 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500428 switch (pathEffectType) {
429 case 0: {
430 return nullptr;
431 }
432 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500433 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
434 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500435 return SkPathEffect::MakeSum(std::move(first), std::move(second));
436 }
437 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500438 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
439 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500440 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
441 }
442 case 3: {
443 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -0500444 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500445 SkScalar advance, phase;
446 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500447 SkPath1DPathEffect::Style style;
448 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
449 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500450 }
451 case 4: {
452 SkScalar width;
453 SkMatrix matrix;
454 fuzz->next(&width, &matrix);
455 return SkLine2DPathEffect::Make(width, matrix);
456 }
457 case 5: {
458 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -0500459 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500460 SkMatrix matrix;
461 fuzz->next(&matrix);
462 return SkPath2DPathEffect::Make(matrix, path);
463 }
464 case 6: {
465 SkScalar radius;
466 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500467 return SkCornerPathEffect::Make(radius);
468 }
Mike Reed40e7e652017-07-22 22:12:59 -0400469 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500470 SkScalar phase;
471 fuzz->next(&phase);
472 SkScalar intervals[20];
473 int count;
474 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
475 fuzz->nextN(intervals, count);
476 return SkDashPathEffect::Make(intervals, count, phase);
477 }
Mike Reed40e7e652017-07-22 22:12:59 -0400478 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500479 SkScalar segLength, dev;
480 uint32_t seed;
481 fuzz->next(&segLength, &dev, &seed);
482 return SkDiscretePathEffect::Make(segLength, dev, seed);
483 }
484 default:
485 SkASSERT(false);
486 return nullptr;
487 }
488}
Hal Canary24ac42b2017-02-14 13:35:14 -0500489
Hal Canary1e0138b2017-03-10 13:56:08 -0500490static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500491 int maskfilterType;
492 fuzz->nextRange(&maskfilterType, 0, 2);
493 switch (maskfilterType) {
494 case 0:
495 return nullptr;
496 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500497 SkBlurStyle blurStyle;
498 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500499 SkScalar sigma;
500 fuzz->next(&sigma);
501 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Hal Canary1e0138b2017-03-10 13:56:08 -0500502 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -0500503 fuzz->next(&occluder);
504 }
505 uint32_t flags;
Mike Reed1be1f8d2018-03-14 13:01:17 -0400506 fuzz->nextRange(&flags, 0, 1);
507 bool respectCTM = flags != 0;
508 return SkMaskFilter::MakeBlur(blurStyle, sigma, occluder, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500509 }
510 case 2: {
511 SkRRect first, second;
512 SkScalar radius;
513 fuzz->next(&first, &second, &radius);
514 return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
515 }
516 default:
517 SkASSERT(false);
518 return nullptr;
519 }
520}
Hal Canary24ac42b2017-02-14 13:35:14 -0500521
Hal Canary1e0138b2017-03-10 13:56:08 -0500522static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
523 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500524 return nullptr;
525 }
526 auto fontMugger = SkFontMgr::RefDefault();
527 SkASSERT(fontMugger);
528 int familyCount = fontMugger->countFamilies();
529 int i, j;
530 fuzz->nextRange(&i, 0, familyCount - 1);
531 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
532 int styleCount = family->count();
533 fuzz->nextRange(&j, 0, styleCount - 1);
534 return sk_sp<SkTypeface>(family->createTypeface(j));
535}
Hal Canary24ac42b2017-02-14 13:35:14 -0500536
Hal Canarye03c3e52017-03-09 11:33:35 -0500537template <>
538inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
539 SkRect rect;
540 uint8_t flags;
541 this->next(&rect);
542 this->nextRange(&flags, 0, 0xF);
543 *cropRect = SkImageFilter::CropRect(rect, flags);
544}
545
Hal Canary1e0138b2017-03-10 13:56:08 -0500546static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500547
548static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
549 if (depth <= 0) {
550 return nullptr;
551 }
552 uint8_t imageFilterType;
553 fuzz->nextRange(&imageFilterType, 1, 6);
554 SkPoint3 p, q;
555 SkColor lightColor;
556 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
557 sk_sp<SkImageFilter> input;
558 SkImageFilter::CropRect cropRect;
559 bool useCropRect;
560 fuzz->next(&useCropRect);
561 if (useCropRect) {
562 fuzz->next(&cropRect);
563 }
564 switch (imageFilterType) {
565 case 1:
566 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500567 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500568 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
569 std::move(input),
570 useCropRect ? &cropRect : nullptr);
571 case 2:
572 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500573 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500574 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
575 std::move(input),
576 useCropRect ? &cropRect : nullptr);
577 case 3:
578 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500579 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500580 return SkLightingImageFilter::MakeSpotLitDiffuse(
581 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
582 std::move(input), useCropRect ? &cropRect : nullptr);
583 case 4:
584 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500585 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500586 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
587 shininess, std::move(input),
588 useCropRect ? &cropRect : nullptr);
589 case 5:
590 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500591 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500592 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
593 shininess, std::move(input),
594 useCropRect ? &cropRect : nullptr);
595 case 6:
596 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
597 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500598 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500599 return SkLightingImageFilter::MakeSpotLitSpecular(
600 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
601 std::move(input), useCropRect ? &cropRect : nullptr);
602 default:
603 SkASSERT(false);
604 return nullptr;
605 }
606}
607
Hal Canary1e0138b2017-03-10 13:56:08 -0500608static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500609
Hal Canary1e0138b2017-03-10 13:56:08 -0500610static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500611 if (depth <= 0) {
612 return nullptr;
613 }
614 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500615 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500616 switch (imageFilterType) {
617 case 0:
618 return nullptr;
619 case 1: {
620 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500621 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500622 bool useCropRect;
623 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400624 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500625 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400626 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500627 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400628 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500629 useCropRect ? &cropRect : nullptr);
630 }
631 case 2: {
632 SkMatrix matrix;
633 SkFilterQuality quality;
634 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500635 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500636 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
637 }
638 case 3: {
639 SkRegion region;
640 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500641 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500642 bool useCropRect;
643 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400644 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500645 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400646 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500647 }
648 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
649 useCropRect ? &cropRect : nullptr);
650 }
651 case 4: {
652 float k1, k2, k3, k4;
653 bool enforcePMColor;
654 bool useCropRect;
655 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500656 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
657 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500658 SkImageFilter::CropRect cropRect;
659 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400660 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500661 }
662 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
663 std::move(background), std::move(foreground),
664 useCropRect ? &cropRect : nullptr);
665 }
666 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500667 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
668 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500669 bool useCropRect;
670 SkImageFilter::CropRect cropRect;
671 fuzz->next(&useCropRect);
672 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400673 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500674 }
675 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
676 useCropRect ? &cropRect : nullptr);
677 }
678 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500679 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
680 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500681 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
682 }
683 case 7: {
684 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400685 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
686 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500687 SkScalar scale;
688 bool useCropRect;
689 fuzz->next(&scale, &useCropRect);
690 SkImageFilter::CropRect cropRect;
691 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400692 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500693 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500694 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
695 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500696 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
697 std::move(displacement), std::move(color),
698 useCropRect ? &cropRect : nullptr);
699 }
700 case 8: {
701 SkScalar dx, dy, sigmaX, sigmaY;
702 SkColor color;
703 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500704 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500705 bool useCropRect;
706 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
707 SkImageFilter::CropRect cropRect;
708 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400709 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500710 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500711 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500712 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
713 std::move(input),
714 useCropRect ? &cropRect : nullptr);
715 }
716 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500717 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500718 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500719 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500720 SkRect srcRect, dstRect;
721 SkFilterQuality filterQuality;
722 fuzz->next(&srcRect, &dstRect, &filterQuality);
723 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
724 }
725 case 11:
726 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
727 case 12: {
728 SkRect srcRect;
729 SkScalar inset;
730 bool useCropRect;
731 SkImageFilter::CropRect cropRect;
732 fuzz->next(&srcRect, &inset, &useCropRect);
733 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400734 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500735 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500736 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500737 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
738 useCropRect ? &cropRect : nullptr);
739 }
740 case 13: {
741 constexpr int kMaxKernelSize = 5;
742 int32_t n, m;
743 fuzz->nextRange(&n, 1, kMaxKernelSize);
744 fuzz->nextRange(&m, 1, kMaxKernelSize);
745 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
746 fuzz->nextN(kernel, n * m);
747 int32_t offsetX, offsetY;
748 fuzz->nextRange(&offsetX, 0, n - 1);
749 fuzz->nextRange(&offsetY, 0, m - 1);
750 SkScalar gain, bias;
751 bool convolveAlpha, useCropRect;
752 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
753 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500754 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500755 SkImageFilter::CropRect cropRect;
756 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400757 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500758 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500759 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500760 return SkMatrixConvolutionImageFilter::Make(
761 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
762 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
763 }
764 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500765 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
766 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500767 bool useCropRect;
768 fuzz->next(&useCropRect);
769 SkImageFilter::CropRect cropRect;
770 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400771 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500772 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400773 return SkMergeImageFilter::Make(std::move(first), std::move(second),
774 useCropRect ? &cropRect : nullptr);
775 }
776 case 15: {
777 constexpr int kMaxCount = 4;
778 sk_sp<SkImageFilter> ifs[kMaxCount];
779 int count;
780 fuzz->nextRange(&count, 1, kMaxCount);
781 for (int i = 0; i < count; ++i) {
782 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
783 }
784 bool useCropRect;
785 fuzz->next(&useCropRect);
786 SkImageFilter::CropRect cropRect;
787 if (useCropRect) {
788 fuzz->next(&cropRect);
789 }
790 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500791 }
792 case 16: {
793 int rx, ry;
794 fuzz->next(&rx, &ry);
795 bool useCropRect;
796 fuzz->next(&useCropRect);
797 SkImageFilter::CropRect cropRect;
798 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400799 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500800 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500801 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500802 return SkDilateImageFilter::Make(rx, ry, std::move(input),
803 useCropRect ? &cropRect : nullptr);
804 }
805 case 17: {
806 int rx, ry;
807 fuzz->next(&rx, &ry);
808 bool useCropRect;
809 fuzz->next(&useCropRect);
810 SkImageFilter::CropRect cropRect;
811 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400812 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500813 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500814 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500815 return SkErodeImageFilter::Make(rx, ry, std::move(input),
816 useCropRect ? &cropRect : nullptr);
817 }
818 case 18: {
819 SkScalar dx, dy;
820 fuzz->next(&dx, &dy);
821 bool useCropRect;
822 fuzz->next(&useCropRect);
823 SkImageFilter::CropRect cropRect;
824 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400825 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500826 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500827 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500828 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
829 useCropRect ? &cropRect : nullptr);
830 }
831 case 19: {
832 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500833 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500834 bool useCropRect;
835 fuzz->next(&useCropRect);
836 SkImageFilter::CropRect cropRect;
837 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400838 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500839 }
840 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
841 }
842 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500843 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500844 return SkPictureImageFilter::Make(std::move(picture));
845 }
846 case 21: {
847 SkRect cropRect;
848 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500849 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500850 return SkPictureImageFilter::Make(std::move(picture), cropRect);
851 }
852 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500853 SkRect src, dst;
854 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500855 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500856 return SkTileImageFilter::Make(src, dst, std::move(input));
857 }
Mike Reed77e487d2017-11-09 21:50:20 +0000858 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500859 SkBlendMode blendMode;
860 bool useCropRect;
861 fuzz->next(&useCropRect, &blendMode);
862 SkImageFilter::CropRect cropRect;
863 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400864 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500865 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500866 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
867 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500868 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
869 useCropRect ? &cropRect : nullptr);
870 }
871 default:
872 SkASSERT(false);
873 return nullptr;
874 }
875}
Hal Canary24ac42b2017-02-14 13:35:14 -0500876
Hal Canary1e0138b2017-03-10 13:56:08 -0500877static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500878 int w, h;
879 fuzz->nextRange(&w, 1, 1024);
880 fuzz->nextRange(&h, 1, 1024);
881 SkAutoTMalloc<SkPMColor> data(w * h);
882 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
883 int n = w * h;
884 for (int i = 0; i < n; ++i) {
885 SkColor c;
886 fuzz->next(&c);
887 data[i] = SkPreMultiplyColor(c);
888 }
889 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500890 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
891 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500892}
893
Hal Canary1e0138b2017-03-10 13:56:08 -0500894static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500895 SkBitmap bitmap;
896 int w, h;
897 fuzz->nextRange(&w, 1, 1024);
898 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500899 if (!bitmap.tryAllocN32Pixels(w, h)) {
900 SkDEBUGF(("Could not allocate pixels %d x %d", w, h));
901 return bitmap;
902 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500903 for (int y = 0; y < h; ++y) {
904 for (int x = 0; x < w; ++x) {
905 SkColor c;
906 fuzz->next(&c);
907 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
908 }
909 }
910 return bitmap;
911}
912
Hal Canary1e0138b2017-03-10 13:56:08 -0500913template <typename T, typename Min, typename Max>
914inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
915 T value;
916 fuzz_enum_range(fuzz, &value, minv, maxv);
917 return value;
918}
919
920static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500921 if (!fuzz || !paint || depth <= 0) {
922 return;
923 }
924
Hal Canary1e0138b2017-03-10 13:56:08 -0500925 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
926 paint->setDither( make_fuzz_t<bool>(fuzz));
927 paint->setColor( make_fuzz_t<SkColor>(fuzz));
928 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
929 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
930 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
931 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
932 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
933 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
934 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
935 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500936
937 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500938 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
939 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
940 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
941 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500942 }
943}
944
Hal Canary1e0138b2017-03-10 13:56:08 -0500945static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
946 paint->setTypeface( make_fuzz_typeface(fuzz));
947 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
948 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
949 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
950 paint->setLinearText( make_fuzz_t<bool>(fuzz));
951 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
952 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
953 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
954 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
955 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
956 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
957 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
958 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
959 SkPaint::kFull_Hinting));
960 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -0500961}
962
963static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500964 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -0500965}
966
Hal Canary5395c592017-03-08 16:52:18 -0500967constexpr int kMaxGlyphCount = 30;
968
Hal Canary1e0138b2017-03-10 13:56:08 -0500969static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -0500970 SkTDArray<uint8_t> array;
971 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500972 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
973 : SkTypeface::MakeDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500974 if (glyphRange == 0) {
975 // Some fuzzing environments have no fonts, so empty array is the best
976 // we can do.
977 return array;
978 }
Hal Canary671e4422017-02-27 13:36:38 -0500979 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500980 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500981 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
982 for (int i = 0; i < glyphCount; ++i) {
983 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
984 }
985 return array;
986 }
987 static const SkUnichar ranges[][2] = {
988 {0x0020, 0x007F},
989 {0x00A1, 0x0250},
990 {0x0400, 0x0500},
991 };
992 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500993 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500994 count += (ranges[i][1] - ranges[i][0]);
995 }
Hal Canary5395c592017-03-08 16:52:18 -0500996 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500997 SkUnichar buffer[kMaxLength];
998 int length;
999 fuzz->nextRange(&length, 1, kMaxLength);
1000 for (int j = 0; j < length; ++j) {
1001 int32_t value;
1002 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001003 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001004 if (value + ranges[i][0] < ranges[i][1]) {
1005 buffer[j] = value + ranges[i][0];
1006 break;
1007 } else {
1008 value -= (ranges[i][1] - ranges[i][0]);
1009 }
1010 }
1011 }
1012 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001013 case SkPaint::kUTF8_TextEncoding: {
1014 size_t utf8len = 0;
1015 for (int j = 0; j < length; ++j) {
1016 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001017 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001018 char* ptr = (char*)array.append(utf8len);
1019 for (int j = 0; j < length; ++j) {
1020 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001021 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001022 } break;
1023 case SkPaint::kUTF16_TextEncoding: {
1024 size_t utf16len = 0;
1025 for (int j = 0; j < length; ++j) {
1026 utf16len += SkUTF16_FromUnichar(buffer[j]);
1027 }
1028 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1029 for (int j = 0; j < length; ++j) {
1030 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1031 }
1032 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001033 case SkPaint::kUTF32_TextEncoding:
1034 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001035 break;
Hal Canary671e4422017-02-27 13:36:38 -05001036 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001037 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001038 break;
Hal Canary671e4422017-02-27 13:36:38 -05001039 }
1040 return array;
1041}
1042
Hal Canary5395c592017-03-08 16:52:18 -05001043static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1044 SkTextBlobBuilder textBlobBuilder;
1045 int8_t runCount;
1046 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1047 while (runCount-- > 0) {
1048 SkPaint paint;
1049 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001050 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001051 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1052 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1053 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1054 SkASSERT(glyphCount <= kMaxGlyphCount);
1055 SkScalar x, y;
1056 const SkTextBlobBuilder::RunBuffer* buffer;
1057 uint8_t runType;
1058 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1059 switch (runType) {
1060 case 0:
1061 fuzz->next(&x, &y);
1062 // TODO: Test other variations of this.
1063 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1064 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1065 break;
1066 case 1:
1067 fuzz->next(&y);
1068 // TODO: Test other variations of this.
1069 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1070 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1071 fuzz->nextN(buffer->pos, glyphCount);
1072 break;
1073 case 2:
1074 // TODO: Test other variations of this.
1075 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1076 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1077 fuzz->nextN(buffer->pos, glyphCount * 2);
1078 break;
1079 default:
1080 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001081 break;
Hal Canary5395c592017-03-08 16:52:18 -05001082 }
1083 }
1084 return textBlobBuilder.make();
1085}
1086
Hal Canary1e0138b2017-03-10 13:56:08 -05001087static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001088 if (!fuzz || !canvas || depth <= 0) {
1089 return;
1090 }
1091 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1092 unsigned N;
1093 fuzz->nextRange(&N, 0, 2000);
1094 for (unsigned i = 0; i < N; ++i) {
1095 if (fuzz->exhausted()) {
1096 return;
1097 }
1098 SkPaint paint;
1099 SkMatrix matrix;
1100 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001101 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001102 switch (drawCommand) {
1103 case 0:
1104 canvas->flush();
1105 break;
1106 case 1:
1107 canvas->save();
1108 break;
1109 case 2: {
1110 SkRect bounds;
1111 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001112 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001113 canvas->saveLayer(&bounds, &paint);
1114 break;
1115 }
1116 case 3: {
1117 SkRect bounds;
1118 fuzz->next(&bounds);
1119 canvas->saveLayer(&bounds, nullptr);
1120 break;
1121 }
1122 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001123 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001124 canvas->saveLayer(nullptr, &paint);
1125 break;
1126 case 5:
1127 canvas->saveLayer(nullptr, nullptr);
1128 break;
1129 case 6: {
1130 uint8_t alpha;
1131 fuzz->next(&alpha);
1132 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1133 break;
1134 }
1135 case 7: {
1136 SkRect bounds;
1137 uint8_t alpha;
1138 fuzz->next(&bounds, &alpha);
1139 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1140 break;
1141 }
1142 case 8: {
1143 SkCanvas::SaveLayerRec saveLayerRec;
1144 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001145 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001146 fuzz->next(&bounds);
1147 saveLayerRec.fBounds = &bounds;
1148 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001149 if (make_fuzz_t<bool>(fuzz)) {
1150 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001151 saveLayerRec.fPaint = &paint;
1152 }
1153 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001154 if (make_fuzz_t<bool>(fuzz)) {
1155 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001156 saveLayerRec.fBackdrop = imageFilter.get();
1157 }
Hal Canary5395c592017-03-08 16:52:18 -05001158 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001159 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001160 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1161 // }
1162
Hal Canary24ac42b2017-02-14 13:35:14 -05001163 canvas->saveLayer(saveLayerRec);
1164 break;
1165 }
1166 case 9:
1167 canvas->restore();
1168 break;
1169 case 10: {
1170 int saveCount;
1171 fuzz->next(&saveCount);
1172 canvas->restoreToCount(saveCount);
1173 break;
1174 }
1175 case 11: {
1176 SkScalar x, y;
1177 fuzz->next(&x, &y);
1178 canvas->translate(x, y);
1179 break;
1180 }
1181 case 12: {
1182 SkScalar x, y;
1183 fuzz->next(&x, &y);
1184 canvas->scale(x, y);
1185 break;
1186 }
1187 case 13: {
1188 SkScalar v;
1189 fuzz->next(&v);
1190 canvas->rotate(v);
1191 break;
1192 }
1193 case 14: {
1194 SkScalar x, y, v;
1195 fuzz->next(&x, &y, &v);
1196 canvas->rotate(v, x, y);
1197 break;
1198 }
1199 case 15: {
1200 SkScalar x, y;
1201 fuzz->next(&x, &y);
1202 canvas->skew(x, y);
1203 break;
1204 }
1205 case 16: {
1206 SkMatrix mat;
1207 fuzz->next(&mat);
1208 canvas->concat(mat);
1209 break;
1210 }
1211 case 17: {
1212 SkMatrix mat;
1213 fuzz->next(&mat);
1214 canvas->setMatrix(mat);
1215 break;
1216 }
1217 case 18:
1218 canvas->resetMatrix();
1219 break;
1220 case 19: {
1221 SkRect r;
1222 int op;
1223 bool doAntiAlias;
1224 fuzz->next(&r, &doAntiAlias);
1225 fuzz->nextRange(&op, 0, 1);
1226 r.sort();
1227 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1228 break;
1229 }
1230 case 20: {
1231 SkRRect rr;
1232 int op;
1233 bool doAntiAlias;
1234 fuzz->next(&rr);
1235 fuzz->next(&doAntiAlias);
1236 fuzz->nextRange(&op, 0, 1);
1237 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1238 break;
1239 }
1240 case 21: {
1241 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001242 FuzzPath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001243 int op;
1244 bool doAntiAlias;
1245 fuzz->next(&doAntiAlias);
1246 fuzz->nextRange(&op, 0, 1);
1247 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1248 break;
1249 }
1250 case 22: {
1251 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001252 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001253 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001254 fuzz->nextRange(&op, 0, 1);
1255 canvas->clipRegion(region, (SkClipOp)op);
1256 break;
1257 }
1258 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001259 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001260 canvas->drawPaint(paint);
1261 break;
1262 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001263 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001264 SkCanvas::PointMode pointMode;
1265 fuzz_enum_range(fuzz, &pointMode,
1266 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001267 size_t count;
1268 constexpr int kMaxCount = 30;
1269 fuzz->nextRange(&count, 0, kMaxCount);
1270 SkPoint pts[kMaxCount];
1271 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001272 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001273 break;
1274 }
1275 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001276 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001277 SkRect r;
1278 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001279 if (!r.isFinite()) {
1280 break;
1281 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001282 canvas->drawRect(r, paint);
1283 break;
1284 }
1285 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001286 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001287 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001288 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001289 canvas->drawRegion(region, paint);
1290 break;
1291 }
1292 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001293 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001294 SkRect r;
1295 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001296 if (!r.isFinite()) {
1297 break;
1298 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001299 canvas->drawOval(r, paint);
1300 break;
1301 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001302 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001303 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001304 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001305 SkRRect rr;
1306 fuzz->next(&rr);
1307 canvas->drawRRect(rr, paint);
1308 break;
1309 }
1310 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001311 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001312 SkRRect orr, irr;
1313 fuzz->next(&orr);
1314 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001315 if (orr.getBounds().contains(irr.getBounds())) {
1316 canvas->drawDRRect(orr, irr, paint);
1317 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001318 break;
1319 }
1320 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001321 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001322 SkRect r;
1323 SkScalar start, sweep;
1324 bool useCenter;
1325 fuzz->next(&r, &start, &sweep, &useCenter);
1326 canvas->drawArc(r, start, sweep, useCenter, paint);
1327 break;
1328 }
1329 case 32: {
1330 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001331 FuzzPath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001332 canvas->drawPath(path, paint);
1333 break;
1334 }
1335 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001336 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001337 SkScalar left, top;
1338 bool usePaint;
1339 fuzz->next(&left, &top, &usePaint);
1340 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001341 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001342 }
1343 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1344 break;
1345 }
1346 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001347 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001348 SkRect src, dst;
1349 bool usePaint;
1350 fuzz->next(&src, &dst, &usePaint);
1351 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001352 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001353 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001354 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1356 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001357 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1358 break;
1359 }
1360 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 SkIRect src;
1363 SkRect dst;
1364 bool usePaint;
1365 fuzz->next(&src, &dst, &usePaint);
1366 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001367 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001368 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001369 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001370 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1371 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001372 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1373 break;
1374 }
1375 case 36: {
1376 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001377 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001378 SkRect dst;
1379 fuzz->next(&dst, &usePaint);
1380 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001381 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001382 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001383 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001384 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1385 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001386 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1387 break;
1388 }
1389 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001390 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001391 SkIRect center;
1392 SkRect dst;
1393 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001394 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001395 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001396 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001397 }
Hal Canary0361d492017-03-15 12:58:15 -04001398 if (make_fuzz_t<bool>(fuzz)) {
1399 fuzz->next(&center);
1400 } else { // Make valid center, see SkLatticeIter::Valid().
1401 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1402 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1403 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1404 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1405 }
1406 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001407 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1408 break;
1409 }
1410 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001411 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001412 SkScalar left, top;
1413 bool usePaint;
1414 fuzz->next(&left, &top, &usePaint);
1415 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001416 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001417 }
1418 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1419 break;
1420 }
1421 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001422 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001423 SkRect src, dst;
1424 bool usePaint;
1425 fuzz->next(&src, &dst, &usePaint);
1426 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001427 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001428 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001429 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001430 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1431 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001432 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1433 break;
1434 }
1435 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001436 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001437 SkIRect src;
1438 SkRect dst;
1439 bool usePaint;
1440 fuzz->next(&src, &dst, &usePaint);
1441 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001442 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001443 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001444 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001445 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1446 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001447 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1448 break;
1449 }
1450 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001451 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001452 SkRect dst;
1453 bool usePaint;
1454 fuzz->next(&dst, &usePaint);
1455 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001456 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001457 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001458 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001459 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1460 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001461 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1462 break;
1463 }
1464 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001465 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001466 SkIRect center;
1467 SkRect dst;
1468 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001469 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001470 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001471 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001472 }
Hal Canary0361d492017-03-15 12:58:15 -04001473 if (make_fuzz_t<bool>(fuzz)) {
1474 fuzz->next(&center);
1475 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001476 if (img.width() == 0 || img.height() == 0) {
1477 // bitmap may not have had its pixels initialized.
1478 break;
1479 }
Hal Canary0361d492017-03-15 12:58:15 -04001480 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1481 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1482 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1483 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1484 }
1485 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001486 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1487 break;
1488 }
1489 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001490 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001491 bool usePaint;
1492 SkRect dst;
1493 fuzz->next(&usePaint, &dst);
1494 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001495 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001496 }
1497 constexpr int kMax = 6;
1498 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001499 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001500 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1501 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1502 fuzz->nextN(xDivs, lattice.fXCount);
1503 fuzz->nextN(yDivs, lattice.fYCount);
1504 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1505 break;
1506 }
1507 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001508 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001509 bool usePaint;
1510 SkRect dst;
1511 fuzz->next(&usePaint, &dst);
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 }
1515 constexpr int kMax = 6;
1516 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001517 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001518 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1519 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1520 fuzz->nextN(xDivs, lattice.fXCount);
1521 fuzz->nextN(yDivs, lattice.fYCount);
1522 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1523 break;
1524 }
1525 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001526 fuzz_paint(fuzz, &paint, depth - 1);
1527 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001528 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001529 SkScalar x, y;
1530 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001531 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001532 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001533 break;
1534 }
1535 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001536 fuzz_paint(fuzz, &paint, depth - 1);
1537 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001538 fuzz_paint_text_encoding(fuzz, &paint);
1539 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001540 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001541 if (glyphCount < 1) {
1542 break;
1543 }
1544 SkAutoTMalloc<SkPoint> pos(glyphCount);
1545 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001546 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001547 pos[0] = {0, 0};
1548 for (int i = 1; i < glyphCount; ++i) {
1549 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001550 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001551 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1552 }
Hal Canary671e4422017-02-27 13:36:38 -05001553 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001554 break;
1555 }
1556 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001557 fuzz_paint(fuzz, &paint, depth - 1);
1558 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001559 fuzz_paint_text_encoding(fuzz, &paint);
1560 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001561 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001562 SkAutoTMalloc<SkScalar> widths(glyphCount);
1563 if (glyphCount < 1) {
1564 break;
1565 }
Hal Canary671e4422017-02-27 13:36:38 -05001566 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001567 SkScalar x = widths[0];
1568 for (int i = 0; i < glyphCount; ++i) {
1569 SkTSwap(x, widths[i]);
1570 x += widths[i];
1571 SkScalar offset;
1572 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001573 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001574 widths[i] += offset;
1575 }
1576 SkScalar y;
1577 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001578 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001579 break;
1580 }
1581 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001582 fuzz_paint(fuzz, &paint, depth - 1);
1583 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001584 fuzz_paint_text_encoding(fuzz, &paint);
1585 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001586 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001587 FuzzPath(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001588 SkScalar hOffset, vOffset;
1589 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001590 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1591 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001592 break;
1593 }
1594 case 49: {
1595 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001596 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001597 if (useMatrix) {
1598 fuzz->next(&matrix);
1599 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001600 fuzz_paint(fuzz, &paint, depth - 1);
1601 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001602 fuzz_paint_text_encoding(fuzz, &paint);
1603 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001604 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001605 FuzzPath(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001606 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001607 useMatrix ? &matrix : nullptr, paint);
1608 break;
1609 }
1610 case 50: {
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);
1615 SkRSXform rSXform[kMaxGlyphCount];
1616 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1617 SkASSERT(glyphCount <= kMaxGlyphCount);
1618 fuzz->nextN(rSXform, glyphCount);
1619 SkRect cullRect;
1620 bool useCullRect;
1621 fuzz->next(&useCullRect);
1622 if (useCullRect) {
1623 fuzz->next(&cullRect);
1624 }
1625 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1626 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001627 break;
1628 }
1629 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001630 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001631 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001632 SkScalar x, y;
1633 fuzz->next(&x, &y);
1634 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001635 break;
1636 }
1637 case 52: {
1638 bool usePaint, useMatrix;
1639 fuzz->next(&usePaint, &useMatrix);
1640 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001641 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001642 }
1643 if (useMatrix) {
1644 fuzz->next(&matrix);
1645 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001646 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001647 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1648 usePaint ? &paint : nullptr);
1649 break;
1650 }
1651 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001652 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001653 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001654 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001655 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001656 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001657 constexpr int kMaxCount = 100;
1658 int vertexCount;
1659 SkPoint vertices[kMaxCount];
1660 SkPoint texs[kMaxCount];
1661 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001662 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1663 fuzz->nextN(vertices, vertexCount);
1664 bool useTexs, useColors;
1665 fuzz->next(&useTexs, &useColors);
1666 if (useTexs) {
1667 fuzz->nextN(texs, vertexCount);
1668 }
1669 if (useColors) {
1670 fuzz->nextN(colors, vertexCount);
1671 }
1672 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001673 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001674 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001675 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1676 for (int i = 0; i < indexCount; ++i) {
1677 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1678 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001679 }
Mike Reed887cdf12017-04-03 11:11:09 -04001680 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1681 useTexs ? texs : nullptr,
1682 useColors ? colors : nullptr,
1683 indexCount, indices),
1684 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001685 break;
1686 }
1687 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001688 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001689 break;
1690 }
1691 }
1692}
1693
Hal Canary1e0138b2017-03-10 13:56:08 -05001694static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001695 SkScalar w, h;
1696 fuzz->next(&w, &h);
1697 SkPictureRecorder pictureRecorder;
1698 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1699 return pictureRecorder.finishRecordingAsPicture();
1700}
1701
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001702DEF_FUZZ(NullCanvas, fuzz) {
1703 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001704}
1705
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001706constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001707
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001708DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001709 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001710 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001711 fuzz_canvas(fuzz, surface->getCanvas());
1712}
1713
Hal Canarye2924432017-12-01 11:46:26 -05001714DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1715 SkPictureRecorder recorder;
1716 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1717 SkIntToScalar(kCanvasSize.height())));
1718 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1719 if (!pic) { fuzz->signalBug(); }
1720 sk_sp<SkData> data = pic->serialize();
1721 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001722 SkReadBuffer rb(data->data(), data->size());
1723 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001724 if (!deserialized) { fuzz->signalBug(); }
1725 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1726 SkASSERT(surface && surface->getCanvas());
1727 surface->getCanvas()->drawPicture(deserialized);
1728}
1729
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001730DEF_FUZZ(ImageFilter, fuzz) {
1731 auto fil = make_fuzz_imageFilter(fuzz, 20);
1732
1733 SkPaint paint;
1734 paint.setImageFilter(fil);
1735 SkBitmap bitmap;
1736 SkCanvas canvas(bitmap);
1737 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1738}
1739
1740
1741//SkRandom _rand;
1742#define SK_ADD_RANDOM_BIT_FLIPS
1743
1744DEF_FUZZ(SerializedImageFilter, fuzz) {
1745 auto filter = make_fuzz_imageFilter(fuzz, 20);
1746 auto data = filter->serialize();
1747 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1748 size_t len = data->size();
1749#ifdef SK_ADD_RANDOM_BIT_FLIPS
1750 unsigned char* p = const_cast<unsigned char*>(ptr);
1751 for (size_t i = 0; i < len; ++i, ++p) {
1752 uint8_t j;
1753 fuzz->nextRange(&j, 1, 250);
1754 if (j == 1) { // 0.4% of the time, flip a bit or byte
1755 uint8_t k;
1756 fuzz->nextRange(&k, 1, 10);
1757 if (k == 1) { // Then 10% of the time, change a whole byte
1758 uint8_t s;
1759 fuzz->nextRange(&s, 0, 2);
1760 switch(s) {
1761 case 0:
1762 *p ^= 0xFF; // Flip entire byte
1763 break;
1764 case 1:
1765 *p = 0xFF; // Set all bits to 1
1766 break;
1767 case 2:
1768 *p = 0x00; // Set all bits to 0
1769 break;
1770 }
1771 } else {
1772 uint8_t s;
1773 fuzz->nextRange(&s, 0, 7);
1774 *p ^= (1 << 7);
1775 }
1776 }
1777 }
1778#endif // SK_ADD_RANDOM_BIT_FLIPS
1779 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1780
1781 // uncomment below to write out a serialized image filter (to make corpus
1782 // for -t filter_fuzz)
1783 // SkString s("./serialized_filters/sf");
1784 // s.appendU32(_rand.nextU());
1785 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1786 // sk_fwrite(data->bytes(), data->size(), file);
1787 // sk_fclose(file);
1788
1789 SkPaint paint;
1790 paint.setImageFilter(deserializedFil);
1791 SkBitmap bitmap;
1792 SkCanvas canvas(bitmap);
1793 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1794}
1795
Hal Canary44801ca2017-03-15 11:39:06 -04001796#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001797static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1798 SkASSERT(context);
1799 auto surface = SkSurface::MakeRenderTarget(
1800 context,
1801 SkBudgeted::kNo,
1802 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1803 SkASSERT(surface && surface->getCanvas());
1804 fuzz_canvas(fuzz, surface->getCanvas());
1805}
1806
Hal Canary44801ca2017-03-15 11:39:06 -04001807DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001808 sk_gpu_test::GrContextFactory f;
1809 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001810 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001811 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001812 }
Hal Canary5aa91582017-03-21 11:11:44 -07001813 fuzz_ganesh(fuzz, context);
1814}
1815
Kevin Lubick27d42192018-04-03 12:30:32 -04001816// This target is deprecated, NullGLContext is not well maintained.
1817// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001818DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001819 sk_gpu_test::GrContextFactory f;
1820 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001821}
1822
Kevin Lubick30709262018-04-02 11:06:41 -04001823// This target is deprecated, DebugGLContext is not well maintained.
Kevin Lubick27d42192018-04-03 12:30:32 -04001824// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001825DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001826 sk_gpu_test::GrContextFactory f;
1827 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001828}
Kevin Lubick30709262018-04-02 11:06:41 -04001829
Kevin Lubick27d42192018-04-03 12:30:32 -04001830DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001831 sk_gpu_test::GrContextFactory f;
1832 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1833}
Hal Canary44801ca2017-03-15 11:39:06 -04001834#endif
1835
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001836DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001837 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001838 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001839 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1840 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001841}
1842
1843// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1844DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001845 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001846 fuzz_canvas(fuzz, &debugCanvas);
1847 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1848 UrlDataManager dataManager(SkString("data"));
1849 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1850 Json::StyledStreamWriter(" ").write(std::cout, json);
1851}