blob: b843804460462142e31d2e405dd5bcb243844385 [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
Hal Canary1e0138b2017-03-10 13:56:08 -05001080static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001081 if (!fuzz || !canvas || depth <= 0) {
1082 return;
1083 }
1084 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1085 unsigned N;
1086 fuzz->nextRange(&N, 0, 2000);
1087 for (unsigned i = 0; i < N; ++i) {
1088 if (fuzz->exhausted()) {
1089 return;
1090 }
1091 SkPaint paint;
1092 SkMatrix matrix;
1093 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001094 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001095 switch (drawCommand) {
1096 case 0:
1097 canvas->flush();
1098 break;
1099 case 1:
1100 canvas->save();
1101 break;
1102 case 2: {
1103 SkRect bounds;
1104 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001105 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001106 canvas->saveLayer(&bounds, &paint);
1107 break;
1108 }
1109 case 3: {
1110 SkRect bounds;
1111 fuzz->next(&bounds);
1112 canvas->saveLayer(&bounds, nullptr);
1113 break;
1114 }
1115 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001116 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001117 canvas->saveLayer(nullptr, &paint);
1118 break;
1119 case 5:
1120 canvas->saveLayer(nullptr, nullptr);
1121 break;
1122 case 6: {
1123 uint8_t alpha;
1124 fuzz->next(&alpha);
1125 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1126 break;
1127 }
1128 case 7: {
1129 SkRect bounds;
1130 uint8_t alpha;
1131 fuzz->next(&bounds, &alpha);
1132 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1133 break;
1134 }
1135 case 8: {
1136 SkCanvas::SaveLayerRec saveLayerRec;
1137 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001138 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001139 fuzz->next(&bounds);
1140 saveLayerRec.fBounds = &bounds;
1141 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001142 if (make_fuzz_t<bool>(fuzz)) {
1143 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001144 saveLayerRec.fPaint = &paint;
1145 }
1146 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001147 if (make_fuzz_t<bool>(fuzz)) {
1148 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001149 saveLayerRec.fBackdrop = imageFilter.get();
1150 }
Hal Canary5395c592017-03-08 16:52:18 -05001151 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001152 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001153 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1154 // }
1155
Hal Canary24ac42b2017-02-14 13:35:14 -05001156 canvas->saveLayer(saveLayerRec);
1157 break;
1158 }
1159 case 9:
1160 canvas->restore();
1161 break;
1162 case 10: {
1163 int saveCount;
1164 fuzz->next(&saveCount);
1165 canvas->restoreToCount(saveCount);
1166 break;
1167 }
1168 case 11: {
1169 SkScalar x, y;
1170 fuzz->next(&x, &y);
1171 canvas->translate(x, y);
1172 break;
1173 }
1174 case 12: {
1175 SkScalar x, y;
1176 fuzz->next(&x, &y);
1177 canvas->scale(x, y);
1178 break;
1179 }
1180 case 13: {
1181 SkScalar v;
1182 fuzz->next(&v);
1183 canvas->rotate(v);
1184 break;
1185 }
1186 case 14: {
1187 SkScalar x, y, v;
1188 fuzz->next(&x, &y, &v);
1189 canvas->rotate(v, x, y);
1190 break;
1191 }
1192 case 15: {
1193 SkScalar x, y;
1194 fuzz->next(&x, &y);
1195 canvas->skew(x, y);
1196 break;
1197 }
1198 case 16: {
1199 SkMatrix mat;
1200 fuzz->next(&mat);
1201 canvas->concat(mat);
1202 break;
1203 }
1204 case 17: {
1205 SkMatrix mat;
1206 fuzz->next(&mat);
1207 canvas->setMatrix(mat);
1208 break;
1209 }
1210 case 18:
1211 canvas->resetMatrix();
1212 break;
1213 case 19: {
1214 SkRect r;
1215 int op;
1216 bool doAntiAlias;
1217 fuzz->next(&r, &doAntiAlias);
1218 fuzz->nextRange(&op, 0, 1);
1219 r.sort();
1220 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1221 break;
1222 }
1223 case 20: {
1224 SkRRect rr;
1225 int op;
1226 bool doAntiAlias;
1227 fuzz->next(&rr);
1228 fuzz->next(&doAntiAlias);
1229 fuzz->nextRange(&op, 0, 1);
1230 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1231 break;
1232 }
1233 case 21: {
1234 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001235 FuzzPath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001236 int op;
1237 bool doAntiAlias;
1238 fuzz->next(&doAntiAlias);
1239 fuzz->nextRange(&op, 0, 1);
1240 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1241 break;
1242 }
1243 case 22: {
1244 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001245 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001246 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001247 fuzz->nextRange(&op, 0, 1);
1248 canvas->clipRegion(region, (SkClipOp)op);
1249 break;
1250 }
1251 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001252 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001253 canvas->drawPaint(paint);
1254 break;
1255 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001256 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001257 SkCanvas::PointMode pointMode;
1258 fuzz_enum_range(fuzz, &pointMode,
1259 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001260 size_t count;
1261 constexpr int kMaxCount = 30;
1262 fuzz->nextRange(&count, 0, kMaxCount);
1263 SkPoint pts[kMaxCount];
1264 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001265 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001266 break;
1267 }
1268 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001269 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001270 SkRect r;
1271 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001272 if (!r.isFinite()) {
1273 break;
1274 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001275 canvas->drawRect(r, paint);
1276 break;
1277 }
1278 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001279 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001280 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001281 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001282 canvas->drawRegion(region, paint);
1283 break;
1284 }
1285 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001286 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001287 SkRect r;
1288 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001289 if (!r.isFinite()) {
1290 break;
1291 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001292 canvas->drawOval(r, paint);
1293 break;
1294 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001295 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001296 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001297 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001298 SkRRect rr;
1299 fuzz->next(&rr);
1300 canvas->drawRRect(rr, paint);
1301 break;
1302 }
1303 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001304 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001305 SkRRect orr, irr;
1306 fuzz->next(&orr);
1307 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001308 if (orr.getBounds().contains(irr.getBounds())) {
1309 canvas->drawDRRect(orr, irr, paint);
1310 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001311 break;
1312 }
1313 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001314 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001315 SkRect r;
1316 SkScalar start, sweep;
1317 bool useCenter;
1318 fuzz->next(&r, &start, &sweep, &useCenter);
1319 canvas->drawArc(r, start, sweep, useCenter, paint);
1320 break;
1321 }
1322 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001323 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001324 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001325 FuzzPath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001326 canvas->drawPath(path, paint);
1327 break;
1328 }
1329 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001330 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001331 SkScalar left, top;
1332 bool usePaint;
1333 fuzz->next(&left, &top, &usePaint);
1334 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001335 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001336 }
1337 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1338 break;
1339 }
1340 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001341 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001342 SkRect src, dst;
1343 bool usePaint;
1344 fuzz->next(&src, &dst, &usePaint);
1345 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001346 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001347 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001348 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001349 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1350 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001351 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1352 break;
1353 }
1354 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001356 SkIRect src;
1357 SkRect dst;
1358 bool usePaint;
1359 fuzz->next(&src, &dst, &usePaint);
1360 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001363 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001364 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1365 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001366 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1367 break;
1368 }
1369 case 36: {
1370 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001371 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001372 SkRect dst;
1373 fuzz->next(&dst, &usePaint);
1374 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001375 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001376 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001377 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001378 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1379 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001380 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1381 break;
1382 }
1383 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001384 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001385 SkIRect center;
1386 SkRect dst;
1387 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001388 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001389 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001390 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001391 }
Hal Canary0361d492017-03-15 12:58:15 -04001392 if (make_fuzz_t<bool>(fuzz)) {
1393 fuzz->next(&center);
1394 } else { // Make valid center, see SkLatticeIter::Valid().
1395 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1396 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1397 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1398 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1399 }
1400 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001401 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1402 break;
1403 }
1404 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001405 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001406 SkScalar left, top;
1407 bool usePaint;
1408 fuzz->next(&left, &top, &usePaint);
1409 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001410 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001411 }
1412 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1413 break;
1414 }
1415 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001416 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001417 SkRect src, dst;
1418 bool usePaint;
1419 fuzz->next(&src, &dst, &usePaint);
1420 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001421 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001422 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001423 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001424 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1425 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001426 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1427 break;
1428 }
1429 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001430 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001431 SkIRect src;
1432 SkRect dst;
1433 bool usePaint;
1434 fuzz->next(&src, &dst, &usePaint);
1435 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001436 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001437 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001438 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001439 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1440 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001441 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1442 break;
1443 }
1444 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001445 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001446 SkRect dst;
1447 bool usePaint;
1448 fuzz->next(&dst, &usePaint);
1449 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001450 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001451 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001452 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001453 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1454 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001455 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1456 break;
1457 }
1458 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001459 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001460 SkIRect center;
1461 SkRect dst;
1462 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001463 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001464 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001465 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001466 }
Hal Canary0361d492017-03-15 12:58:15 -04001467 if (make_fuzz_t<bool>(fuzz)) {
1468 fuzz->next(&center);
1469 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001470 if (img.width() == 0 || img.height() == 0) {
1471 // bitmap may not have had its pixels initialized.
1472 break;
1473 }
Hal Canary0361d492017-03-15 12:58:15 -04001474 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1475 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1476 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1477 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1478 }
1479 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001480 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1481 break;
1482 }
1483 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001484 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001485 bool usePaint;
1486 SkRect dst;
1487 fuzz->next(&usePaint, &dst);
1488 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001489 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001490 }
1491 constexpr int kMax = 6;
1492 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001493 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001494 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1495 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1496 fuzz->nextN(xDivs, lattice.fXCount);
1497 fuzz->nextN(yDivs, lattice.fYCount);
1498 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1499 break;
1500 }
1501 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001502 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001503 bool usePaint;
1504 SkRect dst;
1505 fuzz->next(&usePaint, &dst);
1506 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001507 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001508 }
1509 constexpr int kMax = 6;
1510 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001511 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001512 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1513 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1514 fuzz->nextN(xDivs, lattice.fXCount);
1515 fuzz->nextN(yDivs, lattice.fYCount);
1516 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1517 break;
1518 }
1519 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001520 fuzz_paint(fuzz, &paint, depth - 1);
1521 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001522 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001523 SkScalar x, y;
1524 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001525 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001526 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001527 break;
1528 }
1529 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001530 fuzz_paint(fuzz, &paint, depth - 1);
1531 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001532 fuzz_paint_text_encoding(fuzz, &paint);
1533 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001534 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001535 if (glyphCount < 1) {
1536 break;
1537 }
1538 SkAutoTMalloc<SkPoint> pos(glyphCount);
1539 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001540 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001541 pos[0] = {0, 0};
1542 for (int i = 1; i < glyphCount; ++i) {
1543 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001544 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001545 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1546 }
Hal Canary671e4422017-02-27 13:36:38 -05001547 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001548 break;
1549 }
1550 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001551 fuzz_paint(fuzz, &paint, depth - 1);
1552 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001553 fuzz_paint_text_encoding(fuzz, &paint);
1554 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001555 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001556 SkAutoTMalloc<SkScalar> widths(glyphCount);
1557 if (glyphCount < 1) {
1558 break;
1559 }
Hal Canary671e4422017-02-27 13:36:38 -05001560 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001561 SkScalar x = widths[0];
1562 for (int i = 0; i < glyphCount; ++i) {
1563 SkTSwap(x, widths[i]);
1564 x += widths[i];
1565 SkScalar offset;
1566 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001567 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001568 widths[i] += offset;
1569 }
1570 SkScalar y;
1571 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001572 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001573 break;
1574 }
1575 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001576 fuzz_paint(fuzz, &paint, depth - 1);
1577 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001578 fuzz_paint_text_encoding(fuzz, &paint);
1579 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001580 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001581 FuzzPath(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001582 SkScalar hOffset, vOffset;
1583 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001584 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1585 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001586 break;
1587 }
1588 case 49: {
1589 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001590 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001591 if (useMatrix) {
1592 fuzz->next(&matrix);
1593 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001594 fuzz_paint(fuzz, &paint, depth - 1);
1595 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001596 fuzz_paint_text_encoding(fuzz, &paint);
1597 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001598 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001599 FuzzPath(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001600 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001601 useMatrix ? &matrix : nullptr, paint);
1602 break;
1603 }
1604 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001605 fuzz_paint(fuzz, &paint, depth - 1);
1606 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001607 fuzz_paint_text_encoding(fuzz, &paint);
1608 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1609 SkRSXform rSXform[kMaxGlyphCount];
1610 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1611 SkASSERT(glyphCount <= kMaxGlyphCount);
1612 fuzz->nextN(rSXform, glyphCount);
1613 SkRect cullRect;
1614 bool useCullRect;
1615 fuzz->next(&useCullRect);
1616 if (useCullRect) {
1617 fuzz->next(&cullRect);
1618 }
1619 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1620 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001621 break;
1622 }
1623 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001624 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001625 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001626 SkScalar x, y;
1627 fuzz->next(&x, &y);
1628 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001629 break;
1630 }
1631 case 52: {
1632 bool usePaint, useMatrix;
1633 fuzz->next(&usePaint, &useMatrix);
1634 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001635 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001636 }
1637 if (useMatrix) {
1638 fuzz->next(&matrix);
1639 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001640 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001641 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1642 usePaint ? &paint : nullptr);
1643 break;
1644 }
1645 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001646 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001647 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001648 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001649 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001650 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001651 constexpr int kMaxCount = 100;
1652 int vertexCount;
1653 SkPoint vertices[kMaxCount];
1654 SkPoint texs[kMaxCount];
1655 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001656 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1657 fuzz->nextN(vertices, vertexCount);
1658 bool useTexs, useColors;
1659 fuzz->next(&useTexs, &useColors);
1660 if (useTexs) {
1661 fuzz->nextN(texs, vertexCount);
1662 }
1663 if (useColors) {
1664 fuzz->nextN(colors, vertexCount);
1665 }
1666 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001667 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001668 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001669 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1670 for (int i = 0; i < indexCount; ++i) {
1671 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1672 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001673 }
Mike Reed887cdf12017-04-03 11:11:09 -04001674 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1675 useTexs ? texs : nullptr,
1676 useColors ? colors : nullptr,
1677 indexCount, indices),
1678 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001679 break;
1680 }
1681 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001682 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001683 break;
1684 }
1685 }
1686}
1687
Hal Canary1e0138b2017-03-10 13:56:08 -05001688static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001689 SkScalar w, h;
1690 fuzz->next(&w, &h);
1691 SkPictureRecorder pictureRecorder;
1692 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1693 return pictureRecorder.finishRecordingAsPicture();
1694}
1695
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001696DEF_FUZZ(NullCanvas, fuzz) {
1697 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001698}
1699
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001700constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001701
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001702DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001703 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001704 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001705 fuzz_canvas(fuzz, surface->getCanvas());
1706}
1707
Hal Canarye2924432017-12-01 11:46:26 -05001708DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1709 SkPictureRecorder recorder;
1710 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1711 SkIntToScalar(kCanvasSize.height())));
1712 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1713 if (!pic) { fuzz->signalBug(); }
1714 sk_sp<SkData> data = pic->serialize();
1715 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001716 SkReadBuffer rb(data->data(), data->size());
1717 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001718 if (!deserialized) { fuzz->signalBug(); }
1719 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1720 SkASSERT(surface && surface->getCanvas());
1721 surface->getCanvas()->drawPicture(deserialized);
1722}
1723
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001724DEF_FUZZ(ImageFilter, fuzz) {
1725 auto fil = make_fuzz_imageFilter(fuzz, 20);
1726
1727 SkPaint paint;
1728 paint.setImageFilter(fil);
1729 SkBitmap bitmap;
1730 SkCanvas canvas(bitmap);
1731 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1732}
1733
1734
1735//SkRandom _rand;
1736#define SK_ADD_RANDOM_BIT_FLIPS
1737
1738DEF_FUZZ(SerializedImageFilter, fuzz) {
1739 auto filter = make_fuzz_imageFilter(fuzz, 20);
1740 auto data = filter->serialize();
1741 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1742 size_t len = data->size();
1743#ifdef SK_ADD_RANDOM_BIT_FLIPS
1744 unsigned char* p = const_cast<unsigned char*>(ptr);
1745 for (size_t i = 0; i < len; ++i, ++p) {
1746 uint8_t j;
1747 fuzz->nextRange(&j, 1, 250);
1748 if (j == 1) { // 0.4% of the time, flip a bit or byte
1749 uint8_t k;
1750 fuzz->nextRange(&k, 1, 10);
1751 if (k == 1) { // Then 10% of the time, change a whole byte
1752 uint8_t s;
1753 fuzz->nextRange(&s, 0, 2);
1754 switch(s) {
1755 case 0:
1756 *p ^= 0xFF; // Flip entire byte
1757 break;
1758 case 1:
1759 *p = 0xFF; // Set all bits to 1
1760 break;
1761 case 2:
1762 *p = 0x00; // Set all bits to 0
1763 break;
1764 }
1765 } else {
1766 uint8_t s;
1767 fuzz->nextRange(&s, 0, 7);
1768 *p ^= (1 << 7);
1769 }
1770 }
1771 }
1772#endif // SK_ADD_RANDOM_BIT_FLIPS
1773 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1774
1775 // uncomment below to write out a serialized image filter (to make corpus
1776 // for -t filter_fuzz)
1777 // SkString s("./serialized_filters/sf");
1778 // s.appendU32(_rand.nextU());
1779 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1780 // sk_fwrite(data->bytes(), data->size(), file);
1781 // sk_fclose(file);
1782
1783 SkPaint paint;
1784 paint.setImageFilter(deserializedFil);
1785 SkBitmap bitmap;
1786 SkCanvas canvas(bitmap);
1787 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1788}
1789
Hal Canary44801ca2017-03-15 11:39:06 -04001790#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001791static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1792 SkASSERT(context);
1793 auto surface = SkSurface::MakeRenderTarget(
1794 context,
1795 SkBudgeted::kNo,
1796 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1797 SkASSERT(surface && surface->getCanvas());
1798 fuzz_canvas(fuzz, surface->getCanvas());
1799}
1800
Hal Canary44801ca2017-03-15 11:39:06 -04001801DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001802 sk_gpu_test::GrContextFactory f;
1803 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001804 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001805 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001806 }
Hal Canary5aa91582017-03-21 11:11:44 -07001807 fuzz_ganesh(fuzz, context);
1808}
1809
Kevin Lubick27d42192018-04-03 12:30:32 -04001810// This target is deprecated, NullGLContext is not well maintained.
1811// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001812DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001813 sk_gpu_test::GrContextFactory f;
1814 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001815}
1816
Kevin Lubick30709262018-04-02 11:06:41 -04001817// This target is deprecated, DebugGLContext is not well maintained.
Kevin Lubick27d42192018-04-03 12:30:32 -04001818// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001819DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001820 sk_gpu_test::GrContextFactory f;
1821 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001822}
Kevin Lubick30709262018-04-02 11:06:41 -04001823
Kevin Lubick27d42192018-04-03 12:30:32 -04001824DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001825 sk_gpu_test::GrContextFactory f;
1826 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1827}
Hal Canary44801ca2017-03-15 11:39:06 -04001828#endif
1829
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001830DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001831 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001832 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001833 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1834 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001835}
1836
1837// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1838DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001839 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001840 fuzz_canvas(fuzz, &debugCanvas);
1841 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1842 UrlDataManager dataManager(SkString("data"));
1843 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1844 Json::StyledStreamWriter(" ").write(std::cout, json);
1845}