blob: 38281ae97c8e171b5c85c873a54cd7a04bacabfc [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 Canarye03c3e52017-03-09 11:33:35 -050031#include "SkArithmeticImageFilter.h"
Robert Phillips70e3e9a2017-06-26 14:22:01 -040032#include "SkBlurImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050033#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050034#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050035#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050036#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050037#include "SkCornerPathEffect.h"
38#include "SkDashPathEffect.h"
39#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050040#include "SkDisplacementMapEffect.h"
41#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050042#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050043#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050044#include "SkImageSource.h"
45#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050046#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050047#include "SkMagnifierImageFilter.h"
48#include "SkMatrixConvolutionImageFilter.h"
49#include "SkMergeImageFilter.h"
50#include "SkMorphologyImageFilter.h"
51#include "SkOffsetImageFilter.h"
52#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050053#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050054#include "SkPictureImageFilter.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050055#include "SkReadBuffer.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;
Mike Reed40e7e652017-07-22 22:12:59 -0400499 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500500 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);
Hal Canary5395c592017-03-08 16:52:18 -0500539 return SkCornerPathEffect::Make(radius);
540 }
Mike Reed40e7e652017-07-22 22:12:59 -0400541 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500542 SkScalar phase;
543 fuzz->next(&phase);
544 SkScalar intervals[20];
545 int count;
546 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
547 fuzz->nextN(intervals, count);
548 return SkDashPathEffect::Make(intervals, count, phase);
549 }
Mike Reed40e7e652017-07-22 22:12:59 -0400550 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500551 SkScalar segLength, dev;
552 uint32_t seed;
553 fuzz->next(&segLength, &dev, &seed);
554 return SkDiscretePathEffect::Make(segLength, dev, seed);
555 }
556 default:
557 SkASSERT(false);
558 return nullptr;
559 }
560}
Hal Canary24ac42b2017-02-14 13:35:14 -0500561
Hal Canary1e0138b2017-03-10 13:56:08 -0500562static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500563 int maskfilterType;
564 fuzz->nextRange(&maskfilterType, 0, 2);
565 switch (maskfilterType) {
566 case 0:
567 return nullptr;
568 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500569 SkBlurStyle blurStyle;
570 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500571 SkScalar sigma;
572 fuzz->next(&sigma);
573 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Hal Canary1e0138b2017-03-10 13:56:08 -0500574 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -0500575 fuzz->next(&occluder);
576 }
577 uint32_t flags;
578 fuzz->nextRange(&flags, 0, 3);
Hal Canaryf7005202017-03-10 08:48:28 -0500579 return SkBlurMaskFilter::Make(blurStyle, sigma, occluder, flags);
Hal Canary5395c592017-03-08 16:52:18 -0500580 }
581 case 2: {
582 SkRRect first, second;
583 SkScalar radius;
584 fuzz->next(&first, &second, &radius);
585 return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
586 }
587 default:
588 SkASSERT(false);
589 return nullptr;
590 }
591}
Hal Canary24ac42b2017-02-14 13:35:14 -0500592
Hal Canary1e0138b2017-03-10 13:56:08 -0500593static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
594 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500595 return nullptr;
596 }
597 auto fontMugger = SkFontMgr::RefDefault();
598 SkASSERT(fontMugger);
599 int familyCount = fontMugger->countFamilies();
600 int i, j;
601 fuzz->nextRange(&i, 0, familyCount - 1);
602 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
603 int styleCount = family->count();
604 fuzz->nextRange(&j, 0, styleCount - 1);
605 return sk_sp<SkTypeface>(family->createTypeface(j));
606}
Hal Canary24ac42b2017-02-14 13:35:14 -0500607
Hal Canarye03c3e52017-03-09 11:33:35 -0500608template <>
609inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
610 SkRect rect;
611 uint8_t flags;
612 this->next(&rect);
613 this->nextRange(&flags, 0, 0xF);
614 *cropRect = SkImageFilter::CropRect(rect, flags);
615}
616
Hal Canary1e0138b2017-03-10 13:56:08 -0500617static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500618
619static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
620 if (depth <= 0) {
621 return nullptr;
622 }
623 uint8_t imageFilterType;
624 fuzz->nextRange(&imageFilterType, 1, 6);
625 SkPoint3 p, q;
626 SkColor lightColor;
627 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
628 sk_sp<SkImageFilter> input;
629 SkImageFilter::CropRect cropRect;
630 bool useCropRect;
631 fuzz->next(&useCropRect);
632 if (useCropRect) {
633 fuzz->next(&cropRect);
634 }
635 switch (imageFilterType) {
636 case 1:
637 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500638 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500639 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
640 std::move(input),
641 useCropRect ? &cropRect : nullptr);
642 case 2:
643 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500644 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500645 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
646 std::move(input),
647 useCropRect ? &cropRect : nullptr);
648 case 3:
649 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500650 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500651 return SkLightingImageFilter::MakeSpotLitDiffuse(
652 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
653 std::move(input), useCropRect ? &cropRect : nullptr);
654 case 4:
655 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500656 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500657 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
658 shininess, std::move(input),
659 useCropRect ? &cropRect : nullptr);
660 case 5:
661 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500662 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500663 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
664 shininess, std::move(input),
665 useCropRect ? &cropRect : nullptr);
666 case 6:
667 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
668 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500669 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500670 return SkLightingImageFilter::MakeSpotLitSpecular(
671 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
672 std::move(input), useCropRect ? &cropRect : nullptr);
673 default:
674 SkASSERT(false);
675 return nullptr;
676 }
677}
678
Hal Canary1e0138b2017-03-10 13:56:08 -0500679static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500680
Hal Canary1e0138b2017-03-10 13:56:08 -0500681static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500682 if (depth <= 0) {
683 return nullptr;
684 }
685 uint8_t imageFilterType;
686 fuzz->nextRange(&imageFilterType, 0, 24);
687 switch (imageFilterType) {
688 case 0:
689 return nullptr;
690 case 1: {
691 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500692 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500693 bool useCropRect;
694 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400695 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500696 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400697 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500698 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400699 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500700 useCropRect ? &cropRect : nullptr);
701 }
702 case 2: {
703 SkMatrix matrix;
704 SkFilterQuality quality;
705 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500706 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500707 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
708 }
709 case 3: {
710 SkRegion region;
711 SkScalar innerMin, outerMax;
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 bool useCropRect;
714 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400715 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500716 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400717 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500718 }
719 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
720 useCropRect ? &cropRect : nullptr);
721 }
722 case 4: {
723 float k1, k2, k3, k4;
724 bool enforcePMColor;
725 bool useCropRect;
726 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500727 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
728 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500729 SkImageFilter::CropRect cropRect;
730 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400731 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500732 }
733 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
734 std::move(background), std::move(foreground),
735 useCropRect ? &cropRect : nullptr);
736 }
737 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500738 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
739 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500740 bool useCropRect;
741 SkImageFilter::CropRect cropRect;
742 fuzz->next(&useCropRect);
743 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400744 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500745 }
746 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
747 useCropRect ? &cropRect : nullptr);
748 }
749 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500750 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
751 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500752 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
753 }
754 case 7: {
755 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400756 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
757 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500758 SkScalar scale;
759 bool useCropRect;
760 fuzz->next(&scale, &useCropRect);
761 SkImageFilter::CropRect cropRect;
762 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400763 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500764 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500765 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
766 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500767 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
768 std::move(displacement), std::move(color),
769 useCropRect ? &cropRect : nullptr);
770 }
771 case 8: {
772 SkScalar dx, dy, sigmaX, sigmaY;
773 SkColor color;
774 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500775 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500776 bool useCropRect;
777 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
778 SkImageFilter::CropRect cropRect;
779 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400780 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500781 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500782 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500783 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
784 std::move(input),
785 useCropRect ? &cropRect : nullptr);
786 }
787 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500788 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500789 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500790 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500791 SkRect srcRect, dstRect;
792 SkFilterQuality filterQuality;
793 fuzz->next(&srcRect, &dstRect, &filterQuality);
794 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
795 }
796 case 11:
797 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
798 case 12: {
799 SkRect srcRect;
800 SkScalar inset;
801 bool useCropRect;
802 SkImageFilter::CropRect cropRect;
803 fuzz->next(&srcRect, &inset, &useCropRect);
804 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400805 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500806 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500807 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500808 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
809 useCropRect ? &cropRect : nullptr);
810 }
811 case 13: {
812 constexpr int kMaxKernelSize = 5;
813 int32_t n, m;
814 fuzz->nextRange(&n, 1, kMaxKernelSize);
815 fuzz->nextRange(&m, 1, kMaxKernelSize);
816 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
817 fuzz->nextN(kernel, n * m);
818 int32_t offsetX, offsetY;
819 fuzz->nextRange(&offsetX, 0, n - 1);
820 fuzz->nextRange(&offsetY, 0, m - 1);
821 SkScalar gain, bias;
822 bool convolveAlpha, useCropRect;
823 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
824 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500825 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500826 SkImageFilter::CropRect cropRect;
827 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400828 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500829 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500830 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500831 return SkMatrixConvolutionImageFilter::Make(
832 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
833 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
834 }
835 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500836 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
837 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500838 bool useCropRect;
839 fuzz->next(&useCropRect);
840 SkImageFilter::CropRect cropRect;
841 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400842 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500843 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400844 return SkMergeImageFilter::Make(std::move(first), std::move(second),
845 useCropRect ? &cropRect : nullptr);
846 }
847 case 15: {
848 constexpr int kMaxCount = 4;
849 sk_sp<SkImageFilter> ifs[kMaxCount];
850 int count;
851 fuzz->nextRange(&count, 1, kMaxCount);
852 for (int i = 0; i < count; ++i) {
853 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
854 }
855 bool useCropRect;
856 fuzz->next(&useCropRect);
857 SkImageFilter::CropRect cropRect;
858 if (useCropRect) {
859 fuzz->next(&cropRect);
860 }
861 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500862 }
863 case 16: {
864 int rx, ry;
865 fuzz->next(&rx, &ry);
866 bool useCropRect;
867 fuzz->next(&useCropRect);
868 SkImageFilter::CropRect cropRect;
869 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400870 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500871 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500872 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500873 return SkDilateImageFilter::Make(rx, ry, std::move(input),
874 useCropRect ? &cropRect : nullptr);
875 }
876 case 17: {
877 int rx, ry;
878 fuzz->next(&rx, &ry);
879 bool useCropRect;
880 fuzz->next(&useCropRect);
881 SkImageFilter::CropRect cropRect;
882 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400883 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500884 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500885 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500886 return SkErodeImageFilter::Make(rx, ry, std::move(input),
887 useCropRect ? &cropRect : nullptr);
888 }
889 case 18: {
890 SkScalar dx, dy;
891 fuzz->next(&dx, &dy);
892 bool useCropRect;
893 fuzz->next(&useCropRect);
894 SkImageFilter::CropRect cropRect;
895 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400896 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500897 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500898 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500899 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
900 useCropRect ? &cropRect : nullptr);
901 }
902 case 19: {
903 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500904 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500905 bool useCropRect;
906 fuzz->next(&useCropRect);
907 SkImageFilter::CropRect cropRect;
908 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400909 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500910 }
911 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
912 }
913 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500914 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500915 return SkPictureImageFilter::Make(std::move(picture));
916 }
917 case 21: {
918 SkRect cropRect;
919 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500920 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500921 return SkPictureImageFilter::Make(std::move(picture), cropRect);
922 }
923 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500924 SkRect src, dst;
925 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500926 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500927 return SkTileImageFilter::Make(src, dst, std::move(input));
928 }
Mike Reed77e487d2017-11-09 21:50:20 +0000929 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500930 SkBlendMode blendMode;
931 bool useCropRect;
932 fuzz->next(&useCropRect, &blendMode);
933 SkImageFilter::CropRect cropRect;
934 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400935 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500936 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500937 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
938 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500939 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
940 useCropRect ? &cropRect : nullptr);
941 }
942 default:
943 SkASSERT(false);
944 return nullptr;
945 }
946}
Hal Canary24ac42b2017-02-14 13:35:14 -0500947
Hal Canary1e0138b2017-03-10 13:56:08 -0500948static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500949 int w, h;
950 fuzz->nextRange(&w, 1, 1024);
951 fuzz->nextRange(&h, 1, 1024);
952 SkAutoTMalloc<SkPMColor> data(w * h);
953 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
954 int n = w * h;
955 for (int i = 0; i < n; ++i) {
956 SkColor c;
957 fuzz->next(&c);
958 data[i] = SkPreMultiplyColor(c);
959 }
960 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500961 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
962 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500963}
964
Hal Canary1e0138b2017-03-10 13:56:08 -0500965static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500966 SkBitmap bitmap;
967 int w, h;
968 fuzz->nextRange(&w, 1, 1024);
969 fuzz->nextRange(&h, 1, 1024);
970 bitmap.allocN32Pixels(w, h);
Hal Canary24ac42b2017-02-14 13:35:14 -0500971 for (int y = 0; y < h; ++y) {
972 for (int x = 0; x < w; ++x) {
973 SkColor c;
974 fuzz->next(&c);
975 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
976 }
977 }
978 return bitmap;
979}
980
Hal Canary1e0138b2017-03-10 13:56:08 -0500981template <typename T, typename Min, typename Max>
982inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
983 T value;
984 fuzz_enum_range(fuzz, &value, minv, maxv);
985 return value;
986}
987
988static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500989 if (!fuzz || !paint || depth <= 0) {
990 return;
991 }
992
Hal Canary1e0138b2017-03-10 13:56:08 -0500993 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
994 paint->setDither( make_fuzz_t<bool>(fuzz));
995 paint->setColor( make_fuzz_t<SkColor>(fuzz));
996 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
997 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
998 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
999 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
1000 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
1001 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
1002 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
1003 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -05001004
1005 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001006 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
1007 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
1008 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
1009 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -05001010 }
1011}
1012
Hal Canary1e0138b2017-03-10 13:56:08 -05001013static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
1014 paint->setTypeface( make_fuzz_typeface(fuzz));
1015 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
1016 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
1017 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
1018 paint->setLinearText( make_fuzz_t<bool>(fuzz));
1019 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
1020 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
1021 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
1022 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
1023 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
1024 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
1025 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
1026 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
1027 SkPaint::kFull_Hinting));
1028 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -05001029}
1030
1031static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001032 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -05001033}
1034
Hal Canary5395c592017-03-08 16:52:18 -05001035constexpr int kMaxGlyphCount = 30;
1036
Hal Canary1e0138b2017-03-10 13:56:08 -05001037static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -05001038 SkTDArray<uint8_t> array;
1039 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001040 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
1041 : SkTypeface::MakeDefault()->countGlyphs();
Hal Canary671e4422017-02-27 13:36:38 -05001042 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -05001043 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001044 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
1045 for (int i = 0; i < glyphCount; ++i) {
1046 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
1047 }
1048 return array;
1049 }
1050 static const SkUnichar ranges[][2] = {
1051 {0x0020, 0x007F},
1052 {0x00A1, 0x0250},
1053 {0x0400, 0x0500},
1054 };
1055 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001056 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001057 count += (ranges[i][1] - ranges[i][0]);
1058 }
Hal Canary5395c592017-03-08 16:52:18 -05001059 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -05001060 SkUnichar buffer[kMaxLength];
1061 int length;
1062 fuzz->nextRange(&length, 1, kMaxLength);
1063 for (int j = 0; j < length; ++j) {
1064 int32_t value;
1065 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001066 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001067 if (value + ranges[i][0] < ranges[i][1]) {
1068 buffer[j] = value + ranges[i][0];
1069 break;
1070 } else {
1071 value -= (ranges[i][1] - ranges[i][0]);
1072 }
1073 }
1074 }
1075 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001076 case SkPaint::kUTF8_TextEncoding: {
1077 size_t utf8len = 0;
1078 for (int j = 0; j < length; ++j) {
1079 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001080 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001081 char* ptr = (char*)array.append(utf8len);
1082 for (int j = 0; j < length; ++j) {
1083 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001084 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001085 } break;
1086 case SkPaint::kUTF16_TextEncoding: {
1087 size_t utf16len = 0;
1088 for (int j = 0; j < length; ++j) {
1089 utf16len += SkUTF16_FromUnichar(buffer[j]);
1090 }
1091 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1092 for (int j = 0; j < length; ++j) {
1093 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1094 }
1095 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001096 case SkPaint::kUTF32_TextEncoding:
1097 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001098 break;
Hal Canary671e4422017-02-27 13:36:38 -05001099 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001100 SkASSERT(false);
Hal Canary671e4422017-02-27 13:36:38 -05001101 }
1102 return array;
1103}
1104
Hal Canary5395c592017-03-08 16:52:18 -05001105static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1106 SkTextBlobBuilder textBlobBuilder;
1107 int8_t runCount;
1108 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1109 while (runCount-- > 0) {
1110 SkPaint paint;
1111 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001112 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001113 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1114 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1115 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1116 SkASSERT(glyphCount <= kMaxGlyphCount);
1117 SkScalar x, y;
1118 const SkTextBlobBuilder::RunBuffer* buffer;
1119 uint8_t runType;
1120 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1121 switch (runType) {
1122 case 0:
1123 fuzz->next(&x, &y);
1124 // TODO: Test other variations of this.
1125 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1126 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1127 break;
1128 case 1:
1129 fuzz->next(&y);
1130 // TODO: Test other variations of this.
1131 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1132 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1133 fuzz->nextN(buffer->pos, glyphCount);
1134 break;
1135 case 2:
1136 // TODO: Test other variations of this.
1137 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1138 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1139 fuzz->nextN(buffer->pos, glyphCount * 2);
1140 break;
1141 default:
1142 SkASSERT(false);
1143 }
1144 }
1145 return textBlobBuilder.make();
1146}
1147
Hal Canary1e0138b2017-03-10 13:56:08 -05001148static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001149 if (!fuzz || !canvas || depth <= 0) {
1150 return;
1151 }
1152 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1153 unsigned N;
1154 fuzz->nextRange(&N, 0, 2000);
1155 for (unsigned i = 0; i < N; ++i) {
1156 if (fuzz->exhausted()) {
1157 return;
1158 }
1159 SkPaint paint;
1160 SkMatrix matrix;
1161 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001162 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001163 switch (drawCommand) {
1164 case 0:
1165 canvas->flush();
1166 break;
1167 case 1:
1168 canvas->save();
1169 break;
1170 case 2: {
1171 SkRect bounds;
1172 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001173 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001174 canvas->saveLayer(&bounds, &paint);
1175 break;
1176 }
1177 case 3: {
1178 SkRect bounds;
1179 fuzz->next(&bounds);
1180 canvas->saveLayer(&bounds, nullptr);
1181 break;
1182 }
1183 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001184 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001185 canvas->saveLayer(nullptr, &paint);
1186 break;
1187 case 5:
1188 canvas->saveLayer(nullptr, nullptr);
1189 break;
1190 case 6: {
1191 uint8_t alpha;
1192 fuzz->next(&alpha);
1193 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1194 break;
1195 }
1196 case 7: {
1197 SkRect bounds;
1198 uint8_t alpha;
1199 fuzz->next(&bounds, &alpha);
1200 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1201 break;
1202 }
1203 case 8: {
1204 SkCanvas::SaveLayerRec saveLayerRec;
1205 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001206 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001207 fuzz->next(&bounds);
1208 saveLayerRec.fBounds = &bounds;
1209 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001210 if (make_fuzz_t<bool>(fuzz)) {
1211 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001212 saveLayerRec.fPaint = &paint;
1213 }
1214 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001215 if (make_fuzz_t<bool>(fuzz)) {
1216 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001217 saveLayerRec.fBackdrop = imageFilter.get();
1218 }
Hal Canary5395c592017-03-08 16:52:18 -05001219 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001220 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001221 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kIsOpaque_SaveLayerFlag;
1222 // }
Hal Canary1e0138b2017-03-10 13:56:08 -05001223 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001224 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1225 // }
1226
Hal Canary24ac42b2017-02-14 13:35:14 -05001227 canvas->saveLayer(saveLayerRec);
1228 break;
1229 }
1230 case 9:
1231 canvas->restore();
1232 break;
1233 case 10: {
1234 int saveCount;
1235 fuzz->next(&saveCount);
1236 canvas->restoreToCount(saveCount);
1237 break;
1238 }
1239 case 11: {
1240 SkScalar x, y;
1241 fuzz->next(&x, &y);
1242 canvas->translate(x, y);
1243 break;
1244 }
1245 case 12: {
1246 SkScalar x, y;
1247 fuzz->next(&x, &y);
1248 canvas->scale(x, y);
1249 break;
1250 }
1251 case 13: {
1252 SkScalar v;
1253 fuzz->next(&v);
1254 canvas->rotate(v);
1255 break;
1256 }
1257 case 14: {
1258 SkScalar x, y, v;
1259 fuzz->next(&x, &y, &v);
1260 canvas->rotate(v, x, y);
1261 break;
1262 }
1263 case 15: {
1264 SkScalar x, y;
1265 fuzz->next(&x, &y);
1266 canvas->skew(x, y);
1267 break;
1268 }
1269 case 16: {
1270 SkMatrix mat;
1271 fuzz->next(&mat);
1272 canvas->concat(mat);
1273 break;
1274 }
1275 case 17: {
1276 SkMatrix mat;
1277 fuzz->next(&mat);
1278 canvas->setMatrix(mat);
1279 break;
1280 }
1281 case 18:
1282 canvas->resetMatrix();
1283 break;
1284 case 19: {
1285 SkRect r;
1286 int op;
1287 bool doAntiAlias;
1288 fuzz->next(&r, &doAntiAlias);
1289 fuzz->nextRange(&op, 0, 1);
1290 r.sort();
1291 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1292 break;
1293 }
1294 case 20: {
1295 SkRRect rr;
1296 int op;
1297 bool doAntiAlias;
1298 fuzz->next(&rr);
1299 fuzz->next(&doAntiAlias);
1300 fuzz->nextRange(&op, 0, 1);
1301 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1302 break;
1303 }
1304 case 21: {
1305 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001306 fuzz_path(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001307 int op;
1308 bool doAntiAlias;
1309 fuzz->next(&doAntiAlias);
1310 fuzz->nextRange(&op, 0, 1);
1311 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1312 break;
1313 }
1314 case 22: {
1315 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001316 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001317 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001318 fuzz->nextRange(&op, 0, 1);
1319 canvas->clipRegion(region, (SkClipOp)op);
1320 break;
1321 }
1322 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001323 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001324 canvas->drawPaint(paint);
1325 break;
1326 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001327 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001328 SkCanvas::PointMode pointMode;
1329 fuzz_enum_range(fuzz, &pointMode,
1330 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001331 size_t count;
1332 constexpr int kMaxCount = 30;
1333 fuzz->nextRange(&count, 0, kMaxCount);
1334 SkPoint pts[kMaxCount];
1335 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001336 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001337 break;
1338 }
1339 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001340 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001341 SkRect r;
1342 fuzz->next(&r);
1343 canvas->drawRect(r, paint);
1344 break;
1345 }
1346 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001347 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001348 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001349 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001350 canvas->drawRegion(region, paint);
1351 break;
1352 }
1353 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001354 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001355 SkRect r;
1356 fuzz->next(&r);
1357 canvas->drawOval(r, paint);
1358 break;
1359 }
1360 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 SkRRect rr;
1363 fuzz->next(&rr);
1364 canvas->drawRRect(rr, paint);
1365 break;
1366 }
1367 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001368 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001369 SkRRect orr, irr;
1370 fuzz->next(&orr);
1371 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001372 if (orr.getBounds().contains(irr.getBounds())) {
1373 canvas->drawDRRect(orr, irr, paint);
1374 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001375 break;
1376 }
1377 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001378 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001379 SkRect r;
1380 SkScalar start, sweep;
1381 bool useCenter;
1382 fuzz->next(&r, &start, &sweep, &useCenter);
1383 canvas->drawArc(r, start, sweep, useCenter, paint);
1384 break;
1385 }
1386 case 32: {
1387 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001388 fuzz_path(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001389 canvas->drawPath(path, paint);
1390 break;
1391 }
1392 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001393 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001394 SkScalar left, top;
1395 bool usePaint;
1396 fuzz->next(&left, &top, &usePaint);
1397 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001398 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001399 }
1400 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1401 break;
1402 }
1403 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001404 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001405 SkRect src, dst;
1406 bool usePaint;
1407 fuzz->next(&src, &dst, &usePaint);
1408 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001409 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001410 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001411 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001412 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1413 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001414 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1415 break;
1416 }
1417 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001418 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001419 SkIRect src;
1420 SkRect dst;
1421 bool usePaint;
1422 fuzz->next(&src, &dst, &usePaint);
1423 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001424 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001425 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001426 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001427 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1428 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001429 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1430 break;
1431 }
1432 case 36: {
1433 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001434 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001435 SkRect dst;
1436 fuzz->next(&dst, &usePaint);
1437 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001438 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001439 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001440 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001441 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1442 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001443 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1444 break;
1445 }
1446 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001447 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001448 SkIRect center;
1449 SkRect dst;
1450 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001451 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001452 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001453 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001454 }
Hal Canary0361d492017-03-15 12:58:15 -04001455 if (make_fuzz_t<bool>(fuzz)) {
1456 fuzz->next(&center);
1457 } else { // Make valid center, see SkLatticeIter::Valid().
1458 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1459 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1460 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1461 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1462 }
1463 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001464 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1465 break;
1466 }
1467 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001468 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001469 SkScalar left, top;
1470 bool usePaint;
1471 fuzz->next(&left, &top, &usePaint);
1472 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001473 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001474 }
1475 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1476 break;
1477 }
1478 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001479 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001480 SkRect src, dst;
1481 bool usePaint;
1482 fuzz->next(&src, &dst, &usePaint);
1483 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001484 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001485 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001486 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001487 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1488 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001489 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1490 break;
1491 }
1492 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001493 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001494 SkIRect src;
1495 SkRect dst;
1496 bool usePaint;
1497 fuzz->next(&src, &dst, &usePaint);
1498 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001499 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001500 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001501 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001502 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1503 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001504 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1505 break;
1506 }
1507 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001508 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001509 SkRect dst;
1510 bool usePaint;
1511 fuzz->next(&dst, &usePaint);
1512 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001513 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001514 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001515 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001516 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1517 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001518 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1519 break;
1520 }
1521 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001522 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001523 SkIRect center;
1524 SkRect dst;
1525 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001526 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001527 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001528 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001529 }
Hal Canary0361d492017-03-15 12:58:15 -04001530 if (make_fuzz_t<bool>(fuzz)) {
1531 fuzz->next(&center);
1532 } else { // Make valid center, see SkLatticeIter::Valid().
1533 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1534 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1535 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1536 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1537 }
1538 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001539 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1540 break;
1541 }
1542 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001543 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001544 bool usePaint;
1545 SkRect dst;
1546 fuzz->next(&usePaint, &dst);
1547 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001548 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001549 }
1550 constexpr int kMax = 6;
1551 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001552 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001553 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1554 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1555 fuzz->nextN(xDivs, lattice.fXCount);
1556 fuzz->nextN(yDivs, lattice.fYCount);
1557 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1558 break;
1559 }
1560 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001561 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001562 bool usePaint;
1563 SkRect dst;
1564 fuzz->next(&usePaint, &dst);
1565 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001566 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001567 }
1568 constexpr int kMax = 6;
1569 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001570 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001571 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1572 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1573 fuzz->nextN(xDivs, lattice.fXCount);
1574 fuzz->nextN(yDivs, lattice.fYCount);
1575 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1576 break;
1577 }
1578 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001579 fuzz_paint(fuzz, &paint, depth - 1);
1580 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001581 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001582 SkScalar x, y;
1583 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001584 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001585 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001586 break;
1587 }
1588 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001589 fuzz_paint(fuzz, &paint, depth - 1);
1590 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001591 fuzz_paint_text_encoding(fuzz, &paint);
1592 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001593 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001594 if (glyphCount < 1) {
1595 break;
1596 }
1597 SkAutoTMalloc<SkPoint> pos(glyphCount);
1598 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001599 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001600 pos[0] = {0, 0};
1601 for (int i = 1; i < glyphCount; ++i) {
1602 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001603 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001604 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1605 }
Hal Canary671e4422017-02-27 13:36:38 -05001606 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001607 break;
1608 }
1609 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001610 fuzz_paint(fuzz, &paint, depth - 1);
1611 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001612 fuzz_paint_text_encoding(fuzz, &paint);
1613 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001614 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001615 SkAutoTMalloc<SkScalar> widths(glyphCount);
1616 if (glyphCount < 1) {
1617 break;
1618 }
Hal Canary671e4422017-02-27 13:36:38 -05001619 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001620 SkScalar x = widths[0];
1621 for (int i = 0; i < glyphCount; ++i) {
1622 SkTSwap(x, widths[i]);
1623 x += widths[i];
1624 SkScalar offset;
1625 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001626 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001627 widths[i] += offset;
1628 }
1629 SkScalar y;
1630 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001631 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001632 break;
1633 }
1634 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001635 fuzz_paint(fuzz, &paint, depth - 1);
1636 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001637 fuzz_paint_text_encoding(fuzz, &paint);
1638 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001639 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001640 fuzz_path(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001641 SkScalar hOffset, vOffset;
1642 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001643 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1644 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001645 break;
1646 }
1647 case 49: {
1648 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001649 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001650 if (useMatrix) {
1651 fuzz->next(&matrix);
1652 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001653 fuzz_paint(fuzz, &paint, depth - 1);
1654 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001655 fuzz_paint_text_encoding(fuzz, &paint);
1656 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001657 SkPath path;
Hal Canaryce540ea2017-03-06 08:30:44 -05001658 fuzz_path(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001659 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001660 useMatrix ? &matrix : nullptr, paint);
1661 break;
1662 }
1663 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001664 fuzz_paint(fuzz, &paint, depth - 1);
1665 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001666 fuzz_paint_text_encoding(fuzz, &paint);
1667 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1668 SkRSXform rSXform[kMaxGlyphCount];
1669 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1670 SkASSERT(glyphCount <= kMaxGlyphCount);
1671 fuzz->nextN(rSXform, glyphCount);
1672 SkRect cullRect;
1673 bool useCullRect;
1674 fuzz->next(&useCullRect);
1675 if (useCullRect) {
1676 fuzz->next(&cullRect);
1677 }
1678 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1679 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001680 break;
1681 }
1682 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001683 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001684 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001685 SkScalar x, y;
1686 fuzz->next(&x, &y);
1687 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001688 break;
1689 }
1690 case 52: {
1691 bool usePaint, useMatrix;
1692 fuzz->next(&usePaint, &useMatrix);
1693 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001694 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001695 }
1696 if (useMatrix) {
1697 fuzz->next(&matrix);
1698 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001699 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001700 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1701 usePaint ? &paint : nullptr);
1702 break;
1703 }
1704 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001705 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001706 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001707 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001708 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001709 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001710 constexpr int kMaxCount = 100;
1711 int vertexCount;
1712 SkPoint vertices[kMaxCount];
1713 SkPoint texs[kMaxCount];
1714 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001715 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1716 fuzz->nextN(vertices, vertexCount);
1717 bool useTexs, useColors;
1718 fuzz->next(&useTexs, &useColors);
1719 if (useTexs) {
1720 fuzz->nextN(texs, vertexCount);
1721 }
1722 if (useColors) {
1723 fuzz->nextN(colors, vertexCount);
1724 }
1725 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001726 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001727 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001728 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1729 for (int i = 0; i < indexCount; ++i) {
1730 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1731 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001732 }
Mike Reed887cdf12017-04-03 11:11:09 -04001733 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1734 useTexs ? texs : nullptr,
1735 useColors ? colors : nullptr,
1736 indexCount, indices),
1737 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001738 break;
1739 }
1740 default:
1741 break;
1742 }
1743 }
1744}
1745
Hal Canary1e0138b2017-03-10 13:56:08 -05001746static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001747 SkScalar w, h;
1748 fuzz->next(&w, &h);
1749 SkPictureRecorder pictureRecorder;
1750 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1751 return pictureRecorder.finishRecordingAsPicture();
1752}
1753
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001754DEF_FUZZ(NullCanvas, fuzz) {
1755 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001756}
1757
Hal Canary44801ca2017-03-15 11:39:06 -04001758// 8.5x11 letter paper at 72ppi.
1759constexpr SkISize kCanvasSize = {612, 792};
1760
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001761DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001762 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001763 SkASSERT(surface && surface->getCanvas());
1764 fuzz_canvas(fuzz, surface->getCanvas());
1765}
1766
Hal Canarye2924432017-12-01 11:46:26 -05001767DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1768 SkPictureRecorder recorder;
1769 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1770 SkIntToScalar(kCanvasSize.height())));
1771 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1772 if (!pic) { fuzz->signalBug(); }
1773 sk_sp<SkData> data = pic->serialize();
1774 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001775 SkReadBuffer rb(data->data(), data->size());
1776 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001777 if (!deserialized) { fuzz->signalBug(); }
1778 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1779 SkASSERT(surface && surface->getCanvas());
1780 surface->getCanvas()->drawPicture(deserialized);
1781}
1782
Hal Canary44801ca2017-03-15 11:39:06 -04001783#if SK_SUPPORT_GPU
Hal Canary5aa91582017-03-21 11:11:44 -07001784static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1785 SkASSERT(context);
1786 auto surface = SkSurface::MakeRenderTarget(
1787 context,
1788 SkBudgeted::kNo,
1789 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1790 SkASSERT(surface && surface->getCanvas());
1791 fuzz_canvas(fuzz, surface->getCanvas());
1792}
1793
Hal Canary44801ca2017-03-15 11:39:06 -04001794DEF_FUZZ(NativeGLCanvas, fuzz) {
Brian Salomon6405e712017-03-20 08:54:16 -04001795 GrContext* context = sk_gpu_test::GrContextFactory().get(
1796 sk_gpu_test::GrContextFactory::kGL_ContextType);
1797 if (!context) {
1798 context = sk_gpu_test::GrContextFactory().get(
1799 sk_gpu_test::GrContextFactory::kGLES_ContextType);
1800 }
Hal Canary5aa91582017-03-21 11:11:44 -07001801 fuzz_ganesh(fuzz, context);
1802}
1803
1804DEF_FUZZ(NullGLCanvas, fuzz) {
1805 fuzz_ganesh(fuzz, sk_gpu_test::GrContextFactory().get(
1806 sk_gpu_test::GrContextFactory::kNullGL_ContextType));
1807}
1808
1809DEF_FUZZ(DebugGLCanvas, fuzz) {
1810 fuzz_ganesh(fuzz, sk_gpu_test::GrContextFactory().get(
1811 sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001812}
1813#endif
1814
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001815DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001816 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001817 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001818 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1819 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001820}
1821
1822// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1823DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001824 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001825 fuzz_canvas(fuzz, &debugCanvas);
1826 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1827 UrlDataManager dataManager(SkString("data"));
1828 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1829 Json::StyledStreamWriter(" ").write(std::cout, json);
1830}