blob: c7f141979b203d67f97e909e960b0d90ed1d4877 [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;
Cary Clark91390c82018-03-09 14:02:46 -0500450 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500451 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;
Cary Clark91390c82018-03-09 14:02:46 -0500465 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500466 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;
Mike Reed1be1f8d2018-03-14 13:01:17 -0400512 fuzz->nextRange(&flags, 0, 1);
513 bool respectCTM = flags != 0;
514 return SkMaskFilter::MakeBlur(blurStyle, sigma, occluder, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500515 }
516 case 2: {
517 SkRRect first, second;
518 SkScalar radius;
519 fuzz->next(&first, &second, &radius);
520 return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
521 }
522 default:
523 SkASSERT(false);
524 return nullptr;
525 }
526}
Hal Canary24ac42b2017-02-14 13:35:14 -0500527
Hal Canary1e0138b2017-03-10 13:56:08 -0500528static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
529 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500530 return nullptr;
531 }
532 auto fontMugger = SkFontMgr::RefDefault();
533 SkASSERT(fontMugger);
534 int familyCount = fontMugger->countFamilies();
535 int i, j;
536 fuzz->nextRange(&i, 0, familyCount - 1);
537 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
538 int styleCount = family->count();
539 fuzz->nextRange(&j, 0, styleCount - 1);
540 return sk_sp<SkTypeface>(family->createTypeface(j));
541}
Hal Canary24ac42b2017-02-14 13:35:14 -0500542
Hal Canarye03c3e52017-03-09 11:33:35 -0500543template <>
544inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
545 SkRect rect;
546 uint8_t flags;
547 this->next(&rect);
548 this->nextRange(&flags, 0, 0xF);
549 *cropRect = SkImageFilter::CropRect(rect, flags);
550}
551
Hal Canary1e0138b2017-03-10 13:56:08 -0500552static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500553
554static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
555 if (depth <= 0) {
556 return nullptr;
557 }
558 uint8_t imageFilterType;
559 fuzz->nextRange(&imageFilterType, 1, 6);
560 SkPoint3 p, q;
561 SkColor lightColor;
562 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
563 sk_sp<SkImageFilter> input;
564 SkImageFilter::CropRect cropRect;
565 bool useCropRect;
566 fuzz->next(&useCropRect);
567 if (useCropRect) {
568 fuzz->next(&cropRect);
569 }
570 switch (imageFilterType) {
571 case 1:
572 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500573 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500574 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
575 std::move(input),
576 useCropRect ? &cropRect : nullptr);
577 case 2:
578 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500579 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500580 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
581 std::move(input),
582 useCropRect ? &cropRect : nullptr);
583 case 3:
584 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500585 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500586 return SkLightingImageFilter::MakeSpotLitDiffuse(
587 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
588 std::move(input), useCropRect ? &cropRect : nullptr);
589 case 4:
590 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500591 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500592 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
593 shininess, std::move(input),
594 useCropRect ? &cropRect : nullptr);
595 case 5:
596 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500597 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500598 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
599 shininess, std::move(input),
600 useCropRect ? &cropRect : nullptr);
601 case 6:
602 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
603 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500604 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500605 return SkLightingImageFilter::MakeSpotLitSpecular(
606 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
607 std::move(input), useCropRect ? &cropRect : nullptr);
608 default:
609 SkASSERT(false);
610 return nullptr;
611 }
612}
613
Hal Canary1e0138b2017-03-10 13:56:08 -0500614static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500615
Hal Canary1e0138b2017-03-10 13:56:08 -0500616static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500617 if (depth <= 0) {
618 return nullptr;
619 }
620 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500621 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500622 switch (imageFilterType) {
623 case 0:
624 return nullptr;
625 case 1: {
626 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500627 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500628 bool useCropRect;
629 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400630 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500631 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400632 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500633 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400634 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500635 useCropRect ? &cropRect : nullptr);
636 }
637 case 2: {
638 SkMatrix matrix;
639 SkFilterQuality quality;
640 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500641 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500642 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
643 }
644 case 3: {
645 SkRegion region;
646 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500647 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500648 bool useCropRect;
649 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400650 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500651 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400652 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500653 }
654 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
655 useCropRect ? &cropRect : nullptr);
656 }
657 case 4: {
658 float k1, k2, k3, k4;
659 bool enforcePMColor;
660 bool useCropRect;
661 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500662 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
663 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500664 SkImageFilter::CropRect cropRect;
665 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400666 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500667 }
668 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
669 std::move(background), std::move(foreground),
670 useCropRect ? &cropRect : nullptr);
671 }
672 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500673 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
674 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500675 bool useCropRect;
676 SkImageFilter::CropRect cropRect;
677 fuzz->next(&useCropRect);
678 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400679 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500680 }
681 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
682 useCropRect ? &cropRect : nullptr);
683 }
684 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500685 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
686 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500687 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
688 }
689 case 7: {
690 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400691 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
692 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500693 SkScalar scale;
694 bool useCropRect;
695 fuzz->next(&scale, &useCropRect);
696 SkImageFilter::CropRect cropRect;
697 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400698 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500699 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500700 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
701 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500702 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
703 std::move(displacement), std::move(color),
704 useCropRect ? &cropRect : nullptr);
705 }
706 case 8: {
707 SkScalar dx, dy, sigmaX, sigmaY;
708 SkColor color;
709 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500710 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500711 bool useCropRect;
712 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
713 SkImageFilter::CropRect cropRect;
714 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400715 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500716 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500717 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500718 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
719 std::move(input),
720 useCropRect ? &cropRect : nullptr);
721 }
722 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500723 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500724 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500725 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500726 SkRect srcRect, dstRect;
727 SkFilterQuality filterQuality;
728 fuzz->next(&srcRect, &dstRect, &filterQuality);
729 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
730 }
731 case 11:
732 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
733 case 12: {
734 SkRect srcRect;
735 SkScalar inset;
736 bool useCropRect;
737 SkImageFilter::CropRect cropRect;
738 fuzz->next(&srcRect, &inset, &useCropRect);
739 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400740 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500741 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500742 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500743 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
744 useCropRect ? &cropRect : nullptr);
745 }
746 case 13: {
747 constexpr int kMaxKernelSize = 5;
748 int32_t n, m;
749 fuzz->nextRange(&n, 1, kMaxKernelSize);
750 fuzz->nextRange(&m, 1, kMaxKernelSize);
751 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
752 fuzz->nextN(kernel, n * m);
753 int32_t offsetX, offsetY;
754 fuzz->nextRange(&offsetX, 0, n - 1);
755 fuzz->nextRange(&offsetY, 0, m - 1);
756 SkScalar gain, bias;
757 bool convolveAlpha, useCropRect;
758 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
759 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500760 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500761 SkImageFilter::CropRect cropRect;
762 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400763 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500764 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500765 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500766 return SkMatrixConvolutionImageFilter::Make(
767 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
768 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
769 }
770 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500771 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
772 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500773 bool useCropRect;
774 fuzz->next(&useCropRect);
775 SkImageFilter::CropRect cropRect;
776 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400777 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500778 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400779 return SkMergeImageFilter::Make(std::move(first), std::move(second),
780 useCropRect ? &cropRect : nullptr);
781 }
782 case 15: {
783 constexpr int kMaxCount = 4;
784 sk_sp<SkImageFilter> ifs[kMaxCount];
785 int count;
786 fuzz->nextRange(&count, 1, kMaxCount);
787 for (int i = 0; i < count; ++i) {
788 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
789 }
790 bool useCropRect;
791 fuzz->next(&useCropRect);
792 SkImageFilter::CropRect cropRect;
793 if (useCropRect) {
794 fuzz->next(&cropRect);
795 }
796 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500797 }
798 case 16: {
799 int rx, ry;
800 fuzz->next(&rx, &ry);
801 bool useCropRect;
802 fuzz->next(&useCropRect);
803 SkImageFilter::CropRect cropRect;
804 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400805 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500806 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500807 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500808 return SkDilateImageFilter::Make(rx, ry, std::move(input),
809 useCropRect ? &cropRect : nullptr);
810 }
811 case 17: {
812 int rx, ry;
813 fuzz->next(&rx, &ry);
814 bool useCropRect;
815 fuzz->next(&useCropRect);
816 SkImageFilter::CropRect cropRect;
817 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400818 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500819 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500820 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500821 return SkErodeImageFilter::Make(rx, ry, std::move(input),
822 useCropRect ? &cropRect : nullptr);
823 }
824 case 18: {
825 SkScalar dx, dy;
826 fuzz->next(&dx, &dy);
827 bool useCropRect;
828 fuzz->next(&useCropRect);
829 SkImageFilter::CropRect cropRect;
830 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400831 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500832 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500833 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500834 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
835 useCropRect ? &cropRect : nullptr);
836 }
837 case 19: {
838 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500839 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500840 bool useCropRect;
841 fuzz->next(&useCropRect);
842 SkImageFilter::CropRect cropRect;
843 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400844 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500845 }
846 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
847 }
848 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500849 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500850 return SkPictureImageFilter::Make(std::move(picture));
851 }
852 case 21: {
853 SkRect cropRect;
854 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500855 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500856 return SkPictureImageFilter::Make(std::move(picture), cropRect);
857 }
858 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500859 SkRect src, dst;
860 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500861 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500862 return SkTileImageFilter::Make(src, dst, std::move(input));
863 }
Mike Reed77e487d2017-11-09 21:50:20 +0000864 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500865 SkBlendMode blendMode;
866 bool useCropRect;
867 fuzz->next(&useCropRect, &blendMode);
868 SkImageFilter::CropRect cropRect;
869 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400870 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500871 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500872 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
873 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500874 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
875 useCropRect ? &cropRect : nullptr);
876 }
877 default:
878 SkASSERT(false);
879 return nullptr;
880 }
881}
Hal Canary24ac42b2017-02-14 13:35:14 -0500882
Hal Canary1e0138b2017-03-10 13:56:08 -0500883static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500884 int w, h;
885 fuzz->nextRange(&w, 1, 1024);
886 fuzz->nextRange(&h, 1, 1024);
887 SkAutoTMalloc<SkPMColor> data(w * h);
888 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
889 int n = w * h;
890 for (int i = 0; i < n; ++i) {
891 SkColor c;
892 fuzz->next(&c);
893 data[i] = SkPreMultiplyColor(c);
894 }
895 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500896 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
897 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500898}
899
Hal Canary1e0138b2017-03-10 13:56:08 -0500900static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500901 SkBitmap bitmap;
902 int w, h;
903 fuzz->nextRange(&w, 1, 1024);
904 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500905 if (!bitmap.tryAllocN32Pixels(w, h)) {
906 SkDEBUGF(("Could not allocate pixels %d x %d", w, h));
907 return bitmap;
908 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500909 for (int y = 0; y < h; ++y) {
910 for (int x = 0; x < w; ++x) {
911 SkColor c;
912 fuzz->next(&c);
913 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
914 }
915 }
916 return bitmap;
917}
918
Hal Canary1e0138b2017-03-10 13:56:08 -0500919template <typename T, typename Min, typename Max>
920inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
921 T value;
922 fuzz_enum_range(fuzz, &value, minv, maxv);
923 return value;
924}
925
926static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500927 if (!fuzz || !paint || depth <= 0) {
928 return;
929 }
930
Hal Canary1e0138b2017-03-10 13:56:08 -0500931 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
932 paint->setDither( make_fuzz_t<bool>(fuzz));
933 paint->setColor( make_fuzz_t<SkColor>(fuzz));
934 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
935 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
936 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
937 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
938 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
939 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
940 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
941 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500942
943 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500944 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
945 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
946 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
947 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500948 }
949}
950
Hal Canary1e0138b2017-03-10 13:56:08 -0500951static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
952 paint->setTypeface( make_fuzz_typeface(fuzz));
953 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
954 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
955 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
956 paint->setLinearText( make_fuzz_t<bool>(fuzz));
957 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
958 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
959 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
960 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
961 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
962 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
963 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
964 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
965 SkPaint::kFull_Hinting));
966 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -0500967}
968
969static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500970 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -0500971}
972
Hal Canary5395c592017-03-08 16:52:18 -0500973constexpr int kMaxGlyphCount = 30;
974
Hal Canary1e0138b2017-03-10 13:56:08 -0500975static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -0500976 SkTDArray<uint8_t> array;
977 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500978 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
979 : SkTypeface::MakeDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500980 if (glyphRange == 0) {
981 // Some fuzzing environments have no fonts, so empty array is the best
982 // we can do.
983 return array;
984 }
Hal Canary671e4422017-02-27 13:36:38 -0500985 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500986 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500987 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
988 for (int i = 0; i < glyphCount; ++i) {
989 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
990 }
991 return array;
992 }
993 static const SkUnichar ranges[][2] = {
994 {0x0020, 0x007F},
995 {0x00A1, 0x0250},
996 {0x0400, 0x0500},
997 };
998 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500999 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001000 count += (ranges[i][1] - ranges[i][0]);
1001 }
Hal Canary5395c592017-03-08 16:52:18 -05001002 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -05001003 SkUnichar buffer[kMaxLength];
1004 int length;
1005 fuzz->nextRange(&length, 1, kMaxLength);
1006 for (int j = 0; j < length; ++j) {
1007 int32_t value;
1008 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001009 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001010 if (value + ranges[i][0] < ranges[i][1]) {
1011 buffer[j] = value + ranges[i][0];
1012 break;
1013 } else {
1014 value -= (ranges[i][1] - ranges[i][0]);
1015 }
1016 }
1017 }
1018 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001019 case SkPaint::kUTF8_TextEncoding: {
1020 size_t utf8len = 0;
1021 for (int j = 0; j < length; ++j) {
1022 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001023 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001024 char* ptr = (char*)array.append(utf8len);
1025 for (int j = 0; j < length; ++j) {
1026 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001027 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001028 } break;
1029 case SkPaint::kUTF16_TextEncoding: {
1030 size_t utf16len = 0;
1031 for (int j = 0; j < length; ++j) {
1032 utf16len += SkUTF16_FromUnichar(buffer[j]);
1033 }
1034 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1035 for (int j = 0; j < length; ++j) {
1036 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1037 }
1038 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001039 case SkPaint::kUTF32_TextEncoding:
1040 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001041 break;
Hal Canary671e4422017-02-27 13:36:38 -05001042 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001043 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001044 break;
Hal Canary671e4422017-02-27 13:36:38 -05001045 }
1046 return array;
1047}
1048
Hal Canary5395c592017-03-08 16:52:18 -05001049static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1050 SkTextBlobBuilder textBlobBuilder;
1051 int8_t runCount;
1052 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1053 while (runCount-- > 0) {
1054 SkPaint paint;
1055 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001056 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001057 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1058 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1059 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1060 SkASSERT(glyphCount <= kMaxGlyphCount);
1061 SkScalar x, y;
1062 const SkTextBlobBuilder::RunBuffer* buffer;
1063 uint8_t runType;
1064 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1065 switch (runType) {
1066 case 0:
1067 fuzz->next(&x, &y);
1068 // TODO: Test other variations of this.
1069 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1070 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1071 break;
1072 case 1:
1073 fuzz->next(&y);
1074 // TODO: Test other variations of this.
1075 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1076 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1077 fuzz->nextN(buffer->pos, glyphCount);
1078 break;
1079 case 2:
1080 // TODO: Test other variations of this.
1081 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1082 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1083 fuzz->nextN(buffer->pos, glyphCount * 2);
1084 break;
1085 default:
1086 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001087 break;
Hal Canary5395c592017-03-08 16:52:18 -05001088 }
1089 }
1090 return textBlobBuilder.make();
1091}
1092
Hal Canary1e0138b2017-03-10 13:56:08 -05001093static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001094 if (!fuzz || !canvas || depth <= 0) {
1095 return;
1096 }
1097 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1098 unsigned N;
1099 fuzz->nextRange(&N, 0, 2000);
1100 for (unsigned i = 0; i < N; ++i) {
1101 if (fuzz->exhausted()) {
1102 return;
1103 }
1104 SkPaint paint;
1105 SkMatrix matrix;
1106 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001107 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001108 switch (drawCommand) {
1109 case 0:
1110 canvas->flush();
1111 break;
1112 case 1:
1113 canvas->save();
1114 break;
1115 case 2: {
1116 SkRect bounds;
1117 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001118 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001119 canvas->saveLayer(&bounds, &paint);
1120 break;
1121 }
1122 case 3: {
1123 SkRect bounds;
1124 fuzz->next(&bounds);
1125 canvas->saveLayer(&bounds, nullptr);
1126 break;
1127 }
1128 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001129 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001130 canvas->saveLayer(nullptr, &paint);
1131 break;
1132 case 5:
1133 canvas->saveLayer(nullptr, nullptr);
1134 break;
1135 case 6: {
1136 uint8_t alpha;
1137 fuzz->next(&alpha);
1138 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1139 break;
1140 }
1141 case 7: {
1142 SkRect bounds;
1143 uint8_t alpha;
1144 fuzz->next(&bounds, &alpha);
1145 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1146 break;
1147 }
1148 case 8: {
1149 SkCanvas::SaveLayerRec saveLayerRec;
1150 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001151 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001152 fuzz->next(&bounds);
1153 saveLayerRec.fBounds = &bounds;
1154 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001155 if (make_fuzz_t<bool>(fuzz)) {
1156 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001157 saveLayerRec.fPaint = &paint;
1158 }
1159 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001160 if (make_fuzz_t<bool>(fuzz)) {
1161 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001162 saveLayerRec.fBackdrop = imageFilter.get();
1163 }
Hal Canary5395c592017-03-08 16:52:18 -05001164 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001165 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001166 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1167 // }
1168
Hal Canary24ac42b2017-02-14 13:35:14 -05001169 canvas->saveLayer(saveLayerRec);
1170 break;
1171 }
1172 case 9:
1173 canvas->restore();
1174 break;
1175 case 10: {
1176 int saveCount;
1177 fuzz->next(&saveCount);
1178 canvas->restoreToCount(saveCount);
1179 break;
1180 }
1181 case 11: {
1182 SkScalar x, y;
1183 fuzz->next(&x, &y);
1184 canvas->translate(x, y);
1185 break;
1186 }
1187 case 12: {
1188 SkScalar x, y;
1189 fuzz->next(&x, &y);
1190 canvas->scale(x, y);
1191 break;
1192 }
1193 case 13: {
1194 SkScalar v;
1195 fuzz->next(&v);
1196 canvas->rotate(v);
1197 break;
1198 }
1199 case 14: {
1200 SkScalar x, y, v;
1201 fuzz->next(&x, &y, &v);
1202 canvas->rotate(v, x, y);
1203 break;
1204 }
1205 case 15: {
1206 SkScalar x, y;
1207 fuzz->next(&x, &y);
1208 canvas->skew(x, y);
1209 break;
1210 }
1211 case 16: {
1212 SkMatrix mat;
1213 fuzz->next(&mat);
1214 canvas->concat(mat);
1215 break;
1216 }
1217 case 17: {
1218 SkMatrix mat;
1219 fuzz->next(&mat);
1220 canvas->setMatrix(mat);
1221 break;
1222 }
1223 case 18:
1224 canvas->resetMatrix();
1225 break;
1226 case 19: {
1227 SkRect r;
1228 int op;
1229 bool doAntiAlias;
1230 fuzz->next(&r, &doAntiAlias);
1231 fuzz->nextRange(&op, 0, 1);
1232 r.sort();
1233 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1234 break;
1235 }
1236 case 20: {
1237 SkRRect rr;
1238 int op;
1239 bool doAntiAlias;
1240 fuzz->next(&rr);
1241 fuzz->next(&doAntiAlias);
1242 fuzz->nextRange(&op, 0, 1);
1243 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1244 break;
1245 }
1246 case 21: {
1247 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001248 FuzzPath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001249 int op;
1250 bool doAntiAlias;
1251 fuzz->next(&doAntiAlias);
1252 fuzz->nextRange(&op, 0, 1);
1253 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1254 break;
1255 }
1256 case 22: {
1257 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001258 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001259 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001260 fuzz->nextRange(&op, 0, 1);
1261 canvas->clipRegion(region, (SkClipOp)op);
1262 break;
1263 }
1264 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001265 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001266 canvas->drawPaint(paint);
1267 break;
1268 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001269 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001270 SkCanvas::PointMode pointMode;
1271 fuzz_enum_range(fuzz, &pointMode,
1272 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001273 size_t count;
1274 constexpr int kMaxCount = 30;
1275 fuzz->nextRange(&count, 0, kMaxCount);
1276 SkPoint pts[kMaxCount];
1277 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001278 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001279 break;
1280 }
1281 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001282 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001283 SkRect r;
1284 fuzz->next(&r);
1285 canvas->drawRect(r, paint);
1286 break;
1287 }
1288 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001289 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001290 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001291 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001292 canvas->drawRegion(region, paint);
1293 break;
1294 }
1295 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001296 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001297 SkRect r;
1298 fuzz->next(&r);
1299 canvas->drawOval(r, paint);
1300 break;
1301 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001302 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001303 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001304 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001305 SkRRect rr;
1306 fuzz->next(&rr);
1307 canvas->drawRRect(rr, paint);
1308 break;
1309 }
1310 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001311 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001312 SkRRect orr, irr;
1313 fuzz->next(&orr);
1314 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001315 if (orr.getBounds().contains(irr.getBounds())) {
1316 canvas->drawDRRect(orr, irr, paint);
1317 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001318 break;
1319 }
1320 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001321 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001322 SkRect r;
1323 SkScalar start, sweep;
1324 bool useCenter;
1325 fuzz->next(&r, &start, &sweep, &useCenter);
1326 canvas->drawArc(r, start, sweep, useCenter, paint);
1327 break;
1328 }
1329 case 32: {
1330 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001331 FuzzPath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001332 canvas->drawPath(path, paint);
1333 break;
1334 }
1335 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001336 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001337 SkScalar left, top;
1338 bool usePaint;
1339 fuzz->next(&left, &top, &usePaint);
1340 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001341 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001342 }
1343 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1344 break;
1345 }
1346 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001347 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001348 SkRect src, dst;
1349 bool usePaint;
1350 fuzz->next(&src, &dst, &usePaint);
1351 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001352 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001353 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001354 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1356 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001357 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1358 break;
1359 }
1360 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 SkIRect src;
1363 SkRect dst;
1364 bool usePaint;
1365 fuzz->next(&src, &dst, &usePaint);
1366 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001367 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001368 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001369 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001370 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1371 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001372 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1373 break;
1374 }
1375 case 36: {
1376 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001377 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001378 SkRect dst;
1379 fuzz->next(&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->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1387 break;
1388 }
1389 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001390 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001391 SkIRect center;
1392 SkRect dst;
1393 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001394 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001395 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001396 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001397 }
Hal Canary0361d492017-03-15 12:58:15 -04001398 if (make_fuzz_t<bool>(fuzz)) {
1399 fuzz->next(&center);
1400 } else { // Make valid center, see SkLatticeIter::Valid().
1401 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1402 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1403 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1404 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1405 }
1406 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001407 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1408 break;
1409 }
1410 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001411 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001412 SkScalar left, top;
1413 bool usePaint;
1414 fuzz->next(&left, &top, &usePaint);
1415 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001416 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001417 }
1418 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1419 break;
1420 }
1421 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001422 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001423 SkRect src, dst;
1424 bool usePaint;
1425 fuzz->next(&src, &dst, &usePaint);
1426 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001427 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001428 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001429 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001430 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1431 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001432 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1433 break;
1434 }
1435 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001436 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001437 SkIRect src;
1438 SkRect dst;
1439 bool usePaint;
1440 fuzz->next(&src, &dst, &usePaint);
1441 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001442 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001443 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001444 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001445 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1446 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001447 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1448 break;
1449 }
1450 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001451 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001452 SkRect dst;
1453 bool usePaint;
1454 fuzz->next(&dst, &usePaint);
1455 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001456 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001457 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001458 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001459 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1460 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001461 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1462 break;
1463 }
1464 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001465 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001466 SkIRect center;
1467 SkRect dst;
1468 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001469 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001470 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001471 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001472 }
Hal Canary0361d492017-03-15 12:58:15 -04001473 if (make_fuzz_t<bool>(fuzz)) {
1474 fuzz->next(&center);
1475 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001476 if (img.width() == 0 || img.height() == 0) {
1477 // bitmap may not have had its pixels initialized.
1478 break;
1479 }
Hal Canary0361d492017-03-15 12:58:15 -04001480 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1481 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1482 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1483 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1484 }
1485 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001486 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1487 break;
1488 }
1489 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001490 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001491 bool usePaint;
1492 SkRect dst;
1493 fuzz->next(&usePaint, &dst);
1494 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001495 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001496 }
1497 constexpr int kMax = 6;
1498 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001499 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001500 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1501 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1502 fuzz->nextN(xDivs, lattice.fXCount);
1503 fuzz->nextN(yDivs, lattice.fYCount);
1504 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1505 break;
1506 }
1507 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001508 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001509 bool usePaint;
1510 SkRect dst;
1511 fuzz->next(&usePaint, &dst);
1512 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001513 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001514 }
1515 constexpr int kMax = 6;
1516 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001517 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001518 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1519 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1520 fuzz->nextN(xDivs, lattice.fXCount);
1521 fuzz->nextN(yDivs, lattice.fYCount);
1522 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1523 break;
1524 }
1525 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001526 fuzz_paint(fuzz, &paint, depth - 1);
1527 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001528 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001529 SkScalar x, y;
1530 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001531 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001532 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001533 break;
1534 }
1535 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001536 fuzz_paint(fuzz, &paint, depth - 1);
1537 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001538 fuzz_paint_text_encoding(fuzz, &paint);
1539 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001540 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001541 if (glyphCount < 1) {
1542 break;
1543 }
1544 SkAutoTMalloc<SkPoint> pos(glyphCount);
1545 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001546 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001547 pos[0] = {0, 0};
1548 for (int i = 1; i < glyphCount; ++i) {
1549 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001550 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001551 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1552 }
Hal Canary671e4422017-02-27 13:36:38 -05001553 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001554 break;
1555 }
1556 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001557 fuzz_paint(fuzz, &paint, depth - 1);
1558 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001559 fuzz_paint_text_encoding(fuzz, &paint);
1560 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001561 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001562 SkAutoTMalloc<SkScalar> widths(glyphCount);
1563 if (glyphCount < 1) {
1564 break;
1565 }
Hal Canary671e4422017-02-27 13:36:38 -05001566 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001567 SkScalar x = widths[0];
1568 for (int i = 0; i < glyphCount; ++i) {
1569 SkTSwap(x, widths[i]);
1570 x += widths[i];
1571 SkScalar offset;
1572 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001573 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001574 widths[i] += offset;
1575 }
1576 SkScalar y;
1577 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001578 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001579 break;
1580 }
1581 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001582 fuzz_paint(fuzz, &paint, depth - 1);
1583 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001584 fuzz_paint_text_encoding(fuzz, &paint);
1585 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001586 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001587 FuzzPath(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001588 SkScalar hOffset, vOffset;
1589 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001590 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1591 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001592 break;
1593 }
1594 case 49: {
1595 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001596 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001597 if (useMatrix) {
1598 fuzz->next(&matrix);
1599 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001600 fuzz_paint(fuzz, &paint, depth - 1);
1601 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001602 fuzz_paint_text_encoding(fuzz, &paint);
1603 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001604 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001605 FuzzPath(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001606 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001607 useMatrix ? &matrix : nullptr, paint);
1608 break;
1609 }
1610 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001611 fuzz_paint(fuzz, &paint, depth - 1);
1612 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001613 fuzz_paint_text_encoding(fuzz, &paint);
1614 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1615 SkRSXform rSXform[kMaxGlyphCount];
1616 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1617 SkASSERT(glyphCount <= kMaxGlyphCount);
1618 fuzz->nextN(rSXform, glyphCount);
1619 SkRect cullRect;
1620 bool useCullRect;
1621 fuzz->next(&useCullRect);
1622 if (useCullRect) {
1623 fuzz->next(&cullRect);
1624 }
1625 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1626 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001627 break;
1628 }
1629 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001630 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001631 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001632 SkScalar x, y;
1633 fuzz->next(&x, &y);
1634 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001635 break;
1636 }
1637 case 52: {
1638 bool usePaint, useMatrix;
1639 fuzz->next(&usePaint, &useMatrix);
1640 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001641 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001642 }
1643 if (useMatrix) {
1644 fuzz->next(&matrix);
1645 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001646 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001647 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1648 usePaint ? &paint : nullptr);
1649 break;
1650 }
1651 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001652 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001653 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001654 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001655 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001656 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001657 constexpr int kMaxCount = 100;
1658 int vertexCount;
1659 SkPoint vertices[kMaxCount];
1660 SkPoint texs[kMaxCount];
1661 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001662 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1663 fuzz->nextN(vertices, vertexCount);
1664 bool useTexs, useColors;
1665 fuzz->next(&useTexs, &useColors);
1666 if (useTexs) {
1667 fuzz->nextN(texs, vertexCount);
1668 }
1669 if (useColors) {
1670 fuzz->nextN(colors, vertexCount);
1671 }
1672 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001673 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001674 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001675 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1676 for (int i = 0; i < indexCount; ++i) {
1677 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1678 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001679 }
Mike Reed887cdf12017-04-03 11:11:09 -04001680 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1681 useTexs ? texs : nullptr,
1682 useColors ? colors : nullptr,
1683 indexCount, indices),
1684 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001685 break;
1686 }
1687 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001688 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001689 break;
1690 }
1691 }
1692}
1693
Hal Canary1e0138b2017-03-10 13:56:08 -05001694static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001695 SkScalar w, h;
1696 fuzz->next(&w, &h);
1697 SkPictureRecorder pictureRecorder;
1698 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1699 return pictureRecorder.finishRecordingAsPicture();
1700}
1701
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001702DEF_FUZZ(NullCanvas, fuzz) {
1703 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001704}
1705
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001706constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001707
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001708DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001709 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001710 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001711 fuzz_canvas(fuzz, surface->getCanvas());
1712}
1713
Hal Canarye2924432017-12-01 11:46:26 -05001714DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1715 SkPictureRecorder recorder;
1716 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1717 SkIntToScalar(kCanvasSize.height())));
1718 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1719 if (!pic) { fuzz->signalBug(); }
1720 sk_sp<SkData> data = pic->serialize();
1721 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001722 SkReadBuffer rb(data->data(), data->size());
1723 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001724 if (!deserialized) { fuzz->signalBug(); }
1725 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1726 SkASSERT(surface && surface->getCanvas());
1727 surface->getCanvas()->drawPicture(deserialized);
1728}
1729
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001730DEF_FUZZ(ImageFilter, fuzz) {
1731 auto fil = make_fuzz_imageFilter(fuzz, 20);
1732
1733 SkPaint paint;
1734 paint.setImageFilter(fil);
1735 SkBitmap bitmap;
1736 SkCanvas canvas(bitmap);
1737 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1738}
1739
1740
1741//SkRandom _rand;
1742#define SK_ADD_RANDOM_BIT_FLIPS
1743
1744DEF_FUZZ(SerializedImageFilter, fuzz) {
1745 auto filter = make_fuzz_imageFilter(fuzz, 20);
1746 auto data = filter->serialize();
1747 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1748 size_t len = data->size();
1749#ifdef SK_ADD_RANDOM_BIT_FLIPS
1750 unsigned char* p = const_cast<unsigned char*>(ptr);
1751 for (size_t i = 0; i < len; ++i, ++p) {
1752 uint8_t j;
1753 fuzz->nextRange(&j, 1, 250);
1754 if (j == 1) { // 0.4% of the time, flip a bit or byte
1755 uint8_t k;
1756 fuzz->nextRange(&k, 1, 10);
1757 if (k == 1) { // Then 10% of the time, change a whole byte
1758 uint8_t s;
1759 fuzz->nextRange(&s, 0, 2);
1760 switch(s) {
1761 case 0:
1762 *p ^= 0xFF; // Flip entire byte
1763 break;
1764 case 1:
1765 *p = 0xFF; // Set all bits to 1
1766 break;
1767 case 2:
1768 *p = 0x00; // Set all bits to 0
1769 break;
1770 }
1771 } else {
1772 uint8_t s;
1773 fuzz->nextRange(&s, 0, 7);
1774 *p ^= (1 << 7);
1775 }
1776 }
1777 }
1778#endif // SK_ADD_RANDOM_BIT_FLIPS
1779 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1780
1781 // uncomment below to write out a serialized image filter (to make corpus
1782 // for -t filter_fuzz)
1783 // SkString s("./serialized_filters/sf");
1784 // s.appendU32(_rand.nextU());
1785 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1786 // sk_fwrite(data->bytes(), data->size(), file);
1787 // sk_fclose(file);
1788
1789 SkPaint paint;
1790 paint.setImageFilter(deserializedFil);
1791 SkBitmap bitmap;
1792 SkCanvas canvas(bitmap);
1793 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1794}
1795
Hal Canary44801ca2017-03-15 11:39:06 -04001796#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001797static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1798 SkASSERT(context);
1799 auto surface = SkSurface::MakeRenderTarget(
1800 context,
1801 SkBudgeted::kNo,
1802 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1803 SkASSERT(surface && surface->getCanvas());
1804 fuzz_canvas(fuzz, surface->getCanvas());
1805}
1806
Hal Canary44801ca2017-03-15 11:39:06 -04001807DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001808 sk_gpu_test::GrContextFactory f;
1809 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001810 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001811 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001812 }
Hal Canary5aa91582017-03-21 11:11:44 -07001813 fuzz_ganesh(fuzz, context);
1814}
1815
1816DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001817 sk_gpu_test::GrContextFactory f;
1818 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001819}
1820
1821DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001822 sk_gpu_test::GrContextFactory f;
1823 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001824}
1825#endif
1826
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001827DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001828 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001829 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001830 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1831 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001832}
1833
1834// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1835DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001836 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001837 fuzz_canvas(fuzz, &debugCanvas);
1838 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1839 UrlDataManager dataManager(SkString("data"));
1840 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1841 Json::StyledStreamWriter(" ").write(std::cout, json);
1842}