blob: 21918d9f045f3434365c197f05b3692a92a9dc11 [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) {
906 int glyphRange = font.getTypeface() ? font.getTypeface()->countGlyphs()
Hal Canaryb69c4b82017-03-08 11:02:40 -0500907 : SkTypeface::MakeDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500908 if (glyphRange == 0) {
909 // Some fuzzing environments have no fonts, so empty array is the best
910 // we can do.
911 return array;
912 }
Hal Canary671e4422017-02-27 13:36:38 -0500913 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500914 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500915 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
916 for (int i = 0; i < glyphCount; ++i) {
917 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
918 }
919 return array;
920 }
921 static const SkUnichar ranges[][2] = {
922 {0x0020, 0x007F},
923 {0x00A1, 0x0250},
924 {0x0400, 0x0500},
925 };
926 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500927 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500928 count += (ranges[i][1] - ranges[i][0]);
929 }
Hal Canary5395c592017-03-08 16:52:18 -0500930 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500931 SkUnichar buffer[kMaxLength];
932 int length;
933 fuzz->nextRange(&length, 1, kMaxLength);
934 for (int j = 0; j < length; ++j) {
935 int32_t value;
936 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500937 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500938 if (value + ranges[i][0] < ranges[i][1]) {
939 buffer[j] = value + ranges[i][0];
940 break;
941 } else {
942 value -= (ranges[i][1] - ranges[i][0]);
943 }
944 }
945 }
Mike Reed2ed78202018-11-21 15:10:08 -0500946 switch (encoding) {
947 case kUTF8_SkTextEncoding: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500948 size_t utf8len = 0;
949 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400950 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -0500951 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500952 char* ptr = (char*)array.append(utf8len);
953 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400954 ptr += SkUTF::ToUTF8(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -0500955 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500956 } break;
Mike Reed2ed78202018-11-21 15:10:08 -0500957 case kUTF16_SkTextEncoding: {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500958 size_t utf16len = 0;
959 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400960 utf16len += SkUTF::ToUTF16(buffer[j]);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500961 }
962 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
963 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -0400964 ptr += SkUTF::ToUTF16(buffer[j], ptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500965 }
966 } break;
Mike Reed2ed78202018-11-21 15:10:08 -0500967 case kUTF32_SkTextEncoding:
Hal Canary671e4422017-02-27 13:36:38 -0500968 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -0500969 break;
Hal Canary671e4422017-02-27 13:36:38 -0500970 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -0500971 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -0500972 break;
Hal Canary671e4422017-02-27 13:36:38 -0500973 }
974 return array;
975}
976
Hal Canary5395c592017-03-08 16:52:18 -0500977static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
978 SkTextBlobBuilder textBlobBuilder;
979 int8_t runCount;
980 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
981 while (runCount-- > 0) {
Mike Reed2ed78202018-11-21 15:10:08 -0500982 SkFont font;
983 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
984 font.setEdging(make_fuzz_t<bool>(fuzz) ? SkFont::Edging::kAlias : SkFont::Edging::kAntiAlias);
985 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
986 int glyphCount = font.countText(text.begin(), SkToSizeT(text.count()), encoding);
Hal Canary5395c592017-03-08 16:52:18 -0500987 SkASSERT(glyphCount <= kMaxGlyphCount);
988 SkScalar x, y;
989 const SkTextBlobBuilder::RunBuffer* buffer;
990 uint8_t runType;
991 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
Hal Canaryfc97f222018-12-17 13:48:44 -0500992 const void* textPtr = text.begin();
993 size_t textLen = SkToSizeT(text.count());
Hal Canary5395c592017-03-08 16:52:18 -0500994 switch (runType) {
995 case 0:
996 fuzz->next(&x, &y);
997 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -0500998 buffer = &textBlobBuilder.allocRun(font, glyphCount, x, y);
Hal Canaryfc97f222018-12-17 13:48:44 -0500999 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001000 break;
1001 case 1:
1002 fuzz->next(&y);
1003 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -05001004 buffer = &textBlobBuilder.allocRunPosH(font, glyphCount, y);
Hal Canaryfc97f222018-12-17 13:48:44 -05001005 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001006 fuzz->nextN(buffer->pos, glyphCount);
1007 break;
1008 case 2:
1009 // TODO: Test other variations of this.
Mike Reed2ed78202018-11-21 15:10:08 -05001010 buffer = &textBlobBuilder.allocRunPos(font, glyphCount);
Hal Canaryfc97f222018-12-17 13:48:44 -05001011 (void)font.textToGlyphs(textPtr, textLen, encoding, buffer->glyphs, glyphCount);
Hal Canary5395c592017-03-08 16:52:18 -05001012 fuzz->nextN(buffer->pos, glyphCount * 2);
1013 break;
1014 default:
1015 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001016 break;
Hal Canary5395c592017-03-08 16:52:18 -05001017 }
1018 }
1019 return textBlobBuilder.make();
1020}
1021
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001022extern std::atomic<bool> gSkUseDeltaAA;
1023extern std::atomic<bool> gSkForceDeltaAA;
1024
Hal Canary1e0138b2017-03-10 13:56:08 -05001025static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001026 if (!fuzz || !canvas || depth <= 0) {
1027 return;
1028 }
1029 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001030 bool useDAA;
1031 fuzz->next(&useDAA);
1032 if (useDAA) {
1033 gSkForceDeltaAA = true;
1034 gSkUseDeltaAA = true;
1035 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001036 unsigned N;
1037 fuzz->nextRange(&N, 0, 2000);
1038 for (unsigned i = 0; i < N; ++i) {
1039 if (fuzz->exhausted()) {
1040 return;
1041 }
1042 SkPaint paint;
Mike Reed358fcad2018-11-23 15:27:51 -05001043 SkFont font;
Hal Canary24ac42b2017-02-14 13:35:14 -05001044 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001045 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001046 switch (drawCommand) {
1047 case 0:
1048 canvas->flush();
1049 break;
1050 case 1:
1051 canvas->save();
1052 break;
1053 case 2: {
1054 SkRect bounds;
1055 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001056 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001057 canvas->saveLayer(&bounds, &paint);
1058 break;
1059 }
1060 case 3: {
1061 SkRect bounds;
1062 fuzz->next(&bounds);
1063 canvas->saveLayer(&bounds, nullptr);
1064 break;
1065 }
1066 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001067 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001068 canvas->saveLayer(nullptr, &paint);
1069 break;
1070 case 5:
1071 canvas->saveLayer(nullptr, nullptr);
1072 break;
1073 case 6: {
1074 uint8_t alpha;
1075 fuzz->next(&alpha);
1076 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1077 break;
1078 }
1079 case 7: {
1080 SkRect bounds;
1081 uint8_t alpha;
1082 fuzz->next(&bounds, &alpha);
1083 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1084 break;
1085 }
1086 case 8: {
1087 SkCanvas::SaveLayerRec saveLayerRec;
1088 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001089 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001090 fuzz->next(&bounds);
1091 saveLayerRec.fBounds = &bounds;
1092 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001093 if (make_fuzz_t<bool>(fuzz)) {
1094 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001095 saveLayerRec.fPaint = &paint;
1096 }
1097 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001098 if (make_fuzz_t<bool>(fuzz)) {
1099 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001100 saveLayerRec.fBackdrop = imageFilter.get();
1101 }
Hal Canary5395c592017-03-08 16:52:18 -05001102 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001103 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001104 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1105 // }
1106
Hal Canary24ac42b2017-02-14 13:35:14 -05001107 canvas->saveLayer(saveLayerRec);
1108 break;
1109 }
1110 case 9:
1111 canvas->restore();
1112 break;
1113 case 10: {
1114 int saveCount;
1115 fuzz->next(&saveCount);
1116 canvas->restoreToCount(saveCount);
1117 break;
1118 }
1119 case 11: {
1120 SkScalar x, y;
1121 fuzz->next(&x, &y);
1122 canvas->translate(x, y);
1123 break;
1124 }
1125 case 12: {
1126 SkScalar x, y;
1127 fuzz->next(&x, &y);
1128 canvas->scale(x, y);
1129 break;
1130 }
1131 case 13: {
1132 SkScalar v;
1133 fuzz->next(&v);
1134 canvas->rotate(v);
1135 break;
1136 }
1137 case 14: {
1138 SkScalar x, y, v;
1139 fuzz->next(&x, &y, &v);
1140 canvas->rotate(v, x, y);
1141 break;
1142 }
1143 case 15: {
1144 SkScalar x, y;
1145 fuzz->next(&x, &y);
1146 canvas->skew(x, y);
1147 break;
1148 }
1149 case 16: {
1150 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001151 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001152 canvas->concat(mat);
1153 break;
1154 }
1155 case 17: {
1156 SkMatrix mat;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001157 FuzzNiceMatrix(fuzz, &mat);
Hal Canary24ac42b2017-02-14 13:35:14 -05001158 canvas->setMatrix(mat);
1159 break;
1160 }
1161 case 18:
1162 canvas->resetMatrix();
1163 break;
1164 case 19: {
1165 SkRect r;
1166 int op;
1167 bool doAntiAlias;
1168 fuzz->next(&r, &doAntiAlias);
1169 fuzz->nextRange(&op, 0, 1);
1170 r.sort();
1171 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1172 break;
1173 }
1174 case 20: {
1175 SkRRect rr;
1176 int op;
1177 bool doAntiAlias;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001178 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001179 fuzz->next(&doAntiAlias);
1180 fuzz->nextRange(&op, 0, 1);
1181 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1182 break;
1183 }
1184 case 21: {
1185 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001186 FuzzNicePath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001187 int op;
1188 bool doAntiAlias;
1189 fuzz->next(&doAntiAlias);
1190 fuzz->nextRange(&op, 0, 1);
1191 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1192 break;
1193 }
1194 case 22: {
1195 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001196 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001197 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001198 fuzz->nextRange(&op, 0, 1);
1199 canvas->clipRegion(region, (SkClipOp)op);
1200 break;
1201 }
1202 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001203 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001204 canvas->drawPaint(paint);
1205 break;
1206 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001207 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001208 SkCanvas::PointMode pointMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001209 fuzz->nextRange(&pointMode,
Hal Canaryc8bebd42017-12-20 11:21:05 -05001210 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001211 size_t count;
1212 constexpr int kMaxCount = 30;
1213 fuzz->nextRange(&count, 0, kMaxCount);
1214 SkPoint pts[kMaxCount];
1215 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001216 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001217 break;
1218 }
1219 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001220 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001221 SkRect r;
1222 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001223 if (!r.isFinite()) {
1224 break;
1225 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001226 canvas->drawRect(r, paint);
1227 break;
1228 }
1229 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001230 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001231 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001232 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001233 canvas->drawRegion(region, paint);
1234 break;
1235 }
1236 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001237 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001238 SkRect r;
1239 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001240 if (!r.isFinite()) {
1241 break;
1242 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001243 canvas->drawOval(r, paint);
1244 break;
1245 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001246 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001247 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001248 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001249 SkRRect rr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001250 FuzzNiceRRect(fuzz, &rr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001251 canvas->drawRRect(rr, paint);
1252 break;
1253 }
1254 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001255 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001256 SkRRect orr, irr;
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001257 FuzzNiceRRect(fuzz, &orr);
1258 FuzzNiceRRect(fuzz, &irr);
Hal Canary27bece82017-03-07 16:23:20 -05001259 if (orr.getBounds().contains(irr.getBounds())) {
1260 canvas->drawDRRect(orr, irr, paint);
1261 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001262 break;
1263 }
1264 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001265 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001266 SkRect r;
1267 SkScalar start, sweep;
1268 bool useCenter;
1269 fuzz->next(&r, &start, &sweep, &useCenter);
1270 canvas->drawArc(r, start, sweep, useCenter, paint);
1271 break;
1272 }
1273 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001274 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001275 SkPath path;
Mike Klein7ffa40c2018-09-25 12:16:53 -04001276 FuzzNicePath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001277 canvas->drawPath(path, paint);
1278 break;
1279 }
1280 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001281 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001282 SkScalar left, top;
1283 bool usePaint;
1284 fuzz->next(&left, &top, &usePaint);
1285 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001286 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001287 }
1288 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1289 break;
1290 }
1291 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001292 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001293 SkRect src, dst;
1294 bool usePaint;
1295 fuzz->next(&src, &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, src, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001300 break;
1301 }
1302 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001303 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001304 SkIRect src;
1305 SkRect dst;
1306 bool usePaint;
1307 fuzz->next(&src, &dst, &usePaint);
1308 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001309 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001310 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001311 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001312 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1313 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001314 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1315 break;
1316 }
1317 case 36: {
1318 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001319 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001320 SkRect dst;
1321 fuzz->next(&dst, &usePaint);
1322 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001323 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001324 }
Brian Salomonf08002c2018-10-26 16:15:46 -04001325 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -05001326 break;
1327 }
1328 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001329 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001330 SkIRect center;
1331 SkRect dst;
1332 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001333 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001334 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001335 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001336 }
Hal Canary0361d492017-03-15 12:58:15 -04001337 if (make_fuzz_t<bool>(fuzz)) {
1338 fuzz->next(&center);
1339 } else { // Make valid center, see SkLatticeIter::Valid().
1340 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1341 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1342 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1343 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1344 }
1345 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001346 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1347 break;
1348 }
1349 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001350 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001351 SkScalar left, top;
1352 bool usePaint;
1353 fuzz->next(&left, &top, &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 }
1357 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1358 break;
1359 }
1360 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 SkRect src, dst;
1363 bool usePaint;
1364 fuzz->next(&src, &dst, &usePaint);
1365 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001366 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001367 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001368 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001369 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1370 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001371 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1372 break;
1373 }
1374 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001375 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001376 SkIRect src;
1377 SkRect dst;
1378 bool usePaint;
1379 fuzz->next(&src, &dst, &usePaint);
1380 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001381 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001382 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001383 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001384 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1385 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001386 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1387 break;
1388 }
1389 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001390 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001391 SkRect dst;
1392 bool usePaint;
1393 fuzz->next(&dst, &usePaint);
1394 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001395 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001396 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001397 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001398 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1399 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001400 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1401 break;
1402 }
1403 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001404 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001405 SkIRect center;
1406 SkRect dst;
1407 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001408 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001409 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001410 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001411 }
Hal Canary0361d492017-03-15 12:58:15 -04001412 if (make_fuzz_t<bool>(fuzz)) {
1413 fuzz->next(&center);
1414 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001415 if (img.width() == 0 || img.height() == 0) {
1416 // bitmap may not have had its pixels initialized.
1417 break;
1418 }
Hal Canary0361d492017-03-15 12:58:15 -04001419 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1420 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1421 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1422 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1423 }
1424 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001425 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1426 break;
1427 }
1428 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001429 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001430 bool usePaint;
1431 SkRect dst;
1432 fuzz->next(&usePaint, &dst);
1433 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001434 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001435 }
1436 constexpr int kMax = 6;
1437 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001438 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001439 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1440 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1441 fuzz->nextN(xDivs, lattice.fXCount);
1442 fuzz->nextN(yDivs, lattice.fYCount);
1443 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1444 break;
1445 }
1446 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001447 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001448 bool usePaint;
1449 SkRect dst;
1450 fuzz->next(&usePaint, &dst);
1451 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001452 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001453 }
1454 constexpr int kMax = 6;
1455 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001456 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001457 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1458 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1459 fuzz->nextN(xDivs, lattice.fXCount);
1460 fuzz->nextN(yDivs, lattice.fYCount);
1461 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1462 break;
1463 }
1464 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001465 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed358fcad2018-11-23 15:27:51 -05001466 font = fuzz_font(fuzz);
1467 SkTextEncoding encoding = fuzz_paint_text_encoding(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001468 SkScalar x, y;
1469 fuzz->next(&x, &y);
Mike Reed358fcad2018-11-23 15:27:51 -05001470 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
1471 canvas->drawSimpleText(text.begin(), SkToSizeT(text.count()), encoding, x, y,
1472 font, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001473 break;
1474 }
1475 case 46: {
Mike Reed212e9062018-12-25 17:35:49 -05001476 // was drawPosText
Hal Canary24ac42b2017-02-14 13:35:14 -05001477 break;
1478 }
1479 case 47: {
Mike Reed212e9062018-12-25 17:35:49 -05001480 // was drawPosTextH
Hal Canary24ac42b2017-02-14 13:35:14 -05001481 break;
1482 }
1483 case 48: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001484 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001485 break;
1486 }
1487 case 49: {
Mike Reed1eb9af92018-10-01 12:16:59 -04001488 // was drawtextonpath
Hal Canary24ac42b2017-02-14 13:35:14 -05001489 break;
1490 }
1491 case 50: {
Mike Reed212e9062018-12-25 17:35:49 -05001492 // was drawTextRSXform
Hal Canary24ac42b2017-02-14 13:35:14 -05001493 break;
1494 }
1495 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001496 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001497 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001498 SkScalar x, y;
1499 fuzz->next(&x, &y);
1500 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001501 break;
1502 }
1503 case 52: {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001504 SkMatrix matrix;
Hal Canary24ac42b2017-02-14 13:35:14 -05001505 bool usePaint, useMatrix;
1506 fuzz->next(&usePaint, &useMatrix);
1507 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001508 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001509 }
1510 if (useMatrix) {
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001511 FuzzNiceMatrix(fuzz, &matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -05001512 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001513 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001514 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1515 usePaint ? &paint : nullptr);
1516 break;
1517 }
1518 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001519 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001520 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001521 SkBlendMode blendMode;
Mike Kleinf88f5ef2018-11-19 12:21:46 -05001522 fuzz->nextRange(&vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
1523 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001524 constexpr int kMaxCount = 100;
1525 int vertexCount;
1526 SkPoint vertices[kMaxCount];
1527 SkPoint texs[kMaxCount];
1528 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001529 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1530 fuzz->nextN(vertices, vertexCount);
1531 bool useTexs, useColors;
1532 fuzz->next(&useTexs, &useColors);
1533 if (useTexs) {
1534 fuzz->nextN(texs, vertexCount);
1535 }
1536 if (useColors) {
1537 fuzz->nextN(colors, vertexCount);
1538 }
1539 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001540 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001541 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001542 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1543 for (int i = 0; i < indexCount; ++i) {
1544 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1545 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001546 }
Mike Reed887cdf12017-04-03 11:11:09 -04001547 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1548 useTexs ? texs : nullptr,
1549 useColors ? colors : nullptr,
1550 indexCount, indices),
1551 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001552 break;
1553 }
1554 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001555 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001556 break;
1557 }
1558 }
1559}
1560
Hal Canary1e0138b2017-03-10 13:56:08 -05001561static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001562 SkScalar w, h;
1563 fuzz->next(&w, &h);
1564 SkPictureRecorder pictureRecorder;
1565 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1566 return pictureRecorder.finishRecordingAsPicture();
1567}
1568
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001569DEF_FUZZ(NullCanvas, fuzz) {
1570 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001571}
1572
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001573constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001574
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001575DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001576 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001577 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001578 fuzz_canvas(fuzz, surface->getCanvas());
1579}
1580
Hal Canarye2924432017-12-01 11:46:26 -05001581DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1582 SkPictureRecorder recorder;
1583 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1584 SkIntToScalar(kCanvasSize.height())));
1585 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1586 if (!pic) { fuzz->signalBug(); }
1587 sk_sp<SkData> data = pic->serialize();
1588 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001589 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001590 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001591 if (!deserialized) { fuzz->signalBug(); }
1592 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1593 SkASSERT(surface && surface->getCanvas());
1594 surface->getCanvas()->drawPicture(deserialized);
1595}
1596
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001597DEF_FUZZ(ImageFilter, fuzz) {
1598 auto fil = make_fuzz_imageFilter(fuzz, 20);
1599
1600 SkPaint paint;
1601 paint.setImageFilter(fil);
1602 SkBitmap bitmap;
1603 SkCanvas canvas(bitmap);
1604 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1605}
1606
1607
1608//SkRandom _rand;
1609#define SK_ADD_RANDOM_BIT_FLIPS
1610
1611DEF_FUZZ(SerializedImageFilter, fuzz) {
1612 auto filter = make_fuzz_imageFilter(fuzz, 20);
Robert Phillips66f6ef42018-09-14 11:58:40 -04001613 if (!filter) {
1614 return;
1615 }
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001616 auto data = filter->serialize();
1617 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1618 size_t len = data->size();
1619#ifdef SK_ADD_RANDOM_BIT_FLIPS
1620 unsigned char* p = const_cast<unsigned char*>(ptr);
1621 for (size_t i = 0; i < len; ++i, ++p) {
1622 uint8_t j;
1623 fuzz->nextRange(&j, 1, 250);
1624 if (j == 1) { // 0.4% of the time, flip a bit or byte
1625 uint8_t k;
1626 fuzz->nextRange(&k, 1, 10);
1627 if (k == 1) { // Then 10% of the time, change a whole byte
1628 uint8_t s;
1629 fuzz->nextRange(&s, 0, 2);
1630 switch(s) {
1631 case 0:
1632 *p ^= 0xFF; // Flip entire byte
1633 break;
1634 case 1:
1635 *p = 0xFF; // Set all bits to 1
1636 break;
1637 case 2:
1638 *p = 0x00; // Set all bits to 0
1639 break;
1640 }
1641 } else {
1642 uint8_t s;
1643 fuzz->nextRange(&s, 0, 7);
1644 *p ^= (1 << 7);
1645 }
1646 }
1647 }
1648#endif // SK_ADD_RANDOM_BIT_FLIPS
1649 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1650
1651 // uncomment below to write out a serialized image filter (to make corpus
1652 // for -t filter_fuzz)
1653 // SkString s("./serialized_filters/sf");
1654 // s.appendU32(_rand.nextU());
1655 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1656 // sk_fwrite(data->bytes(), data->size(), file);
1657 // sk_fclose(file);
1658
1659 SkPaint paint;
1660 paint.setImageFilter(deserializedFil);
1661 SkBitmap bitmap;
1662 SkCanvas canvas(bitmap);
1663 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1664}
1665
Hal Canary44801ca2017-03-15 11:39:06 -04001666#if SK_SUPPORT_GPU
Kevin Lubickfaef5142018-06-07 10:33:11 -04001667
1668static void dump_GPU_info(GrContext* context) {
1669 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->contextPriv().getGpu())
1670 ->glInterface();
1671 const GrGLubyte* output;
1672 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1673 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1674
1675 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1676 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1677
1678 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1679 SkDebugf("GL_VERSION %s\n", (const char*) output);
1680}
1681
Hal Canary5aa91582017-03-21 11:11:44 -07001682static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1683 SkASSERT(context);
1684 auto surface = SkSurface::MakeRenderTarget(
1685 context,
1686 SkBudgeted::kNo,
1687 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1688 SkASSERT(surface && surface->getCanvas());
1689 fuzz_canvas(fuzz, surface->getCanvas());
1690}
1691
Hal Canary44801ca2017-03-15 11:39:06 -04001692DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001693 sk_gpu_test::GrContextFactory f;
1694 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001695 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001696 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001697 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001698 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001699 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001700 }
Hal Canary5aa91582017-03-21 11:11:44 -07001701 fuzz_ganesh(fuzz, context);
1702}
1703
Kevin Lubick27d42192018-04-03 12:30:32 -04001704// This target is deprecated, NullGLContext is not well maintained.
1705// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001706DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001707 sk_gpu_test::GrContextFactory f;
1708 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001709}
1710
Kevin Lubick27d42192018-04-03 12:30:32 -04001711DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001712 sk_gpu_test::GrContextFactory f;
1713 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1714}
Hal Canary44801ca2017-03-15 11:39:06 -04001715#endif
1716
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001717DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001718 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -04001719 auto doc = SkPDF::MakeDocument(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001720 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1721 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001722}
1723
1724// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1725DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001726 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001727 fuzz_canvas(fuzz, &debugCanvas);
1728 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1729 UrlDataManager dataManager(SkString("data"));
1730 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1731 Json::StyledStreamWriter(" ").write(std::cout, json);
1732}