blob: 28a338bf4229b57630b5e275f89946ff7aad8f1e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "fuzz/Fuzz.h"
9#include "fuzz/FuzzCommon.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050010
11// CORE
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkCanvas.h"
13#include "include/core/SkColorFilter.h"
14#include "include/core/SkFontMgr.h"
15#include "include/core/SkImageFilter.h"
16#include "include/core/SkMaskFilter.h"
17#include "include/core/SkPathEffect.h"
18#include "include/core/SkPictureRecorder.h"
19#include "include/core/SkPoint3.h"
20#include "include/core/SkRSXform.h"
21#include "include/core/SkRegion.h"
22#include "include/core/SkSurface.h"
23#include "include/core/SkTypeface.h"
24#include "include/docs/SkPDFDocument.h"
25#include "include/private/SkTo.h"
26#include "include/utils/SkNullCanvas.h"
27#include "src/core/SkOSFile.h"
28#include "src/core/SkPicturePriv.h"
29#include "tools/debugger/DebugCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050030
31// EFFECTS
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "include/core/SkTextBlob.h"
33#include "include/effects/Sk1DPathEffect.h"
34#include "include/effects/Sk2DPathEffect.h"
35#include "include/effects/SkAlphaThresholdFilter.h"
36#include "include/effects/SkArithmeticImageFilter.h"
37#include "include/effects/SkBlurImageFilter.h"
38#include "include/effects/SkBlurMaskFilter.h"
39#include "include/effects/SkColorFilterImageFilter.h"
40#include "include/effects/SkColorMatrixFilter.h"
41#include "include/effects/SkComposeImageFilter.h"
42#include "include/effects/SkCornerPathEffect.h"
43#include "include/effects/SkDashPathEffect.h"
44#include "include/effects/SkDiscretePathEffect.h"
45#include "include/effects/SkDisplacementMapEffect.h"
46#include "include/effects/SkDropShadowImageFilter.h"
47#include "include/effects/SkGradientShader.h"
48#include "include/effects/SkHighContrastFilter.h"
49#include "include/effects/SkImageSource.h"
50#include "include/effects/SkLightingImageFilter.h"
51#include "include/effects/SkLumaColorFilter.h"
52#include "include/effects/SkMagnifierImageFilter.h"
53#include "include/effects/SkMatrixConvolutionImageFilter.h"
54#include "include/effects/SkMergeImageFilter.h"
55#include "include/effects/SkMorphologyImageFilter.h"
56#include "include/effects/SkOffsetImageFilter.h"
57#include "include/effects/SkPaintImageFilter.h"
58#include "include/effects/SkPerlinNoiseShader.h"
59#include "include/effects/SkPictureImageFilter.h"
60#include "include/effects/SkTableColorFilter.h"
61#include "include/effects/SkTileImageFilter.h"
62#include "include/effects/SkXfermodeImageFilter.h"
63#include "src/core/SkReadBuffer.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050064
65// SRC
Mike Kleinc0bd9f92019-04-23 12:05:21 -050066#include "src/utils/SkUTF.h"
67#include "tools/flags/CommandLineFlags.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050068
Hal Canary44801ca2017-03-15 11:39:06 -040069#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050070#include "include/gpu/gl/GrGLFunctions.h"
71#include "src/gpu/GrContextPriv.h"
72#include "src/gpu/gl/GrGLGpu.h"
73#include "src/gpu/gl/GrGLUtil.h"
74#include "tools/gpu/GrContextFactory.h"
Hal Canary44801ca2017-03-15 11:39:06 -040075#endif
76
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050077// MISC
78
79#include <iostream>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040080#include <utility>
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050081
Mike Klein84836b72019-03-21 11:31:36 -050082static DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets.");
Kevin Lubickfaef5142018-06-07 10:33:11 -040083
Hal Canary24ac42b2017-02-14 13:35:14 -050084// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050085// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040086// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050087
Hal Canary1e0138b2017-03-10 13:56:08 -050088// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
89// In fact, all make_fuzz_foo() functions have this potential problem.
90// Use sequence points!
91template <typename T>
92inline T make_fuzz_t(Fuzz* fuzz) {
93 T t;
94 fuzz->next(&t);
95 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050096}
97
Hal Canary1e0138b2017-03-10 13:56:08 -050098static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -050099
Hal Canary1e0138b2017-03-10 13:56:08 -0500100static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500101
Hal Canary1e0138b2017-03-10 13:56:08 -0500102static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500103
Hal Canary1e0138b2017-03-10 13:56:08 -0500104static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500105 if (depth <= 0) {
106 return nullptr;
107 }
108 int colorFilterType;
109 fuzz->nextRange(&colorFilterType, 0, 8);
110 switch (colorFilterType) {
111 case 0:
112 return nullptr;
113 case 1: {
114 SkColor color;
115 SkBlendMode mode;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400116 fuzz->next(&color);
Kevin Lubick00587e32019-06-03 11:27:16 -0400117 fuzz->nextEnum(&mode, SkBlendMode::kLastMode);
Mike Reedb286bc22019-04-08 16:23:20 -0400118 return SkColorFilters::Blend(color, mode);
Hal Canary27bece82017-03-07 16:23:20 -0500119 }
120 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500121 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500122 if (!outer) {
123 return nullptr;
124 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500125 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500126 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500127 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500128 }
129 case 3: {
Mike Reede869a1e2019-04-30 12:18:54 -0400130 float array[20];
Hal Canary27bece82017-03-07 16:23:20 -0500131 fuzz->nextN(array, SK_ARRAY_COUNT(array));
Mike Reede869a1e2019-04-30 12:18:54 -0400132 return SkColorFilters::Matrix(array);
Hal Canary27bece82017-03-07 16:23:20 -0500133 }
134 case 4: {
135 SkColor mul, add;
136 fuzz->next(&mul, &add);
137 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
138 }
139 case 5: {
140 bool grayscale;
141 int invertStyle;
142 float contrast;
143 fuzz->next(&grayscale);
144 fuzz->nextRange(&invertStyle, 0, 2);
145 fuzz->nextRange(&contrast, -1.0f, 1.0f);
146 return SkHighContrastFilter::Make(SkHighContrastConfig(
147 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
148 }
149 case 6:
150 return SkLumaColorFilter::Make();
151 case 7: {
152 uint8_t table[256];
153 fuzz->nextN(table, SK_ARRAY_COUNT(table));
154 return SkTableColorFilter::Make(table);
155 }
156 case 8: {
157 uint8_t tableA[256];
158 uint8_t tableR[256];
159 uint8_t tableG[256];
160 uint8_t tableB[256];
161 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
162 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
163 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
164 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
165 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
166 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500167 default:
168 SkASSERT(false);
169 break;
Hal Canary27bece82017-03-07 16:23:20 -0500170 }
171 return nullptr;
172}
Hal Canary24ac42b2017-02-14 13:35:14 -0500173
Hal Canary1e0138b2017-03-10 13:56:08 -0500174static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500175 SkScalar totalPos = 0;
176 for (int i = 0; i < colorCount; ++i) {
177 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
178 totalPos += pos[i];
179 }
180 totalPos = 1.0f / totalPos;
181 for (int i = 0; i < colorCount; ++i) {
182 pos[i] *= totalPos;
183 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500184 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500185 pos[colorCount - 1] = 1.0f;
186}
187
Hal Canary1e0138b2017-03-10 13:56:08 -0500188static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500189 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
190 sk_sp<SkColorFilter> colorFilter(nullptr);
191 SkBitmap bitmap;
192 sk_sp<SkImage> img;
Mike Reedfae8fce2019-04-03 10:27:45 -0400193 SkTileMode tmX, tmY;
Hal Canary24ac42b2017-02-14 13:35:14 -0500194 bool useMatrix;
195 SkColor color;
196 SkMatrix matrix;
197 SkBlendMode blendMode;
198 int shaderType;
199 if (depth <= 0) {
200 return nullptr;
201 }
Hal Canary671e4422017-02-27 13:36:38 -0500202 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500203 switch (shaderType) {
204 case 0:
205 return nullptr;
206 case 1:
Mike Reedc8bea7d2019-04-09 13:55:36 -0400207 return SkShaders::Empty();
Hal Canary24ac42b2017-02-14 13:35:14 -0500208 case 2:
209 fuzz->next(&color);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400210 return SkShaders::Color(color);
Hal Canary24ac42b2017-02-14 13:35:14 -0500211 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500212 img = make_fuzz_image(fuzz);
Kevin Lubick00587e32019-06-03 11:27:16 -0400213 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
214 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400215 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500216 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400217 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500218 }
219 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
220 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500221 bitmap = make_fuzz_bitmap(fuzz);
Kevin Lubick00587e32019-06-03 11:27:16 -0400222 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
223 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400224 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500225 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400226 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500227 }
Mike Reed50acf8f2019-04-08 13:20:23 -0400228 return bitmap.makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500229 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500230 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400231 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500232 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
233 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500234 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
235 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500236 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
237 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500238 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
239 shader2 = make_fuzz_shader(fuzz, depth - 1);
Kevin Lubick00587e32019-06-03 11:27:16 -0400240 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400241 return SkShaders::Blend(blendMode, std::move(shader1), std::move(shader2));
Hal Canaryb69c4b82017-03-08 11:02:40 -0500242 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500243 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500244 bool useTile;
245 SkRect tile;
Kevin Lubick00587e32019-06-03 11:27:16 -0400246 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
247 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400248 fuzz->next(&useMatrix, &useTile);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500249 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400250 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500251 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500252 if (useTile) {
253 fuzz->next(&tile);
254 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400255 return pic->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr, useTile ? &tile : nullptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500256 }
Hal Canary671e4422017-02-27 13:36:38 -0500257 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500258 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000259 // Deprecated SkGaussianEdgeShader
260 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500261 case 10: {
262 constexpr int kMaxColors = 12;
263 SkPoint pts[2];
264 SkColor colors[kMaxColors];
265 SkScalar pos[kMaxColors];
266 int colorCount;
267 bool usePos;
268 fuzz->nextN(pts, 2);
269 fuzz->nextRange(&colorCount, 2, kMaxColors);
270 fuzz->nextN(colors, colorCount);
Kevin Lubick00587e32019-06-03 11:27:16 -0400271 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400272 fuzz->next(&useMatrix, &usePos);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500273 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400274 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500275 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500276 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500277 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500278 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500279 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
280 tmX, 0, useMatrix ? &matrix : nullptr);
281 }
282 case 11: {
283 constexpr int kMaxColors = 12;
284 SkPoint center;
285 SkScalar radius;
286 int colorCount;
287 bool usePos;
288 SkColor colors[kMaxColors];
289 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400290 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400291 fuzz->next(&useMatrix, &usePos, &center, &radius);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500292 fuzz->nextRange(&colorCount, 2, kMaxColors);
293 fuzz->nextN(colors, colorCount);
294 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400295 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500296 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500297 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500298 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500299 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500300 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
301 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
302 }
303 case 12: {
304 constexpr int kMaxColors = 12;
305 SkPoint start, end;
306 SkScalar startRadius, endRadius;
307 int colorCount;
308 bool usePos;
309 SkColor colors[kMaxColors];
310 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400311 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400312 fuzz->next(&useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500313 fuzz->nextRange(&colorCount, 2, kMaxColors);
314 fuzz->nextN(colors, colorCount);
315 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400316 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500317 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500318 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500319 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500320 }
321 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
322 usePos ? pos : nullptr, colorCount, tmX, 0,
323 useMatrix ? &matrix : nullptr);
324 }
325 case 13: {
326 constexpr int kMaxColors = 12;
327 SkScalar cx, cy;
328 int colorCount;
329 bool usePos;
330 SkColor colors[kMaxColors];
331 SkScalar pos[kMaxColors];
332 fuzz->next(&cx, &cy, &useMatrix, &usePos);
333 fuzz->nextRange(&colorCount, 2, kMaxColors);
334 fuzz->nextN(colors, colorCount);
335 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400336 FuzzNiceMatrix(fuzz, &matrix);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500337 }
338 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500339 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500340 }
341 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
342 0, useMatrix ? &matrix : nullptr);
343 }
344 case 14: {
345 SkScalar baseFrequencyX, baseFrequencyY, seed;
346 int numOctaves;
347 SkISize tileSize;
348 bool useTileSize, turbulence;
349 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
350 if (useTileSize) {
351 fuzz->next(&tileSize);
352 }
353 fuzz->nextRange(&numOctaves, 2, 7);
354 if (turbulence) {
355 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
356 numOctaves, seed,
357 useTileSize ? &tileSize : nullptr);
358 } else {
359 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
360 numOctaves, seed,
361 useTileSize ? &tileSize : nullptr);
362 }
363 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500364 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500365 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500366 break;
367 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500368 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500369}
370
Hal Canary1e0138b2017-03-10 13:56:08 -0500371static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500372 if (depth <= 0) {
373 return nullptr;
374 }
375 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400376 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500377 switch (pathEffectType) {
378 case 0: {
379 return nullptr;
380 }
381 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500382 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
383 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500384 return SkPathEffect::MakeSum(std::move(first), std::move(second));
385 }
386 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500387 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
388 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500389 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
390 }
391 case 3: {
392 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400393 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500394 SkScalar advance, phase;
395 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500396 SkPath1DPathEffect::Style style;
Kevin Lubick00587e32019-06-03 11:27:16 -0400397 fuzz->nextEnum(&style, SkPath1DPathEffect::kLastEnum_Style);
Hal Canaryf7005202017-03-10 08:48:28 -0500398 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500399 }
400 case 4: {
401 SkScalar width;
402 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400403 fuzz->next(&width);
404 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500405 return SkLine2DPathEffect::Make(width, matrix);
406 }
407 case 5: {
408 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400409 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500410 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400411 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500412 return SkPath2DPathEffect::Make(matrix, path);
413 }
414 case 6: {
415 SkScalar radius;
416 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500417 return SkCornerPathEffect::Make(radius);
418 }
Mike Reed40e7e652017-07-22 22:12:59 -0400419 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500420 SkScalar phase;
421 fuzz->next(&phase);
422 SkScalar intervals[20];
423 int count;
424 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
425 fuzz->nextN(intervals, count);
426 return SkDashPathEffect::Make(intervals, count, phase);
427 }
Mike Reed40e7e652017-07-22 22:12:59 -0400428 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500429 SkScalar segLength, dev;
430 uint32_t seed;
431 fuzz->next(&segLength, &dev, &seed);
432 return SkDiscretePathEffect::Make(segLength, dev, seed);
433 }
434 default:
435 SkASSERT(false);
436 return nullptr;
437 }
438}
Hal Canary24ac42b2017-02-14 13:35:14 -0500439
Hal Canary1e0138b2017-03-10 13:56:08 -0500440static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500441 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400442 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500443 switch (maskfilterType) {
444 case 0:
445 return nullptr;
446 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500447 SkBlurStyle blurStyle;
Kevin Lubick00587e32019-06-03 11:27:16 -0400448 fuzz->nextEnum(&blurStyle, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500449 SkScalar sigma;
450 fuzz->next(&sigma);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400451 bool respectCTM;
452 fuzz->next(&respectCTM);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400453 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500454 }
Hal Canary5395c592017-03-08 16:52:18 -0500455 default:
456 SkASSERT(false);
457 return nullptr;
458 }
459}
Hal Canary24ac42b2017-02-14 13:35:14 -0500460
Hal Canary1e0138b2017-03-10 13:56:08 -0500461static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
462 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500463 return nullptr;
464 }
465 auto fontMugger = SkFontMgr::RefDefault();
466 SkASSERT(fontMugger);
467 int familyCount = fontMugger->countFamilies();
468 int i, j;
469 fuzz->nextRange(&i, 0, familyCount - 1);
470 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
471 int styleCount = family->count();
472 fuzz->nextRange(&j, 0, styleCount - 1);
473 return sk_sp<SkTypeface>(family->createTypeface(j));
474}
Hal Canary24ac42b2017-02-14 13:35:14 -0500475
Hal Canary1e0138b2017-03-10 13:56:08 -0500476static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500477
478static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
479 if (depth <= 0) {
480 return nullptr;
481 }
482 uint8_t imageFilterType;
483 fuzz->nextRange(&imageFilterType, 1, 6);
484 SkPoint3 p, q;
485 SkColor lightColor;
486 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
487 sk_sp<SkImageFilter> input;
488 SkImageFilter::CropRect cropRect;
489 bool useCropRect;
490 fuzz->next(&useCropRect);
491 if (useCropRect) {
492 fuzz->next(&cropRect);
493 }
494 switch (imageFilterType) {
495 case 1:
496 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500497 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500498 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
499 std::move(input),
500 useCropRect ? &cropRect : nullptr);
501 case 2:
502 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500503 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500504 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
505 std::move(input),
506 useCropRect ? &cropRect : nullptr);
507 case 3:
508 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500509 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500510 return SkLightingImageFilter::MakeSpotLitDiffuse(
511 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
512 std::move(input), useCropRect ? &cropRect : nullptr);
513 case 4:
514 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500515 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500516 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
517 shininess, std::move(input),
518 useCropRect ? &cropRect : nullptr);
519 case 5:
520 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500521 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500522 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
523 shininess, std::move(input),
524 useCropRect ? &cropRect : nullptr);
525 case 6:
526 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
527 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500528 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500529 return SkLightingImageFilter::MakeSpotLitSpecular(
530 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
531 std::move(input), useCropRect ? &cropRect : nullptr);
532 default:
533 SkASSERT(false);
534 return nullptr;
535 }
536}
537
Hal Canary1e0138b2017-03-10 13:56:08 -0500538static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500539
Hal Canary1e0138b2017-03-10 13:56:08 -0500540static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500541 if (depth <= 0) {
542 return nullptr;
543 }
544 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500545 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500546 switch (imageFilterType) {
547 case 0:
548 return nullptr;
549 case 1: {
550 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500551 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500552 bool useCropRect;
553 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400554 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500555 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400556 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500557 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400558 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500559 useCropRect ? &cropRect : nullptr);
560 }
561 case 2: {
562 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400563 FuzzNiceMatrix(fuzz, &matrix);
Hal Canarye03c3e52017-03-09 11:33:35 -0500564 SkFilterQuality quality;
Kevin Lubick00587e32019-06-03 11:27:16 -0400565 fuzz->nextEnum(&quality, SkFilterQuality::kLast_SkFilterQuality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500566 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500567 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
568 }
569 case 3: {
570 SkRegion region;
571 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500572 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500573 bool useCropRect;
574 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400575 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500576 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400577 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500578 }
579 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
580 useCropRect ? &cropRect : nullptr);
581 }
582 case 4: {
583 float k1, k2, k3, k4;
584 bool enforcePMColor;
585 bool useCropRect;
586 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500587 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
588 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500589 SkImageFilter::CropRect cropRect;
590 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400591 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500592 }
593 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
594 std::move(background), std::move(foreground),
595 useCropRect ? &cropRect : nullptr);
596 }
597 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500598 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
599 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500600 bool useCropRect;
601 SkImageFilter::CropRect cropRect;
602 fuzz->next(&useCropRect);
603 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400604 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500605 }
606 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
607 useCropRect ? &cropRect : nullptr);
608 }
609 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500610 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
611 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500612 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
613 }
614 case 7: {
615 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500616 fuzz->nextRange(&xChannelSelector, 1, 4);
617 fuzz->nextRange(&yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500618 SkScalar scale;
619 bool useCropRect;
620 fuzz->next(&scale, &useCropRect);
621 SkImageFilter::CropRect cropRect;
622 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400623 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500624 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500625 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
626 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500627 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
628 std::move(displacement), std::move(color),
629 useCropRect ? &cropRect : nullptr);
630 }
631 case 8: {
632 SkScalar dx, dy, sigmaX, sigmaY;
633 SkColor color;
634 SkDropShadowImageFilter::ShadowMode shadowMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500635 fuzz->nextRange(&shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500636 bool useCropRect;
637 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
638 SkImageFilter::CropRect cropRect;
639 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400640 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500641 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500642 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500643 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
644 std::move(input),
645 useCropRect ? &cropRect : nullptr);
646 }
647 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500648 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500649 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500650 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500651 SkRect srcRect, dstRect;
652 SkFilterQuality filterQuality;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400653 fuzz->next(&srcRect, &dstRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400654 fuzz->nextEnum(&filterQuality, SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500655 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
656 }
657 case 11:
658 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
659 case 12: {
660 SkRect srcRect;
661 SkScalar inset;
662 bool useCropRect;
663 SkImageFilter::CropRect cropRect;
664 fuzz->next(&srcRect, &inset, &useCropRect);
665 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400666 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500667 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500668 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500669 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
670 useCropRect ? &cropRect : nullptr);
671 }
672 case 13: {
673 constexpr int kMaxKernelSize = 5;
674 int32_t n, m;
675 fuzz->nextRange(&n, 1, kMaxKernelSize);
676 fuzz->nextRange(&m, 1, kMaxKernelSize);
677 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
678 fuzz->nextN(kernel, n * m);
679 int32_t offsetX, offsetY;
680 fuzz->nextRange(&offsetX, 0, n - 1);
681 fuzz->nextRange(&offsetY, 0, m - 1);
682 SkScalar gain, bias;
683 bool convolveAlpha, useCropRect;
684 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
685 SkMatrixConvolutionImageFilter::TileMode tileMode;
Kevin Lubick00587e32019-06-03 11:27:16 -0400686 fuzz->nextEnum(&tileMode, SkMatrixConvolutionImageFilter::TileMode::kLast_TileMode);
Hal Canarye03c3e52017-03-09 11:33:35 -0500687 SkImageFilter::CropRect cropRect;
688 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400689 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500690 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500691 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500692 return SkMatrixConvolutionImageFilter::Make(
693 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
694 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
695 }
696 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500697 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
698 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500699 bool useCropRect;
700 fuzz->next(&useCropRect);
701 SkImageFilter::CropRect cropRect;
702 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400703 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500704 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400705 return SkMergeImageFilter::Make(std::move(first), std::move(second),
706 useCropRect ? &cropRect : nullptr);
707 }
708 case 15: {
709 constexpr int kMaxCount = 4;
710 sk_sp<SkImageFilter> ifs[kMaxCount];
711 int count;
712 fuzz->nextRange(&count, 1, kMaxCount);
713 for (int i = 0; i < count; ++i) {
714 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
715 }
716 bool useCropRect;
717 fuzz->next(&useCropRect);
718 SkImageFilter::CropRect cropRect;
719 if (useCropRect) {
720 fuzz->next(&cropRect);
721 }
722 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500723 }
724 case 16: {
725 int rx, ry;
726 fuzz->next(&rx, &ry);
727 bool useCropRect;
728 fuzz->next(&useCropRect);
729 SkImageFilter::CropRect cropRect;
730 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400731 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500732 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500733 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500734 return SkDilateImageFilter::Make(rx, ry, std::move(input),
735 useCropRect ? &cropRect : nullptr);
736 }
737 case 17: {
738 int rx, ry;
739 fuzz->next(&rx, &ry);
740 bool useCropRect;
741 fuzz->next(&useCropRect);
742 SkImageFilter::CropRect cropRect;
743 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400744 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500745 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500746 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500747 return SkErodeImageFilter::Make(rx, ry, std::move(input),
748 useCropRect ? &cropRect : nullptr);
749 }
750 case 18: {
751 SkScalar dx, dy;
752 fuzz->next(&dx, &dy);
753 bool useCropRect;
754 fuzz->next(&useCropRect);
755 SkImageFilter::CropRect cropRect;
756 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400757 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500758 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500759 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500760 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
761 useCropRect ? &cropRect : nullptr);
762 }
763 case 19: {
764 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500765 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500766 bool useCropRect;
767 fuzz->next(&useCropRect);
768 SkImageFilter::CropRect cropRect;
769 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400770 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500771 }
772 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
773 }
774 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500775 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500776 return SkPictureImageFilter::Make(std::move(picture));
777 }
778 case 21: {
779 SkRect cropRect;
780 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500781 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500782 return SkPictureImageFilter::Make(std::move(picture), cropRect);
783 }
784 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500785 SkRect src, dst;
786 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500787 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500788 return SkTileImageFilter::Make(src, dst, std::move(input));
789 }
Mike Reed77e487d2017-11-09 21:50:20 +0000790 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500791 SkBlendMode blendMode;
792 bool useCropRect;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400793 fuzz->next(&useCropRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400794 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Hal Canarye03c3e52017-03-09 11:33:35 -0500795 SkImageFilter::CropRect cropRect;
796 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400797 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500798 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500799 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
800 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500801 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
802 useCropRect ? &cropRect : nullptr);
803 }
804 default:
805 SkASSERT(false);
806 return nullptr;
807 }
808}
Hal Canary24ac42b2017-02-14 13:35:14 -0500809
Hal Canary1e0138b2017-03-10 13:56:08 -0500810static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500811 int w, h;
812 fuzz->nextRange(&w, 1, 1024);
813 fuzz->nextRange(&h, 1, 1024);
814 SkAutoTMalloc<SkPMColor> data(w * h);
815 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
816 int n = w * h;
817 for (int i = 0; i < n; ++i) {
818 SkColor c;
819 fuzz->next(&c);
820 data[i] = SkPreMultiplyColor(c);
821 }
822 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500823 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
824 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500825}
826
Hal Canary1e0138b2017-03-10 13:56:08 -0500827static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500828 SkBitmap bitmap;
829 int w, h;
830 fuzz->nextRange(&w, 1, 1024);
831 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500832 if (!bitmap.tryAllocN32Pixels(w, h)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400833 SkDEBUGF("Could not allocate pixels %d x %d", w, h);
Kevin Lubick1991f552018-02-27 10:59:10 -0500834 return bitmap;
835 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500836 for (int y = 0; y < h; ++y) {
837 for (int x = 0; x < w; ++x) {
838 SkColor c;
839 fuzz->next(&c);
840 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
841 }
842 }
843 return bitmap;
844}
845
Kevin Lubick00587e32019-06-03 11:27:16 -0400846template <typename T>
847static T make_fuzz_enum_range(Fuzz* fuzz, T maxv) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500848 T value;
Kevin Lubick00587e32019-06-03 11:27:16 -0400849 fuzz->nextEnum(&value, maxv);
Hal Canary1e0138b2017-03-10 13:56:08 -0500850 return value;
851}
852
853static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500854 if (!fuzz || !paint || depth <= 0) {
855 return;
856 }
857
Hal Canary1e0138b2017-03-10 13:56:08 -0500858 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
859 paint->setDither( make_fuzz_t<bool>(fuzz));
860 paint->setColor( make_fuzz_t<SkColor>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400861 paint->setBlendMode( make_fuzz_enum_range<SkBlendMode>(fuzz, SkBlendMode::kLastMode));
862 paint->setFilterQuality(make_fuzz_enum_range<SkFilterQuality>(fuzz, kLast_SkFilterQuality));
863 paint->setStyle( make_fuzz_enum_range<SkPaint::Style>(fuzz,
864 SkPaint::Style::kStrokeAndFill_Style));
Hal Canary1e0138b2017-03-10 13:56:08 -0500865 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
866 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
867 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
868 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
869 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500870
871 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500872 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
873 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400874 paint->setStrokeCap( make_fuzz_enum_range<SkPaint::Cap>(fuzz, SkPaint::kLast_Cap));
875 paint->setStrokeJoin( make_fuzz_enum_range<SkPaint::Join>(fuzz, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500876 }
877}
878
Mike Reed358fcad2018-11-23 15:27:51 -0500879static SkFont fuzz_font(Fuzz* fuzz) {
880 SkFont font;
881 font.setTypeface( make_fuzz_typeface(fuzz));
882 font.setSize( make_fuzz_t<SkScalar>(fuzz));
883 font.setScaleX( make_fuzz_t<SkScalar>(fuzz));
884 font.setSkewX( make_fuzz_t<SkScalar>(fuzz));
885 font.setLinearMetrics( make_fuzz_t<bool>(fuzz));
886 font.setSubpixel( make_fuzz_t<bool>(fuzz));
887 font.setEmbeddedBitmaps( make_fuzz_t<bool>(fuzz));
888 font.setForceAutoHinting( make_fuzz_t<bool>(fuzz));
889 font.setEmbolden( make_fuzz_t<bool>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400890 font.setHinting( make_fuzz_enum_range<SkFontHinting>(fuzz, SkFontHinting::kFull));
891 font.setEdging( make_fuzz_enum_range<SkFont::Edging>(fuzz,
892 SkFont::Edging::kSubpixelAntiAlias));
Mike Reed358fcad2018-11-23 15:27:51 -0500893 return font;
Hal Canary5395c592017-03-08 16:52:18 -0500894}
895
Mike Reed2ed78202018-11-21 15:10:08 -0500896static SkTextEncoding fuzz_paint_text_encoding(Fuzz* fuzz) {
Kevin Lubick00587e32019-06-03 11:27:16 -0400897 return make_fuzz_enum_range<SkTextEncoding>(fuzz, SkTextEncoding::kUTF32);
Hal Canary24ac42b2017-02-14 13:35:14 -0500898}
899
Hal Canary5395c592017-03-08 16:52:18 -0500900constexpr int kMaxGlyphCount = 30;
901
Mike Reed2ed78202018-11-21 15:10:08 -0500902static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkFont& font, SkTextEncoding encoding) {
Hal Canary671e4422017-02-27 13:36:38 -0500903 SkTDArray<uint8_t> array;
Ben Wagner51e15a62019-05-07 15:38:46 -0400904 if (SkTextEncoding::kGlyphID == encoding) {
Herb Derby087fad72019-01-22 14:45:16 -0500905 int glyphRange = font.getTypefaceOrDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500906 if (glyphRange == 0) {
907 // Some fuzzing environments have no fonts, so empty array is the best
908 // we can do.
909 return array;
910 }
Hal Canary671e4422017-02-27 13:36:38 -0500911 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500912 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500913 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
914 for (int i = 0; i < glyphCount; ++i) {
915 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
916 }
917 return array;
918 }
919 static const SkUnichar ranges[][2] = {
920 {0x0020, 0x007F},
921 {0x00A1, 0x0250},
922 {0x0400, 0x0500},
923 };
924 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500925 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500926 count += (ranges[i][1] - ranges[i][0]);
927 }
Hal Canary5395c592017-03-08 16:52:18 -0500928 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500929 SkUnichar buffer[kMaxLength];
930 int length;
931 fuzz->nextRange(&length, 1, kMaxLength);
932 for (int j = 0; j < length; ++j) {
933 int32_t value;
934 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500935 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500936 if (value + ranges[i][0] < ranges[i][1]) {
937 buffer[j] = value + ranges[i][0];
938 break;
939 } else {
940 value -= (ranges[i][1] - ranges[i][0]);
941 }
942 }
943 }
Mike Reed2ed78202018-11-21 15:10:08 -0500944 switch (encoding) {
Ben Wagner51e15a62019-05-07 15:38:46 -0400945 case SkTextEncoding::kUTF8: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500946 size_t utf8len = 0;
947 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400948 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -0500949 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500950 char* ptr = (char*)array.append(utf8len);
951 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400952 ptr += SkUTF::ToUTF8(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -0500953 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500954 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400955 case SkTextEncoding::kUTF16: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500956 size_t utf16len = 0;
957 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400958 utf16len += SkUTF::ToUTF16(buffer[j]);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500959 }
960 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
961 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400962 ptr += SkUTF::ToUTF16(buffer[j], ptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500963 }
964 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400965 case SkTextEncoding::kUTF32:
Hal Canary671e4422017-02-27 13:36:38 -0500966 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -0500967 break;
Hal Canary671e4422017-02-27 13:36:38 -0500968 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -0500969 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -0500970 break;
Hal Canary671e4422017-02-27 13:36:38 -0500971 }
972 return array;
973}
974
Hal Canary5395c592017-03-08 16:52:18 -0500975static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
976 SkTextBlobBuilder textBlobBuilder;
977 int8_t runCount;
978 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
979 while (runCount-- > 0) {
Mike Reed2ed78202018-11-21 15:10:08 -0500980 SkFont font;
981 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
982 font.setEdging(make_fuzz_t<bool>(fuzz) ? SkFont::Edging::kAlias : SkFont::Edging::kAntiAlias);
983 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
984 int glyphCount = font.countText(text.begin(), SkToSizeT(text.count()), encoding);
Hal Canary5395c592017-03-08 16:52:18 -0500985 SkASSERT(glyphCount <= kMaxGlyphCount);
986 SkScalar x, y;
987 const SkTextBlobBuilder::RunBuffer* buffer;
988 uint8_t runType;
989 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
Hal Canaryfc97f222018-12-17 13:48:44 -0500990 const void* textPtr = text.begin();
991 size_t textLen = SkToSizeT(text.count());
Hal Canary5395c592017-03-08 16:52:18 -0500992 switch (runType) {
993 case 0:
994 fuzz->next(&x, &y);
995 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500996 buffer = &textBlobBuilder.allocRun(font, glyphCount, x, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500997 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500998 break;
999 case 1:
1000 fuzz->next(&y);
1001 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -05001002 buffer = &textBlobBuilder.allocRunPosH(font, glyphCount, y);
Hal Canaryfc97f222018-12-17 13:48:44 -05001003 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001004 fuzz->nextN(buffer->pos, glyphCount);
1005 break;
1006 case 2:
1007 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -05001008 buffer = &textBlobBuilder.allocRunPos(font, glyphCount);
Hal Canaryfc97f222018-12-17 13:48:44 -05001009 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001010 fuzz->nextN(buffer->pos, glyphCount * 2);
1011 break;
1012 default:
1013 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001014 break;
Hal Canary5395c592017-03-08 16:52:18 -05001015 }
1016 }
1017 return textBlobBuilder.make();
1018}
1019
Hal Canary1e0138b2017-03-10 13:56:08 -05001020static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001021 if (!fuzz || !canvas || depth <= 0) {
1022 return;
1023 }
1024 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1025 unsigned N;
1026 fuzz->nextRange(&N, 0, 2000);
1027 for (unsigned i = 0; i < N; ++i) {
1028 if (fuzz->exhausted()) {
1029 return;
1030 }
1031 SkPaint paint;
Mike Reed358fcad2018-11-23 15:27:51 -05001032 SkFont font;
Hal Canary24ac42b2017-02-14 13:35:14 -05001033 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001034 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001035 switch (drawCommand) {
1036 case 0:
1037 canvas->flush();
1038 break;
1039 case 1:
1040 canvas->save();
1041 break;
1042 case 2: {
1043 SkRect bounds;
1044 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001045 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001046 canvas->saveLayer(&bounds, &paint);
1047 break;
1048 }
1049 case 3: {
1050 SkRect bounds;
1051 fuzz->next(&bounds);
1052 canvas->saveLayer(&bounds, nullptr);
1053 break;
1054 }
1055 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001056 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001057 canvas->saveLayer(nullptr, &paint);
1058 break;
1059 case 5:
1060 canvas->saveLayer(nullptr, nullptr);
1061 break;
1062 case 6: {
1063 uint8_t alpha;
1064 fuzz->next(&alpha);
1065 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1066 break;
1067 }
1068 case 7: {
1069 SkRect bounds;
1070 uint8_t alpha;
1071 fuzz->next(&bounds, &alpha);
1072 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1073 break;
1074 }
1075 case 8: {
1076 SkCanvas::SaveLayerRec saveLayerRec;
1077 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001078 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001079 fuzz->next(&bounds);
1080 saveLayerRec.fBounds = &bounds;
1081 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001082 if (make_fuzz_t<bool>(fuzz)) {
1083 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001084 saveLayerRec.fPaint = &paint;
1085 }
1086 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001087 if (make_fuzz_t<bool>(fuzz)) {
1088 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001089 saveLayerRec.fBackdrop = imageFilter.get();
1090 }
Hal Canary5395c592017-03-08 16:52:18 -05001091 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001092 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001093 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1094 // }
1095
Hal Canary24ac42b2017-02-14 13:35:14 -05001096 canvas->saveLayer(saveLayerRec);
1097 break;
1098 }
1099 case 9:
1100 canvas->restore();
1101 break;
1102 case 10: {
1103 int saveCount;
1104 fuzz->next(&saveCount);
1105 canvas->restoreToCount(saveCount);
1106 break;
1107 }
1108 case 11: {
1109 SkScalar x, y;
1110 fuzz->next(&x, &y);
1111 canvas->translate(x, y);
1112 break;
1113 }
1114 case 12: {
1115 SkScalar x, y;
1116 fuzz->next(&x, &y);
1117 canvas->scale(x, y);
1118 break;
1119 }
1120 case 13: {
1121 SkScalar v;
1122 fuzz->next(&v);
1123 canvas->rotate(v);
1124 break;
1125 }
1126 case 14: {
1127 SkScalar x, y, v;
1128 fuzz->next(&x, &y, &v);
1129 canvas->rotate(v, x, y);
1130 break;
1131 }
1132 case 15: {
1133 SkScalar x, y;
1134 fuzz->next(&x, &y);
1135 canvas->skew(x, y);
1136 break;
1137 }
1138 case 16: {
1139 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001140 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001141 canvas->concat(mat);
1142 break;
1143 }
1144 case 17: {
1145 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001146 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001147 canvas->setMatrix(mat);
1148 break;
1149 }
1150 case 18:
1151 canvas->resetMatrix();
1152 break;
1153 case 19: {
1154 SkRect r;
1155 int op;
1156 bool doAntiAlias;
1157 fuzz->next(&r, &doAntiAlias);
1158 fuzz->nextRange(&op, 0, 1);
1159 r.sort();
1160 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1161 break;
1162 }
1163 case 20: {
1164 SkRRect rr;
1165 int op;
1166 bool doAntiAlias;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001167 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001168 fuzz->next(&doAntiAlias);
1169 fuzz->nextRange(&op, 0, 1);
1170 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1171 break;
1172 }
1173 case 21: {
1174 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001175 FuzzNicePath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001176 int op;
1177 bool doAntiAlias;
1178 fuzz->next(&doAntiAlias);
1179 fuzz->nextRange(&op, 0, 1);
1180 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1181 break;
1182 }
1183 case 22: {
1184 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001185 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001186 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001187 fuzz->nextRange(&op, 0, 1);
1188 canvas->clipRegion(region, (SkClipOp)op);
1189 break;
1190 }
1191 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001192 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001193 canvas->drawPaint(paint);
1194 break;
1195 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001196 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001197 SkCanvas::PointMode pointMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001198 fuzz->nextRange(&pointMode,
Hal Canaryc8bebd42017-12-20 11:21:05 -05001199 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001200 size_t count;
1201 constexpr int kMaxCount = 30;
1202 fuzz->nextRange(&count, 0, kMaxCount);
1203 SkPoint pts[kMaxCount];
1204 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001205 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001206 break;
1207 }
1208 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001209 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001210 SkRect r;
1211 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001212 if (!r.isFinite()) {
1213 break;
1214 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001215 canvas->drawRect(r, paint);
1216 break;
1217 }
1218 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001219 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001220 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001221 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001222 canvas->drawRegion(region, paint);
1223 break;
1224 }
1225 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001226 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001227 SkRect r;
1228 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001229 if (!r.isFinite()) {
1230 break;
1231 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001232 canvas->drawOval(r, paint);
1233 break;
1234 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001235 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001236 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001237 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001238 SkRRect rr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001239 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001240 canvas->drawRRect(rr, paint);
1241 break;
1242 }
1243 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001244 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001245 SkRRect orr, irr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001246 FuzzNiceRRect(fuzz, &orr);
1247 FuzzNiceRRect(fuzz, &irr);
Hal Canary27bece82017-03-07 16:23:20 -05001248 if (orr.getBounds().contains(irr.getBounds())) {
1249 canvas->drawDRRect(orr, irr, paint);
1250 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001251 break;
1252 }
1253 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001254 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001255 SkRect r;
1256 SkScalar start, sweep;
1257 bool useCenter;
1258 fuzz->next(&r, &start, &sweep, &useCenter);
1259 canvas->drawArc(r, start, sweep, useCenter, paint);
1260 break;
1261 }
1262 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001263 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001264 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001265 FuzzNicePath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001266 canvas->drawPath(path, paint);
1267 break;
1268 }
1269 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001270 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001271 SkScalar left, top;
1272 bool usePaint;
1273 fuzz->next(&left, &top, &usePaint);
1274 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001275 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001276 }
1277 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1278 break;
1279 }
1280 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001281 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001282 SkRect src, dst;
1283 bool usePaint;
1284 fuzz->next(&src, &dst, &usePaint);
1285 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001286 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001287 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001288 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001289 break;
1290 }
1291 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001292 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001293 SkIRect src;
1294 SkRect dst;
1295 bool usePaint;
1296 fuzz->next(&src, &dst, &usePaint);
1297 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001298 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001299 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001300 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001301 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1302 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001303 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1304 break;
1305 }
1306 case 36: {
1307 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001308 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001309 SkRect dst;
1310 fuzz->next(&dst, &usePaint);
1311 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001312 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001313 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001314 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001315 break;
1316 }
1317 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001318 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001319 SkIRect center;
1320 SkRect dst;
1321 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001322 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001323 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001324 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001325 }
Hal Canary0361d492017-03-15 12:58:15 -04001326 if (make_fuzz_t<bool>(fuzz)) {
1327 fuzz->next(&center);
1328 } else { // Make valid center, see SkLatticeIter::Valid().
1329 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1330 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1331 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1332 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1333 }
1334 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001335 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1336 break;
1337 }
1338 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001339 SkBitmap bitmap = make_fuzz_bitmap(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->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1347 break;
1348 }
1349 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001350 SkBitmap bitmap = make_fuzz_bitmap(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->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1361 break;
1362 }
1363 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001364 SkBitmap img = make_fuzz_bitmap(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->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1376 break;
1377 }
1378 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001379 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001380 SkRect dst;
1381 bool usePaint;
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->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1390 break;
1391 }
1392 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001393 SkBitmap img = make_fuzz_bitmap(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().
Kevin Lubick1991f552018-02-27 10:59:10 -05001404 if (img.width() == 0 || img.height() == 0) {
1405 // bitmap may not have had its pixels initialized.
1406 break;
1407 }
Hal Canary0361d492017-03-15 12:58:15 -04001408 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1409 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1410 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1411 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1412 }
1413 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001414 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1415 break;
1416 }
1417 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001418 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001419 bool usePaint;
1420 SkRect dst;
1421 fuzz->next(&usePaint, &dst);
1422 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001423 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001424 }
1425 constexpr int kMax = 6;
1426 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001427 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001428 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1429 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1430 fuzz->nextN(xDivs, lattice.fXCount);
1431 fuzz->nextN(yDivs, lattice.fYCount);
1432 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1433 break;
1434 }
1435 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001436 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001437 bool usePaint;
1438 SkRect dst;
1439 fuzz->next(&usePaint, &dst);
1440 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001441 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001442 }
1443 constexpr int kMax = 6;
1444 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001445 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001446 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1447 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1448 fuzz->nextN(xDivs, lattice.fXCount);
1449 fuzz->nextN(yDivs, lattice.fYCount);
1450 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1451 break;
1452 }
1453 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001454 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed358fcad2018-11-23 15:27:51 -05001455 font = fuzz_font(fuzz);
1456 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001457 SkScalar x, y;
1458 fuzz->next(&x, &y);
Mike Reed358fcad2018-11-23 15:27:51 -05001459 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
1460 canvas->drawSimpleText(text.begin(), SkToSizeT(text.count()), encoding, x, y,
1461 font, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001462 break;
1463 }
1464 case 46: {
Mike Reed212e9062018-12-25 17:35:49 -05001465 // was drawPosText
Hal Canary24ac42b2017-02-14 13:35:14 -05001466 break;
1467 }
1468 case 47: {
Mike Reed212e9062018-12-25 17:35:49 -05001469 // was drawPosTextH
Hal Canary24ac42b2017-02-14 13:35:14 -05001470 break;
1471 }
1472 case 48: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001473 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001474 break;
1475 }
1476 case 49: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001477 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001478 break;
1479 }
1480 case 50: {
Mike Reed212e9062018-12-25 17:35:49 -05001481 // was drawTextRSXform
Hal Canary24ac42b2017-02-14 13:35:14 -05001482 break;
1483 }
1484 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001485 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001486 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001487 SkScalar x, y;
1488 fuzz->next(&x, &y);
1489 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001490 break;
1491 }
1492 case 52: {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001493 SkMatrix matrix;
Hal Canary24ac42b2017-02-14 13:35:14 -05001494 bool usePaint, useMatrix;
1495 fuzz->next(&usePaint, &useMatrix);
1496 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001497 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001498 }
1499 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001500 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -05001501 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001502 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001503 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1504 usePaint ? &paint : nullptr);
1505 break;
1506 }
1507 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001508 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001509 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001510 SkBlendMode blendMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001511 fuzz->nextRange(&vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
1512 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001513 constexpr int kMaxCount = 100;
1514 int vertexCount;
1515 SkPoint vertices[kMaxCount];
1516 SkPoint texs[kMaxCount];
1517 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001518 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1519 fuzz->nextN(vertices, vertexCount);
1520 bool useTexs, useColors;
1521 fuzz->next(&useTexs, &useColors);
1522 if (useTexs) {
1523 fuzz->nextN(texs, vertexCount);
1524 }
1525 if (useColors) {
1526 fuzz->nextN(colors, vertexCount);
1527 }
1528 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001529 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001530 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001531 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1532 for (int i = 0; i < indexCount; ++i) {
1533 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1534 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001535 }
Mike Reed887cdf12017-04-03 11:11:09 -04001536 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1537 useTexs ? texs : nullptr,
1538 useColors ? colors : nullptr,
1539 indexCount, indices),
1540 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001541 break;
1542 }
1543 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001544 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001545 break;
1546 }
1547 }
1548}
1549
Hal Canary1e0138b2017-03-10 13:56:08 -05001550static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001551 SkScalar w, h;
1552 fuzz->next(&w, &h);
1553 SkPictureRecorder pictureRecorder;
1554 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1555 return pictureRecorder.finishRecordingAsPicture();
1556}
1557
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001558DEF_FUZZ(NullCanvas, fuzz) {
1559 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001560}
1561
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001562constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001563
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001564DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001565 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001566 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001567 fuzz_canvas(fuzz, surface->getCanvas());
1568}
1569
Hal Canarye2924432017-12-01 11:46:26 -05001570DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1571 SkPictureRecorder recorder;
1572 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1573 SkIntToScalar(kCanvasSize.height())));
1574 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1575 if (!pic) { fuzz->signalBug(); }
1576 sk_sp<SkData> data = pic->serialize();
1577 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001578 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001579 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001580 if (!deserialized) { fuzz->signalBug(); }
1581 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1582 SkASSERT(surface && surface->getCanvas());
1583 surface->getCanvas()->drawPicture(deserialized);
1584}
1585
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001586DEF_FUZZ(ImageFilter, fuzz) {
1587 auto fil = make_fuzz_imageFilter(fuzz, 20);
1588
1589 SkPaint paint;
1590 paint.setImageFilter(fil);
1591 SkBitmap bitmap;
1592 SkCanvas canvas(bitmap);
1593 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1594}
1595
1596
1597//SkRandom _rand;
1598#define SK_ADD_RANDOM_BIT_FLIPS
1599
1600DEF_FUZZ(SerializedImageFilter, fuzz) {
Robert Phillipse5476bb2019-03-14 13:30:08 -04001601 SkBitmap bitmap;
1602 if (!bitmap.tryAllocN32Pixels(256, 256)) {
1603 SkDEBUGF("Could not allocate 256x256 bitmap in SerializedImageFilter");
1604 return;
1605 }
1606
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001607 auto filter = make_fuzz_imageFilter(fuzz, 20);
Robert Phillips66f6ef42018-09-14 11:58:40 -04001608 if (!filter) {
1609 return;
1610 }
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001611 auto data = filter->serialize();
1612 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1613 size_t len = data->size();
1614#ifdef SK_ADD_RANDOM_BIT_FLIPS
1615 unsigned char* p = const_cast<unsigned char*>(ptr);
1616 for (size_t i = 0; i < len; ++i, ++p) {
1617 uint8_t j;
1618 fuzz->nextRange(&j, 1, 250);
1619 if (j == 1) { // 0.4% of the time, flip a bit or byte
1620 uint8_t k;
1621 fuzz->nextRange(&k, 1, 10);
1622 if (k == 1) { // Then 10% of the time, change a whole byte
1623 uint8_t s;
1624 fuzz->nextRange(&s, 0, 2);
1625 switch(s) {
1626 case 0:
1627 *p ^= 0xFF; // Flip entire byte
1628 break;
1629 case 1:
1630 *p = 0xFF; // Set all bits to 1
1631 break;
1632 case 2:
1633 *p = 0x00; // Set all bits to 0
1634 break;
1635 }
1636 } else {
1637 uint8_t s;
1638 fuzz->nextRange(&s, 0, 7);
1639 *p ^= (1 << 7);
1640 }
1641 }
1642 }
1643#endif // SK_ADD_RANDOM_BIT_FLIPS
1644 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1645
1646 // uncomment below to write out a serialized image filter (to make corpus
1647 // for -t filter_fuzz)
1648 // SkString s("./serialized_filters/sf");
1649 // s.appendU32(_rand.nextU());
1650 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1651 // sk_fwrite(data->bytes(), data->size(), file);
1652 // sk_fclose(file);
1653
1654 SkPaint paint;
1655 paint.setImageFilter(deserializedFil);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001656
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001657 SkCanvas canvas(bitmap);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001658 canvas.saveLayer(SkRect::MakeWH(256, 256), &paint);
1659 canvas.restore();
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001660}
1661
Hal Canary44801ca2017-03-15 11:39:06 -04001662#if SK_SUPPORT_GPU
Kevin Lubickfaef5142018-06-07 10:33:11 -04001663
1664static void dump_GPU_info(GrContext* context) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001665 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->priv().getGpu())
Kevin Lubickfaef5142018-06-07 10:33:11 -04001666 ->glInterface();
1667 const GrGLubyte* output;
1668 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1669 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1670
1671 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1672 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1673
1674 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1675 SkDebugf("GL_VERSION %s\n", (const char*) output);
1676}
1677
Hal Canary5aa91582017-03-21 11:11:44 -07001678static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1679 SkASSERT(context);
1680 auto surface = SkSurface::MakeRenderTarget(
1681 context,
1682 SkBudgeted::kNo,
1683 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1684 SkASSERT(surface && surface->getCanvas());
1685 fuzz_canvas(fuzz, surface->getCanvas());
1686}
1687
Hal Canary44801ca2017-03-15 11:39:06 -04001688DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001689 sk_gpu_test::GrContextFactory f;
1690 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001691 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001692 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001693 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001694 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001695 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001696 }
Hal Canary5aa91582017-03-21 11:11:44 -07001697 fuzz_ganesh(fuzz, context);
1698}
1699
Kevin Lubick27d42192018-04-03 12:30:32 -04001700DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001701 sk_gpu_test::GrContextFactory f;
1702 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1703}
Hal Canary44801ca2017-03-15 11:39:06 -04001704#endif
1705
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001706DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001707 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -04001708 auto doc = SkPDF::MakeDocument(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001709 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1710 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001711}
1712
1713// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1714DEF_FUZZ(_DumpCanvas, fuzz) {
Mike Klein8f4e2242019-03-20 11:59:00 -05001715 DebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001716 fuzz_canvas(fuzz, &debugCanvas);
1717 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1718 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05001719 SkDynamicMemoryWStream stream;
1720 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1721 writer.beginObject(); // root
1722 debugCanvas.toJSON(writer, dataManager, debugCanvas.getSize(), nullCanvas.get());
1723 writer.endObject(); // root
1724 writer.flush();
1725 sk_sp<SkData> json = stream.detachAsData();
1726 fwrite(json->data(), json->size(), 1, stdout);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001727}