blob: ffc8781e6b0f413dd9a4f14079bfbddd367f9958 [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);
186 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary27bece82017-03-07 16:23:20 -0500187 return SkColorFilter::MakeComposeFilter(std::move(outer), std::move(inner));
188 }
189 case 3: {
190 SkScalar array[20];
191 fuzz->nextN(array, SK_ARRAY_COUNT(array));
192 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
193 }
194 case 4: {
195 SkColor mul, add;
196 fuzz->next(&mul, &add);
197 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
198 }
199 case 5: {
200 bool grayscale;
201 int invertStyle;
202 float contrast;
203 fuzz->next(&grayscale);
204 fuzz->nextRange(&invertStyle, 0, 2);
205 fuzz->nextRange(&contrast, -1.0f, 1.0f);
206 return SkHighContrastFilter::Make(SkHighContrastConfig(
207 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
208 }
209 case 6:
210 return SkLumaColorFilter::Make();
211 case 7: {
212 uint8_t table[256];
213 fuzz->nextN(table, SK_ARRAY_COUNT(table));
214 return SkTableColorFilter::Make(table);
215 }
216 case 8: {
217 uint8_t tableA[256];
218 uint8_t tableR[256];
219 uint8_t tableG[256];
220 uint8_t tableB[256];
221 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
222 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
223 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
224 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
225 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
226 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500227 default:
228 SkASSERT(false);
229 break;
Hal Canary27bece82017-03-07 16:23:20 -0500230 }
231 return nullptr;
232}
Hal Canary24ac42b2017-02-14 13:35:14 -0500233
Hal Canary1e0138b2017-03-10 13:56:08 -0500234static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500235 SkScalar totalPos = 0;
236 for (int i = 0; i < colorCount; ++i) {
237 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
238 totalPos += pos[i];
239 }
240 totalPos = 1.0f / totalPos;
241 for (int i = 0; i < colorCount; ++i) {
242 pos[i] *= totalPos;
243 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500244 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500245 pos[colorCount - 1] = 1.0f;
246}
247
Hal Canary1e0138b2017-03-10 13:56:08 -0500248static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500249 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
250 sk_sp<SkColorFilter> colorFilter(nullptr);
251 SkBitmap bitmap;
252 sk_sp<SkImage> img;
253 SkShader::TileMode tmX, tmY;
254 bool useMatrix;
255 SkColor color;
256 SkMatrix matrix;
257 SkBlendMode blendMode;
258 int shaderType;
259 if (depth <= 0) {
260 return nullptr;
261 }
Hal Canary671e4422017-02-27 13:36:38 -0500262 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500263 switch (shaderType) {
264 case 0:
265 return nullptr;
266 case 1:
267 return SkShader::MakeEmptyShader();
268 case 2:
269 fuzz->next(&color);
270 return SkShader::MakeColorShader(color);
271 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500272 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500273 fuzz->next(&tmX, &tmY, &useMatrix);
274 if (useMatrix) {
275 fuzz->next(&matrix);
276 }
277 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
278 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500279 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500280 fuzz->next(&tmX, &tmY, &useMatrix);
281 if (useMatrix) {
282 fuzz->next(&matrix);
283 }
284 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
285 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500286 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500287 fuzz->next(&matrix);
288 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
289 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500290 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
291 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500292 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
293 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500294 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
295 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500296 fuzz->next(&blendMode);
297 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500298 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500299 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500300 bool useTile;
301 SkRect tile;
302 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
303 if (useMatrix) {
304 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500305 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500306 if (useTile) {
307 fuzz->next(&tile);
308 }
309 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
310 useMatrix ? &matrix : nullptr,
311 useTile ? &tile : nullptr);
312 }
Hal Canary671e4422017-02-27 13:36:38 -0500313 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500314 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000315 // Deprecated SkGaussianEdgeShader
316 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500317 case 10: {
318 constexpr int kMaxColors = 12;
319 SkPoint pts[2];
320 SkColor colors[kMaxColors];
321 SkScalar pos[kMaxColors];
322 int colorCount;
323 bool usePos;
324 fuzz->nextN(pts, 2);
325 fuzz->nextRange(&colorCount, 2, kMaxColors);
326 fuzz->nextN(colors, colorCount);
327 fuzz->next(&tmX, &useMatrix, &usePos);
328 if (useMatrix) {
329 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500330 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500331 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500332 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500333 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500334 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
335 tmX, 0, useMatrix ? &matrix : nullptr);
336 }
337 case 11: {
338 constexpr int kMaxColors = 12;
339 SkPoint center;
340 SkScalar radius;
341 int colorCount;
342 bool usePos;
343 SkColor colors[kMaxColors];
344 SkScalar pos[kMaxColors];
345 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
346 fuzz->nextRange(&colorCount, 2, kMaxColors);
347 fuzz->nextN(colors, colorCount);
348 if (useMatrix) {
349 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500350 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500351 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500352 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500353 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500354 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
355 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
356 }
357 case 12: {
358 constexpr int kMaxColors = 12;
359 SkPoint start, end;
360 SkScalar startRadius, endRadius;
361 int colorCount;
362 bool usePos;
363 SkColor colors[kMaxColors];
364 SkScalar pos[kMaxColors];
365 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
366 fuzz->nextRange(&colorCount, 2, kMaxColors);
367 fuzz->nextN(colors, colorCount);
368 if (useMatrix) {
369 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500370 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500371 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500372 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500373 }
374 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
375 usePos ? pos : nullptr, colorCount, tmX, 0,
376 useMatrix ? &matrix : nullptr);
377 }
378 case 13: {
379 constexpr int kMaxColors = 12;
380 SkScalar cx, cy;
381 int colorCount;
382 bool usePos;
383 SkColor colors[kMaxColors];
384 SkScalar pos[kMaxColors];
385 fuzz->next(&cx, &cy, &useMatrix, &usePos);
386 fuzz->nextRange(&colorCount, 2, kMaxColors);
387 fuzz->nextN(colors, colorCount);
388 if (useMatrix) {
389 fuzz->next(&matrix);
390 }
391 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500392 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500393 }
394 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
395 0, useMatrix ? &matrix : nullptr);
396 }
397 case 14: {
398 SkScalar baseFrequencyX, baseFrequencyY, seed;
399 int numOctaves;
400 SkISize tileSize;
401 bool useTileSize, turbulence;
402 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
403 if (useTileSize) {
404 fuzz->next(&tileSize);
405 }
406 fuzz->nextRange(&numOctaves, 2, 7);
407 if (turbulence) {
408 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
409 numOctaves, seed,
410 useTileSize ? &tileSize : nullptr);
411 } else {
412 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
413 numOctaves, seed,
414 useTileSize ? &tileSize : nullptr);
415 }
416 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500417 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500418 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500419 break;
420 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500421 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500422}
423
Hal Canary1e0138b2017-03-10 13:56:08 -0500424static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500425 if (depth <= 0) {
426 return nullptr;
427 }
428 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400429 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500430 switch (pathEffectType) {
431 case 0: {
432 return nullptr;
433 }
434 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500435 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
436 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500437 return SkPathEffect::MakeSum(std::move(first), std::move(second));
438 }
439 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500440 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
441 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500442 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
443 }
444 case 3: {
445 SkPath path;
446 fuzz_path(fuzz, &path, 20);
447 SkScalar advance, phase;
448 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500449 SkPath1DPathEffect::Style style;
450 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
451 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500452 }
453 case 4: {
454 SkScalar width;
455 SkMatrix matrix;
456 fuzz->next(&width, &matrix);
457 return SkLine2DPathEffect::Make(width, matrix);
458 }
459 case 5: {
460 SkPath path;
461 fuzz_path(fuzz, &path, 20);
462 SkMatrix matrix;
463 fuzz->next(&matrix);
464 return SkPath2DPathEffect::Make(matrix, path);
465 }
466 case 6: {
467 SkScalar radius;
468 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500469 return SkCornerPathEffect::Make(radius);
470 }
Mike Reed40e7e652017-07-22 22:12:59 -0400471 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500472 SkScalar phase;
473 fuzz->next(&phase);
474 SkScalar intervals[20];
475 int count;
476 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
477 fuzz->nextN(intervals, count);
478 return SkDashPathEffect::Make(intervals, count, phase);
479 }
Mike Reed40e7e652017-07-22 22:12:59 -0400480 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500481 SkScalar segLength, dev;
482 uint32_t seed;
483 fuzz->next(&segLength, &dev, &seed);
484 return SkDiscretePathEffect::Make(segLength, dev, seed);
485 }
486 default:
487 SkASSERT(false);
488 return nullptr;
489 }
490}
Hal Canary24ac42b2017-02-14 13:35:14 -0500491
Hal Canary1e0138b2017-03-10 13:56:08 -0500492static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500493 int maskfilterType;
494 fuzz->nextRange(&maskfilterType, 0, 2);
495 switch (maskfilterType) {
496 case 0:
497 return nullptr;
498 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500499 SkBlurStyle blurStyle;
500 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500501 SkScalar sigma;
502 fuzz->next(&sigma);
503 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Hal Canary1e0138b2017-03-10 13:56:08 -0500504 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -0500505 fuzz->next(&occluder);
506 }
507 uint32_t flags;
508 fuzz->nextRange(&flags, 0, 3);
Hal Canaryf7005202017-03-10 08:48:28 -0500509 return SkBlurMaskFilter::Make(blurStyle, sigma, occluder, flags);
Hal Canary5395c592017-03-08 16:52:18 -0500510 }
511 case 2: {
512 SkRRect first, second;
513 SkScalar radius;
514 fuzz->next(&first, &second, &radius);
515 return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
516 }
517 default:
518 SkASSERT(false);
519 return nullptr;
520 }
521}
Hal Canary24ac42b2017-02-14 13:35:14 -0500522
Hal Canary1e0138b2017-03-10 13:56:08 -0500523static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
524 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500525 return nullptr;
526 }
527 auto fontMugger = SkFontMgr::RefDefault();
528 SkASSERT(fontMugger);
529 int familyCount = fontMugger->countFamilies();
530 int i, j;
531 fuzz->nextRange(&i, 0, familyCount - 1);
532 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
533 int styleCount = family->count();
534 fuzz->nextRange(&j, 0, styleCount - 1);
535 return sk_sp<SkTypeface>(family->createTypeface(j));
536}
Hal Canary24ac42b2017-02-14 13:35:14 -0500537
Hal Canarye03c3e52017-03-09 11:33:35 -0500538template <>
539inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
540 SkRect rect;
541 uint8_t flags;
542 this->next(&rect);
543 this->nextRange(&flags, 0, 0xF);
544 *cropRect = SkImageFilter::CropRect(rect, flags);
545}
546
Hal Canary1e0138b2017-03-10 13:56:08 -0500547static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500548
549static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
550 if (depth <= 0) {
551 return nullptr;
552 }
553 uint8_t imageFilterType;
554 fuzz->nextRange(&imageFilterType, 1, 6);
555 SkPoint3 p, q;
556 SkColor lightColor;
557 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
558 sk_sp<SkImageFilter> input;
559 SkImageFilter::CropRect cropRect;
560 bool useCropRect;
561 fuzz->next(&useCropRect);
562 if (useCropRect) {
563 fuzz->next(&cropRect);
564 }
565 switch (imageFilterType) {
566 case 1:
567 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500568 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500569 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
570 std::move(input),
571 useCropRect ? &cropRect : nullptr);
572 case 2:
573 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500574 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500575 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
576 std::move(input),
577 useCropRect ? &cropRect : nullptr);
578 case 3:
579 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500580 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500581 return SkLightingImageFilter::MakeSpotLitDiffuse(
582 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
583 std::move(input), useCropRect ? &cropRect : nullptr);
584 case 4:
585 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500586 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500587 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
588 shininess, std::move(input),
589 useCropRect ? &cropRect : nullptr);
590 case 5:
591 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500592 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500593 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
594 shininess, std::move(input),
595 useCropRect ? &cropRect : nullptr);
596 case 6:
597 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
598 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500599 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500600 return SkLightingImageFilter::MakeSpotLitSpecular(
601 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
602 std::move(input), useCropRect ? &cropRect : nullptr);
603 default:
604 SkASSERT(false);
605 return nullptr;
606 }
607}
608
Hal Canary1e0138b2017-03-10 13:56:08 -0500609static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500610
Hal Canary1e0138b2017-03-10 13:56:08 -0500611static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500612 if (depth <= 0) {
613 return nullptr;
614 }
615 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500616 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500617 switch (imageFilterType) {
618 case 0:
619 return nullptr;
620 case 1: {
621 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500622 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500623 bool useCropRect;
624 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400625 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500626 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400627 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500628 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400629 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500630 useCropRect ? &cropRect : nullptr);
631 }
632 case 2: {
633 SkMatrix matrix;
634 SkFilterQuality quality;
635 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500636 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500637 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
638 }
639 case 3: {
640 SkRegion region;
641 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500642 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500643 bool useCropRect;
644 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400645 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500646 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400647 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500648 }
649 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
650 useCropRect ? &cropRect : nullptr);
651 }
652 case 4: {
653 float k1, k2, k3, k4;
654 bool enforcePMColor;
655 bool useCropRect;
656 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500657 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
658 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500659 SkImageFilter::CropRect cropRect;
660 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400661 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500662 }
663 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
664 std::move(background), std::move(foreground),
665 useCropRect ? &cropRect : nullptr);
666 }
667 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500668 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
669 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500670 bool useCropRect;
671 SkImageFilter::CropRect cropRect;
672 fuzz->next(&useCropRect);
673 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400674 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500675 }
676 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
677 useCropRect ? &cropRect : nullptr);
678 }
679 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500680 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
681 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500682 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
683 }
684 case 7: {
685 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400686 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
687 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500688 SkScalar scale;
689 bool useCropRect;
690 fuzz->next(&scale, &useCropRect);
691 SkImageFilter::CropRect cropRect;
692 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400693 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500694 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500695 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
696 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500697 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
698 std::move(displacement), std::move(color),
699 useCropRect ? &cropRect : nullptr);
700 }
701 case 8: {
702 SkScalar dx, dy, sigmaX, sigmaY;
703 SkColor color;
704 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500705 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500706 bool useCropRect;
707 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
708 SkImageFilter::CropRect cropRect;
709 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400710 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500711 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500712 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500713 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
714 std::move(input),
715 useCropRect ? &cropRect : nullptr);
716 }
717 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500718 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500719 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500720 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500721 SkRect srcRect, dstRect;
722 SkFilterQuality filterQuality;
723 fuzz->next(&srcRect, &dstRect, &filterQuality);
724 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
725 }
726 case 11:
727 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
728 case 12: {
729 SkRect srcRect;
730 SkScalar inset;
731 bool useCropRect;
732 SkImageFilter::CropRect cropRect;
733 fuzz->next(&srcRect, &inset, &useCropRect);
734 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400735 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500736 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500737 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500738 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
739 useCropRect ? &cropRect : nullptr);
740 }
741 case 13: {
742 constexpr int kMaxKernelSize = 5;
743 int32_t n, m;
744 fuzz->nextRange(&n, 1, kMaxKernelSize);
745 fuzz->nextRange(&m, 1, kMaxKernelSize);
746 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
747 fuzz->nextN(kernel, n * m);
748 int32_t offsetX, offsetY;
749 fuzz->nextRange(&offsetX, 0, n - 1);
750 fuzz->nextRange(&offsetY, 0, m - 1);
751 SkScalar gain, bias;
752 bool convolveAlpha, useCropRect;
753 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
754 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500755 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500756 SkImageFilter::CropRect cropRect;
757 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400758 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500759 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500760 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500761 return SkMatrixConvolutionImageFilter::Make(
762 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
763 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
764 }
765 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500766 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
767 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500768 bool useCropRect;
769 fuzz->next(&useCropRect);
770 SkImageFilter::CropRect cropRect;
771 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400772 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500773 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400774 return SkMergeImageFilter::Make(std::move(first), std::move(second),
775 useCropRect ? &cropRect : nullptr);
776 }
777 case 15: {
778 constexpr int kMaxCount = 4;
779 sk_sp<SkImageFilter> ifs[kMaxCount];
780 int count;
781 fuzz->nextRange(&count, 1, kMaxCount);
782 for (int i = 0; i < count; ++i) {
783 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
784 }
785 bool useCropRect;
786 fuzz->next(&useCropRect);
787 SkImageFilter::CropRect cropRect;
788 if (useCropRect) {
789 fuzz->next(&cropRect);
790 }
791 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500792 }
793 case 16: {
794 int rx, ry;
795 fuzz->next(&rx, &ry);
796 bool useCropRect;
797 fuzz->next(&useCropRect);
798 SkImageFilter::CropRect cropRect;
799 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400800 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500801 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500802 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500803 return SkDilateImageFilter::Make(rx, ry, std::move(input),
804 useCropRect ? &cropRect : nullptr);
805 }
806 case 17: {
807 int rx, ry;
808 fuzz->next(&rx, &ry);
809 bool useCropRect;
810 fuzz->next(&useCropRect);
811 SkImageFilter::CropRect cropRect;
812 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400813 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500814 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500815 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500816 return SkErodeImageFilter::Make(rx, ry, std::move(input),
817 useCropRect ? &cropRect : nullptr);
818 }
819 case 18: {
820 SkScalar dx, dy;
821 fuzz->next(&dx, &dy);
822 bool useCropRect;
823 fuzz->next(&useCropRect);
824 SkImageFilter::CropRect cropRect;
825 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400826 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500827 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500828 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500829 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
830 useCropRect ? &cropRect : nullptr);
831 }
832 case 19: {
833 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500834 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500835 bool useCropRect;
836 fuzz->next(&useCropRect);
837 SkImageFilter::CropRect cropRect;
838 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400839 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500840 }
841 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
842 }
843 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500844 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500845 return SkPictureImageFilter::Make(std::move(picture));
846 }
847 case 21: {
848 SkRect cropRect;
849 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500850 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500851 return SkPictureImageFilter::Make(std::move(picture), cropRect);
852 }
853 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500854 SkRect src, dst;
855 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500856 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500857 return SkTileImageFilter::Make(src, dst, std::move(input));
858 }
Mike Reed77e487d2017-11-09 21:50:20 +0000859 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500860 SkBlendMode blendMode;
861 bool useCropRect;
862 fuzz->next(&useCropRect, &blendMode);
863 SkImageFilter::CropRect cropRect;
864 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400865 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500866 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500867 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
868 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500869 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
870 useCropRect ? &cropRect : nullptr);
871 }
872 default:
873 SkASSERT(false);
874 return nullptr;
875 }
876}
Hal Canary24ac42b2017-02-14 13:35:14 -0500877
Hal Canary1e0138b2017-03-10 13:56:08 -0500878static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500879 int w, h;
880 fuzz->nextRange(&w, 1, 1024);
881 fuzz->nextRange(&h, 1, 1024);
882 SkAutoTMalloc<SkPMColor> data(w * h);
883 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
884 int n = w * h;
885 for (int i = 0; i < n; ++i) {
886 SkColor c;
887 fuzz->next(&c);
888 data[i] = SkPreMultiplyColor(c);
889 }
890 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500891 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
892 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500893}
894
Hal Canary1e0138b2017-03-10 13:56:08 -0500895static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500896 SkBitmap bitmap;
897 int w, h;
898 fuzz->nextRange(&w, 1, 1024);
899 fuzz->nextRange(&h, 1, 1024);
900 bitmap.allocN32Pixels(w, h);
Hal Canary24ac42b2017-02-14 13:35:14 -0500901 for (int y = 0; y < h; ++y) {
902 for (int x = 0; x < w; ++x) {
903 SkColor c;
904 fuzz->next(&c);
905 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
906 }
907 }
908 return bitmap;
909}
910
Hal Canary1e0138b2017-03-10 13:56:08 -0500911template <typename T, typename Min, typename Max>
912inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
913 T value;
914 fuzz_enum_range(fuzz, &value, minv, maxv);
915 return value;
916}
917
918static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500919 if (!fuzz || !paint || depth <= 0) {
920 return;
921 }
922
Hal Canary1e0138b2017-03-10 13:56:08 -0500923 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
924 paint->setDither( make_fuzz_t<bool>(fuzz));
925 paint->setColor( make_fuzz_t<SkColor>(fuzz));
926 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
927 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
928 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
929 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
930 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
931 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
932 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
933 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500934
935 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500936 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
937 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
938 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
939 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500940 }
941}
942
Hal Canary1e0138b2017-03-10 13:56:08 -0500943static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
944 paint->setTypeface( make_fuzz_typeface(fuzz));
945 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
946 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
947 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
948 paint->setLinearText( make_fuzz_t<bool>(fuzz));
949 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
950 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
951 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
952 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
953 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
954 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
955 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
956 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
957 SkPaint::kFull_Hinting));
958 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -0500959}
960
961static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500962 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -0500963}
964
Hal Canary5395c592017-03-08 16:52:18 -0500965constexpr int kMaxGlyphCount = 30;
966
Hal Canary1e0138b2017-03-10 13:56:08 -0500967static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -0500968 SkTDArray<uint8_t> array;
969 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500970 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
971 : SkTypeface::MakeDefault()->countGlyphs();
Hal Canary671e4422017-02-27 13:36:38 -0500972 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500973 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500974 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
975 for (int i = 0; i < glyphCount; ++i) {
976 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
977 }
978 return array;
979 }
980 static const SkUnichar ranges[][2] = {
981 {0x0020, 0x007F},
982 {0x00A1, 0x0250},
983 {0x0400, 0x0500},
984 };
985 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500986 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500987 count += (ranges[i][1] - ranges[i][0]);
988 }
Hal Canary5395c592017-03-08 16:52:18 -0500989 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500990 SkUnichar buffer[kMaxLength];
991 int length;
992 fuzz->nextRange(&length, 1, kMaxLength);
993 for (int j = 0; j < length; ++j) {
994 int32_t value;
995 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500996 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500997 if (value + ranges[i][0] < ranges[i][1]) {
998 buffer[j] = value + ranges[i][0];
999 break;
1000 } else {
1001 value -= (ranges[i][1] - ranges[i][0]);
1002 }
1003 }
1004 }
1005 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001006 case SkPaint::kUTF8_TextEncoding: {
1007 size_t utf8len = 0;
1008 for (int j = 0; j < length; ++j) {
1009 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001010 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001011 char* ptr = (char*)array.append(utf8len);
1012 for (int j = 0; j < length; ++j) {
1013 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001014 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001015 } break;
1016 case SkPaint::kUTF16_TextEncoding: {
1017 size_t utf16len = 0;
1018 for (int j = 0; j < length; ++j) {
1019 utf16len += SkUTF16_FromUnichar(buffer[j]);
1020 }
1021 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1022 for (int j = 0; j < length; ++j) {
1023 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1024 }
1025 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001026 case SkPaint::kUTF32_TextEncoding:
1027 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001028 break;
Hal Canary671e4422017-02-27 13:36:38 -05001029 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001030 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001031 break;
Hal Canary671e4422017-02-27 13:36:38 -05001032 }
1033 return array;
1034}
1035
Hal Canary5395c592017-03-08 16:52:18 -05001036static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1037 SkTextBlobBuilder textBlobBuilder;
1038 int8_t runCount;
1039 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1040 while (runCount-- > 0) {
1041 SkPaint paint;
1042 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001043 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001044 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1045 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1046 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1047 SkASSERT(glyphCount <= kMaxGlyphCount);
1048 SkScalar x, y;
1049 const SkTextBlobBuilder::RunBuffer* buffer;
1050 uint8_t runType;
1051 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1052 switch (runType) {
1053 case 0:
1054 fuzz->next(&x, &y);
1055 // TODO: Test other variations of this.
1056 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1057 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1058 break;
1059 case 1:
1060 fuzz->next(&y);
1061 // TODO: Test other variations of this.
1062 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1063 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1064 fuzz->nextN(buffer->pos, glyphCount);
1065 break;
1066 case 2:
1067 // TODO: Test other variations of this.
1068 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1069 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1070 fuzz->nextN(buffer->pos, glyphCount * 2);
1071 break;
1072 default:
1073 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001074 break;
Hal Canary5395c592017-03-08 16:52:18 -05001075 }
1076 }
1077 return textBlobBuilder.make();
1078}
1079
Hal Canary1e0138b2017-03-10 13:56:08 -05001080static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001081 if (!fuzz || !canvas || depth <= 0) {
1082 return;
1083 }
1084 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1085 unsigned N;
1086 fuzz->nextRange(&N, 0, 2000);
1087 for (unsigned i = 0; i < N; ++i) {
1088 if (fuzz->exhausted()) {
1089 return;
1090 }
1091 SkPaint paint;
1092 SkMatrix matrix;
1093 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001094 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001095 switch (drawCommand) {
1096 case 0:
1097 canvas->flush();
1098 break;
1099 case 1:
1100 canvas->save();
1101 break;
1102 case 2: {
1103 SkRect bounds;
1104 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001105 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001106 canvas->saveLayer(&bounds, &paint);
1107 break;
1108 }
1109 case 3: {
1110 SkRect bounds;
1111 fuzz->next(&bounds);
1112 canvas->saveLayer(&bounds, nullptr);
1113 break;
1114 }
1115 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001116 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001117 canvas->saveLayer(nullptr, &paint);
1118 break;
1119 case 5:
1120 canvas->saveLayer(nullptr, nullptr);
1121 break;
1122 case 6: {
1123 uint8_t alpha;
1124 fuzz->next(&alpha);
1125 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1126 break;
1127 }
1128 case 7: {
1129 SkRect bounds;
1130 uint8_t alpha;
1131 fuzz->next(&bounds, &alpha);
1132 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1133 break;
1134 }
1135 case 8: {
1136 SkCanvas::SaveLayerRec saveLayerRec;
1137 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001138 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001139 fuzz->next(&bounds);
1140 saveLayerRec.fBounds = &bounds;
1141 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001142 if (make_fuzz_t<bool>(fuzz)) {
1143 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001144 saveLayerRec.fPaint = &paint;
1145 }
1146 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001147 if (make_fuzz_t<bool>(fuzz)) {
1148 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001149 saveLayerRec.fBackdrop = imageFilter.get();
1150 }
Hal Canary5395c592017-03-08 16:52:18 -05001151 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001152 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001153 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1154 // }
1155
Hal Canary24ac42b2017-02-14 13:35:14 -05001156 canvas->saveLayer(saveLayerRec);
1157 break;
1158 }
1159 case 9:
1160 canvas->restore();
1161 break;
1162 case 10: {
1163 int saveCount;
1164 fuzz->next(&saveCount);
1165 canvas->restoreToCount(saveCount);
1166 break;
1167 }
1168 case 11: {
1169 SkScalar x, y;
1170 fuzz->next(&x, &y);
1171 canvas->translate(x, y);
1172 break;
1173 }
1174 case 12: {
1175 SkScalar x, y;
1176 fuzz->next(&x, &y);
1177 canvas->scale(x, y);
1178 break;
1179 }
1180 case 13: {
1181 SkScalar v;
1182 fuzz->next(&v);
1183 canvas->rotate(v);
1184 break;
1185 }
1186 case 14: {
1187 SkScalar x, y, v;
1188 fuzz->next(&x, &y, &v);
1189 canvas->rotate(v, x, y);
1190 break;
1191 }
1192 case 15: {
1193 SkScalar x, y;
1194 fuzz->next(&x, &y);
1195 canvas->skew(x, y);
1196 break;
1197 }
1198 case 16: {
1199 SkMatrix mat;
1200 fuzz->next(&mat);
1201 canvas->concat(mat);
1202 break;
1203 }
1204 case 17: {
1205 SkMatrix mat;
1206 fuzz->next(&mat);
1207 canvas->setMatrix(mat);
1208 break;
1209 }
1210 case 18:
1211 canvas->resetMatrix();
1212 break;
1213 case 19: {
1214 SkRect r;
1215 int op;
1216 bool doAntiAlias;
1217 fuzz->next(&r, &doAntiAlias);
1218 fuzz->nextRange(&op, 0, 1);
1219 r.sort();
1220 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1221 break;
1222 }
1223 case 20: {
1224 SkRRect rr;
1225 int op;
1226 bool doAntiAlias;
1227 fuzz->next(&rr);
1228 fuzz->next(&doAntiAlias);
1229 fuzz->nextRange(&op, 0, 1);
1230 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1231 break;
1232 }
1233 case 21: {
1234 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001235 fuzz_path(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001236 int op;
1237 bool doAntiAlias;
1238 fuzz->next(&doAntiAlias);
1239 fuzz->nextRange(&op, 0, 1);
1240 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1241 break;
1242 }
1243 case 22: {
1244 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001245 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001246 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001247 fuzz->nextRange(&op, 0, 1);
1248 canvas->clipRegion(region, (SkClipOp)op);
1249 break;
1250 }
1251 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001252 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001253 canvas->drawPaint(paint);
1254 break;
1255 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001256 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001257 SkCanvas::PointMode pointMode;
1258 fuzz_enum_range(fuzz, &pointMode,
1259 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001260 size_t count;
1261 constexpr int kMaxCount = 30;
1262 fuzz->nextRange(&count, 0, kMaxCount);
1263 SkPoint pts[kMaxCount];
1264 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001265 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001266 break;
1267 }
1268 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001269 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001270 SkRect r;
1271 fuzz->next(&r);
1272 canvas->drawRect(r, paint);
1273 break;
1274 }
1275 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001276 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001277 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001278 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001279 canvas->drawRegion(region, paint);
1280 break;
1281 }
1282 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001283 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001284 SkRect r;
1285 fuzz->next(&r);
1286 canvas->drawOval(r, paint);
1287 break;
1288 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001289 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001290 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001291 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001292 SkRRect rr;
1293 fuzz->next(&rr);
1294 canvas->drawRRect(rr, paint);
1295 break;
1296 }
1297 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001298 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001299 SkRRect orr, irr;
1300 fuzz->next(&orr);
1301 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001302 if (orr.getBounds().contains(irr.getBounds())) {
1303 canvas->drawDRRect(orr, irr, paint);
1304 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001305 break;
1306 }
1307 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001308 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001309 SkRect r;
1310 SkScalar start, sweep;
1311 bool useCenter;
1312 fuzz->next(&r, &start, &sweep, &useCenter);
1313 canvas->drawArc(r, start, sweep, useCenter, paint);
1314 break;
1315 }
1316 case 32: {
1317 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001318 fuzz_path(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001319 canvas->drawPath(path, paint);
1320 break;
1321 }
1322 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001323 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001324 SkScalar left, top;
1325 bool usePaint;
1326 fuzz->next(&left, &top, &usePaint);
1327 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001328 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001329 }
1330 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1331 break;
1332 }
1333 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001334 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001335 SkRect src, dst;
1336 bool usePaint;
1337 fuzz->next(&src, &dst, &usePaint);
1338 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001339 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001340 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001341 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001342 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1343 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001344 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1345 break;
1346 }
1347 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001348 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001349 SkIRect src;
1350 SkRect dst;
1351 bool usePaint;
1352 fuzz->next(&src, &dst, &usePaint);
1353 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001354 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001355 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001356 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001357 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1358 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001359 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1360 break;
1361 }
1362 case 36: {
1363 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001364 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001365 SkRect dst;
1366 fuzz->next(&dst, &usePaint);
1367 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001368 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001369 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001370 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001371 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1372 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001373 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1374 break;
1375 }
1376 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001377 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001378 SkIRect center;
1379 SkRect dst;
1380 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001381 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001382 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001383 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001384 }
Hal Canary0361d492017-03-15 12:58:15 -04001385 if (make_fuzz_t<bool>(fuzz)) {
1386 fuzz->next(&center);
1387 } else { // Make valid center, see SkLatticeIter::Valid().
1388 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1389 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1390 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1391 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1392 }
1393 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001394 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1395 break;
1396 }
1397 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001398 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001399 SkScalar left, top;
1400 bool usePaint;
1401 fuzz->next(&left, &top, &usePaint);
1402 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001403 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001404 }
1405 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1406 break;
1407 }
1408 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001409 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001410 SkRect src, dst;
1411 bool usePaint;
1412 fuzz->next(&src, &dst, &usePaint);
1413 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001414 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001415 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001416 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001417 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1418 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001419 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1420 break;
1421 }
1422 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001423 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001424 SkIRect src;
1425 SkRect dst;
1426 bool usePaint;
1427 fuzz->next(&src, &dst, &usePaint);
1428 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001429 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001430 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001431 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001432 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1433 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001434 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1435 break;
1436 }
1437 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001438 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001439 SkRect dst;
1440 bool usePaint;
1441 fuzz->next(&dst, &usePaint);
1442 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001443 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001444 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001445 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001446 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1447 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001448 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1449 break;
1450 }
1451 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001452 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001453 SkIRect center;
1454 SkRect dst;
1455 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001456 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001457 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001458 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001459 }
Hal Canary0361d492017-03-15 12:58:15 -04001460 if (make_fuzz_t<bool>(fuzz)) {
1461 fuzz->next(&center);
1462 } else { // Make valid center, see SkLatticeIter::Valid().
1463 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1464 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1465 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1466 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1467 }
1468 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001469 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1470 break;
1471 }
1472 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001473 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001474 bool usePaint;
1475 SkRect dst;
1476 fuzz->next(&usePaint, &dst);
1477 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001478 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001479 }
1480 constexpr int kMax = 6;
1481 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001482 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001483 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1484 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1485 fuzz->nextN(xDivs, lattice.fXCount);
1486 fuzz->nextN(yDivs, lattice.fYCount);
1487 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1488 break;
1489 }
1490 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001491 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001492 bool usePaint;
1493 SkRect dst;
1494 fuzz->next(&usePaint, &dst);
1495 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001496 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001497 }
1498 constexpr int kMax = 6;
1499 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001500 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001501 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1502 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1503 fuzz->nextN(xDivs, lattice.fXCount);
1504 fuzz->nextN(yDivs, lattice.fYCount);
1505 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1506 break;
1507 }
1508 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001509 fuzz_paint(fuzz, &paint, depth - 1);
1510 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001511 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001512 SkScalar x, y;
1513 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001514 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001515 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001516 break;
1517 }
1518 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001519 fuzz_paint(fuzz, &paint, depth - 1);
1520 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001521 fuzz_paint_text_encoding(fuzz, &paint);
1522 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001523 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001524 if (glyphCount < 1) {
1525 break;
1526 }
1527 SkAutoTMalloc<SkPoint> pos(glyphCount);
1528 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001529 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001530 pos[0] = {0, 0};
1531 for (int i = 1; i < glyphCount; ++i) {
1532 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001533 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001534 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1535 }
Hal Canary671e4422017-02-27 13:36:38 -05001536 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001537 break;
1538 }
1539 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001540 fuzz_paint(fuzz, &paint, depth - 1);
1541 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001542 fuzz_paint_text_encoding(fuzz, &paint);
1543 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001544 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001545 SkAutoTMalloc<SkScalar> widths(glyphCount);
1546 if (glyphCount < 1) {
1547 break;
1548 }
Hal Canary671e4422017-02-27 13:36:38 -05001549 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001550 SkScalar x = widths[0];
1551 for (int i = 0; i < glyphCount; ++i) {
1552 SkTSwap(x, widths[i]);
1553 x += widths[i];
1554 SkScalar offset;
1555 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001556 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001557 widths[i] += offset;
1558 }
1559 SkScalar y;
1560 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001561 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001562 break;
1563 }
1564 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001565 fuzz_paint(fuzz, &paint, depth - 1);
1566 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001567 fuzz_paint_text_encoding(fuzz, &paint);
1568 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001569 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001570 fuzz_path(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001571 SkScalar hOffset, vOffset;
1572 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001573 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1574 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001575 break;
1576 }
1577 case 49: {
1578 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001579 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001580 if (useMatrix) {
1581 fuzz->next(&matrix);
1582 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001583 fuzz_paint(fuzz, &paint, depth - 1);
1584 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001585 fuzz_paint_text_encoding(fuzz, &paint);
1586 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001587 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001588 fuzz_path(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001589 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001590 useMatrix ? &matrix : nullptr, paint);
1591 break;
1592 }
1593 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001594 fuzz_paint(fuzz, &paint, depth - 1);
1595 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001596 fuzz_paint_text_encoding(fuzz, &paint);
1597 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1598 SkRSXform rSXform[kMaxGlyphCount];
1599 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1600 SkASSERT(glyphCount <= kMaxGlyphCount);
1601 fuzz->nextN(rSXform, glyphCount);
1602 SkRect cullRect;
1603 bool useCullRect;
1604 fuzz->next(&useCullRect);
1605 if (useCullRect) {
1606 fuzz->next(&cullRect);
1607 }
1608 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1609 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001610 break;
1611 }
1612 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001613 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001614 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001615 SkScalar x, y;
1616 fuzz->next(&x, &y);
1617 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001618 break;
1619 }
1620 case 52: {
1621 bool usePaint, useMatrix;
1622 fuzz->next(&usePaint, &useMatrix);
1623 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001624 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001625 }
1626 if (useMatrix) {
1627 fuzz->next(&matrix);
1628 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001629 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001630 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1631 usePaint ? &paint : nullptr);
1632 break;
1633 }
1634 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001635 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001636 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001637 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001638 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001639 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001640 constexpr int kMaxCount = 100;
1641 int vertexCount;
1642 SkPoint vertices[kMaxCount];
1643 SkPoint texs[kMaxCount];
1644 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001645 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1646 fuzz->nextN(vertices, vertexCount);
1647 bool useTexs, useColors;
1648 fuzz->next(&useTexs, &useColors);
1649 if (useTexs) {
1650 fuzz->nextN(texs, vertexCount);
1651 }
1652 if (useColors) {
1653 fuzz->nextN(colors, vertexCount);
1654 }
1655 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001656 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001657 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001658 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1659 for (int i = 0; i < indexCount; ++i) {
1660 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1661 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001662 }
Mike Reed887cdf12017-04-03 11:11:09 -04001663 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1664 useTexs ? texs : nullptr,
1665 useColors ? colors : nullptr,
1666 indexCount, indices),
1667 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001668 break;
1669 }
1670 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001671 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001672 break;
1673 }
1674 }
1675}
1676
Hal Canary1e0138b2017-03-10 13:56:08 -05001677static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001678 SkScalar w, h;
1679 fuzz->next(&w, &h);
1680 SkPictureRecorder pictureRecorder;
1681 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1682 return pictureRecorder.finishRecordingAsPicture();
1683}
1684
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001685DEF_FUZZ(NullCanvas, fuzz) {
1686 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001687}
1688
Hal Canary44801ca2017-03-15 11:39:06 -04001689// 8.5x11 letter paper at 72ppi.
1690constexpr SkISize kCanvasSize = {612, 792};
1691
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001692DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001693 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001694 SkASSERT(surface && surface->getCanvas());
1695 fuzz_canvas(fuzz, surface->getCanvas());
1696}
1697
Hal Canarye2924432017-12-01 11:46:26 -05001698DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1699 SkPictureRecorder recorder;
1700 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1701 SkIntToScalar(kCanvasSize.height())));
1702 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1703 if (!pic) { fuzz->signalBug(); }
1704 sk_sp<SkData> data = pic->serialize();
1705 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001706 SkReadBuffer rb(data->data(), data->size());
1707 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001708 if (!deserialized) { fuzz->signalBug(); }
1709 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1710 SkASSERT(surface && surface->getCanvas());
1711 surface->getCanvas()->drawPicture(deserialized);
1712}
1713
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001714DEF_FUZZ(ImageFilter, fuzz) {
1715 auto fil = make_fuzz_imageFilter(fuzz, 20);
1716
1717 SkPaint paint;
1718 paint.setImageFilter(fil);
1719 SkBitmap bitmap;
1720 SkCanvas canvas(bitmap);
1721 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1722}
1723
1724
1725//SkRandom _rand;
1726#define SK_ADD_RANDOM_BIT_FLIPS
1727
1728DEF_FUZZ(SerializedImageFilter, fuzz) {
1729 auto filter = make_fuzz_imageFilter(fuzz, 20);
1730 auto data = filter->serialize();
1731 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1732 size_t len = data->size();
1733#ifdef SK_ADD_RANDOM_BIT_FLIPS
1734 unsigned char* p = const_cast<unsigned char*>(ptr);
1735 for (size_t i = 0; i < len; ++i, ++p) {
1736 uint8_t j;
1737 fuzz->nextRange(&j, 1, 250);
1738 if (j == 1) { // 0.4% of the time, flip a bit or byte
1739 uint8_t k;
1740 fuzz->nextRange(&k, 1, 10);
1741 if (k == 1) { // Then 10% of the time, change a whole byte
1742 uint8_t s;
1743 fuzz->nextRange(&s, 0, 2);
1744 switch(s) {
1745 case 0:
1746 *p ^= 0xFF; // Flip entire byte
1747 break;
1748 case 1:
1749 *p = 0xFF; // Set all bits to 1
1750 break;
1751 case 2:
1752 *p = 0x00; // Set all bits to 0
1753 break;
1754 }
1755 } else {
1756 uint8_t s;
1757 fuzz->nextRange(&s, 0, 7);
1758 *p ^= (1 << 7);
1759 }
1760 }
1761 }
1762#endif // SK_ADD_RANDOM_BIT_FLIPS
1763 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1764
1765 // uncomment below to write out a serialized image filter (to make corpus
1766 // for -t filter_fuzz)
1767 // SkString s("./serialized_filters/sf");
1768 // s.appendU32(_rand.nextU());
1769 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1770 // sk_fwrite(data->bytes(), data->size(), file);
1771 // sk_fclose(file);
1772
1773 SkPaint paint;
1774 paint.setImageFilter(deserializedFil);
1775 SkBitmap bitmap;
1776 SkCanvas canvas(bitmap);
1777 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1778}
1779
Hal Canary44801ca2017-03-15 11:39:06 -04001780#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001781static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1782 SkASSERT(context);
1783 auto surface = SkSurface::MakeRenderTarget(
1784 context,
1785 SkBudgeted::kNo,
1786 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1787 SkASSERT(surface && surface->getCanvas());
1788 fuzz_canvas(fuzz, surface->getCanvas());
1789}
1790
Hal Canary44801ca2017-03-15 11:39:06 -04001791DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001792 sk_gpu_test::GrContextFactory f;
1793 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001794 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001795 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001796 }
Hal Canary5aa91582017-03-21 11:11:44 -07001797 fuzz_ganesh(fuzz, context);
1798}
1799
1800DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001801 sk_gpu_test::GrContextFactory f;
1802 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001803}
1804
1805DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001806 sk_gpu_test::GrContextFactory f;
1807 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001808}
1809#endif
1810
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001811DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001812 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001813 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001814 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1815 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001816}
1817
1818// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1819DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001820 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001821 fuzz_canvas(fuzz, &debugCanvas);
1822 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1823 UrlDataManager dataManager(SkString("data"));
1824 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1825 Json::StyledStreamWriter(" ").write(std::cout, json);
1826}