blob: 400a2f19a24cfdfb2b030d8242c173af591879ba [file] [log] [blame]
Hal Canary24ac42b2017-02-14 13:35:14 -05001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "fuzz/Fuzz.h"
9#include "fuzz/FuzzCommon.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050010
11// CORE
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkCanvas.h"
13#include "include/core/SkColorFilter.h"
14#include "include/core/SkFontMgr.h"
15#include "include/core/SkImageFilter.h"
16#include "include/core/SkMaskFilter.h"
17#include "include/core/SkPathEffect.h"
18#include "include/core/SkPictureRecorder.h"
19#include "include/core/SkPoint3.h"
20#include "include/core/SkRSXform.h"
21#include "include/core/SkRegion.h"
22#include "include/core/SkSurface.h"
23#include "include/core/SkTypeface.h"
24#include "include/docs/SkPDFDocument.h"
25#include "include/private/SkTo.h"
26#include "include/utils/SkNullCanvas.h"
27#include "src/core/SkOSFile.h"
28#include "src/core/SkPicturePriv.h"
29#include "tools/debugger/DebugCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050030
31// EFFECTS
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "include/core/SkTextBlob.h"
33#include "include/effects/Sk1DPathEffect.h"
34#include "include/effects/Sk2DPathEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "include/effects/SkBlurMaskFilter.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
Hal Canary44801ca2017-03-15 11:39:06 -040052#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "include/gpu/gl/GrGLFunctions.h"
54#include "src/gpu/GrContextPriv.h"
55#include "src/gpu/gl/GrGLGpu.h"
56#include "src/gpu/gl/GrGLUtil.h"
57#include "tools/gpu/GrContextFactory.h"
Hal Canary44801ca2017-03-15 11:39:06 -040058#endif
59
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050060// MISC
61
62#include <iostream>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040063#include <utility>
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050064
Mike Klein84836b72019-03-21 11:31:36 -050065static DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets.");
Kevin Lubickfaef5142018-06-07 10:33:11 -040066
Hal Canary24ac42b2017-02-14 13:35:14 -050067// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050068// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040069// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050070
Hal Canary1e0138b2017-03-10 13:56:08 -050071// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
72// In fact, all make_fuzz_foo() functions have this potential problem.
73// Use sequence points!
74template <typename T>
75inline T make_fuzz_t(Fuzz* fuzz) {
76 T t;
77 fuzz->next(&t);
78 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050079}
80
Hal Canary1e0138b2017-03-10 13:56:08 -050081static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -050082
Hal Canary1e0138b2017-03-10 13:56:08 -050083static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -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 }
202 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
203 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500204 bitmap = make_fuzz_bitmap(fuzz);
Kevin Lubick00587e32019-06-03 11:27:16 -0400205 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
206 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400207 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500208 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400209 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500210 }
Mike Reed50acf8f2019-04-08 13:20:23 -0400211 return bitmap.makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500212 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500213 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400214 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500215 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
216 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500217 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
218 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500219 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
220 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500221 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
222 shader2 = make_fuzz_shader(fuzz, depth - 1);
Kevin Lubick00587e32019-06-03 11:27:16 -0400223 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400224 return SkShaders::Blend(blendMode, std::move(shader1), std::move(shader2));
Hal Canaryb69c4b82017-03-08 11:02:40 -0500225 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500226 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500227 bool useTile;
228 SkRect tile;
Kevin Lubick00587e32019-06-03 11:27:16 -0400229 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
230 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400231 fuzz->next(&useMatrix, &useTile);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500232 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400233 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500234 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500235 if (useTile) {
236 fuzz->next(&tile);
237 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400238 return pic->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr, useTile ? &tile : nullptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500239 }
Hal Canary671e4422017-02-27 13:36:38 -0500240 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500241 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000242 // Deprecated SkGaussianEdgeShader
243 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500244 case 10: {
245 constexpr int kMaxColors = 12;
246 SkPoint pts[2];
247 SkColor colors[kMaxColors];
248 SkScalar pos[kMaxColors];
249 int colorCount;
250 bool usePos;
251 fuzz->nextN(pts, 2);
252 fuzz->nextRange(&colorCount, 2, kMaxColors);
253 fuzz->nextN(colors, colorCount);
Kevin Lubick00587e32019-06-03 11:27:16 -0400254 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400255 fuzz->next(&useMatrix, &usePos);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500256 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400257 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500258 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500259 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500260 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500261 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500262 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
263 tmX, 0, useMatrix ? &matrix : nullptr);
264 }
265 case 11: {
266 constexpr int kMaxColors = 12;
267 SkPoint center;
268 SkScalar radius;
269 int colorCount;
270 bool usePos;
271 SkColor colors[kMaxColors];
272 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400273 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400274 fuzz->next(&useMatrix, &usePos, &center, &radius);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500275 fuzz->nextRange(&colorCount, 2, kMaxColors);
276 fuzz->nextN(colors, colorCount);
277 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400278 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500279 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500280 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500281 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500282 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500283 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
284 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
285 }
286 case 12: {
287 constexpr int kMaxColors = 12;
288 SkPoint start, end;
289 SkScalar startRadius, endRadius;
290 int colorCount;
291 bool usePos;
292 SkColor colors[kMaxColors];
293 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400294 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400295 fuzz->next(&useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500296 fuzz->nextRange(&colorCount, 2, kMaxColors);
297 fuzz->nextN(colors, colorCount);
298 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400299 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500300 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500301 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500302 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500303 }
304 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
305 usePos ? pos : nullptr, colorCount, tmX, 0,
306 useMatrix ? &matrix : nullptr);
307 }
308 case 13: {
309 constexpr int kMaxColors = 12;
310 SkScalar cx, cy;
311 int colorCount;
312 bool usePos;
313 SkColor colors[kMaxColors];
314 SkScalar pos[kMaxColors];
315 fuzz->next(&cx, &cy, &useMatrix, &usePos);
316 fuzz->nextRange(&colorCount, 2, kMaxColors);
317 fuzz->nextN(colors, colorCount);
318 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400319 FuzzNiceMatrix(fuzz, &matrix);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500320 }
321 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500322 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500323 }
324 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
325 0, useMatrix ? &matrix : nullptr);
326 }
327 case 14: {
328 SkScalar baseFrequencyX, baseFrequencyY, seed;
329 int numOctaves;
330 SkISize tileSize;
331 bool useTileSize, turbulence;
332 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
333 if (useTileSize) {
334 fuzz->next(&tileSize);
335 }
336 fuzz->nextRange(&numOctaves, 2, 7);
337 if (turbulence) {
338 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
339 numOctaves, seed,
340 useTileSize ? &tileSize : nullptr);
341 } else {
342 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
343 numOctaves, seed,
344 useTileSize ? &tileSize : nullptr);
345 }
346 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500347 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500348 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500349 break;
350 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500351 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500352}
353
Hal Canary1e0138b2017-03-10 13:56:08 -0500354static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500355 if (depth <= 0) {
356 return nullptr;
357 }
358 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400359 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500360 switch (pathEffectType) {
361 case 0: {
362 return nullptr;
363 }
364 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500365 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
366 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500367 return SkPathEffect::MakeSum(std::move(first), std::move(second));
368 }
369 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500370 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
371 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500372 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
373 }
374 case 3: {
375 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400376 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500377 SkScalar advance, phase;
378 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500379 SkPath1DPathEffect::Style style;
Kevin Lubick00587e32019-06-03 11:27:16 -0400380 fuzz->nextEnum(&style, SkPath1DPathEffect::kLastEnum_Style);
Hal Canaryf7005202017-03-10 08:48:28 -0500381 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500382 }
383 case 4: {
384 SkScalar width;
385 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400386 fuzz->next(&width);
387 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500388 return SkLine2DPathEffect::Make(width, matrix);
389 }
390 case 5: {
391 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400392 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500393 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400394 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500395 return SkPath2DPathEffect::Make(matrix, path);
396 }
397 case 6: {
398 SkScalar radius;
399 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500400 return SkCornerPathEffect::Make(radius);
401 }
Mike Reed40e7e652017-07-22 22:12:59 -0400402 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500403 SkScalar phase;
404 fuzz->next(&phase);
405 SkScalar intervals[20];
406 int count;
407 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
408 fuzz->nextN(intervals, count);
409 return SkDashPathEffect::Make(intervals, count, phase);
410 }
Mike Reed40e7e652017-07-22 22:12:59 -0400411 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500412 SkScalar segLength, dev;
413 uint32_t seed;
414 fuzz->next(&segLength, &dev, &seed);
415 return SkDiscretePathEffect::Make(segLength, dev, seed);
416 }
417 default:
418 SkASSERT(false);
419 return nullptr;
420 }
421}
Hal Canary24ac42b2017-02-14 13:35:14 -0500422
Hal Canary1e0138b2017-03-10 13:56:08 -0500423static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500424 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400425 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500426 switch (maskfilterType) {
427 case 0:
428 return nullptr;
429 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500430 SkBlurStyle blurStyle;
Kevin Lubick00587e32019-06-03 11:27:16 -0400431 fuzz->nextEnum(&blurStyle, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500432 SkScalar sigma;
433 fuzz->next(&sigma);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400434 bool respectCTM;
435 fuzz->next(&respectCTM);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400436 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500437 }
Hal Canary5395c592017-03-08 16:52:18 -0500438 default:
439 SkASSERT(false);
440 return nullptr;
441 }
442}
Hal Canary24ac42b2017-02-14 13:35:14 -0500443
Hal Canary1e0138b2017-03-10 13:56:08 -0500444static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
445 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500446 return nullptr;
447 }
448 auto fontMugger = SkFontMgr::RefDefault();
449 SkASSERT(fontMugger);
450 int familyCount = fontMugger->countFamilies();
451 int i, j;
452 fuzz->nextRange(&i, 0, familyCount - 1);
453 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
454 int styleCount = family->count();
455 fuzz->nextRange(&j, 0, styleCount - 1);
456 return sk_sp<SkTypeface>(family->createTypeface(j));
457}
Hal Canary24ac42b2017-02-14 13:35:14 -0500458
Hal Canary1e0138b2017-03-10 13:56:08 -0500459static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500460
461static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
462 if (depth <= 0) {
463 return nullptr;
464 }
465 uint8_t imageFilterType;
466 fuzz->nextRange(&imageFilterType, 1, 6);
467 SkPoint3 p, q;
468 SkColor lightColor;
469 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
470 sk_sp<SkImageFilter> input;
Michael Ludwigef43f682019-08-01 16:38:46 -0400471 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500472 bool useCropRect;
473 fuzz->next(&useCropRect);
474 if (useCropRect) {
475 fuzz->next(&cropRect);
476 }
477 switch (imageFilterType) {
478 case 1:
479 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500480 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400481 return SkImageFilters::DistantLitDiffuse(p, lightColor, surfaceScale, k,
482 std::move(input),
483 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500484 case 2:
485 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500486 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400487 return SkImageFilters::PointLitDiffuse(p, lightColor, surfaceScale, k,
488 std::move(input),
489 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500490 case 3:
491 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500492 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400493 return SkImageFilters::SpotLitDiffuse(
Hal Canarye03c3e52017-03-09 11:33:35 -0500494 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
495 std::move(input), useCropRect ? &cropRect : nullptr);
496 case 4:
497 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500498 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400499 return SkImageFilters::DistantLitSpecular(p, lightColor, surfaceScale, k,
500 shininess, std::move(input),
501 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500502 case 5:
503 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500504 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400505 return SkImageFilters::PointLitSpecular(p, lightColor, surfaceScale, k,
506 shininess, std::move(input),
507 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500508 case 6:
509 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
510 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500511 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400512 return SkImageFilters::SpotLitSpecular(
Hal Canarye03c3e52017-03-09 11:33:35 -0500513 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
514 std::move(input), useCropRect ? &cropRect : nullptr);
515 default:
516 SkASSERT(false);
517 return nullptr;
518 }
519}
520
Hal Canary1e0138b2017-03-10 13:56:08 -0500521static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500522
Hal Canary1e0138b2017-03-10 13:56:08 -0500523static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500524 if (depth <= 0) {
525 return nullptr;
526 }
527 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500528 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500529 switch (imageFilterType) {
530 case 0:
531 return nullptr;
532 case 1: {
533 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500534 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500535 bool useCropRect;
536 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400537 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500538 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400539 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500540 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400541 return SkImageFilters::Blur(sigmaX, sigmaY, std::move(input),
542 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500543 }
544 case 2: {
545 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400546 FuzzNiceMatrix(fuzz, &matrix);
Hal Canarye03c3e52017-03-09 11:33:35 -0500547 SkFilterQuality quality;
Kevin Lubick00587e32019-06-03 11:27:16 -0400548 fuzz->nextEnum(&quality, SkFilterQuality::kLast_SkFilterQuality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500549 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400550 return SkImageFilters::MatrixTransform(matrix, quality, std::move(input));
Hal Canarye03c3e52017-03-09 11:33:35 -0500551 }
552 case 3: {
553 SkRegion region;
554 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500555 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500556 bool useCropRect;
557 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400558 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500559 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400560 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500561 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400562 return SkImageFilters::AlphaThreshold(region, innerMin, outerMax, std::move(input),
563 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500564 }
565 case 4: {
566 float k1, k2, k3, k4;
567 bool enforcePMColor;
568 bool useCropRect;
569 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500570 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
571 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400572 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500573 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400574 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500575 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400576 return SkImageFilters::Arithmetic(k1, k2, k3, k4, enforcePMColor,
577 std::move(background), std::move(foreground),
578 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500579 }
580 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500581 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
582 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500583 bool useCropRect;
Michael Ludwigef43f682019-08-01 16:38:46 -0400584 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500585 fuzz->next(&useCropRect);
586 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400587 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500588 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400589 return SkImageFilters::ColorFilter(std::move(cf), std::move(input),
590 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500591 }
592 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500593 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
594 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400595 return SkImageFilters::Compose(std::move(ifo), std::move(ifi));
Hal Canarye03c3e52017-03-09 11:33:35 -0500596 }
597 case 7: {
Michael Ludwigef43f682019-08-01 16:38:46 -0400598 SkColorChannel xChannelSelector, yChannelSelector;
599 fuzz->nextEnum(&xChannelSelector, SkColorChannel::kLastEnum);
600 fuzz->nextEnum(&yChannelSelector, SkColorChannel::kLastEnum);
Hal Canarye03c3e52017-03-09 11:33:35 -0500601 SkScalar scale;
602 bool useCropRect;
603 fuzz->next(&scale, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400604 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500605 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400606 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500607 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500608 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
609 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400610 return SkImageFilters::DisplacementMap(xChannelSelector, yChannelSelector, scale,
611 std::move(displacement), std::move(color),
612 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500613 }
614 case 8: {
615 SkScalar dx, dy, sigmaX, sigmaY;
616 SkColor color;
Michael Ludwigef43f682019-08-01 16:38:46 -0400617 bool shadowOnly, useCropRect;
618 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &shadowOnly, &useCropRect);
619 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500620 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400621 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500622 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500623 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400624 if (shadowOnly) {
625 return SkImageFilters::DropShadowOnly(dx, dy, sigmaX, sigmaY, color,
626 std::move(input),
627 useCropRect ? &cropRect : nullptr);
628 } else {
629 return SkImageFilters::DropShadow(dx, dy, sigmaX, sigmaY, color, std::move(input),
630 useCropRect ? &cropRect : nullptr);
631 }
Hal Canarye03c3e52017-03-09 11:33:35 -0500632 }
633 case 9:
Michael Ludwigef43f682019-08-01 16:38:46 -0400634 return SkImageFilters::Image(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500635 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500636 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500637 SkRect srcRect, dstRect;
638 SkFilterQuality filterQuality;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400639 fuzz->next(&srcRect, &dstRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400640 fuzz->nextEnum(&filterQuality, SkFilterQuality::kLast_SkFilterQuality);
Michael Ludwigef43f682019-08-01 16:38:46 -0400641 return SkImageFilters::Image(std::move(image), srcRect, dstRect, filterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500642 }
643 case 11:
644 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
645 case 12: {
646 SkRect srcRect;
647 SkScalar inset;
648 bool useCropRect;
Michael Ludwigef43f682019-08-01 16:38:46 -0400649 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500650 fuzz->next(&srcRect, &inset, &useCropRect);
651 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400652 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500653 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500654 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400655 return SkImageFilters::Magnifier(srcRect, inset, std::move(input),
656 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500657 }
658 case 13: {
659 constexpr int kMaxKernelSize = 5;
660 int32_t n, m;
661 fuzz->nextRange(&n, 1, kMaxKernelSize);
662 fuzz->nextRange(&m, 1, kMaxKernelSize);
663 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
664 fuzz->nextN(kernel, n * m);
665 int32_t offsetX, offsetY;
666 fuzz->nextRange(&offsetX, 0, n - 1);
667 fuzz->nextRange(&offsetY, 0, m - 1);
668 SkScalar gain, bias;
669 bool convolveAlpha, useCropRect;
670 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400671 SkTileMode tileMode;
672 fuzz->nextEnum(&tileMode, SkTileMode::kLastTileMode);
673 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500674 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400675 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500676 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500677 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400678 return SkImageFilters::MatrixConvolution(
Hal Canarye03c3e52017-03-09 11:33:35 -0500679 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
680 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
681 }
682 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500683 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
684 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500685 bool useCropRect;
686 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400687 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500688 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400689 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500690 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400691 return SkImageFilters::Merge(std::move(first), std::move(second),
692 useCropRect ? &cropRect : nullptr);
Mike Reed0bdaf052017-06-18 23:35:57 -0400693 }
694 case 15: {
695 constexpr int kMaxCount = 4;
696 sk_sp<SkImageFilter> ifs[kMaxCount];
697 int count;
698 fuzz->nextRange(&count, 1, kMaxCount);
699 for (int i = 0; i < count; ++i) {
700 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
701 }
702 bool useCropRect;
703 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400704 SkIRect cropRect;
Mike Reed0bdaf052017-06-18 23:35:57 -0400705 if (useCropRect) {
706 fuzz->next(&cropRect);
707 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400708 return SkImageFilters::Merge(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500709 }
710 case 16: {
711 int rx, ry;
712 fuzz->next(&rx, &ry);
713 bool useCropRect;
714 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400715 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500716 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400717 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500718 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500719 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400720 return SkImageFilters::Dilate(rx, ry, std::move(input),
721 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500722 }
723 case 17: {
724 int rx, ry;
725 fuzz->next(&rx, &ry);
726 bool useCropRect;
727 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400728 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500729 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400730 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500731 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500732 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400733 return SkImageFilters::Erode(rx, ry, std::move(input),
734 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500735 }
736 case 18: {
737 SkScalar dx, dy;
738 fuzz->next(&dx, &dy);
739 bool useCropRect;
740 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400741 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500742 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400743 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500744 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500745 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400746 return SkImageFilters::Offset(dx, dy, std::move(input),
747 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500748 }
749 case 19: {
750 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500751 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500752 bool useCropRect;
753 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400754 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500755 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400756 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500757 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400758 return SkImageFilters::Paint(paint, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500759 }
760 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500761 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400762 return SkImageFilters::Picture(std::move(picture));
Hal Canarye03c3e52017-03-09 11:33:35 -0500763 }
764 case 21: {
765 SkRect cropRect;
766 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500767 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400768 return SkImageFilters::Picture(std::move(picture), cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500769 }
770 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500771 SkRect src, dst;
772 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500773 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400774 return SkImageFilters::Tile(src, dst, std::move(input));
Hal Canarye03c3e52017-03-09 11:33:35 -0500775 }
Mike Reed77e487d2017-11-09 21:50:20 +0000776 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500777 SkBlendMode blendMode;
778 bool useCropRect;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400779 fuzz->next(&useCropRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400780 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Michael Ludwigef43f682019-08-01 16:38:46 -0400781 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500782 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400783 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500784 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500785 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
786 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400787 return SkImageFilters::Xfermode(blendMode, std::move(bg), std::move(fg),
788 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500789 }
790 default:
791 SkASSERT(false);
792 return nullptr;
793 }
794}
Hal Canary24ac42b2017-02-14 13:35:14 -0500795
Hal Canary1e0138b2017-03-10 13:56:08 -0500796static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500797 int w, h;
798 fuzz->nextRange(&w, 1, 1024);
799 fuzz->nextRange(&h, 1, 1024);
800 SkAutoTMalloc<SkPMColor> data(w * h);
801 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
802 int n = w * h;
803 for (int i = 0; i < n; ++i) {
804 SkColor c;
805 fuzz->next(&c);
806 data[i] = SkPreMultiplyColor(c);
807 }
808 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500809 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
810 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500811}
812
Hal Canary1e0138b2017-03-10 13:56:08 -0500813static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500814 SkBitmap bitmap;
815 int w, h;
816 fuzz->nextRange(&w, 1, 1024);
817 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500818 if (!bitmap.tryAllocN32Pixels(w, h)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400819 SkDEBUGF("Could not allocate pixels %d x %d", w, h);
Kevin Lubick1991f552018-02-27 10:59:10 -0500820 return bitmap;
821 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500822 for (int y = 0; y < h; ++y) {
823 for (int x = 0; x < w; ++x) {
824 SkColor c;
825 fuzz->next(&c);
826 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
827 }
828 }
829 return bitmap;
830}
831
Kevin Lubick00587e32019-06-03 11:27:16 -0400832template <typename T>
833static T make_fuzz_enum_range(Fuzz* fuzz, T maxv) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500834 T value;
Kevin Lubick00587e32019-06-03 11:27:16 -0400835 fuzz->nextEnum(&value, maxv);
Hal Canary1e0138b2017-03-10 13:56:08 -0500836 return value;
837}
838
839static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500840 if (!fuzz || !paint || depth <= 0) {
841 return;
842 }
843
Hal Canary1e0138b2017-03-10 13:56:08 -0500844 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
845 paint->setDither( make_fuzz_t<bool>(fuzz));
846 paint->setColor( make_fuzz_t<SkColor>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400847 paint->setBlendMode( make_fuzz_enum_range<SkBlendMode>(fuzz, SkBlendMode::kLastMode));
848 paint->setFilterQuality(make_fuzz_enum_range<SkFilterQuality>(fuzz, kLast_SkFilterQuality));
849 paint->setStyle( make_fuzz_enum_range<SkPaint::Style>(fuzz,
850 SkPaint::Style::kStrokeAndFill_Style));
Hal Canary1e0138b2017-03-10 13:56:08 -0500851 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
852 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
853 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
854 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
855 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500856
857 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500858 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
859 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400860 paint->setStrokeCap( make_fuzz_enum_range<SkPaint::Cap>(fuzz, SkPaint::kLast_Cap));
861 paint->setStrokeJoin( make_fuzz_enum_range<SkPaint::Join>(fuzz, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500862 }
863}
864
Mike Reed358fcad2018-11-23 15:27:51 -0500865static SkFont fuzz_font(Fuzz* fuzz) {
866 SkFont font;
867 font.setTypeface( make_fuzz_typeface(fuzz));
868 font.setSize( make_fuzz_t<SkScalar>(fuzz));
869 font.setScaleX( make_fuzz_t<SkScalar>(fuzz));
870 font.setSkewX( make_fuzz_t<SkScalar>(fuzz));
871 font.setLinearMetrics( make_fuzz_t<bool>(fuzz));
872 font.setSubpixel( make_fuzz_t<bool>(fuzz));
873 font.setEmbeddedBitmaps( make_fuzz_t<bool>(fuzz));
874 font.setForceAutoHinting( make_fuzz_t<bool>(fuzz));
875 font.setEmbolden( make_fuzz_t<bool>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400876 font.setHinting( make_fuzz_enum_range<SkFontHinting>(fuzz, SkFontHinting::kFull));
877 font.setEdging( make_fuzz_enum_range<SkFont::Edging>(fuzz,
878 SkFont::Edging::kSubpixelAntiAlias));
Mike Reed358fcad2018-11-23 15:27:51 -0500879 return font;
Hal Canary5395c592017-03-08 16:52:18 -0500880}
881
Mike Reed2ed78202018-11-21 15:10:08 -0500882static SkTextEncoding fuzz_paint_text_encoding(Fuzz* fuzz) {
Kevin Lubick00587e32019-06-03 11:27:16 -0400883 return make_fuzz_enum_range<SkTextEncoding>(fuzz, SkTextEncoding::kUTF32);
Hal Canary24ac42b2017-02-14 13:35:14 -0500884}
885
Hal Canary5395c592017-03-08 16:52:18 -0500886constexpr int kMaxGlyphCount = 30;
887
Mike Reed2ed78202018-11-21 15:10:08 -0500888static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkFont& font, SkTextEncoding encoding) {
Hal Canary671e4422017-02-27 13:36:38 -0500889 SkTDArray<uint8_t> array;
Ben Wagner51e15a62019-05-07 15:38:46 -0400890 if (SkTextEncoding::kGlyphID == encoding) {
Herb Derby087fad72019-01-22 14:45:16 -0500891 int glyphRange = font.getTypefaceOrDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500892 if (glyphRange == 0) {
893 // Some fuzzing environments have no fonts, so empty array is the best
894 // we can do.
895 return array;
896 }
Hal Canary671e4422017-02-27 13:36:38 -0500897 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500898 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500899 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
900 for (int i = 0; i < glyphCount; ++i) {
901 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
902 }
903 return array;
904 }
905 static const SkUnichar ranges[][2] = {
906 {0x0020, 0x007F},
907 {0x00A1, 0x0250},
908 {0x0400, 0x0500},
909 };
910 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500911 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500912 count += (ranges[i][1] - ranges[i][0]);
913 }
Hal Canary5395c592017-03-08 16:52:18 -0500914 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500915 SkUnichar buffer[kMaxLength];
916 int length;
917 fuzz->nextRange(&length, 1, kMaxLength);
918 for (int j = 0; j < length; ++j) {
919 int32_t value;
920 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500921 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500922 if (value + ranges[i][0] < ranges[i][1]) {
923 buffer[j] = value + ranges[i][0];
924 break;
925 } else {
926 value -= (ranges[i][1] - ranges[i][0]);
927 }
928 }
929 }
Mike Reed2ed78202018-11-21 15:10:08 -0500930 switch (encoding) {
Ben Wagner51e15a62019-05-07 15:38:46 -0400931 case SkTextEncoding::kUTF8: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500932 size_t utf8len = 0;
933 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400934 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -0500935 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500936 char* ptr = (char*)array.append(utf8len);
937 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400938 ptr += SkUTF::ToUTF8(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -0500939 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500940 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400941 case SkTextEncoding::kUTF16: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500942 size_t utf16len = 0;
943 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400944 utf16len += SkUTF::ToUTF16(buffer[j]);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500945 }
946 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
947 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400948 ptr += SkUTF::ToUTF16(buffer[j], ptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500949 }
950 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400951 case SkTextEncoding::kUTF32:
Hal Canary671e4422017-02-27 13:36:38 -0500952 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -0500953 break;
Hal Canary671e4422017-02-27 13:36:38 -0500954 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -0500955 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -0500956 break;
Hal Canary671e4422017-02-27 13:36:38 -0500957 }
958 return array;
959}
960
Hal Canary5395c592017-03-08 16:52:18 -0500961static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
962 SkTextBlobBuilder textBlobBuilder;
963 int8_t runCount;
964 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
965 while (runCount-- > 0) {
Mike Reed2ed78202018-11-21 15:10:08 -0500966 SkFont font;
967 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
968 font.setEdging(make_fuzz_t<bool>(fuzz) ? SkFont::Edging::kAlias : SkFont::Edging::kAntiAlias);
969 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
970 int glyphCount = font.countText(text.begin(), SkToSizeT(text.count()), encoding);
Hal Canary5395c592017-03-08 16:52:18 -0500971 SkASSERT(glyphCount <= kMaxGlyphCount);
972 SkScalar x, y;
973 const SkTextBlobBuilder::RunBuffer* buffer;
974 uint8_t runType;
975 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
Hal Canaryfc97f222018-12-17 13:48:44 -0500976 const void* textPtr = text.begin();
977 size_t textLen = SkToSizeT(text.count());
Hal Canary5395c592017-03-08 16:52:18 -0500978 switch (runType) {
979 case 0:
980 fuzz->next(&x, &y);
981 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500982 buffer = &textBlobBuilder.allocRun(font, glyphCount, x, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500983 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500984 break;
985 case 1:
986 fuzz->next(&y);
987 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500988 buffer = &textBlobBuilder.allocRunPosH(font, glyphCount, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500989 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500990 fuzz->nextN(buffer->pos, glyphCount);
991 break;
992 case 2:
993 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500994 buffer = &textBlobBuilder.allocRunPos(font, glyphCount);
Hal Canaryfc97f222018-12-17 13:48:44 -0500995 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500996 fuzz->nextN(buffer->pos, glyphCount * 2);
997 break;
998 default:
999 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001000 break;
Hal Canary5395c592017-03-08 16:52:18 -05001001 }
1002 }
1003 return textBlobBuilder.make();
1004}
1005
Hal Canary1e0138b2017-03-10 13:56:08 -05001006static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001007 if (!fuzz || !canvas || depth <= 0) {
1008 return;
1009 }
1010 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1011 unsigned N;
1012 fuzz->nextRange(&N, 0, 2000);
1013 for (unsigned i = 0; i < N; ++i) {
1014 if (fuzz->exhausted()) {
1015 return;
1016 }
1017 SkPaint paint;
Mike Reed358fcad2018-11-23 15:27:51 -05001018 SkFont font;
Hal Canary24ac42b2017-02-14 13:35:14 -05001019 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001020 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001021 switch (drawCommand) {
1022 case 0:
1023 canvas->flush();
1024 break;
1025 case 1:
1026 canvas->save();
1027 break;
1028 case 2: {
1029 SkRect bounds;
1030 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001031 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001032 canvas->saveLayer(&bounds, &paint);
1033 break;
1034 }
1035 case 3: {
1036 SkRect bounds;
1037 fuzz->next(&bounds);
1038 canvas->saveLayer(&bounds, nullptr);
1039 break;
1040 }
1041 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001042 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001043 canvas->saveLayer(nullptr, &paint);
1044 break;
1045 case 5:
1046 canvas->saveLayer(nullptr, nullptr);
1047 break;
1048 case 6: {
1049 uint8_t alpha;
1050 fuzz->next(&alpha);
1051 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1052 break;
1053 }
1054 case 7: {
1055 SkRect bounds;
1056 uint8_t alpha;
1057 fuzz->next(&bounds, &alpha);
1058 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1059 break;
1060 }
1061 case 8: {
1062 SkCanvas::SaveLayerRec saveLayerRec;
1063 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001064 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001065 fuzz->next(&bounds);
1066 saveLayerRec.fBounds = &bounds;
1067 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001068 if (make_fuzz_t<bool>(fuzz)) {
1069 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001070 saveLayerRec.fPaint = &paint;
1071 }
1072 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001073 if (make_fuzz_t<bool>(fuzz)) {
1074 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001075 saveLayerRec.fBackdrop = imageFilter.get();
1076 }
Hal Canary5395c592017-03-08 16:52:18 -05001077 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001078 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001079 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1080 // }
1081
Hal Canary24ac42b2017-02-14 13:35:14 -05001082 canvas->saveLayer(saveLayerRec);
1083 break;
1084 }
1085 case 9:
1086 canvas->restore();
1087 break;
1088 case 10: {
1089 int saveCount;
1090 fuzz->next(&saveCount);
1091 canvas->restoreToCount(saveCount);
1092 break;
1093 }
1094 case 11: {
1095 SkScalar x, y;
1096 fuzz->next(&x, &y);
1097 canvas->translate(x, y);
1098 break;
1099 }
1100 case 12: {
1101 SkScalar x, y;
1102 fuzz->next(&x, &y);
1103 canvas->scale(x, y);
1104 break;
1105 }
1106 case 13: {
1107 SkScalar v;
1108 fuzz->next(&v);
1109 canvas->rotate(v);
1110 break;
1111 }
1112 case 14: {
1113 SkScalar x, y, v;
1114 fuzz->next(&x, &y, &v);
1115 canvas->rotate(v, x, y);
1116 break;
1117 }
1118 case 15: {
1119 SkScalar x, y;
1120 fuzz->next(&x, &y);
1121 canvas->skew(x, y);
1122 break;
1123 }
1124 case 16: {
1125 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001126 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001127 canvas->concat(mat);
1128 break;
1129 }
1130 case 17: {
1131 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001132 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001133 canvas->setMatrix(mat);
1134 break;
1135 }
1136 case 18:
1137 canvas->resetMatrix();
1138 break;
1139 case 19: {
1140 SkRect r;
1141 int op;
1142 bool doAntiAlias;
1143 fuzz->next(&r, &doAntiAlias);
1144 fuzz->nextRange(&op, 0, 1);
1145 r.sort();
1146 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1147 break;
1148 }
1149 case 20: {
1150 SkRRect rr;
1151 int op;
1152 bool doAntiAlias;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001153 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001154 fuzz->next(&doAntiAlias);
1155 fuzz->nextRange(&op, 0, 1);
1156 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1157 break;
1158 }
1159 case 21: {
1160 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001161 FuzzNicePath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001162 int op;
1163 bool doAntiAlias;
1164 fuzz->next(&doAntiAlias);
1165 fuzz->nextRange(&op, 0, 1);
1166 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1167 break;
1168 }
1169 case 22: {
1170 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001171 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001172 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001173 fuzz->nextRange(&op, 0, 1);
1174 canvas->clipRegion(region, (SkClipOp)op);
1175 break;
1176 }
1177 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001178 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001179 canvas->drawPaint(paint);
1180 break;
1181 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001182 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001183 SkCanvas::PointMode pointMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001184 fuzz->nextRange(&pointMode,
Hal Canaryc8bebd42017-12-20 11:21:05 -05001185 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001186 size_t count;
1187 constexpr int kMaxCount = 30;
1188 fuzz->nextRange(&count, 0, kMaxCount);
1189 SkPoint pts[kMaxCount];
1190 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001191 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001192 break;
1193 }
1194 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001195 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001196 SkRect r;
1197 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001198 if (!r.isFinite()) {
1199 break;
1200 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001201 canvas->drawRect(r, paint);
1202 break;
1203 }
1204 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001205 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001206 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001207 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001208 canvas->drawRegion(region, paint);
1209 break;
1210 }
1211 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001212 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001213 SkRect r;
1214 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001215 if (!r.isFinite()) {
1216 break;
1217 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001218 canvas->drawOval(r, paint);
1219 break;
1220 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001221 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001222 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001223 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001224 SkRRect rr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001225 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001226 canvas->drawRRect(rr, paint);
1227 break;
1228 }
1229 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001230 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001231 SkRRect orr, irr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001232 FuzzNiceRRect(fuzz, &orr);
1233 FuzzNiceRRect(fuzz, &irr);
Hal Canary27bece82017-03-07 16:23:20 -05001234 if (orr.getBounds().contains(irr.getBounds())) {
1235 canvas->drawDRRect(orr, irr, paint);
1236 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001237 break;
1238 }
1239 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001240 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001241 SkRect r;
1242 SkScalar start, sweep;
1243 bool useCenter;
1244 fuzz->next(&r, &start, &sweep, &useCenter);
1245 canvas->drawArc(r, start, sweep, useCenter, paint);
1246 break;
1247 }
1248 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001249 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001250 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001251 FuzzNicePath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001252 canvas->drawPath(path, paint);
1253 break;
1254 }
1255 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001256 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001257 SkScalar left, top;
1258 bool usePaint;
1259 fuzz->next(&left, &top, &usePaint);
1260 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001261 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001262 }
1263 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1264 break;
1265 }
1266 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001267 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001268 SkRect src, dst;
1269 bool usePaint;
1270 fuzz->next(&src, &dst, &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 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001274 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001275 break;
1276 }
1277 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001278 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001279 SkIRect src;
1280 SkRect dst;
1281 bool usePaint;
1282 fuzz->next(&src, &dst, &usePaint);
1283 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001284 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001285 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001286 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001287 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1288 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001289 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1290 break;
1291 }
1292 case 36: {
1293 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001294 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001295 SkRect dst;
1296 fuzz->next(&dst, &usePaint);
1297 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001298 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001299 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001300 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001301 break;
1302 }
1303 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001304 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001305 SkIRect center;
1306 SkRect dst;
1307 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001308 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001309 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001310 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001311 }
Hal Canary0361d492017-03-15 12:58:15 -04001312 if (make_fuzz_t<bool>(fuzz)) {
1313 fuzz->next(&center);
1314 } else { // Make valid center, see SkLatticeIter::Valid().
1315 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1316 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1317 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1318 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1319 }
1320 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001321 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1322 break;
1323 }
1324 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001325 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001326 SkScalar left, top;
1327 bool usePaint;
1328 fuzz->next(&left, &top, &usePaint);
1329 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001330 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001331 }
1332 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1333 break;
1334 }
1335 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001336 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001337 SkRect src, dst;
1338 bool usePaint;
1339 fuzz->next(&src, &dst, &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 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001343 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001344 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1345 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001346 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1347 break;
1348 }
1349 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001350 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001351 SkIRect src;
1352 SkRect dst;
1353 bool usePaint;
1354 fuzz->next(&src, &dst, &usePaint);
1355 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001356 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001357 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001358 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001359 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1360 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001361 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1362 break;
1363 }
1364 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001365 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001366 SkRect dst;
1367 bool usePaint;
1368 fuzz->next(&dst, &usePaint);
1369 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001370 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001371 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001372 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001373 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1374 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001375 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1376 break;
1377 }
1378 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001379 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001380 SkIRect center;
1381 SkRect dst;
1382 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001383 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001384 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001385 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001386 }
Hal Canary0361d492017-03-15 12:58:15 -04001387 if (make_fuzz_t<bool>(fuzz)) {
1388 fuzz->next(&center);
1389 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001390 if (img.width() == 0 || img.height() == 0) {
1391 // bitmap may not have had its pixels initialized.
1392 break;
1393 }
Hal Canary0361d492017-03-15 12:58:15 -04001394 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1395 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1396 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1397 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1398 }
1399 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001400 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1401 break;
1402 }
1403 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001404 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001405 bool usePaint;
1406 SkRect dst;
1407 fuzz->next(&usePaint, &dst);
1408 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001409 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001410 }
1411 constexpr int kMax = 6;
1412 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001413 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001414 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1415 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1416 fuzz->nextN(xDivs, lattice.fXCount);
1417 fuzz->nextN(yDivs, lattice.fYCount);
1418 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1419 break;
1420 }
1421 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001422 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001423 bool usePaint;
1424 SkRect dst;
1425 fuzz->next(&usePaint, &dst);
1426 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001427 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001428 }
1429 constexpr int kMax = 6;
1430 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001431 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001432 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1433 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1434 fuzz->nextN(xDivs, lattice.fXCount);
1435 fuzz->nextN(yDivs, lattice.fYCount);
1436 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1437 break;
1438 }
1439 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001440 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed358fcad2018-11-23 15:27:51 -05001441 font = fuzz_font(fuzz);
1442 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001443 SkScalar x, y;
1444 fuzz->next(&x, &y);
Mike Reed358fcad2018-11-23 15:27:51 -05001445 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
1446 canvas->drawSimpleText(text.begin(), SkToSizeT(text.count()), encoding, x, y,
1447 font, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001448 break;
1449 }
1450 case 46: {
Mike Reed212e9062018-12-25 17:35:49 -05001451 // was drawPosText
Hal Canary24ac42b2017-02-14 13:35:14 -05001452 break;
1453 }
1454 case 47: {
Mike Reed212e9062018-12-25 17:35:49 -05001455 // was drawPosTextH
Hal Canary24ac42b2017-02-14 13:35:14 -05001456 break;
1457 }
1458 case 48: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001459 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001460 break;
1461 }
1462 case 49: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001463 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001464 break;
1465 }
1466 case 50: {
Mike Reed212e9062018-12-25 17:35:49 -05001467 // was drawTextRSXform
Hal Canary24ac42b2017-02-14 13:35:14 -05001468 break;
1469 }
1470 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001471 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001472 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001473 SkScalar x, y;
1474 fuzz->next(&x, &y);
1475 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001476 break;
1477 }
1478 case 52: {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001479 SkMatrix matrix;
Hal Canary24ac42b2017-02-14 13:35:14 -05001480 bool usePaint, useMatrix;
1481 fuzz->next(&usePaint, &useMatrix);
1482 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001483 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001484 }
1485 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001486 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -05001487 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001488 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001489 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1490 usePaint ? &paint : nullptr);
1491 break;
1492 }
1493 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001494 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001495 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001496 SkBlendMode blendMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001497 fuzz->nextRange(&vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
1498 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001499 constexpr int kMaxCount = 100;
1500 int vertexCount;
1501 SkPoint vertices[kMaxCount];
1502 SkPoint texs[kMaxCount];
1503 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001504 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1505 fuzz->nextN(vertices, vertexCount);
1506 bool useTexs, useColors;
1507 fuzz->next(&useTexs, &useColors);
1508 if (useTexs) {
1509 fuzz->nextN(texs, vertexCount);
1510 }
1511 if (useColors) {
1512 fuzz->nextN(colors, vertexCount);
1513 }
1514 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001515 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001516 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001517 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1518 for (int i = 0; i < indexCount; ++i) {
1519 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1520 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001521 }
Mike Reed887cdf12017-04-03 11:11:09 -04001522 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1523 useTexs ? texs : nullptr,
1524 useColors ? colors : nullptr,
1525 indexCount, indices),
1526 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001527 break;
1528 }
1529 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001530 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001531 break;
1532 }
1533 }
1534}
1535
Hal Canary1e0138b2017-03-10 13:56:08 -05001536static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001537 SkScalar w, h;
1538 fuzz->next(&w, &h);
1539 SkPictureRecorder pictureRecorder;
1540 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1541 return pictureRecorder.finishRecordingAsPicture();
1542}
1543
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001544DEF_FUZZ(NullCanvas, fuzz) {
1545 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001546}
1547
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001548constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001549
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001550DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001551 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001552 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001553 fuzz_canvas(fuzz, surface->getCanvas());
1554}
1555
Hal Canarye2924432017-12-01 11:46:26 -05001556DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1557 SkPictureRecorder recorder;
1558 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1559 SkIntToScalar(kCanvasSize.height())));
1560 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1561 if (!pic) { fuzz->signalBug(); }
1562 sk_sp<SkData> data = pic->serialize();
1563 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001564 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001565 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001566 if (!deserialized) { fuzz->signalBug(); }
1567 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1568 SkASSERT(surface && surface->getCanvas());
1569 surface->getCanvas()->drawPicture(deserialized);
1570}
1571
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001572DEF_FUZZ(ImageFilter, fuzz) {
1573 auto fil = make_fuzz_imageFilter(fuzz, 20);
1574
1575 SkPaint paint;
1576 paint.setImageFilter(fil);
1577 SkBitmap bitmap;
1578 SkCanvas canvas(bitmap);
1579 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1580}
1581
1582
1583//SkRandom _rand;
1584#define SK_ADD_RANDOM_BIT_FLIPS
1585
1586DEF_FUZZ(SerializedImageFilter, fuzz) {
Robert Phillipse5476bb2019-03-14 13:30:08 -04001587 SkBitmap bitmap;
1588 if (!bitmap.tryAllocN32Pixels(256, 256)) {
1589 SkDEBUGF("Could not allocate 256x256 bitmap in SerializedImageFilter");
1590 return;
1591 }
1592
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001593 auto filter = make_fuzz_imageFilter(fuzz, 20);
Robert Phillips66f6ef42018-09-14 11:58:40 -04001594 if (!filter) {
1595 return;
1596 }
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001597 auto data = filter->serialize();
1598 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1599 size_t len = data->size();
1600#ifdef SK_ADD_RANDOM_BIT_FLIPS
1601 unsigned char* p = const_cast<unsigned char*>(ptr);
1602 for (size_t i = 0; i < len; ++i, ++p) {
1603 uint8_t j;
1604 fuzz->nextRange(&j, 1, 250);
1605 if (j == 1) { // 0.4% of the time, flip a bit or byte
1606 uint8_t k;
1607 fuzz->nextRange(&k, 1, 10);
1608 if (k == 1) { // Then 10% of the time, change a whole byte
1609 uint8_t s;
1610 fuzz->nextRange(&s, 0, 2);
1611 switch(s) {
1612 case 0:
1613 *p ^= 0xFF; // Flip entire byte
1614 break;
1615 case 1:
1616 *p = 0xFF; // Set all bits to 1
1617 break;
1618 case 2:
1619 *p = 0x00; // Set all bits to 0
1620 break;
1621 }
1622 } else {
1623 uint8_t s;
1624 fuzz->nextRange(&s, 0, 7);
1625 *p ^= (1 << 7);
1626 }
1627 }
1628 }
1629#endif // SK_ADD_RANDOM_BIT_FLIPS
1630 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1631
1632 // uncomment below to write out a serialized image filter (to make corpus
1633 // for -t filter_fuzz)
1634 // SkString s("./serialized_filters/sf");
1635 // s.appendU32(_rand.nextU());
1636 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1637 // sk_fwrite(data->bytes(), data->size(), file);
1638 // sk_fclose(file);
1639
1640 SkPaint paint;
1641 paint.setImageFilter(deserializedFil);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001642
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001643 SkCanvas canvas(bitmap);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001644 canvas.saveLayer(SkRect::MakeWH(256, 256), &paint);
1645 canvas.restore();
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001646}
1647
Hal Canary44801ca2017-03-15 11:39:06 -04001648#if SK_SUPPORT_GPU
Kevin Lubickfaef5142018-06-07 10:33:11 -04001649
1650static void dump_GPU_info(GrContext* context) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001651 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->priv().getGpu())
Kevin Lubickfaef5142018-06-07 10:33:11 -04001652 ->glInterface();
1653 const GrGLubyte* output;
1654 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1655 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1656
1657 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1658 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1659
1660 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1661 SkDebugf("GL_VERSION %s\n", (const char*) output);
1662}
1663
Hal Canary5aa91582017-03-21 11:11:44 -07001664static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1665 SkASSERT(context);
1666 auto surface = SkSurface::MakeRenderTarget(
1667 context,
1668 SkBudgeted::kNo,
1669 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1670 SkASSERT(surface && surface->getCanvas());
1671 fuzz_canvas(fuzz, surface->getCanvas());
1672}
1673
Hal Canary44801ca2017-03-15 11:39:06 -04001674DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001675 sk_gpu_test::GrContextFactory f;
1676 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001677 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001678 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001679 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001680 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001681 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001682 }
Hal Canary5aa91582017-03-21 11:11:44 -07001683 fuzz_ganesh(fuzz, context);
1684}
1685
Kevin Lubick27d42192018-04-03 12:30:32 -04001686DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001687 sk_gpu_test::GrContextFactory f;
1688 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1689}
Hal Canary44801ca2017-03-15 11:39:06 -04001690#endif
1691
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001692DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001693 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -04001694 auto doc = SkPDF::MakeDocument(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001695 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1696 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001697}
1698
1699// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1700DEF_FUZZ(_DumpCanvas, fuzz) {
Mike Klein8f4e2242019-03-20 11:59:00 -05001701 DebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001702 fuzz_canvas(fuzz, &debugCanvas);
1703 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1704 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05001705 SkDynamicMemoryWStream stream;
1706 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1707 writer.beginObject(); // root
1708 debugCanvas.toJSON(writer, dataManager, debugCanvas.getSize(), nullCanvas.get());
1709 writer.endObject(); // root
1710 writer.flush();
1711 sk_sp<SkData> json = stream.detachAsData();
1712 fwrite(json->data(), json->size(), 1, stdout);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001713}