blob: 77ed87988092cde6700ad2037f00f2bbb4cd28ef [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"
Mike Reede78f8ce2020-12-19 15:14:06 -050029#include "src/core/SkPaintPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/core/SkPicturePriv.h"
31#include "tools/debugger/DebugCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050032
33// EFFECTS
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "include/core/SkTextBlob.h"
35#include "include/effects/Sk1DPathEffect.h"
36#include "include/effects/Sk2DPathEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "include/effects/SkColorMatrixFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "include/effects/SkCornerPathEffect.h"
39#include "include/effects/SkDashPathEffect.h"
40#include "include/effects/SkDiscretePathEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "include/effects/SkGradientShader.h"
42#include "include/effects/SkHighContrastFilter.h"
Michael Ludwigef43f682019-08-01 16:38:46 -040043#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "include/effects/SkLumaColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "include/effects/SkPerlinNoiseShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "include/effects/SkTableColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/core/SkReadBuffer.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050048
49// SRC
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/utils/SkUTF.h"
51#include "tools/flags/CommandLineFlags.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050052
Brian Salomonf4ba4ec2020-03-19 15:54:28 -040053#ifdef SK_GL
Robert Phillips7b4e43c2020-07-01 16:59:17 -040054#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050055#include "include/gpu/gl/GrGLFunctions.h"
Adlai Hollera0693042020-10-14 11:23:11 -040056#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050057#include "src/gpu/gl/GrGLGpu.h"
58#include "src/gpu/gl/GrGLUtil.h"
59#include "tools/gpu/GrContextFactory.h"
Hal Canary44801ca2017-03-15 11:39:06 -040060#endif
61
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050062// MISC
63
64#include <iostream>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040065#include <utility>
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050066
Mike Klein84836b72019-03-21 11:31:36 -050067static DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets.");
Kevin Lubickfaef5142018-06-07 10:33:11 -040068
Hal Canary24ac42b2017-02-14 13:35:14 -050069// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050070// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040071// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050072
Hal Canary1e0138b2017-03-10 13:56:08 -050073// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
74// In fact, all make_fuzz_foo() functions have this potential problem.
75// Use sequence points!
76template <typename T>
77inline T make_fuzz_t(Fuzz* fuzz) {
78 T t;
79 fuzz->next(&t);
80 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050081}
82
Hal Canary1e0138b2017-03-10 13:56:08 -050083static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -050084
Hal Canary1e0138b2017-03-10 13:56:08 -050085static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -050086
Hal Canary1e0138b2017-03-10 13:56:08 -050087static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -050088 if (depth <= 0) {
89 return nullptr;
90 }
91 int colorFilterType;
92 fuzz->nextRange(&colorFilterType, 0, 8);
93 switch (colorFilterType) {
94 case 0:
95 return nullptr;
96 case 1: {
97 SkColor color;
98 SkBlendMode mode;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -040099 fuzz->next(&color);
Kevin Lubick00587e32019-06-03 11:27:16 -0400100 fuzz->nextEnum(&mode, SkBlendMode::kLastMode);
Mike Reedb286bc22019-04-08 16:23:20 -0400101 return SkColorFilters::Blend(color, mode);
Hal Canary27bece82017-03-07 16:23:20 -0500102 }
103 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500104 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500105 if (!outer) {
106 return nullptr;
107 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500108 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500109 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500110 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500111 }
112 case 3: {
Mike Reede869a1e2019-04-30 12:18:54 -0400113 float array[20];
Hal Canary27bece82017-03-07 16:23:20 -0500114 fuzz->nextN(array, SK_ARRAY_COUNT(array));
Mike Reede869a1e2019-04-30 12:18:54 -0400115 return SkColorFilters::Matrix(array);
Hal Canary27bece82017-03-07 16:23:20 -0500116 }
117 case 4: {
118 SkColor mul, add;
119 fuzz->next(&mul, &add);
120 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
121 }
122 case 5: {
123 bool grayscale;
124 int invertStyle;
125 float contrast;
126 fuzz->next(&grayscale);
127 fuzz->nextRange(&invertStyle, 0, 2);
128 fuzz->nextRange(&contrast, -1.0f, 1.0f);
129 return SkHighContrastFilter::Make(SkHighContrastConfig(
130 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
131 }
132 case 6:
133 return SkLumaColorFilter::Make();
134 case 7: {
135 uint8_t table[256];
136 fuzz->nextN(table, SK_ARRAY_COUNT(table));
137 return SkTableColorFilter::Make(table);
138 }
139 case 8: {
140 uint8_t tableA[256];
141 uint8_t tableR[256];
142 uint8_t tableG[256];
143 uint8_t tableB[256];
144 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
145 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
146 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
147 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
148 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
149 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500150 default:
151 SkASSERT(false);
152 break;
Hal Canary27bece82017-03-07 16:23:20 -0500153 }
154 return nullptr;
155}
Hal Canary24ac42b2017-02-14 13:35:14 -0500156
Hal Canary1e0138b2017-03-10 13:56:08 -0500157static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500158 SkScalar totalPos = 0;
159 for (int i = 0; i < colorCount; ++i) {
160 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
161 totalPos += pos[i];
162 }
163 totalPos = 1.0f / totalPos;
164 for (int i = 0; i < colorCount; ++i) {
165 pos[i] *= totalPos;
166 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500167 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500168 pos[colorCount - 1] = 1.0f;
169}
170
Hal Canary1e0138b2017-03-10 13:56:08 -0500171static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500172 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
173 sk_sp<SkColorFilter> colorFilter(nullptr);
174 SkBitmap bitmap;
175 sk_sp<SkImage> img;
Mike Reedfae8fce2019-04-03 10:27:45 -0400176 SkTileMode tmX, tmY;
Hal Canary24ac42b2017-02-14 13:35:14 -0500177 bool useMatrix;
178 SkColor color;
179 SkMatrix matrix;
180 SkBlendMode blendMode;
181 int shaderType;
182 if (depth <= 0) {
183 return nullptr;
184 }
Hal Canary671e4422017-02-27 13:36:38 -0500185 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500186 switch (shaderType) {
187 case 0:
188 return nullptr;
189 case 1:
Mike Reedc8bea7d2019-04-09 13:55:36 -0400190 return SkShaders::Empty();
Hal Canary24ac42b2017-02-14 13:35:14 -0500191 case 2:
192 fuzz->next(&color);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400193 return SkShaders::Color(color);
Hal Canary24ac42b2017-02-14 13:35:14 -0500194 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500195 img = make_fuzz_image(fuzz);
Kevin Lubick00587e32019-06-03 11:27:16 -0400196 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
197 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400198 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500199 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400200 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500201 }
Mike Reed99c94462020-12-08 13:16:56 -0500202 return img->makeShader(tmX, tmY, SkSamplingOptions(), useMatrix ? &matrix : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500203 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500204 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400205 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500206 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
207 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500208 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
209 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500210 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
211 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500212 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
213 shader2 = make_fuzz_shader(fuzz, depth - 1);
Kevin Lubick00587e32019-06-03 11:27:16 -0400214 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400215 return SkShaders::Blend(blendMode, std::move(shader1), std::move(shader2));
Hal Canaryb69c4b82017-03-08 11:02:40 -0500216 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500217 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500218 bool useTile;
219 SkRect tile;
Kevin Lubick00587e32019-06-03 11:27:16 -0400220 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
221 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400222 fuzz->next(&useMatrix, &useTile);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500223 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400224 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500225 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500226 if (useTile) {
227 fuzz->next(&tile);
228 }
Mike Reed10a5ff22021-03-18 17:18:58 -0400229 return pic->makeShader(tmX, tmY, SkFilterMode::kNearest,
230 useMatrix ? &matrix : nullptr, useTile ? &tile : nullptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500231 }
Hal Canary671e4422017-02-27 13:36:38 -0500232 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500233 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000234 // Deprecated SkGaussianEdgeShader
235 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500236 case 10: {
237 constexpr int kMaxColors = 12;
238 SkPoint pts[2];
239 SkColor colors[kMaxColors];
240 SkScalar pos[kMaxColors];
241 int colorCount;
242 bool usePos;
243 fuzz->nextN(pts, 2);
244 fuzz->nextRange(&colorCount, 2, kMaxColors);
245 fuzz->nextN(colors, colorCount);
Kevin Lubick00587e32019-06-03 11:27:16 -0400246 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400247 fuzz->next(&useMatrix, &usePos);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500248 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400249 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500250 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500251 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500252 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500253 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500254 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
255 tmX, 0, useMatrix ? &matrix : nullptr);
256 }
257 case 11: {
258 constexpr int kMaxColors = 12;
259 SkPoint center;
260 SkScalar radius;
261 int colorCount;
262 bool usePos;
263 SkColor colors[kMaxColors];
264 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400265 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400266 fuzz->next(&useMatrix, &usePos, &center, &radius);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500267 fuzz->nextRange(&colorCount, 2, kMaxColors);
268 fuzz->nextN(colors, colorCount);
269 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400270 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500271 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500272 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500273 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500274 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500275 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
276 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
277 }
278 case 12: {
279 constexpr int kMaxColors = 12;
280 SkPoint start, end;
281 SkScalar startRadius, endRadius;
282 int colorCount;
283 bool usePos;
284 SkColor colors[kMaxColors];
285 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400286 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400287 fuzz->next(&useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500288 fuzz->nextRange(&colorCount, 2, kMaxColors);
289 fuzz->nextN(colors, colorCount);
290 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400291 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500292 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500293 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500294 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500295 }
296 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
297 usePos ? pos : nullptr, colorCount, tmX, 0,
298 useMatrix ? &matrix : nullptr);
299 }
300 case 13: {
301 constexpr int kMaxColors = 12;
302 SkScalar cx, cy;
303 int colorCount;
304 bool usePos;
305 SkColor colors[kMaxColors];
306 SkScalar pos[kMaxColors];
307 fuzz->next(&cx, &cy, &useMatrix, &usePos);
308 fuzz->nextRange(&colorCount, 2, kMaxColors);
309 fuzz->nextN(colors, colorCount);
310 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400311 FuzzNiceMatrix(fuzz, &matrix);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500312 }
313 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500314 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500315 }
316 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
317 0, useMatrix ? &matrix : nullptr);
318 }
319 case 14: {
320 SkScalar baseFrequencyX, baseFrequencyY, seed;
321 int numOctaves;
322 SkISize tileSize;
323 bool useTileSize, turbulence;
324 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
325 if (useTileSize) {
326 fuzz->next(&tileSize);
327 }
328 fuzz->nextRange(&numOctaves, 2, 7);
329 if (turbulence) {
330 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
331 numOctaves, seed,
332 useTileSize ? &tileSize : nullptr);
333 } else {
334 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
335 numOctaves, seed,
336 useTileSize ? &tileSize : nullptr);
337 }
338 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500339 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500340 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500341 break;
342 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500343 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500344}
345
Hal Canary1e0138b2017-03-10 13:56:08 -0500346static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500347 if (depth <= 0) {
348 return nullptr;
349 }
350 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400351 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500352 switch (pathEffectType) {
353 case 0: {
354 return nullptr;
355 }
356 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500357 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
358 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500359 return SkPathEffect::MakeSum(std::move(first), std::move(second));
360 }
361 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500362 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
363 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500364 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
365 }
366 case 3: {
367 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400368 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500369 SkScalar advance, phase;
370 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500371 SkPath1DPathEffect::Style style;
Kevin Lubick00587e32019-06-03 11:27:16 -0400372 fuzz->nextEnum(&style, SkPath1DPathEffect::kLastEnum_Style);
Hal Canaryf7005202017-03-10 08:48:28 -0500373 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500374 }
375 case 4: {
376 SkScalar width;
377 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400378 fuzz->next(&width);
379 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500380 return SkLine2DPathEffect::Make(width, matrix);
381 }
382 case 5: {
383 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400384 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500385 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400386 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500387 return SkPath2DPathEffect::Make(matrix, path);
388 }
389 case 6: {
390 SkScalar radius;
391 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500392 return SkCornerPathEffect::Make(radius);
393 }
Mike Reed40e7e652017-07-22 22:12:59 -0400394 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500395 SkScalar phase;
396 fuzz->next(&phase);
397 SkScalar intervals[20];
398 int count;
399 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
400 fuzz->nextN(intervals, count);
401 return SkDashPathEffect::Make(intervals, count, phase);
402 }
Mike Reed40e7e652017-07-22 22:12:59 -0400403 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500404 SkScalar segLength, dev;
405 uint32_t seed;
406 fuzz->next(&segLength, &dev, &seed);
407 return SkDiscretePathEffect::Make(segLength, dev, seed);
408 }
409 default:
410 SkASSERT(false);
411 return nullptr;
412 }
413}
Hal Canary24ac42b2017-02-14 13:35:14 -0500414
Hal Canary1e0138b2017-03-10 13:56:08 -0500415static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500416 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400417 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500418 switch (maskfilterType) {
419 case 0:
420 return nullptr;
421 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500422 SkBlurStyle blurStyle;
Kevin Lubick00587e32019-06-03 11:27:16 -0400423 fuzz->nextEnum(&blurStyle, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500424 SkScalar sigma;
425 fuzz->next(&sigma);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400426 bool respectCTM;
427 fuzz->next(&respectCTM);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400428 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500429 }
Hal Canary5395c592017-03-08 16:52:18 -0500430 default:
431 SkASSERT(false);
432 return nullptr;
433 }
434}
Hal Canary24ac42b2017-02-14 13:35:14 -0500435
Hal Canary1e0138b2017-03-10 13:56:08 -0500436static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
437 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500438 return nullptr;
439 }
440 auto fontMugger = SkFontMgr::RefDefault();
441 SkASSERT(fontMugger);
442 int familyCount = fontMugger->countFamilies();
443 int i, j;
444 fuzz->nextRange(&i, 0, familyCount - 1);
445 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
446 int styleCount = family->count();
447 fuzz->nextRange(&j, 0, styleCount - 1);
448 return sk_sp<SkTypeface>(family->createTypeface(j));
449}
Hal Canary24ac42b2017-02-14 13:35:14 -0500450
Hal Canary1e0138b2017-03-10 13:56:08 -0500451static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500452
453static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
454 if (depth <= 0) {
455 return nullptr;
456 }
457 uint8_t imageFilterType;
458 fuzz->nextRange(&imageFilterType, 1, 6);
459 SkPoint3 p, q;
460 SkColor lightColor;
461 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
462 sk_sp<SkImageFilter> input;
Michael Ludwigef43f682019-08-01 16:38:46 -0400463 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500464 bool useCropRect;
465 fuzz->next(&useCropRect);
466 if (useCropRect) {
467 fuzz->next(&cropRect);
468 }
469 switch (imageFilterType) {
470 case 1:
471 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500472 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400473 return SkImageFilters::DistantLitDiffuse(p, lightColor, surfaceScale, k,
474 std::move(input),
475 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500476 case 2:
477 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500478 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400479 return SkImageFilters::PointLitDiffuse(p, lightColor, surfaceScale, k,
480 std::move(input),
481 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500482 case 3:
483 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500484 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400485 return SkImageFilters::SpotLitDiffuse(
Hal Canarye03c3e52017-03-09 11:33:35 -0500486 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
487 std::move(input), useCropRect ? &cropRect : nullptr);
488 case 4:
489 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500490 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400491 return SkImageFilters::DistantLitSpecular(p, lightColor, surfaceScale, k,
492 shininess, std::move(input),
493 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500494 case 5:
495 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500496 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400497 return SkImageFilters::PointLitSpecular(p, lightColor, surfaceScale, k,
498 shininess, std::move(input),
499 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500500 case 6:
501 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
502 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500503 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400504 return SkImageFilters::SpotLitSpecular(
Hal Canarye03c3e52017-03-09 11:33:35 -0500505 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
506 std::move(input), useCropRect ? &cropRect : nullptr);
507 default:
508 SkASSERT(false);
509 return nullptr;
510 }
511}
512
Hal Canary1e0138b2017-03-10 13:56:08 -0500513static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500514
Mike Reed1f261da2021-07-18 10:54:25 -0400515static SkSamplingOptions next_sampling(Fuzz* fuzz) {
516 if (fuzz->nextBool()) {
517 float B, C;
518 fuzz->next(&B, &C);
519 return SkSamplingOptions({B, C});
520 } else {
521 SkFilterMode fm;
522 SkMipmapMode mm;
523 fuzz->nextEnum(&fm, SkFilterMode::kLast);
524 fuzz->nextEnum(&mm, SkMipmapMode::kLast);
525 return SkSamplingOptions(fm, mm);
526 }
527}
528
Hal Canary1e0138b2017-03-10 13:56:08 -0500529static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500530 if (depth <= 0) {
531 return nullptr;
532 }
533 uint8_t imageFilterType;
Michael Ludwig7d0f8532020-10-07 15:27:20 -0400534 fuzz->nextRange(&imageFilterType, 0, 24);
Hal Canarye03c3e52017-03-09 11:33:35 -0500535 switch (imageFilterType) {
536 case 0:
537 return nullptr;
538 case 1: {
539 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500540 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500541 bool useCropRect;
542 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400543 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500544 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400545 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500546 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400547 return SkImageFilters::Blur(sigmaX, sigmaY, std::move(input),
548 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500549 }
550 case 2: {
551 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400552 FuzzNiceMatrix(fuzz, &matrix);
Mike Reed1f261da2021-07-18 10:54:25 -0400553 const auto sampling = next_sampling(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -0500554 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Mike Reed92236652021-02-01 13:07:32 -0500555 return SkImageFilters::MatrixTransform(matrix, sampling, std::move(input));
Hal Canarye03c3e52017-03-09 11:33:35 -0500556 }
557 case 3: {
558 SkRegion region;
559 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500560 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500561 bool useCropRect;
562 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400563 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500564 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400565 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500566 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400567 return SkImageFilters::AlphaThreshold(region, innerMin, outerMax, std::move(input),
568 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500569 }
570 case 4: {
571 float k1, k2, k3, k4;
572 bool enforcePMColor;
573 bool useCropRect;
574 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500575 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
576 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400577 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500578 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400579 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500580 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400581 return SkImageFilters::Arithmetic(k1, k2, k3, k4, enforcePMColor,
582 std::move(background), std::move(foreground),
583 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500584 }
585 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500586 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
587 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500588 bool useCropRect;
Michael Ludwigef43f682019-08-01 16:38:46 -0400589 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500590 fuzz->next(&useCropRect);
591 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400592 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500593 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400594 return SkImageFilters::ColorFilter(std::move(cf), std::move(input),
595 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500596 }
597 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500598 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
599 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400600 return SkImageFilters::Compose(std::move(ifo), std::move(ifi));
Hal Canarye03c3e52017-03-09 11:33:35 -0500601 }
602 case 7: {
Michael Ludwigef43f682019-08-01 16:38:46 -0400603 SkColorChannel xChannelSelector, yChannelSelector;
604 fuzz->nextEnum(&xChannelSelector, SkColorChannel::kLastEnum);
605 fuzz->nextEnum(&yChannelSelector, SkColorChannel::kLastEnum);
Hal Canarye03c3e52017-03-09 11:33:35 -0500606 SkScalar scale;
607 bool useCropRect;
608 fuzz->next(&scale, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400609 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500610 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400611 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500612 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500613 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
614 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400615 return SkImageFilters::DisplacementMap(xChannelSelector, yChannelSelector, scale,
616 std::move(displacement), std::move(color),
617 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500618 }
619 case 8: {
620 SkScalar dx, dy, sigmaX, sigmaY;
621 SkColor color;
Michael Ludwigef43f682019-08-01 16:38:46 -0400622 bool shadowOnly, useCropRect;
623 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &shadowOnly, &useCropRect);
624 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500625 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400626 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500627 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500628 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400629 if (shadowOnly) {
630 return SkImageFilters::DropShadowOnly(dx, dy, sigmaX, sigmaY, color,
631 std::move(input),
632 useCropRect ? &cropRect : nullptr);
633 } else {
634 return SkImageFilters::DropShadow(dx, dy, sigmaX, sigmaY, color, std::move(input),
635 useCropRect ? &cropRect : nullptr);
636 }
Hal Canarye03c3e52017-03-09 11:33:35 -0500637 }
638 case 9:
Michael Ludwigef43f682019-08-01 16:38:46 -0400639 return SkImageFilters::Image(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500640 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500641 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500642 SkRect srcRect, dstRect;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400643 fuzz->next(&srcRect, &dstRect);
Mike Reed1f261da2021-07-18 10:54:25 -0400644 return SkImageFilters::Image(std::move(image), srcRect, dstRect, next_sampling(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500645 }
646 case 11:
647 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
648 case 12: {
649 SkRect srcRect;
650 SkScalar inset;
651 bool useCropRect;
Michael Ludwigef43f682019-08-01 16:38:46 -0400652 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500653 fuzz->next(&srcRect, &inset, &useCropRect);
654 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400655 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500656 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500657 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400658 return SkImageFilters::Magnifier(srcRect, inset, std::move(input),
659 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500660 }
661 case 13: {
662 constexpr int kMaxKernelSize = 5;
663 int32_t n, m;
664 fuzz->nextRange(&n, 1, kMaxKernelSize);
665 fuzz->nextRange(&m, 1, kMaxKernelSize);
666 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
667 fuzz->nextN(kernel, n * m);
668 int32_t offsetX, offsetY;
669 fuzz->nextRange(&offsetX, 0, n - 1);
670 fuzz->nextRange(&offsetY, 0, m - 1);
671 SkScalar gain, bias;
672 bool convolveAlpha, useCropRect;
673 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400674 SkTileMode tileMode;
675 fuzz->nextEnum(&tileMode, SkTileMode::kLastTileMode);
676 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500677 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400678 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500679 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500680 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400681 return SkImageFilters::MatrixConvolution(
Hal Canarye03c3e52017-03-09 11:33:35 -0500682 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
683 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
684 }
685 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500686 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
687 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500688 bool useCropRect;
689 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400690 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500691 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400692 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500693 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400694 return SkImageFilters::Merge(std::move(first), std::move(second),
695 useCropRect ? &cropRect : nullptr);
Mike Reed0bdaf052017-06-18 23:35:57 -0400696 }
697 case 15: {
698 constexpr int kMaxCount = 4;
699 sk_sp<SkImageFilter> ifs[kMaxCount];
700 int count;
701 fuzz->nextRange(&count, 1, kMaxCount);
702 for (int i = 0; i < count; ++i) {
703 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
704 }
705 bool useCropRect;
706 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400707 SkIRect cropRect;
Mike Reed0bdaf052017-06-18 23:35:57 -0400708 if (useCropRect) {
709 fuzz->next(&cropRect);
710 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400711 return SkImageFilters::Merge(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500712 }
713 case 16: {
714 int rx, ry;
715 fuzz->next(&rx, &ry);
716 bool useCropRect;
717 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400718 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500719 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400720 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500721 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500722 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400723 return SkImageFilters::Dilate(rx, ry, std::move(input),
724 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500725 }
726 case 17: {
727 int rx, ry;
728 fuzz->next(&rx, &ry);
729 bool useCropRect;
730 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400731 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500732 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400733 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500734 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500735 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400736 return SkImageFilters::Erode(rx, ry, std::move(input),
737 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500738 }
739 case 18: {
740 SkScalar dx, dy;
741 fuzz->next(&dx, &dy);
742 bool useCropRect;
743 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400744 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500745 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400746 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500747 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500748 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400749 return SkImageFilters::Offset(dx, dy, std::move(input),
750 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500751 }
752 case 19: {
753 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500754 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500755 bool useCropRect;
756 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400757 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500758 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400759 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500760 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400761 return SkImageFilters::Paint(paint, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500762 }
763 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500764 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400765 return SkImageFilters::Picture(std::move(picture));
Hal Canarye03c3e52017-03-09 11:33:35 -0500766 }
767 case 21: {
768 SkRect cropRect;
769 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500770 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400771 return SkImageFilters::Picture(std::move(picture), cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500772 }
773 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500774 SkRect src, dst;
775 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500776 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400777 return SkImageFilters::Tile(src, dst, std::move(input));
Hal Canarye03c3e52017-03-09 11:33:35 -0500778 }
Mike Reed77e487d2017-11-09 21:50:20 +0000779 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500780 SkBlendMode blendMode;
781 bool useCropRect;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400782 fuzz->next(&useCropRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400783 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Michael Ludwigef43f682019-08-01 16:38:46 -0400784 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500785 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400786 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500787 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500788 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
789 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwig01b93ea2020-10-09 10:45:07 -0400790 return SkImageFilters::Blend(blendMode, std::move(bg), std::move(fg),
791 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500792 }
Michael Ludwig7d0f8532020-10-07 15:27:20 -0400793 case 24: {
794 sk_sp<SkShader> shader = make_fuzz_shader(fuzz, depth - 1);
795 bool useCropRect;
796 fuzz->next(&useCropRect);
797 SkIRect cropRect;
798 if (useCropRect) {
799 fuzz->next(&cropRect);
800 }
801 return SkImageFilters::Shader(std::move(shader), useCropRect ? &cropRect : nullptr);
802 }
Hal Canarye03c3e52017-03-09 11:33:35 -0500803 default:
804 SkASSERT(false);
805 return nullptr;
806 }
807}
Hal Canary24ac42b2017-02-14 13:35:14 -0500808
Hal Canary1e0138b2017-03-10 13:56:08 -0500809static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500810 int w, h;
811 fuzz->nextRange(&w, 1, 1024);
812 fuzz->nextRange(&h, 1, 1024);
813 SkAutoTMalloc<SkPMColor> data(w * h);
814 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
815 int n = w * h;
816 for (int i = 0; i < n; ++i) {
817 SkColor c;
818 fuzz->next(&c);
819 data[i] = SkPreMultiplyColor(c);
820 }
821 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500822 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
823 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500824}
825
Kevin Lubick00587e32019-06-03 11:27:16 -0400826template <typename T>
827static T make_fuzz_enum_range(Fuzz* fuzz, T maxv) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500828 T value;
Kevin Lubick00587e32019-06-03 11:27:16 -0400829 fuzz->nextEnum(&value, maxv);
Hal Canary1e0138b2017-03-10 13:56:08 -0500830 return value;
831}
832
833static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500834 if (!fuzz || !paint || depth <= 0) {
835 return;
836 }
837
Hal Canary1e0138b2017-03-10 13:56:08 -0500838 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
839 paint->setDither( make_fuzz_t<bool>(fuzz));
840 paint->setColor( make_fuzz_t<SkColor>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400841 paint->setBlendMode( make_fuzz_enum_range<SkBlendMode>(fuzz, SkBlendMode::kLastMode));
Kevin Lubick00587e32019-06-03 11:27:16 -0400842 paint->setStyle( make_fuzz_enum_range<SkPaint::Style>(fuzz,
843 SkPaint::Style::kStrokeAndFill_Style));
Hal Canary1e0138b2017-03-10 13:56:08 -0500844 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
845 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
846 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
847 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
848 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500849
850 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500851 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
852 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400853 paint->setStrokeCap( make_fuzz_enum_range<SkPaint::Cap>(fuzz, SkPaint::kLast_Cap));
854 paint->setStrokeJoin( make_fuzz_enum_range<SkPaint::Join>(fuzz, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500855 }
856}
857
Mike Reed358fcad2018-11-23 15:27:51 -0500858static SkFont fuzz_font(Fuzz* fuzz) {
859 SkFont font;
860 font.setTypeface( make_fuzz_typeface(fuzz));
861 font.setSize( make_fuzz_t<SkScalar>(fuzz));
862 font.setScaleX( make_fuzz_t<SkScalar>(fuzz));
863 font.setSkewX( make_fuzz_t<SkScalar>(fuzz));
864 font.setLinearMetrics( make_fuzz_t<bool>(fuzz));
865 font.setSubpixel( make_fuzz_t<bool>(fuzz));
866 font.setEmbeddedBitmaps( make_fuzz_t<bool>(fuzz));
867 font.setForceAutoHinting( make_fuzz_t<bool>(fuzz));
868 font.setEmbolden( make_fuzz_t<bool>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400869 font.setHinting( make_fuzz_enum_range<SkFontHinting>(fuzz, SkFontHinting::kFull));
870 font.setEdging( make_fuzz_enum_range<SkFont::Edging>(fuzz,
871 SkFont::Edging::kSubpixelAntiAlias));
Mike Reed358fcad2018-11-23 15:27:51 -0500872 return font;
Hal Canary5395c592017-03-08 16:52:18 -0500873}
874
Mike Reed2ed78202018-11-21 15:10:08 -0500875static SkTextEncoding fuzz_paint_text_encoding(Fuzz* fuzz) {
Kevin Lubick00587e32019-06-03 11:27:16 -0400876 return make_fuzz_enum_range<SkTextEncoding>(fuzz, SkTextEncoding::kUTF32);
Hal Canary24ac42b2017-02-14 13:35:14 -0500877}
878
Hal Canary5395c592017-03-08 16:52:18 -0500879constexpr int kMaxGlyphCount = 30;
880
Mike Reed2ed78202018-11-21 15:10:08 -0500881static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkFont& font, SkTextEncoding encoding) {
Hal Canary671e4422017-02-27 13:36:38 -0500882 SkTDArray<uint8_t> array;
Ben Wagner51e15a62019-05-07 15:38:46 -0400883 if (SkTextEncoding::kGlyphID == encoding) {
Herb Derby087fad72019-01-22 14:45:16 -0500884 int glyphRange = font.getTypefaceOrDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500885 if (glyphRange == 0) {
886 // Some fuzzing environments have no fonts, so empty array is the best
887 // we can do.
888 return array;
889 }
Hal Canary671e4422017-02-27 13:36:38 -0500890 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500891 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500892 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
893 for (int i = 0; i < glyphCount; ++i) {
894 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
895 }
896 return array;
897 }
898 static const SkUnichar ranges[][2] = {
899 {0x0020, 0x007F},
900 {0x00A1, 0x0250},
901 {0x0400, 0x0500},
902 };
903 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500904 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500905 count += (ranges[i][1] - ranges[i][0]);
906 }
Hal Canary5395c592017-03-08 16:52:18 -0500907 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500908 SkUnichar buffer[kMaxLength];
909 int length;
910 fuzz->nextRange(&length, 1, kMaxLength);
911 for (int j = 0; j < length; ++j) {
912 int32_t value;
913 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500914 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500915 if (value + ranges[i][0] < ranges[i][1]) {
916 buffer[j] = value + ranges[i][0];
917 break;
918 } else {
919 value -= (ranges[i][1] - ranges[i][0]);
920 }
921 }
922 }
Mike Reed2ed78202018-11-21 15:10:08 -0500923 switch (encoding) {
Ben Wagner51e15a62019-05-07 15:38:46 -0400924 case SkTextEncoding::kUTF8: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500925 size_t utf8len = 0;
926 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400927 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -0500928 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500929 char* ptr = (char*)array.append(utf8len);
930 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400931 ptr += SkUTF::ToUTF8(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -0500932 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500933 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400934 case SkTextEncoding::kUTF16: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500935 size_t utf16len = 0;
936 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400937 utf16len += SkUTF::ToUTF16(buffer[j]);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500938 }
939 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
940 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400941 ptr += SkUTF::ToUTF16(buffer[j], ptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500942 }
943 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400944 case SkTextEncoding::kUTF32:
Hal Canary671e4422017-02-27 13:36:38 -0500945 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -0500946 break;
Hal Canary671e4422017-02-27 13:36:38 -0500947 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -0500948 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -0500949 break;
Hal Canary671e4422017-02-27 13:36:38 -0500950 }
951 return array;
952}
953
Zepeng Huba7cbf72020-07-01 13:21:03 +0000954static std::string make_fuzz_string(Fuzz* fuzz) {
955 int len;
956 fuzz->nextRange(&len, 0, kMaxGlyphCount);
957 std::string str(len, 0);
958 for (int i = 0; i < len; i++) {
959 fuzz->next(&str[i]);
960 }
961 return str;
962}
963
Hal Canary5395c592017-03-08 16:52:18 -0500964static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
965 SkTextBlobBuilder textBlobBuilder;
966 int8_t runCount;
967 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
968 while (runCount-- > 0) {
Mike Reed2ed78202018-11-21 15:10:08 -0500969 SkFont font;
970 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
971 font.setEdging(make_fuzz_t<bool>(fuzz) ? SkFont::Edging::kAlias : SkFont::Edging::kAntiAlias);
972 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
973 int glyphCount = font.countText(text.begin(), SkToSizeT(text.count()), encoding);
Hal Canary5395c592017-03-08 16:52:18 -0500974 SkASSERT(glyphCount <= kMaxGlyphCount);
975 SkScalar x, y;
976 const SkTextBlobBuilder::RunBuffer* buffer;
977 uint8_t runType;
978 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
Hal Canaryfc97f222018-12-17 13:48:44 -0500979 const void* textPtr = text.begin();
980 size_t textLen = SkToSizeT(text.count());
Hal Canary5395c592017-03-08 16:52:18 -0500981 switch (runType) {
982 case 0:
983 fuzz->next(&x, &y);
984 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500985 buffer = &textBlobBuilder.allocRun(font, glyphCount, x, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500986 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500987 break;
988 case 1:
989 fuzz->next(&y);
990 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500991 buffer = &textBlobBuilder.allocRunPosH(font, glyphCount, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500992 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500993 fuzz->nextN(buffer->pos, glyphCount);
994 break;
995 case 2:
996 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500997 buffer = &textBlobBuilder.allocRunPos(font, glyphCount);
Hal Canaryfc97f222018-12-17 13:48:44 -0500998 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500999 fuzz->nextN(buffer->pos, glyphCount * 2);
1000 break;
1001 default:
1002 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001003 break;
Hal Canary5395c592017-03-08 16:52:18 -05001004 }
1005 }
1006 return textBlobBuilder.make();
1007}
1008
Hal Canary1e0138b2017-03-10 13:56:08 -05001009static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001010 if (!fuzz || !canvas || depth <= 0) {
1011 return;
1012 }
1013 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1014 unsigned N;
1015 fuzz->nextRange(&N, 0, 2000);
John Stiles13c9f662021-08-16 12:16:29 -04001016 for (unsigned loop = 0; loop < N; ++loop) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001017 if (fuzz->exhausted()) {
1018 return;
1019 }
1020 SkPaint paint;
Mike Reed358fcad2018-11-23 15:27:51 -05001021 SkFont font;
Hal Canary24ac42b2017-02-14 13:35:14 -05001022 unsigned drawCommand;
Zepeng Huba7cbf72020-07-01 13:21:03 +00001023 fuzz->nextRange(&drawCommand, 0, 62);
Hal Canary24ac42b2017-02-14 13:35:14 -05001024 switch (drawCommand) {
1025 case 0:
1026 canvas->flush();
1027 break;
1028 case 1:
1029 canvas->save();
1030 break;
1031 case 2: {
1032 SkRect bounds;
1033 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001034 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001035 canvas->saveLayer(&bounds, &paint);
1036 break;
1037 }
1038 case 3: {
1039 SkRect bounds;
1040 fuzz->next(&bounds);
1041 canvas->saveLayer(&bounds, nullptr);
1042 break;
1043 }
1044 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001045 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001046 canvas->saveLayer(nullptr, &paint);
1047 break;
1048 case 5:
1049 canvas->saveLayer(nullptr, nullptr);
1050 break;
1051 case 6: {
1052 uint8_t alpha;
1053 fuzz->next(&alpha);
1054 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1055 break;
1056 }
1057 case 7: {
1058 SkRect bounds;
1059 uint8_t alpha;
1060 fuzz->next(&bounds, &alpha);
1061 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1062 break;
1063 }
1064 case 8: {
1065 SkCanvas::SaveLayerRec saveLayerRec;
1066 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001067 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001068 fuzz->next(&bounds);
1069 saveLayerRec.fBounds = &bounds;
1070 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001071 if (make_fuzz_t<bool>(fuzz)) {
1072 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001073 saveLayerRec.fPaint = &paint;
1074 }
1075 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001076 if (make_fuzz_t<bool>(fuzz)) {
1077 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001078 saveLayerRec.fBackdrop = imageFilter.get();
1079 }
Hal Canary5395c592017-03-08 16:52:18 -05001080 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001081 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001082 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1083 // }
1084
Hal Canary24ac42b2017-02-14 13:35:14 -05001085 canvas->saveLayer(saveLayerRec);
1086 break;
1087 }
1088 case 9:
1089 canvas->restore();
1090 break;
1091 case 10: {
1092 int saveCount;
1093 fuzz->next(&saveCount);
1094 canvas->restoreToCount(saveCount);
1095 break;
1096 }
1097 case 11: {
1098 SkScalar x, y;
1099 fuzz->next(&x, &y);
1100 canvas->translate(x, y);
1101 break;
1102 }
1103 case 12: {
1104 SkScalar x, y;
1105 fuzz->next(&x, &y);
1106 canvas->scale(x, y);
1107 break;
1108 }
1109 case 13: {
1110 SkScalar v;
1111 fuzz->next(&v);
1112 canvas->rotate(v);
1113 break;
1114 }
1115 case 14: {
1116 SkScalar x, y, v;
1117 fuzz->next(&x, &y, &v);
1118 canvas->rotate(v, x, y);
1119 break;
1120 }
1121 case 15: {
1122 SkScalar x, y;
1123 fuzz->next(&x, &y);
1124 canvas->skew(x, y);
1125 break;
1126 }
1127 case 16: {
1128 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001129 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001130 canvas->concat(mat);
1131 break;
1132 }
1133 case 17: {
1134 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001135 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001136 canvas->setMatrix(mat);
1137 break;
1138 }
1139 case 18:
1140 canvas->resetMatrix();
1141 break;
1142 case 19: {
1143 SkRect r;
1144 int op;
1145 bool doAntiAlias;
1146 fuzz->next(&r, &doAntiAlias);
1147 fuzz->nextRange(&op, 0, 1);
1148 r.sort();
1149 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1150 break;
1151 }
1152 case 20: {
1153 SkRRect rr;
1154 int op;
1155 bool doAntiAlias;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001156 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001157 fuzz->next(&doAntiAlias);
1158 fuzz->nextRange(&op, 0, 1);
1159 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1160 break;
1161 }
1162 case 21: {
1163 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001164 FuzzNicePath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001165 int op;
1166 bool doAntiAlias;
1167 fuzz->next(&doAntiAlias);
1168 fuzz->nextRange(&op, 0, 1);
1169 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1170 break;
1171 }
1172 case 22: {
1173 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001174 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001175 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001176 fuzz->nextRange(&op, 0, 1);
1177 canvas->clipRegion(region, (SkClipOp)op);
1178 break;
1179 }
1180 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001181 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001182 canvas->drawPaint(paint);
1183 break;
1184 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001185 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001186 SkCanvas::PointMode pointMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001187 fuzz->nextRange(&pointMode,
Hal Canaryc8bebd42017-12-20 11:21:05 -05001188 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001189 size_t count;
1190 constexpr int kMaxCount = 30;
1191 fuzz->nextRange(&count, 0, kMaxCount);
1192 SkPoint pts[kMaxCount];
1193 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001194 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001195 break;
1196 }
1197 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001198 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001199 SkRect r;
1200 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001201 if (!r.isFinite()) {
1202 break;
1203 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001204 canvas->drawRect(r, paint);
1205 break;
1206 }
1207 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001208 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001209 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001210 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001211 canvas->drawRegion(region, paint);
1212 break;
1213 }
1214 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001215 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001216 SkRect r;
1217 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001218 if (!r.isFinite()) {
1219 break;
1220 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001221 canvas->drawOval(r, paint);
1222 break;
1223 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001224 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001225 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001226 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001227 SkRRect rr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001228 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001229 canvas->drawRRect(rr, paint);
1230 break;
1231 }
1232 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001233 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001234 SkRRect orr, irr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001235 FuzzNiceRRect(fuzz, &orr);
1236 FuzzNiceRRect(fuzz, &irr);
Hal Canary27bece82017-03-07 16:23:20 -05001237 if (orr.getBounds().contains(irr.getBounds())) {
1238 canvas->drawDRRect(orr, irr, paint);
1239 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001240 break;
1241 }
1242 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001243 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001244 SkRect r;
1245 SkScalar start, sweep;
1246 bool useCenter;
1247 fuzz->next(&r, &start, &sweep, &useCenter);
1248 canvas->drawArc(r, start, sweep, useCenter, paint);
1249 break;
1250 }
1251 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001252 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001253 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001254 FuzzNicePath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001255 canvas->drawPath(path, paint);
1256 break;
1257 }
1258 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001259 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001260 SkScalar left, top;
1261 bool usePaint;
1262 fuzz->next(&left, &top, &usePaint);
1263 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001264 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001265 }
Mike Reed34c56a52021-01-22 15:26:41 -05001266 canvas->drawImage(img.get(), left, top, SkSamplingOptions(),
1267 usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001268 break;
1269 }
1270 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001271 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001272 SkIRect src;
1273 SkRect dst;
1274 bool usePaint;
1275 fuzz->next(&src, &dst, &usePaint);
1276 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001277 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001278 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001279 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001280 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1281 : SkCanvas::kFast_SrcRectConstraint;
Mike Reed34c56a52021-01-22 15:26:41 -05001282 canvas->drawImageRect(img.get(), SkRect::Make(src), dst, SkSamplingOptions(),
1283 usePaint ? &paint : nullptr, constraint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001284 break;
1285 }
1286 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001287 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001288 SkIRect center;
1289 SkRect dst;
1290 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001291 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001292 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001293 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001294 }
Hal Canary0361d492017-03-15 12:58:15 -04001295 if (make_fuzz_t<bool>(fuzz)) {
1296 fuzz->next(&center);
1297 } else { // Make valid center, see SkLatticeIter::Valid().
1298 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1299 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1300 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1301 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1302 }
1303 fuzz->next(&dst);
Mike Reed99116302021-01-25 11:37:10 -05001304 canvas->drawImageNine(img.get(), center, dst, SkFilterMode::kNearest,
1305 usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001306 break;
1307 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001308 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001309 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001310 bool usePaint;
1311 SkRect dst;
1312 fuzz->next(&usePaint, &dst);
1313 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001314 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001315 }
1316 constexpr int kMax = 6;
1317 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001318 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001319 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1320 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1321 fuzz->nextN(xDivs, lattice.fXCount);
1322 fuzz->nextN(yDivs, lattice.fYCount);
Mike Reed99116302021-01-25 11:37:10 -05001323 canvas->drawImageLattice(img.get(), lattice, dst, SkFilterMode::kLinear,
1324 usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001325 break;
1326 }
1327 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001328 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed358fcad2018-11-23 15:27:51 -05001329 font = fuzz_font(fuzz);
1330 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001331 SkScalar x, y;
1332 fuzz->next(&x, &y);
Mike Reed358fcad2018-11-23 15:27:51 -05001333 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
1334 canvas->drawSimpleText(text.begin(), SkToSizeT(text.count()), encoding, x, y,
1335 font, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001336 break;
1337 }
1338 case 46: {
Mike Reed212e9062018-12-25 17:35:49 -05001339 // was drawPosText
Hal Canary24ac42b2017-02-14 13:35:14 -05001340 break;
1341 }
1342 case 47: {
Mike Reed212e9062018-12-25 17:35:49 -05001343 // was drawPosTextH
Hal Canary24ac42b2017-02-14 13:35:14 -05001344 break;
1345 }
1346 case 48: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001347 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001348 break;
1349 }
1350 case 49: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001351 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001352 break;
1353 }
1354 case 50: {
Mike Reed212e9062018-12-25 17:35:49 -05001355 // was drawTextRSXform
Hal Canary24ac42b2017-02-14 13:35:14 -05001356 break;
1357 }
1358 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001359 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001360 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001361 SkScalar x, y;
1362 fuzz->next(&x, &y);
1363 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001364 break;
1365 }
1366 case 52: {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001367 SkMatrix matrix;
Hal Canary24ac42b2017-02-14 13:35:14 -05001368 bool usePaint, useMatrix;
1369 fuzz->next(&usePaint, &useMatrix);
1370 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001371 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001372 }
1373 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001374 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -05001375 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001376 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001377 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1378 usePaint ? &paint : nullptr);
1379 break;
1380 }
1381 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001382 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001383 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001384 SkBlendMode blendMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001385 fuzz->nextRange(&vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
1386 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001387 constexpr int kMaxCount = 100;
1388 int vertexCount;
1389 SkPoint vertices[kMaxCount];
1390 SkPoint texs[kMaxCount];
1391 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001392 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1393 fuzz->nextN(vertices, vertexCount);
1394 bool useTexs, useColors;
1395 fuzz->next(&useTexs, &useColors);
1396 if (useTexs) {
1397 fuzz->nextN(texs, vertexCount);
1398 }
1399 if (useColors) {
1400 fuzz->nextN(colors, vertexCount);
1401 }
1402 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001403 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001404 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001405 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
John Stiles13c9f662021-08-16 12:16:29 -04001406 for (int index = 0; index < indexCount; ++index) {
1407 fuzz->nextRange(&indices[index], 0, vertexCount - 1);
Hal Canary68b9b572017-03-02 15:27:23 -05001408 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001409 }
Mike Reed887cdf12017-04-03 11:11:09 -04001410 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1411 useTexs ? texs : nullptr,
1412 useColors ? colors : nullptr,
1413 indexCount, indices),
1414 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001415 break;
1416 }
Zepeng Huba7cbf72020-07-01 13:21:03 +00001417 case 54: {
1418 SkColor color;
1419 SkBlendMode blendMode;
1420 fuzz->nextRange(&blendMode, 0, SkBlendMode::kSrcOver);
1421 fuzz->next(&color);
1422 canvas->drawColor(color, blendMode);
1423 break;
1424 }
1425 case 55: {
1426 SkColor4f color;
1427 SkBlendMode blendMode;
1428 float R, G, B, Alpha;
1429 fuzz->nextRange(&blendMode, 0, SkBlendMode::kSrcOver);
1430 fuzz->nextRange(&R, -1, 2);
1431 fuzz->nextRange(&G, -1, 2);
1432 fuzz->nextRange(&B, -1, 2);
1433 fuzz->nextRange(&Alpha, 0, 1);
1434 color = {R, G, B, Alpha};
1435 canvas->drawColor(color, blendMode);
1436 break;
1437 }
1438 case 56: {
1439 fuzz_paint(fuzz, &paint, depth - 1);
1440 SkPoint p0, p1;
1441 fuzz->next(&p0, &p1);
1442 canvas->drawLine(p0, p1, paint);
1443 break;
1444 }
1445 case 57: {
1446 fuzz_paint(fuzz, &paint, depth - 1);
1447 SkIRect r;
1448 fuzz->next(&r);
1449 canvas->drawIRect(r, paint);
1450 break;
1451 }
1452 case 58: {
1453 fuzz_paint(fuzz, &paint, depth - 1);
1454 SkScalar radius;
1455 SkPoint center;
1456 fuzz->next(&radius, &center);
1457 canvas->drawCircle(center, radius, paint);
1458 break;
1459 }
1460 case 59: {
1461 fuzz_paint(fuzz, &paint, depth - 1);
1462 SkRect oval;
1463 SkScalar startAngle, sweepAngle;
1464 bool useCenter;
1465 fuzz->next(&oval, &startAngle, &sweepAngle, &useCenter);
1466 canvas->drawArc(oval, startAngle, sweepAngle, useCenter, paint);
1467 break;
1468 }
1469 case 60: {
1470 fuzz_paint(fuzz, &paint, depth - 1);
1471 SkRect rect;
1472 SkScalar rx, ry;
1473 fuzz->next(&rect, &rx, &ry);
1474 canvas->drawRoundRect(rect, rx, ry, paint);
1475 break;
1476 }
1477 case 61: {
1478 fuzz_paint(fuzz, &paint, depth - 1);
1479 font = fuzz_font(fuzz);
1480 std::string str = make_fuzz_string(fuzz);
1481 SkScalar x, y;
1482 fuzz->next(&x, &y);
1483 canvas->drawString(str.c_str(), x, y, font, paint);
1484 break;
1485 }
1486 case 62: {
1487 fuzz_paint(fuzz, &paint, depth - 1);
1488 SkPoint cubics[12];
1489 SkColor colors[4];
1490 SkPoint texCoords[4];
1491 bool useTexCoords;
1492 fuzz->nextN(cubics, 12);
1493 fuzz->nextN(colors, 4);
1494 fuzz->next(&useTexCoords);
1495 if (useTexCoords) {
1496 fuzz->nextN(texCoords, 4);
1497 }
1498 SkBlendMode mode;
1499 fuzz->nextEnum(&mode, SkBlendMode::kLastMode);
1500 canvas->drawPatch(cubics, colors, useTexCoords ? texCoords : nullptr
1501 , mode, paint);
1502 break;
1503 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001504 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001505 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001506 break;
1507 }
1508 }
1509}
1510
Hal Canary1e0138b2017-03-10 13:56:08 -05001511static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001512 SkScalar w, h;
1513 fuzz->next(&w, &h);
1514 SkPictureRecorder pictureRecorder;
1515 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1516 return pictureRecorder.finishRecordingAsPicture();
1517}
1518
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001519DEF_FUZZ(NullCanvas, fuzz) {
1520 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001521}
1522
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001523constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001524
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001525DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001526 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001527 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001528 fuzz_canvas(fuzz, surface->getCanvas());
1529}
1530
Hal Canarye2924432017-12-01 11:46:26 -05001531DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1532 SkPictureRecorder recorder;
1533 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1534 SkIntToScalar(kCanvasSize.height())));
1535 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1536 if (!pic) { fuzz->signalBug(); }
1537 sk_sp<SkData> data = pic->serialize();
1538 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001539 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001540 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001541 if (!deserialized) { fuzz->signalBug(); }
1542 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1543 SkASSERT(surface && surface->getCanvas());
1544 surface->getCanvas()->drawPicture(deserialized);
1545}
1546
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001547DEF_FUZZ(ImageFilter, fuzz) {
1548 auto fil = make_fuzz_imageFilter(fuzz, 20);
1549
1550 SkPaint paint;
1551 paint.setImageFilter(fil);
1552 SkBitmap bitmap;
1553 SkCanvas canvas(bitmap);
1554 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1555}
1556
1557
1558//SkRandom _rand;
1559#define SK_ADD_RANDOM_BIT_FLIPS
1560
1561DEF_FUZZ(SerializedImageFilter, fuzz) {
Robert Phillipse5476bb2019-03-14 13:30:08 -04001562 SkBitmap bitmap;
1563 if (!bitmap.tryAllocN32Pixels(256, 256)) {
1564 SkDEBUGF("Could not allocate 256x256 bitmap in SerializedImageFilter");
1565 return;
1566 }
1567
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001568 auto filter = make_fuzz_imageFilter(fuzz, 20);
Robert Phillips66f6ef42018-09-14 11:58:40 -04001569 if (!filter) {
1570 return;
1571 }
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001572 auto data = filter->serialize();
1573 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1574 size_t len = data->size();
1575#ifdef SK_ADD_RANDOM_BIT_FLIPS
1576 unsigned char* p = const_cast<unsigned char*>(ptr);
1577 for (size_t i = 0; i < len; ++i, ++p) {
1578 uint8_t j;
1579 fuzz->nextRange(&j, 1, 250);
1580 if (j == 1) { // 0.4% of the time, flip a bit or byte
1581 uint8_t k;
1582 fuzz->nextRange(&k, 1, 10);
1583 if (k == 1) { // Then 10% of the time, change a whole byte
1584 uint8_t s;
1585 fuzz->nextRange(&s, 0, 2);
1586 switch(s) {
1587 case 0:
1588 *p ^= 0xFF; // Flip entire byte
1589 break;
1590 case 1:
1591 *p = 0xFF; // Set all bits to 1
1592 break;
1593 case 2:
1594 *p = 0x00; // Set all bits to 0
1595 break;
1596 }
1597 } else {
1598 uint8_t s;
1599 fuzz->nextRange(&s, 0, 7);
1600 *p ^= (1 << 7);
1601 }
1602 }
1603 }
1604#endif // SK_ADD_RANDOM_BIT_FLIPS
1605 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1606
1607 // uncomment below to write out a serialized image filter (to make corpus
1608 // for -t filter_fuzz)
1609 // SkString s("./serialized_filters/sf");
1610 // s.appendU32(_rand.nextU());
1611 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1612 // sk_fwrite(data->bytes(), data->size(), file);
1613 // sk_fclose(file);
1614
1615 SkPaint paint;
1616 paint.setImageFilter(deserializedFil);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001617
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001618 SkCanvas canvas(bitmap);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001619 canvas.saveLayer(SkRect::MakeWH(256, 256), &paint);
1620 canvas.restore();
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001621}
1622
Brian Salomonf4ba4ec2020-03-19 15:54:28 -04001623#ifdef SK_GL
Kevin Lubickfaef5142018-06-07 10:33:11 -04001624
Robert Phillips7b4e43c2020-07-01 16:59:17 -04001625static void dump_GPU_info(GrDirectContext* context) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001626 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->priv().getGpu())
Kevin Lubickfaef5142018-06-07 10:33:11 -04001627 ->glInterface();
1628 const GrGLubyte* output;
1629 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1630 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1631
1632 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1633 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1634
1635 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1636 SkDebugf("GL_VERSION %s\n", (const char*) output);
1637}
1638
Robert Phillips7b4e43c2020-07-01 16:59:17 -04001639static void fuzz_ganesh(Fuzz* fuzz, GrDirectContext* context) {
Hal Canary5aa91582017-03-21 11:11:44 -07001640 SkASSERT(context);
1641 auto surface = SkSurface::MakeRenderTarget(
1642 context,
1643 SkBudgeted::kNo,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001644 SkImageInfo::Make(kCanvasSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
Hal Canary5aa91582017-03-21 11:11:44 -07001645 SkASSERT(surface && surface->getCanvas());
1646 fuzz_canvas(fuzz, surface->getCanvas());
1647}
1648
Hal Canary44801ca2017-03-15 11:39:06 -04001649DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001650 sk_gpu_test::GrContextFactory f;
Robert Phillips7b4e43c2020-07-01 16:59:17 -04001651 auto context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001652 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001653 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001654 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001655 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001656 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001657 }
Hal Canary5aa91582017-03-21 11:11:44 -07001658 fuzz_ganesh(fuzz, context);
1659}
1660
Kevin Lubick27d42192018-04-03 12:30:32 -04001661DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001662 sk_gpu_test::GrContextFactory f;
1663 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1664}
Hal Canary44801ca2017-03-15 11:39:06 -04001665#endif
1666
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001667DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001668 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -04001669 auto doc = SkPDF::MakeDocument(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001670 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1671 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001672}
1673
1674// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1675DEF_FUZZ(_DumpCanvas, fuzz) {
Mike Klein8f4e2242019-03-20 11:59:00 -05001676 DebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001677 fuzz_canvas(fuzz, &debugCanvas);
1678 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1679 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05001680 SkDynamicMemoryWStream stream;
1681 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1682 writer.beginObject(); // root
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -05001683 debugCanvas.toJSON(writer, dataManager, nullCanvas.get());
Brian Osmand8a90f92019-01-28 13:41:19 -05001684 writer.endObject(); // root
1685 writer.flush();
1686 sk_sp<SkData> json = stream.detachAsData();
1687 fwrite(json->data(), json->size(), 1, stdout);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001688}
Zepeng Huba7cbf72020-07-01 13:21:03 +00001689
1690DEF_FUZZ(SVGCanvas, fuzz) {
1691 SkNullWStream stream;
1692 SkRect bounds = SkRect::MakeIWH(150, 150);
1693 std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &stream);
1694 fuzz_canvas(fuzz, canvas.get());
1695}