blob: 58cfcde3a6eb5829403eb377e36fa9cc64e44bf4 [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 Canaryea60b952018-08-21 11:45:46 -040067#include "SkUTF.h"
Hal Canary24ac42b2017-02-14 13:35:14 -050068
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);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400510 bool respectCTM;
511 fuzz->next(&respectCTM);
Kevin Lubick1b1a5572018-06-04 17:02:46 -0400512 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
Hal Canary5395c592017-03-08 16:52:18 -0500513 }
Hal Canary5395c592017-03-08 16:52:18 -0500514 default:
515 SkASSERT(false);
516 return nullptr;
517 }
518}
Hal Canary24ac42b2017-02-14 13:35:14 -0500519
Hal Canary1e0138b2017-03-10 13:56:08 -0500520static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
521 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary671e4422017-02-27 13:36:38 -0500522 return nullptr;
523 }
524 auto fontMugger = SkFontMgr::RefDefault();
525 SkASSERT(fontMugger);
526 int familyCount = fontMugger->countFamilies();
527 int i, j;
528 fuzz->nextRange(&i, 0, familyCount - 1);
529 sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
530 int styleCount = family->count();
531 fuzz->nextRange(&j, 0, styleCount - 1);
532 return sk_sp<SkTypeface>(family->createTypeface(j));
533}
Hal Canary24ac42b2017-02-14 13:35:14 -0500534
Hal Canarye03c3e52017-03-09 11:33:35 -0500535template <>
536inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
537 SkRect rect;
538 uint8_t flags;
539 this->next(&rect);
540 this->nextRange(&flags, 0, 0xF);
541 *cropRect = SkImageFilter::CropRect(rect, flags);
542}
543
Hal Canary1e0138b2017-03-10 13:56:08 -0500544static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500545
546static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
547 if (depth <= 0) {
548 return nullptr;
549 }
550 uint8_t imageFilterType;
551 fuzz->nextRange(&imageFilterType, 1, 6);
552 SkPoint3 p, q;
553 SkColor lightColor;
554 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
555 sk_sp<SkImageFilter> input;
556 SkImageFilter::CropRect cropRect;
557 bool useCropRect;
558 fuzz->next(&useCropRect);
559 if (useCropRect) {
560 fuzz->next(&cropRect);
561 }
562 switch (imageFilterType) {
563 case 1:
564 fuzz->next(&p, &lightColor, &surfaceScale, &k);
Hal Canary1e0138b2017-03-10 13:56:08 -0500565 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500566 return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
567 std::move(input),
568 useCropRect ? &cropRect : nullptr);
569 case 2:
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::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
573 std::move(input),
574 useCropRect ? &cropRect : nullptr);
575 case 3:
576 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &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::MakeSpotLitDiffuse(
579 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
580 std::move(input), useCropRect ? &cropRect : nullptr);
581 case 4:
582 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
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::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
585 shininess, std::move(input),
586 useCropRect ? &cropRect : nullptr);
587 case 5:
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::MakePointLitSpecular(p, lightColor, surfaceScale, k,
591 shininess, std::move(input),
592 useCropRect ? &cropRect : nullptr);
593 case 6:
594 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
595 &shininess);
Hal Canary1e0138b2017-03-10 13:56:08 -0500596 input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500597 return SkLightingImageFilter::MakeSpotLitSpecular(
598 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
599 std::move(input), useCropRect ? &cropRect : nullptr);
600 default:
601 SkASSERT(false);
602 return nullptr;
603 }
604}
605
Hal Canary1e0138b2017-03-10 13:56:08 -0500606static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);
Hal Canarye03c3e52017-03-09 11:33:35 -0500607
Hal Canary1e0138b2017-03-10 13:56:08 -0500608static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
Hal Canarye03c3e52017-03-09 11:33:35 -0500609 if (depth <= 0) {
610 return nullptr;
611 }
612 uint8_t imageFilterType;
Kevin Lubick54f20e02018-01-11 14:50:21 -0500613 fuzz->nextRange(&imageFilterType, 0, 23);
Hal Canarye03c3e52017-03-09 11:33:35 -0500614 switch (imageFilterType) {
615 case 0:
616 return nullptr;
617 case 1: {
618 SkScalar sigmaX, sigmaY;
Hal Canary1e0138b2017-03-10 13:56:08 -0500619 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500620 bool useCropRect;
621 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400622 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500623 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400624 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500625 }
Robert Phillips70e3e9a2017-06-26 14:22:01 -0400626 return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
Hal Canarye03c3e52017-03-09 11:33:35 -0500627 useCropRect ? &cropRect : nullptr);
628 }
629 case 2: {
630 SkMatrix matrix;
631 SkFilterQuality quality;
632 fuzz->next(&matrix, &quality);
Hal Canary1e0138b2017-03-10 13:56:08 -0500633 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500634 return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
635 }
636 case 3: {
637 SkRegion region;
638 SkScalar innerMin, outerMax;
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 bool useCropRect;
641 fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
Kevin Lubickdad29a02017-03-14 17:20:24 -0400642 SkImageFilter::CropRect cropRect;
Hal Canarye03c3e52017-03-09 11:33:35 -0500643 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400644 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500645 }
646 return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
647 useCropRect ? &cropRect : nullptr);
648 }
649 case 4: {
650 float k1, k2, k3, k4;
651 bool enforcePMColor;
652 bool useCropRect;
653 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
Hal Canary1e0138b2017-03-10 13:56:08 -0500654 sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
655 sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500656 SkImageFilter::CropRect cropRect;
657 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400658 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500659 }
660 return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
661 std::move(background), std::move(foreground),
662 useCropRect ? &cropRect : nullptr);
663 }
664 case 5: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500665 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
666 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500667 bool useCropRect;
668 SkImageFilter::CropRect cropRect;
669 fuzz->next(&useCropRect);
670 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400671 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500672 }
673 return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
674 useCropRect ? &cropRect : nullptr);
675 }
676 case 6: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500677 sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
678 sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500679 return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
680 }
681 case 7: {
682 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
Hal Canaryf49b1e02017-03-15 16:58:33 -0400683 fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
684 fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
Hal Canarye03c3e52017-03-09 11:33:35 -0500685 SkScalar scale;
686 bool useCropRect;
687 fuzz->next(&scale, &useCropRect);
688 SkImageFilter::CropRect cropRect;
689 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400690 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500691 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500692 sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
693 sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500694 return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
695 std::move(displacement), std::move(color),
696 useCropRect ? &cropRect : nullptr);
697 }
698 case 8: {
699 SkScalar dx, dy, sigmaX, sigmaY;
700 SkColor color;
701 SkDropShadowImageFilter::ShadowMode shadowMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500702 fuzz_enum_range(fuzz, &shadowMode, 0, 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500703 bool useCropRect;
704 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
705 SkImageFilter::CropRect cropRect;
706 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400707 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500708 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500709 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500710 return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
711 std::move(input),
712 useCropRect ? &cropRect : nullptr);
713 }
714 case 9:
Hal Canary1e0138b2017-03-10 13:56:08 -0500715 return SkImageSource::Make(make_fuzz_image(fuzz));
Hal Canarye03c3e52017-03-09 11:33:35 -0500716 case 10: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500717 sk_sp<SkImage> image = make_fuzz_image(fuzz);
Hal Canarye03c3e52017-03-09 11:33:35 -0500718 SkRect srcRect, dstRect;
719 SkFilterQuality filterQuality;
720 fuzz->next(&srcRect, &dstRect, &filterQuality);
721 return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
722 }
723 case 11:
724 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
725 case 12: {
726 SkRect srcRect;
727 SkScalar inset;
728 bool useCropRect;
729 SkImageFilter::CropRect cropRect;
730 fuzz->next(&srcRect, &inset, &useCropRect);
731 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400732 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500733 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500734 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500735 return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
736 useCropRect ? &cropRect : nullptr);
737 }
738 case 13: {
739 constexpr int kMaxKernelSize = 5;
740 int32_t n, m;
741 fuzz->nextRange(&n, 1, kMaxKernelSize);
742 fuzz->nextRange(&m, 1, kMaxKernelSize);
743 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
744 fuzz->nextN(kernel, n * m);
745 int32_t offsetX, offsetY;
746 fuzz->nextRange(&offsetX, 0, n - 1);
747 fuzz->nextRange(&offsetY, 0, m - 1);
748 SkScalar gain, bias;
749 bool convolveAlpha, useCropRect;
750 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
751 SkMatrixConvolutionImageFilter::TileMode tileMode;
Hal Canaryf7005202017-03-10 08:48:28 -0500752 fuzz_enum_range(fuzz, &tileMode, 0, 2);
Hal Canarye03c3e52017-03-09 11:33:35 -0500753 SkImageFilter::CropRect cropRect;
754 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400755 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500756 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500757 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500758 return SkMatrixConvolutionImageFilter::Make(
759 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
760 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
761 }
762 case 14: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500763 sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
764 sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500765 bool useCropRect;
766 fuzz->next(&useCropRect);
767 SkImageFilter::CropRect cropRect;
768 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400769 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500770 }
Mike Reed0bdaf052017-06-18 23:35:57 -0400771 return SkMergeImageFilter::Make(std::move(first), std::move(second),
772 useCropRect ? &cropRect : nullptr);
773 }
774 case 15: {
775 constexpr int kMaxCount = 4;
776 sk_sp<SkImageFilter> ifs[kMaxCount];
777 int count;
778 fuzz->nextRange(&count, 1, kMaxCount);
779 for (int i = 0; i < count; ++i) {
780 ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
781 }
782 bool useCropRect;
783 fuzz->next(&useCropRect);
784 SkImageFilter::CropRect cropRect;
785 if (useCropRect) {
786 fuzz->next(&cropRect);
787 }
788 return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
Hal Canarye03c3e52017-03-09 11:33:35 -0500789 }
790 case 16: {
791 int rx, ry;
792 fuzz->next(&rx, &ry);
793 bool useCropRect;
794 fuzz->next(&useCropRect);
795 SkImageFilter::CropRect cropRect;
796 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400797 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500798 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500799 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500800 return SkDilateImageFilter::Make(rx, ry, std::move(input),
801 useCropRect ? &cropRect : nullptr);
802 }
803 case 17: {
804 int rx, ry;
805 fuzz->next(&rx, &ry);
806 bool useCropRect;
807 fuzz->next(&useCropRect);
808 SkImageFilter::CropRect cropRect;
809 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400810 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500811 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500812 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500813 return SkErodeImageFilter::Make(rx, ry, std::move(input),
814 useCropRect ? &cropRect : nullptr);
815 }
816 case 18: {
817 SkScalar dx, dy;
818 fuzz->next(&dx, &dy);
819 bool useCropRect;
820 fuzz->next(&useCropRect);
821 SkImageFilter::CropRect cropRect;
822 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400823 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500824 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500825 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500826 return SkOffsetImageFilter::Make(dx, dy, std::move(input),
827 useCropRect ? &cropRect : nullptr);
828 }
829 case 19: {
830 SkPaint paint;
Hal Canary1e0138b2017-03-10 13:56:08 -0500831 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500832 bool useCropRect;
833 fuzz->next(&useCropRect);
834 SkImageFilter::CropRect cropRect;
835 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400836 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500837 }
838 return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
839 }
840 case 20: {
Hal Canary1e0138b2017-03-10 13:56:08 -0500841 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500842 return SkPictureImageFilter::Make(std::move(picture));
843 }
844 case 21: {
845 SkRect cropRect;
846 fuzz->next(&cropRect);
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), cropRect);
849 }
850 case 22: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500851 SkRect src, dst;
852 fuzz->next(&src, &dst);
Hal Canary1e0138b2017-03-10 13:56:08 -0500853 sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500854 return SkTileImageFilter::Make(src, dst, std::move(input));
855 }
Mike Reed77e487d2017-11-09 21:50:20 +0000856 case 23: {
Hal Canarye03c3e52017-03-09 11:33:35 -0500857 SkBlendMode blendMode;
858 bool useCropRect;
859 fuzz->next(&useCropRect, &blendMode);
860 SkImageFilter::CropRect cropRect;
861 if (useCropRect) {
Kevin Lubickdad29a02017-03-14 17:20:24 -0400862 fuzz->next(&cropRect);
Hal Canarye03c3e52017-03-09 11:33:35 -0500863 }
Hal Canary1e0138b2017-03-10 13:56:08 -0500864 sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
865 sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canarye03c3e52017-03-09 11:33:35 -0500866 return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
867 useCropRect ? &cropRect : nullptr);
868 }
869 default:
870 SkASSERT(false);
871 return nullptr;
872 }
873}
Hal Canary24ac42b2017-02-14 13:35:14 -0500874
Hal Canary1e0138b2017-03-10 13:56:08 -0500875static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500876 int w, h;
877 fuzz->nextRange(&w, 1, 1024);
878 fuzz->nextRange(&h, 1, 1024);
879 SkAutoTMalloc<SkPMColor> data(w * h);
880 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
881 int n = w * h;
882 for (int i = 0; i < n; ++i) {
883 SkColor c;
884 fuzz->next(&c);
885 data[i] = SkPreMultiplyColor(c);
886 }
887 (void)data.release();
Hal Canaryb69c4b82017-03-08 11:02:40 -0500888 return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
889 nullptr);
Hal Canary24ac42b2017-02-14 13:35:14 -0500890}
891
Hal Canary1e0138b2017-03-10 13:56:08 -0500892static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500893 SkBitmap bitmap;
894 int w, h;
895 fuzz->nextRange(&w, 1, 1024);
896 fuzz->nextRange(&h, 1, 1024);
Kevin Lubick1991f552018-02-27 10:59:10 -0500897 if (!bitmap.tryAllocN32Pixels(w, h)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400898 SkDEBUGF("Could not allocate pixels %d x %d", w, h);
Kevin Lubick1991f552018-02-27 10:59:10 -0500899 return bitmap;
900 }
Hal Canary24ac42b2017-02-14 13:35:14 -0500901 for (int y = 0; y < h; ++y) {
902 for (int x = 0; x < w; ++x) {
903 SkColor c;
904 fuzz->next(&c);
905 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
906 }
907 }
908 return bitmap;
909}
910
Hal Canary1e0138b2017-03-10 13:56:08 -0500911template <typename T, typename Min, typename Max>
912inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
913 T value;
914 fuzz_enum_range(fuzz, &value, minv, maxv);
915 return value;
916}
917
918static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -0500919 if (!fuzz || !paint || depth <= 0) {
920 return;
921 }
922
Hal Canary1e0138b2017-03-10 13:56:08 -0500923 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
924 paint->setDither( make_fuzz_t<bool>(fuzz));
925 paint->setColor( make_fuzz_t<SkColor>(fuzz));
926 paint->setBlendMode( make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
927 paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
928 paint->setStyle( make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
929 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
930 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
931 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
932 paint->setImageFilter( make_fuzz_imageFilter(fuzz, depth - 1));
933 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
Hal Canary24ac42b2017-02-14 13:35:14 -0500934
935 if (paint->getStyle() != SkPaint::kFill_Style) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500936 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
937 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
938 paint->setStrokeCap( make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
939 paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
Hal Canary24ac42b2017-02-14 13:35:14 -0500940 }
941}
942
Hal Canary1e0138b2017-03-10 13:56:08 -0500943static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
944 paint->setTypeface( make_fuzz_typeface(fuzz));
945 paint->setTextSize( make_fuzz_t<SkScalar>(fuzz));
946 paint->setTextScaleX( make_fuzz_t<SkScalar>(fuzz));
947 paint->setTextSkewX( make_fuzz_t<SkScalar>(fuzz));
948 paint->setLinearText( make_fuzz_t<bool>(fuzz));
949 paint->setSubpixelText( make_fuzz_t<bool>(fuzz));
950 paint->setLCDRenderText( make_fuzz_t<bool>(fuzz));
951 paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
952 paint->setAutohinted( make_fuzz_t<bool>(fuzz));
953 paint->setVerticalText( make_fuzz_t<bool>(fuzz));
954 paint->setFakeBoldText( make_fuzz_t<bool>(fuzz));
955 paint->setDevKernText( make_fuzz_t<bool>(fuzz));
956 paint->setHinting( make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
957 SkPaint::kFull_Hinting));
958 paint->setTextAlign( make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
Hal Canary5395c592017-03-08 16:52:18 -0500959}
960
961static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
Hal Canary1e0138b2017-03-10 13:56:08 -0500962 paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
Hal Canary24ac42b2017-02-14 13:35:14 -0500963}
964
Hal Canary5395c592017-03-08 16:52:18 -0500965constexpr int kMaxGlyphCount = 30;
966
Hal Canary1e0138b2017-03-10 13:56:08 -0500967static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
Hal Canary671e4422017-02-27 13:36:38 -0500968 SkTDArray<uint8_t> array;
969 if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -0500970 int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
971 : SkTypeface::MakeDefault()->countGlyphs();
Kevin Lubick1991f552018-02-27 10:59:10 -0500972 if (glyphRange == 0) {
973 // Some fuzzing environments have no fonts, so empty array is the best
974 // we can do.
975 return array;
976 }
Hal Canary671e4422017-02-27 13:36:38 -0500977 int glyphCount;
Hal Canary5395c592017-03-08 16:52:18 -0500978 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
Hal Canary671e4422017-02-27 13:36:38 -0500979 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
980 for (int i = 0; i < glyphCount; ++i) {
981 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
982 }
983 return array;
984 }
985 static const SkUnichar ranges[][2] = {
986 {0x0020, 0x007F},
987 {0x00A1, 0x0250},
988 {0x0400, 0x0500},
989 };
990 int32_t count = 0;
Hal Canaryb69c4b82017-03-08 11:02:40 -0500991 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -0500992 count += (ranges[i][1] - ranges[i][0]);
993 }
Hal Canary5395c592017-03-08 16:52:18 -0500994 constexpr int kMaxLength = kMaxGlyphCount;
Hal Canary671e4422017-02-27 13:36:38 -0500995 SkUnichar buffer[kMaxLength];
996 int length;
997 fuzz->nextRange(&length, 1, kMaxLength);
998 for (int j = 0; j < length; ++j) {
999 int32_t value;
1000 fuzz->nextRange(&value, 0, count - 1);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001001 for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
Hal Canary671e4422017-02-27 13:36:38 -05001002 if (value + ranges[i][0] < ranges[i][1]) {
1003 buffer[j] = value + ranges[i][0];
1004 break;
1005 } else {
1006 value -= (ranges[i][1] - ranges[i][0]);
1007 }
1008 }
1009 }
1010 switch (paint.getTextEncoding()) {
Hal Canaryb69c4b82017-03-08 11:02:40 -05001011 case SkPaint::kUTF8_TextEncoding: {
1012 size_t utf8len = 0;
1013 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -04001014 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
Hal Canary671e4422017-02-27 13:36:38 -05001015 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001016 char* ptr = (char*)array.append(utf8len);
1017 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -04001018 ptr += SkUTF::ToUTF8(buffer[j], ptr);
Hal Canary671e4422017-02-27 13:36:38 -05001019 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001020 } break;
1021 case SkPaint::kUTF16_TextEncoding: {
1022 size_t utf16len = 0;
1023 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -04001024 utf16len += SkUTF::ToUTF16(buffer[j]);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001025 }
1026 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
1027 for (int j = 0; j < length; ++j) {
Hal Canaryf107a2f2018-07-25 16:52:48 -04001028 ptr += SkUTF::ToUTF16(buffer[j], ptr);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001029 }
1030 } break;
Hal Canary671e4422017-02-27 13:36:38 -05001031 case SkPaint::kUTF32_TextEncoding:
1032 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
Hal Canaryc1a70e22017-03-01 15:40:46 -05001033 break;
Hal Canary671e4422017-02-27 13:36:38 -05001034 default:
Hal Canaryb69c4b82017-03-08 11:02:40 -05001035 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001036 break;
Hal Canary671e4422017-02-27 13:36:38 -05001037 }
1038 return array;
1039}
1040
Hal Canary5395c592017-03-08 16:52:18 -05001041static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
1042 SkTextBlobBuilder textBlobBuilder;
1043 int8_t runCount;
1044 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
1045 while (runCount-- > 0) {
1046 SkPaint paint;
1047 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary1e0138b2017-03-10 13:56:08 -05001048 paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
Hal Canary5395c592017-03-08 16:52:18 -05001049 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1050 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1051 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1052 SkASSERT(glyphCount <= kMaxGlyphCount);
1053 SkScalar x, y;
1054 const SkTextBlobBuilder::RunBuffer* buffer;
1055 uint8_t runType;
1056 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
1057 switch (runType) {
1058 case 0:
1059 fuzz->next(&x, &y);
1060 // TODO: Test other variations of this.
1061 buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
1062 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1063 break;
1064 case 1:
1065 fuzz->next(&y);
1066 // TODO: Test other variations of this.
1067 buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
1068 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1069 fuzz->nextN(buffer->pos, glyphCount);
1070 break;
1071 case 2:
1072 // TODO: Test other variations of this.
1073 buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
1074 memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
1075 fuzz->nextN(buffer->pos, glyphCount * 2);
1076 break;
1077 default:
1078 SkASSERT(false);
Kevin Lubick54f20e02018-01-11 14:50:21 -05001079 break;
Hal Canary5395c592017-03-08 16:52:18 -05001080 }
1081 }
1082 return textBlobBuilder.make();
1083}
1084
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001085extern std::atomic<bool> gSkUseDeltaAA;
1086extern std::atomic<bool> gSkForceDeltaAA;
1087
Hal Canary1e0138b2017-03-10 13:56:08 -05001088static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001089 if (!fuzz || !canvas || depth <= 0) {
1090 return;
1091 }
1092 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
Kevin Lubickd2ae8dc2018-06-01 13:16:20 -04001093 bool useDAA;
1094 fuzz->next(&useDAA);
1095 if (useDAA) {
1096 gSkForceDeltaAA = true;
1097 gSkUseDeltaAA = true;
1098 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001099 unsigned N;
1100 fuzz->nextRange(&N, 0, 2000);
1101 for (unsigned i = 0; i < N; ++i) {
1102 if (fuzz->exhausted()) {
1103 return;
1104 }
1105 SkPaint paint;
1106 SkMatrix matrix;
1107 unsigned drawCommand;
Hal Canary5af600e2017-03-09 14:10:36 -05001108 fuzz->nextRange(&drawCommand, 0, 53);
Hal Canary24ac42b2017-02-14 13:35:14 -05001109 switch (drawCommand) {
1110 case 0:
1111 canvas->flush();
1112 break;
1113 case 1:
1114 canvas->save();
1115 break;
1116 case 2: {
1117 SkRect bounds;
1118 fuzz->next(&bounds);
Hal Canary1e0138b2017-03-10 13:56:08 -05001119 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001120 canvas->saveLayer(&bounds, &paint);
1121 break;
1122 }
1123 case 3: {
1124 SkRect bounds;
1125 fuzz->next(&bounds);
1126 canvas->saveLayer(&bounds, nullptr);
1127 break;
1128 }
1129 case 4:
Hal Canary1e0138b2017-03-10 13:56:08 -05001130 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001131 canvas->saveLayer(nullptr, &paint);
1132 break;
1133 case 5:
1134 canvas->saveLayer(nullptr, nullptr);
1135 break;
1136 case 6: {
1137 uint8_t alpha;
1138 fuzz->next(&alpha);
1139 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1140 break;
1141 }
1142 case 7: {
1143 SkRect bounds;
1144 uint8_t alpha;
1145 fuzz->next(&bounds, &alpha);
1146 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1147 break;
1148 }
1149 case 8: {
1150 SkCanvas::SaveLayerRec saveLayerRec;
1151 SkRect bounds;
Hal Canary1e0138b2017-03-10 13:56:08 -05001152 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001153 fuzz->next(&bounds);
1154 saveLayerRec.fBounds = &bounds;
1155 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001156 if (make_fuzz_t<bool>(fuzz)) {
1157 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001158 saveLayerRec.fPaint = &paint;
1159 }
1160 sk_sp<SkImageFilter> imageFilter;
Hal Canary1e0138b2017-03-10 13:56:08 -05001161 if (make_fuzz_t<bool>(fuzz)) {
1162 imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001163 saveLayerRec.fBackdrop = imageFilter.get();
1164 }
Hal Canary5395c592017-03-08 16:52:18 -05001165 // _DumpCanvas can't handle this.
Hal Canary1e0138b2017-03-10 13:56:08 -05001166 // if (make_fuzz_t<bool>(fuzz)) {
Hal Canary5395c592017-03-08 16:52:18 -05001167 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1168 // }
1169
Hal Canary24ac42b2017-02-14 13:35:14 -05001170 canvas->saveLayer(saveLayerRec);
1171 break;
1172 }
1173 case 9:
1174 canvas->restore();
1175 break;
1176 case 10: {
1177 int saveCount;
1178 fuzz->next(&saveCount);
1179 canvas->restoreToCount(saveCount);
1180 break;
1181 }
1182 case 11: {
1183 SkScalar x, y;
1184 fuzz->next(&x, &y);
1185 canvas->translate(x, y);
1186 break;
1187 }
1188 case 12: {
1189 SkScalar x, y;
1190 fuzz->next(&x, &y);
1191 canvas->scale(x, y);
1192 break;
1193 }
1194 case 13: {
1195 SkScalar v;
1196 fuzz->next(&v);
1197 canvas->rotate(v);
1198 break;
1199 }
1200 case 14: {
1201 SkScalar x, y, v;
1202 fuzz->next(&x, &y, &v);
1203 canvas->rotate(v, x, y);
1204 break;
1205 }
1206 case 15: {
1207 SkScalar x, y;
1208 fuzz->next(&x, &y);
1209 canvas->skew(x, y);
1210 break;
1211 }
1212 case 16: {
1213 SkMatrix mat;
1214 fuzz->next(&mat);
1215 canvas->concat(mat);
1216 break;
1217 }
1218 case 17: {
1219 SkMatrix mat;
1220 fuzz->next(&mat);
1221 canvas->setMatrix(mat);
1222 break;
1223 }
1224 case 18:
1225 canvas->resetMatrix();
1226 break;
1227 case 19: {
1228 SkRect r;
1229 int op;
1230 bool doAntiAlias;
1231 fuzz->next(&r, &doAntiAlias);
1232 fuzz->nextRange(&op, 0, 1);
1233 r.sort();
1234 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1235 break;
1236 }
1237 case 20: {
1238 SkRRect rr;
1239 int op;
1240 bool doAntiAlias;
1241 fuzz->next(&rr);
1242 fuzz->next(&doAntiAlias);
1243 fuzz->nextRange(&op, 0, 1);
1244 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1245 break;
1246 }
1247 case 21: {
1248 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001249 FuzzPath(fuzz, &path, 30);
Hal Canary24ac42b2017-02-14 13:35:14 -05001250 int op;
1251 bool doAntiAlias;
1252 fuzz->next(&doAntiAlias);
1253 fuzz->nextRange(&op, 0, 1);
1254 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1255 break;
1256 }
1257 case 22: {
1258 SkRegion region;
Hal Canary24ac42b2017-02-14 13:35:14 -05001259 int op;
Hal Canarye03c3e52017-03-09 11:33:35 -05001260 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001261 fuzz->nextRange(&op, 0, 1);
1262 canvas->clipRegion(region, (SkClipOp)op);
1263 break;
1264 }
1265 case 23:
Hal Canary1e0138b2017-03-10 13:56:08 -05001266 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001267 canvas->drawPaint(paint);
1268 break;
1269 case 24: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001270 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001271 SkCanvas::PointMode pointMode;
1272 fuzz_enum_range(fuzz, &pointMode,
1273 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001274 size_t count;
1275 constexpr int kMaxCount = 30;
1276 fuzz->nextRange(&count, 0, kMaxCount);
1277 SkPoint pts[kMaxCount];
1278 fuzz->nextN(pts, count);
Hal Canaryc8bebd42017-12-20 11:21:05 -05001279 canvas->drawPoints(pointMode, count, pts, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001280 break;
1281 }
1282 case 25: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001283 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001284 SkRect r;
1285 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001286 if (!r.isFinite()) {
1287 break;
1288 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001289 canvas->drawRect(r, paint);
1290 break;
1291 }
1292 case 26: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001293 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001294 SkRegion region;
Hal Canarye03c3e52017-03-09 11:33:35 -05001295 fuzz->next(&region);
Hal Canary24ac42b2017-02-14 13:35:14 -05001296 canvas->drawRegion(region, paint);
1297 break;
1298 }
1299 case 27: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001300 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001301 SkRect r;
1302 fuzz->next(&r);
Kevin Lubickc5f04272018-04-05 12:54:00 -04001303 if (!r.isFinite()) {
1304 break;
1305 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001306 canvas->drawOval(r, paint);
1307 break;
1308 }
Mike Reed9cec1bc2018-01-19 12:57:01 -05001309 case 28: break; // must have deleted this some time earlier
Hal Canary24ac42b2017-02-14 13:35:14 -05001310 case 29: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001311 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001312 SkRRect rr;
1313 fuzz->next(&rr);
1314 canvas->drawRRect(rr, paint);
1315 break;
1316 }
1317 case 30: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001318 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001319 SkRRect orr, irr;
1320 fuzz->next(&orr);
1321 fuzz->next(&irr);
Hal Canary27bece82017-03-07 16:23:20 -05001322 if (orr.getBounds().contains(irr.getBounds())) {
1323 canvas->drawDRRect(orr, irr, paint);
1324 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001325 break;
1326 }
1327 case 31: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001328 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001329 SkRect r;
1330 SkScalar start, sweep;
1331 bool useCenter;
1332 fuzz->next(&r, &start, &sweep, &useCenter);
1333 canvas->drawArc(r, start, sweep, useCenter, paint);
1334 break;
1335 }
1336 case 32: {
Kevin Lubick0938ce72018-05-21 21:17:15 -04001337 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001338 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001339 FuzzPath(fuzz, &path, 60);
Hal Canary24ac42b2017-02-14 13:35:14 -05001340 canvas->drawPath(path, paint);
1341 break;
1342 }
1343 case 33: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001344 sk_sp<SkImage> img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001345 SkScalar left, top;
1346 bool usePaint;
1347 fuzz->next(&left, &top, &usePaint);
1348 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001349 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001350 }
1351 canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
1352 break;
1353 }
1354 case 34: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001355 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001356 SkRect src, dst;
1357 bool usePaint;
1358 fuzz->next(&src, &dst, &usePaint);
1359 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001360 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001361 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001362 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001363 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1364 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001365 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1366 break;
1367 }
1368 case 35: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001369 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001370 SkIRect src;
1371 SkRect dst;
1372 bool usePaint;
1373 fuzz->next(&src, &dst, &usePaint);
1374 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001375 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001376 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001377 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001378 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1379 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001380 canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1381 break;
1382 }
1383 case 36: {
1384 bool usePaint;
Hal Canary1e0138b2017-03-10 13:56:08 -05001385 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001386 SkRect dst;
1387 fuzz->next(&dst, &usePaint);
1388 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001389 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001390 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001391 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001392 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1393 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001394 canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
1395 break;
1396 }
1397 case 37: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001398 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001399 SkIRect center;
1400 SkRect dst;
1401 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001402 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001403 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001404 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001405 }
Hal Canary0361d492017-03-15 12:58:15 -04001406 if (make_fuzz_t<bool>(fuzz)) {
1407 fuzz->next(&center);
1408 } else { // Make valid center, see SkLatticeIter::Valid().
1409 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1410 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1411 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1412 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1413 }
1414 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001415 canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
1416 break;
1417 }
1418 case 38: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001419 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001420 SkScalar left, top;
1421 bool usePaint;
1422 fuzz->next(&left, &top, &usePaint);
1423 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001424 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001425 }
1426 canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
1427 break;
1428 }
1429 case 39: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001430 SkBitmap bitmap = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001431 SkRect src, dst;
1432 bool usePaint;
1433 fuzz->next(&src, &dst, &usePaint);
1434 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001435 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001436 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001437 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001438 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1439 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001440 canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
1441 break;
1442 }
1443 case 40: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001444 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001445 SkIRect src;
1446 SkRect dst;
1447 bool usePaint;
1448 fuzz->next(&src, &dst, &usePaint);
1449 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001450 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001451 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001452 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001453 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1454 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001455 canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
1456 break;
1457 }
1458 case 41: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001459 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001460 SkRect dst;
1461 bool usePaint;
1462 fuzz->next(&dst, &usePaint);
1463 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001464 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001465 }
Hal Canaryb69c4b82017-03-08 11:02:40 -05001466 SkCanvas::SrcRectConstraint constraint =
Hal Canary1e0138b2017-03-10 13:56:08 -05001467 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1468 : SkCanvas::kFast_SrcRectConstraint;
Hal Canary24ac42b2017-02-14 13:35:14 -05001469 canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
1470 break;
1471 }
1472 case 42: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001473 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001474 SkIRect center;
1475 SkRect dst;
1476 bool usePaint;
Hal Canary0361d492017-03-15 12:58:15 -04001477 fuzz->next(&usePaint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001478 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001479 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001480 }
Hal Canary0361d492017-03-15 12:58:15 -04001481 if (make_fuzz_t<bool>(fuzz)) {
1482 fuzz->next(&center);
1483 } else { // Make valid center, see SkLatticeIter::Valid().
Kevin Lubick1991f552018-02-27 10:59:10 -05001484 if (img.width() == 0 || img.height() == 0) {
1485 // bitmap may not have had its pixels initialized.
1486 break;
1487 }
Hal Canary0361d492017-03-15 12:58:15 -04001488 fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
1489 fuzz->nextRange(&center.fTop, 0, img.height() - 1);
1490 fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
1491 fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
1492 }
1493 fuzz->next(&dst);
Hal Canary24ac42b2017-02-14 13:35:14 -05001494 canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
1495 break;
1496 }
1497 case 43: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001498 SkBitmap img = make_fuzz_bitmap(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001499 bool usePaint;
1500 SkRect dst;
1501 fuzz->next(&usePaint, &dst);
1502 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001503 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001504 }
1505 constexpr int kMax = 6;
1506 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001507 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001508 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1509 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1510 fuzz->nextN(xDivs, lattice.fXCount);
1511 fuzz->nextN(yDivs, lattice.fYCount);
1512 canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
1513 break;
1514 }
1515 case 44: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001516 auto img = make_fuzz_image(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001517 bool usePaint;
1518 SkRect dst;
1519 fuzz->next(&usePaint, &dst);
1520 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001521 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001522 }
1523 constexpr int kMax = 6;
1524 int xDivs[kMax], yDivs[kMax];
Stan Ilievca8c0952017-12-11 13:01:58 -05001525 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
Hal Canary24ac42b2017-02-14 13:35:14 -05001526 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1527 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1528 fuzz->nextN(xDivs, lattice.fXCount);
1529 fuzz->nextN(yDivs, lattice.fYCount);
1530 canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
1531 break;
1532 }
1533 case 45: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001534 fuzz_paint(fuzz, &paint, depth - 1);
1535 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001536 fuzz_paint_text_encoding(fuzz, &paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001537 SkScalar x, y;
1538 fuzz->next(&x, &y);
Hal Canary5395c592017-03-08 16:52:18 -05001539 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001540 canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001541 break;
1542 }
1543 case 46: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001544 fuzz_paint(fuzz, &paint, depth - 1);
1545 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001546 fuzz_paint_text_encoding(fuzz, &paint);
1547 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001548 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001549 if (glyphCount < 1) {
1550 break;
1551 }
1552 SkAutoTMalloc<SkPoint> pos(glyphCount);
1553 SkAutoTMalloc<SkScalar> widths(glyphCount);
Hal Canary671e4422017-02-27 13:36:38 -05001554 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001555 pos[0] = {0, 0};
1556 for (int i = 1; i < glyphCount; ++i) {
1557 float y;
Hal Canaryb69c4b82017-03-08 11:02:40 -05001558 fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001559 pos[i] = {pos[i - 1].x() + widths[i - 1], y};
1560 }
Hal Canary671e4422017-02-27 13:36:38 -05001561 canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001562 break;
1563 }
1564 case 47: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001565 fuzz_paint(fuzz, &paint, depth - 1);
1566 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001567 fuzz_paint_text_encoding(fuzz, &paint);
1568 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary671e4422017-02-27 13:36:38 -05001569 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
Hal Canary24ac42b2017-02-14 13:35:14 -05001570 SkAutoTMalloc<SkScalar> widths(glyphCount);
1571 if (glyphCount < 1) {
1572 break;
1573 }
Hal Canary671e4422017-02-27 13:36:38 -05001574 paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001575 SkScalar x = widths[0];
1576 for (int i = 0; i < glyphCount; ++i) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001577 using std::swap;
1578 swap(x, widths[i]);
Hal Canary24ac42b2017-02-14 13:35:14 -05001579 x += widths[i];
1580 SkScalar offset;
1581 fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
Hal Canaryb69c4b82017-03-08 11:02:40 -05001582 0.125f * paint.getTextSize());
Hal Canary24ac42b2017-02-14 13:35:14 -05001583 widths[i] += offset;
1584 }
1585 SkScalar y;
1586 fuzz->next(&y);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001587 canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001588 break;
1589 }
1590 case 48: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001591 fuzz_paint(fuzz, &paint, depth - 1);
1592 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001593 fuzz_paint_text_encoding(fuzz, &paint);
1594 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001595 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001596 FuzzPath(fuzz, &path, 20);
Hal Canary24ac42b2017-02-14 13:35:14 -05001597 SkScalar hOffset, vOffset;
1598 fuzz->next(&hOffset, &vOffset);
Hal Canaryb69c4b82017-03-08 11:02:40 -05001599 canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
1600 vOffset, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001601 break;
1602 }
1603 case 49: {
1604 SkMatrix matrix;
Hal Canary1e0138b2017-03-10 13:56:08 -05001605 bool useMatrix = make_fuzz_t<bool>(fuzz);
Hal Canary24ac42b2017-02-14 13:35:14 -05001606 if (useMatrix) {
1607 fuzz->next(&matrix);
1608 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001609 fuzz_paint(fuzz, &paint, depth - 1);
1610 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001611 fuzz_paint_text_encoding(fuzz, &paint);
1612 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001613 SkPath path;
Cary Clark91390c82018-03-09 14:02:46 -05001614 FuzzPath(fuzz, &path, 20);
Hal Canary671e4422017-02-27 13:36:38 -05001615 canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
Hal Canary24ac42b2017-02-14 13:35:14 -05001616 useMatrix ? &matrix : nullptr, paint);
1617 break;
1618 }
1619 case 50: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001620 fuzz_paint(fuzz, &paint, depth - 1);
1621 fuzz_paint_text(fuzz, &paint);
Hal Canary5395c592017-03-08 16:52:18 -05001622 fuzz_paint_text_encoding(fuzz, &paint);
1623 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
1624 SkRSXform rSXform[kMaxGlyphCount];
1625 int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
1626 SkASSERT(glyphCount <= kMaxGlyphCount);
1627 fuzz->nextN(rSXform, glyphCount);
1628 SkRect cullRect;
1629 bool useCullRect;
1630 fuzz->next(&useCullRect);
1631 if (useCullRect) {
1632 fuzz->next(&cullRect);
1633 }
1634 canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
1635 useCullRect ? &cullRect : nullptr, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001636 break;
1637 }
1638 case 51: {
Hal Canary5395c592017-03-08 16:52:18 -05001639 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
Hal Canary1e0138b2017-03-10 13:56:08 -05001640 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary5395c592017-03-08 16:52:18 -05001641 SkScalar x, y;
1642 fuzz->next(&x, &y);
1643 canvas->drawTextBlob(blob, x, y, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001644 break;
1645 }
1646 case 52: {
1647 bool usePaint, useMatrix;
1648 fuzz->next(&usePaint, &useMatrix);
1649 if (usePaint) {
Hal Canary1e0138b2017-03-10 13:56:08 -05001650 fuzz_paint(fuzz, &paint, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001651 }
1652 if (useMatrix) {
1653 fuzz->next(&matrix);
1654 }
Hal Canary1e0138b2017-03-10 13:56:08 -05001655 auto pic = make_fuzz_picture(fuzz, depth - 1);
Hal Canary24ac42b2017-02-14 13:35:14 -05001656 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1657 usePaint ? &paint : nullptr);
1658 break;
1659 }
1660 case 53: {
Hal Canary1e0138b2017-03-10 13:56:08 -05001661 fuzz_paint(fuzz, &paint, depth - 1);
Mike Reed887cdf12017-04-03 11:11:09 -04001662 SkVertices::VertexMode vertexMode;
Hal Canary5af600e2017-03-09 14:10:36 -05001663 SkBlendMode blendMode;
Mike Reed887cdf12017-04-03 11:11:09 -04001664 fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
Hal Canary5af600e2017-03-09 14:10:36 -05001665 fuzz->next(&blendMode);
Hal Canary24ac42b2017-02-14 13:35:14 -05001666 constexpr int kMaxCount = 100;
1667 int vertexCount;
1668 SkPoint vertices[kMaxCount];
1669 SkPoint texs[kMaxCount];
1670 SkColor colors[kMaxCount];
Hal Canary24ac42b2017-02-14 13:35:14 -05001671 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1672 fuzz->nextN(vertices, vertexCount);
1673 bool useTexs, useColors;
1674 fuzz->next(&useTexs, &useColors);
1675 if (useTexs) {
1676 fuzz->nextN(texs, vertexCount);
1677 }
1678 if (useColors) {
1679 fuzz->nextN(colors, vertexCount);
1680 }
1681 int indexCount = 0;
Hal Canary68b9b572017-03-02 15:27:23 -05001682 uint16_t indices[kMaxCount * 2];
Hal Canary1e0138b2017-03-10 13:56:08 -05001683 if (make_fuzz_t<bool>(fuzz)) {
Hal Canary68b9b572017-03-02 15:27:23 -05001684 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1685 for (int i = 0; i < indexCount; ++i) {
1686 fuzz->nextRange(&indices[i], 0, vertexCount - 1);
1687 }
Hal Canary24ac42b2017-02-14 13:35:14 -05001688 }
Mike Reed887cdf12017-04-03 11:11:09 -04001689 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1690 useTexs ? texs : nullptr,
1691 useColors ? colors : nullptr,
1692 indexCount, indices),
1693 blendMode, paint);
Hal Canary24ac42b2017-02-14 13:35:14 -05001694 break;
1695 }
1696 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -05001697 SkASSERT(false);
Hal Canary24ac42b2017-02-14 13:35:14 -05001698 break;
1699 }
1700 }
1701}
1702
Hal Canary1e0138b2017-03-10 13:56:08 -05001703static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
Hal Canary24ac42b2017-02-14 13:35:14 -05001704 SkScalar w, h;
1705 fuzz->next(&w, &h);
1706 SkPictureRecorder pictureRecorder;
1707 fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1708 return pictureRecorder.finishRecordingAsPicture();
1709}
1710
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001711DEF_FUZZ(NullCanvas, fuzz) {
1712 fuzz_canvas(fuzz, SkMakeNullCanvas().get());
Hal Canary24ac42b2017-02-14 13:35:14 -05001713}
1714
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001715constexpr SkISize kCanvasSize = {128, 160};
Hal Canary44801ca2017-03-15 11:39:06 -04001716
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001717DEF_FUZZ(RasterN32Canvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001718 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick486ee3d2018-03-21 10:17:25 -04001719 if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001720 fuzz_canvas(fuzz, surface->getCanvas());
1721}
1722
Hal Canarye2924432017-12-01 11:46:26 -05001723DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
1724 SkPictureRecorder recorder;
1725 fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
1726 SkIntToScalar(kCanvasSize.height())));
1727 sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
1728 if (!pic) { fuzz->signalBug(); }
1729 sk_sp<SkData> data = pic->serialize();
1730 if (!data) { fuzz->signalBug(); }
Mike Reedfadbfcd2017-12-06 16:09:20 -05001731 SkReadBuffer rb(data->data(), data->size());
Cary Clarkefd99cc2018-06-11 16:25:43 -04001732 auto deserialized = SkPicturePriv::MakeFromBuffer(rb);
Hal Canarye2924432017-12-01 11:46:26 -05001733 if (!deserialized) { fuzz->signalBug(); }
1734 auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
1735 SkASSERT(surface && surface->getCanvas());
1736 surface->getCanvas()->drawPicture(deserialized);
1737}
1738
Kevin Lubickedef8ec2018-01-09 15:32:58 -05001739DEF_FUZZ(ImageFilter, fuzz) {
1740 auto fil = make_fuzz_imageFilter(fuzz, 20);
1741
1742 SkPaint paint;
1743 paint.setImageFilter(fil);
1744 SkBitmap bitmap;
1745 SkCanvas canvas(bitmap);
1746 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1747}
1748
1749
1750//SkRandom _rand;
1751#define SK_ADD_RANDOM_BIT_FLIPS
1752
1753DEF_FUZZ(SerializedImageFilter, fuzz) {
1754 auto filter = make_fuzz_imageFilter(fuzz, 20);
1755 auto data = filter->serialize();
1756 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
1757 size_t len = data->size();
1758#ifdef SK_ADD_RANDOM_BIT_FLIPS
1759 unsigned char* p = const_cast<unsigned char*>(ptr);
1760 for (size_t i = 0; i < len; ++i, ++p) {
1761 uint8_t j;
1762 fuzz->nextRange(&j, 1, 250);
1763 if (j == 1) { // 0.4% of the time, flip a bit or byte
1764 uint8_t k;
1765 fuzz->nextRange(&k, 1, 10);
1766 if (k == 1) { // Then 10% of the time, change a whole byte
1767 uint8_t s;
1768 fuzz->nextRange(&s, 0, 2);
1769 switch(s) {
1770 case 0:
1771 *p ^= 0xFF; // Flip entire byte
1772 break;
1773 case 1:
1774 *p = 0xFF; // Set all bits to 1
1775 break;
1776 case 2:
1777 *p = 0x00; // Set all bits to 0
1778 break;
1779 }
1780 } else {
1781 uint8_t s;
1782 fuzz->nextRange(&s, 0, 7);
1783 *p ^= (1 << 7);
1784 }
1785 }
1786 }
1787#endif // SK_ADD_RANDOM_BIT_FLIPS
1788 auto deserializedFil = SkImageFilter::Deserialize(ptr, len);
1789
1790 // uncomment below to write out a serialized image filter (to make corpus
1791 // for -t filter_fuzz)
1792 // SkString s("./serialized_filters/sf");
1793 // s.appendU32(_rand.nextU());
1794 // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
1795 // sk_fwrite(data->bytes(), data->size(), file);
1796 // sk_fclose(file);
1797
1798 SkPaint paint;
1799 paint.setImageFilter(deserializedFil);
1800 SkBitmap bitmap;
1801 SkCanvas canvas(bitmap);
1802 canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
1803}
1804
Hal Canary44801ca2017-03-15 11:39:06 -04001805#if SK_SUPPORT_GPU
Kevin Lubickfaef5142018-06-07 10:33:11 -04001806
1807static void dump_GPU_info(GrContext* context) {
1808 const GrGLInterface* gl = static_cast<GrGLGpu*>(context->contextPriv().getGpu())
1809 ->glInterface();
1810 const GrGLubyte* output;
1811 GR_GL_CALL_RET(gl, output, GetString(GR_GL_RENDERER));
1812 SkDebugf("GL_RENDERER %s\n", (const char*) output);
1813
1814 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VENDOR));
1815 SkDebugf("GL_VENDOR %s\n", (const char*) output);
1816
1817 GR_GL_CALL_RET(gl, output, GetString(GR_GL_VERSION));
1818 SkDebugf("GL_VERSION %s\n", (const char*) output);
1819}
1820
Hal Canary5aa91582017-03-21 11:11:44 -07001821static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
1822 SkASSERT(context);
1823 auto surface = SkSurface::MakeRenderTarget(
1824 context,
1825 SkBudgeted::kNo,
1826 SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1827 SkASSERT(surface && surface->getCanvas());
1828 fuzz_canvas(fuzz, surface->getCanvas());
1829}
1830
Hal Canary44801ca2017-03-15 11:39:06 -04001831DEF_FUZZ(NativeGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001832 sk_gpu_test::GrContextFactory f;
1833 GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001834 if (!context) {
Hal Canary549be4a2018-01-05 16:59:53 -05001835 context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
Brian Salomon6405e712017-03-20 08:54:16 -04001836 }
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001837 if (FLAGS_gpuInfo) {
Kevin Lubickfaef5142018-06-07 10:33:11 -04001838 dump_GPU_info(context);
Kevin Lubickfe6b4892018-06-05 17:21:30 -04001839 }
Hal Canary5aa91582017-03-21 11:11:44 -07001840 fuzz_ganesh(fuzz, context);
1841}
1842
Kevin Lubick27d42192018-04-03 12:30:32 -04001843// This target is deprecated, NullGLContext is not well maintained.
1844// Please use MockGPUCanvas instead.
Hal Canary5aa91582017-03-21 11:11:44 -07001845DEF_FUZZ(NullGLCanvas, fuzz) {
Hal Canary549be4a2018-01-05 16:59:53 -05001846 sk_gpu_test::GrContextFactory f;
1847 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
Hal Canary5aa91582017-03-21 11:11:44 -07001848}
1849
Kevin Lubick27d42192018-04-03 12:30:32 -04001850DEF_FUZZ(MockGPUCanvas, fuzz) {
Kevin Lubick30709262018-04-02 11:06:41 -04001851 sk_gpu_test::GrContextFactory f;
1852 fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
1853}
Hal Canary44801ca2017-03-15 11:39:06 -04001854#endif
1855
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001856DEF_FUZZ(PDFCanvas, fuzz) {
Hal Canaryfe759302017-08-26 17:06:42 -04001857 SkNullWStream stream;
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001858 auto doc = SkDocument::MakePDF(&stream);
Hal Canary44801ca2017-03-15 11:39:06 -04001859 fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
1860 SkIntToScalar(kCanvasSize.height())));
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001861}
1862
1863// not a "real" thing to fuzz, used to debug errors found while fuzzing.
1864DEF_FUZZ(_DumpCanvas, fuzz) {
Hal Canary44801ca2017-03-15 11:39:06 -04001865 SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
Kevin Lubick1ac8fd22017-03-01 10:42:45 -05001866 fuzz_canvas(fuzz, &debugCanvas);
1867 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
1868 UrlDataManager dataManager(SkString("data"));
1869 Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
1870 Json::StyledStreamWriter(" ").write(std::cout, json);
1871}