blob: 317578caca955fc393b4c3c76f80dfd39fc30fcd [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"
Hal Canary24ac42b2017-02-14 13:35:14 -05009
10// CORE
11#include "SkCanvas.h"
12#include "SkColorFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050013#include "SkDebugCanvas.h"
14#include "SkDocument.h"
Hal Canary671e4422017-02-27 13:36:38 -050015#include "SkFontMgr.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050016#include "SkImageFilter.h"
17#include "SkMaskFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050018#include "SkNullCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050019#include "SkPathEffect.h"
20#include "SkPictureRecorder.h"
Mike Reed54518ac2017-07-22 08:29:48 -040021#include "SkPoint3.h"
Hal Canary5395c592017-03-08 16:52:18 -050022#include "SkRSXform.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050023#include "SkRegion.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050024#include "SkSurface.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050025#include "SkTypeface.h"
26
27// EFFECTS
Hal Canary5395c592017-03-08 16:52:18 -050028#include "Sk1DPathEffect.h"
29#include "Sk2DPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050030#include "SkAlphaThresholdFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050031#include "SkArcToPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050032#include "SkArithmeticImageFilter.h"
Robert Phillips70e3e9a2017-06-26 14:22:01 -040033#include "SkBlurImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050034#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050035#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050036#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050037#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050038#include "SkCornerPathEffect.h"
39#include "SkDashPathEffect.h"
40#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050041#include "SkDisplacementMapEffect.h"
42#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050043#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050044#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050045#include "SkImageSource.h"
46#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050047#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050048#include "SkMagnifierImageFilter.h"
49#include "SkMatrixConvolutionImageFilter.h"
50#include "SkMergeImageFilter.h"
51#include "SkMorphologyImageFilter.h"
52#include "SkOffsetImageFilter.h"
53#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050054#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050055#include "SkPictureImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050056#include "SkRRectsGaussianEdgeMaskFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050057#include "SkTableColorFilter.h"
Mike Reedc090c642017-05-16 10:39:06 -040058#include "SkTextBlob.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050059#include "SkTileImageFilter.h"
60#include "SkXfermodeImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050061
62// SRC
63#include "SkUtils.h"
64
Hal Canary44801ca2017-03-15 11:39:06 -040065#if SK_SUPPORT_GPU
66#include "GrContextFactory.h"
67#endif
68
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050069// MISC
70
71#include <iostream>
72
Hal Canary24ac42b2017-02-14 13:35:14 -050073// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050074// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040075// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050076
Hal Canaryf7005202017-03-10 08:48:28 -050077template <typename T, typename Min, typename Max>
78inline void fuzz_enum_range(Fuzz* fuzz, T* value, Min rmin, Max rmax) {
Hal Canary24ac42b2017-02-14 13:35:14 -050079 using U = skstd::underlying_type_t<T>;
Hal Canaryf7005202017-03-10 08:48:28 -050080 fuzz->nextRange((U*)value, (U)rmin, (U)rmax);
81}
82
Hal Canary1e0138b2017-03-10 13:56:08 -050083// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
84// In fact, all make_fuzz_foo() functions have this potential problem.
85// Use sequence points!
86template <typename T>
87inline T make_fuzz_t(Fuzz* fuzz) {
88 T t;
89 fuzz->next(&t);
90 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050091}
92
Hal Canary1e0138b2017-03-10 13:56:08 -050093// We don't always want to test NaNs and infinities.
Hal Canaryce540ea2017-03-06 08:30:44 -050094static void fuzz_nice_float(Fuzz* fuzz, float* f) {
Hal Canary1e0138b2017-03-10 13:56:08 -050095 float v;
96 fuzz->next(&v);
97 constexpr float kLimit = 1.0e35f; // FLT_MAX?
98 *f = (v == v && v <= kLimit && v >= -kLimit) ? v : 0.0f;
Hal Canaryce540ea2017-03-06 08:30:44 -050099}
Hal Canary24ac42b2017-02-14 13:35:14 -0500100
Hal Canaryce540ea2017-03-06 08:30:44 -0500101template <typename... Args>
Hal Canary1e0138b2017-03-10 13:56:08 -0500102inline void fuzz_nice_float(Fuzz* fuzz, float* f, Args... rest) {
Hal Canaryce540ea2017-03-06 08:30:44 -0500103 fuzz_nice_float(fuzz, f);
104 fuzz_nice_float(fuzz, rest...);
105}
106
107static void fuzz_path(Fuzz* fuzz, SkPath* path, int maxOps) {
108 if (maxOps < 2) {
109 maxOps = 2;
110 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500111 uint8_t fillType;
112 fuzz->nextRange(&fillType, 0, (uint8_t)SkPath::kInverseEvenOdd_FillType);
113 path->setFillType((SkPath::FillType)fillType);
114 uint8_t numOps;
Hal Canaryce540ea2017-03-06 08:30:44 -0500115 fuzz->nextRange(&numOps, 2, maxOps);
Hal Canary24ac42b2017-02-14 13:35:14 -0500116 for (uint8_t i = 0; i < numOps; ++i) {
117 uint8_t op;
118 fuzz->nextRange(&op, 0, 6);
119 SkScalar a, b, c, d, e, f;
120 switch (op) {
121 case 0:
Hal Canaryce540ea2017-03-06 08:30:44 -0500122 fuzz_nice_float(fuzz, &a, &b);
Hal Canary24ac42b2017-02-14 13:35:14 -0500123 path->moveTo(a, b);
124 break;
125 case 1:
Hal Canaryce540ea2017-03-06 08:30:44 -0500126 fuzz_nice_float(fuzz, &a, &b);
Hal Canary24ac42b2017-02-14 13:35:14 -0500127 path->lineTo(a, b);
128 break;
129 case 2:
Hal Canaryce540ea2017-03-06 08:30:44 -0500130 fuzz_nice_float(fuzz, &a, &b, &c, &d);
Hal Canary24ac42b2017-02-14 13:35:14 -0500131 path->quadTo(a, b, c, d);
132 break;
133 case 3:
Hal Canaryce540ea2017-03-06 08:30:44 -0500134 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
Hal Canary24ac42b2017-02-14 13:35:14 -0500135 path->conicTo(a, b, c, d, e);
136 break;
137 case 4:
Hal Canaryce540ea2017-03-06 08:30:44 -0500138 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e, &f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500139 path->cubicTo(a, b, c, d, e, f);
140 break;
141 case 5:
Hal Canaryce540ea2017-03-06 08:30:44 -0500142 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
Hal Canary24ac42b2017-02-14 13:35:14 -0500143 path->arcTo(a, b, c, d, e);
144 break;
145 case 6:
146 path->close();
147 break;
148 default:
149 break;
150 }
151 }
152}
153
Hal Canarye03c3e52017-03-09 11:33:35 -0500154template <>
155inline void Fuzz::next(SkRegion* region) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500156 uint8_t N;
Hal Canarye03c3e52017-03-09 11:33:35 -0500157 this->nextRange(&N, 0, 10);
Hal Canary24ac42b2017-02-14 13:35:14 -0500158 for (uint8_t i = 0; i < N; ++i) {
159 SkIRect r;
160 uint8_t op;
Hal Canarye03c3e52017-03-09 11:33:35 -0500161 this->next(&r);
Hal Canary24ac42b2017-02-14 13:35:14 -0500162 r.sort();
Hal Canarye03c3e52017-03-09 11:33:35 -0500163 this->nextRange(&op, 0, (uint8_t)SkRegion::kLastOp);
Hal Canary24ac42b2017-02-14 13:35:14 -0500164 if (!region->op(r, (SkRegion::Op)op)) {
165 return;
166 }
167 }
168}
169
Hal Canaryb69c4b82017-03-08 11:02:40 -0500170template <>
171inline void Fuzz::next(SkShader::TileMode* m) {
Hal Canaryf7005202017-03-10 08:48:28 -0500172 fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500173}
174
Hal Canaryb69c4b82017-03-08 11:02:40 -0500175template <>
Hal Canarye03c3e52017-03-09 11:33:35 -0500176inline void Fuzz::next(SkFilterQuality* q) {
Hal Canaryf7005202017-03-10 08:48:28 -0500177 fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
178 SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500179}
180
181template <>
Hal Canaryb69c4b82017-03-08 11:02:40 -0500182inline void Fuzz::next(SkMatrix* m) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500183 constexpr int kArrayLength = 9;
184 SkScalar buffer[kArrayLength];
185 int matrixType;
186 this->nextRange(&matrixType, 0, 4);
187 switch (matrixType) {
188 case 0: // identity
189 *m = SkMatrix::I();
190 return;
191 case 1: // translate
192 this->nextRange(&buffer[0], -4000.0f, 4000.0f);
193 this->nextRange(&buffer[1], -4000.0f, 4000.0f);
194 *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
195 return;
196 case 2: // translate + scale
197 this->nextRange(&buffer[0], -400.0f, 400.0f);
198 this->nextRange(&buffer[1], -400.0f, 400.0f);
199 this->nextRange(&buffer[2], -4000.0f, 4000.0f);
200 this->nextRange(&buffer[3], -4000.0f, 4000.0f);
201 *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
202 m->postTranslate(buffer[2], buffer[3]);
203 return;
204 case 3: // affine
205 this->nextN(buffer, 6);
206 m->setAffine(buffer);
207 return;
208 case 4: // perspective
209 this->nextN(buffer, kArrayLength);
210 m->set9(buffer);
211 return;
212 default:
213 return;
214 }
215}
216
Hal Canaryb69c4b82017-03-08 11:02:40 -0500217template <>
218inline void Fuzz::next(SkRRect* rr) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500219 SkRect r;
220 SkVector radii[4];
221 this->next(&r);
Hal Canary27bece82017-03-07 16:23:20 -0500222 r.sort();
223 for (SkVector& vec : radii) {
224 this->nextRange(&vec.fX, 0.0f, 1.0f);
225 vec.fX *= 0.5f * r.width();
226 this->nextRange(&vec.fY, 0.0f, 1.0f);
227 vec.fY *= 0.5f * r.height();
228 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500229 rr->setRectRadii(r, radii);
Hal Canary24ac42b2017-02-14 13:35:14 -0500230}
231
Hal Canaryb69c4b82017-03-08 11:02:40 -0500232template <>
233inline void Fuzz::next(SkBlendMode* mode) {
Hal Canaryf7005202017-03-10 08:48:28 -0500234 fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500235}
236
Hal Canary1e0138b2017-03-10 13:56:08 -0500237static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -0500238
Hal Canary1e0138b2017-03-10 13:56:08 -0500239static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500240
Hal Canary1e0138b2017-03-10 13:56:08 -0500241static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500242
Hal Canary1e0138b2017-03-10 13:56:08 -0500243static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500244 if (depth <= 0) {
245 return nullptr;
246 }
247 int colorFilterType;
248 fuzz->nextRange(&colorFilterType, 0, 8);
249 switch (colorFilterType) {
250 case 0:
251 return nullptr;
252 case 1: {
253 SkColor color;
254 SkBlendMode mode;
255 fuzz->next(&color, &mode);
256 return SkColorFilter::MakeModeFilter(color, mode);
257 }
258 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500259 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
260 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary27bece82017-03-07 16:23:20 -0500261 return SkColorFilter::MakeComposeFilter(std::move(outer), std::move(inner));
262 }
263 case 3: {
264 SkScalar array[20];
265 fuzz->nextN(array, SK_ARRAY_COUNT(array));
266 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
267 }
268 case 4: {
269 SkColor mul, add;
270 fuzz->next(&mul, &add);
271 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
272 }
273 case 5: {
274 bool grayscale;
275 int invertStyle;
276 float contrast;
277 fuzz->next(&grayscale);
278 fuzz->nextRange(&invertStyle, 0, 2);
279 fuzz->nextRange(&contrast, -1.0f, 1.0f);
280 return SkHighContrastFilter::Make(SkHighContrastConfig(
281 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
282 }
283 case 6:
284 return SkLumaColorFilter::Make();
285 case 7: {
286 uint8_t table[256];
287 fuzz->nextN(table, SK_ARRAY_COUNT(table));
288 return SkTableColorFilter::Make(table);
289 }
290 case 8: {
291 uint8_t tableA[256];
292 uint8_t tableR[256];
293 uint8_t tableG[256];
294 uint8_t tableB[256];
295 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
296 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
297 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
298 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
299 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
300 }
301 }
302 return nullptr;
303}
Hal Canary24ac42b2017-02-14 13:35:14 -0500304
Hal Canary1e0138b2017-03-10 13:56:08 -0500305static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500306 SkScalar totalPos = 0;
307 for (int i = 0; i < colorCount; ++i) {
308 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
309 totalPos += pos[i];
310 }
311 totalPos = 1.0f / totalPos;
312 for (int i = 0; i < colorCount; ++i) {
313 pos[i] *= totalPos;
314 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500315 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500316 pos[colorCount - 1] = 1.0f;
317}
318
Hal Canary1e0138b2017-03-10 13:56:08 -0500319static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500320 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
321 sk_sp<SkColorFilter> colorFilter(nullptr);
322 SkBitmap bitmap;
323 sk_sp<SkImage> img;
324 SkShader::TileMode tmX, tmY;
325 bool useMatrix;
326 SkColor color;
327 SkMatrix matrix;
328 SkBlendMode blendMode;
329 int shaderType;
330 if (depth <= 0) {
331 return nullptr;
332 }
Hal Canary671e4422017-02-27 13:36:38 -0500333 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500334 switch (shaderType) {
335 case 0:
336 return nullptr;
337 case 1:
338 return SkShader::MakeEmptyShader();
339 case 2:
340 fuzz->next(&color);
341 return SkShader::MakeColorShader(color);
342 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500343 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500344 fuzz->next(&tmX, &tmY, &useMatrix);
345 if (useMatrix) {
346 fuzz->next(&matrix);
347 }
348 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
349 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500350 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500351 fuzz->next(&tmX, &tmY, &useMatrix);
352 if (useMatrix) {
353 fuzz->next(&matrix);
354 }
355 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
356 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500357 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500358 fuzz->next(&matrix);
359 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
360 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500361 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
362 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500363 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
364 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500365 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
366 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500367 fuzz->next(&blendMode);
368 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500369 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500370 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500371 bool useTile;
372 SkRect tile;
373 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
374 if (useMatrix) {
375 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500376 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500377 if (useTile) {
378 fuzz->next(&tile);
379 }
380 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
381 useMatrix ? &matrix : nullptr,
382 useTile ? &tile : nullptr);
383 }
Hal Canary671e4422017-02-27 13:36:38 -0500384 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500385 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000386 // Deprecated SkGaussianEdgeShader
387 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500388 case 10: {
389 constexpr int kMaxColors = 12;
390 SkPoint pts[2];
391 SkColor colors[kMaxColors];
392 SkScalar pos[kMaxColors];
393 int colorCount;
394 bool usePos;
395 fuzz->nextN(pts, 2);
396 fuzz->nextRange(&colorCount, 2, kMaxColors);
397 fuzz->nextN(colors, colorCount);
398 fuzz->next(&tmX, &useMatrix, &usePos);
399 if (useMatrix) {
400 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500401 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500402 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500403 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500404 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500405 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
406 tmX, 0, useMatrix ? &matrix : nullptr);
407 }
408 case 11: {
409 constexpr int kMaxColors = 12;
410 SkPoint center;
411 SkScalar radius;
412 int colorCount;
413 bool usePos;
414 SkColor colors[kMaxColors];
415 SkScalar pos[kMaxColors];
416 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
417 fuzz->nextRange(&colorCount, 2, kMaxColors);
418 fuzz->nextN(colors, colorCount);
419 if (useMatrix) {
420 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500421 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500422 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500423 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500424 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500425 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
426 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
427 }
428 case 12: {
429 constexpr int kMaxColors = 12;
430 SkPoint start, end;
431 SkScalar startRadius, endRadius;
432 int colorCount;
433 bool usePos;
434 SkColor colors[kMaxColors];
435 SkScalar pos[kMaxColors];
436 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
437 fuzz->nextRange(&colorCount, 2, kMaxColors);
438 fuzz->nextN(colors, colorCount);
439 if (useMatrix) {
440 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500441 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500442 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500443 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500444 }
445 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
446 usePos ? pos : nullptr, colorCount, tmX, 0,
447 useMatrix ? &matrix : nullptr);
448 }
449 case 13: {
450 constexpr int kMaxColors = 12;
451 SkScalar cx, cy;
452 int colorCount;
453 bool usePos;
454 SkColor colors[kMaxColors];
455 SkScalar pos[kMaxColors];
456 fuzz->next(&cx, &cy, &useMatrix, &usePos);
457 fuzz->nextRange(&colorCount, 2, kMaxColors);
458 fuzz->nextN(colors, colorCount);
459 if (useMatrix) {
460 fuzz->next(&matrix);
461 }
462 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500463 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500464 }
465 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
466 0, useMatrix ? &matrix : nullptr);
467 }
468 case 14: {
469 SkScalar baseFrequencyX, baseFrequencyY, seed;
470 int numOctaves;
471 SkISize tileSize;
472 bool useTileSize, turbulence;
473 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
474 if (useTileSize) {
475 fuzz->next(&tileSize);
476 }
477 fuzz->nextRange(&numOctaves, 2, 7);
478 if (turbulence) {
479 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
480 numOctaves, seed,
481 useTileSize ? &tileSize : nullptr);
482 } else {
483 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
484 numOctaves, seed,
485 useTileSize ? &tileSize : nullptr);
486 }
487 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500488 default:
489 break;
490 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500491 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500492}
493
Hal Canary1e0138b2017-03-10 13:56:08 -0500494static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500495 if (depth <= 0) {
496 return nullptr;
497 }
498 uint8_t pathEffectType;
499 fuzz->nextRange(&pathEffectType, 0, 9);
500 switch (pathEffectType) {
501 case 0: {
502 return nullptr;
503 }
504 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500505 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
506 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500507 return SkPathEffect::MakeSum(std::move(first), std::move(second));
508 }
509 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500510 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
511 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500512 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
513 }
514 case 3: {
515 SkPath path;
516 fuzz_path(fuzz, &path, 20);
517 SkScalar advance, phase;
518 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500519 SkPath1DPathEffect::Style style;
520 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
521 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500522 }
523 case 4: {
524 SkScalar width;
525 SkMatrix matrix;
526 fuzz->next(&width, &matrix);
527 return SkLine2DPathEffect::Make(width, matrix);
528 }
529 case 5: {
530 SkPath path;
531 fuzz_path(fuzz, &path, 20);
532 SkMatrix matrix;
533 fuzz->next(&matrix);
534 return SkPath2DPathEffect::Make(matrix, path);
535 }
536 case 6: {
537 SkScalar radius;
538 fuzz->next(&radius);
539 return SkArcToPathEffect::Make(radius);
540 }
541 case 7: {
542 SkScalar radius;
543 fuzz->next(&radius);
544 return SkCornerPathEffect::Make(radius);
545 }
546 case 8: {
547 SkScalar phase;
548 fuzz->next(&phase);
549 SkScalar intervals[20];
550 int count;
551 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
552 fuzz->nextN(intervals, count);
553 return SkDashPathEffect::Make(intervals, count, phase);
554 }
555 case 9: {
556 SkScalar segLength, dev;
557 uint32_t seed;
558 fuzz->next(&segLength, &dev, &seed);
559 return SkDiscretePathEffect::Make(segLength, dev, seed);
560 }
561 default:
562 SkASSERT(false);
563 return nullptr;
564 }
565}
Hal Canary24ac42b2017-02-14 13:35:14 -0500566
Hal Canary1e0138b2017-03-10 13:56:08 -0500567static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500568 int maskfilterType;
569 fuzz->nextRange(&maskfilterType, 0, 2);
570 switch (maskfilterType) {
571 case 0:
572 return nullptr;
573 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500574 SkBlurStyle blurStyle;
575 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500576 SkScalar sigma;
577 fuzz->next(&sigma);
578 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Hal Canary1e0138b2017-03-10 13:56:08 -0500579 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -0500580 fuzz->next(&occluder);
581 }
582 uint32_t flags;
583 fuzz->nextRange(&flags, 0, 3);
Hal Canaryf7005202017-03-10 08:48:28 -0500584 return SkBlurMaskFilter::Make(blurStyle, sigma, occluder, flags);
Hal Canary5395c592017-03-08 16:52:18 -0500585 }
586 case 2: {
587 SkRRect first, second;
588 SkScalar radius;
589 fuzz->next(&first, &second, &radius);
590 return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
591 }
592 default:
593 SkASSERT(false);
594 return nullptr;
595 }
596}
Hal Canary24ac42b2017-02-14 13:35:14 -0500597
Hal Canary1e0138b2017-03-10 13:56:08 -0500598static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
599 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500600 return nullptr;
601 }
602 auto fontMugger = SkFontMgr::RefDefault();
603 SkASSERT(fontMugger);
604 int familyCount = fontMugger->countFamilies();
605 int i, j;
606 fuzz->nextRange(&i, 0, familyCount - 1);
607 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
608 int styleCount = family->count();
609 fuzz->nextRange(&j, 0, styleCount - 1);
610 return sk_sp<SkTypeface>(family->createTypeface(j));
611}
Hal Canary24ac42b2017-02-14 13:35:14 -0500612
Hal Canarye03c3e52017-03-09 11:33:35 -0500613template <>
614inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
615 SkRect rect;
616 uint8_t flags;
617 this->next(&rect);
618 this->nextRange(&flags, 0, 0xF);
619 *cropRect = SkImageFilter::CropRect(rect, flags);
620}
621
Hal Canary1e0138b2017-03-10 13:56:08 -0500622static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500623
624static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
625 if (depth <= 0) {
626 return nullptr;
627 }
628 uint8_t imageFilterType;
629 fuzz->nextRange(&imageFilterType, 1, 6);
630 SkPoint3 p, q;
631 SkColor lightColor;
632 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
633 sk_sp<SkImageFilter> input;
634 SkImageFilter::CropRect cropRect;
635 bool useCropRect;
636 fuzz->next(&useCropRect);
637 if (useCropRect) {
638 fuzz->next(&cropRect);
639 }
640 switch (imageFilterType) {
641 case 1:
642 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500643 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500644 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
645 std::move(input),
646 useCropRect ? &cropRect : nullptr);
647 case 2:
648 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500649 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500650 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
651 std::move(input),
652 useCropRect ? &cropRect : nullptr);
653 case 3:
654 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500655 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500656 return SkLightingImageFilter::MakeSpotLitDiffuse(
657 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
658 std::move(input), useCropRect ? &cropRect : nullptr);
659 case 4:
660 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500661 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500662 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
663 shininess, std::move(input),
664 useCropRect ? &cropRect : nullptr);
665 case 5:
666 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500667 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500668 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
669 shininess, std::move(input),
670 useCropRect ? &cropRect : nullptr);
671 case 6:
672 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
673 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500674 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500675 return SkLightingImageFilter::MakeSpotLitSpecular(
676 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
677 std::move(input), useCropRect ? &cropRect : nullptr);
678 default:
679 SkASSERT(false);
680 return nullptr;
681 }
682}
683
Hal Canary1e0138b2017-03-10 13:56:08 -0500684static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500685
Hal Canary1e0138b2017-03-10 13:56:08 -0500686static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500687 if (depth <= 0) {
688 return nullptr;
689 }
690 uint8_t imageFilterType;
691 fuzz->nextRange(&imageFilterType, 0, 24);
692 switch (imageFilterType) {
693 case 0:
694 return nullptr;
695 case 1: {
696 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500697 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500698 bool useCropRect;
699 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400700 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500701 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400702 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500703 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400704 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500705 useCropRect ? &cropRect : nullptr);
706 }
707 case 2: {
708 SkMatrix matrix;
709 SkFilterQuality quality;
710 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500711 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500712 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
713 }
714 case 3: {
715 SkRegion region;
716 SkScalar innerMin, outerMax;
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 bool useCropRect;
719 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400720 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500721 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400722 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500723 }
724 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
725 useCropRect ? &cropRect : nullptr);
726 }
727 case 4: {
728 float k1, k2, k3, k4;
729 bool enforcePMColor;
730 bool useCropRect;
731 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500732 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
733 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500734 SkImageFilter::CropRect cropRect;
735 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400736 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500737 }
738 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
739 std::move(background), std::move(foreground),
740 useCropRect ? &cropRect : nullptr);
741 }
742 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500743 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
744 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500745 bool useCropRect;
746 SkImageFilter::CropRect cropRect;
747 fuzz->next(&useCropRect);
748 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400749 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500750 }
751 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
752 useCropRect ? &cropRect : nullptr);
753 }
754 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500755 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
756 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500757 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
758 }
759 case 7: {
760 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400761 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
762 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500763 SkScalar scale;
764 bool useCropRect;
765 fuzz->next(&scale, &useCropRect);
766 SkImageFilter::CropRect cropRect;
767 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400768 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500769 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500770 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
771 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500772 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
773 std::move(displacement), std::move(color),
774 useCropRect ? &cropRect : nullptr);
775 }
776 case 8: {
777 SkScalar dx, dy, sigmaX, sigmaY;
778 SkColor color;
779 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500780 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500781 bool useCropRect;
782 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
783 SkImageFilter::CropRect cropRect;
784 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400785 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500786 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500787 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500788 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
789 std::move(input),
790 useCropRect ? &cropRect : nullptr);
791 }
792 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500793 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500794 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500795 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500796 SkRect srcRect, dstRect;
797 SkFilterQuality filterQuality;
798 fuzz->next(&srcRect, &dstRect, &filterQuality);
799 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
800 }
801 case 11:
802 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
803 case 12: {
804 SkRect srcRect;
805 SkScalar inset;
806 bool useCropRect;
807 SkImageFilter::CropRect cropRect;
808 fuzz->next(&srcRect, &inset, &useCropRect);
809 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400810 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500811 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500812 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500813 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
814 useCropRect ? &cropRect : nullptr);
815 }
816 case 13: {
817 constexpr int kMaxKernelSize = 5;
818 int32_t n, m;
819 fuzz->nextRange(&n, 1, kMaxKernelSize);
820 fuzz->nextRange(&m, 1, kMaxKernelSize);
821 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
822 fuzz->nextN(kernel, n * m);
823 int32_t offsetX, offsetY;
824 fuzz->nextRange(&offsetX, 0, n - 1);
825 fuzz->nextRange(&offsetY, 0, m - 1);
826 SkScalar gain, bias;
827 bool convolveAlpha, useCropRect;
828 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
829 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500830 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500831 SkImageFilter::CropRect cropRect;
832 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400833 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500834 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500835 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500836 return SkMatrixConvolutionImageFilter::Make(
837 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
838 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
839 }
840 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500841 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
842 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500843 bool useCropRect;
844 fuzz->next(&useCropRect);
845 SkImageFilter::CropRect cropRect;
846 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400847 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500848 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400849 return SkMergeImageFilter::Make(std::move(first), std::move(second),
850 useCropRect ? &cropRect : nullptr);
851 }
852 case 15: {
853 constexpr int kMaxCount = 4;
854 sk_sp<SkImageFilter> ifs[kMaxCount];
855 int count;
856 fuzz->nextRange(&count, 1, kMaxCount);
857 for (int i = 0; i < count; ++i) {
858 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
859 }
860 bool useCropRect;
861 fuzz->next(&useCropRect);
862 SkImageFilter::CropRect cropRect;
863 if (useCropRect) {
864 fuzz->next(&cropRect);
865 }
866 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500867 }
868 case 16: {
869 int rx, ry;
870 fuzz->next(&rx, &ry);
871 bool useCropRect;
872 fuzz->next(&useCropRect);
873 SkImageFilter::CropRect cropRect;
874 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400875 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500876 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500877 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500878 return SkDilateImageFilter::Make(rx, ry, std::move(input),
879 useCropRect ? &cropRect : nullptr);
880 }
881 case 17: {
882 int rx, ry;
883 fuzz->next(&rx, &ry);
884 bool useCropRect;
885 fuzz->next(&useCropRect);
886 SkImageFilter::CropRect cropRect;
887 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400888 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500889 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500890 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500891 return SkErodeImageFilter::Make(rx, ry, std::move(input),
892 useCropRect ? &cropRect : nullptr);
893 }
894 case 18: {
895 SkScalar dx, dy;
896 fuzz->next(&dx, &dy);
897 bool useCropRect;
898 fuzz->next(&useCropRect);
899 SkImageFilter::CropRect cropRect;
900 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400901 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500902 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500903 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500904 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
905 useCropRect ? &cropRect : nullptr);
906 }
907 case 19: {
908 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500909 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500910 bool useCropRect;
911 fuzz->next(&useCropRect);
912 SkImageFilter::CropRect cropRect;
913 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400914 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500915 }
916 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
917 }
918 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500919 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500920 return SkPictureImageFilter::Make(std::move(picture));
921 }
922 case 21: {
923 SkRect cropRect;
924 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500925 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500926 return SkPictureImageFilter::Make(std::move(picture), cropRect);
927 }
928 case 22: {
929 SkRect cropRect;
930 SkFilterQuality filterQuality;
931 fuzz->next(&cropRect, &filterQuality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500932 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500933 return SkPictureImageFilter::MakeForLocalSpace(std::move(picture), cropRect,
934 filterQuality);
935 }
936 case 23: {
937 SkRect src, dst;
938 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500939 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500940 return SkTileImageFilter::Make(src, dst, std::move(input));
941 }
942 case 24: {
943 SkBlendMode blendMode;
944 bool useCropRect;
945 fuzz->next(&useCropRect, &blendMode);
946 SkImageFilter::CropRect cropRect;
947 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400948 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500949 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500950 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
951 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500952 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
953 useCropRect ? &cropRect : nullptr);
954 }
955 default:
956 SkASSERT(false);
957 return nullptr;
958 }
959}
Hal Canary24ac42b2017-02-14 13:35:14 -0500960
Hal Canary1e0138b2017-03-10 13:56:08 -0500961static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500962 int w, h;
963 fuzz->nextRange(&w, 1, 1024);
964 fuzz->nextRange(&h, 1, 1024);
965 SkAutoTMalloc<SkPMColor> data(w * h);
966 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
967 int n = w * h;
968 for (int i = 0; i < n; ++i) {
969 SkColor c;
970 fuzz->next(&c);
971 data[i] = SkPreMultiplyColor(c);
972 }
973 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500974 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
975 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500976}
977
Hal Canary1e0138b2017-03-10 13:56:08 -0500978static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500979 SkBitmap bitmap;
980 int w, h;
981 fuzz->nextRange(&w, 1, 1024);
982 fuzz->nextRange(&h, 1, 1024);
983 bitmap.allocN32Pixels(w, h);
Hal Canary24ac42b2017-02-14 13:35:14 -0500984 for (int y = 0; y < h; ++y) {
985 for (int x = 0; x < w; ++x) {
986 SkColor c;
987 fuzz->next(&c);
988 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
989 }
990 }
991 return bitmap;
992}
993
Hal Canary1e0138b2017-03-10 13:56:08 -0500994template <typename T, typename Min, typename Max>
995inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
996 T value;
997 fuzz_enum_range(fuzz, &value, minv, maxv);
998 return value;
999}
1000
1001static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001002 if (!fuzz || !paint || depth <= 0) {
1003 return;
1004 }
1005
Hal Canary1e0138b2017-03-10 13:56:08 -05001006 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
1007 paint->setDither( make_fuzz_t<bool>(fuzz));
1008 paint->setColor( make_fuzz_t<SkColor>(fuzz));
1009 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
1010 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
1011 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
1012 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
1013 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
1014 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
1015 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
1016 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -05001017
1018 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001019 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
1020 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
1021 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
1022 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -05001023 }
1024}
1025
Hal Canary1e0138b2017-03-10 13:56:08 -05001026static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
1027 paint->setTypeface( make_fuzz_typeface(fuzz));
1028 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
1029 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
1030 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
1031 paint->setLinearText( make_fuzz_t<bool>(fuzz));
1032 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
1033 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
1034 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
1035 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
1036 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
1037 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
1038 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
1039 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
1040 SkPaint::kFull_Hinting));
1041 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -05001042}
1043
1044static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001045 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -05001046}
1047
Hal Canary5395c592017-03-08 16:52:18 -05001048constexpr int kMaxGlyphCount = 30;
1049
Hal Canary1e0138b2017-03-10 13:56:08 -05001050static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -05001051 SkTDArray<uint8_t> array;
1052 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001053 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
1054 : SkTypeface::MakeDefault()->countGlyphs();
Hal Canary671e4422017-02-27 13:36:38 -05001055 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -05001056 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001057 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
1058 for (int i = 0; i < glyphCount; ++i) {
1059 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
1060 }
1061 return array;
1062 }
1063 static const SkUnichar ranges[][2] = {
1064 {0x0020, 0x007F},
1065 {0x00A1, 0x0250},
1066 {0x0400, 0x0500},
1067 };
1068 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001069 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001070 count += (ranges[i][1] - ranges[i][0]);
1071 }
Hal Canary5395c592017-03-08 16:52:18 -05001072 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -05001073 SkUnichar buffer[kMaxLength];
1074 int length;
1075 fuzz->nextRange(&length, 1, kMaxLength);
1076 for (int j = 0; j < length; ++j) {
1077 int32_t value;
1078 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001079 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001080 if (value + ranges[i][0] < ranges[i][1]) {
1081 buffer[j] = value + ranges[i][0];
1082 break;
1083 } else {
1084 value -= (ranges[i][1] - ranges[i][0]);
1085 }
1086 }
1087 }
1088 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001089 case SkPaint::kUTF8_TextEncoding: {
1090 size_t utf8len = 0;
1091 for (int j = 0; j < length; ++j) {
1092 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001093 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001094 char* ptr = (char*)array.append(utf8len);
1095 for (int j = 0; j < length; ++j) {
1096 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001097 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001098 } break;
1099 case SkPaint::kUTF16_TextEncoding: {
1100 size_t utf16len = 0;
1101 for (int j = 0; j < length; ++j) {
1102 utf16len += SkUTF16_FromUnichar(buffer[j]);
1103 }
1104 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1105 for (int j = 0; j < length; ++j) {
1106 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1107 }
1108 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001109 case SkPaint::kUTF32_TextEncoding:
1110 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001111 break;
Hal Canary671e4422017-02-27 13:36:38 -05001112 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001113 SkASSERT(false);
Hal Canary671e4422017-02-27 13:36:38 -05001114 }
1115 return array;
1116}
1117
Hal Canary5395c592017-03-08 16:52:18 -05001118static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1119 SkTextBlobBuilder textBlobBuilder;
1120 int8_t runCount;
1121 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1122 while (runCount-- > 0) {
1123 SkPaint paint;
1124 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001125 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001126 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1127 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1128 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1129 SkASSERT(glyphCount <= kMaxGlyphCount);
1130 SkScalar x, y;
1131 const SkTextBlobBuilder::RunBuffer* buffer;
1132 uint8_t runType;
1133 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1134 switch (runType) {
1135 case 0:
1136 fuzz->next(&x, &y);
1137 // TODO: Test other variations of this.
1138 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1139 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1140 break;
1141 case 1:
1142 fuzz->next(&y);
1143 // TODO: Test other variations of this.
1144 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1145 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1146 fuzz->nextN(buffer->pos, glyphCount);
1147 break;
1148 case 2:
1149 // TODO: Test other variations of this.
1150 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1151 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1152 fuzz->nextN(buffer->pos, glyphCount * 2);
1153 break;
1154 default:
1155 SkASSERT(false);
1156 }
1157 }
1158 return textBlobBuilder.make();
1159}
1160
Hal Canary1e0138b2017-03-10 13:56:08 -05001161static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001162 if (!fuzz || !canvas || depth <= 0) {
1163 return;
1164 }
1165 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1166 unsigned N;
1167 fuzz->nextRange(&N, 0, 2000);
1168 for (unsigned i = 0; i < N; ++i) {
1169 if (fuzz->exhausted()) {
1170 return;
1171 }
1172 SkPaint paint;
1173 SkMatrix matrix;
1174 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001175 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001176 switch (drawCommand) {
1177 case 0:
1178 canvas->flush();
1179 break;
1180 case 1:
1181 canvas->save();
1182 break;
1183 case 2: {
1184 SkRect bounds;
1185 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001186 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001187 canvas->saveLayer(&bounds, &paint);
1188 break;
1189 }
1190 case 3: {
1191 SkRect bounds;
1192 fuzz->next(&bounds);
1193 canvas->saveLayer(&bounds, nullptr);
1194 break;
1195 }
1196 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001197 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001198 canvas->saveLayer(nullptr, &paint);
1199 break;
1200 case 5:
1201 canvas->saveLayer(nullptr, nullptr);
1202 break;
1203 case 6: {
1204 uint8_t alpha;
1205 fuzz->next(&alpha);
1206 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1207 break;
1208 }
1209 case 7: {
1210 SkRect bounds;
1211 uint8_t alpha;
1212 fuzz->next(&bounds, &alpha);
1213 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1214 break;
1215 }
1216 case 8: {
1217 SkCanvas::SaveLayerRec saveLayerRec;
1218 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001219 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001220 fuzz->next(&bounds);
1221 saveLayerRec.fBounds = &bounds;
1222 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001223 if (make_fuzz_t<bool>(fuzz)) {
1224 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001225 saveLayerRec.fPaint = &paint;
1226 }
1227 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001228 if (make_fuzz_t<bool>(fuzz)) {
1229 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001230 saveLayerRec.fBackdrop = imageFilter.get();
1231 }
Hal Canary5395c592017-03-08 16:52:18 -05001232 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001233 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001234 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kIsOpaque_SaveLayerFlag;
1235 // }
Hal Canary1e0138b2017-03-10 13:56:08 -05001236 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001237 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1238 // }
1239
Hal Canary24ac42b2017-02-14 13:35:14 -05001240 canvas->saveLayer(saveLayerRec);
1241 break;
1242 }
1243 case 9:
1244 canvas->restore();
1245 break;
1246 case 10: {
1247 int saveCount;
1248 fuzz->next(&saveCount);
1249 canvas->restoreToCount(saveCount);
1250 break;
1251 }
1252 case 11: {
1253 SkScalar x, y;
1254 fuzz->next(&x, &y);
1255 canvas->translate(x, y);
1256 break;
1257 }
1258 case 12: {
1259 SkScalar x, y;
1260 fuzz->next(&x, &y);
1261 canvas->scale(x, y);
1262 break;
1263 }
1264 case 13: {
1265 SkScalar v;
1266 fuzz->next(&v);
1267 canvas->rotate(v);
1268 break;
1269 }
1270 case 14: {
1271 SkScalar x, y, v;
1272 fuzz->next(&x, &y, &v);
1273 canvas->rotate(v, x, y);
1274 break;
1275 }
1276 case 15: {
1277 SkScalar x, y;
1278 fuzz->next(&x, &y);
1279 canvas->skew(x, y);
1280 break;
1281 }
1282 case 16: {
1283 SkMatrix mat;
1284 fuzz->next(&mat);
1285 canvas->concat(mat);
1286 break;
1287 }
1288 case 17: {
1289 SkMatrix mat;
1290 fuzz->next(&mat);
1291 canvas->setMatrix(mat);
1292 break;
1293 }
1294 case 18:
1295 canvas->resetMatrix();
1296 break;
1297 case 19: {
1298 SkRect r;
1299 int op;
1300 bool doAntiAlias;
1301 fuzz->next(&r, &doAntiAlias);
1302 fuzz->nextRange(&op, 0, 1);
1303 r.sort();
1304 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1305 break;
1306 }
1307 case 20: {
1308 SkRRect rr;
1309 int op;
1310 bool doAntiAlias;
1311 fuzz->next(&rr);
1312 fuzz->next(&doAntiAlias);
1313 fuzz->nextRange(&op, 0, 1);
1314 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1315 break;
1316 }
1317 case 21: {
1318 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001319 fuzz_path(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001320 int op;
1321 bool doAntiAlias;
1322 fuzz->next(&doAntiAlias);
1323 fuzz->nextRange(&op, 0, 1);
1324 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1325 break;
1326 }
1327 case 22: {
1328 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001329 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001330 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001331 fuzz->nextRange(&op, 0, 1);
1332 canvas->clipRegion(region, (SkClipOp)op);
1333 break;
1334 }
1335 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001336 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001337 canvas->drawPaint(paint);
1338 break;
1339 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001340 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001341 uint8_t pointMode;
1342 fuzz->nextRange(&pointMode, 0, 3);
1343 size_t count;
1344 constexpr int kMaxCount = 30;
1345 fuzz->nextRange(&count, 0, kMaxCount);
1346 SkPoint pts[kMaxCount];
1347 fuzz->nextN(pts, count);
1348 canvas->drawPoints((SkCanvas::PointMode)pointMode, count, pts, paint);
1349 break;
1350 }
1351 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001352 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001353 SkRect r;
1354 fuzz->next(&r);
1355 canvas->drawRect(r, paint);
1356 break;
1357 }
1358 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001359 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001360 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001361 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 canvas->drawRegion(region, paint);
1363 break;
1364 }
1365 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001366 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001367 SkRect r;
1368 fuzz->next(&r);
1369 canvas->drawOval(r, paint);
1370 break;
1371 }
1372 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001373 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001374 SkRRect rr;
1375 fuzz->next(&rr);
1376 canvas->drawRRect(rr, paint);
1377 break;
1378 }
1379 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001380 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001381 SkRRect orr, irr;
1382 fuzz->next(&orr);
1383 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001384 if (orr.getBounds().contains(irr.getBounds())) {
1385 canvas->drawDRRect(orr, irr, paint);
1386 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001387 break;
1388 }
1389 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001390 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001391 SkRect r;
1392 SkScalar start, sweep;
1393 bool useCenter;
1394 fuzz->next(&r, &start, &sweep, &useCenter);
1395 canvas->drawArc(r, start, sweep, useCenter, paint);
1396 break;
1397 }
1398 case 32: {
1399 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001400 fuzz_path(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001401 canvas->drawPath(path, paint);
1402 break;
1403 }
1404 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001405 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001406 SkScalar left, top;
1407 bool usePaint;
1408 fuzz->next(&left, &top, &usePaint);
1409 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001410 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001411 }
1412 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1413 break;
1414 }
1415 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001416 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001417 SkRect src, dst;
1418 bool usePaint;
1419 fuzz->next(&src, &dst, &usePaint);
1420 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001421 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001422 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001423 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001424 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1425 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001426 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1427 break;
1428 }
1429 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001430 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001431 SkIRect src;
1432 SkRect dst;
1433 bool usePaint;
1434 fuzz->next(&src, &dst, &usePaint);
1435 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001436 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001437 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001438 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001439 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1440 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001441 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1442 break;
1443 }
1444 case 36: {
1445 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001446 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001447 SkRect dst;
1448 fuzz->next(&dst, &usePaint);
1449 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001450 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001451 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001452 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001453 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1454 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001455 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1456 break;
1457 }
1458 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001459 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001460 SkIRect center;
1461 SkRect dst;
1462 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001463 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001464 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001465 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001466 }
Hal Canary0361d492017-03-15 12:58:15 -04001467 if (make_fuzz_t<bool>(fuzz)) {
1468 fuzz->next(&center);
1469 } else { // Make valid center, see SkLatticeIter::Valid().
1470 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1471 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1472 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1473 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1474 }
1475 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001476 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1477 break;
1478 }
1479 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001480 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001481 SkScalar left, top;
1482 bool usePaint;
1483 fuzz->next(&left, &top, &usePaint);
1484 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001485 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001486 }
1487 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1488 break;
1489 }
1490 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001491 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001492 SkRect src, dst;
1493 bool usePaint;
1494 fuzz->next(&src, &dst, &usePaint);
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 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001498 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001499 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1500 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001501 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1502 break;
1503 }
1504 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001505 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001506 SkIRect src;
1507 SkRect dst;
1508 bool usePaint;
1509 fuzz->next(&src, &dst, &usePaint);
1510 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001511 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001512 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001513 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001514 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1515 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001516 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1517 break;
1518 }
1519 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001520 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001521 SkRect dst;
1522 bool usePaint;
1523 fuzz->next(&dst, &usePaint);
1524 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001525 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001526 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001527 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001528 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1529 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001530 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1531 break;
1532 }
1533 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001534 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001535 SkIRect center;
1536 SkRect dst;
1537 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001538 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001539 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001540 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001541 }
Hal Canary0361d492017-03-15 12:58:15 -04001542 if (make_fuzz_t<bool>(fuzz)) {
1543 fuzz->next(&center);
1544 } else { // Make valid center, see SkLatticeIter::Valid().
1545 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1546 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1547 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1548 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1549 }
1550 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001551 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1552 break;
1553 }
1554 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001555 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001556 bool usePaint;
1557 SkRect dst;
1558 fuzz->next(&usePaint, &dst);
1559 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001560 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001561 }
1562 constexpr int kMax = 6;
1563 int xDivs[kMax], yDivs[kMax];
1564 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr};
1565 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1566 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1567 fuzz->nextN(xDivs, lattice.fXCount);
1568 fuzz->nextN(yDivs, lattice.fYCount);
1569 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1570 break;
1571 }
1572 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001573 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001574 bool usePaint;
1575 SkRect dst;
1576 fuzz->next(&usePaint, &dst);
1577 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001578 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001579 }
1580 constexpr int kMax = 6;
1581 int xDivs[kMax], yDivs[kMax];
1582 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr};
1583 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1584 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1585 fuzz->nextN(xDivs, lattice.fXCount);
1586 fuzz->nextN(yDivs, lattice.fYCount);
1587 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1588 break;
1589 }
1590 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001591 fuzz_paint(fuzz, &paint, depth - 1);
1592 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001593 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001594 SkScalar x, y;
1595 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001596 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001597 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001598 break;
1599 }
1600 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001601 fuzz_paint(fuzz, &paint, depth - 1);
1602 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001603 fuzz_paint_text_encoding(fuzz, &paint);
1604 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001605 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001606 if (glyphCount < 1) {
1607 break;
1608 }
1609 SkAutoTMalloc<SkPoint> pos(glyphCount);
1610 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001611 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001612 pos[0] = {0, 0};
1613 for (int i = 1; i < glyphCount; ++i) {
1614 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001615 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001616 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1617 }
Hal Canary671e4422017-02-27 13:36:38 -05001618 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001619 break;
1620 }
1621 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001622 fuzz_paint(fuzz, &paint, depth - 1);
1623 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001624 fuzz_paint_text_encoding(fuzz, &paint);
1625 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001626 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001627 SkAutoTMalloc<SkScalar> widths(glyphCount);
1628 if (glyphCount < 1) {
1629 break;
1630 }
Hal Canary671e4422017-02-27 13:36:38 -05001631 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001632 SkScalar x = widths[0];
1633 for (int i = 0; i < glyphCount; ++i) {
1634 SkTSwap(x, widths[i]);
1635 x += widths[i];
1636 SkScalar offset;
1637 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001638 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001639 widths[i] += offset;
1640 }
1641 SkScalar y;
1642 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001643 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001644 break;
1645 }
1646 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001647 fuzz_paint(fuzz, &paint, depth - 1);
1648 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001649 fuzz_paint_text_encoding(fuzz, &paint);
1650 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001651 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001652 fuzz_path(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001653 SkScalar hOffset, vOffset;
1654 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001655 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1656 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001657 break;
1658 }
1659 case 49: {
1660 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001661 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001662 if (useMatrix) {
1663 fuzz->next(&matrix);
1664 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001665 fuzz_paint(fuzz, &paint, depth - 1);
1666 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001667 fuzz_paint_text_encoding(fuzz, &paint);
1668 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001669 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001670 fuzz_path(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001671 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001672 useMatrix ? &matrix : nullptr, paint);
1673 break;
1674 }
1675 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001676 fuzz_paint(fuzz, &paint, depth - 1);
1677 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001678 fuzz_paint_text_encoding(fuzz, &paint);
1679 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1680 SkRSXform rSXform[kMaxGlyphCount];
1681 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1682 SkASSERT(glyphCount <= kMaxGlyphCount);
1683 fuzz->nextN(rSXform, glyphCount);
1684 SkRect cullRect;
1685 bool useCullRect;
1686 fuzz->next(&useCullRect);
1687 if (useCullRect) {
1688 fuzz->next(&cullRect);
1689 }
1690 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1691 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001692 break;
1693 }
1694 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001695 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001696 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001697 SkScalar x, y;
1698 fuzz->next(&x, &y);
1699 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001700 break;
1701 }
1702 case 52: {
1703 bool usePaint, useMatrix;
1704 fuzz->next(&usePaint, &useMatrix);
1705 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001706 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001707 }
1708 if (useMatrix) {
1709 fuzz->next(&matrix);
1710 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001711 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001712 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1713 usePaint ? &paint : nullptr);
1714 break;
1715 }
1716 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001717 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001718 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001719 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001720 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001721 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001722 constexpr int kMaxCount = 100;
1723 int vertexCount;
1724 SkPoint vertices[kMaxCount];
1725 SkPoint texs[kMaxCount];
1726 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001727 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1728 fuzz->nextN(vertices, vertexCount);
1729 bool useTexs, useColors;
1730 fuzz->next(&useTexs, &useColors);
1731 if (useTexs) {
1732 fuzz->nextN(texs, vertexCount);
1733 }
1734 if (useColors) {
1735 fuzz->nextN(colors, vertexCount);
1736 }
1737 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001738 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001739 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001740 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1741 for (int i = 0; i < indexCount; ++i) {
1742 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1743 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001744 }
Mike Reed887cdf12017-04-03 11:11:09 -04001745 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1746 useTexs ? texs : nullptr,
1747 useColors ? colors : nullptr,
1748 indexCount, indices),
1749 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001750 break;
1751 }
1752 default:
1753 break;
1754 }
1755 }
1756}
1757
Hal Canary1e0138b2017-03-10 13:56:08 -05001758static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001759 SkScalar w, h;
1760 fuzz->next(&w, &h);
1761 SkPictureRecorder pictureRecorder;
1762 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1763 return pictureRecorder.finishRecordingAsPicture();
1764}
1765
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001766DEF_FUZZ(NullCanvas, fuzz) {
1767 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001768}
1769
Hal Canary44801ca2017-03-15 11:39:06 -04001770// 8.5x11 letter paper at 72ppi.
1771constexpr SkISize kCanvasSize = {612, 792};
1772
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001773DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001774 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001775 SkASSERT(surface && surface->getCanvas());
1776 fuzz_canvas(fuzz, surface->getCanvas());
1777}
1778
Hal Canary44801ca2017-03-15 11:39:06 -04001779#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001780static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1781 SkASSERT(context);
1782 auto surface = SkSurface::MakeRenderTarget(
1783 context,
1784 SkBudgeted::kNo,
1785 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1786 SkASSERT(surface && surface->getCanvas());
1787 fuzz_canvas(fuzz, surface->getCanvas());
1788}
1789
Hal Canary44801ca2017-03-15 11:39:06 -04001790DEF_FUZZ(NativeGLCanvas, fuzz) {
Brian Salomon6405e712017-03-20 08:54:16 -04001791 GrContext* context = sk_gpu_test::GrContextFactory().get(
1792 sk_gpu_test::GrContextFactory::kGL_ContextType);
1793 if (!context) {
1794 context = sk_gpu_test::GrContextFactory().get(
1795 sk_gpu_test::GrContextFactory::kGLES_ContextType);
1796 }
Hal Canary5aa91582017-03-21 11:11:44 -07001797 fuzz_ganesh(fuzz, context);
1798}
1799
1800DEF_FUZZ(NullGLCanvas, fuzz) {
1801 fuzz_ganesh(fuzz, sk_gpu_test::GrContextFactory().get(
1802 sk_gpu_test::GrContextFactory::kNullGL_ContextType));
1803}
1804
1805DEF_FUZZ(DebugGLCanvas, fuzz) {
1806 fuzz_ganesh(fuzz, sk_gpu_test::GrContextFactory().get(
1807 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) {
1812 struct final : public SkWStream {
1813 bool write(const void*, size_t n) override { fN += n; return true; }
1814 size_t bytesWritten() const override { return fN; }
1815 size_t fN = 0;
1816 } stream;
1817 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001818 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1819 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001820}
1821
1822// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1823DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001824 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001825 fuzz_canvas(fuzz, &debugCanvas);
1826 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1827 UrlDataManager dataManager(SkString("data"));
1828 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1829 Json::StyledStreamWriter(" ").write(std::cout, json);
1830}