blob: 8e84e88b3c15071970e81202ce09821f3eb3977c [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 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 Canary1e0138b2017-03-10 13:56:08 -050078// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
79// In fact, all make_fuzz_foo() functions have this potential problem.
80// Use sequence points!
81template <typename T>
82inline T make_fuzz_t(Fuzz* fuzz) {
83 T t;
84 fuzz->next(&t);
85 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050086}
87
Hal Canaryb69c4b82017-03-08 11:02:40 -050088template <>
89inline void Fuzz::next(SkShader::TileMode* m) {
Hal Canaryf7005202017-03-10 08:48:28 -050090 fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -050091}
92
Hal Canaryb69c4b82017-03-08 11:02:40 -050093template <>
Hal Canarye03c3e52017-03-09 11:33:35 -050094inline void Fuzz::next(SkFilterQuality* q) {
Hal Canaryf7005202017-03-10 08:48:28 -050095 fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
96 SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -050097}
98
99template <>
Hal Canaryb69c4b82017-03-08 11:02:40 -0500100inline void Fuzz::next(SkMatrix* m) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500101 constexpr int kArrayLength = 9;
102 SkScalar buffer[kArrayLength];
103 int matrixType;
104 this->nextRange(&matrixType, 0, 4);
105 switch (matrixType) {
106 case 0: // identity
107 *m = SkMatrix::I();
108 return;
109 case 1: // translate
110 this->nextRange(&buffer[0], -4000.0f, 4000.0f);
111 this->nextRange(&buffer[1], -4000.0f, 4000.0f);
112 *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
113 return;
114 case 2: // translate + scale
115 this->nextRange(&buffer[0], -400.0f, 400.0f);
116 this->nextRange(&buffer[1], -400.0f, 400.0f);
117 this->nextRange(&buffer[2], -4000.0f, 4000.0f);
118 this->nextRange(&buffer[3], -4000.0f, 4000.0f);
119 *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
120 m->postTranslate(buffer[2], buffer[3]);
121 return;
122 case 3: // affine
123 this->nextN(buffer, 6);
124 m->setAffine(buffer);
125 return;
126 case 4: // perspective
127 this->nextN(buffer, kArrayLength);
128 m->set9(buffer);
129 return;
130 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500131 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500132 return;
133 }
134}
135
Hal Canaryb69c4b82017-03-08 11:02:40 -0500136template <>
137inline void Fuzz::next(SkRRect* rr) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500138 SkRect r;
139 SkVector radii[4];
140 this->next(&r);
Hal Canary27bece82017-03-07 16:23:20 -0500141 r.sort();
142 for (SkVector& vec : radii) {
143 this->nextRange(&vec.fX, 0.0f, 1.0f);
144 vec.fX *= 0.5f * r.width();
145 this->nextRange(&vec.fY, 0.0f, 1.0f);
146 vec.fY *= 0.5f * r.height();
147 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500148 rr->setRectRadii(r, radii);
Hal Canary24ac42b2017-02-14 13:35:14 -0500149}
150
Hal Canaryb69c4b82017-03-08 11:02:40 -0500151template <>
152inline void Fuzz::next(SkBlendMode* mode) {
Hal Canaryf7005202017-03-10 08:48:28 -0500153 fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500154}
155
Hal Canary1e0138b2017-03-10 13:56:08 -0500156static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -0500157
Hal Canary1e0138b2017-03-10 13:56:08 -0500158static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500159
Hal Canary1e0138b2017-03-10 13:56:08 -0500160static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500161
Hal Canary1e0138b2017-03-10 13:56:08 -0500162static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500163 if (depth <= 0) {
164 return nullptr;
165 }
166 int colorFilterType;
167 fuzz->nextRange(&colorFilterType, 0, 8);
168 switch (colorFilterType) {
169 case 0:
170 return nullptr;
171 case 1: {
172 SkColor color;
173 SkBlendMode mode;
174 fuzz->next(&color, &mode);
175 return SkColorFilter::MakeModeFilter(color, mode);
176 }
177 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500178 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500179 if (!outer) {
180 return nullptr;
181 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500182 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500183 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500184 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500185 }
186 case 3: {
187 SkScalar array[20];
188 fuzz->nextN(array, SK_ARRAY_COUNT(array));
189 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
190 }
191 case 4: {
192 SkColor mul, add;
193 fuzz->next(&mul, &add);
194 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
195 }
196 case 5: {
197 bool grayscale;
198 int invertStyle;
199 float contrast;
200 fuzz->next(&grayscale);
201 fuzz->nextRange(&invertStyle, 0, 2);
202 fuzz->nextRange(&contrast, -1.0f, 1.0f);
203 return SkHighContrastFilter::Make(SkHighContrastConfig(
204 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
205 }
206 case 6:
207 return SkLumaColorFilter::Make();
208 case 7: {
209 uint8_t table[256];
210 fuzz->nextN(table, SK_ARRAY_COUNT(table));
211 return SkTableColorFilter::Make(table);
212 }
213 case 8: {
214 uint8_t tableA[256];
215 uint8_t tableR[256];
216 uint8_t tableG[256];
217 uint8_t tableB[256];
218 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
219 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
220 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
221 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
222 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
223 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500224 default:
225 SkASSERT(false);
226 break;
Hal Canary27bece82017-03-07 16:23:20 -0500227 }
228 return nullptr;
229}
Hal Canary24ac42b2017-02-14 13:35:14 -0500230
Hal Canary1e0138b2017-03-10 13:56:08 -0500231static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500232 SkScalar totalPos = 0;
233 for (int i = 0; i < colorCount; ++i) {
234 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
235 totalPos += pos[i];
236 }
237 totalPos = 1.0f / totalPos;
238 for (int i = 0; i < colorCount; ++i) {
239 pos[i] *= totalPos;
240 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500241 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500242 pos[colorCount - 1] = 1.0f;
243}
244
Hal Canary1e0138b2017-03-10 13:56:08 -0500245static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500246 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
247 sk_sp<SkColorFilter> colorFilter(nullptr);
248 SkBitmap bitmap;
249 sk_sp<SkImage> img;
250 SkShader::TileMode tmX, tmY;
251 bool useMatrix;
252 SkColor color;
253 SkMatrix matrix;
254 SkBlendMode blendMode;
255 int shaderType;
256 if (depth <= 0) {
257 return nullptr;
258 }
Hal Canary671e4422017-02-27 13:36:38 -0500259 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500260 switch (shaderType) {
261 case 0:
262 return nullptr;
263 case 1:
264 return SkShader::MakeEmptyShader();
265 case 2:
266 fuzz->next(&color);
267 return SkShader::MakeColorShader(color);
268 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500269 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500270 fuzz->next(&tmX, &tmY, &useMatrix);
271 if (useMatrix) {
272 fuzz->next(&matrix);
273 }
274 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
275 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500276 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500277 fuzz->next(&tmX, &tmY, &useMatrix);
278 if (useMatrix) {
279 fuzz->next(&matrix);
280 }
281 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
282 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500283 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500284 fuzz->next(&matrix);
285 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
286 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500287 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
288 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500289 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
290 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500291 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
292 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500293 fuzz->next(&blendMode);
294 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500295 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500296 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500297 bool useTile;
298 SkRect tile;
299 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
300 if (useMatrix) {
301 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500302 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500303 if (useTile) {
304 fuzz->next(&tile);
305 }
306 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
307 useMatrix ? &matrix : nullptr,
308 useTile ? &tile : nullptr);
309 }
Hal Canary671e4422017-02-27 13:36:38 -0500310 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500311 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000312 // Deprecated SkGaussianEdgeShader
313 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500314 case 10: {
315 constexpr int kMaxColors = 12;
316 SkPoint pts[2];
317 SkColor colors[kMaxColors];
318 SkScalar pos[kMaxColors];
319 int colorCount;
320 bool usePos;
321 fuzz->nextN(pts, 2);
322 fuzz->nextRange(&colorCount, 2, kMaxColors);
323 fuzz->nextN(colors, colorCount);
324 fuzz->next(&tmX, &useMatrix, &usePos);
325 if (useMatrix) {
326 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500327 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500328 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500329 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500330 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500331 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
332 tmX, 0, useMatrix ? &matrix : nullptr);
333 }
334 case 11: {
335 constexpr int kMaxColors = 12;
336 SkPoint center;
337 SkScalar radius;
338 int colorCount;
339 bool usePos;
340 SkColor colors[kMaxColors];
341 SkScalar pos[kMaxColors];
342 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
343 fuzz->nextRange(&colorCount, 2, kMaxColors);
344 fuzz->nextN(colors, colorCount);
345 if (useMatrix) {
346 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500347 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500348 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500349 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500350 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500351 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
352 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
353 }
354 case 12: {
355 constexpr int kMaxColors = 12;
356 SkPoint start, end;
357 SkScalar startRadius, endRadius;
358 int colorCount;
359 bool usePos;
360 SkColor colors[kMaxColors];
361 SkScalar pos[kMaxColors];
362 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
363 fuzz->nextRange(&colorCount, 2, kMaxColors);
364 fuzz->nextN(colors, colorCount);
365 if (useMatrix) {
366 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500367 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500368 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500369 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500370 }
371 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
372 usePos ? pos : nullptr, colorCount, tmX, 0,
373 useMatrix ? &matrix : nullptr);
374 }
375 case 13: {
376 constexpr int kMaxColors = 12;
377 SkScalar cx, cy;
378 int colorCount;
379 bool usePos;
380 SkColor colors[kMaxColors];
381 SkScalar pos[kMaxColors];
382 fuzz->next(&cx, &cy, &useMatrix, &usePos);
383 fuzz->nextRange(&colorCount, 2, kMaxColors);
384 fuzz->nextN(colors, colorCount);
385 if (useMatrix) {
386 fuzz->next(&matrix);
387 }
388 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500389 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500390 }
391 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
392 0, useMatrix ? &matrix : nullptr);
393 }
394 case 14: {
395 SkScalar baseFrequencyX, baseFrequencyY, seed;
396 int numOctaves;
397 SkISize tileSize;
398 bool useTileSize, turbulence;
399 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
400 if (useTileSize) {
401 fuzz->next(&tileSize);
402 }
403 fuzz->nextRange(&numOctaves, 2, 7);
404 if (turbulence) {
405 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
406 numOctaves, seed,
407 useTileSize ? &tileSize : nullptr);
408 } else {
409 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
410 numOctaves, seed,
411 useTileSize ? &tileSize : nullptr);
412 }
413 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500414 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500415 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500416 break;
417 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500418 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500419}
420
Hal Canary1e0138b2017-03-10 13:56:08 -0500421static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500422 if (depth <= 0) {
423 return nullptr;
424 }
425 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400426 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500427 switch (pathEffectType) {
428 case 0: {
429 return nullptr;
430 }
431 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500432 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
433 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500434 return SkPathEffect::MakeSum(std::move(first), std::move(second));
435 }
436 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500437 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
438 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500439 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
440 }
441 case 3: {
442 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -0500443 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500444 SkScalar advance, phase;
445 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500446 SkPath1DPathEffect::Style style;
447 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
448 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500449 }
450 case 4: {
451 SkScalar width;
452 SkMatrix matrix;
453 fuzz->next(&width, &matrix);
454 return SkLine2DPathEffect::Make(width, matrix);
455 }
456 case 5: {
457 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -0500458 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500459 SkMatrix matrix;
460 fuzz->next(&matrix);
461 return SkPath2DPathEffect::Make(matrix, path);
462 }
463 case 6: {
464 SkScalar radius;
465 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500466 return SkCornerPathEffect::Make(radius);
467 }
Mike Reed40e7e652017-07-22 22:12:59 -0400468 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500469 SkScalar phase;
470 fuzz->next(&phase);
471 SkScalar intervals[20];
472 int count;
473 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
474 fuzz->nextN(intervals, count);
475 return SkDashPathEffect::Make(intervals, count, phase);
476 }
Mike Reed40e7e652017-07-22 22:12:59 -0400477 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500478 SkScalar segLength, dev;
479 uint32_t seed;
480 fuzz->next(&segLength, &dev, &seed);
481 return SkDiscretePathEffect::Make(segLength, dev, seed);
482 }
483 default:
484 SkASSERT(false);
485 return nullptr;
486 }
487}
Hal Canary24ac42b2017-02-14 13:35:14 -0500488
Hal Canary1e0138b2017-03-10 13:56:08 -0500489static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500490 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400491 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500492 switch (maskfilterType) {
493 case 0:
494 return nullptr;
495 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500496 SkBlurStyle blurStyle;
497 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500498 SkScalar sigma;
499 fuzz->next(&sigma);
500 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Hal Canary1e0138b2017-03-10 13:56:08 -0500501 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -0500502 fuzz->next(&occluder);
503 }
504 uint32_t flags;
Mike Reed1be1f8d2018-03-14 13:01:17 -0400505 fuzz->nextRange(&flags, 0, 1);
506 bool respectCTM = flags != 0;
507 return SkMaskFilter::MakeBlur(blurStyle, sigma, occluder, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500508 }
Hal Canary5395c592017-03-08 16:52:18 -0500509 default:
510 SkASSERT(false);
511 return nullptr;
512 }
513}
Hal Canary24ac42b2017-02-14 13:35:14 -0500514
Hal Canary1e0138b2017-03-10 13:56:08 -0500515static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
516 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500517 return nullptr;
518 }
519 auto fontMugger = SkFontMgr::RefDefault();
520 SkASSERT(fontMugger);
521 int familyCount = fontMugger->countFamilies();
522 int i, j;
523 fuzz->nextRange(&i, 0, familyCount - 1);
524 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
525 int styleCount = family->count();
526 fuzz->nextRange(&j, 0, styleCount - 1);
527 return sk_sp<SkTypeface>(family->createTypeface(j));
528}
Hal Canary24ac42b2017-02-14 13:35:14 -0500529
Hal Canarye03c3e52017-03-09 11:33:35 -0500530template <>
531inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
532 SkRect rect;
533 uint8_t flags;
534 this->next(&rect);
535 this->nextRange(&flags, 0, 0xF);
536 *cropRect = SkImageFilter::CropRect(rect, flags);
537}
538
Hal Canary1e0138b2017-03-10 13:56:08 -0500539static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500540
541static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
542 if (depth <= 0) {
543 return nullptr;
544 }
545 uint8_t imageFilterType;
546 fuzz->nextRange(&imageFilterType, 1, 6);
547 SkPoint3 p, q;
548 SkColor lightColor;
549 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
550 sk_sp<SkImageFilter> input;
551 SkImageFilter::CropRect cropRect;
552 bool useCropRect;
553 fuzz->next(&useCropRect);
554 if (useCropRect) {
555 fuzz->next(&cropRect);
556 }
557 switch (imageFilterType) {
558 case 1:
559 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500560 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500561 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
562 std::move(input),
563 useCropRect ? &cropRect : nullptr);
564 case 2:
565 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500566 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500567 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
568 std::move(input),
569 useCropRect ? &cropRect : nullptr);
570 case 3:
571 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500572 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500573 return SkLightingImageFilter::MakeSpotLitDiffuse(
574 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
575 std::move(input), useCropRect ? &cropRect : nullptr);
576 case 4:
577 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500578 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500579 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
580 shininess, std::move(input),
581 useCropRect ? &cropRect : nullptr);
582 case 5:
583 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500584 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500585 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
586 shininess, std::move(input),
587 useCropRect ? &cropRect : nullptr);
588 case 6:
589 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
590 &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::MakeSpotLitSpecular(
593 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
594 std::move(input), useCropRect ? &cropRect : nullptr);
595 default:
596 SkASSERT(false);
597 return nullptr;
598 }
599}
600
Hal Canary1e0138b2017-03-10 13:56:08 -0500601static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500602
Hal Canary1e0138b2017-03-10 13:56:08 -0500603static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500604 if (depth <= 0) {
605 return nullptr;
606 }
607 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500608 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500609 switch (imageFilterType) {
610 case 0:
611 return nullptr;
612 case 1: {
613 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500614 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500615 bool useCropRect;
616 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400617 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500618 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400619 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500620 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400621 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500622 useCropRect ? &cropRect : nullptr);
623 }
624 case 2: {
625 SkMatrix matrix;
626 SkFilterQuality quality;
627 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500628 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500629 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
630 }
631 case 3: {
632 SkRegion region;
633 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500634 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500635 bool useCropRect;
636 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400637 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500638 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400639 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500640 }
641 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
642 useCropRect ? &cropRect : nullptr);
643 }
644 case 4: {
645 float k1, k2, k3, k4;
646 bool enforcePMColor;
647 bool useCropRect;
648 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500649 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
650 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500651 SkImageFilter::CropRect cropRect;
652 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400653 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500654 }
655 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
656 std::move(background), std::move(foreground),
657 useCropRect ? &cropRect : nullptr);
658 }
659 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500660 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
661 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500662 bool useCropRect;
663 SkImageFilter::CropRect cropRect;
664 fuzz->next(&useCropRect);
665 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400666 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500667 }
668 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
669 useCropRect ? &cropRect : nullptr);
670 }
671 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500672 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
673 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500674 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
675 }
676 case 7: {
677 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400678 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
679 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500680 SkScalar scale;
681 bool useCropRect;
682 fuzz->next(&scale, &useCropRect);
683 SkImageFilter::CropRect cropRect;
684 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400685 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500686 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500687 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
688 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500689 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
690 std::move(displacement), std::move(color),
691 useCropRect ? &cropRect : nullptr);
692 }
693 case 8: {
694 SkScalar dx, dy, sigmaX, sigmaY;
695 SkColor color;
696 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500697 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500698 bool useCropRect;
699 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
700 SkImageFilter::CropRect cropRect;
701 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400702 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500703 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500704 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500705 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
706 std::move(input),
707 useCropRect ? &cropRect : nullptr);
708 }
709 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500710 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500711 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500712 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500713 SkRect srcRect, dstRect;
714 SkFilterQuality filterQuality;
715 fuzz->next(&srcRect, &dstRect, &filterQuality);
716 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
717 }
718 case 11:
719 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
720 case 12: {
721 SkRect srcRect;
722 SkScalar inset;
723 bool useCropRect;
724 SkImageFilter::CropRect cropRect;
725 fuzz->next(&srcRect, &inset, &useCropRect);
726 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400727 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500728 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500729 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500730 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
731 useCropRect ? &cropRect : nullptr);
732 }
733 case 13: {
734 constexpr int kMaxKernelSize = 5;
735 int32_t n, m;
736 fuzz->nextRange(&n, 1, kMaxKernelSize);
737 fuzz->nextRange(&m, 1, kMaxKernelSize);
738 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
739 fuzz->nextN(kernel, n * m);
740 int32_t offsetX, offsetY;
741 fuzz->nextRange(&offsetX, 0, n - 1);
742 fuzz->nextRange(&offsetY, 0, m - 1);
743 SkScalar gain, bias;
744 bool convolveAlpha, useCropRect;
745 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
746 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500747 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500748 SkImageFilter::CropRect cropRect;
749 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400750 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500751 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500752 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500753 return SkMatrixConvolutionImageFilter::Make(
754 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
755 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
756 }
757 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500758 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
759 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500760 bool useCropRect;
761 fuzz->next(&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 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400766 return SkMergeImageFilter::Make(std::move(first), std::move(second),
767 useCropRect ? &cropRect : nullptr);
768 }
769 case 15: {
770 constexpr int kMaxCount = 4;
771 sk_sp<SkImageFilter> ifs[kMaxCount];
772 int count;
773 fuzz->nextRange(&count, 1, kMaxCount);
774 for (int i = 0; i < count; ++i) {
775 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
776 }
777 bool useCropRect;
778 fuzz->next(&useCropRect);
779 SkImageFilter::CropRect cropRect;
780 if (useCropRect) {
781 fuzz->next(&cropRect);
782 }
783 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500784 }
785 case 16: {
786 int rx, ry;
787 fuzz->next(&rx, &ry);
788 bool useCropRect;
789 fuzz->next(&useCropRect);
790 SkImageFilter::CropRect cropRect;
791 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400792 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500793 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500794 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500795 return SkDilateImageFilter::Make(rx, ry, std::move(input),
796 useCropRect ? &cropRect : nullptr);
797 }
798 case 17: {
799 int rx, ry;
800 fuzz->next(&rx, &ry);
801 bool useCropRect;
802 fuzz->next(&useCropRect);
803 SkImageFilter::CropRect cropRect;
804 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400805 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500806 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500807 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500808 return SkErodeImageFilter::Make(rx, ry, std::move(input),
809 useCropRect ? &cropRect : nullptr);
810 }
811 case 18: {
812 SkScalar dx, dy;
813 fuzz->next(&dx, &dy);
814 bool useCropRect;
815 fuzz->next(&useCropRect);
816 SkImageFilter::CropRect cropRect;
817 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400818 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500819 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500820 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500821 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
822 useCropRect ? &cropRect : nullptr);
823 }
824 case 19: {
825 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500826 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500827 bool useCropRect;
828 fuzz->next(&useCropRect);
829 SkImageFilter::CropRect cropRect;
830 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400831 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500832 }
833 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
834 }
835 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500836 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500837 return SkPictureImageFilter::Make(std::move(picture));
838 }
839 case 21: {
840 SkRect cropRect;
841 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500842 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500843 return SkPictureImageFilter::Make(std::move(picture), cropRect);
844 }
845 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500846 SkRect src, dst;
847 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500848 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500849 return SkTileImageFilter::Make(src, dst, std::move(input));
850 }
Mike Reed77e487d2017-11-09 21:50:20 +0000851 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500852 SkBlendMode blendMode;
853 bool useCropRect;
854 fuzz->next(&useCropRect, &blendMode);
855 SkImageFilter::CropRect cropRect;
856 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400857 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500858 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500859 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
860 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500861 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
862 useCropRect ? &cropRect : nullptr);
863 }
864 default:
865 SkASSERT(false);
866 return nullptr;
867 }
868}
Hal Canary24ac42b2017-02-14 13:35:14 -0500869
Hal Canary1e0138b2017-03-10 13:56:08 -0500870static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500871 int w, h;
872 fuzz->nextRange(&w, 1, 1024);
873 fuzz->nextRange(&h, 1, 1024);
874 SkAutoTMalloc<SkPMColor> data(w * h);
875 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
876 int n = w * h;
877 for (int i = 0; i < n; ++i) {
878 SkColor c;
879 fuzz->next(&c);
880 data[i] = SkPreMultiplyColor(c);
881 }
882 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500883 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
884 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500885}
886
Hal Canary1e0138b2017-03-10 13:56:08 -0500887static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500888 SkBitmap bitmap;
889 int w, h;
890 fuzz->nextRange(&w, 1, 1024);
891 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500892 if (!bitmap.tryAllocN32Pixels(w, h)) {
893 SkDEBUGF(("Could not allocate pixels %d x %d", w, h));
894 return bitmap;
895 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500896 for (int y = 0; y < h; ++y) {
897 for (int x = 0; x < w; ++x) {
898 SkColor c;
899 fuzz->next(&c);
900 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
901 }
902 }
903 return bitmap;
904}
905
Hal Canary1e0138b2017-03-10 13:56:08 -0500906template <typename T, typename Min, typename Max>
907inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
908 T value;
909 fuzz_enum_range(fuzz, &value, minv, maxv);
910 return value;
911}
912
913static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500914 if (!fuzz || !paint || depth <= 0) {
915 return;
916 }
917
Hal Canary1e0138b2017-03-10 13:56:08 -0500918 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
919 paint->setDither( make_fuzz_t<bool>(fuzz));
920 paint->setColor( make_fuzz_t<SkColor>(fuzz));
921 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
922 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
923 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
924 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
925 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
926 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
927 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
928 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500929
930 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500931 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
932 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
933 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
934 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500935 }
936}
937
Hal Canary1e0138b2017-03-10 13:56:08 -0500938static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
939 paint->setTypeface( make_fuzz_typeface(fuzz));
940 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
941 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
942 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
943 paint->setLinearText( make_fuzz_t<bool>(fuzz));
944 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
945 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
946 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
947 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
948 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
949 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
950 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
951 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
952 SkPaint::kFull_Hinting));
953 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -0500954}
955
956static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500957 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -0500958}
959
Hal Canary5395c592017-03-08 16:52:18 -0500960constexpr int kMaxGlyphCount = 30;
961
Hal Canary1e0138b2017-03-10 13:56:08 -0500962static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -0500963 SkTDArray<uint8_t> array;
964 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500965 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
966 : SkTypeface::MakeDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500967 if (glyphRange == 0) {
968 // Some fuzzing environments have no fonts, so empty array is the best
969 // we can do.
970 return array;
971 }
Hal Canary671e4422017-02-27 13:36:38 -0500972 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500973 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500974 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
975 for (int i = 0; i < glyphCount; ++i) {
976 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
977 }
978 return array;
979 }
980 static const SkUnichar ranges[][2] = {
981 {0x0020, 0x007F},
982 {0x00A1, 0x0250},
983 {0x0400, 0x0500},
984 };
985 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500986 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500987 count += (ranges[i][1] - ranges[i][0]);
988 }
Hal Canary5395c592017-03-08 16:52:18 -0500989 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500990 SkUnichar buffer[kMaxLength];
991 int length;
992 fuzz->nextRange(&length, 1, kMaxLength);
993 for (int j = 0; j < length; ++j) {
994 int32_t value;
995 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500996 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500997 if (value + ranges[i][0] < ranges[i][1]) {
998 buffer[j] = value + ranges[i][0];
999 break;
1000 } else {
1001 value -= (ranges[i][1] - ranges[i][0]);
1002 }
1003 }
1004 }
1005 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001006 case SkPaint::kUTF8_TextEncoding: {
1007 size_t utf8len = 0;
1008 for (int j = 0; j < length; ++j) {
1009 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001010 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001011 char* ptr = (char*)array.append(utf8len);
1012 for (int j = 0; j < length; ++j) {
1013 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001014 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001015 } break;
1016 case SkPaint::kUTF16_TextEncoding: {
1017 size_t utf16len = 0;
1018 for (int j = 0; j < length; ++j) {
1019 utf16len += SkUTF16_FromUnichar(buffer[j]);
1020 }
1021 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1022 for (int j = 0; j < length; ++j) {
1023 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1024 }
1025 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001026 case SkPaint::kUTF32_TextEncoding:
1027 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001028 break;
Hal Canary671e4422017-02-27 13:36:38 -05001029 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001030 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001031 break;
Hal Canary671e4422017-02-27 13:36:38 -05001032 }
1033 return array;
1034}
1035
Hal Canary5395c592017-03-08 16:52:18 -05001036static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1037 SkTextBlobBuilder textBlobBuilder;
1038 int8_t runCount;
1039 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1040 while (runCount-- > 0) {
1041 SkPaint paint;
1042 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001043 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001044 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1045 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1046 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1047 SkASSERT(glyphCount <= kMaxGlyphCount);
1048 SkScalar x, y;
1049 const SkTextBlobBuilder::RunBuffer* buffer;
1050 uint8_t runType;
1051 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1052 switch (runType) {
1053 case 0:
1054 fuzz->next(&x, &y);
1055 // TODO: Test other variations of this.
1056 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1057 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1058 break;
1059 case 1:
1060 fuzz->next(&y);
1061 // TODO: Test other variations of this.
1062 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1063 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1064 fuzz->nextN(buffer->pos, glyphCount);
1065 break;
1066 case 2:
1067 // TODO: Test other variations of this.
1068 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1069 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1070 fuzz->nextN(buffer->pos, glyphCount * 2);
1071 break;
1072 default:
1073 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001074 break;
Hal Canary5395c592017-03-08 16:52:18 -05001075 }
1076 }
1077 return textBlobBuilder.make();
1078}
1079
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001080extern std::atomic<bool> gSkUseDeltaAA;
1081extern std::atomic<bool> gSkForceDeltaAA;
1082
Hal Canary1e0138b2017-03-10 13:56:08 -05001083static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001084 if (!fuzz || !canvas || depth <= 0) {
1085 return;
1086 }
1087 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001088 bool useDAA;
1089 fuzz->next(&useDAA);
1090 if (useDAA) {
1091 gSkForceDeltaAA = true;
1092 gSkUseDeltaAA = true;
1093 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001094 unsigned N;
1095 fuzz->nextRange(&N, 0, 2000);
1096 for (unsigned i = 0; i < N; ++i) {
1097 if (fuzz->exhausted()) {
1098 return;
1099 }
1100 SkPaint paint;
1101 SkMatrix matrix;
1102 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001103 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001104 switch (drawCommand) {
1105 case 0:
1106 canvas->flush();
1107 break;
1108 case 1:
1109 canvas->save();
1110 break;
1111 case 2: {
1112 SkRect bounds;
1113 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001114 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001115 canvas->saveLayer(&bounds, &paint);
1116 break;
1117 }
1118 case 3: {
1119 SkRect bounds;
1120 fuzz->next(&bounds);
1121 canvas->saveLayer(&bounds, nullptr);
1122 break;
1123 }
1124 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001125 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001126 canvas->saveLayer(nullptr, &paint);
1127 break;
1128 case 5:
1129 canvas->saveLayer(nullptr, nullptr);
1130 break;
1131 case 6: {
1132 uint8_t alpha;
1133 fuzz->next(&alpha);
1134 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1135 break;
1136 }
1137 case 7: {
1138 SkRect bounds;
1139 uint8_t alpha;
1140 fuzz->next(&bounds, &alpha);
1141 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1142 break;
1143 }
1144 case 8: {
1145 SkCanvas::SaveLayerRec saveLayerRec;
1146 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001147 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001148 fuzz->next(&bounds);
1149 saveLayerRec.fBounds = &bounds;
1150 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001151 if (make_fuzz_t<bool>(fuzz)) {
1152 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001153 saveLayerRec.fPaint = &paint;
1154 }
1155 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001156 if (make_fuzz_t<bool>(fuzz)) {
1157 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001158 saveLayerRec.fBackdrop = imageFilter.get();
1159 }
Hal Canary5395c592017-03-08 16:52:18 -05001160 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001161 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001162 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1163 // }
1164
Hal Canary24ac42b2017-02-14 13:35:14 -05001165 canvas->saveLayer(saveLayerRec);
1166 break;
1167 }
1168 case 9:
1169 canvas->restore();
1170 break;
1171 case 10: {
1172 int saveCount;
1173 fuzz->next(&saveCount);
1174 canvas->restoreToCount(saveCount);
1175 break;
1176 }
1177 case 11: {
1178 SkScalar x, y;
1179 fuzz->next(&x, &y);
1180 canvas->translate(x, y);
1181 break;
1182 }
1183 case 12: {
1184 SkScalar x, y;
1185 fuzz->next(&x, &y);
1186 canvas->scale(x, y);
1187 break;
1188 }
1189 case 13: {
1190 SkScalar v;
1191 fuzz->next(&v);
1192 canvas->rotate(v);
1193 break;
1194 }
1195 case 14: {
1196 SkScalar x, y, v;
1197 fuzz->next(&x, &y, &v);
1198 canvas->rotate(v, x, y);
1199 break;
1200 }
1201 case 15: {
1202 SkScalar x, y;
1203 fuzz->next(&x, &y);
1204 canvas->skew(x, y);
1205 break;
1206 }
1207 case 16: {
1208 SkMatrix mat;
1209 fuzz->next(&mat);
1210 canvas->concat(mat);
1211 break;
1212 }
1213 case 17: {
1214 SkMatrix mat;
1215 fuzz->next(&mat);
1216 canvas->setMatrix(mat);
1217 break;
1218 }
1219 case 18:
1220 canvas->resetMatrix();
1221 break;
1222 case 19: {
1223 SkRect r;
1224 int op;
1225 bool doAntiAlias;
1226 fuzz->next(&r, &doAntiAlias);
1227 fuzz->nextRange(&op, 0, 1);
1228 r.sort();
1229 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1230 break;
1231 }
1232 case 20: {
1233 SkRRect rr;
1234 int op;
1235 bool doAntiAlias;
1236 fuzz->next(&rr);
1237 fuzz->next(&doAntiAlias);
1238 fuzz->nextRange(&op, 0, 1);
1239 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1240 break;
1241 }
1242 case 21: {
1243 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001244 FuzzPath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001245 int op;
1246 bool doAntiAlias;
1247 fuzz->next(&doAntiAlias);
1248 fuzz->nextRange(&op, 0, 1);
1249 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1250 break;
1251 }
1252 case 22: {
1253 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001254 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001255 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001256 fuzz->nextRange(&op, 0, 1);
1257 canvas->clipRegion(region, (SkClipOp)op);
1258 break;
1259 }
1260 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001261 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001262 canvas->drawPaint(paint);
1263 break;
1264 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001265 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001266 SkCanvas::PointMode pointMode;
1267 fuzz_enum_range(fuzz, &pointMode,
1268 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001269 size_t count;
1270 constexpr int kMaxCount = 30;
1271 fuzz->nextRange(&count, 0, kMaxCount);
1272 SkPoint pts[kMaxCount];
1273 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001274 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001275 break;
1276 }
1277 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001278 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001279 SkRect r;
1280 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001281 if (!r.isFinite()) {
1282 break;
1283 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001284 canvas->drawRect(r, paint);
1285 break;
1286 }
1287 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001288 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001289 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001290 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001291 canvas->drawRegion(region, paint);
1292 break;
1293 }
1294 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001295 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001296 SkRect r;
1297 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001298 if (!r.isFinite()) {
1299 break;
1300 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001301 canvas->drawOval(r, paint);
1302 break;
1303 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001304 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001305 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001306 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001307 SkRRect rr;
1308 fuzz->next(&rr);
1309 canvas->drawRRect(rr, paint);
1310 break;
1311 }
1312 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001313 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001314 SkRRect orr, irr;
1315 fuzz->next(&orr);
1316 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001317 if (orr.getBounds().contains(irr.getBounds())) {
1318 canvas->drawDRRect(orr, irr, paint);
1319 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001320 break;
1321 }
1322 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001323 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001324 SkRect r;
1325 SkScalar start, sweep;
1326 bool useCenter;
1327 fuzz->next(&r, &start, &sweep, &useCenter);
1328 canvas->drawArc(r, start, sweep, useCenter, paint);
1329 break;
1330 }
1331 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001332 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001333 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001334 FuzzPath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001335 canvas->drawPath(path, paint);
1336 break;
1337 }
1338 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001339 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001340 SkScalar left, top;
1341 bool usePaint;
1342 fuzz->next(&left, &top, &usePaint);
1343 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001344 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001345 }
1346 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1347 break;
1348 }
1349 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001350 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001351 SkRect src, dst;
1352 bool usePaint;
1353 fuzz->next(&src, &dst, &usePaint);
1354 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001356 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001357 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001358 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1359 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001360 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1361 break;
1362 }
1363 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001364 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001365 SkIRect src;
1366 SkRect dst;
1367 bool usePaint;
1368 fuzz->next(&src, &dst, &usePaint);
1369 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001370 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001371 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001372 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001373 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1374 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001375 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1376 break;
1377 }
1378 case 36: {
1379 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001380 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001381 SkRect dst;
1382 fuzz->next(&dst, &usePaint);
1383 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001384 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001385 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001386 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001387 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1388 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001389 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1390 break;
1391 }
1392 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001393 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001394 SkIRect center;
1395 SkRect dst;
1396 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001397 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001398 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001399 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001400 }
Hal Canary0361d492017-03-15 12:58:15 -04001401 if (make_fuzz_t<bool>(fuzz)) {
1402 fuzz->next(&center);
1403 } else { // Make valid center, see SkLatticeIter::Valid().
1404 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1405 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1406 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1407 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1408 }
1409 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001410 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1411 break;
1412 }
1413 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001414 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001415 SkScalar left, top;
1416 bool usePaint;
1417 fuzz->next(&left, &top, &usePaint);
1418 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001419 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001420 }
1421 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1422 break;
1423 }
1424 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001425 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001426 SkRect src, dst;
1427 bool usePaint;
1428 fuzz->next(&src, &dst, &usePaint);
1429 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001430 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001431 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001432 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001433 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1434 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001435 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1436 break;
1437 }
1438 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001439 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001440 SkIRect src;
1441 SkRect dst;
1442 bool usePaint;
1443 fuzz->next(&src, &dst, &usePaint);
1444 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001445 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001446 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001447 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001448 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1449 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001450 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1451 break;
1452 }
1453 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001454 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001455 SkRect dst;
1456 bool usePaint;
1457 fuzz->next(&dst, &usePaint);
1458 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001459 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001460 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001461 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001462 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1463 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001464 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1465 break;
1466 }
1467 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001468 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001469 SkIRect center;
1470 SkRect dst;
1471 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001472 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001473 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001474 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001475 }
Hal Canary0361d492017-03-15 12:58:15 -04001476 if (make_fuzz_t<bool>(fuzz)) {
1477 fuzz->next(&center);
1478 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001479 if (img.width() == 0 || img.height() == 0) {
1480 // bitmap may not have had its pixels initialized.
1481 break;
1482 }
Hal Canary0361d492017-03-15 12:58:15 -04001483 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1484 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1485 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1486 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1487 }
1488 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001489 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1490 break;
1491 }
1492 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001493 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001494 bool usePaint;
1495 SkRect dst;
1496 fuzz->next(&usePaint, &dst);
1497 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001498 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001499 }
1500 constexpr int kMax = 6;
1501 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001502 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001503 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1504 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1505 fuzz->nextN(xDivs, lattice.fXCount);
1506 fuzz->nextN(yDivs, lattice.fYCount);
1507 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1508 break;
1509 }
1510 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001511 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001512 bool usePaint;
1513 SkRect dst;
1514 fuzz->next(&usePaint, &dst);
1515 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001516 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001517 }
1518 constexpr int kMax = 6;
1519 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001520 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001521 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1522 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1523 fuzz->nextN(xDivs, lattice.fXCount);
1524 fuzz->nextN(yDivs, lattice.fYCount);
1525 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1526 break;
1527 }
1528 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001529 fuzz_paint(fuzz, &paint, depth - 1);
1530 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001531 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001532 SkScalar x, y;
1533 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001534 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001535 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001536 break;
1537 }
1538 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001539 fuzz_paint(fuzz, &paint, depth - 1);
1540 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001541 fuzz_paint_text_encoding(fuzz, &paint);
1542 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001543 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001544 if (glyphCount < 1) {
1545 break;
1546 }
1547 SkAutoTMalloc<SkPoint> pos(glyphCount);
1548 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001549 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001550 pos[0] = {0, 0};
1551 for (int i = 1; i < glyphCount; ++i) {
1552 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001553 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001554 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1555 }
Hal Canary671e4422017-02-27 13:36:38 -05001556 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001557 break;
1558 }
1559 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001560 fuzz_paint(fuzz, &paint, depth - 1);
1561 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001562 fuzz_paint_text_encoding(fuzz, &paint);
1563 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001564 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001565 SkAutoTMalloc<SkScalar> widths(glyphCount);
1566 if (glyphCount < 1) {
1567 break;
1568 }
Hal Canary671e4422017-02-27 13:36:38 -05001569 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001570 SkScalar x = widths[0];
1571 for (int i = 0; i < glyphCount; ++i) {
1572 SkTSwap(x, widths[i]);
1573 x += widths[i];
1574 SkScalar offset;
1575 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001576 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001577 widths[i] += offset;
1578 }
1579 SkScalar y;
1580 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001581 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001582 break;
1583 }
1584 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001585 fuzz_paint(fuzz, &paint, depth - 1);
1586 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001587 fuzz_paint_text_encoding(fuzz, &paint);
1588 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001589 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001590 FuzzPath(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001591 SkScalar hOffset, vOffset;
1592 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001593 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1594 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001595 break;
1596 }
1597 case 49: {
1598 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001599 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001600 if (useMatrix) {
1601 fuzz->next(&matrix);
1602 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001603 fuzz_paint(fuzz, &paint, depth - 1);
1604 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001605 fuzz_paint_text_encoding(fuzz, &paint);
1606 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001607 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001608 FuzzPath(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001609 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001610 useMatrix ? &matrix : nullptr, paint);
1611 break;
1612 }
1613 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001614 fuzz_paint(fuzz, &paint, depth - 1);
1615 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001616 fuzz_paint_text_encoding(fuzz, &paint);
1617 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1618 SkRSXform rSXform[kMaxGlyphCount];
1619 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1620 SkASSERT(glyphCount <= kMaxGlyphCount);
1621 fuzz->nextN(rSXform, glyphCount);
1622 SkRect cullRect;
1623 bool useCullRect;
1624 fuzz->next(&useCullRect);
1625 if (useCullRect) {
1626 fuzz->next(&cullRect);
1627 }
1628 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1629 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001630 break;
1631 }
1632 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001633 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001634 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001635 SkScalar x, y;
1636 fuzz->next(&x, &y);
1637 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001638 break;
1639 }
1640 case 52: {
1641 bool usePaint, useMatrix;
1642 fuzz->next(&usePaint, &useMatrix);
1643 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001644 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001645 }
1646 if (useMatrix) {
1647 fuzz->next(&matrix);
1648 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001649 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001650 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1651 usePaint ? &paint : nullptr);
1652 break;
1653 }
1654 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001655 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001656 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001657 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001658 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001659 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001660 constexpr int kMaxCount = 100;
1661 int vertexCount;
1662 SkPoint vertices[kMaxCount];
1663 SkPoint texs[kMaxCount];
1664 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001665 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1666 fuzz->nextN(vertices, vertexCount);
1667 bool useTexs, useColors;
1668 fuzz->next(&useTexs, &useColors);
1669 if (useTexs) {
1670 fuzz->nextN(texs, vertexCount);
1671 }
1672 if (useColors) {
1673 fuzz->nextN(colors, vertexCount);
1674 }
1675 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001676 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001677 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001678 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1679 for (int i = 0; i < indexCount; ++i) {
1680 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1681 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001682 }
Mike Reed887cdf12017-04-03 11:11:09 -04001683 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1684 useTexs ? texs : nullptr,
1685 useColors ? colors : nullptr,
1686 indexCount, indices),
1687 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001688 break;
1689 }
1690 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001691 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001692 break;
1693 }
1694 }
1695}
1696
Hal Canary1e0138b2017-03-10 13:56:08 -05001697static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001698 SkScalar w, h;
1699 fuzz->next(&w, &h);
1700 SkPictureRecorder pictureRecorder;
1701 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1702 return pictureRecorder.finishRecordingAsPicture();
1703}
1704
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001705DEF_FUZZ(NullCanvas, fuzz) {
1706 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001707}
1708
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001709constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001710
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001711DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001712 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001713 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001714 fuzz_canvas(fuzz, surface->getCanvas());
1715}
1716
Hal Canarye2924432017-12-01 11:46:26 -05001717DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1718 SkPictureRecorder recorder;
1719 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1720 SkIntToScalar(kCanvasSize.height())));
1721 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1722 if (!pic) { fuzz->signalBug(); }
1723 sk_sp<SkData> data = pic->serialize();
1724 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001725 SkReadBuffer rb(data->data(), data->size());
1726 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001727 if (!deserialized) { fuzz->signalBug(); }
1728 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1729 SkASSERT(surface && surface->getCanvas());
1730 surface->getCanvas()->drawPicture(deserialized);
1731}
1732
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001733DEF_FUZZ(ImageFilter, fuzz) {
1734 auto fil = make_fuzz_imageFilter(fuzz, 20);
1735
1736 SkPaint paint;
1737 paint.setImageFilter(fil);
1738 SkBitmap bitmap;
1739 SkCanvas canvas(bitmap);
1740 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1741}
1742
1743
1744//SkRandom _rand;
1745#define SK_ADD_RANDOM_BIT_FLIPS
1746
1747DEF_FUZZ(SerializedImageFilter, fuzz) {
1748 auto filter = make_fuzz_imageFilter(fuzz, 20);
1749 auto data = filter->serialize();
1750 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1751 size_t len = data->size();
1752#ifdef SK_ADD_RANDOM_BIT_FLIPS
1753 unsigned char* p = const_cast<unsigned char*>(ptr);
1754 for (size_t i = 0; i < len; ++i, ++p) {
1755 uint8_t j;
1756 fuzz->nextRange(&j, 1, 250);
1757 if (j == 1) { // 0.4% of the time, flip a bit or byte
1758 uint8_t k;
1759 fuzz->nextRange(&k, 1, 10);
1760 if (k == 1) { // Then 10% of the time, change a whole byte
1761 uint8_t s;
1762 fuzz->nextRange(&s, 0, 2);
1763 switch(s) {
1764 case 0:
1765 *p ^= 0xFF; // Flip entire byte
1766 break;
1767 case 1:
1768 *p = 0xFF; // Set all bits to 1
1769 break;
1770 case 2:
1771 *p = 0x00; // Set all bits to 0
1772 break;
1773 }
1774 } else {
1775 uint8_t s;
1776 fuzz->nextRange(&s, 0, 7);
1777 *p ^= (1 << 7);
1778 }
1779 }
1780 }
1781#endif // SK_ADD_RANDOM_BIT_FLIPS
1782 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1783
1784 // uncomment below to write out a serialized image filter (to make corpus
1785 // for -t filter_fuzz)
1786 // SkString s("./serialized_filters/sf");
1787 // s.appendU32(_rand.nextU());
1788 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1789 // sk_fwrite(data->bytes(), data->size(), file);
1790 // sk_fclose(file);
1791
1792 SkPaint paint;
1793 paint.setImageFilter(deserializedFil);
1794 SkBitmap bitmap;
1795 SkCanvas canvas(bitmap);
1796 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1797}
1798
Hal Canary44801ca2017-03-15 11:39:06 -04001799#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001800static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1801 SkASSERT(context);
1802 auto surface = SkSurface::MakeRenderTarget(
1803 context,
1804 SkBudgeted::kNo,
1805 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1806 SkASSERT(surface && surface->getCanvas());
1807 fuzz_canvas(fuzz, surface->getCanvas());
1808}
1809
Hal Canary44801ca2017-03-15 11:39:06 -04001810DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001811 sk_gpu_test::GrContextFactory f;
1812 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001813 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001814 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001815 }
Hal Canary5aa91582017-03-21 11:11:44 -07001816 fuzz_ganesh(fuzz, context);
1817}
1818
Kevin Lubick27d42192018-04-03 12:30:32 -04001819// This target is deprecated, NullGLContext is not well maintained.
1820// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001821DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001822 sk_gpu_test::GrContextFactory f;
1823 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001824}
1825
Kevin Lubick30709262018-04-02 11:06:41 -04001826// This target is deprecated, DebugGLContext is not well maintained.
Kevin Lubick27d42192018-04-03 12:30:32 -04001827// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001828DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001829 sk_gpu_test::GrContextFactory f;
1830 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001831}
Kevin Lubick30709262018-04-02 11:06:41 -04001832
Kevin Lubick27d42192018-04-03 12:30:32 -04001833DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001834 sk_gpu_test::GrContextFactory f;
1835 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1836}
Hal Canary44801ca2017-03-15 11:39:06 -04001837#endif
1838
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001839DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001840 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001841 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001842 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1843 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001844}
1845
1846// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1847DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001848 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001849 fuzz_canvas(fuzz, &debugCanvas);
1850 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1851 UrlDataManager dataManager(SkString("data"));
1852 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1853 Json::StyledStreamWriter(" ").write(std::cout, json);
1854}