blob: 7ca14d2223bd43e03e278c59453af14f063a8c28 [file] [log] [blame]
Hal Canary24ac42b2017-02-14 13:35:14 -05001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Fuzz.h"
Kevin Lubick2541edf2018-01-11 10:27:14 -05009#include "FuzzCommon.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050010
11// CORE
12#include "SkCanvas.h"
13#include "SkColorFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050014#include "SkDebugCanvas.h"
15#include "SkDocument.h"
Hal Canary671e4422017-02-27 13:36:38 -050016#include "SkFontMgr.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050017#include "SkImageFilter.h"
18#include "SkMaskFilter.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050019#include "SkNullCanvas.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050020#include "SkPathEffect.h"
21#include "SkPictureRecorder.h"
Mike Reed54518ac2017-07-22 08:29:48 -040022#include "SkPoint3.h"
Hal Canary5395c592017-03-08 16:52:18 -050023#include "SkRSXform.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050024#include "SkRegion.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050025#include "SkSurface.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050026#include "SkTypeface.h"
Kevin Lubickedef8ec2018-01-09 15:32:58 -050027#include "SkOSFile.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050028
29// EFFECTS
Hal Canary5395c592017-03-08 16:52:18 -050030#include "Sk1DPathEffect.h"
31#include "Sk2DPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050032#include "SkAlphaThresholdFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050033#include "SkArithmeticImageFilter.h"
Robert Phillips70e3e9a2017-06-26 14:22:01 -040034#include "SkBlurImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050035#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050036#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050037#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050038#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050039#include "SkCornerPathEffect.h"
40#include "SkDashPathEffect.h"
41#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050042#include "SkDisplacementMapEffect.h"
43#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050044#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050045#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050046#include "SkImageSource.h"
47#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050048#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050049#include "SkMagnifierImageFilter.h"
50#include "SkMatrixConvolutionImageFilter.h"
51#include "SkMergeImageFilter.h"
52#include "SkMorphologyImageFilter.h"
53#include "SkOffsetImageFilter.h"
54#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050055#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050056#include "SkPictureImageFilter.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050057#include "SkReadBuffer.h"
Hal Canary27bece82017-03-07 16:23:20 -050058#include "SkTableColorFilter.h"
Mike Reedc090c642017-05-16 10:39:06 -040059#include "SkTextBlob.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050060#include "SkTileImageFilter.h"
61#include "SkXfermodeImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050062
63// SRC
Kevin Lubickfaef5142018-06-07 10:33:11 -040064#include "SkCommandLineFlags.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050065#include "SkUtils.h"
66
Hal Canary44801ca2017-03-15 11:39:06 -040067#if SK_SUPPORT_GPU
68#include "GrContextFactory.h"
Kevin Lubickfaef5142018-06-07 10:33:11 -040069#include "GrContextPriv.h"
70#include "gl/GrGLFunctions.h"
71#include "gl/GrGLGpu.h"
72#include "gl/GrGLUtil.h"
Hal Canary44801ca2017-03-15 11:39:06 -040073#endif
74
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050075// MISC
76
77#include <iostream>
78
Kevin Lubickfaef5142018-06-07 10:33:11 -040079DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets.");
80
Hal Canary24ac42b2017-02-14 13:35:14 -050081// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050082// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040083// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050084
Hal Canary1e0138b2017-03-10 13:56:08 -050085// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
86// In fact, all make_fuzz_foo() functions have this potential problem.
87// Use sequence points!
88template <typename T>
89inline T make_fuzz_t(Fuzz* fuzz) {
90 T t;
91 fuzz->next(&t);
92 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050093}
94
Hal Canaryb69c4b82017-03-08 11:02:40 -050095template <>
96inline void Fuzz::next(SkShader::TileMode* m) {
Hal Canaryf7005202017-03-10 08:48:28 -050097 fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -050098}
99
Hal Canaryb69c4b82017-03-08 11:02:40 -0500100template <>
Hal Canarye03c3e52017-03-09 11:33:35 -0500101inline void Fuzz::next(SkFilterQuality* q) {
Hal Canaryf7005202017-03-10 08:48:28 -0500102 fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
103 SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500104}
105
106template <>
Hal Canaryb69c4b82017-03-08 11:02:40 -0500107inline void Fuzz::next(SkMatrix* m) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500108 constexpr int kArrayLength = 9;
109 SkScalar buffer[kArrayLength];
110 int matrixType;
111 this->nextRange(&matrixType, 0, 4);
112 switch (matrixType) {
113 case 0: // identity
114 *m = SkMatrix::I();
115 return;
116 case 1: // translate
117 this->nextRange(&buffer[0], -4000.0f, 4000.0f);
118 this->nextRange(&buffer[1], -4000.0f, 4000.0f);
119 *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
120 return;
121 case 2: // translate + scale
122 this->nextRange(&buffer[0], -400.0f, 400.0f);
123 this->nextRange(&buffer[1], -400.0f, 400.0f);
124 this->nextRange(&buffer[2], -4000.0f, 4000.0f);
125 this->nextRange(&buffer[3], -4000.0f, 4000.0f);
126 *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
127 m->postTranslate(buffer[2], buffer[3]);
128 return;
129 case 3: // affine
130 this->nextN(buffer, 6);
131 m->setAffine(buffer);
132 return;
133 case 4: // perspective
134 this->nextN(buffer, kArrayLength);
135 m->set9(buffer);
136 return;
137 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500138 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500139 return;
140 }
141}
142
Hal Canaryb69c4b82017-03-08 11:02:40 -0500143template <>
144inline void Fuzz::next(SkRRect* rr) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500145 SkRect r;
146 SkVector radii[4];
147 this->next(&r);
Hal Canary27bece82017-03-07 16:23:20 -0500148 r.sort();
149 for (SkVector& vec : radii) {
150 this->nextRange(&vec.fX, 0.0f, 1.0f);
151 vec.fX *= 0.5f * r.width();
152 this->nextRange(&vec.fY, 0.0f, 1.0f);
153 vec.fY *= 0.5f * r.height();
154 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500155 rr->setRectRadii(r, radii);
Hal Canary24ac42b2017-02-14 13:35:14 -0500156}
157
Hal Canaryb69c4b82017-03-08 11:02:40 -0500158template <>
159inline void Fuzz::next(SkBlendMode* mode) {
Hal Canaryf7005202017-03-10 08:48:28 -0500160 fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500161}
162
Hal Canary1e0138b2017-03-10 13:56:08 -0500163static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -0500164
Hal Canary1e0138b2017-03-10 13:56:08 -0500165static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500166
Hal Canary1e0138b2017-03-10 13:56:08 -0500167static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500168
Hal Canary1e0138b2017-03-10 13:56:08 -0500169static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500170 if (depth <= 0) {
171 return nullptr;
172 }
173 int colorFilterType;
174 fuzz->nextRange(&colorFilterType, 0, 8);
175 switch (colorFilterType) {
176 case 0:
177 return nullptr;
178 case 1: {
179 SkColor color;
180 SkBlendMode mode;
181 fuzz->next(&color, &mode);
182 return SkColorFilter::MakeModeFilter(color, mode);
183 }
184 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500185 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500186 if (!outer) {
187 return nullptr;
188 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500189 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500190 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500191 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500192 }
193 case 3: {
194 SkScalar array[20];
195 fuzz->nextN(array, SK_ARRAY_COUNT(array));
196 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
197 }
198 case 4: {
199 SkColor mul, add;
200 fuzz->next(&mul, &add);
201 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
202 }
203 case 5: {
204 bool grayscale;
205 int invertStyle;
206 float contrast;
207 fuzz->next(&grayscale);
208 fuzz->nextRange(&invertStyle, 0, 2);
209 fuzz->nextRange(&contrast, -1.0f, 1.0f);
210 return SkHighContrastFilter::Make(SkHighContrastConfig(
211 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
212 }
213 case 6:
214 return SkLumaColorFilter::Make();
215 case 7: {
216 uint8_t table[256];
217 fuzz->nextN(table, SK_ARRAY_COUNT(table));
218 return SkTableColorFilter::Make(table);
219 }
220 case 8: {
221 uint8_t tableA[256];
222 uint8_t tableR[256];
223 uint8_t tableG[256];
224 uint8_t tableB[256];
225 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
226 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
227 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
228 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
229 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
230 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500231 default:
232 SkASSERT(false);
233 break;
Hal Canary27bece82017-03-07 16:23:20 -0500234 }
235 return nullptr;
236}
Hal Canary24ac42b2017-02-14 13:35:14 -0500237
Hal Canary1e0138b2017-03-10 13:56:08 -0500238static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500239 SkScalar totalPos = 0;
240 for (int i = 0; i < colorCount; ++i) {
241 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
242 totalPos += pos[i];
243 }
244 totalPos = 1.0f / totalPos;
245 for (int i = 0; i < colorCount; ++i) {
246 pos[i] *= totalPos;
247 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500248 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500249 pos[colorCount - 1] = 1.0f;
250}
251
Hal Canary1e0138b2017-03-10 13:56:08 -0500252static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500253 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
254 sk_sp<SkColorFilter> colorFilter(nullptr);
255 SkBitmap bitmap;
256 sk_sp<SkImage> img;
257 SkShader::TileMode tmX, tmY;
258 bool useMatrix;
259 SkColor color;
260 SkMatrix matrix;
261 SkBlendMode blendMode;
262 int shaderType;
263 if (depth <= 0) {
264 return nullptr;
265 }
Hal Canary671e4422017-02-27 13:36:38 -0500266 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500267 switch (shaderType) {
268 case 0:
269 return nullptr;
270 case 1:
271 return SkShader::MakeEmptyShader();
272 case 2:
273 fuzz->next(&color);
274 return SkShader::MakeColorShader(color);
275 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500276 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500277 fuzz->next(&tmX, &tmY, &useMatrix);
278 if (useMatrix) {
279 fuzz->next(&matrix);
280 }
281 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
282 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500283 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500284 fuzz->next(&tmX, &tmY, &useMatrix);
285 if (useMatrix) {
286 fuzz->next(&matrix);
287 }
288 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
289 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500290 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500291 fuzz->next(&matrix);
292 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
293 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500294 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
295 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500296 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
297 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500298 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
299 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500300 fuzz->next(&blendMode);
301 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500302 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500303 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500304 bool useTile;
305 SkRect tile;
306 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
307 if (useMatrix) {
308 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500309 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500310 if (useTile) {
311 fuzz->next(&tile);
312 }
313 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
314 useMatrix ? &matrix : nullptr,
315 useTile ? &tile : nullptr);
316 }
Hal Canary671e4422017-02-27 13:36:38 -0500317 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500318 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000319 // Deprecated SkGaussianEdgeShader
320 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500321 case 10: {
322 constexpr int kMaxColors = 12;
323 SkPoint pts[2];
324 SkColor colors[kMaxColors];
325 SkScalar pos[kMaxColors];
326 int colorCount;
327 bool usePos;
328 fuzz->nextN(pts, 2);
329 fuzz->nextRange(&colorCount, 2, kMaxColors);
330 fuzz->nextN(colors, colorCount);
331 fuzz->next(&tmX, &useMatrix, &usePos);
332 if (useMatrix) {
333 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500334 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500335 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500336 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500337 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500338 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
339 tmX, 0, useMatrix ? &matrix : nullptr);
340 }
341 case 11: {
342 constexpr int kMaxColors = 12;
343 SkPoint center;
344 SkScalar radius;
345 int colorCount;
346 bool usePos;
347 SkColor colors[kMaxColors];
348 SkScalar pos[kMaxColors];
349 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
350 fuzz->nextRange(&colorCount, 2, kMaxColors);
351 fuzz->nextN(colors, colorCount);
352 if (useMatrix) {
353 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500354 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500355 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500356 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500357 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500358 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
359 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
360 }
361 case 12: {
362 constexpr int kMaxColors = 12;
363 SkPoint start, end;
364 SkScalar startRadius, endRadius;
365 int colorCount;
366 bool usePos;
367 SkColor colors[kMaxColors];
368 SkScalar pos[kMaxColors];
369 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
370 fuzz->nextRange(&colorCount, 2, kMaxColors);
371 fuzz->nextN(colors, colorCount);
372 if (useMatrix) {
373 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500374 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500375 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500376 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500377 }
378 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
379 usePos ? pos : nullptr, colorCount, tmX, 0,
380 useMatrix ? &matrix : nullptr);
381 }
382 case 13: {
383 constexpr int kMaxColors = 12;
384 SkScalar cx, cy;
385 int colorCount;
386 bool usePos;
387 SkColor colors[kMaxColors];
388 SkScalar pos[kMaxColors];
389 fuzz->next(&cx, &cy, &useMatrix, &usePos);
390 fuzz->nextRange(&colorCount, 2, kMaxColors);
391 fuzz->nextN(colors, colorCount);
392 if (useMatrix) {
393 fuzz->next(&matrix);
394 }
395 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500396 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500397 }
398 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
399 0, useMatrix ? &matrix : nullptr);
400 }
401 case 14: {
402 SkScalar baseFrequencyX, baseFrequencyY, seed;
403 int numOctaves;
404 SkISize tileSize;
405 bool useTileSize, turbulence;
406 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
407 if (useTileSize) {
408 fuzz->next(&tileSize);
409 }
410 fuzz->nextRange(&numOctaves, 2, 7);
411 if (turbulence) {
412 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
413 numOctaves, seed,
414 useTileSize ? &tileSize : nullptr);
415 } else {
416 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
417 numOctaves, seed,
418 useTileSize ? &tileSize : nullptr);
419 }
420 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500421 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500422 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500423 break;
424 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500425 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500426}
427
Hal Canary1e0138b2017-03-10 13:56:08 -0500428static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500429 if (depth <= 0) {
430 return nullptr;
431 }
432 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400433 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500434 switch (pathEffectType) {
435 case 0: {
436 return nullptr;
437 }
438 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500439 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
440 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500441 return SkPathEffect::MakeSum(std::move(first), std::move(second));
442 }
443 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500444 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
445 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500446 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
447 }
448 case 3: {
449 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -0500450 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500451 SkScalar advance, phase;
452 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500453 SkPath1DPathEffect::Style style;
454 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
455 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500456 }
457 case 4: {
458 SkScalar width;
459 SkMatrix matrix;
460 fuzz->next(&width, &matrix);
461 return SkLine2DPathEffect::Make(width, matrix);
462 }
463 case 5: {
464 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -0500465 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500466 SkMatrix matrix;
467 fuzz->next(&matrix);
468 return SkPath2DPathEffect::Make(matrix, path);
469 }
470 case 6: {
471 SkScalar radius;
472 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500473 return SkCornerPathEffect::Make(radius);
474 }
Mike Reed40e7e652017-07-22 22:12:59 -0400475 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500476 SkScalar phase;
477 fuzz->next(&phase);
478 SkScalar intervals[20];
479 int count;
480 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
481 fuzz->nextN(intervals, count);
482 return SkDashPathEffect::Make(intervals, count, phase);
483 }
Mike Reed40e7e652017-07-22 22:12:59 -0400484 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500485 SkScalar segLength, dev;
486 uint32_t seed;
487 fuzz->next(&segLength, &dev, &seed);
488 return SkDiscretePathEffect::Make(segLength, dev, seed);
489 }
490 default:
491 SkASSERT(false);
492 return nullptr;
493 }
494}
Hal Canary24ac42b2017-02-14 13:35:14 -0500495
Hal Canary1e0138b2017-03-10 13:56:08 -0500496static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500497 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400498 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500499 switch (maskfilterType) {
500 case 0:
501 return nullptr;
502 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500503 SkBlurStyle blurStyle;
504 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500505 SkScalar sigma;
506 fuzz->next(&sigma);
507 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400508 bool useOccluder;
509 fuzz->next(&useOccluder);
510 if (useOccluder) {
Hal Canary5395c592017-03-08 16:52:18 -0500511 fuzz->next(&occluder);
512 }
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400513 bool respectCTM;
514 fuzz->next(&respectCTM);
515 if (useOccluder) {
516 return SkMaskFilter::MakeBlur(blurStyle, sigma, occluder, respectCTM);
517 }
518 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500519 }
Hal Canary5395c592017-03-08 16:52:18 -0500520 default:
521 SkASSERT(false);
522 return nullptr;
523 }
524}
Hal Canary24ac42b2017-02-14 13:35:14 -0500525
Hal Canary1e0138b2017-03-10 13:56:08 -0500526static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
527 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500528 return nullptr;
529 }
530 auto fontMugger = SkFontMgr::RefDefault();
531 SkASSERT(fontMugger);
532 int familyCount = fontMugger->countFamilies();
533 int i, j;
534 fuzz->nextRange(&i, 0, familyCount - 1);
535 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
536 int styleCount = family->count();
537 fuzz->nextRange(&j, 0, styleCount - 1);
538 return sk_sp<SkTypeface>(family->createTypeface(j));
539}
Hal Canary24ac42b2017-02-14 13:35:14 -0500540
Hal Canarye03c3e52017-03-09 11:33:35 -0500541template <>
542inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
543 SkRect rect;
544 uint8_t flags;
545 this->next(&rect);
546 this->nextRange(&flags, 0, 0xF);
547 *cropRect = SkImageFilter::CropRect(rect, flags);
548}
549
Hal Canary1e0138b2017-03-10 13:56:08 -0500550static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500551
552static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
553 if (depth <= 0) {
554 return nullptr;
555 }
556 uint8_t imageFilterType;
557 fuzz->nextRange(&imageFilterType, 1, 6);
558 SkPoint3 p, q;
559 SkColor lightColor;
560 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
561 sk_sp<SkImageFilter> input;
562 SkImageFilter::CropRect cropRect;
563 bool useCropRect;
564 fuzz->next(&useCropRect);
565 if (useCropRect) {
566 fuzz->next(&cropRect);
567 }
568 switch (imageFilterType) {
569 case 1:
570 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500571 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500572 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
573 std::move(input),
574 useCropRect ? &cropRect : nullptr);
575 case 2:
576 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500577 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500578 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
579 std::move(input),
580 useCropRect ? &cropRect : nullptr);
581 case 3:
582 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500583 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500584 return SkLightingImageFilter::MakeSpotLitDiffuse(
585 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
586 std::move(input), useCropRect ? &cropRect : nullptr);
587 case 4:
588 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500589 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500590 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
591 shininess, std::move(input),
592 useCropRect ? &cropRect : nullptr);
593 case 5:
594 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500595 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500596 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
597 shininess, std::move(input),
598 useCropRect ? &cropRect : nullptr);
599 case 6:
600 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
601 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500602 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500603 return SkLightingImageFilter::MakeSpotLitSpecular(
604 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
605 std::move(input), useCropRect ? &cropRect : nullptr);
606 default:
607 SkASSERT(false);
608 return nullptr;
609 }
610}
611
Hal Canary1e0138b2017-03-10 13:56:08 -0500612static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500613
Hal Canary1e0138b2017-03-10 13:56:08 -0500614static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500615 if (depth <= 0) {
616 return nullptr;
617 }
618 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500619 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500620 switch (imageFilterType) {
621 case 0:
622 return nullptr;
623 case 1: {
624 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500625 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500626 bool useCropRect;
627 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400628 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500629 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400630 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500631 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400632 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500633 useCropRect ? &cropRect : nullptr);
634 }
635 case 2: {
636 SkMatrix matrix;
637 SkFilterQuality quality;
638 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500639 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500640 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
641 }
642 case 3: {
643 SkRegion region;
644 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500645 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500646 bool useCropRect;
647 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400648 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500649 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400650 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500651 }
652 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
653 useCropRect ? &cropRect : nullptr);
654 }
655 case 4: {
656 float k1, k2, k3, k4;
657 bool enforcePMColor;
658 bool useCropRect;
659 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500660 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
661 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500662 SkImageFilter::CropRect cropRect;
663 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400664 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500665 }
666 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
667 std::move(background), std::move(foreground),
668 useCropRect ? &cropRect : nullptr);
669 }
670 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500671 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
672 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500673 bool useCropRect;
674 SkImageFilter::CropRect cropRect;
675 fuzz->next(&useCropRect);
676 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400677 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500678 }
679 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
680 useCropRect ? &cropRect : nullptr);
681 }
682 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500683 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
684 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500685 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
686 }
687 case 7: {
688 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400689 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
690 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500691 SkScalar scale;
692 bool useCropRect;
693 fuzz->next(&scale, &useCropRect);
694 SkImageFilter::CropRect cropRect;
695 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400696 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500697 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500698 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
699 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500700 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
701 std::move(displacement), std::move(color),
702 useCropRect ? &cropRect : nullptr);
703 }
704 case 8: {
705 SkScalar dx, dy, sigmaX, sigmaY;
706 SkColor color;
707 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500708 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500709 bool useCropRect;
710 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
711 SkImageFilter::CropRect cropRect;
712 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400713 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500714 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500715 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500716 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
717 std::move(input),
718 useCropRect ? &cropRect : nullptr);
719 }
720 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500721 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500722 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500723 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500724 SkRect srcRect, dstRect;
725 SkFilterQuality filterQuality;
726 fuzz->next(&srcRect, &dstRect, &filterQuality);
727 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
728 }
729 case 11:
730 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
731 case 12: {
732 SkRect srcRect;
733 SkScalar inset;
734 bool useCropRect;
735 SkImageFilter::CropRect cropRect;
736 fuzz->next(&srcRect, &inset, &useCropRect);
737 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400738 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500739 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500740 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500741 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
742 useCropRect ? &cropRect : nullptr);
743 }
744 case 13: {
745 constexpr int kMaxKernelSize = 5;
746 int32_t n, m;
747 fuzz->nextRange(&n, 1, kMaxKernelSize);
748 fuzz->nextRange(&m, 1, kMaxKernelSize);
749 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
750 fuzz->nextN(kernel, n * m);
751 int32_t offsetX, offsetY;
752 fuzz->nextRange(&offsetX, 0, n - 1);
753 fuzz->nextRange(&offsetY, 0, m - 1);
754 SkScalar gain, bias;
755 bool convolveAlpha, useCropRect;
756 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
757 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500758 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500759 SkImageFilter::CropRect cropRect;
760 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400761 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500762 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500763 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500764 return SkMatrixConvolutionImageFilter::Make(
765 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
766 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
767 }
768 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500769 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
770 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500771 bool useCropRect;
772 fuzz->next(&useCropRect);
773 SkImageFilter::CropRect cropRect;
774 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400775 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500776 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400777 return SkMergeImageFilter::Make(std::move(first), std::move(second),
778 useCropRect ? &cropRect : nullptr);
779 }
780 case 15: {
781 constexpr int kMaxCount = 4;
782 sk_sp<SkImageFilter> ifs[kMaxCount];
783 int count;
784 fuzz->nextRange(&count, 1, kMaxCount);
785 for (int i = 0; i < count; ++i) {
786 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
787 }
788 bool useCropRect;
789 fuzz->next(&useCropRect);
790 SkImageFilter::CropRect cropRect;
791 if (useCropRect) {
792 fuzz->next(&cropRect);
793 }
794 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500795 }
796 case 16: {
797 int rx, ry;
798 fuzz->next(&rx, &ry);
799 bool useCropRect;
800 fuzz->next(&useCropRect);
801 SkImageFilter::CropRect cropRect;
802 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400803 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500804 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500805 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500806 return SkDilateImageFilter::Make(rx, ry, std::move(input),
807 useCropRect ? &cropRect : nullptr);
808 }
809 case 17: {
810 int rx, ry;
811 fuzz->next(&rx, &ry);
812 bool useCropRect;
813 fuzz->next(&useCropRect);
814 SkImageFilter::CropRect cropRect;
815 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400816 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500817 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500818 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500819 return SkErodeImageFilter::Make(rx, ry, std::move(input),
820 useCropRect ? &cropRect : nullptr);
821 }
822 case 18: {
823 SkScalar dx, dy;
824 fuzz->next(&dx, &dy);
825 bool useCropRect;
826 fuzz->next(&useCropRect);
827 SkImageFilter::CropRect cropRect;
828 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400829 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500830 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500831 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500832 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
833 useCropRect ? &cropRect : nullptr);
834 }
835 case 19: {
836 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500837 fuzz_paint(fuzz, &paint, 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 }
844 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
845 }
846 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500847 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500848 return SkPictureImageFilter::Make(std::move(picture));
849 }
850 case 21: {
851 SkRect cropRect;
852 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500853 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500854 return SkPictureImageFilter::Make(std::move(picture), cropRect);
855 }
856 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500857 SkRect src, dst;
858 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500859 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500860 return SkTileImageFilter::Make(src, dst, std::move(input));
861 }
Mike Reed77e487d2017-11-09 21:50:20 +0000862 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500863 SkBlendMode blendMode;
864 bool useCropRect;
865 fuzz->next(&useCropRect, &blendMode);
866 SkImageFilter::CropRect cropRect;
867 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400868 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500869 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500870 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
871 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500872 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
873 useCropRect ? &cropRect : nullptr);
874 }
875 default:
876 SkASSERT(false);
877 return nullptr;
878 }
879}
Hal Canary24ac42b2017-02-14 13:35:14 -0500880
Hal Canary1e0138b2017-03-10 13:56:08 -0500881static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500882 int w, h;
883 fuzz->nextRange(&w, 1, 1024);
884 fuzz->nextRange(&h, 1, 1024);
885 SkAutoTMalloc<SkPMColor> data(w * h);
886 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
887 int n = w * h;
888 for (int i = 0; i < n; ++i) {
889 SkColor c;
890 fuzz->next(&c);
891 data[i] = SkPreMultiplyColor(c);
892 }
893 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500894 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
895 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500896}
897
Hal Canary1e0138b2017-03-10 13:56:08 -0500898static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500899 SkBitmap bitmap;
900 int w, h;
901 fuzz->nextRange(&w, 1, 1024);
902 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500903 if (!bitmap.tryAllocN32Pixels(w, h)) {
904 SkDEBUGF(("Could not allocate pixels %d x %d", w, h));
905 return bitmap;
906 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500907 for (int y = 0; y < h; ++y) {
908 for (int x = 0; x < w; ++x) {
909 SkColor c;
910 fuzz->next(&c);
911 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
912 }
913 }
914 return bitmap;
915}
916
Hal Canary1e0138b2017-03-10 13:56:08 -0500917template <typename T, typename Min, typename Max>
918inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
919 T value;
920 fuzz_enum_range(fuzz, &value, minv, maxv);
921 return value;
922}
923
924static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500925 if (!fuzz || !paint || depth <= 0) {
926 return;
927 }
928
Hal Canary1e0138b2017-03-10 13:56:08 -0500929 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
930 paint->setDither( make_fuzz_t<bool>(fuzz));
931 paint->setColor( make_fuzz_t<SkColor>(fuzz));
932 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
933 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
934 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
935 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
936 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
937 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
938 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
939 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500940
941 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500942 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
943 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
944 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
945 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500946 }
947}
948
Hal Canary1e0138b2017-03-10 13:56:08 -0500949static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
950 paint->setTypeface( make_fuzz_typeface(fuzz));
951 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
952 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
953 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
954 paint->setLinearText( make_fuzz_t<bool>(fuzz));
955 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
956 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
957 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
958 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
959 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
960 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
961 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
962 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
963 SkPaint::kFull_Hinting));
964 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -0500965}
966
967static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500968 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -0500969}
970
Hal Canary5395c592017-03-08 16:52:18 -0500971constexpr int kMaxGlyphCount = 30;
972
Hal Canary1e0138b2017-03-10 13:56:08 -0500973static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -0500974 SkTDArray<uint8_t> array;
975 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500976 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
977 : SkTypeface::MakeDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500978 if (glyphRange == 0) {
979 // Some fuzzing environments have no fonts, so empty array is the best
980 // we can do.
981 return array;
982 }
Hal Canary671e4422017-02-27 13:36:38 -0500983 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500984 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500985 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
986 for (int i = 0; i < glyphCount; ++i) {
987 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
988 }
989 return array;
990 }
991 static const SkUnichar ranges[][2] = {
992 {0x0020, 0x007F},
993 {0x00A1, 0x0250},
994 {0x0400, 0x0500},
995 };
996 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500997 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500998 count += (ranges[i][1] - ranges[i][0]);
999 }
Hal Canary5395c592017-03-08 16:52:18 -05001000 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -05001001 SkUnichar buffer[kMaxLength];
1002 int length;
1003 fuzz->nextRange(&length, 1, kMaxLength);
1004 for (int j = 0; j < length; ++j) {
1005 int32_t value;
1006 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001007 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001008 if (value + ranges[i][0] < ranges[i][1]) {
1009 buffer[j] = value + ranges[i][0];
1010 break;
1011 } else {
1012 value -= (ranges[i][1] - ranges[i][0]);
1013 }
1014 }
1015 }
1016 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001017 case SkPaint::kUTF8_TextEncoding: {
1018 size_t utf8len = 0;
1019 for (int j = 0; j < length; ++j) {
1020 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001021 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001022 char* ptr = (char*)array.append(utf8len);
1023 for (int j = 0; j < length; ++j) {
1024 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001025 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001026 } break;
1027 case SkPaint::kUTF16_TextEncoding: {
1028 size_t utf16len = 0;
1029 for (int j = 0; j < length; ++j) {
1030 utf16len += SkUTF16_FromUnichar(buffer[j]);
1031 }
1032 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1033 for (int j = 0; j < length; ++j) {
1034 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1035 }
1036 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001037 case SkPaint::kUTF32_TextEncoding:
1038 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001039 break;
Hal Canary671e4422017-02-27 13:36:38 -05001040 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001041 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001042 break;
Hal Canary671e4422017-02-27 13:36:38 -05001043 }
1044 return array;
1045}
1046
Hal Canary5395c592017-03-08 16:52:18 -05001047static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1048 SkTextBlobBuilder textBlobBuilder;
1049 int8_t runCount;
1050 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1051 while (runCount-- > 0) {
1052 SkPaint paint;
1053 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001054 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001055 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1056 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1057 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1058 SkASSERT(glyphCount <= kMaxGlyphCount);
1059 SkScalar x, y;
1060 const SkTextBlobBuilder::RunBuffer* buffer;
1061 uint8_t runType;
1062 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1063 switch (runType) {
1064 case 0:
1065 fuzz->next(&x, &y);
1066 // TODO: Test other variations of this.
1067 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1068 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1069 break;
1070 case 1:
1071 fuzz->next(&y);
1072 // TODO: Test other variations of this.
1073 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1074 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1075 fuzz->nextN(buffer->pos, glyphCount);
1076 break;
1077 case 2:
1078 // TODO: Test other variations of this.
1079 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1080 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1081 fuzz->nextN(buffer->pos, glyphCount * 2);
1082 break;
1083 default:
1084 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001085 break;
Hal Canary5395c592017-03-08 16:52:18 -05001086 }
1087 }
1088 return textBlobBuilder.make();
1089}
1090
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001091extern std::atomic<bool> gSkUseDeltaAA;
1092extern std::atomic<bool> gSkForceDeltaAA;
1093
Hal Canary1e0138b2017-03-10 13:56:08 -05001094static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001095 if (!fuzz || !canvas || depth <= 0) {
1096 return;
1097 }
1098 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001099 bool useDAA;
1100 fuzz->next(&useDAA);
1101 if (useDAA) {
1102 gSkForceDeltaAA = true;
1103 gSkUseDeltaAA = true;
1104 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001105 unsigned N;
1106 fuzz->nextRange(&N, 0, 2000);
1107 for (unsigned i = 0; i < N; ++i) {
1108 if (fuzz->exhausted()) {
1109 return;
1110 }
1111 SkPaint paint;
1112 SkMatrix matrix;
1113 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001114 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001115 switch (drawCommand) {
1116 case 0:
1117 canvas->flush();
1118 break;
1119 case 1:
1120 canvas->save();
1121 break;
1122 case 2: {
1123 SkRect bounds;
1124 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001125 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001126 canvas->saveLayer(&bounds, &paint);
1127 break;
1128 }
1129 case 3: {
1130 SkRect bounds;
1131 fuzz->next(&bounds);
1132 canvas->saveLayer(&bounds, nullptr);
1133 break;
1134 }
1135 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001136 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001137 canvas->saveLayer(nullptr, &paint);
1138 break;
1139 case 5:
1140 canvas->saveLayer(nullptr, nullptr);
1141 break;
1142 case 6: {
1143 uint8_t alpha;
1144 fuzz->next(&alpha);
1145 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1146 break;
1147 }
1148 case 7: {
1149 SkRect bounds;
1150 uint8_t alpha;
1151 fuzz->next(&bounds, &alpha);
1152 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1153 break;
1154 }
1155 case 8: {
1156 SkCanvas::SaveLayerRec saveLayerRec;
1157 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001158 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001159 fuzz->next(&bounds);
1160 saveLayerRec.fBounds = &bounds;
1161 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001162 if (make_fuzz_t<bool>(fuzz)) {
1163 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001164 saveLayerRec.fPaint = &paint;
1165 }
1166 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001167 if (make_fuzz_t<bool>(fuzz)) {
1168 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001169 saveLayerRec.fBackdrop = imageFilter.get();
1170 }
Hal Canary5395c592017-03-08 16:52:18 -05001171 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001172 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001173 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1174 // }
1175
Hal Canary24ac42b2017-02-14 13:35:14 -05001176 canvas->saveLayer(saveLayerRec);
1177 break;
1178 }
1179 case 9:
1180 canvas->restore();
1181 break;
1182 case 10: {
1183 int saveCount;
1184 fuzz->next(&saveCount);
1185 canvas->restoreToCount(saveCount);
1186 break;
1187 }
1188 case 11: {
1189 SkScalar x, y;
1190 fuzz->next(&x, &y);
1191 canvas->translate(x, y);
1192 break;
1193 }
1194 case 12: {
1195 SkScalar x, y;
1196 fuzz->next(&x, &y);
1197 canvas->scale(x, y);
1198 break;
1199 }
1200 case 13: {
1201 SkScalar v;
1202 fuzz->next(&v);
1203 canvas->rotate(v);
1204 break;
1205 }
1206 case 14: {
1207 SkScalar x, y, v;
1208 fuzz->next(&x, &y, &v);
1209 canvas->rotate(v, x, y);
1210 break;
1211 }
1212 case 15: {
1213 SkScalar x, y;
1214 fuzz->next(&x, &y);
1215 canvas->skew(x, y);
1216 break;
1217 }
1218 case 16: {
1219 SkMatrix mat;
1220 fuzz->next(&mat);
1221 canvas->concat(mat);
1222 break;
1223 }
1224 case 17: {
1225 SkMatrix mat;
1226 fuzz->next(&mat);
1227 canvas->setMatrix(mat);
1228 break;
1229 }
1230 case 18:
1231 canvas->resetMatrix();
1232 break;
1233 case 19: {
1234 SkRect r;
1235 int op;
1236 bool doAntiAlias;
1237 fuzz->next(&r, &doAntiAlias);
1238 fuzz->nextRange(&op, 0, 1);
1239 r.sort();
1240 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1241 break;
1242 }
1243 case 20: {
1244 SkRRect rr;
1245 int op;
1246 bool doAntiAlias;
1247 fuzz->next(&rr);
1248 fuzz->next(&doAntiAlias);
1249 fuzz->nextRange(&op, 0, 1);
1250 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1251 break;
1252 }
1253 case 21: {
1254 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001255 FuzzPath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001256 int op;
1257 bool doAntiAlias;
1258 fuzz->next(&doAntiAlias);
1259 fuzz->nextRange(&op, 0, 1);
1260 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1261 break;
1262 }
1263 case 22: {
1264 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001265 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001266 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001267 fuzz->nextRange(&op, 0, 1);
1268 canvas->clipRegion(region, (SkClipOp)op);
1269 break;
1270 }
1271 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001272 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001273 canvas->drawPaint(paint);
1274 break;
1275 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001276 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001277 SkCanvas::PointMode pointMode;
1278 fuzz_enum_range(fuzz, &pointMode,
1279 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001280 size_t count;
1281 constexpr int kMaxCount = 30;
1282 fuzz->nextRange(&count, 0, kMaxCount);
1283 SkPoint pts[kMaxCount];
1284 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001285 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001286 break;
1287 }
1288 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001289 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001290 SkRect r;
1291 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001292 if (!r.isFinite()) {
1293 break;
1294 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001295 canvas->drawRect(r, paint);
1296 break;
1297 }
1298 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001299 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001300 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001301 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001302 canvas->drawRegion(region, paint);
1303 break;
1304 }
1305 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001306 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001307 SkRect r;
1308 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001309 if (!r.isFinite()) {
1310 break;
1311 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001312 canvas->drawOval(r, paint);
1313 break;
1314 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001315 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001316 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001317 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001318 SkRRect rr;
1319 fuzz->next(&rr);
1320 canvas->drawRRect(rr, paint);
1321 break;
1322 }
1323 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001324 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001325 SkRRect orr, irr;
1326 fuzz->next(&orr);
1327 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001328 if (orr.getBounds().contains(irr.getBounds())) {
1329 canvas->drawDRRect(orr, irr, paint);
1330 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001331 break;
1332 }
1333 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001334 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001335 SkRect r;
1336 SkScalar start, sweep;
1337 bool useCenter;
1338 fuzz->next(&r, &start, &sweep, &useCenter);
1339 canvas->drawArc(r, start, sweep, useCenter, paint);
1340 break;
1341 }
1342 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001343 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001344 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001345 FuzzPath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001346 canvas->drawPath(path, paint);
1347 break;
1348 }
1349 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001350 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001351 SkScalar left, top;
1352 bool usePaint;
1353 fuzz->next(&left, &top, &usePaint);
1354 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001356 }
1357 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1358 break;
1359 }
1360 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001361 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001362 SkRect src, dst;
1363 bool usePaint;
1364 fuzz->next(&src, &dst, &usePaint);
1365 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001366 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001367 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001368 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001369 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1370 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001371 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1372 break;
1373 }
1374 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001375 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001376 SkIRect src;
1377 SkRect dst;
1378 bool usePaint;
1379 fuzz->next(&src, &dst, &usePaint);
1380 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001381 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001382 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001383 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001384 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1385 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001386 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1387 break;
1388 }
1389 case 36: {
1390 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001391 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001392 SkRect dst;
1393 fuzz->next(&dst, &usePaint);
1394 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001395 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001396 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001397 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001398 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1399 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001400 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1401 break;
1402 }
1403 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001404 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001405 SkIRect center;
1406 SkRect dst;
1407 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001408 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001409 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001410 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001411 }
Hal Canary0361d492017-03-15 12:58:15 -04001412 if (make_fuzz_t<bool>(fuzz)) {
1413 fuzz->next(&center);
1414 } else { // Make valid center, see SkLatticeIter::Valid().
1415 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1416 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1417 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1418 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1419 }
1420 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001421 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1422 break;
1423 }
1424 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001425 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001426 SkScalar left, top;
1427 bool usePaint;
1428 fuzz->next(&left, &top, &usePaint);
1429 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001430 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001431 }
1432 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1433 break;
1434 }
1435 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001436 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001437 SkRect src, dst;
1438 bool usePaint;
1439 fuzz->next(&src, &dst, &usePaint);
1440 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001441 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001442 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001443 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001444 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1445 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001446 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1447 break;
1448 }
1449 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001450 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001451 SkIRect src;
1452 SkRect dst;
1453 bool usePaint;
1454 fuzz->next(&src, &dst, &usePaint);
1455 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001456 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001457 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001458 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001459 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1460 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001461 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1462 break;
1463 }
1464 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001465 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001466 SkRect dst;
1467 bool usePaint;
1468 fuzz->next(&dst, &usePaint);
1469 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001470 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001471 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001472 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001473 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1474 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001475 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1476 break;
1477 }
1478 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001479 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001480 SkIRect center;
1481 SkRect dst;
1482 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001483 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001484 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001485 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001486 }
Hal Canary0361d492017-03-15 12:58:15 -04001487 if (make_fuzz_t<bool>(fuzz)) {
1488 fuzz->next(&center);
1489 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001490 if (img.width() == 0 || img.height() == 0) {
1491 // bitmap may not have had its pixels initialized.
1492 break;
1493 }
Hal Canary0361d492017-03-15 12:58:15 -04001494 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1495 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1496 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1497 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1498 }
1499 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001500 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1501 break;
1502 }
1503 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001504 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001505 bool usePaint;
1506 SkRect dst;
1507 fuzz->next(&usePaint, &dst);
1508 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001509 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001510 }
1511 constexpr int kMax = 6;
1512 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001513 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001514 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1515 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1516 fuzz->nextN(xDivs, lattice.fXCount);
1517 fuzz->nextN(yDivs, lattice.fYCount);
1518 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1519 break;
1520 }
1521 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001522 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001523 bool usePaint;
1524 SkRect dst;
1525 fuzz->next(&usePaint, &dst);
1526 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001527 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001528 }
1529 constexpr int kMax = 6;
1530 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001531 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001532 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1533 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1534 fuzz->nextN(xDivs, lattice.fXCount);
1535 fuzz->nextN(yDivs, lattice.fYCount);
1536 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1537 break;
1538 }
1539 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001540 fuzz_paint(fuzz, &paint, depth - 1);
1541 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001542 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001543 SkScalar x, y;
1544 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001545 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001546 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001547 break;
1548 }
1549 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001550 fuzz_paint(fuzz, &paint, depth - 1);
1551 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001552 fuzz_paint_text_encoding(fuzz, &paint);
1553 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001554 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001555 if (glyphCount < 1) {
1556 break;
1557 }
1558 SkAutoTMalloc<SkPoint> pos(glyphCount);
1559 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001560 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001561 pos[0] = {0, 0};
1562 for (int i = 1; i < glyphCount; ++i) {
1563 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001564 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001565 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1566 }
Hal Canary671e4422017-02-27 13:36:38 -05001567 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001568 break;
1569 }
1570 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001571 fuzz_paint(fuzz, &paint, depth - 1);
1572 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001573 fuzz_paint_text_encoding(fuzz, &paint);
1574 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001575 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001576 SkAutoTMalloc<SkScalar> widths(glyphCount);
1577 if (glyphCount < 1) {
1578 break;
1579 }
Hal Canary671e4422017-02-27 13:36:38 -05001580 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001581 SkScalar x = widths[0];
1582 for (int i = 0; i < glyphCount; ++i) {
1583 SkTSwap(x, widths[i]);
1584 x += widths[i];
1585 SkScalar offset;
1586 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001587 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001588 widths[i] += offset;
1589 }
1590 SkScalar y;
1591 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001592 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001593 break;
1594 }
1595 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001596 fuzz_paint(fuzz, &paint, depth - 1);
1597 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001598 fuzz_paint_text_encoding(fuzz, &paint);
1599 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001600 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001601 FuzzPath(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001602 SkScalar hOffset, vOffset;
1603 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001604 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1605 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001606 break;
1607 }
1608 case 49: {
1609 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001610 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001611 if (useMatrix) {
1612 fuzz->next(&matrix);
1613 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001614 fuzz_paint(fuzz, &paint, depth - 1);
1615 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001616 fuzz_paint_text_encoding(fuzz, &paint);
1617 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001618 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001619 FuzzPath(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001620 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001621 useMatrix ? &matrix : nullptr, paint);
1622 break;
1623 }
1624 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001625 fuzz_paint(fuzz, &paint, depth - 1);
1626 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001627 fuzz_paint_text_encoding(fuzz, &paint);
1628 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1629 SkRSXform rSXform[kMaxGlyphCount];
1630 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1631 SkASSERT(glyphCount <= kMaxGlyphCount);
1632 fuzz->nextN(rSXform, glyphCount);
1633 SkRect cullRect;
1634 bool useCullRect;
1635 fuzz->next(&useCullRect);
1636 if (useCullRect) {
1637 fuzz->next(&cullRect);
1638 }
1639 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1640 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001641 break;
1642 }
1643 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001644 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001645 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001646 SkScalar x, y;
1647 fuzz->next(&x, &y);
1648 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001649 break;
1650 }
1651 case 52: {
1652 bool usePaint, useMatrix;
1653 fuzz->next(&usePaint, &useMatrix);
1654 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001655 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001656 }
1657 if (useMatrix) {
1658 fuzz->next(&matrix);
1659 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001660 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001661 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1662 usePaint ? &paint : nullptr);
1663 break;
1664 }
1665 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001666 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001667 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001668 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001669 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001670 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001671 constexpr int kMaxCount = 100;
1672 int vertexCount;
1673 SkPoint vertices[kMaxCount];
1674 SkPoint texs[kMaxCount];
1675 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001676 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1677 fuzz->nextN(vertices, vertexCount);
1678 bool useTexs, useColors;
1679 fuzz->next(&useTexs, &useColors);
1680 if (useTexs) {
1681 fuzz->nextN(texs, vertexCount);
1682 }
1683 if (useColors) {
1684 fuzz->nextN(colors, vertexCount);
1685 }
1686 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001687 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001688 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001689 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1690 for (int i = 0; i < indexCount; ++i) {
1691 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1692 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001693 }
Mike Reed887cdf12017-04-03 11:11:09 -04001694 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1695 useTexs ? texs : nullptr,
1696 useColors ? colors : nullptr,
1697 indexCount, indices),
1698 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001699 break;
1700 }
1701 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001702 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001703 break;
1704 }
1705 }
1706}
1707
Hal Canary1e0138b2017-03-10 13:56:08 -05001708static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001709 SkScalar w, h;
1710 fuzz->next(&w, &h);
1711 SkPictureRecorder pictureRecorder;
1712 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1713 return pictureRecorder.finishRecordingAsPicture();
1714}
1715
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001716DEF_FUZZ(NullCanvas, fuzz) {
1717 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001718}
1719
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001720constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001721
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001722DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001723 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001724 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001725 fuzz_canvas(fuzz, surface->getCanvas());
1726}
1727
Hal Canarye2924432017-12-01 11:46:26 -05001728DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1729 SkPictureRecorder recorder;
1730 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1731 SkIntToScalar(kCanvasSize.height())));
1732 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1733 if (!pic) { fuzz->signalBug(); }
1734 sk_sp<SkData> data = pic->serialize();
1735 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001736 SkReadBuffer rb(data->data(), data->size());
1737 auto deserialized = SkPicture::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001738 if (!deserialized) { fuzz->signalBug(); }
1739 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1740 SkASSERT(surface && surface->getCanvas());
1741 surface->getCanvas()->drawPicture(deserialized);
1742}
1743
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001744DEF_FUZZ(ImageFilter, fuzz) {
1745 auto fil = make_fuzz_imageFilter(fuzz, 20);
1746
1747 SkPaint paint;
1748 paint.setImageFilter(fil);
1749 SkBitmap bitmap;
1750 SkCanvas canvas(bitmap);
1751 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1752}
1753
1754
1755//SkRandom _rand;
1756#define SK_ADD_RANDOM_BIT_FLIPS
1757
1758DEF_FUZZ(SerializedImageFilter, fuzz) {
1759 auto filter = make_fuzz_imageFilter(fuzz, 20);
1760 auto data = filter->serialize();
1761 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1762 size_t len = data->size();
1763#ifdef SK_ADD_RANDOM_BIT_FLIPS
1764 unsigned char* p = const_cast<unsigned char*>(ptr);
1765 for (size_t i = 0; i < len; ++i, ++p) {
1766 uint8_t j;
1767 fuzz->nextRange(&j, 1, 250);
1768 if (j == 1) { // 0.4% of the time, flip a bit or byte
1769 uint8_t k;
1770 fuzz->nextRange(&k, 1, 10);
1771 if (k == 1) { // Then 10% of the time, change a whole byte
1772 uint8_t s;
1773 fuzz->nextRange(&s, 0, 2);
1774 switch(s) {
1775 case 0:
1776 *p ^= 0xFF; // Flip entire byte
1777 break;
1778 case 1:
1779 *p = 0xFF; // Set all bits to 1
1780 break;
1781 case 2:
1782 *p = 0x00; // Set all bits to 0
1783 break;
1784 }
1785 } else {
1786 uint8_t s;
1787 fuzz->nextRange(&s, 0, 7);
1788 *p ^= (1 << 7);
1789 }
1790 }
1791 }
1792#endif // SK_ADD_RANDOM_BIT_FLIPS
1793 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1794
1795 // uncomment below to write out a serialized image filter (to make corpus
1796 // for -t filter_fuzz)
1797 // SkString s("./serialized_filters/sf");
1798 // s.appendU32(_rand.nextU());
1799 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1800 // sk_fwrite(data->bytes(), data->size(), file);
1801 // sk_fclose(file);
1802
1803 SkPaint paint;
1804 paint.setImageFilter(deserializedFil);
1805 SkBitmap bitmap;
1806 SkCanvas canvas(bitmap);
1807 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1808}
1809
Hal Canary44801ca2017-03-15 11:39:06 -04001810#if SK_SUPPORT_GPU
Kevin Lubickfaef5142018-06-07 10:33:11 -04001811
1812static void dump_GPU_info(GrContext* context) {
1813 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->contextPriv().getGpu())
1814 ->glInterface();
1815 const GrGLubyte* output;
1816 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1817 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1818
1819 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1820 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1821
1822 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1823 SkDebugf("GL_VERSION %s\n", (const char*) output);
1824}
1825
Hal Canary5aa91582017-03-21 11:11:44 -07001826static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1827 SkASSERT(context);
1828 auto surface = SkSurface::MakeRenderTarget(
1829 context,
1830 SkBudgeted::kNo,
1831 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1832 SkASSERT(surface && surface->getCanvas());
1833 fuzz_canvas(fuzz, surface->getCanvas());
1834}
1835
Hal Canary44801ca2017-03-15 11:39:06 -04001836DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001837 sk_gpu_test::GrContextFactory f;
1838 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001839 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001840 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001841 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001842 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001843 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001844 }
Hal Canary5aa91582017-03-21 11:11:44 -07001845 fuzz_ganesh(fuzz, context);
1846}
1847
Kevin Lubick27d42192018-04-03 12:30:32 -04001848// This target is deprecated, NullGLContext is not well maintained.
1849// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001850DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001851 sk_gpu_test::GrContextFactory f;
1852 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001853}
1854
Kevin Lubick30709262018-04-02 11:06:41 -04001855// This target is deprecated, DebugGLContext is not well maintained.
Kevin Lubick27d42192018-04-03 12:30:32 -04001856// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001857DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001858 sk_gpu_test::GrContextFactory f;
1859 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001860}
Kevin Lubick30709262018-04-02 11:06:41 -04001861
Kevin Lubick27d42192018-04-03 12:30:32 -04001862DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001863 sk_gpu_test::GrContextFactory f;
1864 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1865}
Hal Canary44801ca2017-03-15 11:39:06 -04001866#endif
1867
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001868DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001869 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001870 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001871 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1872 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001873}
1874
1875// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1876DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001877 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001878 fuzz_canvas(fuzz, &debugCanvas);
1879 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1880 UrlDataManager dataManager(SkString("data"));
1881 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1882 Json::StyledStreamWriter(" ").write(std::cout, json);
1883}