blob: 9e117af66f30a8d0706925175a213d4239dc34ec [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"
15#include "SkDocument.h"
Hal Canary671e4422017-02-27 13:36:38 -050016#include "SkFontMgr.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050017#include "SkImageFilter.h"
18#include "SkMaskFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050019#include "SkNullCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050020#include "SkPathEffect.h"
21#include "SkPictureRecorder.h"
Mike Reed54518ac2017-07-22 08:29:48 -040022#include "SkPoint3.h"
Hal Canary5395c592017-03-08 16:52:18 -050023#include "SkRSXform.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050024#include "SkRegion.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050025#include "SkSurface.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050026#include "SkTypeface.h"
Kevin Lubickedef8ec2018-01-09 15:32:58 -050027#include "SkOSFile.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050028
29// EFFECTS
Hal Canary5395c592017-03-08 16:52:18 -050030#include "Sk1DPathEffect.h"
31#include "Sk2DPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050032#include "SkAlphaThresholdFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050033#include "SkArithmeticImageFilter.h"
Robert Phillips70e3e9a2017-06-26 14:22:01 -040034#include "SkBlurImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050035#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050036#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050037#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050038#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050039#include "SkCornerPathEffect.h"
40#include "SkDashPathEffect.h"
41#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050042#include "SkDisplacementMapEffect.h"
43#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050044#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050045#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050046#include "SkImageSource.h"
47#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050048#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050049#include "SkMagnifierImageFilter.h"
50#include "SkMatrixConvolutionImageFilter.h"
51#include "SkMergeImageFilter.h"
52#include "SkMorphologyImageFilter.h"
53#include "SkOffsetImageFilter.h"
54#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050055#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050056#include "SkPictureImageFilter.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050057#include "SkReadBuffer.h"
Hal Canary5395c592017-03-08 16:52:18 -050058#include "SkRRectsGaussianEdgeMaskFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050059#include "SkTableColorFilter.h"
Mike Reedc090c642017-05-16 10:39:06 -040060#include "SkTextBlob.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050061#include "SkTileImageFilter.h"
62#include "SkXfermodeImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050063
64// SRC
65#include "SkUtils.h"
66
Hal Canary44801ca2017-03-15 11:39:06 -040067#if SK_SUPPORT_GPU
68#include "GrContextFactory.h"
69#endif
70
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050071// MISC
72
73#include <iostream>
74
Hal Canary24ac42b2017-02-14 13:35:14 -050075// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050076// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040077// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050078
Hal Canaryf7005202017-03-10 08:48:28 -050079template <typename T, typename Min, typename Max>
80inline void fuzz_enum_range(Fuzz* fuzz, T* value, Min rmin, Max rmax) {
Hal Canary24ac42b2017-02-14 13:35:14 -050081 using U = skstd::underlying_type_t<T>;
Hal Canaryf7005202017-03-10 08:48:28 -050082 fuzz->nextRange((U*)value, (U)rmin, (U)rmax);
83}
84
Hal Canary1e0138b2017-03-10 13:56:08 -050085// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
86// In fact, all make_fuzz_foo() functions have this potential problem.
87// Use sequence points!
88template <typename T>
89inline T make_fuzz_t(Fuzz* fuzz) {
90 T t;
91 fuzz->next(&t);
92 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050093}
94
Hal Canaryb69c4b82017-03-08 11:02:40 -050095template <>
96inline void Fuzz::next(SkShader::TileMode* m) {
Hal Canaryf7005202017-03-10 08:48:28 -050097 fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -050098}
99
Hal Canaryb69c4b82017-03-08 11:02:40 -0500100template <>
Hal Canarye03c3e52017-03-09 11:33:35 -0500101inline void Fuzz::next(SkFilterQuality* q) {
Hal Canaryf7005202017-03-10 08:48:28 -0500102 fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
103 SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500104}
105
106template <>
Hal Canaryb69c4b82017-03-08 11:02:40 -0500107inline void Fuzz::next(SkMatrix* m) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500108 constexpr int kArrayLength = 9;
109 SkScalar buffer[kArrayLength];
110 int matrixType;
111 this->nextRange(&matrixType, 0, 4);
112 switch (matrixType) {
113 case 0: // identity
114 *m = SkMatrix::I();
115 return;
116 case 1: // translate
117 this->nextRange(&buffer[0], -4000.0f, 4000.0f);
118 this->nextRange(&buffer[1], -4000.0f, 4000.0f);
119 *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
120 return;
121 case 2: // translate + scale
122 this->nextRange(&buffer[0], -400.0f, 400.0f);
123 this->nextRange(&buffer[1], -400.0f, 400.0f);
124 this->nextRange(&buffer[2], -4000.0f, 4000.0f);
125 this->nextRange(&buffer[3], -4000.0f, 4000.0f);
126 *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
127 m->postTranslate(buffer[2], buffer[3]);
128 return;
129 case 3: // affine
130 this->nextN(buffer, 6);
131 m->setAffine(buffer);
132 return;
133 case 4: // perspective
134 this->nextN(buffer, kArrayLength);
135 m->set9(buffer);
136 return;
137 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500138 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500139 return;
140 }
141}
142
Hal Canaryb69c4b82017-03-08 11:02:40 -0500143template <>
144inline void Fuzz::next(SkRRect* rr) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500145 SkRect r;
146 SkVector radii[4];
147 this->next(&r);
Hal Canary27bece82017-03-07 16:23:20 -0500148 r.sort();
149 for (SkVector& vec : radii) {
150 this->nextRange(&vec.fX, 0.0f, 1.0f);
151 vec.fX *= 0.5f * r.width();
152 this->nextRange(&vec.fY, 0.0f, 1.0f);
153 vec.fY *= 0.5f * r.height();
154 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500155 rr->setRectRadii(r, radii);
Hal Canary24ac42b2017-02-14 13:35:14 -0500156}
157
Hal Canaryb69c4b82017-03-08 11:02:40 -0500158template <>
159inline void Fuzz::next(SkBlendMode* mode) {
Hal Canaryf7005202017-03-10 08:48:28 -0500160 fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500161}
162
Hal Canary1e0138b2017-03-10 13:56:08 -0500163static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -0500164
Hal Canary1e0138b2017-03-10 13:56:08 -0500165static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500166
Hal Canary1e0138b2017-03-10 13:56:08 -0500167static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500168
Hal Canary1e0138b2017-03-10 13:56:08 -0500169static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500170 if (depth <= 0) {
171 return nullptr;
172 }
173 int colorFilterType;
174 fuzz->nextRange(&colorFilterType, 0, 8);
175 switch (colorFilterType) {
176 case 0:
177 return nullptr;
178 case 1: {
179 SkColor color;
180 SkBlendMode mode;
181 fuzz->next(&color, &mode);
182 return SkColorFilter::MakeModeFilter(color, mode);
183 }
184 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500185 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500186 if (!outer) {
187 return nullptr;
188 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500189 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500190 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500191 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500192 }
193 case 3: {
194 SkScalar array[20];
195 fuzz->nextN(array, SK_ARRAY_COUNT(array));
196 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
197 }
198 case 4: {
199 SkColor mul, add;
200 fuzz->next(&mul, &add);
201 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
202 }
203 case 5: {
204 bool grayscale;
205 int invertStyle;
206 float contrast;
207 fuzz->next(&grayscale);
208 fuzz->nextRange(&invertStyle, 0, 2);
209 fuzz->nextRange(&contrast, -1.0f, 1.0f);
210 return SkHighContrastFilter::Make(SkHighContrastConfig(
211 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
212 }
213 case 6:
214 return SkLumaColorFilter::Make();
215 case 7: {
216 uint8_t table[256];
217 fuzz->nextN(table, SK_ARRAY_COUNT(table));
218 return SkTableColorFilter::Make(table);
219 }
220 case 8: {
221 uint8_t tableA[256];
222 uint8_t tableR[256];
223 uint8_t tableG[256];
224 uint8_t tableB[256];
225 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
226 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
227 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
228 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
229 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
230 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500231 default:
232 SkASSERT(false);
233 break;
Hal Canary27bece82017-03-07 16:23:20 -0500234 }
235 return nullptr;
236}
Hal Canary24ac42b2017-02-14 13:35:14 -0500237
Hal Canary1e0138b2017-03-10 13:56:08 -0500238static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500239 SkScalar totalPos = 0;
240 for (int i = 0; i < colorCount; ++i) {
241 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
242 totalPos += pos[i];
243 }
244 totalPos = 1.0f / totalPos;
245 for (int i = 0; i < colorCount; ++i) {
246 pos[i] *= totalPos;
247 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500248 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500249 pos[colorCount - 1] = 1.0f;
250}
251
Hal Canary1e0138b2017-03-10 13:56:08 -0500252static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500253 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
254 sk_sp<SkColorFilter> colorFilter(nullptr);
255 SkBitmap bitmap;
256 sk_sp<SkImage> img;
257 SkShader::TileMode tmX, tmY;
258 bool useMatrix;
259 SkColor color;
260 SkMatrix matrix;
261 SkBlendMode blendMode;
262 int shaderType;
263 if (depth <= 0) {
264 return nullptr;
265 }
Hal Canary671e4422017-02-27 13:36:38 -0500266 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500267 switch (shaderType) {
268 case 0:
269 return nullptr;
270 case 1:
271 return SkShader::MakeEmptyShader();
272 case 2:
273 fuzz->next(&color);
274 return SkShader::MakeColorShader(color);
275 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500276 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500277 fuzz->next(&tmX, &tmY, &useMatrix);
278 if (useMatrix) {
279 fuzz->next(&matrix);
280 }
281 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
282 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500283 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500284 fuzz->next(&tmX, &tmY, &useMatrix);
285 if (useMatrix) {
286 fuzz->next(&matrix);
287 }
288 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
289 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500290 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500291 fuzz->next(&matrix);
292 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
293 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500294 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
295 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500296 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
297 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500298 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
299 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500300 fuzz->next(&blendMode);
301 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500302 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500303 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500304 bool useTile;
305 SkRect tile;
306 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
307 if (useMatrix) {
308 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500309 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500310 if (useTile) {
311 fuzz->next(&tile);
312 }
313 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
314 useMatrix ? &matrix : nullptr,
315 useTile ? &tile : nullptr);
316 }
Hal Canary671e4422017-02-27 13:36:38 -0500317 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500318 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000319 // Deprecated SkGaussianEdgeShader
320 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500321 case 10: {
322 constexpr int kMaxColors = 12;
323 SkPoint pts[2];
324 SkColor colors[kMaxColors];
325 SkScalar pos[kMaxColors];
326 int colorCount;
327 bool usePos;
328 fuzz->nextN(pts, 2);
329 fuzz->nextRange(&colorCount, 2, kMaxColors);
330 fuzz->nextN(colors, colorCount);
331 fuzz->next(&tmX, &useMatrix, &usePos);
332 if (useMatrix) {
333 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500334 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500335 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500336 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500337 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500338 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
339 tmX, 0, useMatrix ? &matrix : nullptr);
340 }
341 case 11: {
342 constexpr int kMaxColors = 12;
343 SkPoint center;
344 SkScalar radius;
345 int colorCount;
346 bool usePos;
347 SkColor colors[kMaxColors];
348 SkScalar pos[kMaxColors];
349 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
350 fuzz->nextRange(&colorCount, 2, kMaxColors);
351 fuzz->nextN(colors, colorCount);
352 if (useMatrix) {
353 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500354 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500355 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500356 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500357 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500358 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
359 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
360 }
361 case 12: {
362 constexpr int kMaxColors = 12;
363 SkPoint start, end;
364 SkScalar startRadius, endRadius;
365 int colorCount;
366 bool usePos;
367 SkColor colors[kMaxColors];
368 SkScalar pos[kMaxColors];
369 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
370 fuzz->nextRange(&colorCount, 2, kMaxColors);
371 fuzz->nextN(colors, colorCount);
372 if (useMatrix) {
373 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500374 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500375 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500376 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500377 }
378 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
379 usePos ? pos : nullptr, colorCount, tmX, 0,
380 useMatrix ? &matrix : nullptr);
381 }
382 case 13: {
383 constexpr int kMaxColors = 12;
384 SkScalar cx, cy;
385 int colorCount;
386 bool usePos;
387 SkColor colors[kMaxColors];
388 SkScalar pos[kMaxColors];
389 fuzz->next(&cx, &cy, &useMatrix, &usePos);
390 fuzz->nextRange(&colorCount, 2, kMaxColors);
391 fuzz->nextN(colors, colorCount);
392 if (useMatrix) {
393 fuzz->next(&matrix);
394 }
395 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500396 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500397 }
398 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
399 0, useMatrix ? &matrix : nullptr);
400 }
401 case 14: {
402 SkScalar baseFrequencyX, baseFrequencyY, seed;
403 int numOctaves;
404 SkISize tileSize;
405 bool useTileSize, turbulence;
406 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
407 if (useTileSize) {
408 fuzz->next(&tileSize);
409 }
410 fuzz->nextRange(&numOctaves, 2, 7);
411 if (turbulence) {
412 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
413 numOctaves, seed,
414 useTileSize ? &tileSize : nullptr);
415 } else {
416 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
417 numOctaves, seed,
418 useTileSize ? &tileSize : nullptr);
419 }
420 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500421 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500422 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500423 break;
424 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500425 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500426}
427
Hal Canary1e0138b2017-03-10 13:56:08 -0500428static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500429 if (depth <= 0) {
430 return nullptr;
431 }
432 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400433 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500434 switch (pathEffectType) {
435 case 0: {
436 return nullptr;
437 }
438 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500439 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
440 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500441 return SkPathEffect::MakeSum(std::move(first), std::move(second));
442 }
443 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500444 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
445 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500446 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
447 }
448 case 3: {
449 SkPath path;
450 fuzz_path(fuzz, &path, 20);
451 SkScalar advance, phase;
452 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500453 SkPath1DPathEffect::Style style;
454 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
455 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500456 }
457 case 4: {
458 SkScalar width;
459 SkMatrix matrix;
460 fuzz->next(&width, &matrix);
461 return SkLine2DPathEffect::Make(width, matrix);
462 }
463 case 5: {
464 SkPath path;
465 fuzz_path(fuzz, &path, 20);
466 SkMatrix matrix;
467 fuzz->next(&matrix);
468 return SkPath2DPathEffect::Make(matrix, path);
469 }
470 case 6: {
471 SkScalar radius;
472 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500473 return SkCornerPathEffect::Make(radius);
474 }
Mike Reed40e7e652017-07-22 22:12:59 -0400475 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500476 SkScalar phase;
477 fuzz->next(&phase);
478 SkScalar intervals[20];
479 int count;
480 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
481 fuzz->nextN(intervals, count);
482 return SkDashPathEffect::Make(intervals, count, phase);
483 }
Mike Reed40e7e652017-07-22 22:12:59 -0400484 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500485 SkScalar segLength, dev;
486 uint32_t seed;
487 fuzz->next(&segLength, &dev, &seed);
488 return SkDiscretePathEffect::Make(segLength, dev, seed);
489 }
490 default:
491 SkASSERT(false);
492 return nullptr;
493 }
494}
Hal Canary24ac42b2017-02-14 13:35:14 -0500495
Hal Canary1e0138b2017-03-10 13:56:08 -0500496static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500497 int maskfilterType;
498 fuzz->nextRange(&maskfilterType, 0, 2);
499 switch (maskfilterType) {
500 case 0:
501 return nullptr;
502 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500503 SkBlurStyle blurStyle;
504 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500505 SkScalar sigma;
506 fuzz->next(&sigma);
507 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Hal Canary1e0138b2017-03-10 13:56:08 -0500508 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -0500509 fuzz->next(&occluder);
510 }
511 uint32_t flags;
512 fuzz->nextRange(&flags, 0, 3);
Hal Canaryf7005202017-03-10 08:48:28 -0500513 return SkBlurMaskFilter::Make(blurStyle, sigma, occluder, flags);
Hal Canary5395c592017-03-08 16:52:18 -0500514 }
515 case 2: {
516 SkRRect first, second;
517 SkScalar radius;
518 fuzz->next(&first, &second, &radius);
519 return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
520 }
521 default:
522 SkASSERT(false);
523 return nullptr;
524 }
525}
Hal Canary24ac42b2017-02-14 13:35:14 -0500526
Hal Canary1e0138b2017-03-10 13:56:08 -0500527static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
528 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500529 return nullptr;
530 }
531 auto fontMugger = SkFontMgr::RefDefault();
532 SkASSERT(fontMugger);
533 int familyCount = fontMugger->countFamilies();
534 int i, j;
535 fuzz->nextRange(&i, 0, familyCount - 1);
536 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
537 int styleCount = family->count();
538 fuzz->nextRange(&j, 0, styleCount - 1);
539 return sk_sp<SkTypeface>(family->createTypeface(j));
540}
Hal Canary24ac42b2017-02-14 13:35:14 -0500541
Hal Canarye03c3e52017-03-09 11:33:35 -0500542template <>
543inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
544 SkRect rect;
545 uint8_t flags;
546 this->next(&rect);
547 this->nextRange(&flags, 0, 0xF);
548 *cropRect = SkImageFilter::CropRect(rect, flags);
549}
550
Hal Canary1e0138b2017-03-10 13:56:08 -0500551static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500552
553static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
554 if (depth <= 0) {
555 return nullptr;
556 }
557 uint8_t imageFilterType;
558 fuzz->nextRange(&imageFilterType, 1, 6);
559 SkPoint3 p, q;
560 SkColor lightColor;
561 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
562 sk_sp<SkImageFilter> input;
563 SkImageFilter::CropRect cropRect;
564 bool useCropRect;
565 fuzz->next(&useCropRect);
566 if (useCropRect) {
567 fuzz->next(&cropRect);
568 }
569 switch (imageFilterType) {
570 case 1:
571 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500572 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500573 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
574 std::move(input),
575 useCropRect ? &cropRect : nullptr);
576 case 2:
577 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500578 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500579 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
580 std::move(input),
581 useCropRect ? &cropRect : nullptr);
582 case 3:
583 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500584 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500585 return SkLightingImageFilter::MakeSpotLitDiffuse(
586 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
587 std::move(input), useCropRect ? &cropRect : nullptr);
588 case 4:
589 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500590 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500591 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
592 shininess, std::move(input),
593 useCropRect ? &cropRect : nullptr);
594 case 5:
595 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500596 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500597 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
598 shininess, std::move(input),
599 useCropRect ? &cropRect : nullptr);
600 case 6:
601 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
602 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500603 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500604 return SkLightingImageFilter::MakeSpotLitSpecular(
605 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
606 std::move(input), useCropRect ? &cropRect : nullptr);
607 default:
608 SkASSERT(false);
609 return nullptr;
610 }
611}
612
Hal Canary1e0138b2017-03-10 13:56:08 -0500613static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500614
Hal Canary1e0138b2017-03-10 13:56:08 -0500615static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500616 if (depth <= 0) {
617 return nullptr;
618 }
619 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500620 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500621 switch (imageFilterType) {
622 case 0:
623 return nullptr;
624 case 1: {
625 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500626 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500627 bool useCropRect;
628 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400629 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500630 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400631 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500632 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400633 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500634 useCropRect ? &cropRect : nullptr);
635 }
636 case 2: {
637 SkMatrix matrix;
638 SkFilterQuality quality;
639 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500640 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500641 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
642 }
643 case 3: {
644 SkRegion region;
645 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500646 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500647 bool useCropRect;
648 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400649 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500650 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400651 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500652 }
653 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
654 useCropRect ? &cropRect : nullptr);
655 }
656 case 4: {
657 float k1, k2, k3, k4;
658 bool enforcePMColor;
659 bool useCropRect;
660 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500661 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
662 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500663 SkImageFilter::CropRect cropRect;
664 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400665 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500666 }
667 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
668 std::move(background), std::move(foreground),
669 useCropRect ? &cropRect : nullptr);
670 }
671 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500672 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
673 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500674 bool useCropRect;
675 SkImageFilter::CropRect cropRect;
676 fuzz->next(&useCropRect);
677 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400678 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500679 }
680 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
681 useCropRect ? &cropRect : nullptr);
682 }
683 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500684 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
685 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500686 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
687 }
688 case 7: {
689 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400690 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
691 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500692 SkScalar scale;
693 bool useCropRect;
694 fuzz->next(&scale, &useCropRect);
695 SkImageFilter::CropRect cropRect;
696 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400697 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500698 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500699 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
700 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500701 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
702 std::move(displacement), std::move(color),
703 useCropRect ? &cropRect : nullptr);
704 }
705 case 8: {
706 SkScalar dx, dy, sigmaX, sigmaY;
707 SkColor color;
708 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500709 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500710 bool useCropRect;
711 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
712 SkImageFilter::CropRect cropRect;
713 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400714 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500715 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500716 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500717 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
718 std::move(input),
719 useCropRect ? &cropRect : nullptr);
720 }
721 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500722 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500723 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500724 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500725 SkRect srcRect, dstRect;
726 SkFilterQuality filterQuality;
727 fuzz->next(&srcRect, &dstRect, &filterQuality);
728 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
729 }
730 case 11:
731 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
732 case 12: {
733 SkRect srcRect;
734 SkScalar inset;
735 bool useCropRect;
736 SkImageFilter::CropRect cropRect;
737 fuzz->next(&srcRect, &inset, &useCropRect);
738 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400739 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500740 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500741 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500742 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
743 useCropRect ? &cropRect : nullptr);
744 }
745 case 13: {
746 constexpr int kMaxKernelSize = 5;
747 int32_t n, m;
748 fuzz->nextRange(&n, 1, kMaxKernelSize);
749 fuzz->nextRange(&m, 1, kMaxKernelSize);
750 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
751 fuzz->nextN(kernel, n * m);
752 int32_t offsetX, offsetY;
753 fuzz->nextRange(&offsetX, 0, n - 1);
754 fuzz->nextRange(&offsetY, 0, m - 1);
755 SkScalar gain, bias;
756 bool convolveAlpha, useCropRect;
757 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
758 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500759 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500760 SkImageFilter::CropRect cropRect;
761 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400762 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500763 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500764 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500765 return SkMatrixConvolutionImageFilter::Make(
766 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
767 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
768 }
769 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500770 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
771 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500772 bool useCropRect;
773 fuzz->next(&useCropRect);
774 SkImageFilter::CropRect cropRect;
775 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400776 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500777 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400778 return SkMergeImageFilter::Make(std::move(first), std::move(second),
779 useCropRect ? &cropRect : nullptr);
780 }
781 case 15: {
782 constexpr int kMaxCount = 4;
783 sk_sp<SkImageFilter> ifs[kMaxCount];
784 int count;
785 fuzz->nextRange(&count, 1, kMaxCount);
786 for (int i = 0; i < count; ++i) {
787 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
788 }
789 bool useCropRect;
790 fuzz->next(&useCropRect);
791 SkImageFilter::CropRect cropRect;
792 if (useCropRect) {
793 fuzz->next(&cropRect);
794 }
795 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500796 }
797 case 16: {
798 int rx, ry;
799 fuzz->next(&rx, &ry);
800 bool useCropRect;
801 fuzz->next(&useCropRect);
802 SkImageFilter::CropRect cropRect;
803 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400804 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500805 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500806 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500807 return SkDilateImageFilter::Make(rx, ry, std::move(input),
808 useCropRect ? &cropRect : nullptr);
809 }
810 case 17: {
811 int rx, ry;
812 fuzz->next(&rx, &ry);
813 bool useCropRect;
814 fuzz->next(&useCropRect);
815 SkImageFilter::CropRect cropRect;
816 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400817 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500818 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500819 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500820 return SkErodeImageFilter::Make(rx, ry, std::move(input),
821 useCropRect ? &cropRect : nullptr);
822 }
823 case 18: {
824 SkScalar dx, dy;
825 fuzz->next(&dx, &dy);
826 bool useCropRect;
827 fuzz->next(&useCropRect);
828 SkImageFilter::CropRect cropRect;
829 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400830 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500831 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500832 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500833 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
834 useCropRect ? &cropRect : nullptr);
835 }
836 case 19: {
837 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500838 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500839 bool useCropRect;
840 fuzz->next(&useCropRect);
841 SkImageFilter::CropRect cropRect;
842 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400843 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500844 }
845 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
846 }
847 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500848 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500849 return SkPictureImageFilter::Make(std::move(picture));
850 }
851 case 21: {
852 SkRect cropRect;
853 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500854 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500855 return SkPictureImageFilter::Make(std::move(picture), cropRect);
856 }
857 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500858 SkRect src, dst;
859 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500860 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500861 return SkTileImageFilter::Make(src, dst, std::move(input));
862 }
Mike Reed77e487d2017-11-09 21:50:20 +0000863 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500864 SkBlendMode blendMode;
865 bool useCropRect;
866 fuzz->next(&useCropRect, &blendMode);
867 SkImageFilter::CropRect cropRect;
868 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400869 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500870 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500871 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
872 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500873 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
874 useCropRect ? &cropRect : nullptr);
875 }
876 default:
877 SkASSERT(false);
878 return nullptr;
879 }
880}
Hal Canary24ac42b2017-02-14 13:35:14 -0500881
Hal Canary1e0138b2017-03-10 13:56:08 -0500882static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500883 int w, h;
884 fuzz->nextRange(&w, 1, 1024);
885 fuzz->nextRange(&h, 1, 1024);
886 SkAutoTMalloc<SkPMColor> data(w * h);
887 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
888 int n = w * h;
889 for (int i = 0; i < n; ++i) {
890 SkColor c;
891 fuzz->next(&c);
892 data[i] = SkPreMultiplyColor(c);
893 }
894 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500895 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
896 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500897}
898
Hal Canary1e0138b2017-03-10 13:56:08 -0500899static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500900 SkBitmap bitmap;
901 int w, h;
902 fuzz->nextRange(&w, 1, 1024);
903 fuzz->nextRange(&h, 1, 1024);
904 bitmap.allocN32Pixels(w, h);
Hal Canary24ac42b2017-02-14 13:35:14 -0500905 for (int y = 0; y < h; ++y) {
906 for (int x = 0; x < w; ++x) {
907 SkColor c;
908 fuzz->next(&c);
909 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
910 }
911 }
912 return bitmap;
913}
914
Hal Canary1e0138b2017-03-10 13:56:08 -0500915template <typename T, typename Min, typename Max>
916inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
917 T value;
918 fuzz_enum_range(fuzz, &value, minv, maxv);
919 return value;
920}
921
922static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500923 if (!fuzz || !paint || depth <= 0) {
924 return;
925 }
926
Hal Canary1e0138b2017-03-10 13:56:08 -0500927 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
928 paint->setDither( make_fuzz_t<bool>(fuzz));
929 paint->setColor( make_fuzz_t<SkColor>(fuzz));
930 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
931 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
932 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
933 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
934 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
935 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
936 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
937 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500938
939 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500940 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
941 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
942 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
943 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500944 }
945}
946
Hal Canary1e0138b2017-03-10 13:56:08 -0500947static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
948 paint->setTypeface( make_fuzz_typeface(fuzz));
949 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
950 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
951 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
952 paint->setLinearText( make_fuzz_t<bool>(fuzz));
953 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
954 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
955 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
956 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
957 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
958 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
959 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
960 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
961 SkPaint::kFull_Hinting));
962 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -0500963}
964
965static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500966 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -0500967}
968
Hal Canary5395c592017-03-08 16:52:18 -0500969constexpr int kMaxGlyphCount = 30;
970
Hal Canary1e0138b2017-03-10 13:56:08 -0500971static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -0500972 SkTDArray<uint8_t> array;
973 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500974 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
975 : SkTypeface::MakeDefault()->countGlyphs();
Hal Canary671e4422017-02-27 13:36:38 -0500976 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500977 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500978 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
979 for (int i = 0; i < glyphCount; ++i) {
980 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
981 }
982 return array;
983 }
984 static const SkUnichar ranges[][2] = {
985 {0x0020, 0x007F},
986 {0x00A1, 0x0250},
987 {0x0400, 0x0500},
988 };
989 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500990 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500991 count += (ranges[i][1] - ranges[i][0]);
992 }
Hal Canary5395c592017-03-08 16:52:18 -0500993 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500994 SkUnichar buffer[kMaxLength];
995 int length;
996 fuzz->nextRange(&length, 1, kMaxLength);
997 for (int j = 0; j < length; ++j) {
998 int32_t value;
999 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001000 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001001 if (value + ranges[i][0] < ranges[i][1]) {
1002 buffer[j] = value + ranges[i][0];
1003 break;
1004 } else {
1005 value -= (ranges[i][1] - ranges[i][0]);
1006 }
1007 }
1008 }
1009 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001010 case SkPaint::kUTF8_TextEncoding: {
1011 size_t utf8len = 0;
1012 for (int j = 0; j < length; ++j) {
1013 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001014 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001015 char* ptr = (char*)array.append(utf8len);
1016 for (int j = 0; j < length; ++j) {
1017 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001018 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001019 } break;
1020 case SkPaint::kUTF16_TextEncoding: {
1021 size_t utf16len = 0;
1022 for (int j = 0; j < length; ++j) {
1023 utf16len += SkUTF16_FromUnichar(buffer[j]);
1024 }
1025 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1026 for (int j = 0; j < length; ++j) {
1027 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1028 }
1029 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001030 case SkPaint::kUTF32_TextEncoding:
1031 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001032 break;
Hal Canary671e4422017-02-27 13:36:38 -05001033 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001034 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001035 break;
Hal Canary671e4422017-02-27 13:36:38 -05001036 }
1037 return array;
1038}
1039
Hal Canary5395c592017-03-08 16:52:18 -05001040static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1041 SkTextBlobBuilder textBlobBuilder;
1042 int8_t runCount;
1043 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1044 while (runCount-- > 0) {
1045 SkPaint paint;
1046 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001047 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001048 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1049 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1050 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1051 SkASSERT(glyphCount <= kMaxGlyphCount);
1052 SkScalar x, y;
1053 const SkTextBlobBuilder::RunBuffer* buffer;
1054 uint8_t runType;
1055 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1056 switch (runType) {
1057 case 0:
1058 fuzz->next(&x, &y);
1059 // TODO: Test other variations of this.
1060 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1061 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1062 break;
1063 case 1:
1064 fuzz->next(&y);
1065 // TODO: Test other variations of this.
1066 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1067 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1068 fuzz->nextN(buffer->pos, glyphCount);
1069 break;
1070 case 2:
1071 // TODO: Test other variations of this.
1072 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1073 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1074 fuzz->nextN(buffer->pos, glyphCount * 2);
1075 break;
1076 default:
1077 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001078 break;
Hal Canary5395c592017-03-08 16:52:18 -05001079 }
1080 }
1081 return textBlobBuilder.make();
1082}
1083
Hal Canary1e0138b2017-03-10 13:56:08 -05001084static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001085 if (!fuzz || !canvas || depth <= 0) {
1086 return;
1087 }
1088 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1089 unsigned N;
1090 fuzz->nextRange(&N, 0, 2000);
1091 for (unsigned i = 0; i < N; ++i) {
1092 if (fuzz->exhausted()) {
1093 return;
1094 }
1095 SkPaint paint;
1096 SkMatrix matrix;
1097 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001098 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001099 switch (drawCommand) {
1100 case 0:
1101 canvas->flush();
1102 break;
1103 case 1:
1104 canvas->save();
1105 break;
1106 case 2: {
1107 SkRect bounds;
1108 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001109 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001110 canvas->saveLayer(&bounds, &paint);
1111 break;
1112 }
1113 case 3: {
1114 SkRect bounds;
1115 fuzz->next(&bounds);
1116 canvas->saveLayer(&bounds, nullptr);
1117 break;
1118 }
1119 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001120 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001121 canvas->saveLayer(nullptr, &paint);
1122 break;
1123 case 5:
1124 canvas->saveLayer(nullptr, nullptr);
1125 break;
1126 case 6: {
1127 uint8_t alpha;
1128 fuzz->next(&alpha);
1129 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1130 break;
1131 }
1132 case 7: {
1133 SkRect bounds;
1134 uint8_t alpha;
1135 fuzz->next(&bounds, &alpha);
1136 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1137 break;
1138 }
1139 case 8: {
1140 SkCanvas::SaveLayerRec saveLayerRec;
1141 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001142 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001143 fuzz->next(&bounds);
1144 saveLayerRec.fBounds = &bounds;
1145 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001146 if (make_fuzz_t<bool>(fuzz)) {
1147 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001148 saveLayerRec.fPaint = &paint;
1149 }
1150 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001151 if (make_fuzz_t<bool>(fuzz)) {
1152 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001153 saveLayerRec.fBackdrop = imageFilter.get();
1154 }
Hal Canary5395c592017-03-08 16:52:18 -05001155 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001156 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001157 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1158 // }
1159
Hal Canary24ac42b2017-02-14 13:35:14 -05001160 canvas->saveLayer(saveLayerRec);
1161 break;
1162 }
1163 case 9:
1164 canvas->restore();
1165 break;
1166 case 10: {
1167 int saveCount;
1168 fuzz->next(&saveCount);
1169 canvas->restoreToCount(saveCount);
1170 break;
1171 }
1172 case 11: {
1173 SkScalar x, y;
1174 fuzz->next(&x, &y);
1175 canvas->translate(x, y);
1176 break;
1177 }
1178 case 12: {
1179 SkScalar x, y;
1180 fuzz->next(&x, &y);
1181 canvas->scale(x, y);
1182 break;
1183 }
1184 case 13: {
1185 SkScalar v;
1186 fuzz->next(&v);
1187 canvas->rotate(v);
1188 break;
1189 }
1190 case 14: {
1191 SkScalar x, y, v;
1192 fuzz->next(&x, &y, &v);
1193 canvas->rotate(v, x, y);
1194 break;
1195 }
1196 case 15: {
1197 SkScalar x, y;
1198 fuzz->next(&x, &y);
1199 canvas->skew(x, y);
1200 break;
1201 }
1202 case 16: {
1203 SkMatrix mat;
1204 fuzz->next(&mat);
1205 canvas->concat(mat);
1206 break;
1207 }
1208 case 17: {
1209 SkMatrix mat;
1210 fuzz->next(&mat);
1211 canvas->setMatrix(mat);
1212 break;
1213 }
1214 case 18:
1215 canvas->resetMatrix();
1216 break;
1217 case 19: {
1218 SkRect r;
1219 int op;
1220 bool doAntiAlias;
1221 fuzz->next(&r, &doAntiAlias);
1222 fuzz->nextRange(&op, 0, 1);
1223 r.sort();
1224 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1225 break;
1226 }
1227 case 20: {
1228 SkRRect rr;
1229 int op;
1230 bool doAntiAlias;
1231 fuzz->next(&rr);
1232 fuzz->next(&doAntiAlias);
1233 fuzz->nextRange(&op, 0, 1);
1234 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1235 break;
1236 }
1237 case 21: {
1238 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001239 fuzz_path(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001240 int op;
1241 bool doAntiAlias;
1242 fuzz->next(&doAntiAlias);
1243 fuzz->nextRange(&op, 0, 1);
1244 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1245 break;
1246 }
1247 case 22: {
1248 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001249 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001250 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001251 fuzz->nextRange(&op, 0, 1);
1252 canvas->clipRegion(region, (SkClipOp)op);
1253 break;
1254 }
1255 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001256 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001257 canvas->drawPaint(paint);
1258 break;
1259 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001260 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001261 SkCanvas::PointMode pointMode;
1262 fuzz_enum_range(fuzz, &pointMode,
1263 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001264 size_t count;
1265 constexpr int kMaxCount = 30;
1266 fuzz->nextRange(&count, 0, kMaxCount);
1267 SkPoint pts[kMaxCount];
1268 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001269 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001270 break;
1271 }
1272 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001273 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001274 SkRect r;
1275 fuzz->next(&r);
1276 canvas->drawRect(r, paint);
1277 break;
1278 }
1279 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001280 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001281 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001282 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001283 canvas->drawRegion(region, paint);
1284 break;
1285 }
1286 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001287 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001288 SkRect r;
1289 fuzz->next(&r);
1290 canvas->drawOval(r, paint);
1291 break;
1292 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001293 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001294 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001295 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001296 SkRRect rr;
1297 fuzz->next(&rr);
1298 canvas->drawRRect(rr, paint);
1299 break;
1300 }
1301 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001302 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001303 SkRRect orr, irr;
1304 fuzz->next(&orr);
1305 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001306 if (orr.getBounds().contains(irr.getBounds())) {
1307 canvas->drawDRRect(orr, irr, paint);
1308 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001309 break;
1310 }
1311 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001312 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001313 SkRect r;
1314 SkScalar start, sweep;
1315 bool useCenter;
1316 fuzz->next(&r, &start, &sweep, &useCenter);
1317 canvas->drawArc(r, start, sweep, useCenter, paint);
1318 break;
1319 }
1320 case 32: {
1321 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001322 fuzz_path(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001323 canvas->drawPath(path, paint);
1324 break;
1325 }
1326 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001327 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001328 SkScalar left, top;
1329 bool usePaint;
1330 fuzz->next(&left, &top, &usePaint);
1331 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001332 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001333 }
1334 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1335 break;
1336 }
1337 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001338 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001339 SkRect src, dst;
1340 bool usePaint;
1341 fuzz->next(&src, &dst, &usePaint);
1342 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001343 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001344 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001345 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001346 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1347 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001348 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1349 break;
1350 }
1351 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001352 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001353 SkIRect src;
1354 SkRect dst;
1355 bool usePaint;
1356 fuzz->next(&src, &dst, &usePaint);
1357 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001358 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001359 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001360 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1362 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001363 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1364 break;
1365 }
1366 case 36: {
1367 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001368 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001369 SkRect dst;
1370 fuzz->next(&dst, &usePaint);
1371 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001372 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001373 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001374 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001375 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1376 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001377 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1378 break;
1379 }
1380 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001381 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001382 SkIRect center;
1383 SkRect dst;
1384 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001385 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001386 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001387 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001388 }
Hal Canary0361d492017-03-15 12:58:15 -04001389 if (make_fuzz_t<bool>(fuzz)) {
1390 fuzz->next(&center);
1391 } else { // Make valid center, see SkLatticeIter::Valid().
1392 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1393 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1394 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1395 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1396 }
1397 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001398 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1399 break;
1400 }
1401 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001402 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001403 SkScalar left, top;
1404 bool usePaint;
1405 fuzz->next(&left, &top, &usePaint);
1406 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001407 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001408 }
1409 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1410 break;
1411 }
1412 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001413 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001414 SkRect src, dst;
1415 bool usePaint;
1416 fuzz->next(&src, &dst, &usePaint);
1417 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001418 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001419 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001420 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001421 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1422 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001423 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1424 break;
1425 }
1426 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001427 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001428 SkIRect src;
1429 SkRect dst;
1430 bool usePaint;
1431 fuzz->next(&src, &dst, &usePaint);
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 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001435 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001436 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1437 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001438 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1439 break;
1440 }
1441 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001442 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001443 SkRect dst;
1444 bool usePaint;
1445 fuzz->next(&dst, &usePaint);
1446 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001447 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001448 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001449 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001450 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1451 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001452 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1453 break;
1454 }
1455 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001456 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001457 SkIRect center;
1458 SkRect dst;
1459 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001460 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001461 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001462 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001463 }
Hal Canary0361d492017-03-15 12:58:15 -04001464 if (make_fuzz_t<bool>(fuzz)) {
1465 fuzz->next(&center);
1466 } else { // Make valid center, see SkLatticeIter::Valid().
1467 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1468 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1469 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1470 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1471 }
1472 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001473 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1474 break;
1475 }
1476 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001477 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001478 bool usePaint;
1479 SkRect dst;
1480 fuzz->next(&usePaint, &dst);
1481 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001482 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001483 }
1484 constexpr int kMax = 6;
1485 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001486 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001487 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1488 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1489 fuzz->nextN(xDivs, lattice.fXCount);
1490 fuzz->nextN(yDivs, lattice.fYCount);
1491 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1492 break;
1493 }
1494 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001495 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001496 bool usePaint;
1497 SkRect dst;
1498 fuzz->next(&usePaint, &dst);
1499 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001500 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001501 }
1502 constexpr int kMax = 6;
1503 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001504 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001505 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1506 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1507 fuzz->nextN(xDivs, lattice.fXCount);
1508 fuzz->nextN(yDivs, lattice.fYCount);
1509 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1510 break;
1511 }
1512 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001513 fuzz_paint(fuzz, &paint, depth - 1);
1514 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001515 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001516 SkScalar x, y;
1517 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001518 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001519 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001520 break;
1521 }
1522 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001523 fuzz_paint(fuzz, &paint, depth - 1);
1524 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001525 fuzz_paint_text_encoding(fuzz, &paint);
1526 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001527 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001528 if (glyphCount < 1) {
1529 break;
1530 }
1531 SkAutoTMalloc<SkPoint> pos(glyphCount);
1532 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001533 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001534 pos[0] = {0, 0};
1535 for (int i = 1; i < glyphCount; ++i) {
1536 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001537 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001538 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1539 }
Hal Canary671e4422017-02-27 13:36:38 -05001540 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001541 break;
1542 }
1543 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001544 fuzz_paint(fuzz, &paint, depth - 1);
1545 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001546 fuzz_paint_text_encoding(fuzz, &paint);
1547 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001548 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001549 SkAutoTMalloc<SkScalar> widths(glyphCount);
1550 if (glyphCount < 1) {
1551 break;
1552 }
Hal Canary671e4422017-02-27 13:36:38 -05001553 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001554 SkScalar x = widths[0];
1555 for (int i = 0; i < glyphCount; ++i) {
1556 SkTSwap(x, widths[i]);
1557 x += widths[i];
1558 SkScalar offset;
1559 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001560 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001561 widths[i] += offset;
1562 }
1563 SkScalar y;
1564 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001565 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001566 break;
1567 }
1568 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001569 fuzz_paint(fuzz, &paint, depth - 1);
1570 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001571 fuzz_paint_text_encoding(fuzz, &paint);
1572 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001573 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001574 fuzz_path(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001575 SkScalar hOffset, vOffset;
1576 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001577 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1578 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001579 break;
1580 }
1581 case 49: {
1582 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001583 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001584 if (useMatrix) {
1585 fuzz->next(&matrix);
1586 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001587 fuzz_paint(fuzz, &paint, depth - 1);
1588 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001589 fuzz_paint_text_encoding(fuzz, &paint);
1590 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001591 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001592 fuzz_path(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001593 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001594 useMatrix ? &matrix : nullptr, paint);
1595 break;
1596 }
1597 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001598 fuzz_paint(fuzz, &paint, depth - 1);
1599 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001600 fuzz_paint_text_encoding(fuzz, &paint);
1601 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1602 SkRSXform rSXform[kMaxGlyphCount];
1603 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1604 SkASSERT(glyphCount <= kMaxGlyphCount);
1605 fuzz->nextN(rSXform, glyphCount);
1606 SkRect cullRect;
1607 bool useCullRect;
1608 fuzz->next(&useCullRect);
1609 if (useCullRect) {
1610 fuzz->next(&cullRect);
1611 }
1612 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1613 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001614 break;
1615 }
1616 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001617 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001618 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001619 SkScalar x, y;
1620 fuzz->next(&x, &y);
1621 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001622 break;
1623 }
1624 case 52: {
1625 bool usePaint, useMatrix;
1626 fuzz->next(&usePaint, &useMatrix);
1627 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001628 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001629 }
1630 if (useMatrix) {
1631 fuzz->next(&matrix);
1632 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001633 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001634 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1635 usePaint ? &paint : nullptr);
1636 break;
1637 }
1638 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001639 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001640 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001641 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001642 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001643 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001644 constexpr int kMaxCount = 100;
1645 int vertexCount;
1646 SkPoint vertices[kMaxCount];
1647 SkPoint texs[kMaxCount];
1648 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001649 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1650 fuzz->nextN(vertices, vertexCount);
1651 bool useTexs, useColors;
1652 fuzz->next(&useTexs, &useColors);
1653 if (useTexs) {
1654 fuzz->nextN(texs, vertexCount);
1655 }
1656 if (useColors) {
1657 fuzz->nextN(colors, vertexCount);
1658 }
1659 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001660 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001661 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001662 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1663 for (int i = 0; i < indexCount; ++i) {
1664 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1665 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001666 }
Mike Reed887cdf12017-04-03 11:11:09 -04001667 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1668 useTexs ? texs : nullptr,
1669 useColors ? colors : nullptr,
1670 indexCount, indices),
1671 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001672 break;
1673 }
1674 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001675 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001676 break;
1677 }
1678 }
1679}
1680
Hal Canary1e0138b2017-03-10 13:56:08 -05001681static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001682 SkScalar w, h;
1683 fuzz->next(&w, &h);
1684 SkPictureRecorder pictureRecorder;
1685 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1686 return pictureRecorder.finishRecordingAsPicture();
1687}
1688
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001689DEF_FUZZ(NullCanvas, fuzz) {
1690 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001691}
1692
Hal Canary44801ca2017-03-15 11:39:06 -04001693// 8.5x11 letter paper at 72ppi.
1694constexpr SkISize kCanvasSize = {612, 792};
1695
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001696DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001697 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001698 SkASSERT(surface && surface->getCanvas());
1699 fuzz_canvas(fuzz, surface->getCanvas());
1700}
1701
Hal Canarye2924432017-12-01 11:46:26 -05001702DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1703 SkPictureRecorder recorder;
1704 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1705 SkIntToScalar(kCanvasSize.height())));
1706 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1707 if (!pic) { fuzz->signalBug(); }
1708 sk_sp<SkData> data = pic->serialize();
1709 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001710 SkReadBuffer rb(data->data(), data->size());
1711 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001712 if (!deserialized) { fuzz->signalBug(); }
1713 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1714 SkASSERT(surface && surface->getCanvas());
1715 surface->getCanvas()->drawPicture(deserialized);
1716}
1717
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001718DEF_FUZZ(ImageFilter, fuzz) {
1719 auto fil = make_fuzz_imageFilter(fuzz, 20);
1720
1721 SkPaint paint;
1722 paint.setImageFilter(fil);
1723 SkBitmap bitmap;
1724 SkCanvas canvas(bitmap);
1725 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1726}
1727
1728
1729//SkRandom _rand;
1730#define SK_ADD_RANDOM_BIT_FLIPS
1731
1732DEF_FUZZ(SerializedImageFilter, fuzz) {
1733 auto filter = make_fuzz_imageFilter(fuzz, 20);
1734 auto data = filter->serialize();
1735 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1736 size_t len = data->size();
1737#ifdef SK_ADD_RANDOM_BIT_FLIPS
1738 unsigned char* p = const_cast<unsigned char*>(ptr);
1739 for (size_t i = 0; i < len; ++i, ++p) {
1740 uint8_t j;
1741 fuzz->nextRange(&j, 1, 250);
1742 if (j == 1) { // 0.4% of the time, flip a bit or byte
1743 uint8_t k;
1744 fuzz->nextRange(&k, 1, 10);
1745 if (k == 1) { // Then 10% of the time, change a whole byte
1746 uint8_t s;
1747 fuzz->nextRange(&s, 0, 2);
1748 switch(s) {
1749 case 0:
1750 *p ^= 0xFF; // Flip entire byte
1751 break;
1752 case 1:
1753 *p = 0xFF; // Set all bits to 1
1754 break;
1755 case 2:
1756 *p = 0x00; // Set all bits to 0
1757 break;
1758 }
1759 } else {
1760 uint8_t s;
1761 fuzz->nextRange(&s, 0, 7);
1762 *p ^= (1 << 7);
1763 }
1764 }
1765 }
1766#endif // SK_ADD_RANDOM_BIT_FLIPS
1767 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1768
1769 // uncomment below to write out a serialized image filter (to make corpus
1770 // for -t filter_fuzz)
1771 // SkString s("./serialized_filters/sf");
1772 // s.appendU32(_rand.nextU());
1773 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1774 // sk_fwrite(data->bytes(), data->size(), file);
1775 // sk_fclose(file);
1776
1777 SkPaint paint;
1778 paint.setImageFilter(deserializedFil);
1779 SkBitmap bitmap;
1780 SkCanvas canvas(bitmap);
1781 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1782}
1783
Hal Canary44801ca2017-03-15 11:39:06 -04001784#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001785static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1786 SkASSERT(context);
1787 auto surface = SkSurface::MakeRenderTarget(
1788 context,
1789 SkBudgeted::kNo,
1790 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1791 SkASSERT(surface && surface->getCanvas());
1792 fuzz_canvas(fuzz, surface->getCanvas());
1793}
1794
Hal Canary44801ca2017-03-15 11:39:06 -04001795DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001796 sk_gpu_test::GrContextFactory f;
1797 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001798 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001799 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001800 }
Hal Canary5aa91582017-03-21 11:11:44 -07001801 fuzz_ganesh(fuzz, context);
1802}
1803
1804DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001805 sk_gpu_test::GrContextFactory f;
1806 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001807}
1808
1809DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001810 sk_gpu_test::GrContextFactory f;
1811 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001812}
1813#endif
1814
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001815DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001816 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001817 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001818 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1819 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001820}
1821
1822// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1823DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001824 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001825 fuzz_canvas(fuzz, &debugCanvas);
1826 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1827 UrlDataManager dataManager(SkString("data"));
1828 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1829 Json::StyledStreamWriter(" ").write(std::cout, json);
1830}