blob: 2cb9968d5b7c3dd2ed8e4632ae63fb775d86ff21 [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 Canaryc640d0d2018-06-13 09:59:02 -040020#include "SkOSFile.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050021#include "SkPathEffect.h"
Cary Clarkefd99cc2018-06-11 16:25:43 -040022#include "SkPicturePriv.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050023#include "SkPictureRecorder.h"
Mike Reed54518ac2017-07-22 08:29:48 -040024#include "SkPoint3.h"
Hal Canary5395c592017-03-08 16:52:18 -050025#include "SkRSXform.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050026#include "SkRegion.h"
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050027#include "SkSurface.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040028#include "SkTo.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050029#include "SkTypeface.h"
30
31// EFFECTS
Hal Canary5395c592017-03-08 16:52:18 -050032#include "Sk1DPathEffect.h"
33#include "Sk2DPathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050034#include "SkAlphaThresholdFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050035#include "SkArithmeticImageFilter.h"
Robert Phillips70e3e9a2017-06-26 14:22:01 -040036#include "SkBlurImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050037#include "SkBlurMaskFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050038#include "SkColorFilterImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050039#include "SkColorMatrixFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050040#include "SkComposeImageFilter.h"
Hal Canary5395c592017-03-08 16:52:18 -050041#include "SkCornerPathEffect.h"
42#include "SkDashPathEffect.h"
43#include "SkDiscretePathEffect.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050044#include "SkDisplacementMapEffect.h"
45#include "SkDropShadowImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050046#include "SkGradientShader.h"
Hal Canary27bece82017-03-07 16:23:20 -050047#include "SkHighContrastFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050048#include "SkImageSource.h"
49#include "SkLightingImageFilter.h"
Hal Canary27bece82017-03-07 16:23:20 -050050#include "SkLumaColorFilter.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050051#include "SkMagnifierImageFilter.h"
52#include "SkMatrixConvolutionImageFilter.h"
53#include "SkMergeImageFilter.h"
54#include "SkMorphologyImageFilter.h"
55#include "SkOffsetImageFilter.h"
56#include "SkPaintImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050057#include "SkPerlinNoiseShader.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050058#include "SkPictureImageFilter.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050059#include "SkReadBuffer.h"
Hal Canary27bece82017-03-07 16:23:20 -050060#include "SkTableColorFilter.h"
Mike Reedc090c642017-05-16 10:39:06 -040061#include "SkTextBlob.h"
Hal Canarye03c3e52017-03-09 11:33:35 -050062#include "SkTileImageFilter.h"
63#include "SkXfermodeImageFilter.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050064
65// SRC
Kevin Lubickfaef5142018-06-07 10:33:11 -040066#include "SkCommandLineFlags.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050067#include "SkUtils.h"
68
Hal Canary44801ca2017-03-15 11:39:06 -040069#if SK_SUPPORT_GPU
70#include "GrContextFactory.h"
Kevin Lubickfaef5142018-06-07 10:33:11 -040071#include "GrContextPriv.h"
72#include "gl/GrGLFunctions.h"
73#include "gl/GrGLGpu.h"
74#include "gl/GrGLUtil.h"
Hal Canary44801ca2017-03-15 11:39:06 -040075#endif
76
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050077// MISC
78
79#include <iostream>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040080#include <utility>
Kevin Lubick1ac8fd22017-03-01 10:42:45 -050081
Kevin Lubickfaef5142018-06-07 10:33:11 -040082DEFINE_bool2(gpuInfo, g, false, "Display GPU information on relevant targets.");
83
Hal Canary24ac42b2017-02-14 13:35:14 -050084// TODO:
Hal Canary5395c592017-03-08 16:52:18 -050085// SkTextBlob with Unicode
Hal Canary44801ca2017-03-15 11:39:06 -040086// SkImage: more types
Hal Canary24ac42b2017-02-14 13:35:14 -050087
Hal Canary1e0138b2017-03-10 13:56:08 -050088// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
89// In fact, all make_fuzz_foo() functions have this potential problem.
90// Use sequence points!
91template <typename T>
92inline T make_fuzz_t(Fuzz* fuzz) {
93 T t;
94 fuzz->next(&t);
95 return t;
Hal Canary24ac42b2017-02-14 13:35:14 -050096}
97
Hal Canaryb69c4b82017-03-08 11:02:40 -050098template <>
99inline void Fuzz::next(SkShader::TileMode* m) {
Hal Canaryf7005202017-03-10 08:48:28 -0500100 fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500101}
102
Hal Canaryb69c4b82017-03-08 11:02:40 -0500103template <>
Hal Canarye03c3e52017-03-09 11:33:35 -0500104inline void Fuzz::next(SkFilterQuality* q) {
Hal Canaryf7005202017-03-10 08:48:28 -0500105 fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
106 SkFilterQuality::kLast_SkFilterQuality);
Hal Canarye03c3e52017-03-09 11:33:35 -0500107}
108
109template <>
Hal Canaryb69c4b82017-03-08 11:02:40 -0500110inline void Fuzz::next(SkMatrix* m) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500111 constexpr int kArrayLength = 9;
112 SkScalar buffer[kArrayLength];
113 int matrixType;
114 this->nextRange(&matrixType, 0, 4);
115 switch (matrixType) {
116 case 0: // identity
117 *m = SkMatrix::I();
118 return;
119 case 1: // translate
120 this->nextRange(&buffer[0], -4000.0f, 4000.0f);
121 this->nextRange(&buffer[1], -4000.0f, 4000.0f);
122 *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
123 return;
124 case 2: // translate + scale
125 this->nextRange(&buffer[0], -400.0f, 400.0f);
126 this->nextRange(&buffer[1], -400.0f, 400.0f);
127 this->nextRange(&buffer[2], -4000.0f, 4000.0f);
128 this->nextRange(&buffer[3], -4000.0f, 4000.0f);
129 *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
130 m->postTranslate(buffer[2], buffer[3]);
131 return;
132 case 3: // affine
133 this->nextN(buffer, 6);
134 m->setAffine(buffer);
135 return;
136 case 4: // perspective
137 this->nextN(buffer, kArrayLength);
138 m->set9(buffer);
139 return;
140 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500141 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500142 return;
143 }
144}
145
Hal Canaryb69c4b82017-03-08 11:02:40 -0500146template <>
147inline void Fuzz::next(SkRRect* rr) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500148 SkRect r;
149 SkVector radii[4];
150 this->next(&r);
Hal Canary27bece82017-03-07 16:23:20 -0500151 r.sort();
152 for (SkVector& vec : radii) {
153 this->nextRange(&vec.fX, 0.0f, 1.0f);
154 vec.fX *= 0.5f * r.width();
155 this->nextRange(&vec.fY, 0.0f, 1.0f);
156 vec.fY *= 0.5f * r.height();
157 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500158 rr->setRectRadii(r, radii);
Hal Canary24ac42b2017-02-14 13:35:14 -0500159}
160
Hal Canaryb69c4b82017-03-08 11:02:40 -0500161template <>
162inline void Fuzz::next(SkBlendMode* mode) {
Hal Canaryf7005202017-03-10 08:48:28 -0500163 fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
Hal Canary24ac42b2017-02-14 13:35:14 -0500164}
165
Hal Canary1e0138b2017-03-10 13:56:08 -0500166static sk_sp<SkImage> make_fuzz_image(Fuzz*);
Hal Canary671e4422017-02-27 13:36:38 -0500167
Hal Canary1e0138b2017-03-10 13:56:08 -0500168static SkBitmap make_fuzz_bitmap(Fuzz*);
Hal Canary24ac42b2017-02-14 13:35:14 -0500169
Hal Canary1e0138b2017-03-10 13:56:08 -0500170static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
Hal Canary671e4422017-02-27 13:36:38 -0500171
Hal Canary1e0138b2017-03-10 13:56:08 -0500172static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
Hal Canary27bece82017-03-07 16:23:20 -0500173 if (depth <= 0) {
174 return nullptr;
175 }
176 int colorFilterType;
177 fuzz->nextRange(&colorFilterType, 0, 8);
178 switch (colorFilterType) {
179 case 0:
180 return nullptr;
181 case 1: {
182 SkColor color;
183 SkBlendMode mode;
184 fuzz->next(&color, &mode);
185 return SkColorFilter::MakeModeFilter(color, mode);
186 }
187 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500188 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500189 if (!outer) {
190 return nullptr;
191 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500192 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
Kevin Lubick23888662018-02-21 08:07:26 -0500193 // makeComposed should be able to handle nullptr.
Mike Reed19d7bd62018-02-19 14:10:57 -0500194 return outer->makeComposed(std::move(inner));
Hal Canary27bece82017-03-07 16:23:20 -0500195 }
196 case 3: {
197 SkScalar array[20];
198 fuzz->nextN(array, SK_ARRAY_COUNT(array));
199 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
200 }
201 case 4: {
202 SkColor mul, add;
203 fuzz->next(&mul, &add);
204 return SkColorMatrixFilter::MakeLightingFilter(mul, add);
205 }
206 case 5: {
207 bool grayscale;
208 int invertStyle;
209 float contrast;
210 fuzz->next(&grayscale);
211 fuzz->nextRange(&invertStyle, 0, 2);
212 fuzz->nextRange(&contrast, -1.0f, 1.0f);
213 return SkHighContrastFilter::Make(SkHighContrastConfig(
214 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
215 }
216 case 6:
217 return SkLumaColorFilter::Make();
218 case 7: {
219 uint8_t table[256];
220 fuzz->nextN(table, SK_ARRAY_COUNT(table));
221 return SkTableColorFilter::Make(table);
222 }
223 case 8: {
224 uint8_t tableA[256];
225 uint8_t tableR[256];
226 uint8_t tableG[256];
227 uint8_t tableB[256];
228 fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
229 fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
230 fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
231 fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
232 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
233 }
Kevin Lubick54f20e02018-01-11 14:50:21 -0500234 default:
235 SkASSERT(false);
236 break;
Hal Canary27bece82017-03-07 16:23:20 -0500237 }
238 return nullptr;
239}
Hal Canary24ac42b2017-02-14 13:35:14 -0500240
Hal Canary1e0138b2017-03-10 13:56:08 -0500241static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500242 SkScalar totalPos = 0;
243 for (int i = 0; i < colorCount; ++i) {
244 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
245 totalPos += pos[i];
246 }
247 totalPos = 1.0f / totalPos;
248 for (int i = 0; i < colorCount; ++i) {
249 pos[i] *= totalPos;
250 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500251 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
Hal Canary24ac42b2017-02-14 13:35:14 -0500252 pos[colorCount - 1] = 1.0f;
253}
254
Hal Canary1e0138b2017-03-10 13:56:08 -0500255static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500256 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
257 sk_sp<SkColorFilter> colorFilter(nullptr);
258 SkBitmap bitmap;
259 sk_sp<SkImage> img;
260 SkShader::TileMode tmX, tmY;
261 bool useMatrix;
262 SkColor color;
263 SkMatrix matrix;
264 SkBlendMode blendMode;
265 int shaderType;
266 if (depth <= 0) {
267 return nullptr;
268 }
Hal Canary671e4422017-02-27 13:36:38 -0500269 fuzz->nextRange(&shaderType, 0, 14);
Hal Canary24ac42b2017-02-14 13:35:14 -0500270 switch (shaderType) {
271 case 0:
272 return nullptr;
273 case 1:
274 return SkShader::MakeEmptyShader();
275 case 2:
276 fuzz->next(&color);
277 return SkShader::MakeColorShader(color);
278 case 3:
Hal Canary1e0138b2017-03-10 13:56:08 -0500279 img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500280 fuzz->next(&tmX, &tmY, &useMatrix);
281 if (useMatrix) {
282 fuzz->next(&matrix);
283 }
284 return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
285 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -0500286 bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -0500287 fuzz->next(&tmX, &tmY, &useMatrix);
288 if (useMatrix) {
289 fuzz->next(&matrix);
290 }
291 return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
292 case 5:
Hal Canary1e0138b2017-03-10 13:56:08 -0500293 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
Hal Canary24ac42b2017-02-14 13:35:14 -0500294 fuzz->next(&matrix);
295 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
296 case 6:
Hal Canary1e0138b2017-03-10 13:56:08 -0500297 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
298 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500299 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
300 case 7:
Hal Canary1e0138b2017-03-10 13:56:08 -0500301 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
302 shader2 = make_fuzz_shader(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -0500303 fuzz->next(&blendMode);
304 return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500305 case 8: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500306 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500307 bool useTile;
308 SkRect tile;
309 fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
310 if (useMatrix) {
311 fuzz->next(&matrix);
Hal Canary671e4422017-02-27 13:36:38 -0500312 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500313 if (useTile) {
314 fuzz->next(&tile);
315 }
316 return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
317 useMatrix ? &matrix : nullptr,
318 useTile ? &tile : nullptr);
319 }
Hal Canary671e4422017-02-27 13:36:38 -0500320 // EFFECTS:
Hal Canary24ac42b2017-02-14 13:35:14 -0500321 case 9:
Florin Malitabb3f5622017-05-31 14:20:12 +0000322 // Deprecated SkGaussianEdgeShader
323 return nullptr;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500324 case 10: {
325 constexpr int kMaxColors = 12;
326 SkPoint pts[2];
327 SkColor colors[kMaxColors];
328 SkScalar pos[kMaxColors];
329 int colorCount;
330 bool usePos;
331 fuzz->nextN(pts, 2);
332 fuzz->nextRange(&colorCount, 2, kMaxColors);
333 fuzz->nextN(colors, colorCount);
334 fuzz->next(&tmX, &useMatrix, &usePos);
335 if (useMatrix) {
336 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500337 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500338 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500339 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500340 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500341 return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
342 tmX, 0, useMatrix ? &matrix : nullptr);
343 }
344 case 11: {
345 constexpr int kMaxColors = 12;
346 SkPoint center;
347 SkScalar radius;
348 int colorCount;
349 bool usePos;
350 SkColor colors[kMaxColors];
351 SkScalar pos[kMaxColors];
352 fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
353 fuzz->nextRange(&colorCount, 2, kMaxColors);
354 fuzz->nextN(colors, colorCount);
355 if (useMatrix) {
356 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500357 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500358 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500359 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canary24ac42b2017-02-14 13:35:14 -0500360 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500361 return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
362 colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
363 }
364 case 12: {
365 constexpr int kMaxColors = 12;
366 SkPoint start, end;
367 SkScalar startRadius, endRadius;
368 int colorCount;
369 bool usePos;
370 SkColor colors[kMaxColors];
371 SkScalar pos[kMaxColors];
372 fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
373 fuzz->nextRange(&colorCount, 2, kMaxColors);
374 fuzz->nextN(colors, colorCount);
375 if (useMatrix) {
376 fuzz->next(&matrix);
Hal Canary24ac42b2017-02-14 13:35:14 -0500377 }
Hal Canaryb69c4b82017-03-08 11:02:40 -0500378 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500379 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500380 }
381 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
382 usePos ? pos : nullptr, colorCount, tmX, 0,
383 useMatrix ? &matrix : nullptr);
384 }
385 case 13: {
386 constexpr int kMaxColors = 12;
387 SkScalar cx, cy;
388 int colorCount;
389 bool usePos;
390 SkColor colors[kMaxColors];
391 SkScalar pos[kMaxColors];
392 fuzz->next(&cx, &cy, &useMatrix, &usePos);
393 fuzz->nextRange(&colorCount, 2, kMaxColors);
394 fuzz->nextN(colors, colorCount);
395 if (useMatrix) {
396 fuzz->next(&matrix);
397 }
398 if (usePos) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500399 fuzz_gradient_stops(fuzz, pos, colorCount);
Hal Canaryb69c4b82017-03-08 11:02:40 -0500400 }
401 return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
402 0, useMatrix ? &matrix : nullptr);
403 }
404 case 14: {
405 SkScalar baseFrequencyX, baseFrequencyY, seed;
406 int numOctaves;
407 SkISize tileSize;
408 bool useTileSize, turbulence;
409 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
410 if (useTileSize) {
411 fuzz->next(&tileSize);
412 }
413 fuzz->nextRange(&numOctaves, 2, 7);
414 if (turbulence) {
415 return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
416 numOctaves, seed,
417 useTileSize ? &tileSize : nullptr);
418 } else {
419 return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
420 numOctaves, seed,
421 useTileSize ? &tileSize : nullptr);
422 }
423 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500424 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -0500425 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -0500426 break;
427 }
Kevin Lubickedbeb8b2017-02-27 16:45:32 -0500428 return nullptr;
Hal Canary24ac42b2017-02-14 13:35:14 -0500429}
430
Hal Canary1e0138b2017-03-10 13:56:08 -0500431static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
Hal Canary5395c592017-03-08 16:52:18 -0500432 if (depth <= 0) {
433 return nullptr;
434 }
435 uint8_t pathEffectType;
Mike Reed40e7e652017-07-22 22:12:59 -0400436 fuzz->nextRange(&pathEffectType, 0, 8);
Hal Canary5395c592017-03-08 16:52:18 -0500437 switch (pathEffectType) {
438 case 0: {
439 return nullptr;
440 }
441 case 1: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500442 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
443 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500444 return SkPathEffect::MakeSum(std::move(first), std::move(second));
445 }
446 case 2: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500447 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
448 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -0500449 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
450 }
451 case 3: {
452 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -0500453 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500454 SkScalar advance, phase;
455 fuzz->next(&advance, &phase);
Hal Canaryf7005202017-03-10 08:48:28 -0500456 SkPath1DPathEffect::Style style;
457 fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
458 return SkPath1DPathEffect::Make(path, advance, phase, style);
Hal Canary5395c592017-03-08 16:52:18 -0500459 }
460 case 4: {
461 SkScalar width;
462 SkMatrix matrix;
463 fuzz->next(&width, &matrix);
464 return SkLine2DPathEffect::Make(width, matrix);
465 }
466 case 5: {
467 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -0500468 FuzzPath(fuzz, &path, 20);
Hal Canary5395c592017-03-08 16:52:18 -0500469 SkMatrix matrix;
470 fuzz->next(&matrix);
471 return SkPath2DPathEffect::Make(matrix, path);
472 }
473 case 6: {
474 SkScalar radius;
475 fuzz->next(&radius);
Hal Canary5395c592017-03-08 16:52:18 -0500476 return SkCornerPathEffect::Make(radius);
477 }
Mike Reed40e7e652017-07-22 22:12:59 -0400478 case 7: {
Hal Canary5395c592017-03-08 16:52:18 -0500479 SkScalar phase;
480 fuzz->next(&phase);
481 SkScalar intervals[20];
482 int count;
483 fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
484 fuzz->nextN(intervals, count);
485 return SkDashPathEffect::Make(intervals, count, phase);
486 }
Mike Reed40e7e652017-07-22 22:12:59 -0400487 case 8: {
Hal Canary5395c592017-03-08 16:52:18 -0500488 SkScalar segLength, dev;
489 uint32_t seed;
490 fuzz->next(&segLength, &dev, &seed);
491 return SkDiscretePathEffect::Make(segLength, dev, seed);
492 }
493 default:
494 SkASSERT(false);
495 return nullptr;
496 }
497}
Hal Canary24ac42b2017-02-14 13:35:14 -0500498
Hal Canary1e0138b2017-03-10 13:56:08 -0500499static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
Hal Canary5395c592017-03-08 16:52:18 -0500500 int maskfilterType;
Robert Phillipsab4f5bd2018-04-18 10:05:00 -0400501 fuzz->nextRange(&maskfilterType, 0, 1);
Hal Canary5395c592017-03-08 16:52:18 -0500502 switch (maskfilterType) {
503 case 0:
504 return nullptr;
505 case 1: {
Hal Canaryf7005202017-03-10 08:48:28 -0500506 SkBlurStyle blurStyle;
507 fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
Hal Canary5395c592017-03-08 16:52:18 -0500508 SkScalar sigma;
509 fuzz->next(&sigma);
510 SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400511 bool useOccluder;
512 fuzz->next(&useOccluder);
513 if (useOccluder) {
Hal Canary5395c592017-03-08 16:52:18 -0500514 fuzz->next(&occluder);
515 }
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400516 bool respectCTM;
517 fuzz->next(&respectCTM);
518 if (useOccluder) {
519 return SkMaskFilter::MakeBlur(blurStyle, sigma, occluder, respectCTM);
520 }
521 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500522 }
Hal Canary5395c592017-03-08 16:52:18 -0500523 default:
524 SkASSERT(false);
525 return nullptr;
526 }
527}
Hal Canary24ac42b2017-02-14 13:35:14 -0500528
Hal Canary1e0138b2017-03-10 13:56:08 -0500529static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
530 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500531 return nullptr;
532 }
533 auto fontMugger = SkFontMgr::RefDefault();
534 SkASSERT(fontMugger);
535 int familyCount = fontMugger->countFamilies();
536 int i, j;
537 fuzz->nextRange(&i, 0, familyCount - 1);
538 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
539 int styleCount = family->count();
540 fuzz->nextRange(&j, 0, styleCount - 1);
541 return sk_sp<SkTypeface>(family->createTypeface(j));
542}
Hal Canary24ac42b2017-02-14 13:35:14 -0500543
Hal Canarye03c3e52017-03-09 11:33:35 -0500544template <>
545inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
546 SkRect rect;
547 uint8_t flags;
548 this->next(&rect);
549 this->nextRange(&flags, 0, 0xF);
550 *cropRect = SkImageFilter::CropRect(rect, flags);
551}
552
Hal Canary1e0138b2017-03-10 13:56:08 -0500553static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500554
555static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
556 if (depth <= 0) {
557 return nullptr;
558 }
559 uint8_t imageFilterType;
560 fuzz->nextRange(&imageFilterType, 1, 6);
561 SkPoint3 p, q;
562 SkColor lightColor;
563 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
564 sk_sp<SkImageFilter> input;
565 SkImageFilter::CropRect cropRect;
566 bool useCropRect;
567 fuzz->next(&useCropRect);
568 if (useCropRect) {
569 fuzz->next(&cropRect);
570 }
571 switch (imageFilterType) {
572 case 1:
573 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500574 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500575 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
576 std::move(input),
577 useCropRect ? &cropRect : nullptr);
578 case 2:
579 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500580 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500581 return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
582 std::move(input),
583 useCropRect ? &cropRect : nullptr);
584 case 3:
585 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500586 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500587 return SkLightingImageFilter::MakeSpotLitDiffuse(
588 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
589 std::move(input), useCropRect ? &cropRect : nullptr);
590 case 4:
591 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500592 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500593 return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
594 shininess, std::move(input),
595 useCropRect ? &cropRect : nullptr);
596 case 5:
597 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500598 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500599 return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
600 shininess, std::move(input),
601 useCropRect ? &cropRect : nullptr);
602 case 6:
603 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
604 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500605 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500606 return SkLightingImageFilter::MakeSpotLitSpecular(
607 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
608 std::move(input), useCropRect ? &cropRect : nullptr);
609 default:
610 SkASSERT(false);
611 return nullptr;
612 }
613}
614
Hal Canary1e0138b2017-03-10 13:56:08 -0500615static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500616
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 if (depth <= 0) {
619 return nullptr;
620 }
621 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500622 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500623 switch (imageFilterType) {
624 case 0:
625 return nullptr;
626 case 1: {
627 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500628 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500629 bool useCropRect;
630 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400631 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500632 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400633 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500634 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400635 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500636 useCropRect ? &cropRect : nullptr);
637 }
638 case 2: {
639 SkMatrix matrix;
640 SkFilterQuality quality;
641 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500642 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500643 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
644 }
645 case 3: {
646 SkRegion region;
647 SkScalar innerMin, outerMax;
Hal Canary1e0138b2017-03-10 13:56:08 -0500648 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500649 bool useCropRect;
650 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400651 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500652 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400653 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500654 }
655 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
656 useCropRect ? &cropRect : nullptr);
657 }
658 case 4: {
659 float k1, k2, k3, k4;
660 bool enforcePMColor;
661 bool useCropRect;
662 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500663 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
664 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500665 SkImageFilter::CropRect cropRect;
666 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400667 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500668 }
669 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
670 std::move(background), std::move(foreground),
671 useCropRect ? &cropRect : nullptr);
672 }
673 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500674 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
675 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500676 bool useCropRect;
677 SkImageFilter::CropRect cropRect;
678 fuzz->next(&useCropRect);
679 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400680 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500681 }
682 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
683 useCropRect ? &cropRect : nullptr);
684 }
685 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500686 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
687 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500688 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
689 }
690 case 7: {
691 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400692 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
693 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500694 SkScalar scale;
695 bool useCropRect;
696 fuzz->next(&scale, &useCropRect);
697 SkImageFilter::CropRect cropRect;
698 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400699 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500700 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500701 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
702 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500703 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
704 std::move(displacement), std::move(color),
705 useCropRect ? &cropRect : nullptr);
706 }
707 case 8: {
708 SkScalar dx, dy, sigmaX, sigmaY;
709 SkColor color;
710 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500711 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500712 bool useCropRect;
713 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
714 SkImageFilter::CropRect cropRect;
715 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400716 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500717 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500718 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500719 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
720 std::move(input),
721 useCropRect ? &cropRect : nullptr);
722 }
723 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500724 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500725 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500726 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500727 SkRect srcRect, dstRect;
728 SkFilterQuality filterQuality;
729 fuzz->next(&srcRect, &dstRect, &filterQuality);
730 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
731 }
732 case 11:
733 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
734 case 12: {
735 SkRect srcRect;
736 SkScalar inset;
737 bool useCropRect;
738 SkImageFilter::CropRect cropRect;
739 fuzz->next(&srcRect, &inset, &useCropRect);
740 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400741 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500742 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500743 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500744 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
745 useCropRect ? &cropRect : nullptr);
746 }
747 case 13: {
748 constexpr int kMaxKernelSize = 5;
749 int32_t n, m;
750 fuzz->nextRange(&n, 1, kMaxKernelSize);
751 fuzz->nextRange(&m, 1, kMaxKernelSize);
752 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
753 fuzz->nextN(kernel, n * m);
754 int32_t offsetX, offsetY;
755 fuzz->nextRange(&offsetX, 0, n - 1);
756 fuzz->nextRange(&offsetY, 0, m - 1);
757 SkScalar gain, bias;
758 bool convolveAlpha, useCropRect;
759 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
760 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500761 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500762 SkImageFilter::CropRect cropRect;
763 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400764 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500765 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500766 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500767 return SkMatrixConvolutionImageFilter::Make(
768 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
769 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
770 }
771 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500772 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
773 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500774 bool useCropRect;
775 fuzz->next(&useCropRect);
776 SkImageFilter::CropRect cropRect;
777 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400778 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500779 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400780 return SkMergeImageFilter::Make(std::move(first), std::move(second),
781 useCropRect ? &cropRect : nullptr);
782 }
783 case 15: {
784 constexpr int kMaxCount = 4;
785 sk_sp<SkImageFilter> ifs[kMaxCount];
786 int count;
787 fuzz->nextRange(&count, 1, kMaxCount);
788 for (int i = 0; i < count; ++i) {
789 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
790 }
791 bool useCropRect;
792 fuzz->next(&useCropRect);
793 SkImageFilter::CropRect cropRect;
794 if (useCropRect) {
795 fuzz->next(&cropRect);
796 }
797 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500798 }
799 case 16: {
800 int rx, ry;
801 fuzz->next(&rx, &ry);
802 bool useCropRect;
803 fuzz->next(&useCropRect);
804 SkImageFilter::CropRect cropRect;
805 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400806 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500807 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500808 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500809 return SkDilateImageFilter::Make(rx, ry, std::move(input),
810 useCropRect ? &cropRect : nullptr);
811 }
812 case 17: {
813 int rx, ry;
814 fuzz->next(&rx, &ry);
815 bool useCropRect;
816 fuzz->next(&useCropRect);
817 SkImageFilter::CropRect cropRect;
818 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400819 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500820 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500821 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500822 return SkErodeImageFilter::Make(rx, ry, std::move(input),
823 useCropRect ? &cropRect : nullptr);
824 }
825 case 18: {
826 SkScalar dx, dy;
827 fuzz->next(&dx, &dy);
828 bool useCropRect;
829 fuzz->next(&useCropRect);
830 SkImageFilter::CropRect cropRect;
831 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400832 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500833 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500834 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500835 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
836 useCropRect ? &cropRect : nullptr);
837 }
838 case 19: {
839 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500840 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500841 bool useCropRect;
842 fuzz->next(&useCropRect);
843 SkImageFilter::CropRect cropRect;
844 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400845 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500846 }
847 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
848 }
849 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500850 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500851 return SkPictureImageFilter::Make(std::move(picture));
852 }
853 case 21: {
854 SkRect cropRect;
855 fuzz->next(&cropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500856 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500857 return SkPictureImageFilter::Make(std::move(picture), cropRect);
858 }
859 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500860 SkRect src, dst;
861 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500862 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500863 return SkTileImageFilter::Make(src, dst, std::move(input));
864 }
Mike Reed77e487d2017-11-09 21:50:20 +0000865 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500866 SkBlendMode blendMode;
867 bool useCropRect;
868 fuzz->next(&useCropRect, &blendMode);
869 SkImageFilter::CropRect cropRect;
870 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400871 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500872 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500873 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
874 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500875 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
876 useCropRect ? &cropRect : nullptr);
877 }
878 default:
879 SkASSERT(false);
880 return nullptr;
881 }
882}
Hal Canary24ac42b2017-02-14 13:35:14 -0500883
Hal Canary1e0138b2017-03-10 13:56:08 -0500884static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500885 int w, h;
886 fuzz->nextRange(&w, 1, 1024);
887 fuzz->nextRange(&h, 1, 1024);
888 SkAutoTMalloc<SkPMColor> data(w * h);
889 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
890 int n = w * h;
891 for (int i = 0; i < n; ++i) {
892 SkColor c;
893 fuzz->next(&c);
894 data[i] = SkPreMultiplyColor(c);
895 }
896 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500897 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
898 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500899}
900
Hal Canary1e0138b2017-03-10 13:56:08 -0500901static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500902 SkBitmap bitmap;
903 int w, h;
904 fuzz->nextRange(&w, 1, 1024);
905 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500906 if (!bitmap.tryAllocN32Pixels(w, h)) {
907 SkDEBUGF(("Could not allocate pixels %d x %d", w, h));
908 return bitmap;
909 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500910 for (int y = 0; y < h; ++y) {
911 for (int x = 0; x < w; ++x) {
912 SkColor c;
913 fuzz->next(&c);
914 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
915 }
916 }
917 return bitmap;
918}
919
Hal Canary1e0138b2017-03-10 13:56:08 -0500920template <typename T, typename Min, typename Max>
921inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
922 T value;
923 fuzz_enum_range(fuzz, &value, minv, maxv);
924 return value;
925}
926
927static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500928 if (!fuzz || !paint || depth <= 0) {
929 return;
930 }
931
Hal Canary1e0138b2017-03-10 13:56:08 -0500932 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
933 paint->setDither( make_fuzz_t<bool>(fuzz));
934 paint->setColor( make_fuzz_t<SkColor>(fuzz));
935 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
936 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
937 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
938 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
939 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
940 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
941 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
942 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500943
944 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500945 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
946 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
947 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
948 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500949 }
950}
951
Hal Canary1e0138b2017-03-10 13:56:08 -0500952static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
953 paint->setTypeface( make_fuzz_typeface(fuzz));
954 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
955 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
956 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
957 paint->setLinearText( make_fuzz_t<bool>(fuzz));
958 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
959 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
960 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
961 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
962 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
963 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
964 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
965 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
966 SkPaint::kFull_Hinting));
967 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -0500968}
969
970static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500971 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -0500972}
973
Hal Canary5395c592017-03-08 16:52:18 -0500974constexpr int kMaxGlyphCount = 30;
975
Hal Canary1e0138b2017-03-10 13:56:08 -0500976static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -0500977 SkTDArray<uint8_t> array;
978 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500979 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
980 : SkTypeface::MakeDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500981 if (glyphRange == 0) {
982 // Some fuzzing environments have no fonts, so empty array is the best
983 // we can do.
984 return array;
985 }
Hal Canary671e4422017-02-27 13:36:38 -0500986 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500987 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500988 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
989 for (int i = 0; i < glyphCount; ++i) {
990 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
991 }
992 return array;
993 }
994 static const SkUnichar ranges[][2] = {
995 {0x0020, 0x007F},
996 {0x00A1, 0x0250},
997 {0x0400, 0x0500},
998 };
999 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001000 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001001 count += (ranges[i][1] - ranges[i][0]);
1002 }
Hal Canary5395c592017-03-08 16:52:18 -05001003 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -05001004 SkUnichar buffer[kMaxLength];
1005 int length;
1006 fuzz->nextRange(&length, 1, kMaxLength);
1007 for (int j = 0; j < length; ++j) {
1008 int32_t value;
1009 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001010 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001011 if (value + ranges[i][0] < ranges[i][1]) {
1012 buffer[j] = value + ranges[i][0];
1013 break;
1014 } else {
1015 value -= (ranges[i][1] - ranges[i][0]);
1016 }
1017 }
1018 }
1019 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001020 case SkPaint::kUTF8_TextEncoding: {
1021 size_t utf8len = 0;
1022 for (int j = 0; j < length; ++j) {
1023 utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001024 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001025 char* ptr = (char*)array.append(utf8len);
1026 for (int j = 0; j < length; ++j) {
1027 ptr += SkUTF8_FromUnichar(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001028 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001029 } break;
1030 case SkPaint::kUTF16_TextEncoding: {
1031 size_t utf16len = 0;
1032 for (int j = 0; j < length; ++j) {
1033 utf16len += SkUTF16_FromUnichar(buffer[j]);
1034 }
1035 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1036 for (int j = 0; j < length; ++j) {
1037 ptr += SkUTF16_FromUnichar(buffer[j], ptr);
1038 }
1039 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001040 case SkPaint::kUTF32_TextEncoding:
1041 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001042 break;
Hal Canary671e4422017-02-27 13:36:38 -05001043 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001044 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001045 break;
Hal Canary671e4422017-02-27 13:36:38 -05001046 }
1047 return array;
1048}
1049
Hal Canary5395c592017-03-08 16:52:18 -05001050static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1051 SkTextBlobBuilder textBlobBuilder;
1052 int8_t runCount;
1053 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1054 while (runCount-- > 0) {
1055 SkPaint paint;
1056 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001057 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001058 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1059 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1060 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1061 SkASSERT(glyphCount <= kMaxGlyphCount);
1062 SkScalar x, y;
1063 const SkTextBlobBuilder::RunBuffer* buffer;
1064 uint8_t runType;
1065 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1066 switch (runType) {
1067 case 0:
1068 fuzz->next(&x, &y);
1069 // TODO: Test other variations of this.
1070 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1071 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1072 break;
1073 case 1:
1074 fuzz->next(&y);
1075 // TODO: Test other variations of this.
1076 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1077 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1078 fuzz->nextN(buffer->pos, glyphCount);
1079 break;
1080 case 2:
1081 // TODO: Test other variations of this.
1082 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1083 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1084 fuzz->nextN(buffer->pos, glyphCount * 2);
1085 break;
1086 default:
1087 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001088 break;
Hal Canary5395c592017-03-08 16:52:18 -05001089 }
1090 }
1091 return textBlobBuilder.make();
1092}
1093
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001094extern std::atomic<bool> gSkUseDeltaAA;
1095extern std::atomic<bool> gSkForceDeltaAA;
1096
Hal Canary1e0138b2017-03-10 13:56:08 -05001097static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001098 if (!fuzz || !canvas || depth <= 0) {
1099 return;
1100 }
1101 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001102 bool useDAA;
1103 fuzz->next(&useDAA);
1104 if (useDAA) {
1105 gSkForceDeltaAA = true;
1106 gSkUseDeltaAA = true;
1107 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001108 unsigned N;
1109 fuzz->nextRange(&N, 0, 2000);
1110 for (unsigned i = 0; i < N; ++i) {
1111 if (fuzz->exhausted()) {
1112 return;
1113 }
1114 SkPaint paint;
1115 SkMatrix matrix;
1116 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001117 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001118 switch (drawCommand) {
1119 case 0:
1120 canvas->flush();
1121 break;
1122 case 1:
1123 canvas->save();
1124 break;
1125 case 2: {
1126 SkRect bounds;
1127 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001128 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001129 canvas->saveLayer(&bounds, &paint);
1130 break;
1131 }
1132 case 3: {
1133 SkRect bounds;
1134 fuzz->next(&bounds);
1135 canvas->saveLayer(&bounds, nullptr);
1136 break;
1137 }
1138 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001139 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001140 canvas->saveLayer(nullptr, &paint);
1141 break;
1142 case 5:
1143 canvas->saveLayer(nullptr, nullptr);
1144 break;
1145 case 6: {
1146 uint8_t alpha;
1147 fuzz->next(&alpha);
1148 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1149 break;
1150 }
1151 case 7: {
1152 SkRect bounds;
1153 uint8_t alpha;
1154 fuzz->next(&bounds, &alpha);
1155 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1156 break;
1157 }
1158 case 8: {
1159 SkCanvas::SaveLayerRec saveLayerRec;
1160 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001161 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001162 fuzz->next(&bounds);
1163 saveLayerRec.fBounds = &bounds;
1164 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001165 if (make_fuzz_t<bool>(fuzz)) {
1166 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001167 saveLayerRec.fPaint = &paint;
1168 }
1169 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001170 if (make_fuzz_t<bool>(fuzz)) {
1171 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001172 saveLayerRec.fBackdrop = imageFilter.get();
1173 }
Hal Canary5395c592017-03-08 16:52:18 -05001174 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001175 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001176 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1177 // }
1178
Hal Canary24ac42b2017-02-14 13:35:14 -05001179 canvas->saveLayer(saveLayerRec);
1180 break;
1181 }
1182 case 9:
1183 canvas->restore();
1184 break;
1185 case 10: {
1186 int saveCount;
1187 fuzz->next(&saveCount);
1188 canvas->restoreToCount(saveCount);
1189 break;
1190 }
1191 case 11: {
1192 SkScalar x, y;
1193 fuzz->next(&x, &y);
1194 canvas->translate(x, y);
1195 break;
1196 }
1197 case 12: {
1198 SkScalar x, y;
1199 fuzz->next(&x, &y);
1200 canvas->scale(x, y);
1201 break;
1202 }
1203 case 13: {
1204 SkScalar v;
1205 fuzz->next(&v);
1206 canvas->rotate(v);
1207 break;
1208 }
1209 case 14: {
1210 SkScalar x, y, v;
1211 fuzz->next(&x, &y, &v);
1212 canvas->rotate(v, x, y);
1213 break;
1214 }
1215 case 15: {
1216 SkScalar x, y;
1217 fuzz->next(&x, &y);
1218 canvas->skew(x, y);
1219 break;
1220 }
1221 case 16: {
1222 SkMatrix mat;
1223 fuzz->next(&mat);
1224 canvas->concat(mat);
1225 break;
1226 }
1227 case 17: {
1228 SkMatrix mat;
1229 fuzz->next(&mat);
1230 canvas->setMatrix(mat);
1231 break;
1232 }
1233 case 18:
1234 canvas->resetMatrix();
1235 break;
1236 case 19: {
1237 SkRect r;
1238 int op;
1239 bool doAntiAlias;
1240 fuzz->next(&r, &doAntiAlias);
1241 fuzz->nextRange(&op, 0, 1);
1242 r.sort();
1243 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1244 break;
1245 }
1246 case 20: {
1247 SkRRect rr;
1248 int op;
1249 bool doAntiAlias;
1250 fuzz->next(&rr);
1251 fuzz->next(&doAntiAlias);
1252 fuzz->nextRange(&op, 0, 1);
1253 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1254 break;
1255 }
1256 case 21: {
1257 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001258 FuzzPath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001259 int op;
1260 bool doAntiAlias;
1261 fuzz->next(&doAntiAlias);
1262 fuzz->nextRange(&op, 0, 1);
1263 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1264 break;
1265 }
1266 case 22: {
1267 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001268 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001269 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001270 fuzz->nextRange(&op, 0, 1);
1271 canvas->clipRegion(region, (SkClipOp)op);
1272 break;
1273 }
1274 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001275 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001276 canvas->drawPaint(paint);
1277 break;
1278 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001279 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001280 SkCanvas::PointMode pointMode;
1281 fuzz_enum_range(fuzz, &pointMode,
1282 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001283 size_t count;
1284 constexpr int kMaxCount = 30;
1285 fuzz->nextRange(&count, 0, kMaxCount);
1286 SkPoint pts[kMaxCount];
1287 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001288 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001289 break;
1290 }
1291 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001292 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001293 SkRect r;
1294 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001295 if (!r.isFinite()) {
1296 break;
1297 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001298 canvas->drawRect(r, paint);
1299 break;
1300 }
1301 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001302 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001303 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001304 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001305 canvas->drawRegion(region, paint);
1306 break;
1307 }
1308 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001309 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001310 SkRect r;
1311 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001312 if (!r.isFinite()) {
1313 break;
1314 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001315 canvas->drawOval(r, paint);
1316 break;
1317 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001318 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001319 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001320 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001321 SkRRect rr;
1322 fuzz->next(&rr);
1323 canvas->drawRRect(rr, paint);
1324 break;
1325 }
1326 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001327 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001328 SkRRect orr, irr;
1329 fuzz->next(&orr);
1330 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001331 if (orr.getBounds().contains(irr.getBounds())) {
1332 canvas->drawDRRect(orr, irr, paint);
1333 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001334 break;
1335 }
1336 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001337 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001338 SkRect r;
1339 SkScalar start, sweep;
1340 bool useCenter;
1341 fuzz->next(&r, &start, &sweep, &useCenter);
1342 canvas->drawArc(r, start, sweep, useCenter, paint);
1343 break;
1344 }
1345 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001346 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001347 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001348 FuzzPath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001349 canvas->drawPath(path, paint);
1350 break;
1351 }
1352 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001353 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001354 SkScalar left, top;
1355 bool usePaint;
1356 fuzz->next(&left, &top, &usePaint);
1357 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001358 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001359 }
1360 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1361 break;
1362 }
1363 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001364 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001365 SkRect src, dst;
1366 bool usePaint;
1367 fuzz->next(&src, &dst, &usePaint);
1368 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001369 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001370 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001371 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001372 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1373 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001374 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1375 break;
1376 }
1377 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001378 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001379 SkIRect src;
1380 SkRect dst;
1381 bool usePaint;
1382 fuzz->next(&src, &dst, &usePaint);
1383 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001384 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001385 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001386 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001387 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1388 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001389 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1390 break;
1391 }
1392 case 36: {
1393 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001394 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001395 SkRect dst;
1396 fuzz->next(&dst, &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 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001400 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001401 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1402 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001403 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1404 break;
1405 }
1406 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001407 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001408 SkIRect center;
1409 SkRect dst;
1410 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001411 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001412 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001413 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001414 }
Hal Canary0361d492017-03-15 12:58:15 -04001415 if (make_fuzz_t<bool>(fuzz)) {
1416 fuzz->next(&center);
1417 } else { // Make valid center, see SkLatticeIter::Valid().
1418 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1419 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1420 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1421 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1422 }
1423 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001424 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1425 break;
1426 }
1427 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001428 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001429 SkScalar left, top;
1430 bool usePaint;
1431 fuzz->next(&left, &top, &usePaint);
1432 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001433 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001434 }
1435 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1436 break;
1437 }
1438 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001439 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001440 SkRect src, dst;
1441 bool usePaint;
1442 fuzz->next(&src, &dst, &usePaint);
1443 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001444 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001445 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001446 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001447 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1448 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001449 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1450 break;
1451 }
1452 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001453 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001454 SkIRect src;
1455 SkRect dst;
1456 bool usePaint;
1457 fuzz->next(&src, &dst, &usePaint);
1458 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001459 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001460 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001461 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001462 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1463 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001464 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1465 break;
1466 }
1467 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001468 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001469 SkRect dst;
1470 bool usePaint;
1471 fuzz->next(&dst, &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 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001475 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001476 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1477 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001478 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1479 break;
1480 }
1481 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001482 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001483 SkIRect center;
1484 SkRect dst;
1485 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001486 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001487 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001488 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001489 }
Hal Canary0361d492017-03-15 12:58:15 -04001490 if (make_fuzz_t<bool>(fuzz)) {
1491 fuzz->next(&center);
1492 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001493 if (img.width() == 0 || img.height() == 0) {
1494 // bitmap may not have had its pixels initialized.
1495 break;
1496 }
Hal Canary0361d492017-03-15 12:58:15 -04001497 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1498 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1499 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1500 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1501 }
1502 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001503 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1504 break;
1505 }
1506 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001507 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001508 bool usePaint;
1509 SkRect dst;
1510 fuzz->next(&usePaint, &dst);
1511 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001512 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001513 }
1514 constexpr int kMax = 6;
1515 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001516 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001517 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1518 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1519 fuzz->nextN(xDivs, lattice.fXCount);
1520 fuzz->nextN(yDivs, lattice.fYCount);
1521 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1522 break;
1523 }
1524 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001525 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001526 bool usePaint;
1527 SkRect dst;
1528 fuzz->next(&usePaint, &dst);
1529 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001530 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001531 }
1532 constexpr int kMax = 6;
1533 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001534 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001535 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1536 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1537 fuzz->nextN(xDivs, lattice.fXCount);
1538 fuzz->nextN(yDivs, lattice.fYCount);
1539 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1540 break;
1541 }
1542 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001543 fuzz_paint(fuzz, &paint, depth - 1);
1544 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001545 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001546 SkScalar x, y;
1547 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001548 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001549 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001550 break;
1551 }
1552 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001553 fuzz_paint(fuzz, &paint, depth - 1);
1554 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001555 fuzz_paint_text_encoding(fuzz, &paint);
1556 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001557 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001558 if (glyphCount < 1) {
1559 break;
1560 }
1561 SkAutoTMalloc<SkPoint> pos(glyphCount);
1562 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001563 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001564 pos[0] = {0, 0};
1565 for (int i = 1; i < glyphCount; ++i) {
1566 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001567 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001568 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1569 }
Hal Canary671e4422017-02-27 13:36:38 -05001570 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001571 break;
1572 }
1573 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001574 fuzz_paint(fuzz, &paint, depth - 1);
1575 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001576 fuzz_paint_text_encoding(fuzz, &paint);
1577 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001578 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001579 SkAutoTMalloc<SkScalar> widths(glyphCount);
1580 if (glyphCount < 1) {
1581 break;
1582 }
Hal Canary671e4422017-02-27 13:36:38 -05001583 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001584 SkScalar x = widths[0];
1585 for (int i = 0; i < glyphCount; ++i) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001586 using std::swap;
1587 swap(x, widths[i]);
Hal Canary24ac42b2017-02-14 13:35:14 -05001588 x += widths[i];
1589 SkScalar offset;
1590 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001591 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001592 widths[i] += offset;
1593 }
1594 SkScalar y;
1595 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001596 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001597 break;
1598 }
1599 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001600 fuzz_paint(fuzz, &paint, depth - 1);
1601 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001602 fuzz_paint_text_encoding(fuzz, &paint);
1603 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001604 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001605 FuzzPath(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001606 SkScalar hOffset, vOffset;
1607 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001608 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1609 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001610 break;
1611 }
1612 case 49: {
1613 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001614 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001615 if (useMatrix) {
1616 fuzz->next(&matrix);
1617 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001618 fuzz_paint(fuzz, &paint, depth - 1);
1619 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001620 fuzz_paint_text_encoding(fuzz, &paint);
1621 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001622 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001623 FuzzPath(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001624 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001625 useMatrix ? &matrix : nullptr, paint);
1626 break;
1627 }
1628 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001629 fuzz_paint(fuzz, &paint, depth - 1);
1630 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001631 fuzz_paint_text_encoding(fuzz, &paint);
1632 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1633 SkRSXform rSXform[kMaxGlyphCount];
1634 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1635 SkASSERT(glyphCount <= kMaxGlyphCount);
1636 fuzz->nextN(rSXform, glyphCount);
1637 SkRect cullRect;
1638 bool useCullRect;
1639 fuzz->next(&useCullRect);
1640 if (useCullRect) {
1641 fuzz->next(&cullRect);
1642 }
1643 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1644 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001645 break;
1646 }
1647 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001648 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001649 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001650 SkScalar x, y;
1651 fuzz->next(&x, &y);
1652 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001653 break;
1654 }
1655 case 52: {
1656 bool usePaint, useMatrix;
1657 fuzz->next(&usePaint, &useMatrix);
1658 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001659 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001660 }
1661 if (useMatrix) {
1662 fuzz->next(&matrix);
1663 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001664 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001665 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1666 usePaint ? &paint : nullptr);
1667 break;
1668 }
1669 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001670 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001671 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001672 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001673 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001674 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001675 constexpr int kMaxCount = 100;
1676 int vertexCount;
1677 SkPoint vertices[kMaxCount];
1678 SkPoint texs[kMaxCount];
1679 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001680 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1681 fuzz->nextN(vertices, vertexCount);
1682 bool useTexs, useColors;
1683 fuzz->next(&useTexs, &useColors);
1684 if (useTexs) {
1685 fuzz->nextN(texs, vertexCount);
1686 }
1687 if (useColors) {
1688 fuzz->nextN(colors, vertexCount);
1689 }
1690 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001691 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001692 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001693 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1694 for (int i = 0; i < indexCount; ++i) {
1695 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1696 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001697 }
Mike Reed887cdf12017-04-03 11:11:09 -04001698 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1699 useTexs ? texs : nullptr,
1700 useColors ? colors : nullptr,
1701 indexCount, indices),
1702 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001703 break;
1704 }
1705 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001706 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001707 break;
1708 }
1709 }
1710}
1711
Hal Canary1e0138b2017-03-10 13:56:08 -05001712static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001713 SkScalar w, h;
1714 fuzz->next(&w, &h);
1715 SkPictureRecorder pictureRecorder;
1716 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1717 return pictureRecorder.finishRecordingAsPicture();
1718}
1719
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001720DEF_FUZZ(NullCanvas, fuzz) {
1721 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001722}
1723
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001724constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001725
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001726DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001727 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001728 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001729 fuzz_canvas(fuzz, surface->getCanvas());
1730}
1731
Hal Canarye2924432017-12-01 11:46:26 -05001732DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1733 SkPictureRecorder recorder;
1734 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1735 SkIntToScalar(kCanvasSize.height())));
1736 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1737 if (!pic) { fuzz->signalBug(); }
1738 sk_sp<SkData> data = pic->serialize();
1739 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001740 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001741 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001742 if (!deserialized) { fuzz->signalBug(); }
1743 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1744 SkASSERT(surface && surface->getCanvas());
1745 surface->getCanvas()->drawPicture(deserialized);
1746}
1747
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001748DEF_FUZZ(ImageFilter, fuzz) {
1749 auto fil = make_fuzz_imageFilter(fuzz, 20);
1750
1751 SkPaint paint;
1752 paint.setImageFilter(fil);
1753 SkBitmap bitmap;
1754 SkCanvas canvas(bitmap);
1755 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1756}
1757
1758
1759//SkRandom _rand;
1760#define SK_ADD_RANDOM_BIT_FLIPS
1761
1762DEF_FUZZ(SerializedImageFilter, fuzz) {
1763 auto filter = make_fuzz_imageFilter(fuzz, 20);
1764 auto data = filter->serialize();
1765 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1766 size_t len = data->size();
1767#ifdef SK_ADD_RANDOM_BIT_FLIPS
1768 unsigned char* p = const_cast<unsigned char*>(ptr);
1769 for (size_t i = 0; i < len; ++i, ++p) {
1770 uint8_t j;
1771 fuzz->nextRange(&j, 1, 250);
1772 if (j == 1) { // 0.4% of the time, flip a bit or byte
1773 uint8_t k;
1774 fuzz->nextRange(&k, 1, 10);
1775 if (k == 1) { // Then 10% of the time, change a whole byte
1776 uint8_t s;
1777 fuzz->nextRange(&s, 0, 2);
1778 switch(s) {
1779 case 0:
1780 *p ^= 0xFF; // Flip entire byte
1781 break;
1782 case 1:
1783 *p = 0xFF; // Set all bits to 1
1784 break;
1785 case 2:
1786 *p = 0x00; // Set all bits to 0
1787 break;
1788 }
1789 } else {
1790 uint8_t s;
1791 fuzz->nextRange(&s, 0, 7);
1792 *p ^= (1 << 7);
1793 }
1794 }
1795 }
1796#endif // SK_ADD_RANDOM_BIT_FLIPS
1797 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1798
1799 // uncomment below to write out a serialized image filter (to make corpus
1800 // for -t filter_fuzz)
1801 // SkString s("./serialized_filters/sf");
1802 // s.appendU32(_rand.nextU());
1803 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1804 // sk_fwrite(data->bytes(), data->size(), file);
1805 // sk_fclose(file);
1806
1807 SkPaint paint;
1808 paint.setImageFilter(deserializedFil);
1809 SkBitmap bitmap;
1810 SkCanvas canvas(bitmap);
1811 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1812}
1813
Hal Canary44801ca2017-03-15 11:39:06 -04001814#if SK_SUPPORT_GPU
Kevin Lubickfaef5142018-06-07 10:33:11 -04001815
1816static void dump_GPU_info(GrContext* context) {
1817 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->contextPriv().getGpu())
1818 ->glInterface();
1819 const GrGLubyte* output;
1820 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1821 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1822
1823 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1824 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1825
1826 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1827 SkDebugf("GL_VERSION %s\n", (const char*) output);
1828}
1829
Hal Canary5aa91582017-03-21 11:11:44 -07001830static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1831 SkASSERT(context);
1832 auto surface = SkSurface::MakeRenderTarget(
1833 context,
1834 SkBudgeted::kNo,
1835 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1836 SkASSERT(surface && surface->getCanvas());
1837 fuzz_canvas(fuzz, surface->getCanvas());
1838}
1839
Hal Canary44801ca2017-03-15 11:39:06 -04001840DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001841 sk_gpu_test::GrContextFactory f;
1842 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001843 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001844 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001845 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001846 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001847 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001848 }
Hal Canary5aa91582017-03-21 11:11:44 -07001849 fuzz_ganesh(fuzz, context);
1850}
1851
Kevin Lubick27d42192018-04-03 12:30:32 -04001852// This target is deprecated, NullGLContext is not well maintained.
1853// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001854DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001855 sk_gpu_test::GrContextFactory f;
1856 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001857}
1858
Kevin Lubick30709262018-04-02 11:06:41 -04001859// This target is deprecated, DebugGLContext is not well maintained.
Kevin Lubick27d42192018-04-03 12:30:32 -04001860// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001861DEF_FUZZ(DebugGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001862 sk_gpu_test::GrContextFactory f;
1863 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
Hal Canary44801ca2017-03-15 11:39:06 -04001864}
Kevin Lubick30709262018-04-02 11:06:41 -04001865
Kevin Lubick27d42192018-04-03 12:30:32 -04001866DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001867 sk_gpu_test::GrContextFactory f;
1868 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1869}
Hal Canary44801ca2017-03-15 11:39:06 -04001870#endif
1871
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001872DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001873 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001874 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001875 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1876 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001877}
1878
1879// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1880DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001881 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001882 fuzz_canvas(fuzz, &debugCanvas);
1883 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1884 UrlDataManager dataManager(SkString("data"));
1885 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1886 Json::StyledStreamWriter(" ").write(std::cout, json);
1887}