blob: 7fac837560d81e4f3d5d8555c1b581046e64819a [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
8#include "Fuzz.h"
Kevin Lubick2541edf2018-01-11 10:27:14 -05009#include "FuzzCommon.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050010
11// CORE
12#include "SkCanvas.h"
13#include "SkColorFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050014#include "SkDebugCanvas.h"
Hal Canary671e4422017-02-27 13:36:38 -050015#include "SkFontMgr.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050016#include "SkImageFilter.h"
17#include "SkMaskFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050018#include "SkNullCanvas.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040019#include "SkOSFile.h"
Hal Canary23564b92018-09-07 14:33:14 -040020#include "SkPDFDocument.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050021#include "SkPathEffect.h"
Cary Clarkefd99cc2018-06-11 16:25:43 -040022#include "SkPicturePriv.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050023#include "SkPictureRecorder.h"
Mike Reed54518ac2017-07-22 08:29:48 -040024#include "SkPoint3.h"
Hal Canary5395c592017-03-08 16:52:18 -050025#include "SkRSXform.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050026#include "SkRegion.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050027#include "SkSurface.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040028#include "SkTo.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050029#include "SkTypeface.h"
30
31// EFFECTS
Hal Canary5395c592017-03-08 16:52:18 -050032#include "Sk1DPathEffect.h"
33#include "Sk2DPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050034#include "SkAlphaThresholdFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050035#include "SkArithmeticImageFilter.h"
Robert Phillips70e3e9a2017-06-26 14:22:01 -040036#include "SkBlurImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050037#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050038#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050039#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050040#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050041#include "SkCornerPathEffect.h"
42#include "SkDashPathEffect.h"
43#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050044#include "SkDisplacementMapEffect.h"
45#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050046#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050047#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050048#include "SkImageSource.h"
49#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050050#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050051#include "SkMagnifierImageFilter.h"
52#include "SkMatrixConvolutionImageFilter.h"
53#include "SkMergeImageFilter.h"
54#include "SkMorphologyImageFilter.h"
55#include "SkOffsetImageFilter.h"
56#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050057#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050058#include "SkPictureImageFilter.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050059#include "SkReadBuffer.h"
Hal Canary27bece82017-03-07 16:23:20 -050060#include "SkTableColorFilter.h"
Mike Reedc090c642017-05-16 10:39:06 -040061#include "SkTextBlob.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050062#include "SkTileImageFilter.h"
63#include "SkXfermodeImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050064
65// SRC
Kevin Lubickfaef5142018-06-07 10:33:11 -040066#include "SkCommandLineFlags.h"
Hal Canaryea60b952018-08-21 11:45:46 -040067#include "SkUTF.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050068
Hal Canary44801ca2017-03-15 11:39:06 -040069#if SK_SUPPORT_GPU
70#include "GrContextFactory.h"
Kevin Lubickfaef5142018-06-07 10:33:11 -040071#include "GrContextPriv.h"
72#include "gl/GrGLFunctions.h"
73#include "gl/GrGLGpu.h"
74#include "gl/GrGLUtil.h"
Hal Canary44801ca2017-03-15 11:39:06 -040075#endif
76
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050077// MISC
78
79#include <iostream>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040080#include <utility>
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050081
Kevin Lubickfaef5142018-06-07 10:33:11 -040082DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets.");
83
Hal Canary24ac42b2017-02-14 13:35:14 -050084// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050085// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040086// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050087
Hal Canary1e0138b2017-03-10 13:56:08 -050088// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
89// In fact, all make_fuzz_foo() functions have this potential problem.
90// Use sequence points!
91template <typename T>
92inline T make_fuzz_t(Fuzz* fuzz) {
93 T t;
94 fuzz->next(&t);
95 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050096}
97
Hal Canary1e0138b2017-03-10 13:56:08 -050098static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -050099
Hal Canary1e0138b2017-03-10 13:56:08 -0500100static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500101
Hal Canary1e0138b2017-03-10 13:56:08 -0500102static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500103
Hal Canary1e0138b2017-03-10 13:56:08 -0500104static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500105 if (depth <= 0) {
106 return nullptr;
107 }
108 int colorFilterType;
109 fuzz->nextRange(&colorFilterType, 0, 8);
110 switch (colorFilterType) {
111 case 0:
112 return nullptr;
113 case 1: {
114 SkColor color;
115 SkBlendMode mode;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400116 fuzz->next(&color);
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500117 fuzz->nextRange(&mode, 0, SkBlendMode::kLastMode);
Hal Canary27bece82017-03-07 16:23:20 -0500118 return SkColorFilter::MakeModeFilter(color, mode);
119 }
120 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500121 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500122 if (!outer) {
123 return nullptr;
124 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500125 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500126 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500127 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500128 }
129 case 3: {
130 SkScalar array[20];
131 fuzz->nextN(array, SK_ARRAY_COUNT(array));
132 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
133 }
134 case 4: {
135 SkColor mul, add;
136 fuzz->next(&mul, &add);
137 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
138 }
139 case 5: {
140 bool grayscale;
141 int invertStyle;
142 float contrast;
143 fuzz->next(&grayscale);
144 fuzz->nextRange(&invertStyle, 0, 2);
145 fuzz->nextRange(&contrast, -1.0f, 1.0f);
146 return SkHighContrastFilter::Make(SkHighContrastConfig(
147 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
148 }
149 case 6:
150 return SkLumaColorFilter::Make();
151 case 7: {
152 uint8_t table[256];
153 fuzz->nextN(table, SK_ARRAY_COUNT(table));
154 return SkTableColorFilter::Make(table);
155 }
156 case 8: {
157 uint8_t tableA[256];
158 uint8_t tableR[256];
159 uint8_t tableG[256];
160 uint8_t tableB[256];
161 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
162 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
163 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
164 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
165 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
166 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500167 default:
168 SkASSERT(false);
169 break;
Hal Canary27bece82017-03-07 16:23:20 -0500170 }
171 return nullptr;
172}
Hal Canary24ac42b2017-02-14 13:35:14 -0500173
Hal Canary1e0138b2017-03-10 13:56:08 -0500174static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500175 SkScalar totalPos = 0;
176 for (int i = 0; i < colorCount; ++i) {
177 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
178 totalPos += pos[i];
179 }
180 totalPos = 1.0f / totalPos;
181 for (int i = 0; i < colorCount; ++i) {
182 pos[i] *= totalPos;
183 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500184 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500185 pos[colorCount - 1] = 1.0f;
186}
187
Hal Canary1e0138b2017-03-10 13:56:08 -0500188static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500189 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
190 sk_sp<SkColorFilter> colorFilter(nullptr);
191 SkBitmap bitmap;
192 sk_sp<SkImage> img;
193 SkShader::TileMode tmX, tmY;
194 bool useMatrix;
195 SkColor color;
196 SkMatrix matrix;
197 SkBlendMode blendMode;
198 int shaderType;
199 if (depth <= 0) {
200 return nullptr;
201 }
Hal Canary671e4422017-02-27 13:36:38 -0500202 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500203 switch (shaderType) {
204 case 0:
205 return nullptr;
206 case 1:
207 return SkShader::MakeEmptyShader();
208 case 2:
209 fuzz->next(&color);
210 return SkShader::MakeColorShader(color);
211 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500212 img = make_fuzz_image(fuzz);
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500213 fuzz->nextRange(&tmX, 0, SkShader::TileMode::kLast_TileMode);
214 fuzz->nextRange(&tmY, 0, SkShader::TileMode::kLast_TileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400215 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500216 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400217 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500218 }
219 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
220 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500221 bitmap = make_fuzz_bitmap(fuzz);
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500222 fuzz->nextRange(&tmX, 0, SkShader::TileMode::kLast_TileMode);
223 fuzz->nextRange(&tmY, 0, SkShader::TileMode::kLast_TileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400224 fuzz->next(&useMatrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500225 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400226 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500227 }
228 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
229 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500230 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400231 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500232 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
233 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500234 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
235 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500236 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
237 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500238 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
239 shader2 = make_fuzz_shader(fuzz, depth - 1);
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500240 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500241 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500242 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500243 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500244 bool useTile;
245 SkRect tile;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500246 fuzz->nextRange(&tmX, 0, SkShader::TileMode::kLast_TileMode);
247 fuzz->nextRange(&tmY, 0, SkShader::TileMode::kLast_TileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400248 fuzz->next(&useMatrix, &useTile);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500249 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400250 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500251 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500252 if (useTile) {
253 fuzz->next(&tile);
254 }
255 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
256 useMatrix ? &matrix : nullptr,
257 useTile ? &tile : nullptr);
258 }
Hal Canary671e4422017-02-27 13:36:38 -0500259 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500260 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000261 // Deprecated SkGaussianEdgeShader
262 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500263 case 10: {
264 constexpr int kMaxColors = 12;
265 SkPoint pts[2];
266 SkColor colors[kMaxColors];
267 SkScalar pos[kMaxColors];
268 int colorCount;
269 bool usePos;
270 fuzz->nextN(pts, 2);
271 fuzz->nextRange(&colorCount, 2, kMaxColors);
272 fuzz->nextN(colors, colorCount);
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500273 fuzz->nextRange(&tmX, 0, SkShader::TileMode::kLast_TileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400274 fuzz->next(&useMatrix, &usePos);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500275 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400276 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500277 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500278 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500279 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500280 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500281 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
282 tmX, 0, useMatrix ? &matrix : nullptr);
283 }
284 case 11: {
285 constexpr int kMaxColors = 12;
286 SkPoint center;
287 SkScalar radius;
288 int colorCount;
289 bool usePos;
290 SkColor colors[kMaxColors];
291 SkScalar pos[kMaxColors];
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500292 fuzz->nextRange(&tmX, 0, SkShader::TileMode::kLast_TileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400293 fuzz->next(&useMatrix, &usePos, &center, &radius);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500294 fuzz->nextRange(&colorCount, 2, kMaxColors);
295 fuzz->nextN(colors, colorCount);
296 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400297 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500298 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500299 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500300 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500301 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500302 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
303 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
304 }
305 case 12: {
306 constexpr int kMaxColors = 12;
307 SkPoint start, end;
308 SkScalar startRadius, endRadius;
309 int colorCount;
310 bool usePos;
311 SkColor colors[kMaxColors];
312 SkScalar pos[kMaxColors];
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500313 fuzz->nextRange(&tmX, 0, SkShader::TileMode::kLast_TileMode);
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400314 fuzz->next(&useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500315 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 Canary24ac42b2017-02-14 13:35:14 -0500319 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500320 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::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
324 usePos ? pos : nullptr, colorCount, tmX, 0,
325 useMatrix ? &matrix : nullptr);
326 }
327 case 13: {
328 constexpr int kMaxColors = 12;
329 SkScalar cx, cy;
330 int colorCount;
331 bool usePos;
332 SkColor colors[kMaxColors];
333 SkScalar pos[kMaxColors];
334 fuzz->next(&cx, &cy, &useMatrix, &usePos);
335 fuzz->nextRange(&colorCount, 2, kMaxColors);
336 fuzz->nextN(colors, colorCount);
337 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400338 FuzzNiceMatrix(fuzz, &matrix);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500339 }
340 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500341 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500342 }
343 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
344 0, useMatrix ? &matrix : nullptr);
345 }
346 case 14: {
347 SkScalar baseFrequencyX, baseFrequencyY, seed;
348 int numOctaves;
349 SkISize tileSize;
350 bool useTileSize, turbulence;
351 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
352 if (useTileSize) {
353 fuzz->next(&tileSize);
354 }
355 fuzz->nextRange(&numOctaves, 2, 7);
356 if (turbulence) {
357 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
358 numOctaves, seed,
359 useTileSize ? &tileSize : nullptr);
360 } else {
361 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
362 numOctaves, seed,
363 useTileSize ? &tileSize : nullptr);
364 }
365 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500366 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500367 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500368 break;
369 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500370 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500371}
372
Hal Canary1e0138b2017-03-10 13:56:08 -0500373static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500374 if (depth <= 0) {
375 return nullptr;
376 }
377 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400378 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500379 switch (pathEffectType) {
380 case 0: {
381 return nullptr;
382 }
383 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500384 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
385 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500386 return SkPathEffect::MakeSum(std::move(first), std::move(second));
387 }
388 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500389 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
390 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500391 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
392 }
393 case 3: {
394 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400395 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500396 SkScalar advance, phase;
397 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500398 SkPath1DPathEffect::Style style;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500399 fuzz->nextRange(&style, 0, SkPath1DPathEffect::kLastEnum_Style);
Hal Canaryf7005202017-03-10 08:48:28 -0500400 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500401 }
402 case 4: {
403 SkScalar width;
404 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400405 fuzz->next(&width);
406 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500407 return SkLine2DPathEffect::Make(width, matrix);
408 }
409 case 5: {
410 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -0400411 FuzzNicePath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500412 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400413 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary5395c592017-03-08 16:52:18 -0500414 return SkPath2DPathEffect::Make(matrix, path);
415 }
416 case 6: {
417 SkScalar radius;
418 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500419 return SkCornerPathEffect::Make(radius);
420 }
Mike Reed40e7e652017-07-22 22:12:59 -0400421 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500422 SkScalar phase;
423 fuzz->next(&phase);
424 SkScalar intervals[20];
425 int count;
426 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
427 fuzz->nextN(intervals, count);
428 return SkDashPathEffect::Make(intervals, count, phase);
429 }
Mike Reed40e7e652017-07-22 22:12:59 -0400430 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500431 SkScalar segLength, dev;
432 uint32_t seed;
433 fuzz->next(&segLength, &dev, &seed);
434 return SkDiscretePathEffect::Make(segLength, dev, seed);
435 }
436 default:
437 SkASSERT(false);
438 return nullptr;
439 }
440}
Hal Canary24ac42b2017-02-14 13:35:14 -0500441
Hal Canary1e0138b2017-03-10 13:56:08 -0500442static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500443 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400444 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500445 switch (maskfilterType) {
446 case 0:
447 return nullptr;
448 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500449 SkBlurStyle blurStyle;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500450 fuzz->nextRange(&blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500451 SkScalar sigma;
452 fuzz->next(&sigma);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400453 bool respectCTM;
454 fuzz->next(&respectCTM);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400455 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500456 }
Hal Canary5395c592017-03-08 16:52:18 -0500457 default:
458 SkASSERT(false);
459 return nullptr;
460 }
461}
Hal Canary24ac42b2017-02-14 13:35:14 -0500462
Hal Canary1e0138b2017-03-10 13:56:08 -0500463static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
464 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500465 return nullptr;
466 }
467 auto fontMugger = SkFontMgr::RefDefault();
468 SkASSERT(fontMugger);
469 int familyCount = fontMugger->countFamilies();
470 int i, j;
471 fuzz->nextRange(&i, 0, familyCount - 1);
472 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
473 int styleCount = family->count();
474 fuzz->nextRange(&j, 0, styleCount - 1);
475 return sk_sp<SkTypeface>(family->createTypeface(j));
476}
Hal Canary24ac42b2017-02-14 13:35:14 -0500477
Hal Canary1e0138b2017-03-10 13:56:08 -0500478static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500479
480static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
481 if (depth <= 0) {
482 return nullptr;
483 }
484 uint8_t imageFilterType;
485 fuzz->nextRange(&imageFilterType, 1, 6);
486 SkPoint3 p, q;
487 SkColor lightColor;
488 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
489 sk_sp<SkImageFilter> input;
490 SkImageFilter::CropRect cropRect;
491 bool useCropRect;
492 fuzz->next(&useCropRect);
493 if (useCropRect) {
494 fuzz->next(&cropRect);
495 }
496 switch (imageFilterType) {
497 case 1:
498 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500499 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500500 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
501 std::move(input),
502 useCropRect ? &cropRect : nullptr);
503 case 2:
504 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500505 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500506 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
507 std::move(input),
508 useCropRect ? &cropRect : nullptr);
509 case 3:
510 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500511 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500512 return SkLightingImageFilter::MakeSpotLitDiffuse(
513 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
514 std::move(input), useCropRect ? &cropRect : nullptr);
515 case 4:
516 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500517 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500518 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
519 shininess, std::move(input),
520 useCropRect ? &cropRect : nullptr);
521 case 5:
522 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500523 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500524 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
525 shininess, std::move(input),
526 useCropRect ? &cropRect : nullptr);
527 case 6:
528 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
529 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500530 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500531 return SkLightingImageFilter::MakeSpotLitSpecular(
532 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
533 std::move(input), useCropRect ? &cropRect : nullptr);
534 default:
535 SkASSERT(false);
536 return nullptr;
537 }
538}
539
Hal Canary1e0138b2017-03-10 13:56:08 -0500540static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500541
Hal Canary1e0138b2017-03-10 13:56:08 -0500542static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500543 if (depth <= 0) {
544 return nullptr;
545 }
546 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500547 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500548 switch (imageFilterType) {
549 case 0:
550 return nullptr;
551 case 1: {
552 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500553 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500554 bool useCropRect;
555 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400556 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500557 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400558 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500559 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400560 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500561 useCropRect ? &cropRect : nullptr);
562 }
563 case 2: {
564 SkMatrix matrix;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400565 FuzzNiceMatrix(fuzz, &matrix);
Hal Canarye03c3e52017-03-09 11:33:35 -0500566 SkFilterQuality quality;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500567 fuzz->nextRange(&quality, 0, SkFilterQuality::kLast_SkFilterQuality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500568 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500569 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
570 }
571 case 3: {
572 SkRegion region;
573 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500574 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500575 bool useCropRect;
576 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400577 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500578 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400579 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500580 }
581 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
582 useCropRect ? &cropRect : nullptr);
583 }
584 case 4: {
585 float k1, k2, k3, k4;
586 bool enforcePMColor;
587 bool useCropRect;
588 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500589 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
590 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500591 SkImageFilter::CropRect cropRect;
592 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400593 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500594 }
595 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
596 std::move(background), std::move(foreground),
597 useCropRect ? &cropRect : nullptr);
598 }
599 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500600 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
601 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500602 bool useCropRect;
603 SkImageFilter::CropRect cropRect;
604 fuzz->next(&useCropRect);
605 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400606 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500607 }
608 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
609 useCropRect ? &cropRect : nullptr);
610 }
611 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500612 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
613 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500614 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
615 }
616 case 7: {
617 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500618 fuzz->nextRange(&xChannelSelector, 1, 4);
619 fuzz->nextRange(&yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500620 SkScalar scale;
621 bool useCropRect;
622 fuzz->next(&scale, &useCropRect);
623 SkImageFilter::CropRect cropRect;
624 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400625 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500626 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500627 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
628 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500629 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
630 std::move(displacement), std::move(color),
631 useCropRect ? &cropRect : nullptr);
632 }
633 case 8: {
634 SkScalar dx, dy, sigmaX, sigmaY;
635 SkColor color;
636 SkDropShadowImageFilter::ShadowMode shadowMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500637 fuzz->nextRange(&shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500638 bool useCropRect;
639 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
640 SkImageFilter::CropRect cropRect;
641 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400642 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500643 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500644 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500645 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
646 std::move(input),
647 useCropRect ? &cropRect : nullptr);
648 }
649 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500650 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500651 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500652 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500653 SkRect srcRect, dstRect;
654 SkFilterQuality filterQuality;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400655 fuzz->next(&srcRect, &dstRect);
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500656 fuzz->nextRange(&filterQuality, 0, SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500657 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
658 }
659 case 11:
660 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
661 case 12: {
662 SkRect srcRect;
663 SkScalar inset;
664 bool useCropRect;
665 SkImageFilter::CropRect cropRect;
666 fuzz->next(&srcRect, &inset, &useCropRect);
667 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400668 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500669 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500670 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500671 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
672 useCropRect ? &cropRect : nullptr);
673 }
674 case 13: {
675 constexpr int kMaxKernelSize = 5;
676 int32_t n, m;
677 fuzz->nextRange(&n, 1, kMaxKernelSize);
678 fuzz->nextRange(&m, 1, kMaxKernelSize);
679 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
680 fuzz->nextN(kernel, n * m);
681 int32_t offsetX, offsetY;
682 fuzz->nextRange(&offsetX, 0, n - 1);
683 fuzz->nextRange(&offsetY, 0, m - 1);
684 SkScalar gain, bias;
685 bool convolveAlpha, useCropRect;
686 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
687 SkMatrixConvolutionImageFilter::TileMode tileMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500688 fuzz->nextRange(&tileMode, 0, SkMatrixConvolutionImageFilter::TileMode::kLast_TileMode);
Hal Canarye03c3e52017-03-09 11:33:35 -0500689 SkImageFilter::CropRect cropRect;
690 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400691 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500692 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500693 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500694 return SkMatrixConvolutionImageFilter::Make(
695 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
696 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
697 }
698 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500699 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
700 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500701 bool useCropRect;
702 fuzz->next(&useCropRect);
703 SkImageFilter::CropRect cropRect;
704 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400705 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500706 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400707 return SkMergeImageFilter::Make(std::move(first), std::move(second),
708 useCropRect ? &cropRect : nullptr);
709 }
710 case 15: {
711 constexpr int kMaxCount = 4;
712 sk_sp<SkImageFilter> ifs[kMaxCount];
713 int count;
714 fuzz->nextRange(&count, 1, kMaxCount);
715 for (int i = 0; i < count; ++i) {
716 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
717 }
718 bool useCropRect;
719 fuzz->next(&useCropRect);
720 SkImageFilter::CropRect cropRect;
721 if (useCropRect) {
722 fuzz->next(&cropRect);
723 }
724 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500725 }
726 case 16: {
727 int rx, ry;
728 fuzz->next(&rx, &ry);
729 bool useCropRect;
730 fuzz->next(&useCropRect);
731 SkImageFilter::CropRect cropRect;
732 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400733 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500734 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500735 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500736 return SkDilateImageFilter::Make(rx, ry, std::move(input),
737 useCropRect ? &cropRect : nullptr);
738 }
739 case 17: {
740 int rx, ry;
741 fuzz->next(&rx, &ry);
742 bool useCropRect;
743 fuzz->next(&useCropRect);
744 SkImageFilter::CropRect cropRect;
745 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400746 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500747 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500748 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500749 return SkErodeImageFilter::Make(rx, ry, std::move(input),
750 useCropRect ? &cropRect : nullptr);
751 }
752 case 18: {
753 SkScalar dx, dy;
754 fuzz->next(&dx, &dy);
755 bool useCropRect;
756 fuzz->next(&useCropRect);
757 SkImageFilter::CropRect cropRect;
758 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400759 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500760 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500761 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500762 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
763 useCropRect ? &cropRect : nullptr);
764 }
765 case 19: {
766 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500767 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500768 bool useCropRect;
769 fuzz->next(&useCropRect);
770 SkImageFilter::CropRect cropRect;
771 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400772 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500773 }
774 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
775 }
776 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500777 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500778 return SkPictureImageFilter::Make(std::move(picture));
779 }
780 case 21: {
781 SkRect cropRect;
782 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500783 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500784 return SkPictureImageFilter::Make(std::move(picture), cropRect);
785 }
786 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500787 SkRect src, dst;
788 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500789 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500790 return SkTileImageFilter::Make(src, dst, std::move(input));
791 }
Mike Reed77e487d2017-11-09 21:50:20 +0000792 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500793 SkBlendMode blendMode;
794 bool useCropRect;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400795 fuzz->next(&useCropRect);
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500796 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canarye03c3e52017-03-09 11:33:35 -0500797 SkImageFilter::CropRect cropRect;
798 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400799 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500800 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500801 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
802 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500803 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
804 useCropRect ? &cropRect : nullptr);
805 }
806 default:
807 SkASSERT(false);
808 return nullptr;
809 }
810}
Hal Canary24ac42b2017-02-14 13:35:14 -0500811
Hal Canary1e0138b2017-03-10 13:56:08 -0500812static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500813 int w, h;
814 fuzz->nextRange(&w, 1, 1024);
815 fuzz->nextRange(&h, 1, 1024);
816 SkAutoTMalloc<SkPMColor> data(w * h);
817 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
818 int n = w * h;
819 for (int i = 0; i < n; ++i) {
820 SkColor c;
821 fuzz->next(&c);
822 data[i] = SkPreMultiplyColor(c);
823 }
824 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500825 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
826 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500827}
828
Hal Canary1e0138b2017-03-10 13:56:08 -0500829static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500830 SkBitmap bitmap;
831 int w, h;
832 fuzz->nextRange(&w, 1, 1024);
833 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500834 if (!bitmap.tryAllocN32Pixels(w, h)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400835 SkDEBUGF("Could not allocate pixels %d x %d", w, h);
Kevin Lubick1991f552018-02-27 10:59:10 -0500836 return bitmap;
837 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500838 for (int y = 0; y < h; ++y) {
839 for (int x = 0; x < w; ++x) {
840 SkColor c;
841 fuzz->next(&c);
842 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
843 }
844 }
845 return bitmap;
846}
847
Hal Canary1e0138b2017-03-10 13:56:08 -0500848template <typename T, typename Min, typename Max>
849inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
850 T value;
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500851 fuzz->nextRange(&value, minv, maxv);
Hal Canary1e0138b2017-03-10 13:56:08 -0500852 return value;
853}
854
855static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500856 if (!fuzz || !paint || depth <= 0) {
857 return;
858 }
859
Hal Canary1e0138b2017-03-10 13:56:08 -0500860 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
861 paint->setDither( make_fuzz_t<bool>(fuzz));
862 paint->setColor( make_fuzz_t<SkColor>(fuzz));
863 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
864 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
865 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
866 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
867 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
868 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
869 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
870 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500871
872 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500873 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
874 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
875 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
876 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500877 }
878}
879
Mike Reed358fcad2018-11-23 15:27:51 -0500880static SkFont fuzz_font(Fuzz* fuzz) {
881 SkFont font;
882 font.setTypeface( make_fuzz_typeface(fuzz));
883 font.setSize( make_fuzz_t<SkScalar>(fuzz));
884 font.setScaleX( make_fuzz_t<SkScalar>(fuzz));
885 font.setSkewX( make_fuzz_t<SkScalar>(fuzz));
886 font.setLinearMetrics( make_fuzz_t<bool>(fuzz));
887 font.setSubpixel( make_fuzz_t<bool>(fuzz));
888 font.setEmbeddedBitmaps( make_fuzz_t<bool>(fuzz));
889 font.setForceAutoHinting( make_fuzz_t<bool>(fuzz));
890 font.setEmbolden( make_fuzz_t<bool>(fuzz));
891 font.setHinting( make_fuzz_t_range<SkFontHinting>(fuzz, 0, kFull_SkFontHinting));
892 font.setEdging( make_fuzz_t_range<SkFont::Edging>(fuzz, 0,
893 (int)SkFont::Edging::kSubpixelAntiAlias));
894 return font;
Hal Canary5395c592017-03-08 16:52:18 -0500895}
896
Mike Reed2ed78202018-11-21 15:10:08 -0500897static SkTextEncoding fuzz_paint_text_encoding(Fuzz* fuzz) {
898 return make_fuzz_t_range<SkTextEncoding>(fuzz, 0, 3);
Hal Canary24ac42b2017-02-14 13:35:14 -0500899}
900
Hal Canary5395c592017-03-08 16:52:18 -0500901constexpr int kMaxGlyphCount = 30;
902
Mike Reed2ed78202018-11-21 15:10:08 -0500903static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkFont& font, SkTextEncoding encoding) {
Hal Canary671e4422017-02-27 13:36:38 -0500904 SkTDArray<uint8_t> array;
Mike Reed2ed78202018-11-21 15:10:08 -0500905 if (kGlyphID_SkTextEncoding == encoding) {
Herb Derby087fad72019-01-22 14:45:16 -0500906 int glyphRange = font.getTypefaceOrDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500907 if (glyphRange == 0) {
908 // Some fuzzing environments have no fonts, so empty array is the best
909 // we can do.
910 return array;
911 }
Hal Canary671e4422017-02-27 13:36:38 -0500912 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500913 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500914 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
915 for (int i = 0; i < glyphCount; ++i) {
916 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
917 }
918 return array;
919 }
920 static const SkUnichar ranges[][2] = {
921 {0x0020, 0x007F},
922 {0x00A1, 0x0250},
923 {0x0400, 0x0500},
924 };
925 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500926 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500927 count += (ranges[i][1] - ranges[i][0]);
928 }
Hal Canary5395c592017-03-08 16:52:18 -0500929 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500930 SkUnichar buffer[kMaxLength];
931 int length;
932 fuzz->nextRange(&length, 1, kMaxLength);
933 for (int j = 0; j < length; ++j) {
934 int32_t value;
935 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500936 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500937 if (value + ranges[i][0] < ranges[i][1]) {
938 buffer[j] = value + ranges[i][0];
939 break;
940 } else {
941 value -= (ranges[i][1] - ranges[i][0]);
942 }
943 }
944 }
Mike Reed2ed78202018-11-21 15:10:08 -0500945 switch (encoding) {
946 case kUTF8_SkTextEncoding: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500947 size_t utf8len = 0;
948 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400949 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -0500950 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500951 char* ptr = (char*)array.append(utf8len);
952 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400953 ptr += SkUTF::ToUTF8(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -0500954 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500955 } break;
Mike Reed2ed78202018-11-21 15:10:08 -0500956 case kUTF16_SkTextEncoding: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500957 size_t utf16len = 0;
958 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400959 utf16len += SkUTF::ToUTF16(buffer[j]);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500960 }
961 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
962 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400963 ptr += SkUTF::ToUTF16(buffer[j], ptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500964 }
965 } break;
Mike Reed2ed78202018-11-21 15:10:08 -0500966 case kUTF32_SkTextEncoding:
Hal Canary671e4422017-02-27 13:36:38 -0500967 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -0500968 break;
Hal Canary671e4422017-02-27 13:36:38 -0500969 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -0500970 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -0500971 break;
Hal Canary671e4422017-02-27 13:36:38 -0500972 }
973 return array;
974}
975
Hal Canary5395c592017-03-08 16:52:18 -0500976static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
977 SkTextBlobBuilder textBlobBuilder;
978 int8_t runCount;
979 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
980 while (runCount-- > 0) {
Mike Reed2ed78202018-11-21 15:10:08 -0500981 SkFont font;
982 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
983 font.setEdging(make_fuzz_t<bool>(fuzz) ? SkFont::Edging::kAlias : SkFont::Edging::kAntiAlias);
984 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
985 int glyphCount = font.countText(text.begin(), SkToSizeT(text.count()), encoding);
Hal Canary5395c592017-03-08 16:52:18 -0500986 SkASSERT(glyphCount <= kMaxGlyphCount);
987 SkScalar x, y;
988 const SkTextBlobBuilder::RunBuffer* buffer;
989 uint8_t runType;
990 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
Hal Canaryfc97f222018-12-17 13:48:44 -0500991 const void* textPtr = text.begin();
992 size_t textLen = SkToSizeT(text.count());
Hal Canary5395c592017-03-08 16:52:18 -0500993 switch (runType) {
994 case 0:
995 fuzz->next(&x, &y);
996 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500997 buffer = &textBlobBuilder.allocRun(font, glyphCount, x, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500998 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -0500999 break;
1000 case 1:
1001 fuzz->next(&y);
1002 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -05001003 buffer = &textBlobBuilder.allocRunPosH(font, glyphCount, y);
Hal Canaryfc97f222018-12-17 13:48:44 -05001004 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001005 fuzz->nextN(buffer->pos, glyphCount);
1006 break;
1007 case 2:
1008 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -05001009 buffer = &textBlobBuilder.allocRunPos(font, glyphCount);
Hal Canaryfc97f222018-12-17 13:48:44 -05001010 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001011 fuzz->nextN(buffer->pos, glyphCount * 2);
1012 break;
1013 default:
1014 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001015 break;
Hal Canary5395c592017-03-08 16:52:18 -05001016 }
1017 }
1018 return textBlobBuilder.make();
1019}
1020
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001021extern std::atomic<bool> gSkUseDeltaAA;
1022extern std::atomic<bool> gSkForceDeltaAA;
1023
Hal Canary1e0138b2017-03-10 13:56:08 -05001024static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001025 if (!fuzz || !canvas || depth <= 0) {
1026 return;
1027 }
1028 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001029 bool useDAA;
1030 fuzz->next(&useDAA);
1031 if (useDAA) {
1032 gSkForceDeltaAA = true;
1033 gSkUseDeltaAA = true;
1034 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001035 unsigned N;
1036 fuzz->nextRange(&N, 0, 2000);
1037 for (unsigned i = 0; i < N; ++i) {
1038 if (fuzz->exhausted()) {
1039 return;
1040 }
1041 SkPaint paint;
Mike Reed358fcad2018-11-23 15:27:51 -05001042 SkFont font;
Hal Canary24ac42b2017-02-14 13:35:14 -05001043 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001044 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001045 switch (drawCommand) {
1046 case 0:
1047 canvas->flush();
1048 break;
1049 case 1:
1050 canvas->save();
1051 break;
1052 case 2: {
1053 SkRect bounds;
1054 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001055 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001056 canvas->saveLayer(&bounds, &paint);
1057 break;
1058 }
1059 case 3: {
1060 SkRect bounds;
1061 fuzz->next(&bounds);
1062 canvas->saveLayer(&bounds, nullptr);
1063 break;
1064 }
1065 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001066 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001067 canvas->saveLayer(nullptr, &paint);
1068 break;
1069 case 5:
1070 canvas->saveLayer(nullptr, nullptr);
1071 break;
1072 case 6: {
1073 uint8_t alpha;
1074 fuzz->next(&alpha);
1075 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1076 break;
1077 }
1078 case 7: {
1079 SkRect bounds;
1080 uint8_t alpha;
1081 fuzz->next(&bounds, &alpha);
1082 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1083 break;
1084 }
1085 case 8: {
1086 SkCanvas::SaveLayerRec saveLayerRec;
1087 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001088 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001089 fuzz->next(&bounds);
1090 saveLayerRec.fBounds = &bounds;
1091 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001092 if (make_fuzz_t<bool>(fuzz)) {
1093 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001094 saveLayerRec.fPaint = &paint;
1095 }
1096 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001097 if (make_fuzz_t<bool>(fuzz)) {
1098 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001099 saveLayerRec.fBackdrop = imageFilter.get();
1100 }
Hal Canary5395c592017-03-08 16:52:18 -05001101 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001102 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001103 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1104 // }
1105
Hal Canary24ac42b2017-02-14 13:35:14 -05001106 canvas->saveLayer(saveLayerRec);
1107 break;
1108 }
1109 case 9:
1110 canvas->restore();
1111 break;
1112 case 10: {
1113 int saveCount;
1114 fuzz->next(&saveCount);
1115 canvas->restoreToCount(saveCount);
1116 break;
1117 }
1118 case 11: {
1119 SkScalar x, y;
1120 fuzz->next(&x, &y);
1121 canvas->translate(x, y);
1122 break;
1123 }
1124 case 12: {
1125 SkScalar x, y;
1126 fuzz->next(&x, &y);
1127 canvas->scale(x, y);
1128 break;
1129 }
1130 case 13: {
1131 SkScalar v;
1132 fuzz->next(&v);
1133 canvas->rotate(v);
1134 break;
1135 }
1136 case 14: {
1137 SkScalar x, y, v;
1138 fuzz->next(&x, &y, &v);
1139 canvas->rotate(v, x, y);
1140 break;
1141 }
1142 case 15: {
1143 SkScalar x, y;
1144 fuzz->next(&x, &y);
1145 canvas->skew(x, y);
1146 break;
1147 }
1148 case 16: {
1149 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001150 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001151 canvas->concat(mat);
1152 break;
1153 }
1154 case 17: {
1155 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001156 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001157 canvas->setMatrix(mat);
1158 break;
1159 }
1160 case 18:
1161 canvas->resetMatrix();
1162 break;
1163 case 19: {
1164 SkRect r;
1165 int op;
1166 bool doAntiAlias;
1167 fuzz->next(&r, &doAntiAlias);
1168 fuzz->nextRange(&op, 0, 1);
1169 r.sort();
1170 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1171 break;
1172 }
1173 case 20: {
1174 SkRRect rr;
1175 int op;
1176 bool doAntiAlias;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001177 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001178 fuzz->next(&doAntiAlias);
1179 fuzz->nextRange(&op, 0, 1);
1180 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1181 break;
1182 }
1183 case 21: {
1184 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001185 FuzzNicePath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001186 int op;
1187 bool doAntiAlias;
1188 fuzz->next(&doAntiAlias);
1189 fuzz->nextRange(&op, 0, 1);
1190 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1191 break;
1192 }
1193 case 22: {
1194 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001195 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001196 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001197 fuzz->nextRange(&op, 0, 1);
1198 canvas->clipRegion(region, (SkClipOp)op);
1199 break;
1200 }
1201 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001202 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001203 canvas->drawPaint(paint);
1204 break;
1205 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001206 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001207 SkCanvas::PointMode pointMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001208 fuzz->nextRange(&pointMode,
Hal Canaryc8bebd42017-12-20 11:21:05 -05001209 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001210 size_t count;
1211 constexpr int kMaxCount = 30;
1212 fuzz->nextRange(&count, 0, kMaxCount);
1213 SkPoint pts[kMaxCount];
1214 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001215 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001216 break;
1217 }
1218 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001219 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001220 SkRect r;
1221 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001222 if (!r.isFinite()) {
1223 break;
1224 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001225 canvas->drawRect(r, paint);
1226 break;
1227 }
1228 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001229 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001230 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001231 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001232 canvas->drawRegion(region, paint);
1233 break;
1234 }
1235 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001236 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001237 SkRect r;
1238 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001239 if (!r.isFinite()) {
1240 break;
1241 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001242 canvas->drawOval(r, paint);
1243 break;
1244 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001245 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001246 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001247 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001248 SkRRect rr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001249 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001250 canvas->drawRRect(rr, paint);
1251 break;
1252 }
1253 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001254 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001255 SkRRect orr, irr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001256 FuzzNiceRRect(fuzz, &orr);
1257 FuzzNiceRRect(fuzz, &irr);
Hal Canary27bece82017-03-07 16:23:20 -05001258 if (orr.getBounds().contains(irr.getBounds())) {
1259 canvas->drawDRRect(orr, irr, paint);
1260 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001261 break;
1262 }
1263 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001264 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001265 SkRect r;
1266 SkScalar start, sweep;
1267 bool useCenter;
1268 fuzz->next(&r, &start, &sweep, &useCenter);
1269 canvas->drawArc(r, start, sweep, useCenter, paint);
1270 break;
1271 }
1272 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001273 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001274 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001275 FuzzNicePath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001276 canvas->drawPath(path, paint);
1277 break;
1278 }
1279 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001280 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001281 SkScalar left, top;
1282 bool usePaint;
1283 fuzz->next(&left, &top, &usePaint);
1284 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001285 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001286 }
1287 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1288 break;
1289 }
1290 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001291 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001292 SkRect src, dst;
1293 bool usePaint;
1294 fuzz->next(&src, &dst, &usePaint);
1295 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001296 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001297 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001298 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001299 break;
1300 }
1301 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001302 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001303 SkIRect src;
1304 SkRect dst;
1305 bool usePaint;
1306 fuzz->next(&src, &dst, &usePaint);
1307 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001308 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001309 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001310 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001311 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1312 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001313 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1314 break;
1315 }
1316 case 36: {
1317 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001318 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001319 SkRect dst;
1320 fuzz->next(&dst, &usePaint);
1321 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001322 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001323 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001324 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001325 break;
1326 }
1327 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001328 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001329 SkIRect center;
1330 SkRect dst;
1331 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001332 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001333 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001334 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001335 }
Hal Canary0361d492017-03-15 12:58:15 -04001336 if (make_fuzz_t<bool>(fuzz)) {
1337 fuzz->next(&center);
1338 } else { // Make valid center, see SkLatticeIter::Valid().
1339 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1340 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1341 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1342 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1343 }
1344 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001345 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1346 break;
1347 }
1348 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001349 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001350 SkScalar left, top;
1351 bool usePaint;
1352 fuzz->next(&left, &top, &usePaint);
1353 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001354 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001355 }
1356 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1357 break;
1358 }
1359 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001360 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001361 SkRect src, dst;
1362 bool usePaint;
1363 fuzz->next(&src, &dst, &usePaint);
1364 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001365 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001366 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001367 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001368 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1369 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001370 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1371 break;
1372 }
1373 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001374 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001375 SkIRect src;
1376 SkRect dst;
1377 bool usePaint;
1378 fuzz->next(&src, &dst, &usePaint);
1379 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001380 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001381 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001382 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001383 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1384 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001385 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1386 break;
1387 }
1388 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001389 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001390 SkRect dst;
1391 bool usePaint;
1392 fuzz->next(&dst, &usePaint);
1393 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001394 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001395 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001396 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001397 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1398 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001399 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1400 break;
1401 }
1402 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001403 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001404 SkIRect center;
1405 SkRect dst;
1406 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001407 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001408 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001409 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001410 }
Hal Canary0361d492017-03-15 12:58:15 -04001411 if (make_fuzz_t<bool>(fuzz)) {
1412 fuzz->next(&center);
1413 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001414 if (img.width() == 0 || img.height() == 0) {
1415 // bitmap may not have had its pixels initialized.
1416 break;
1417 }
Hal Canary0361d492017-03-15 12:58:15 -04001418 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1419 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1420 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1421 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1422 }
1423 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001424 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1425 break;
1426 }
1427 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001428 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001429 bool usePaint;
1430 SkRect dst;
1431 fuzz->next(&usePaint, &dst);
1432 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001433 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001434 }
1435 constexpr int kMax = 6;
1436 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001437 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001438 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1439 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1440 fuzz->nextN(xDivs, lattice.fXCount);
1441 fuzz->nextN(yDivs, lattice.fYCount);
1442 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1443 break;
1444 }
1445 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001446 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001447 bool usePaint;
1448 SkRect dst;
1449 fuzz->next(&usePaint, &dst);
1450 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001451 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001452 }
1453 constexpr int kMax = 6;
1454 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001455 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001456 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1457 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1458 fuzz->nextN(xDivs, lattice.fXCount);
1459 fuzz->nextN(yDivs, lattice.fYCount);
1460 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1461 break;
1462 }
1463 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001464 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed358fcad2018-11-23 15:27:51 -05001465 font = fuzz_font(fuzz);
1466 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001467 SkScalar x, y;
1468 fuzz->next(&x, &y);
Mike Reed358fcad2018-11-23 15:27:51 -05001469 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
1470 canvas->drawSimpleText(text.begin(), SkToSizeT(text.count()), encoding, x, y,
1471 font, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001472 break;
1473 }
1474 case 46: {
Mike Reed212e9062018-12-25 17:35:49 -05001475 // was drawPosText
Hal Canary24ac42b2017-02-14 13:35:14 -05001476 break;
1477 }
1478 case 47: {
Mike Reed212e9062018-12-25 17:35:49 -05001479 // was drawPosTextH
Hal Canary24ac42b2017-02-14 13:35:14 -05001480 break;
1481 }
1482 case 48: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001483 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001484 break;
1485 }
1486 case 49: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001487 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001488 break;
1489 }
1490 case 50: {
Mike Reed212e9062018-12-25 17:35:49 -05001491 // was drawTextRSXform
Hal Canary24ac42b2017-02-14 13:35:14 -05001492 break;
1493 }
1494 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001495 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001496 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001497 SkScalar x, y;
1498 fuzz->next(&x, &y);
1499 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001500 break;
1501 }
1502 case 52: {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001503 SkMatrix matrix;
Hal Canary24ac42b2017-02-14 13:35:14 -05001504 bool usePaint, useMatrix;
1505 fuzz->next(&usePaint, &useMatrix);
1506 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001507 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001508 }
1509 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001510 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -05001511 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001512 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001513 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1514 usePaint ? &paint : nullptr);
1515 break;
1516 }
1517 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001518 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001519 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001520 SkBlendMode blendMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001521 fuzz->nextRange(&vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
1522 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001523 constexpr int kMaxCount = 100;
1524 int vertexCount;
1525 SkPoint vertices[kMaxCount];
1526 SkPoint texs[kMaxCount];
1527 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001528 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1529 fuzz->nextN(vertices, vertexCount);
1530 bool useTexs, useColors;
1531 fuzz->next(&useTexs, &useColors);
1532 if (useTexs) {
1533 fuzz->nextN(texs, vertexCount);
1534 }
1535 if (useColors) {
1536 fuzz->nextN(colors, vertexCount);
1537 }
1538 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001539 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001540 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001541 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1542 for (int i = 0; i < indexCount; ++i) {
1543 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1544 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001545 }
Mike Reed887cdf12017-04-03 11:11:09 -04001546 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1547 useTexs ? texs : nullptr,
1548 useColors ? colors : nullptr,
1549 indexCount, indices),
1550 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001551 break;
1552 }
1553 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001554 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001555 break;
1556 }
1557 }
1558}
1559
Hal Canary1e0138b2017-03-10 13:56:08 -05001560static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001561 SkScalar w, h;
1562 fuzz->next(&w, &h);
1563 SkPictureRecorder pictureRecorder;
1564 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1565 return pictureRecorder.finishRecordingAsPicture();
1566}
1567
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001568DEF_FUZZ(NullCanvas, fuzz) {
1569 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001570}
1571
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001572constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001573
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001574DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001575 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001576 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001577 fuzz_canvas(fuzz, surface->getCanvas());
1578}
1579
Hal Canarye2924432017-12-01 11:46:26 -05001580DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1581 SkPictureRecorder recorder;
1582 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1583 SkIntToScalar(kCanvasSize.height())));
1584 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1585 if (!pic) { fuzz->signalBug(); }
1586 sk_sp<SkData> data = pic->serialize();
1587 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001588 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001589 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001590 if (!deserialized) { fuzz->signalBug(); }
1591 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1592 SkASSERT(surface && surface->getCanvas());
1593 surface->getCanvas()->drawPicture(deserialized);
1594}
1595
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001596DEF_FUZZ(ImageFilter, fuzz) {
1597 auto fil = make_fuzz_imageFilter(fuzz, 20);
1598
1599 SkPaint paint;
1600 paint.setImageFilter(fil);
1601 SkBitmap bitmap;
1602 SkCanvas canvas(bitmap);
1603 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1604}
1605
1606
1607//SkRandom _rand;
1608#define SK_ADD_RANDOM_BIT_FLIPS
1609
1610DEF_FUZZ(SerializedImageFilter, fuzz) {
1611 auto filter = make_fuzz_imageFilter(fuzz, 20);
Robert Phillips66f6ef42018-09-14 11:58:40 -04001612 if (!filter) {
1613 return;
1614 }
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001615 auto data = filter->serialize();
1616 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1617 size_t len = data->size();
1618#ifdef SK_ADD_RANDOM_BIT_FLIPS
1619 unsigned char* p = const_cast<unsigned char*>(ptr);
1620 for (size_t i = 0; i < len; ++i, ++p) {
1621 uint8_t j;
1622 fuzz->nextRange(&j, 1, 250);
1623 if (j == 1) { // 0.4% of the time, flip a bit or byte
1624 uint8_t k;
1625 fuzz->nextRange(&k, 1, 10);
1626 if (k == 1) { // Then 10% of the time, change a whole byte
1627 uint8_t s;
1628 fuzz->nextRange(&s, 0, 2);
1629 switch(s) {
1630 case 0:
1631 *p ^= 0xFF; // Flip entire byte
1632 break;
1633 case 1:
1634 *p = 0xFF; // Set all bits to 1
1635 break;
1636 case 2:
1637 *p = 0x00; // Set all bits to 0
1638 break;
1639 }
1640 } else {
1641 uint8_t s;
1642 fuzz->nextRange(&s, 0, 7);
1643 *p ^= (1 << 7);
1644 }
1645 }
1646 }
1647#endif // SK_ADD_RANDOM_BIT_FLIPS
1648 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1649
1650 // uncomment below to write out a serialized image filter (to make corpus
1651 // for -t filter_fuzz)
1652 // SkString s("./serialized_filters/sf");
1653 // s.appendU32(_rand.nextU());
1654 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1655 // sk_fwrite(data->bytes(), data->size(), file);
1656 // sk_fclose(file);
1657
1658 SkPaint paint;
1659 paint.setImageFilter(deserializedFil);
1660 SkBitmap bitmap;
1661 SkCanvas canvas(bitmap);
1662 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1663}
1664
Hal Canary44801ca2017-03-15 11:39:06 -04001665#if SK_SUPPORT_GPU
Kevin Lubickfaef5142018-06-07 10:33:11 -04001666
1667static void dump_GPU_info(GrContext* context) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001668 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->priv().getGpu())
Kevin Lubickfaef5142018-06-07 10:33:11 -04001669 ->glInterface();
1670 const GrGLubyte* output;
1671 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1672 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1673
1674 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1675 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1676
1677 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1678 SkDebugf("GL_VERSION %s\n", (const char*) output);
1679}
1680
Hal Canary5aa91582017-03-21 11:11:44 -07001681static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1682 SkASSERT(context);
1683 auto surface = SkSurface::MakeRenderTarget(
1684 context,
1685 SkBudgeted::kNo,
1686 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1687 SkASSERT(surface && surface->getCanvas());
1688 fuzz_canvas(fuzz, surface->getCanvas());
1689}
1690
Hal Canary44801ca2017-03-15 11:39:06 -04001691DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001692 sk_gpu_test::GrContextFactory f;
1693 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001694 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001695 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001696 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001697 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001698 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001699 }
Hal Canary5aa91582017-03-21 11:11:44 -07001700 fuzz_ganesh(fuzz, context);
1701}
1702
Kevin Lubick27d42192018-04-03 12:30:32 -04001703// This target is deprecated, NullGLContext is not well maintained.
1704// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001705DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001706 sk_gpu_test::GrContextFactory f;
1707 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001708}
1709
Kevin Lubick27d42192018-04-03 12:30:32 -04001710DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001711 sk_gpu_test::GrContextFactory f;
1712 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1713}
Hal Canary44801ca2017-03-15 11:39:06 -04001714#endif
1715
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001716DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001717 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -04001718 auto doc = SkPDF::MakeDocument(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001719 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1720 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001721}
1722
1723// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1724DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001725 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001726 fuzz_canvas(fuzz, &debugCanvas);
1727 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1728 UrlDataManager dataManager(SkString("data"));
Brian Osmand8a90f92019-01-28 13:41:19 -05001729 SkDynamicMemoryWStream stream;
1730 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1731 writer.beginObject(); // root
1732 debugCanvas.toJSON(writer, dataManager, debugCanvas.getSize(), nullCanvas.get());
1733 writer.endObject(); // root
1734 writer.flush();
1735 sk_sp<SkData> json = stream.detachAsData();
1736 fwrite(json->data(), json->size(), 1, stdout);
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001737}