blob: 53991b104923037d50a85b1e14e845ede7436c89 [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/SkColorMatrixFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "include/effects/SkCornerPathEffect.h"
37#include "include/effects/SkDashPathEffect.h"
38#include "include/effects/SkDiscretePathEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "include/effects/SkGradientShader.h"
40#include "include/effects/SkHighContrastFilter.h"
Michael Ludwigef43f682019-08-01 16:38:46 -040041#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "include/effects/SkLumaColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "include/effects/SkPerlinNoiseShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "include/effects/SkTableColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/core/SkReadBuffer.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050046
47// SRC
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/utils/SkUTF.h"
49#include "tools/flags/CommandLineFlags.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050050
Hal Canary44801ca2017-03-15 11:39:06 -040051#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "include/gpu/gl/GrGLFunctions.h"
53#include "src/gpu/GrContextPriv.h"
54#include "src/gpu/gl/GrGLGpu.h"
55#include "src/gpu/gl/GrGLUtil.h"
56#include "tools/gpu/GrContextFactory.h"
Hal Canary44801ca2017-03-15 11:39:06 -040057#endif
58
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050059// MISC
60
61#include <iostream>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040062#include <utility>
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050063
Mike Klein84836b72019-03-21 11:31:36 -050064static DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets.");
Kevin Lubickfaef5142018-06-07 10:33:11 -040065
Hal Canary24ac42b2017-02-14 13:35:14 -050066// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050067// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040068// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050069
Hal Canary1e0138b2017-03-10 13:56:08 -050070// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
71// In fact, all make_fuzz_foo() functions have this potential problem.
72// Use sequence points!
73template <typename T>
74inline T make_fuzz_t(Fuzz* fuzz) {
75 T t;
76 fuzz->next(&t);
77 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050078}
79
Hal Canary1e0138b2017-03-10 13:56:08 -050080static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -050081
Hal Canary1e0138b2017-03-10 13:56:08 -050082static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -050083
Hal Canary1e0138b2017-03-10 13:56:08 -050084static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -050085
Hal Canary1e0138b2017-03-10 13:56:08 -050086static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -050087 if (depth <= 0) {
88 return nullptr;
89 }
90 int colorFilterType;
91 fuzz->nextRange(&colorFilterType, 0, 8);
92 switch (colorFilterType) {
93 case 0:
94 return nullptr;
95 case 1: {
96 SkColor color;
97 SkBlendMode mode;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -040098 fuzz->next(&color);
Kevin Lubick00587e32019-06-03 11:27:16 -040099 fuzz->nextEnum(&mode, SkBlendMode::kLastMode);
Mike Reedb286bc22019-04-08 16:23:20 -0400100 return SkColorFilters::Blend(color, mode);
Hal Canary27bece82017-03-07 16:23:20 -0500101 }
102 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500103 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500104 if (!outer) {
105 return nullptr;
106 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500107 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500108 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500109 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500110 }
111 case 3: {
Mike Reede869a1e2019-04-30 12:18:54 -0400112 float array[20];
Hal Canary27bece82017-03-07 16:23:20 -0500113 fuzz->nextN(array, SK_ARRAY_COUNT(array));
Mike Reede869a1e2019-04-30 12:18:54 -0400114 return SkColorFilters::Matrix(array);
Hal Canary27bece82017-03-07 16:23:20 -0500115 }
116 case 4: {
117 SkColor mul, add;
118 fuzz->next(&mul, &add);
119 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
120 }
121 case 5: {
122 bool grayscale;
123 int invertStyle;
124 float contrast;
125 fuzz->next(&grayscale);
126 fuzz->nextRange(&invertStyle, 0, 2);
127 fuzz->nextRange(&contrast, -1.0f, 1.0f);
128 return SkHighContrastFilter::Make(SkHighContrastConfig(
129 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
130 }
131 case 6:
132 return SkLumaColorFilter::Make();
133 case 7: {
134 uint8_t table[256];
135 fuzz->nextN(table, SK_ARRAY_COUNT(table));
136 return SkTableColorFilter::Make(table);
137 }
138 case 8: {
139 uint8_t tableA[256];
140 uint8_t tableR[256];
141 uint8_t tableG[256];
142 uint8_t tableB[256];
143 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
144 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
145 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
146 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
147 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
148 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500149 default:
150 SkASSERT(false);
151 break;
Hal Canary27bece82017-03-07 16:23:20 -0500152 }
153 return nullptr;
154}
Hal Canary24ac42b2017-02-14 13:35:14 -0500155
Hal Canary1e0138b2017-03-10 13:56:08 -0500156static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500157 SkScalar totalPos = 0;
158 for (int i = 0; i < colorCount; ++i) {
159 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
160 totalPos += pos[i];
161 }
162 totalPos = 1.0f / totalPos;
163 for (int i = 0; i < colorCount; ++i) {
164 pos[i] *= totalPos;
165 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500166 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500167 pos[colorCount - 1] = 1.0f;
168}
169
Hal Canary1e0138b2017-03-10 13:56:08 -0500170static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500171 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
172 sk_sp<SkColorFilter> colorFilter(nullptr);
173 SkBitmap bitmap;
174 sk_sp<SkImage> img;
Mike Reedfae8fce2019-04-03 10:27:45 -0400175 SkTileMode tmX, tmY;
Hal Canary24ac42b2017-02-14 13:35:14 -0500176 bool useMatrix;
177 SkColor color;
178 SkMatrix matrix;
179 SkBlendMode blendMode;
180 int shaderType;
181 if (depth <= 0) {
182 return nullptr;
183 }
Hal Canary671e4422017-02-27 13:36:38 -0500184 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500185 switch (shaderType) {
186 case 0:
187 return nullptr;
188 case 1:
Mike Reedc8bea7d2019-04-09 13:55:36 -0400189 return SkShaders::Empty();
Hal Canary24ac42b2017-02-14 13:35:14 -0500190 case 2:
191 fuzz->next(&color);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400192 return SkShaders::Color(color);
Hal Canary24ac42b2017-02-14 13:35:14 -0500193 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500194 img = make_fuzz_image(fuzz);
Kevin Lubick00587e32019-06-03 11:27:16 -0400195 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
196 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400197 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500198 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400199 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500200 }
201 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
202 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500203 bitmap = make_fuzz_bitmap(fuzz);
Kevin Lubick00587e32019-06-03 11:27:16 -0400204 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
205 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400206 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500207 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400208 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500209 }
Mike Reed50acf8f2019-04-08 13:20:23 -0400210 return bitmap.makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500211 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500212 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400213 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500214 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
215 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500216 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
217 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500218 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
219 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500220 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
221 shader2 = make_fuzz_shader(fuzz, depth - 1);
Kevin Lubick00587e32019-06-03 11:27:16 -0400222 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400223 return SkShaders::Blend(blendMode, std::move(shader1), std::move(shader2));
Hal Canaryb69c4b82017-03-08 11:02:40 -0500224 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500225 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500226 bool useTile;
227 SkRect tile;
Kevin Lubick00587e32019-06-03 11:27:16 -0400228 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
229 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400230 fuzz->next(&useMatrix, &useTile);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500231 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400232 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500233 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500234 if (useTile) {
235 fuzz->next(&tile);
236 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400237 return pic->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr, useTile ? &tile : nullptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500238 }
Hal Canary671e4422017-02-27 13:36:38 -0500239 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500240 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000241 // Deprecated SkGaussianEdgeShader
242 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500243 case 10: {
244 constexpr int kMaxColors = 12;
245 SkPoint pts[2];
246 SkColor colors[kMaxColors];
247 SkScalar pos[kMaxColors];
248 int colorCount;
249 bool usePos;
250 fuzz->nextN(pts, 2);
251 fuzz->nextRange(&colorCount, 2, kMaxColors);
252 fuzz->nextN(colors, colorCount);
Kevin Lubick00587e32019-06-03 11:27:16 -0400253 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400254 fuzz->next(&useMatrix, &usePos);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500255 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400256 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500257 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500258 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500259 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500260 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500261 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
262 tmX, 0, useMatrix ? &matrix : nullptr);
263 }
264 case 11: {
265 constexpr int kMaxColors = 12;
266 SkPoint center;
267 SkScalar radius;
268 int colorCount;
269 bool usePos;
270 SkColor colors[kMaxColors];
271 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400272 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400273 fuzz->next(&useMatrix, &usePos, &center, &radius);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500274 fuzz->nextRange(&colorCount, 2, kMaxColors);
275 fuzz->nextN(colors, colorCount);
276 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400277 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500278 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500279 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500280 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500281 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500282 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
283 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
284 }
285 case 12: {
286 constexpr int kMaxColors = 12;
287 SkPoint start, end;
288 SkScalar startRadius, endRadius;
289 int colorCount;
290 bool usePos;
291 SkColor colors[kMaxColors];
292 SkScalar pos[kMaxColors];
Kevin Lubick00587e32019-06-03 11:27:16 -0400293 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400294 fuzz->next(&useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500295 fuzz->nextRange(&colorCount, 2, kMaxColors);
296 fuzz->nextN(colors, colorCount);
297 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400298 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500299 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500300 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500301 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500302 }
303 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
304 usePos ? pos : nullptr, colorCount, tmX, 0,
305 useMatrix ? &matrix : nullptr);
306 }
307 case 13: {
308 constexpr int kMaxColors = 12;
309 SkScalar cx, cy;
310 int colorCount;
311 bool usePos;
312 SkColor colors[kMaxColors];
313 SkScalar pos[kMaxColors];
314 fuzz->next(&cx, &cy, &useMatrix, &usePos);
315 fuzz->nextRange(&colorCount, 2, kMaxColors);
316 fuzz->nextN(colors, colorCount);
317 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400318 FuzzNiceMatrix(fuzz, &matrix);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500319 }
320 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500321 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500322 }
323 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
324 0, useMatrix ? &matrix : nullptr);
325 }
326 case 14: {
327 SkScalar baseFrequencyX, baseFrequencyY, seed;
328 int numOctaves;
329 SkISize tileSize;
330 bool useTileSize, turbulence;
331 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
332 if (useTileSize) {
333 fuzz->next(&tileSize);
334 }
335 fuzz->nextRange(&numOctaves, 2, 7);
336 if (turbulence) {
337 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
338 numOctaves, seed,
339 useTileSize ? &tileSize : nullptr);
340 } else {
341 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
342 numOctaves, seed,
343 useTileSize ? &tileSize : nullptr);
344 }
345 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500346 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500347 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500348 break;
349 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500350 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500351}
352
Hal Canary1e0138b2017-03-10 13:56:08 -0500353static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500354 if (depth <= 0) {
355 return nullptr;
356 }
357 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400358 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500359 switch (pathEffectType) {
360 case 0: {
361 return nullptr;
362 }
363 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500364 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
365 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500366 return SkPathEffect::MakeSum(std::move(first), std::move(second));
367 }
368 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500369 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
370 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500371 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
372 }
373 case 3: {
374 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400375 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500376 SkScalar advance, phase;
377 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500378 SkPath1DPathEffect::Style style;
Kevin Lubick00587e32019-06-03 11:27:16 -0400379 fuzz->nextEnum(&style, SkPath1DPathEffect::kLastEnum_Style);
Hal Canaryf7005202017-03-10 08:48:28 -0500380 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500381 }
382 case 4: {
383 SkScalar width;
384 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400385 fuzz->next(&width);
386 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500387 return SkLine2DPathEffect::Make(width, matrix);
388 }
389 case 5: {
390 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400391 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500392 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400393 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500394 return SkPath2DPathEffect::Make(matrix, path);
395 }
396 case 6: {
397 SkScalar radius;
398 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500399 return SkCornerPathEffect::Make(radius);
400 }
Mike Reed40e7e652017-07-22 22:12:59 -0400401 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500402 SkScalar phase;
403 fuzz->next(&phase);
404 SkScalar intervals[20];
405 int count;
406 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
407 fuzz->nextN(intervals, count);
408 return SkDashPathEffect::Make(intervals, count, phase);
409 }
Mike Reed40e7e652017-07-22 22:12:59 -0400410 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500411 SkScalar segLength, dev;
412 uint32_t seed;
413 fuzz->next(&segLength, &dev, &seed);
414 return SkDiscretePathEffect::Make(segLength, dev, seed);
415 }
416 default:
417 SkASSERT(false);
418 return nullptr;
419 }
420}
Hal Canary24ac42b2017-02-14 13:35:14 -0500421
Hal Canary1e0138b2017-03-10 13:56:08 -0500422static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500423 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400424 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500425 switch (maskfilterType) {
426 case 0:
427 return nullptr;
428 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500429 SkBlurStyle blurStyle;
Kevin Lubick00587e32019-06-03 11:27:16 -0400430 fuzz->nextEnum(&blurStyle, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500431 SkScalar sigma;
432 fuzz->next(&sigma);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400433 bool respectCTM;
434 fuzz->next(&respectCTM);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400435 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500436 }
Hal Canary5395c592017-03-08 16:52:18 -0500437 default:
438 SkASSERT(false);
439 return nullptr;
440 }
441}
Hal Canary24ac42b2017-02-14 13:35:14 -0500442
Hal Canary1e0138b2017-03-10 13:56:08 -0500443static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
444 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500445 return nullptr;
446 }
447 auto fontMugger = SkFontMgr::RefDefault();
448 SkASSERT(fontMugger);
449 int familyCount = fontMugger->countFamilies();
450 int i, j;
451 fuzz->nextRange(&i, 0, familyCount - 1);
452 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
453 int styleCount = family->count();
454 fuzz->nextRange(&j, 0, styleCount - 1);
455 return sk_sp<SkTypeface>(family->createTypeface(j));
456}
Hal Canary24ac42b2017-02-14 13:35:14 -0500457
Hal Canary1e0138b2017-03-10 13:56:08 -0500458static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500459
460static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
461 if (depth <= 0) {
462 return nullptr;
463 }
464 uint8_t imageFilterType;
465 fuzz->nextRange(&imageFilterType, 1, 6);
466 SkPoint3 p, q;
467 SkColor lightColor;
468 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
469 sk_sp<SkImageFilter> input;
Michael Ludwigef43f682019-08-01 16:38:46 -0400470 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500471 bool useCropRect;
472 fuzz->next(&useCropRect);
473 if (useCropRect) {
474 fuzz->next(&cropRect);
475 }
476 switch (imageFilterType) {
477 case 1:
478 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500479 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400480 return SkImageFilters::DistantLitDiffuse(p, lightColor, surfaceScale, k,
481 std::move(input),
482 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500483 case 2:
484 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500485 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400486 return SkImageFilters::PointLitDiffuse(p, lightColor, surfaceScale, k,
487 std::move(input),
488 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500489 case 3:
490 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500491 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400492 return SkImageFilters::SpotLitDiffuse(
Hal Canarye03c3e52017-03-09 11:33:35 -0500493 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
494 std::move(input), useCropRect ? &cropRect : nullptr);
495 case 4:
496 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500497 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400498 return SkImageFilters::DistantLitSpecular(p, lightColor, surfaceScale, k,
499 shininess, std::move(input),
500 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500501 case 5:
502 fuzz->next(&p, &lightColor, &surfaceScale, &k, &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::PointLitSpecular(p, lightColor, surfaceScale, k,
505 shininess, std::move(input),
506 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500507 case 6:
508 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
509 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500510 input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400511 return SkImageFilters::SpotLitSpecular(
Hal Canarye03c3e52017-03-09 11:33:35 -0500512 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
513 std::move(input), useCropRect ? &cropRect : nullptr);
514 default:
515 SkASSERT(false);
516 return nullptr;
517 }
518}
519
Hal Canary1e0138b2017-03-10 13:56:08 -0500520static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500521
Hal Canary1e0138b2017-03-10 13:56:08 -0500522static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500523 if (depth <= 0) {
524 return nullptr;
525 }
526 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500527 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500528 switch (imageFilterType) {
529 case 0:
530 return nullptr;
531 case 1: {
532 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500533 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500534 bool useCropRect;
535 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400536 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500537 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400538 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500539 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400540 return SkImageFilters::Blur(sigmaX, sigmaY, std::move(input),
541 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500542 }
543 case 2: {
544 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400545 FuzzNiceMatrix(fuzz, &matrix);
Hal Canarye03c3e52017-03-09 11:33:35 -0500546 SkFilterQuality quality;
Kevin Lubick00587e32019-06-03 11:27:16 -0400547 fuzz->nextEnum(&quality, SkFilterQuality::kLast_SkFilterQuality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500548 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400549 return SkImageFilters::MatrixTransform(matrix, quality, std::move(input));
Hal Canarye03c3e52017-03-09 11:33:35 -0500550 }
551 case 3: {
552 SkRegion region;
553 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500554 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500555 bool useCropRect;
556 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400557 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500558 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400559 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500560 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400561 return SkImageFilters::AlphaThreshold(region, innerMin, outerMax, std::move(input),
562 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500563 }
564 case 4: {
565 float k1, k2, k3, k4;
566 bool enforcePMColor;
567 bool useCropRect;
568 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500569 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
570 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400571 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500572 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400573 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500574 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400575 return SkImageFilters::Arithmetic(k1, k2, k3, k4, enforcePMColor,
576 std::move(background), std::move(foreground),
577 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500578 }
579 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500580 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
581 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500582 bool useCropRect;
Michael Ludwigef43f682019-08-01 16:38:46 -0400583 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500584 fuzz->next(&useCropRect);
585 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400586 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500587 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400588 return SkImageFilters::ColorFilter(std::move(cf), std::move(input),
589 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500590 }
591 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500592 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
593 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400594 return SkImageFilters::Compose(std::move(ifo), std::move(ifi));
Hal Canarye03c3e52017-03-09 11:33:35 -0500595 }
596 case 7: {
Michael Ludwigef43f682019-08-01 16:38:46 -0400597 SkColorChannel xChannelSelector, yChannelSelector;
598 fuzz->nextEnum(&xChannelSelector, SkColorChannel::kLastEnum);
599 fuzz->nextEnum(&yChannelSelector, SkColorChannel::kLastEnum);
Hal Canarye03c3e52017-03-09 11:33:35 -0500600 SkScalar scale;
601 bool useCropRect;
602 fuzz->next(&scale, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400603 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500604 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400605 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500606 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500607 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
608 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400609 return SkImageFilters::DisplacementMap(xChannelSelector, yChannelSelector, scale,
610 std::move(displacement), std::move(color),
611 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500612 }
613 case 8: {
614 SkScalar dx, dy, sigmaX, sigmaY;
615 SkColor color;
Michael Ludwigef43f682019-08-01 16:38:46 -0400616 bool shadowOnly, useCropRect;
617 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &shadowOnly, &useCropRect);
618 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500619 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400620 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500621 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500622 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400623 if (shadowOnly) {
624 return SkImageFilters::DropShadowOnly(dx, dy, sigmaX, sigmaY, color,
625 std::move(input),
626 useCropRect ? &cropRect : nullptr);
627 } else {
628 return SkImageFilters::DropShadow(dx, dy, sigmaX, sigmaY, color, std::move(input),
629 useCropRect ? &cropRect : nullptr);
630 }
Hal Canarye03c3e52017-03-09 11:33:35 -0500631 }
632 case 9:
Michael Ludwigef43f682019-08-01 16:38:46 -0400633 return SkImageFilters::Image(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500634 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500635 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500636 SkRect srcRect, dstRect;
637 SkFilterQuality filterQuality;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400638 fuzz->next(&srcRect, &dstRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400639 fuzz->nextEnum(&filterQuality, SkFilterQuality::kLast_SkFilterQuality);
Michael Ludwigef43f682019-08-01 16:38:46 -0400640 return SkImageFilters::Image(std::move(image), srcRect, dstRect, filterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500641 }
642 case 11:
643 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
644 case 12: {
645 SkRect srcRect;
646 SkScalar inset;
647 bool useCropRect;
Michael Ludwigef43f682019-08-01 16:38:46 -0400648 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500649 fuzz->next(&srcRect, &inset, &useCropRect);
650 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400651 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500652 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500653 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400654 return SkImageFilters::Magnifier(srcRect, inset, std::move(input),
655 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500656 }
657 case 13: {
658 constexpr int kMaxKernelSize = 5;
659 int32_t n, m;
660 fuzz->nextRange(&n, 1, kMaxKernelSize);
661 fuzz->nextRange(&m, 1, kMaxKernelSize);
662 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
663 fuzz->nextN(kernel, n * m);
664 int32_t offsetX, offsetY;
665 fuzz->nextRange(&offsetX, 0, n - 1);
666 fuzz->nextRange(&offsetY, 0, m - 1);
667 SkScalar gain, bias;
668 bool convolveAlpha, useCropRect;
669 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400670 SkTileMode tileMode;
671 fuzz->nextEnum(&tileMode, SkTileMode::kLastTileMode);
672 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500673 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400674 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500675 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500676 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400677 return SkImageFilters::MatrixConvolution(
Hal Canarye03c3e52017-03-09 11:33:35 -0500678 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
679 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
680 }
681 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500682 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
683 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500684 bool useCropRect;
685 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400686 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500687 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400688 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500689 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400690 return SkImageFilters::Merge(std::move(first), std::move(second),
691 useCropRect ? &cropRect : nullptr);
Mike Reed0bdaf052017-06-18 23:35:57 -0400692 }
693 case 15: {
694 constexpr int kMaxCount = 4;
695 sk_sp<SkImageFilter> ifs[kMaxCount];
696 int count;
697 fuzz->nextRange(&count, 1, kMaxCount);
698 for (int i = 0; i < count; ++i) {
699 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
700 }
701 bool useCropRect;
702 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400703 SkIRect cropRect;
Mike Reed0bdaf052017-06-18 23:35:57 -0400704 if (useCropRect) {
705 fuzz->next(&cropRect);
706 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400707 return SkImageFilters::Merge(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500708 }
709 case 16: {
710 int rx, ry;
711 fuzz->next(&rx, &ry);
712 bool useCropRect;
713 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400714 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500715 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400716 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500717 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500718 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400719 return SkImageFilters::Dilate(rx, ry, std::move(input),
720 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500721 }
722 case 17: {
723 int rx, ry;
724 fuzz->next(&rx, &ry);
725 bool useCropRect;
726 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400727 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500728 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400729 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500730 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500731 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400732 return SkImageFilters::Erode(rx, ry, std::move(input),
733 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500734 }
735 case 18: {
736 SkScalar dx, dy;
737 fuzz->next(&dx, &dy);
738 bool useCropRect;
739 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400740 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500741 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400742 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500743 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500744 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400745 return SkImageFilters::Offset(dx, dy, std::move(input),
746 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500747 }
748 case 19: {
749 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500750 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500751 bool useCropRect;
752 fuzz->next(&useCropRect);
Michael Ludwigef43f682019-08-01 16:38:46 -0400753 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500754 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400755 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500756 }
Michael Ludwigef43f682019-08-01 16:38:46 -0400757 return SkImageFilters::Paint(paint, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500758 }
759 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500760 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400761 return SkImageFilters::Picture(std::move(picture));
Hal Canarye03c3e52017-03-09 11:33:35 -0500762 }
763 case 21: {
764 SkRect cropRect;
765 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500766 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400767 return SkImageFilters::Picture(std::move(picture), cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500768 }
769 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500770 SkRect src, dst;
771 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500772 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400773 return SkImageFilters::Tile(src, dst, std::move(input));
Hal Canarye03c3e52017-03-09 11:33:35 -0500774 }
Mike Reed77e487d2017-11-09 21:50:20 +0000775 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500776 SkBlendMode blendMode;
777 bool useCropRect;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400778 fuzz->next(&useCropRect);
Kevin Lubick00587e32019-06-03 11:27:16 -0400779 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
Michael Ludwigef43f682019-08-01 16:38:46 -0400780 SkIRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500781 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400782 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500783 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500784 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
785 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Michael Ludwigef43f682019-08-01 16:38:46 -0400786 return SkImageFilters::Xfermode(blendMode, std::move(bg), std::move(fg),
787 useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500788 }
789 default:
790 SkASSERT(false);
791 return nullptr;
792 }
793}
Hal Canary24ac42b2017-02-14 13:35:14 -0500794
Hal Canary1e0138b2017-03-10 13:56:08 -0500795static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500796 int w, h;
797 fuzz->nextRange(&w, 1, 1024);
798 fuzz->nextRange(&h, 1, 1024);
799 SkAutoTMalloc<SkPMColor> data(w * h);
800 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
801 int n = w * h;
802 for (int i = 0; i < n; ++i) {
803 SkColor c;
804 fuzz->next(&c);
805 data[i] = SkPreMultiplyColor(c);
806 }
807 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500808 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
809 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500810}
811
Hal Canary1e0138b2017-03-10 13:56:08 -0500812static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500813 SkBitmap bitmap;
814 int w, h;
815 fuzz->nextRange(&w, 1, 1024);
816 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500817 if (!bitmap.tryAllocN32Pixels(w, h)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400818 SkDEBUGF("Could not allocate pixels %d x %d", w, h);
Kevin Lubick1991f552018-02-27 10:59:10 -0500819 return bitmap;
820 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500821 for (int y = 0; y < h; ++y) {
822 for (int x = 0; x < w; ++x) {
823 SkColor c;
824 fuzz->next(&c);
825 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
826 }
827 }
828 return bitmap;
829}
830
Kevin Lubick00587e32019-06-03 11:27:16 -0400831template <typename T>
832static T make_fuzz_enum_range(Fuzz* fuzz, T maxv) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500833 T value;
Kevin Lubick00587e32019-06-03 11:27:16 -0400834 fuzz->nextEnum(&value, maxv);
Hal Canary1e0138b2017-03-10 13:56:08 -0500835 return value;
836}
837
838static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500839 if (!fuzz || !paint || depth <= 0) {
840 return;
841 }
842
Hal Canary1e0138b2017-03-10 13:56:08 -0500843 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
844 paint->setDither( make_fuzz_t<bool>(fuzz));
845 paint->setColor( make_fuzz_t<SkColor>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400846 paint->setBlendMode( make_fuzz_enum_range<SkBlendMode>(fuzz, SkBlendMode::kLastMode));
847 paint->setFilterQuality(make_fuzz_enum_range<SkFilterQuality>(fuzz, kLast_SkFilterQuality));
848 paint->setStyle( make_fuzz_enum_range<SkPaint::Style>(fuzz,
849 SkPaint::Style::kStrokeAndFill_Style));
Hal Canary1e0138b2017-03-10 13:56:08 -0500850 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
851 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
852 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
853 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
854 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500855
856 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500857 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
858 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400859 paint->setStrokeCap( make_fuzz_enum_range<SkPaint::Cap>(fuzz, SkPaint::kLast_Cap));
860 paint->setStrokeJoin( make_fuzz_enum_range<SkPaint::Join>(fuzz, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500861 }
862}
863
Mike Reed358fcad2018-11-23 15:27:51 -0500864static SkFont fuzz_font(Fuzz* fuzz) {
865 SkFont font;
866 font.setTypeface( make_fuzz_typeface(fuzz));
867 font.setSize( make_fuzz_t<SkScalar>(fuzz));
868 font.setScaleX( make_fuzz_t<SkScalar>(fuzz));
869 font.setSkewX( make_fuzz_t<SkScalar>(fuzz));
870 font.setLinearMetrics( make_fuzz_t<bool>(fuzz));
871 font.setSubpixel( make_fuzz_t<bool>(fuzz));
872 font.setEmbeddedBitmaps( make_fuzz_t<bool>(fuzz));
873 font.setForceAutoHinting( make_fuzz_t<bool>(fuzz));
874 font.setEmbolden( make_fuzz_t<bool>(fuzz));
Kevin Lubick00587e32019-06-03 11:27:16 -0400875 font.setHinting( make_fuzz_enum_range<SkFontHinting>(fuzz, SkFontHinting::kFull));
876 font.setEdging( make_fuzz_enum_range<SkFont::Edging>(fuzz,
877 SkFont::Edging::kSubpixelAntiAlias));
Mike Reed358fcad2018-11-23 15:27:51 -0500878 return font;
Hal Canary5395c592017-03-08 16:52:18 -0500879}
880
Mike Reed2ed78202018-11-21 15:10:08 -0500881static SkTextEncoding fuzz_paint_text_encoding(Fuzz* fuzz) {
Kevin Lubick00587e32019-06-03 11:27:16 -0400882 return make_fuzz_enum_range<SkTextEncoding>(fuzz, SkTextEncoding::kUTF32);
Hal Canary24ac42b2017-02-14 13:35:14 -0500883}
884
Hal Canary5395c592017-03-08 16:52:18 -0500885constexpr int kMaxGlyphCount = 30;
886
Mike Reed2ed78202018-11-21 15:10:08 -0500887static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkFont& font, SkTextEncoding encoding) {
Hal Canary671e4422017-02-27 13:36:38 -0500888 SkTDArray<uint8_t> array;
Ben Wagner51e15a62019-05-07 15:38:46 -0400889 if (SkTextEncoding::kGlyphID == encoding) {
Herb Derby087fad72019-01-22 14:45:16 -0500890 int glyphRange = font.getTypefaceOrDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500891 if (glyphRange == 0) {
892 // Some fuzzing environments have no fonts, so empty array is the best
893 // we can do.
894 return array;
895 }
Hal Canary671e4422017-02-27 13:36:38 -0500896 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500897 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500898 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
899 for (int i = 0; i < glyphCount; ++i) {
900 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
901 }
902 return array;
903 }
904 static const SkUnichar ranges[][2] = {
905 {0x0020, 0x007F},
906 {0x00A1, 0x0250},
907 {0x0400, 0x0500},
908 };
909 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500910 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500911 count += (ranges[i][1] - ranges[i][0]);
912 }
Hal Canary5395c592017-03-08 16:52:18 -0500913 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500914 SkUnichar buffer[kMaxLength];
915 int length;
916 fuzz->nextRange(&length, 1, kMaxLength);
917 for (int j = 0; j < length; ++j) {
918 int32_t value;
919 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500920 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500921 if (value + ranges[i][0] < ranges[i][1]) {
922 buffer[j] = value + ranges[i][0];
923 break;
924 } else {
925 value -= (ranges[i][1] - ranges[i][0]);
926 }
927 }
928 }
Mike Reed2ed78202018-11-21 15:10:08 -0500929 switch (encoding) {
Ben Wagner51e15a62019-05-07 15:38:46 -0400930 case SkTextEncoding::kUTF8: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500931 size_t utf8len = 0;
932 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400933 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -0500934 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500935 char* ptr = (char*)array.append(utf8len);
936 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400937 ptr += SkUTF::ToUTF8(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -0500938 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500939 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400940 case SkTextEncoding::kUTF16: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500941 size_t utf16len = 0;
942 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400943 utf16len += SkUTF::ToUTF16(buffer[j]);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500944 }
945 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
946 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400947 ptr += SkUTF::ToUTF16(buffer[j], ptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500948 }
949 } break;
Ben Wagner51e15a62019-05-07 15:38:46 -0400950 case SkTextEncoding::kUTF32:
Hal Canary671e4422017-02-27 13:36:38 -0500951 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -0500952 break;
Hal Canary671e4422017-02-27 13:36:38 -0500953 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -0500954 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -0500955 break;
Hal Canary671e4422017-02-27 13:36:38 -0500956 }
957 return array;
958}
959
Hal Canary5395c592017-03-08 16:52:18 -0500960static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
961 SkTextBlobBuilder textBlobBuilder;
962 int8_t runCount;
963 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
964 while (runCount-- > 0) {
Mike Reed2ed78202018-11-21 15:10:08 -0500965 SkFont font;
966 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
967 font.setEdging(make_fuzz_t<bool>(fuzz) ? SkFont::Edging::kAlias : SkFont::Edging::kAntiAlias);
968 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
969 int glyphCount = font.countText(text.begin(), SkToSizeT(text.count()), encoding);
Hal Canary5395c592017-03-08 16:52:18 -0500970 SkASSERT(glyphCount <= kMaxGlyphCount);
971 SkScalar x, y;
972 const SkTextBlobBuilder::RunBuffer* buffer;
973 uint8_t runType;
974 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
Hal Canaryfc97f222018-12-17 13:48:44 -0500975 const void* textPtr = text.begin();
976 size_t textLen = SkToSizeT(text.count());
Hal Canary5395c592017-03-08 16:52:18 -0500977 switch (runType) {
978 case 0:
979 fuzz->next(&x, &y);
980 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500981 buffer = &textBlobBuilder.allocRun(font, glyphCount, x, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500982 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500983 break;
984 case 1:
985 fuzz->next(&y);
986 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500987 buffer = &textBlobBuilder.allocRunPosH(font, glyphCount, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500988 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500989 fuzz->nextN(buffer->pos, glyphCount);
990 break;
991 case 2:
992 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500993 buffer = &textBlobBuilder.allocRunPos(font, glyphCount);
Hal Canaryfc97f222018-12-17 13:48:44 -0500994 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500995 fuzz->nextN(buffer->pos, glyphCount * 2);
996 break;
997 default:
998 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -0500999 break;
Hal Canary5395c592017-03-08 16:52:18 -05001000 }
1001 }
1002 return textBlobBuilder.make();
1003}
1004
Hal Canary1e0138b2017-03-10 13:56:08 -05001005static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001006 if (!fuzz || !canvas || depth <= 0) {
1007 return;
1008 }
1009 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1010 unsigned N;
1011 fuzz->nextRange(&N, 0, 2000);
1012 for (unsigned i = 0; i < N; ++i) {
1013 if (fuzz->exhausted()) {
1014 return;
1015 }
1016 SkPaint paint;
Mike Reed358fcad2018-11-23 15:27:51 -05001017 SkFont font;
Hal Canary24ac42b2017-02-14 13:35:14 -05001018 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001019 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001020 switch (drawCommand) {
1021 case 0:
1022 canvas->flush();
1023 break;
1024 case 1:
1025 canvas->save();
1026 break;
1027 case 2: {
1028 SkRect bounds;
1029 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001030 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001031 canvas->saveLayer(&bounds, &paint);
1032 break;
1033 }
1034 case 3: {
1035 SkRect bounds;
1036 fuzz->next(&bounds);
1037 canvas->saveLayer(&bounds, nullptr);
1038 break;
1039 }
1040 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001041 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001042 canvas->saveLayer(nullptr, &paint);
1043 break;
1044 case 5:
1045 canvas->saveLayer(nullptr, nullptr);
1046 break;
1047 case 6: {
1048 uint8_t alpha;
1049 fuzz->next(&alpha);
1050 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1051 break;
1052 }
1053 case 7: {
1054 SkRect bounds;
1055 uint8_t alpha;
1056 fuzz->next(&bounds, &alpha);
1057 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1058 break;
1059 }
1060 case 8: {
1061 SkCanvas::SaveLayerRec saveLayerRec;
1062 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001063 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001064 fuzz->next(&bounds);
1065 saveLayerRec.fBounds = &bounds;
1066 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001067 if (make_fuzz_t<bool>(fuzz)) {
1068 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001069 saveLayerRec.fPaint = &paint;
1070 }
1071 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001072 if (make_fuzz_t<bool>(fuzz)) {
1073 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001074 saveLayerRec.fBackdrop = imageFilter.get();
1075 }
Hal Canary5395c592017-03-08 16:52:18 -05001076 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001077 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001078 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1079 // }
1080
Hal Canary24ac42b2017-02-14 13:35:14 -05001081 canvas->saveLayer(saveLayerRec);
1082 break;
1083 }
1084 case 9:
1085 canvas->restore();
1086 break;
1087 case 10: {
1088 int saveCount;
1089 fuzz->next(&saveCount);
1090 canvas->restoreToCount(saveCount);
1091 break;
1092 }
1093 case 11: {
1094 SkScalar x, y;
1095 fuzz->next(&x, &y);
1096 canvas->translate(x, y);
1097 break;
1098 }
1099 case 12: {
1100 SkScalar x, y;
1101 fuzz->next(&x, &y);
1102 canvas->scale(x, y);
1103 break;
1104 }
1105 case 13: {
1106 SkScalar v;
1107 fuzz->next(&v);
1108 canvas->rotate(v);
1109 break;
1110 }
1111 case 14: {
1112 SkScalar x, y, v;
1113 fuzz->next(&x, &y, &v);
1114 canvas->rotate(v, x, y);
1115 break;
1116 }
1117 case 15: {
1118 SkScalar x, y;
1119 fuzz->next(&x, &y);
1120 canvas->skew(x, y);
1121 break;
1122 }
1123 case 16: {
1124 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001125 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001126 canvas->concat(mat);
1127 break;
1128 }
1129 case 17: {
1130 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001131 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001132 canvas->setMatrix(mat);
1133 break;
1134 }
1135 case 18:
1136 canvas->resetMatrix();
1137 break;
1138 case 19: {
1139 SkRect r;
1140 int op;
1141 bool doAntiAlias;
1142 fuzz->next(&r, &doAntiAlias);
1143 fuzz->nextRange(&op, 0, 1);
1144 r.sort();
1145 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1146 break;
1147 }
1148 case 20: {
1149 SkRRect rr;
1150 int op;
1151 bool doAntiAlias;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001152 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001153 fuzz->next(&doAntiAlias);
1154 fuzz->nextRange(&op, 0, 1);
1155 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1156 break;
1157 }
1158 case 21: {
1159 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001160 FuzzNicePath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001161 int op;
1162 bool doAntiAlias;
1163 fuzz->next(&doAntiAlias);
1164 fuzz->nextRange(&op, 0, 1);
1165 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1166 break;
1167 }
1168 case 22: {
1169 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001170 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001171 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001172 fuzz->nextRange(&op, 0, 1);
1173 canvas->clipRegion(region, (SkClipOp)op);
1174 break;
1175 }
1176 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001177 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001178 canvas->drawPaint(paint);
1179 break;
1180 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001181 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001182 SkCanvas::PointMode pointMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001183 fuzz->nextRange(&pointMode,
Hal Canaryc8bebd42017-12-20 11:21:05 -05001184 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001185 size_t count;
1186 constexpr int kMaxCount = 30;
1187 fuzz->nextRange(&count, 0, kMaxCount);
1188 SkPoint pts[kMaxCount];
1189 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001190 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001191 break;
1192 }
1193 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001194 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001195 SkRect r;
1196 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001197 if (!r.isFinite()) {
1198 break;
1199 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001200 canvas->drawRect(r, paint);
1201 break;
1202 }
1203 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001204 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001205 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001206 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001207 canvas->drawRegion(region, paint);
1208 break;
1209 }
1210 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001211 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001212 SkRect r;
1213 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001214 if (!r.isFinite()) {
1215 break;
1216 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001217 canvas->drawOval(r, paint);
1218 break;
1219 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001220 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001221 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001222 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001223 SkRRect rr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001224 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001225 canvas->drawRRect(rr, paint);
1226 break;
1227 }
1228 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001229 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001230 SkRRect orr, irr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001231 FuzzNiceRRect(fuzz, &orr);
1232 FuzzNiceRRect(fuzz, &irr);
Hal Canary27bece82017-03-07 16:23:20 -05001233 if (orr.getBounds().contains(irr.getBounds())) {
1234 canvas->drawDRRect(orr, irr, paint);
1235 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001236 break;
1237 }
1238 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001239 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001240 SkRect r;
1241 SkScalar start, sweep;
1242 bool useCenter;
1243 fuzz->next(&r, &start, &sweep, &useCenter);
1244 canvas->drawArc(r, start, sweep, useCenter, paint);
1245 break;
1246 }
1247 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001248 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001249 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001250 FuzzNicePath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001251 canvas->drawPath(path, paint);
1252 break;
1253 }
1254 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001255 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001256 SkScalar left, top;
1257 bool usePaint;
1258 fuzz->next(&left, &top, &usePaint);
1259 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001260 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001261 }
1262 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1263 break;
1264 }
1265 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001266 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001267 SkRect src, dst;
1268 bool usePaint;
1269 fuzz->next(&src, &dst, &usePaint);
1270 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001271 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001272 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001273 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001274 break;
1275 }
1276 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001277 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001278 SkIRect src;
1279 SkRect dst;
1280 bool usePaint;
1281 fuzz->next(&src, &dst, &usePaint);
1282 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001283 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001284 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001285 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001286 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1287 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001288 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1289 break;
1290 }
1291 case 36: {
1292 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001293 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001294 SkRect dst;
1295 fuzz->next(&dst, &usePaint);
1296 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001297 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001298 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001299 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001300 break;
1301 }
1302 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001303 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001304 SkIRect center;
1305 SkRect dst;
1306 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001307 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001308 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001309 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001310 }
Hal Canary0361d492017-03-15 12:58:15 -04001311 if (make_fuzz_t<bool>(fuzz)) {
1312 fuzz->next(&center);
1313 } else { // Make valid center, see SkLatticeIter::Valid().
1314 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1315 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1316 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1317 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1318 }
1319 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001320 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1321 break;
1322 }
1323 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001324 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001325 SkScalar left, top;
1326 bool usePaint;
1327 fuzz->next(&left, &top, &usePaint);
1328 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001329 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001330 }
1331 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1332 break;
1333 }
1334 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001335 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001336 SkRect src, dst;
1337 bool usePaint;
1338 fuzz->next(&src, &dst, &usePaint);
1339 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001340 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001341 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001342 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001343 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1344 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001345 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1346 break;
1347 }
1348 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001349 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001350 SkIRect src;
1351 SkRect dst;
1352 bool usePaint;
1353 fuzz->next(&src, &dst, &usePaint);
1354 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001356 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001357 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001358 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1359 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001360 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1361 break;
1362 }
1363 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001364 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001365 SkRect dst;
1366 bool usePaint;
1367 fuzz->next(&dst, &usePaint);
1368 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001369 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001370 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001371 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001372 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1373 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001374 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1375 break;
1376 }
1377 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001378 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001379 SkIRect center;
1380 SkRect dst;
1381 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001382 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001383 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001384 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001385 }
Hal Canary0361d492017-03-15 12:58:15 -04001386 if (make_fuzz_t<bool>(fuzz)) {
1387 fuzz->next(&center);
1388 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001389 if (img.width() == 0 || img.height() == 0) {
1390 // bitmap may not have had its pixels initialized.
1391 break;
1392 }
Hal Canary0361d492017-03-15 12:58:15 -04001393 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1394 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1395 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1396 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1397 }
1398 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001399 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1400 break;
1401 }
1402 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001403 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001404 bool usePaint;
1405 SkRect dst;
1406 fuzz->next(&usePaint, &dst);
1407 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001408 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001409 }
1410 constexpr int kMax = 6;
1411 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001412 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001413 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1414 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1415 fuzz->nextN(xDivs, lattice.fXCount);
1416 fuzz->nextN(yDivs, lattice.fYCount);
1417 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1418 break;
1419 }
1420 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001421 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001422 bool usePaint;
1423 SkRect dst;
1424 fuzz->next(&usePaint, &dst);
1425 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001426 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001427 }
1428 constexpr int kMax = 6;
1429 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001430 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001431 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1432 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1433 fuzz->nextN(xDivs, lattice.fXCount);
1434 fuzz->nextN(yDivs, lattice.fYCount);
1435 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1436 break;
1437 }
1438 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001439 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed358fcad2018-11-23 15:27:51 -05001440 font = fuzz_font(fuzz);
1441 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001442 SkScalar x, y;
1443 fuzz->next(&x, &y);
Mike Reed358fcad2018-11-23 15:27:51 -05001444 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
1445 canvas->drawSimpleText(text.begin(), SkToSizeT(text.count()), encoding, x, y,
1446 font, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001447 break;
1448 }
1449 case 46: {
Mike Reed212e9062018-12-25 17:35:49 -05001450 // was drawPosText
Hal Canary24ac42b2017-02-14 13:35:14 -05001451 break;
1452 }
1453 case 47: {
Mike Reed212e9062018-12-25 17:35:49 -05001454 // was drawPosTextH
Hal Canary24ac42b2017-02-14 13:35:14 -05001455 break;
1456 }
1457 case 48: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001458 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001459 break;
1460 }
1461 case 49: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001462 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001463 break;
1464 }
1465 case 50: {
Mike Reed212e9062018-12-25 17:35:49 -05001466 // was drawTextRSXform
Hal Canary24ac42b2017-02-14 13:35:14 -05001467 break;
1468 }
1469 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001470 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001471 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001472 SkScalar x, y;
1473 fuzz->next(&x, &y);
1474 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001475 break;
1476 }
1477 case 52: {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001478 SkMatrix matrix;
Hal Canary24ac42b2017-02-14 13:35:14 -05001479 bool usePaint, useMatrix;
1480 fuzz->next(&usePaint, &useMatrix);
1481 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001482 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001483 }
1484 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001485 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -05001486 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001487 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001488 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1489 usePaint ? &paint : nullptr);
1490 break;
1491 }
1492 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001493 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001494 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001495 SkBlendMode blendMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001496 fuzz->nextRange(&vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
1497 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001498 constexpr int kMaxCount = 100;
1499 int vertexCount;
1500 SkPoint vertices[kMaxCount];
1501 SkPoint texs[kMaxCount];
1502 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001503 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1504 fuzz->nextN(vertices, vertexCount);
1505 bool useTexs, useColors;
1506 fuzz->next(&useTexs, &useColors);
1507 if (useTexs) {
1508 fuzz->nextN(texs, vertexCount);
1509 }
1510 if (useColors) {
1511 fuzz->nextN(colors, vertexCount);
1512 }
1513 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001514 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001515 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001516 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1517 for (int i = 0; i < indexCount; ++i) {
1518 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1519 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001520 }
Mike Reed887cdf12017-04-03 11:11:09 -04001521 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1522 useTexs ? texs : nullptr,
1523 useColors ? colors : nullptr,
1524 indexCount, indices),
1525 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001526 break;
1527 }
1528 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001529 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001530 break;
1531 }
1532 }
1533}
1534
Hal Canary1e0138b2017-03-10 13:56:08 -05001535static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001536 SkScalar w, h;
1537 fuzz->next(&w, &h);
1538 SkPictureRecorder pictureRecorder;
1539 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1540 return pictureRecorder.finishRecordingAsPicture();
1541}
1542
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001543DEF_FUZZ(NullCanvas, fuzz) {
1544 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001545}
1546
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001547constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001548
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001549DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001550 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001551 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001552 fuzz_canvas(fuzz, surface->getCanvas());
1553}
1554
Hal Canarye2924432017-12-01 11:46:26 -05001555DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1556 SkPictureRecorder recorder;
1557 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1558 SkIntToScalar(kCanvasSize.height())));
1559 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1560 if (!pic) { fuzz->signalBug(); }
1561 sk_sp<SkData> data = pic->serialize();
1562 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001563 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001564 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001565 if (!deserialized) { fuzz->signalBug(); }
1566 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1567 SkASSERT(surface && surface->getCanvas());
1568 surface->getCanvas()->drawPicture(deserialized);
1569}
1570
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001571DEF_FUZZ(ImageFilter, fuzz) {
1572 auto fil = make_fuzz_imageFilter(fuzz, 20);
1573
1574 SkPaint paint;
1575 paint.setImageFilter(fil);
1576 SkBitmap bitmap;
1577 SkCanvas canvas(bitmap);
1578 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1579}
1580
1581
1582//SkRandom _rand;
1583#define SK_ADD_RANDOM_BIT_FLIPS
1584
1585DEF_FUZZ(SerializedImageFilter, fuzz) {
Robert Phillipse5476bb2019-03-14 13:30:08 -04001586 SkBitmap bitmap;
1587 if (!bitmap.tryAllocN32Pixels(256, 256)) {
1588 SkDEBUGF("Could not allocate 256x256 bitmap in SerializedImageFilter");
1589 return;
1590 }
1591
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001592 auto filter = make_fuzz_imageFilter(fuzz, 20);
Robert Phillips66f6ef42018-09-14 11:58:40 -04001593 if (!filter) {
1594 return;
1595 }
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001596 auto data = filter->serialize();
1597 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1598 size_t len = data->size();
1599#ifdef SK_ADD_RANDOM_BIT_FLIPS
1600 unsigned char* p = const_cast<unsigned char*>(ptr);
1601 for (size_t i = 0; i < len; ++i, ++p) {
1602 uint8_t j;
1603 fuzz->nextRange(&j, 1, 250);
1604 if (j == 1) { // 0.4% of the time, flip a bit or byte
1605 uint8_t k;
1606 fuzz->nextRange(&k, 1, 10);
1607 if (k == 1) { // Then 10% of the time, change a whole byte
1608 uint8_t s;
1609 fuzz->nextRange(&s, 0, 2);
1610 switch(s) {
1611 case 0:
1612 *p ^= 0xFF; // Flip entire byte
1613 break;
1614 case 1:
1615 *p = 0xFF; // Set all bits to 1
1616 break;
1617 case 2:
1618 *p = 0x00; // Set all bits to 0
1619 break;
1620 }
1621 } else {
1622 uint8_t s;
1623 fuzz->nextRange(&s, 0, 7);
1624 *p ^= (1 << 7);
1625 }
1626 }
1627 }
1628#endif // SK_ADD_RANDOM_BIT_FLIPS
1629 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1630
1631 // uncomment below to write out a serialized image filter (to make corpus
1632 // for -t filter_fuzz)
1633 // SkString s("./serialized_filters/sf");
1634 // s.appendU32(_rand.nextU());
1635 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1636 // sk_fwrite(data->bytes(), data->size(), file);
1637 // sk_fclose(file);
1638
1639 SkPaint paint;
1640 paint.setImageFilter(deserializedFil);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001641
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001642 SkCanvas canvas(bitmap);
Robert Phillipse5476bb2019-03-14 13:30:08 -04001643 canvas.saveLayer(SkRect::MakeWH(256, 256), &paint);
1644 canvas.restore();
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001645}
1646
Hal Canary44801ca2017-03-15 11:39:06 -04001647#if SK_SUPPORT_GPU
Kevin Lubickfaef5142018-06-07 10:33:11 -04001648
1649static void dump_GPU_info(GrContext* context) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001650 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->priv().getGpu())
Kevin Lubickfaef5142018-06-07 10:33:11 -04001651 ->glInterface();
1652 const GrGLubyte* output;
1653 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1654 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1655
1656 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1657 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1658
1659 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1660 SkDebugf("GL_VERSION %s\n", (const char*) output);
1661}
1662
Hal Canary5aa91582017-03-21 11:11:44 -07001663static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1664 SkASSERT(context);
1665 auto surface = SkSurface::MakeRenderTarget(
1666 context,
1667 SkBudgeted::kNo,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001668 SkImageInfo::Make(kCanvasSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
Hal Canary5aa91582017-03-21 11:11:44 -07001669 SkASSERT(surface && surface->getCanvas());
1670 fuzz_canvas(fuzz, surface->getCanvas());
1671}
1672
Hal Canary44801ca2017-03-15 11:39:06 -04001673DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001674 sk_gpu_test::GrContextFactory f;
1675 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001676 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001677 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001678 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001679 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001680 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001681 }
Hal Canary5aa91582017-03-21 11:11:44 -07001682 fuzz_ganesh(fuzz, context);
1683}
1684
Kevin Lubick27d42192018-04-03 12:30:32 -04001685DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001686 sk_gpu_test::GrContextFactory f;
1687 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1688}
Hal Canary44801ca2017-03-15 11:39:06 -04001689#endif
1690
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001691DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001692 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -04001693 auto doc = SkPDF::MakeDocument(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001694 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1695 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001696}
1697
1698// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1699DEF_FUZZ(_DumpCanvas, fuzz) {
Mike Klein8f4e2242019-03-20 11:59:00 -05001700 DebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001701 fuzz_canvas(fuzz, &debugCanvas);
1702 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1703 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05001704 SkDynamicMemoryWStream stream;
1705 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1706 writer.beginObject(); // root
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -05001707 debugCanvas.toJSON(writer, dataManager, nullCanvas.get());
Brian Osmand8a90f92019-01-28 13:41:19 -05001708 writer.endObject(); // root
1709 writer.flush();
1710 sk_sp<SkData> json = stream.detachAsData();
1711 fwrite(json->data(), json->size(), 1, stdout);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001712}