blob: f486a6ce5079270a9be20717ba4ff2cb80dd5b68 [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"
Zepeng Huba7cbf72020-07-01 13:21:03 +000026#include "include/svg/SkSVGCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/utils/SkNullCanvas.h"
28#include "src/core/SkOSFile.h"
29#include "src/core/SkPicturePriv.h"
30#include "tools/debugger/DebugCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050031
32// EFFECTS
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "include/core/SkTextBlob.h"
34#include "include/effects/Sk1DPathEffect.h"
35#include "include/effects/Sk2DPathEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "include/effects/SkColorMatrixFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "include/effects/SkCornerPathEffect.h"
38#include "include/effects/SkDashPathEffect.h"
39#include "include/effects/SkDiscretePathEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "include/effects/SkGradientShader.h"
41#include "include/effects/SkHighContrastFilter.h"
Michael Ludwigef43f682019-08-01 16:38:46 -040042#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "include/effects/SkLumaColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "include/effects/SkPerlinNoiseShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "include/effects/SkTableColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/core/SkReadBuffer.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050047
48// SRC
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/utils/SkUTF.h"
50#include "tools/flags/CommandLineFlags.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050051
Brian Salomonf4ba4ec2020-03-19 15:54:28 -040052#ifdef SK_GL
Robert Phillips7b4e43c2020-07-01 16:59:17 -040053#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054#include "include/gpu/gl/GrGLFunctions.h"
55#include "src/gpu/GrContextPriv.h"
56#include "src/gpu/gl/GrGLGpu.h"
57#include "src/gpu/gl/GrGLUtil.h"
58#include "tools/gpu/GrContextFactory.h"
Hal Canary44801ca2017-03-15 11:39:06 -040059#endif
60
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050061// MISC
62
63#include <iostream>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040064#include <utility>
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050065
Mike Klein84836b72019-03-21 11:31:36 -050066static DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets.");
Kevin Lubickfaef5142018-06-07 10:33:11 -040067
Hal Canary24ac42b2017-02-14 13:35:14 -050068// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050069// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040070// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050071
Hal Canary1e0138b2017-03-10 13:56:08 -050072// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
73// In fact, all make_fuzz_foo() functions have this potential problem.
74// Use sequence points!
75template <typename T>
76inline T make_fuzz_t(Fuzz* fuzz) {
77 T t;
78 fuzz->next(&t);
79 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050080}
81
Hal Canary1e0138b2017-03-10 13:56:08 -050082static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -050083
Hal Canary1e0138b2017-03-10 13:56:08 -050084static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -050085
Hal Canary1e0138b2017-03-10 13:56:08 -050086static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -050087
Hal Canary1e0138b2017-03-10 13:56:08 -050088static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -050089 if (depth <= 0) {
90 return nullptr;
91 }
92 int colorFilterType;
93 fuzz->nextRange(&colorFilterType, 0, 8);
94 switch (colorFilterType) {
95 case 0:
96 return nullptr;
97 case 1: {
98 SkColor color;
99 SkBlendMode mode;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400100 fuzz->next(&color);
Kevin Lubick00587e32019-06-03 11:27:16 -0400101 fuzz->nextEnum(&mode, SkBlendMode::kLastMode);
Mike Reedb286bc22019-04-08 16:23:20 -0400102 return SkColorFilters::Blend(color, mode);
Hal Canary27bece82017-03-07 16:23:20 -0500103 }
104 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500105 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500106 if (!outer) {
107 return nullptr;
108 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500109 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500110 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500111 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500112 }
113 case 3: {
Mike Reede869a1e2019-04-30 12:18:54 -0400114 float array[20];
Hal Canary27bece82017-03-07 16:23:20 -0500115 fuzz->nextN(array, SK_ARRAY_COUNT(array));
Mike Reede869a1e2019-04-30 12:18:54 -0400116 return SkColorFilters::Matrix(array);
Hal Canary27bece82017-03-07 16:23:20 -0500117 }
118 case 4: {
119 SkColor mul, add;
120 fuzz->next(&mul, &add);
121 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
122 }
123 case 5: {
124 bool grayscale;
125 int invertStyle;
126 float contrast;
127 fuzz->next(&grayscale);
128 fuzz->nextRange(&invertStyle, 0, 2);
129 fuzz->nextRange(&contrast, -1.0f, 1.0f);
130 return SkHighContrastFilter::Make(SkHighContrastConfig(
131 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
132 }
133 case 6:
134 return SkLumaColorFilter::Make();
135 case 7: {
136 uint8_t table[256];
137 fuzz->nextN(table, SK_ARRAY_COUNT(table));
138 return SkTableColorFilter::Make(table);
139 }
140 case 8: {
141 uint8_t tableA[256];
142 uint8_t tableR[256];
143 uint8_t tableG[256];
144 uint8_t tableB[256];
145 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
146 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
147 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
148 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
149 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
150 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500151 default:
152 SkASSERT(false);
153 break;
Hal Canary27bece82017-03-07 16:23:20 -0500154 }
155 return nullptr;
156}
Hal Canary24ac42b2017-02-14 13:35:14 -0500157
Hal Canary1e0138b2017-03-10 13:56:08 -0500158static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500159 SkScalar totalPos = 0;
160 for (int i = 0; i < colorCount; ++i) {
161 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
162 totalPos += pos[i];
163 }
164 totalPos = 1.0f / totalPos;
165 for (int i = 0; i < colorCount; ++i) {
166 pos[i] *= totalPos;
167 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500168 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500169 pos[colorCount - 1] = 1.0f;
170}
171
Hal Canary1e0138b2017-03-10 13:56:08 -0500172static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500173 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
174 sk_sp<SkColorFilter> colorFilter(nullptr);
175 SkBitmap bitmap;
176 sk_sp<SkImage> img;
Mike Reedfae8fce2019-04-03 10:27:45 -0400177 SkTileMode tmX, tmY;
Hal Canary24ac42b2017-02-14 13:35:14 -0500178 bool useMatrix;
179 SkColor color;
180 SkMatrix matrix;
181 SkBlendMode blendMode;
182 int shaderType;
183 if (depth <= 0) {
184 return nullptr;
185 }
Hal Canary671e4422017-02-27 13:36:38 -0500186 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500187 switch (shaderType) {
188 case 0:
189 return nullptr;
190 case 1:
Mike Reedc8bea7d2019-04-09 13:55:36 -0400191 return SkShaders::Empty();
Hal Canary24ac42b2017-02-14 13:35:14 -0500192 case 2:
193 fuzz->next(&color);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400194 return SkShaders::Color(color);
Hal Canary24ac42b2017-02-14 13:35:14 -0500195 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500196 img = make_fuzz_image(fuzz);
Kevin Lubick00587e32019-06-03 11:27:16 -0400197 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
198 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400199 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500200 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400201 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500202 }
203 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
204 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500205 bitmap = make_fuzz_bitmap(fuzz);
Kevin Lubick00587e32019-06-03 11:27:16 -0400206 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
207 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400208 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500209 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400210 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500211 }
Mike Reed50acf8f2019-04-08 13:20:23 -0400212 return bitmap.makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500213 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500214 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400215 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500216 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
217 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500218 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
219 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500220 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
221 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500222 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
223 shader2 = make_fuzz_shader(fuzz, depth - 1);
Kevin Lubick00587e32019-06-03 11:27:16 -0400224 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400225 return SkShaders::Blend(blendMode, std::move(shader1), std::move(shader2));
Hal Canaryb69c4b82017-03-08 11:02:40 -0500226 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500227 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500228 bool useTile;
229 SkRect tile;
Kevin Lubick00587e32019-06-03 11:27:16 -0400230 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
231 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400232 fuzz->next(&useMatrix, &useTile);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500233 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400234 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500235 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500236 if (useTile) {
237 fuzz->next(&tile);
238 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400239 return pic->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr, useTile ? &tile : nullptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500240 }
Hal Canary671e4422017-02-27 13:36:38 -0500241 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500242 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000243 // Deprecated SkGaussianEdgeShader
244 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500245 case 10: {
246 constexpr int kMaxColors = 12;
247 SkPoint pts[2];
248 SkColor colors[kMaxColors];
249 SkScalar pos[kMaxColors];
250 int colorCount;
251 bool usePos;
252 fuzz->nextN(pts, 2);
253 fuzz->nextRange(&colorCount, 2, kMaxColors);
254 fuzz->nextN(colors, colorCount);
Kevin Lubick00587e32019-06-03 11:27:16 -0400255 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400256 fuzz->next(&useMatrix, &usePos);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500257 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400258 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500259 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500260 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500261 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500262 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500263 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
264 tmX, 0, useMatrix ? &matrix : nullptr);
265 }
266 case 11: {
267 constexpr int kMaxColors = 12;
268 SkPoint center;
269 SkScalar radius;
270 int colorCount;
271 bool usePos;
272 SkColor colors[kMaxColors];
273 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400274 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400275 fuzz->next(&useMatrix, &usePos, &center, &radius);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500276 fuzz->nextRange(&colorCount, 2, kMaxColors);
277 fuzz->nextN(colors, colorCount);
278 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400279 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500280 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500281 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500282 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500283 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500284 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
285 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
286 }
287 case 12: {
288 constexpr int kMaxColors = 12;
289 SkPoint start, end;
290 SkScalar startRadius, endRadius;
291 int colorCount;
292 bool usePos;
293 SkColor colors[kMaxColors];
294 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400295 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400296 fuzz->next(&useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500297 fuzz->nextRange(&colorCount, 2, kMaxColors);
298 fuzz->nextN(colors, colorCount);
299 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400300 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500301 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500302 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500303 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500304 }
305 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
306 usePos ? pos : nullptr, colorCount, tmX, 0,
307 useMatrix ? &matrix : nullptr);
308 }
309 case 13: {
310 constexpr int kMaxColors = 12;
311 SkScalar cx, cy;
312 int colorCount;
313 bool usePos;
314 SkColor colors[kMaxColors];
315 SkScalar pos[kMaxColors];
316 fuzz->next(&cx, &cy, &useMatrix, &usePos);
317 fuzz->nextRange(&colorCount, 2, kMaxColors);
318 fuzz->nextN(colors, colorCount);
319 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400320 FuzzNiceMatrix(fuzz, &matrix);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500321 }
322 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500323 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500324 }
325 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
326 0, useMatrix ? &matrix : nullptr);
327 }
328 case 14: {
329 SkScalar baseFrequencyX, baseFrequencyY, seed;
330 int numOctaves;
331 SkISize tileSize;
332 bool useTileSize, turbulence;
333 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
334 if (useTileSize) {
335 fuzz->next(&tileSize);
336 }
337 fuzz->nextRange(&numOctaves, 2, 7);
338 if (turbulence) {
339 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
340 numOctaves, seed,
341 useTileSize ? &tileSize : nullptr);
342 } else {
343 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
344 numOctaves, seed,
345 useTileSize ? &tileSize : nullptr);
346 }
347 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500348 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500349 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500350 break;
351 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500352 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500353}
354
Hal Canary1e0138b2017-03-10 13:56:08 -0500355static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500356 if (depth <= 0) {
357 return nullptr;
358 }
359 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400360 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500361 switch (pathEffectType) {
362 case 0: {
363 return nullptr;
364 }
365 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500366 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
367 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500368 return SkPathEffect::MakeSum(std::move(first), std::move(second));
369 }
370 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500371 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
372 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500373 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
374 }
375 case 3: {
376 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400377 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500378 SkScalar advance, phase;
379 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500380 SkPath1DPathEffect::Style style;
Kevin Lubick00587e32019-06-03 11:27:16 -0400381 fuzz->nextEnum(&style, SkPath1DPathEffect::kLastEnum_Style);
Hal Canaryf7005202017-03-10 08:48:28 -0500382 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500383 }
384 case 4: {
385 SkScalar width;
386 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400387 fuzz->next(&width);
388 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500389 return SkLine2DPathEffect::Make(width, matrix);
390 }
391 case 5: {
392 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400393 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500394 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400395 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500396 return SkPath2DPathEffect::Make(matrix, path);
397 }
398 case 6: {
399 SkScalar radius;
400 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500401 return SkCornerPathEffect::Make(radius);
402 }
Mike Reed40e7e652017-07-22 22:12:59 -0400403 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500404 SkScalar phase;
405 fuzz->next(&phase);
406 SkScalar intervals[20];
407 int count;
408 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
409 fuzz->nextN(intervals, count);
410 return SkDashPathEffect::Make(intervals, count, phase);
411 }
Mike Reed40e7e652017-07-22 22:12:59 -0400412 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500413 SkScalar segLength, dev;
414 uint32_t seed;
415 fuzz->next(&segLength, &dev, &seed);
416 return SkDiscretePathEffect::Make(segLength, dev, seed);
417 }
418 default:
419 SkASSERT(false);
420 return nullptr;
421 }
422}
Hal Canary24ac42b2017-02-14 13:35:14 -0500423
Hal Canary1e0138b2017-03-10 13:56:08 -0500424static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500425 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400426 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500427 switch (maskfilterType) {
428 case 0:
429 return nullptr;
430 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500431 SkBlurStyle blurStyle;
Kevin Lubick00587e32019-06-03 11:27:16 -0400432 fuzz->nextEnum(&blurStyle, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500433 SkScalar sigma;
434 fuzz->next(&sigma);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400435 bool respectCTM;
436 fuzz->next(&respectCTM);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400437 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500438 }
Hal Canary5395c592017-03-08 16:52:18 -0500439 default:
440 SkASSERT(false);
441 return nullptr;
442 }
443}
Hal Canary24ac42b2017-02-14 13:35:14 -0500444
Hal Canary1e0138b2017-03-10 13:56:08 -0500445static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
446 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500447 return nullptr;
448 }
449 auto fontMugger = SkFontMgr::RefDefault();
450 SkASSERT(fontMugger);
451 int familyCount = fontMugger->countFamilies();
452 int i, j;
453 fuzz->nextRange(&i, 0, familyCount - 1);
454 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
455 int styleCount = family->count();
456 fuzz->nextRange(&j, 0, styleCount - 1);
457 return sk_sp<SkTypeface>(family->createTypeface(j));
458}
Hal Canary24ac42b2017-02-14 13:35:14 -0500459
Hal Canary1e0138b2017-03-10 13:56:08 -0500460static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500461
462static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
463 if (depth <= 0) {
464 return nullptr;
465 }
466 uint8_t imageFilterType;
467 fuzz->nextRange(&imageFilterType, 1, 6);
468 SkPoint3 p, q;
469 SkColor lightColor;
470 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
471 sk_sp<SkImageFilter> input;
Michael Ludwigef43f682019-08-01 16:38:46 -0400472 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500473 bool useCropRect;
474 fuzz->next(&useCropRect);
475 if (useCropRect) {
476 fuzz->next(&cropRect);
477 }
478 switch (imageFilterType) {
479 case 1:
480 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500481 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400482 return SkImageFilters::DistantLitDiffuse(p, lightColor, surfaceScale, k,
483 std::move(input),
484 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500485 case 2:
486 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500487 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400488 return SkImageFilters::PointLitDiffuse(p, lightColor, surfaceScale, k,
489 std::move(input),
490 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500491 case 3:
492 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500493 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400494 return SkImageFilters::SpotLitDiffuse(
Hal Canarye03c3e52017-03-09 11:33:35 -0500495 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
496 std::move(input), useCropRect ? &cropRect : nullptr);
497 case 4:
498 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500499 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400500 return SkImageFilters::DistantLitSpecular(p, lightColor, surfaceScale, k,
501 shininess, std::move(input),
502 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500503 case 5:
504 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500505 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400506 return SkImageFilters::PointLitSpecular(p, lightColor, surfaceScale, k,
507 shininess, std::move(input),
508 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500509 case 6:
510 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
511 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500512 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400513 return SkImageFilters::SpotLitSpecular(
Hal Canarye03c3e52017-03-09 11:33:35 -0500514 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
515 std::move(input), useCropRect ? &cropRect : nullptr);
516 default:
517 SkASSERT(false);
518 return nullptr;
519 }
520}
521
Hal Canary1e0138b2017-03-10 13:56:08 -0500522static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500523
Hal Canary1e0138b2017-03-10 13:56:08 -0500524static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500525 if (depth <= 0) {
526 return nullptr;
527 }
528 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500529 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500530 switch (imageFilterType) {
531 case 0:
532 return nullptr;
533 case 1: {
534 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500535 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500536 bool useCropRect;
537 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400538 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500539 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400540 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500541 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400542 return SkImageFilters::Blur(sigmaX, sigmaY, std::move(input),
543 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500544 }
545 case 2: {
546 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400547 FuzzNiceMatrix(fuzz, &matrix);
Hal Canarye03c3e52017-03-09 11:33:35 -0500548 SkFilterQuality quality;
Kevin Lubick00587e32019-06-03 11:27:16 -0400549 fuzz->nextEnum(&quality, SkFilterQuality::kLast_SkFilterQuality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500550 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400551 return SkImageFilters::MatrixTransform(matrix, quality, std::move(input));
Hal Canarye03c3e52017-03-09 11:33:35 -0500552 }
553 case 3: {
554 SkRegion region;
555 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500556 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500557 bool useCropRect;
558 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400559 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500560 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400561 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500562 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400563 return SkImageFilters::AlphaThreshold(region, innerMin, outerMax, std::move(input),
564 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500565 }
566 case 4: {
567 float k1, k2, k3, k4;
568 bool enforcePMColor;
569 bool useCropRect;
570 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500571 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
572 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400573 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500574 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400575 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500576 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400577 return SkImageFilters::Arithmetic(k1, k2, k3, k4, enforcePMColor,
578 std::move(background), std::move(foreground),
579 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500580 }
581 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500582 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
583 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500584 bool useCropRect;
Michael Ludwigef43f682019-08-01 16:38:46 -0400585 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500586 fuzz->next(&useCropRect);
587 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400588 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500589 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400590 return SkImageFilters::ColorFilter(std::move(cf), std::move(input),
591 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500592 }
593 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500594 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
595 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400596 return SkImageFilters::Compose(std::move(ifo), std::move(ifi));
Hal Canarye03c3e52017-03-09 11:33:35 -0500597 }
598 case 7: {
Michael Ludwigef43f682019-08-01 16:38:46 -0400599 SkColorChannel xChannelSelector, yChannelSelector;
600 fuzz->nextEnum(&xChannelSelector, SkColorChannel::kLastEnum);
601 fuzz->nextEnum(&yChannelSelector, SkColorChannel::kLastEnum);
Hal Canarye03c3e52017-03-09 11:33:35 -0500602 SkScalar scale;
603 bool useCropRect;
604 fuzz->next(&scale, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400605 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500606 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400607 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500608 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500609 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
610 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400611 return SkImageFilters::DisplacementMap(xChannelSelector, yChannelSelector, scale,
612 std::move(displacement), std::move(color),
613 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500614 }
615 case 8: {
616 SkScalar dx, dy, sigmaX, sigmaY;
617 SkColor color;
Michael Ludwigef43f682019-08-01 16:38:46 -0400618 bool shadowOnly, useCropRect;
619 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &shadowOnly, &useCropRect);
620 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500621 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400622 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500623 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500624 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400625 if (shadowOnly) {
626 return SkImageFilters::DropShadowOnly(dx, dy, sigmaX, sigmaY, color,
627 std::move(input),
628 useCropRect ? &cropRect : nullptr);
629 } else {
630 return SkImageFilters::DropShadow(dx, dy, sigmaX, sigmaY, color, std::move(input),
631 useCropRect ? &cropRect : nullptr);
632 }
Hal Canarye03c3e52017-03-09 11:33:35 -0500633 }
634 case 9:
Michael Ludwigef43f682019-08-01 16:38:46 -0400635 return SkImageFilters::Image(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500636 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500637 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500638 SkRect srcRect, dstRect;
639 SkFilterQuality filterQuality;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400640 fuzz->next(&srcRect, &dstRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400641 fuzz->nextEnum(&filterQuality, SkFilterQuality::kLast_SkFilterQuality);
Michael Ludwigef43f682019-08-01 16:38:46 -0400642 return SkImageFilters::Image(std::move(image), srcRect, dstRect, filterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500643 }
644 case 11:
645 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
646 case 12: {
647 SkRect srcRect;
648 SkScalar inset;
649 bool useCropRect;
Michael Ludwigef43f682019-08-01 16:38:46 -0400650 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500651 fuzz->next(&srcRect, &inset, &useCropRect);
652 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400653 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500654 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500655 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400656 return SkImageFilters::Magnifier(srcRect, inset, std::move(input),
657 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500658 }
659 case 13: {
660 constexpr int kMaxKernelSize = 5;
661 int32_t n, m;
662 fuzz->nextRange(&n, 1, kMaxKernelSize);
663 fuzz->nextRange(&m, 1, kMaxKernelSize);
664 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
665 fuzz->nextN(kernel, n * m);
666 int32_t offsetX, offsetY;
667 fuzz->nextRange(&offsetX, 0, n - 1);
668 fuzz->nextRange(&offsetY, 0, m - 1);
669 SkScalar gain, bias;
670 bool convolveAlpha, useCropRect;
671 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400672 SkTileMode tileMode;
673 fuzz->nextEnum(&tileMode, SkTileMode::kLastTileMode);
674 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500675 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400676 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500677 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500678 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400679 return SkImageFilters::MatrixConvolution(
Hal Canarye03c3e52017-03-09 11:33:35 -0500680 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
681 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
682 }
683 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500684 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
685 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500686 bool useCropRect;
687 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400688 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500689 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400690 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500691 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400692 return SkImageFilters::Merge(std::move(first), std::move(second),
693 useCropRect ? &cropRect : nullptr);
Mike Reed0bdaf052017-06-18 23:35:57 -0400694 }
695 case 15: {
696 constexpr int kMaxCount = 4;
697 sk_sp<SkImageFilter> ifs[kMaxCount];
698 int count;
699 fuzz->nextRange(&count, 1, kMaxCount);
700 for (int i = 0; i < count; ++i) {
701 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
702 }
703 bool useCropRect;
704 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400705 SkIRect cropRect;
Mike Reed0bdaf052017-06-18 23:35:57 -0400706 if (useCropRect) {
707 fuzz->next(&cropRect);
708 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400709 return SkImageFilters::Merge(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500710 }
711 case 16: {
712 int rx, ry;
713 fuzz->next(&rx, &ry);
714 bool useCropRect;
715 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400716 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500717 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400718 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500719 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500720 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400721 return SkImageFilters::Dilate(rx, ry, std::move(input),
722 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500723 }
724 case 17: {
725 int rx, ry;
726 fuzz->next(&rx, &ry);
727 bool useCropRect;
728 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400729 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500730 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);
Michael Ludwigef43f682019-08-01 16:38:46 -0400734 return SkImageFilters::Erode(rx, ry, std::move(input),
735 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500736 }
737 case 18: {
738 SkScalar dx, dy;
739 fuzz->next(&dx, &dy);
740 bool useCropRect;
741 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400742 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500743 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);
Michael Ludwigef43f682019-08-01 16:38:46 -0400747 return SkImageFilters::Offset(dx, dy, std::move(input),
748 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500749 }
750 case 19: {
751 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500752 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500753 bool useCropRect;
754 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400755 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500756 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400757 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500758 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400759 return SkImageFilters::Paint(paint, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500760 }
761 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500762 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400763 return SkImageFilters::Picture(std::move(picture));
Hal Canarye03c3e52017-03-09 11:33:35 -0500764 }
765 case 21: {
766 SkRect cropRect;
767 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500768 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400769 return SkImageFilters::Picture(std::move(picture), cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500770 }
771 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500772 SkRect src, dst;
773 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500774 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400775 return SkImageFilters::Tile(src, dst, std::move(input));
Hal Canarye03c3e52017-03-09 11:33:35 -0500776 }
Mike Reed77e487d2017-11-09 21:50:20 +0000777 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500778 SkBlendMode blendMode;
779 bool useCropRect;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400780 fuzz->next(&useCropRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400781 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Michael Ludwigef43f682019-08-01 16:38:46 -0400782 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500783 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400784 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500785 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500786 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
787 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400788 return SkImageFilters::Xfermode(blendMode, std::move(bg), std::move(fg),
789 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500790 }
791 default:
792 SkASSERT(false);
793 return nullptr;
794 }
795}
Hal Canary24ac42b2017-02-14 13:35:14 -0500796
Hal Canary1e0138b2017-03-10 13:56:08 -0500797static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500798 int w, h;
799 fuzz->nextRange(&w, 1, 1024);
800 fuzz->nextRange(&h, 1, 1024);
801 SkAutoTMalloc<SkPMColor> data(w * h);
802 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
803 int n = w * h;
804 for (int i = 0; i < n; ++i) {
805 SkColor c;
806 fuzz->next(&c);
807 data[i] = SkPreMultiplyColor(c);
808 }
809 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500810 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
811 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500812}
813
Hal Canary1e0138b2017-03-10 13:56:08 -0500814static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500815 SkBitmap bitmap;
816 int w, h;
817 fuzz->nextRange(&w, 1, 1024);
818 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500819 if (!bitmap.tryAllocN32Pixels(w, h)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400820 SkDEBUGF("Could not allocate pixels %d x %d", w, h);
Kevin Lubick1991f552018-02-27 10:59:10 -0500821 return bitmap;
822 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500823 for (int y = 0; y < h; ++y) {
824 for (int x = 0; x < w; ++x) {
825 SkColor c;
826 fuzz->next(&c);
827 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
828 }
829 }
830 return bitmap;
831}
832
Kevin Lubick00587e32019-06-03 11:27:16 -0400833template <typename T>
834static T make_fuzz_enum_range(Fuzz* fuzz, T maxv) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500835 T value;
Kevin Lubick00587e32019-06-03 11:27:16 -0400836 fuzz->nextEnum(&value, maxv);
Hal Canary1e0138b2017-03-10 13:56:08 -0500837 return value;
838}
839
840static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500841 if (!fuzz || !paint || depth <= 0) {
842 return;
843 }
844
Hal Canary1e0138b2017-03-10 13:56:08 -0500845 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
846 paint->setDither( make_fuzz_t<bool>(fuzz));
847 paint->setColor( make_fuzz_t<SkColor>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400848 paint->setBlendMode( make_fuzz_enum_range<SkBlendMode>(fuzz, SkBlendMode::kLastMode));
849 paint->setFilterQuality(make_fuzz_enum_range<SkFilterQuality>(fuzz, kLast_SkFilterQuality));
850 paint->setStyle( make_fuzz_enum_range<SkPaint::Style>(fuzz,
851 SkPaint::Style::kStrokeAndFill_Style));
Hal Canary1e0138b2017-03-10 13:56:08 -0500852 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
853 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
854 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
855 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
856 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500857
858 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500859 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
860 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400861 paint->setStrokeCap( make_fuzz_enum_range<SkPaint::Cap>(fuzz, SkPaint::kLast_Cap));
862 paint->setStrokeJoin( make_fuzz_enum_range<SkPaint::Join>(fuzz, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500863 }
864}
865
Mike Reed358fcad2018-11-23 15:27:51 -0500866static SkFont fuzz_font(Fuzz* fuzz) {
867 SkFont font;
868 font.setTypeface( make_fuzz_typeface(fuzz));
869 font.setSize( make_fuzz_t<SkScalar>(fuzz));
870 font.setScaleX( make_fuzz_t<SkScalar>(fuzz));
871 font.setSkewX( make_fuzz_t<SkScalar>(fuzz));
872 font.setLinearMetrics( make_fuzz_t<bool>(fuzz));
873 font.setSubpixel( make_fuzz_t<bool>(fuzz));
874 font.setEmbeddedBitmaps( make_fuzz_t<bool>(fuzz));
875 font.setForceAutoHinting( make_fuzz_t<bool>(fuzz));
876 font.setEmbolden( make_fuzz_t<bool>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400877 font.setHinting( make_fuzz_enum_range<SkFontHinting>(fuzz, SkFontHinting::kFull));
878 font.setEdging( make_fuzz_enum_range<SkFont::Edging>(fuzz,
879 SkFont::Edging::kSubpixelAntiAlias));
Mike Reed358fcad2018-11-23 15:27:51 -0500880 return font;
Hal Canary5395c592017-03-08 16:52:18 -0500881}
882
Mike Reed2ed78202018-11-21 15:10:08 -0500883static SkTextEncoding fuzz_paint_text_encoding(Fuzz* fuzz) {
Kevin Lubick00587e32019-06-03 11:27:16 -0400884 return make_fuzz_enum_range<SkTextEncoding>(fuzz, SkTextEncoding::kUTF32);
Hal Canary24ac42b2017-02-14 13:35:14 -0500885}
886
Hal Canary5395c592017-03-08 16:52:18 -0500887constexpr int kMaxGlyphCount = 30;
888
Mike Reed2ed78202018-11-21 15:10:08 -0500889static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkFont& font, SkTextEncoding encoding) {
Hal Canary671e4422017-02-27 13:36:38 -0500890 SkTDArray<uint8_t> array;
Ben Wagner51e15a62019-05-07 15:38:46 -0400891 if (SkTextEncoding::kGlyphID == encoding) {
Herb Derby087fad72019-01-22 14:45:16 -0500892 int glyphRange = font.getTypefaceOrDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500893 if (glyphRange == 0) {
894 // Some fuzzing environments have no fonts, so empty array is the best
895 // we can do.
896 return array;
897 }
Hal Canary671e4422017-02-27 13:36:38 -0500898 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500899 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500900 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
901 for (int i = 0; i < glyphCount; ++i) {
902 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
903 }
904 return array;
905 }
906 static const SkUnichar ranges[][2] = {
907 {0x0020, 0x007F},
908 {0x00A1, 0x0250},
909 {0x0400, 0x0500},
910 };
911 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500912 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500913 count += (ranges[i][1] - ranges[i][0]);
914 }
Hal Canary5395c592017-03-08 16:52:18 -0500915 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500916 SkUnichar buffer[kMaxLength];
917 int length;
918 fuzz->nextRange(&length, 1, kMaxLength);
919 for (int j = 0; j < length; ++j) {
920 int32_t value;
921 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500922 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500923 if (value + ranges[i][0] < ranges[i][1]) {
924 buffer[j] = value + ranges[i][0];
925 break;
926 } else {
927 value -= (ranges[i][1] - ranges[i][0]);
928 }
929 }
930 }
Mike Reed2ed78202018-11-21 15:10:08 -0500931 switch (encoding) {
Ben Wagner51e15a62019-05-07 15:38:46 -0400932 case SkTextEncoding::kUTF8: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500933 size_t utf8len = 0;
934 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400935 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -0500936 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500937 char* ptr = (char*)array.append(utf8len);
938 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400939 ptr += SkUTF::ToUTF8(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -0500940 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500941 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400942 case SkTextEncoding::kUTF16: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500943 size_t utf16len = 0;
944 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400945 utf16len += SkUTF::ToUTF16(buffer[j]);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500946 }
947 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
948 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400949 ptr += SkUTF::ToUTF16(buffer[j], ptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500950 }
951 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400952 case SkTextEncoding::kUTF32:
Hal Canary671e4422017-02-27 13:36:38 -0500953 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -0500954 break;
Hal Canary671e4422017-02-27 13:36:38 -0500955 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -0500956 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -0500957 break;
Hal Canary671e4422017-02-27 13:36:38 -0500958 }
959 return array;
960}
961
Zepeng Huba7cbf72020-07-01 13:21:03 +0000962static std::string make_fuzz_string(Fuzz* fuzz) {
963 int len;
964 fuzz->nextRange(&len, 0, kMaxGlyphCount);
965 std::string str(len, 0);
966 for (int i = 0; i < len; i++) {
967 fuzz->next(&str[i]);
968 }
969 return str;
970}
971
Hal Canary5395c592017-03-08 16:52:18 -0500972static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
973 SkTextBlobBuilder textBlobBuilder;
974 int8_t runCount;
975 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
976 while (runCount-- > 0) {
Mike Reed2ed78202018-11-21 15:10:08 -0500977 SkFont font;
978 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
979 font.setEdging(make_fuzz_t<bool>(fuzz) ? SkFont::Edging::kAlias : SkFont::Edging::kAntiAlias);
980 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
981 int glyphCount = font.countText(text.begin(), SkToSizeT(text.count()), encoding);
Hal Canary5395c592017-03-08 16:52:18 -0500982 SkASSERT(glyphCount <= kMaxGlyphCount);
983 SkScalar x, y;
984 const SkTextBlobBuilder::RunBuffer* buffer;
985 uint8_t runType;
986 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
Hal Canaryfc97f222018-12-17 13:48:44 -0500987 const void* textPtr = text.begin();
988 size_t textLen = SkToSizeT(text.count());
Hal Canary5395c592017-03-08 16:52:18 -0500989 switch (runType) {
990 case 0:
991 fuzz->next(&x, &y);
992 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500993 buffer = &textBlobBuilder.allocRun(font, glyphCount, x, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500994 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500995 break;
996 case 1:
997 fuzz->next(&y);
998 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500999 buffer = &textBlobBuilder.allocRunPosH(font, glyphCount, y);
Hal Canaryfc97f222018-12-17 13:48:44 -05001000 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001001 fuzz->nextN(buffer->pos, glyphCount);
1002 break;
1003 case 2:
1004 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -05001005 buffer = &textBlobBuilder.allocRunPos(font, glyphCount);
Hal Canaryfc97f222018-12-17 13:48:44 -05001006 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001007 fuzz->nextN(buffer->pos, glyphCount * 2);
1008 break;
1009 default:
1010 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001011 break;
Hal Canary5395c592017-03-08 16:52:18 -05001012 }
1013 }
1014 return textBlobBuilder.make();
1015}
1016
Hal Canary1e0138b2017-03-10 13:56:08 -05001017static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001018 if (!fuzz || !canvas || depth <= 0) {
1019 return;
1020 }
1021 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1022 unsigned N;
1023 fuzz->nextRange(&N, 0, 2000);
1024 for (unsigned i = 0; i < N; ++i) {
1025 if (fuzz->exhausted()) {
1026 return;
1027 }
1028 SkPaint paint;
Mike Reed358fcad2018-11-23 15:27:51 -05001029 SkFont font;
Hal Canary24ac42b2017-02-14 13:35:14 -05001030 unsigned drawCommand;
Zepeng Huba7cbf72020-07-01 13:21:03 +00001031 fuzz->nextRange(&drawCommand, 0, 62);
Hal Canary24ac42b2017-02-14 13:35:14 -05001032 switch (drawCommand) {
1033 case 0:
1034 canvas->flush();
1035 break;
1036 case 1:
1037 canvas->save();
1038 break;
1039 case 2: {
1040 SkRect bounds;
1041 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001042 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001043 canvas->saveLayer(&bounds, &paint);
1044 break;
1045 }
1046 case 3: {
1047 SkRect bounds;
1048 fuzz->next(&bounds);
1049 canvas->saveLayer(&bounds, nullptr);
1050 break;
1051 }
1052 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001053 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001054 canvas->saveLayer(nullptr, &paint);
1055 break;
1056 case 5:
1057 canvas->saveLayer(nullptr, nullptr);
1058 break;
1059 case 6: {
1060 uint8_t alpha;
1061 fuzz->next(&alpha);
1062 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1063 break;
1064 }
1065 case 7: {
1066 SkRect bounds;
1067 uint8_t alpha;
1068 fuzz->next(&bounds, &alpha);
1069 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1070 break;
1071 }
1072 case 8: {
1073 SkCanvas::SaveLayerRec saveLayerRec;
1074 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001075 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001076 fuzz->next(&bounds);
1077 saveLayerRec.fBounds = &bounds;
1078 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001079 if (make_fuzz_t<bool>(fuzz)) {
1080 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001081 saveLayerRec.fPaint = &paint;
1082 }
1083 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001084 if (make_fuzz_t<bool>(fuzz)) {
1085 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001086 saveLayerRec.fBackdrop = imageFilter.get();
1087 }
Hal Canary5395c592017-03-08 16:52:18 -05001088 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001089 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001090 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1091 // }
1092
Hal Canary24ac42b2017-02-14 13:35:14 -05001093 canvas->saveLayer(saveLayerRec);
1094 break;
1095 }
1096 case 9:
1097 canvas->restore();
1098 break;
1099 case 10: {
1100 int saveCount;
1101 fuzz->next(&saveCount);
1102 canvas->restoreToCount(saveCount);
1103 break;
1104 }
1105 case 11: {
1106 SkScalar x, y;
1107 fuzz->next(&x, &y);
1108 canvas->translate(x, y);
1109 break;
1110 }
1111 case 12: {
1112 SkScalar x, y;
1113 fuzz->next(&x, &y);
1114 canvas->scale(x, y);
1115 break;
1116 }
1117 case 13: {
1118 SkScalar v;
1119 fuzz->next(&v);
1120 canvas->rotate(v);
1121 break;
1122 }
1123 case 14: {
1124 SkScalar x, y, v;
1125 fuzz->next(&x, &y, &v);
1126 canvas->rotate(v, x, y);
1127 break;
1128 }
1129 case 15: {
1130 SkScalar x, y;
1131 fuzz->next(&x, &y);
1132 canvas->skew(x, y);
1133 break;
1134 }
1135 case 16: {
1136 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001137 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001138 canvas->concat(mat);
1139 break;
1140 }
1141 case 17: {
1142 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001143 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001144 canvas->setMatrix(mat);
1145 break;
1146 }
1147 case 18:
1148 canvas->resetMatrix();
1149 break;
1150 case 19: {
1151 SkRect r;
1152 int op;
1153 bool doAntiAlias;
1154 fuzz->next(&r, &doAntiAlias);
1155 fuzz->nextRange(&op, 0, 1);
1156 r.sort();
1157 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1158 break;
1159 }
1160 case 20: {
1161 SkRRect rr;
1162 int op;
1163 bool doAntiAlias;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001164 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001165 fuzz->next(&doAntiAlias);
1166 fuzz->nextRange(&op, 0, 1);
1167 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1168 break;
1169 }
1170 case 21: {
1171 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001172 FuzzNicePath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001173 int op;
1174 bool doAntiAlias;
1175 fuzz->next(&doAntiAlias);
1176 fuzz->nextRange(&op, 0, 1);
1177 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1178 break;
1179 }
1180 case 22: {
1181 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001182 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001183 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001184 fuzz->nextRange(&op, 0, 1);
1185 canvas->clipRegion(region, (SkClipOp)op);
1186 break;
1187 }
1188 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001189 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001190 canvas->drawPaint(paint);
1191 break;
1192 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001193 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001194 SkCanvas::PointMode pointMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001195 fuzz->nextRange(&pointMode,
Hal Canaryc8bebd42017-12-20 11:21:05 -05001196 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001197 size_t count;
1198 constexpr int kMaxCount = 30;
1199 fuzz->nextRange(&count, 0, kMaxCount);
1200 SkPoint pts[kMaxCount];
1201 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001202 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001203 break;
1204 }
1205 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001206 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001207 SkRect r;
1208 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001209 if (!r.isFinite()) {
1210 break;
1211 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001212 canvas->drawRect(r, paint);
1213 break;
1214 }
1215 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001216 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001217 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001218 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001219 canvas->drawRegion(region, paint);
1220 break;
1221 }
1222 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001223 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001224 SkRect r;
1225 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001226 if (!r.isFinite()) {
1227 break;
1228 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001229 canvas->drawOval(r, paint);
1230 break;
1231 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001232 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001233 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001234 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001235 SkRRect rr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001236 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001237 canvas->drawRRect(rr, paint);
1238 break;
1239 }
1240 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001241 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001242 SkRRect orr, irr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001243 FuzzNiceRRect(fuzz, &orr);
1244 FuzzNiceRRect(fuzz, &irr);
Hal Canary27bece82017-03-07 16:23:20 -05001245 if (orr.getBounds().contains(irr.getBounds())) {
1246 canvas->drawDRRect(orr, irr, paint);
1247 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001248 break;
1249 }
1250 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001251 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001252 SkRect r;
1253 SkScalar start, sweep;
1254 bool useCenter;
1255 fuzz->next(&r, &start, &sweep, &useCenter);
1256 canvas->drawArc(r, start, sweep, useCenter, paint);
1257 break;
1258 }
1259 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001260 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001261 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001262 FuzzNicePath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001263 canvas->drawPath(path, paint);
1264 break;
1265 }
1266 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001267 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001268 SkScalar left, top;
1269 bool usePaint;
1270 fuzz->next(&left, &top, &usePaint);
1271 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001272 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001273 }
1274 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1275 break;
1276 }
1277 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001278 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001279 SkRect src, dst;
1280 bool usePaint;
1281 fuzz->next(&src, &dst, &usePaint);
1282 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001283 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001284 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001285 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001286 break;
1287 }
1288 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001289 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001290 SkIRect src;
1291 SkRect dst;
1292 bool usePaint;
1293 fuzz->next(&src, &dst, &usePaint);
1294 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001295 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001296 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001297 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001298 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1299 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001300 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1301 break;
1302 }
1303 case 36: {
1304 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001305 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001306 SkRect dst;
1307 fuzz->next(&dst, &usePaint);
1308 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001309 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001310 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001311 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001312 break;
1313 }
1314 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001315 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001316 SkIRect center;
1317 SkRect dst;
1318 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001319 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001320 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001321 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001322 }
Hal Canary0361d492017-03-15 12:58:15 -04001323 if (make_fuzz_t<bool>(fuzz)) {
1324 fuzz->next(&center);
1325 } else { // Make valid center, see SkLatticeIter::Valid().
1326 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1327 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1328 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1329 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1330 }
1331 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001332 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1333 break;
1334 }
1335 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001336 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001337 SkScalar left, top;
1338 bool usePaint;
1339 fuzz->next(&left, &top, &usePaint);
1340 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001341 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001342 }
1343 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1344 break;
1345 }
1346 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001347 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001348 SkRect src, dst;
1349 bool usePaint;
1350 fuzz->next(&src, &dst, &usePaint);
1351 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001352 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001353 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001354 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1356 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001357 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1358 break;
1359 }
1360 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 SkIRect src;
1363 SkRect dst;
1364 bool usePaint;
1365 fuzz->next(&src, &dst, &usePaint);
1366 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001367 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001368 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001369 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001370 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1371 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001372 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1373 break;
1374 }
1375 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001376 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001377 SkRect dst;
1378 bool usePaint;
1379 fuzz->next(&dst, &usePaint);
1380 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001381 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001382 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001383 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001384 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1385 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001386 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1387 break;
1388 }
1389 case 42: {
Hal Canary24ac42b2017-02-14 13:35:14 -05001390 break;
1391 }
1392 case 43: {
Hal Canary24ac42b2017-02-14 13:35:14 -05001393 break;
1394 }
1395 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001396 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001397 bool usePaint;
1398 SkRect dst;
1399 fuzz->next(&usePaint, &dst);
1400 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001401 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001402 }
1403 constexpr int kMax = 6;
1404 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001405 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001406 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1407 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1408 fuzz->nextN(xDivs, lattice.fXCount);
1409 fuzz->nextN(yDivs, lattice.fYCount);
1410 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1411 break;
1412 }
1413 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001414 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed358fcad2018-11-23 15:27:51 -05001415 font = fuzz_font(fuzz);
1416 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001417 SkScalar x, y;
1418 fuzz->next(&x, &y);
Mike Reed358fcad2018-11-23 15:27:51 -05001419 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
1420 canvas->drawSimpleText(text.begin(), SkToSizeT(text.count()), encoding, x, y,
1421 font, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001422 break;
1423 }
1424 case 46: {
Mike Reed212e9062018-12-25 17:35:49 -05001425 // was drawPosText
Hal Canary24ac42b2017-02-14 13:35:14 -05001426 break;
1427 }
1428 case 47: {
Mike Reed212e9062018-12-25 17:35:49 -05001429 // was drawPosTextH
Hal Canary24ac42b2017-02-14 13:35:14 -05001430 break;
1431 }
1432 case 48: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001433 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001434 break;
1435 }
1436 case 49: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001437 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001438 break;
1439 }
1440 case 50: {
Mike Reed212e9062018-12-25 17:35:49 -05001441 // was drawTextRSXform
Hal Canary24ac42b2017-02-14 13:35:14 -05001442 break;
1443 }
1444 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001445 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001446 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001447 SkScalar x, y;
1448 fuzz->next(&x, &y);
1449 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001450 break;
1451 }
1452 case 52: {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001453 SkMatrix matrix;
Hal Canary24ac42b2017-02-14 13:35:14 -05001454 bool usePaint, useMatrix;
1455 fuzz->next(&usePaint, &useMatrix);
1456 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001457 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001458 }
1459 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001460 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -05001461 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001462 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001463 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1464 usePaint ? &paint : nullptr);
1465 break;
1466 }
1467 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001468 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001469 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001470 SkBlendMode blendMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001471 fuzz->nextRange(&vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
1472 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001473 constexpr int kMaxCount = 100;
1474 int vertexCount;
1475 SkPoint vertices[kMaxCount];
1476 SkPoint texs[kMaxCount];
1477 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001478 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1479 fuzz->nextN(vertices, vertexCount);
1480 bool useTexs, useColors;
1481 fuzz->next(&useTexs, &useColors);
1482 if (useTexs) {
1483 fuzz->nextN(texs, vertexCount);
1484 }
1485 if (useColors) {
1486 fuzz->nextN(colors, vertexCount);
1487 }
1488 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001489 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001490 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001491 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1492 for (int i = 0; i < indexCount; ++i) {
1493 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1494 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001495 }
Mike Reed887cdf12017-04-03 11:11:09 -04001496 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1497 useTexs ? texs : nullptr,
1498 useColors ? colors : nullptr,
1499 indexCount, indices),
1500 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001501 break;
1502 }
Zepeng Huba7cbf72020-07-01 13:21:03 +00001503 case 54: {
1504 SkColor color;
1505 SkBlendMode blendMode;
1506 fuzz->nextRange(&blendMode, 0, SkBlendMode::kSrcOver);
1507 fuzz->next(&color);
1508 canvas->drawColor(color, blendMode);
1509 break;
1510 }
1511 case 55: {
1512 SkColor4f color;
1513 SkBlendMode blendMode;
1514 float R, G, B, Alpha;
1515 fuzz->nextRange(&blendMode, 0, SkBlendMode::kSrcOver);
1516 fuzz->nextRange(&R, -1, 2);
1517 fuzz->nextRange(&G, -1, 2);
1518 fuzz->nextRange(&B, -1, 2);
1519 fuzz->nextRange(&Alpha, 0, 1);
1520 color = {R, G, B, Alpha};
1521 canvas->drawColor(color, blendMode);
1522 break;
1523 }
1524 case 56: {
1525 fuzz_paint(fuzz, &paint, depth - 1);
1526 SkPoint p0, p1;
1527 fuzz->next(&p0, &p1);
1528 canvas->drawLine(p0, p1, paint);
1529 break;
1530 }
1531 case 57: {
1532 fuzz_paint(fuzz, &paint, depth - 1);
1533 SkIRect r;
1534 fuzz->next(&r);
1535 canvas->drawIRect(r, paint);
1536 break;
1537 }
1538 case 58: {
1539 fuzz_paint(fuzz, &paint, depth - 1);
1540 SkScalar radius;
1541 SkPoint center;
1542 fuzz->next(&radius, &center);
1543 canvas->drawCircle(center, radius, paint);
1544 break;
1545 }
1546 case 59: {
1547 fuzz_paint(fuzz, &paint, depth - 1);
1548 SkRect oval;
1549 SkScalar startAngle, sweepAngle;
1550 bool useCenter;
1551 fuzz->next(&oval, &startAngle, &sweepAngle, &useCenter);
1552 canvas->drawArc(oval, startAngle, sweepAngle, useCenter, paint);
1553 break;
1554 }
1555 case 60: {
1556 fuzz_paint(fuzz, &paint, depth - 1);
1557 SkRect rect;
1558 SkScalar rx, ry;
1559 fuzz->next(&rect, &rx, &ry);
1560 canvas->drawRoundRect(rect, rx, ry, paint);
1561 break;
1562 }
1563 case 61: {
1564 fuzz_paint(fuzz, &paint, depth - 1);
1565 font = fuzz_font(fuzz);
1566 std::string str = make_fuzz_string(fuzz);
1567 SkScalar x, y;
1568 fuzz->next(&x, &y);
1569 canvas->drawString(str.c_str(), x, y, font, paint);
1570 break;
1571 }
1572 case 62: {
1573 fuzz_paint(fuzz, &paint, depth - 1);
1574 SkPoint cubics[12];
1575 SkColor colors[4];
1576 SkPoint texCoords[4];
1577 bool useTexCoords;
1578 fuzz->nextN(cubics, 12);
1579 fuzz->nextN(colors, 4);
1580 fuzz->next(&useTexCoords);
1581 if (useTexCoords) {
1582 fuzz->nextN(texCoords, 4);
1583 }
1584 SkBlendMode mode;
1585 fuzz->nextEnum(&mode, SkBlendMode::kLastMode);
1586 canvas->drawPatch(cubics, colors, useTexCoords ? texCoords : nullptr
1587 , mode, paint);
1588 break;
1589 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001590 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001591 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001592 break;
1593 }
1594 }
1595}
1596
Hal Canary1e0138b2017-03-10 13:56:08 -05001597static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001598 SkScalar w, h;
1599 fuzz->next(&w, &h);
1600 SkPictureRecorder pictureRecorder;
1601 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1602 return pictureRecorder.finishRecordingAsPicture();
1603}
1604
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001605DEF_FUZZ(NullCanvas, fuzz) {
1606 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001607}
1608
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001609constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001610
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001611DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001612 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001613 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001614 fuzz_canvas(fuzz, surface->getCanvas());
1615}
1616
Hal Canarye2924432017-12-01 11:46:26 -05001617DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1618 SkPictureRecorder recorder;
1619 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1620 SkIntToScalar(kCanvasSize.height())));
1621 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1622 if (!pic) { fuzz->signalBug(); }
1623 sk_sp<SkData> data = pic->serialize();
1624 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001625 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001626 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001627 if (!deserialized) { fuzz->signalBug(); }
1628 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1629 SkASSERT(surface && surface->getCanvas());
1630 surface->getCanvas()->drawPicture(deserialized);
1631}
1632
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001633DEF_FUZZ(ImageFilter, fuzz) {
1634 auto fil = make_fuzz_imageFilter(fuzz, 20);
1635
1636 SkPaint paint;
1637 paint.setImageFilter(fil);
1638 SkBitmap bitmap;
1639 SkCanvas canvas(bitmap);
1640 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1641}
1642
1643
1644//SkRandom _rand;
1645#define SK_ADD_RANDOM_BIT_FLIPS
1646
1647DEF_FUZZ(SerializedImageFilter, fuzz) {
Robert Phillipse5476bb2019-03-14 13:30:08 -04001648 SkBitmap bitmap;
1649 if (!bitmap.tryAllocN32Pixels(256, 256)) {
1650 SkDEBUGF("Could not allocate 256x256 bitmap in SerializedImageFilter");
1651 return;
1652 }
1653
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001654 auto filter = make_fuzz_imageFilter(fuzz, 20);
Robert Phillips66f6ef42018-09-14 11:58:40 -04001655 if (!filter) {
1656 return;
1657 }
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001658 auto data = filter->serialize();
1659 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1660 size_t len = data->size();
1661#ifdef SK_ADD_RANDOM_BIT_FLIPS
1662 unsigned char* p = const_cast<unsigned char*>(ptr);
1663 for (size_t i = 0; i < len; ++i, ++p) {
1664 uint8_t j;
1665 fuzz->nextRange(&j, 1, 250);
1666 if (j == 1) { // 0.4% of the time, flip a bit or byte
1667 uint8_t k;
1668 fuzz->nextRange(&k, 1, 10);
1669 if (k == 1) { // Then 10% of the time, change a whole byte
1670 uint8_t s;
1671 fuzz->nextRange(&s, 0, 2);
1672 switch(s) {
1673 case 0:
1674 *p ^= 0xFF; // Flip entire byte
1675 break;
1676 case 1:
1677 *p = 0xFF; // Set all bits to 1
1678 break;
1679 case 2:
1680 *p = 0x00; // Set all bits to 0
1681 break;
1682 }
1683 } else {
1684 uint8_t s;
1685 fuzz->nextRange(&s, 0, 7);
1686 *p ^= (1 << 7);
1687 }
1688 }
1689 }
1690#endif // SK_ADD_RANDOM_BIT_FLIPS
1691 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1692
1693 // uncomment below to write out a serialized image filter (to make corpus
1694 // for -t filter_fuzz)
1695 // SkString s("./serialized_filters/sf");
1696 // s.appendU32(_rand.nextU());
1697 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1698 // sk_fwrite(data->bytes(), data->size(), file);
1699 // sk_fclose(file);
1700
1701 SkPaint paint;
1702 paint.setImageFilter(deserializedFil);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001703
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001704 SkCanvas canvas(bitmap);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001705 canvas.saveLayer(SkRect::MakeWH(256, 256), &paint);
1706 canvas.restore();
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001707}
1708
Brian Salomonf4ba4ec2020-03-19 15:54:28 -04001709#ifdef SK_GL
Kevin Lubickfaef5142018-06-07 10:33:11 -04001710
Robert Phillips7b4e43c2020-07-01 16:59:17 -04001711static void dump_GPU_info(GrDirectContext* context) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001712 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->priv().getGpu())
Kevin Lubickfaef5142018-06-07 10:33:11 -04001713 ->glInterface();
1714 const GrGLubyte* output;
1715 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1716 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1717
1718 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1719 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1720
1721 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1722 SkDebugf("GL_VERSION %s\n", (const char*) output);
1723}
1724
Robert Phillips7b4e43c2020-07-01 16:59:17 -04001725static void fuzz_ganesh(Fuzz* fuzz, GrDirectContext* context) {
Hal Canary5aa91582017-03-21 11:11:44 -07001726 SkASSERT(context);
1727 auto surface = SkSurface::MakeRenderTarget(
1728 context,
1729 SkBudgeted::kNo,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001730 SkImageInfo::Make(kCanvasSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
Hal Canary5aa91582017-03-21 11:11:44 -07001731 SkASSERT(surface && surface->getCanvas());
1732 fuzz_canvas(fuzz, surface->getCanvas());
1733}
1734
Hal Canary44801ca2017-03-15 11:39:06 -04001735DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001736 sk_gpu_test::GrContextFactory f;
Robert Phillips7b4e43c2020-07-01 16:59:17 -04001737 auto context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001738 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001739 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001740 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001741 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001742 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001743 }
Hal Canary5aa91582017-03-21 11:11:44 -07001744 fuzz_ganesh(fuzz, context);
1745}
1746
Kevin Lubick27d42192018-04-03 12:30:32 -04001747DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001748 sk_gpu_test::GrContextFactory f;
1749 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1750}
Hal Canary44801ca2017-03-15 11:39:06 -04001751#endif
1752
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001753DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001754 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -04001755 auto doc = SkPDF::MakeDocument(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001756 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1757 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001758}
1759
1760// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1761DEF_FUZZ(_DumpCanvas, fuzz) {
Mike Klein8f4e2242019-03-20 11:59:00 -05001762 DebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001763 fuzz_canvas(fuzz, &debugCanvas);
1764 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1765 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05001766 SkDynamicMemoryWStream stream;
1767 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1768 writer.beginObject(); // root
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -05001769 debugCanvas.toJSON(writer, dataManager, nullCanvas.get());
Brian Osmand8a90f92019-01-28 13:41:19 -05001770 writer.endObject(); // root
1771 writer.flush();
1772 sk_sp<SkData> json = stream.detachAsData();
1773 fwrite(json->data(), json->size(), 1, stdout);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001774}
Zepeng Huba7cbf72020-07-01 13:21:03 +00001775
1776DEF_FUZZ(SVGCanvas, fuzz) {
1777 SkNullWStream stream;
1778 SkRect bounds = SkRect::MakeIWH(150, 150);
1779 std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &stream);
1780 fuzz_canvas(fuzz, canvas.get());
1781}