Debian Codegraph @

fuzz/FuzzCanvasHelpers.cpp

raw
1 /*
2 * Copyright 2025 Google LLC
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 #include "fuzz/FuzzCanvasHelpers.h"
8
9 #include "fuzz/Fuzz.h"
10 #include "fuzz/FuzzCommon.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkBlurTypes.h"
13 #include "include/core/SkCanvas.h"
14 #include "include/core/SkColorFilter.h"
15 #include "include/core/SkFontMgr.h"
16 #include "include/core/SkImageFilter.h"
17 #include "include/core/SkMaskFilter.h"
18 #include "include/core/SkPathEffect.h"
19 #include "include/core/SkPictureRecorder.h"
20 #include "include/core/SkPoint3.h"
21 #include "include/core/SkRSXform.h"
22 #include "include/core/SkRefCnt.h"
23 #include "include/core/SkRegion.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkTextBlob.h"
26 #include "include/core/SkTypeface.h"
27 #include "include/core/SkVertices.h"
28 #include "include/docs/SkPDFDocument.h"
29 #include "include/docs/SkPDFJpegHelpers.h"
30 #include "include/effects/Sk1DPathEffect.h"
31 #include "include/effects/Sk2DPathEffect.h"
32 #include "include/effects/SkCornerPathEffect.h"
33 #include "include/effects/SkDashPathEffect.h"
34 #include "include/effects/SkDiscretePathEffect.h"
35 #include "include/effects/SkGradient.h"
36 #include "include/effects/SkHighContrastFilter.h"
37 #include "include/effects/SkImageFilters.h"
38 #include "include/effects/SkLumaColorFilter.h"
39 #include "include/effects/SkPerlinNoiseShader.h"
40 #include "include/private/base/SkTo.h"
41 #include "include/svg/SkSVGCanvas.h"
42 #include "include/utils/SkNullCanvas.h"
43 #include "src/base/SkUTF.h"
44 #include "src/core/SkFontPriv.h"
45 #include "src/core/SkOSFile.h"
46 #include "src/core/SkPaintPriv.h"
47 #include "src/core/SkPicturePriv.h"
48 #include "src/core/SkReadBuffer.h"
49 #include "src/utils/SkJSONWriter.h"
50 #include "tools/UrlDataManager.h"
51 #include "tools/debugger/DebugCanvas.h"
52 #include "tools/flags/CommandLineFlags.h"
53 #include "tools/fonts/FontToolUtils.h"
54
55 #include <iostream>
56 #include <utility>
57
58 #if defined(SK_GANESH)
59 #include "include/gpu/ganesh/GrDirectContext.h"
60 #include "src/gpu/ganesh/GrDirectContextPriv.h"
61 #include "tools/ganesh/GrContextFactory.h"
62 #endif
63
64 #if defined(SK_GL)
65 #include "include/gpu/ganesh/gl/GrGLFunctions.h"
66 #include "src/gpu/ganesh/gl/GrGLGpu.h"
67 #include "src/gpu/ganesh/gl/GrGLUtil.h"
68 #endif
69
70 using namespace skia_private;
71
72 // TODO:
73 // SkTextBlob with Unicode
74 // SkImage: more types
75
76 // be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
77 // In fact, all make_fuzz_foo() functions have this potential problem.
78 // Use sequence points!
79 template <typename T>
80 inline T make_fuzz_t(Fuzz* fuzz) {
81 T t;
82 fuzz->next(&t);
83 return t;
84 }
85
86 static sk_sp<SkImage> make_fuzz_image(Fuzz*);
87
88 static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);
89
90 static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
91 if (depth <= 0) {
92 return nullptr;
93 }
94 int colorFilterType;
95 fuzz->nextRange(&colorFilterType, 0, 8);
96 switch (colorFilterType) {
97 case 0:
98 return nullptr;
99 case 1: {
100 SkColor color;
101 SkBlendMode mode;
102 fuzz->next(&color);
103 fuzz->nextEnum(&mode, SkBlendMode::kLastMode);
104 return SkColorFilters::Blend(color, mode);
105 }
106 case 2: {
107 sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
108 if (!outer) {
109 return nullptr;
110 }
111 sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
112 // makeComposed should be able to handle nullptr.
113 return outer->makeComposed(std::move(inner));
114 }
115 case 3: {
116 float array[20];
117 fuzz->nextN(array, std::size(array));
118 return SkColorFilters::Matrix(array);
119 }
120 case 4: {
121 SkColor mul, add;
122 fuzz->next(&mul, &add);
123 return SkColorFilters::Lighting(mul, add);
124 }
125 case 5: {
126 bool grayscale;
127 int invertStyle;
128 float contrast;
129 fuzz->next(&grayscale);
130 fuzz->nextRange(&invertStyle, 0, 2);
131 fuzz->nextRange(&contrast, -1.0f, 1.0f);
132 return SkHighContrastFilter::Make(SkHighContrastConfig(
133 grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
134 }
135 case 6:
136 return SkLumaColorFilter::Make();
137 case 7: {
138 uint8_t table[256];
139 fuzz->nextN(table, std::size(table));
140 return SkColorFilters::Table(table);
141 }
142 case 8: {
143 uint8_t tableA[256];
144 uint8_t tableR[256];
145 uint8_t tableG[256];
146 uint8_t tableB[256];
147 fuzz->nextN(tableA, std::size(tableA));
148 fuzz->nextN(tableR, std::size(tableR));
149 fuzz->nextN(tableG, std::size(tableG));
150 fuzz->nextN(tableB, std::size(tableB));
151 return SkColorFilters::TableARGB(tableA, tableR, tableG, tableB);
152 }
153 default:
154 SkASSERT(false);
155 break;
156 }
157 return nullptr;
158 }
159
160 static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
161 SkScalar totalPos = 0;
162 for (int i = 0; i < colorCount; ++i) {
163 fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
164 totalPos += pos[i];
165 }
166 totalPos = 1.0f / totalPos;
167 for (int i = 0; i < colorCount; ++i) {
168 pos[i] *= totalPos;
169 }
170 // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
171 pos[colorCount - 1] = 1.0f;
172 }
173
174 struct GradStorage {
175 static constexpr size_t kMaxColors = 12;
176 SkColor4f m_colors[kMaxColors];
177 float m_pos[kMaxColors];
178
179 SkGradient grad(size_t count, const SkColor colors[], const float positions[], SkTileMode tm) {
180 std::transform(colors, colors + count, m_colors, [](SkColor c) {
181 return SkColor4f::FromColor(c);
182 });
183 SkSpan<const float> pos;
184 if (positions) {
185 pos = {positions, count};
186 }
187 return {{{m_colors, count}, pos, tm}, {}};
188 }
189 };
190
191 static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
192 sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
193 sk_sp<SkColorFilter> colorFilter(nullptr);
194 SkBitmap bitmap;
195 sk_sp<SkImage> img;
196 SkTileMode tmX, tmY;
197 bool useMatrix;
198 SkColor color;
199 SkMatrix matrix;
200 SkBlendMode blendMode;
201 int shaderType;
202 if (depth <= 0) {
203 return nullptr;
204 }
205 fuzz->nextRange(&shaderType, 0, 14);
206 switch (shaderType) {
207 case 0:
208 return nullptr;
209 case 1:
210 return SkShaders::Empty();
211 case 2:
212 fuzz->next(&color);
213 return SkShaders::Color(color);
214 case 3:
215 img = make_fuzz_image(fuzz);
216 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
217 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
218 fuzz->next(&useMatrix);
219 if (useMatrix) {
220 FuzzNiceMatrix(fuzz, &matrix);
221 }
222 return img->makeShader(tmX, tmY, SkSamplingOptions(), useMatrix ? &matrix : nullptr);
223 case 4:
224 return nullptr;
225 case 5:
226 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
227 FuzzNiceMatrix(fuzz, &matrix);
228 return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
229 case 6:
230 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
231 colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
232 return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
233 case 7:
234 shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
235 shader2 = make_fuzz_shader(fuzz, depth - 1);
236 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
237 return SkShaders::Blend(blendMode, std::move(shader1), std::move(shader2));
238 case 8: {
239 auto pic = make_fuzz_picture(fuzz, depth - 1);
240 bool useTile;
241 SkRect tile;
242 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
243 fuzz->nextEnum(&tmY, SkTileMode::kLastTileMode);
244 fuzz->next(&useMatrix, &useTile);
245 if (useMatrix) {
246 FuzzNiceMatrix(fuzz, &matrix);
247 }
248 if (useTile) {
249 fuzz->next(&tile);
250 }
251 return pic->makeShader(tmX, tmY, SkFilterMode::kNearest,
252 useMatrix ? &matrix : nullptr, useTile ? &tile : nullptr);
253 }
254 // EFFECTS:
255 case 9:
256 // Deprecated SkGaussianEdgeShader
257 return nullptr;
258 case 10: {
259 constexpr int kMaxColors = 12;
260 SkPoint pts[2];
261 SkColor colors[kMaxColors];
262 SkScalar pos[kMaxColors];
263 int colorCount;
264 bool usePos;
265 fuzz->nextN(pts, 2);
266 fuzz->nextRange(&colorCount, 2, kMaxColors);
267 fuzz->nextN(colors, colorCount);
268 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
269 fuzz->next(&useMatrix, &usePos);
270 if (useMatrix) {
271 FuzzNiceMatrix(fuzz, &matrix);
272 }
273 if (usePos) {
274 fuzz_gradient_stops(fuzz, pos, colorCount);
275 }
276 GradStorage storage;
277 auto g = storage.grad(colorCount, colors, usePos ? pos : nullptr, tmX);
278 return SkShaders::LinearGradient(pts, g, useMatrix ? &matrix : nullptr);
279 }
280 case 11: {
281 constexpr int kMaxColors = 12;
282 SkPoint center;
283 SkScalar radius;
284 int colorCount;
285 bool usePos;
286 SkColor colors[kMaxColors];
287 SkScalar pos[kMaxColors];
288 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
289 fuzz->next(&useMatrix, &usePos, &center, &radius);
290 fuzz->nextRange(&colorCount, 2, kMaxColors);
291 fuzz->nextN(colors, colorCount);
292 if (useMatrix) {
293 FuzzNiceMatrix(fuzz, &matrix);
294 }
295 if (usePos) {
296 fuzz_gradient_stops(fuzz, pos, colorCount);
297 }
298 GradStorage storage;
299 auto g = storage.grad(colorCount, colors, usePos ? pos : nullptr, tmX);
300 return SkShaders::RadialGradient(center, radius, g, useMatrix ? &matrix : nullptr);
301 }
302 case 12: {
303 constexpr int kMaxColors = 12;
304 SkPoint start, end;
305 SkScalar startRadius, endRadius;
306 int colorCount;
307 bool usePos;
308 SkColor colors[kMaxColors];
309 SkScalar pos[kMaxColors];
310 fuzz->nextEnum(&tmX, SkTileMode::kLastTileMode);
311 fuzz->next(&useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
312 fuzz->nextRange(&colorCount, 2, kMaxColors);
313 fuzz->nextN(colors, colorCount);
314 if (useMatrix) {
315 FuzzNiceMatrix(fuzz, &matrix);
316 }
317 if (usePos) {
318 fuzz_gradient_stops(fuzz, pos, colorCount);
319 }
320 GradStorage storage;
321 auto g = storage.grad(colorCount, colors, usePos ? pos : nullptr, tmX);
322 return SkShaders::TwoPointConicalGradient(start, startRadius, end, endRadius,
323 g, useMatrix ? &matrix : nullptr);
324 }
325 case 13: {
326 constexpr int kMaxColors = 12;
327 SkScalar cx, cy;
328 int colorCount;
329 bool usePos;
330 SkColor colors[kMaxColors];
331 SkScalar pos[kMaxColors];
332 fuzz->next(&cx, &cy, &useMatrix, &usePos);
333 fuzz->nextRange(&colorCount, 2, kMaxColors);
334 fuzz->nextN(colors, colorCount);
335 if (useMatrix) {
336 FuzzNiceMatrix(fuzz, &matrix);
337 }
338 if (usePos) {
339 fuzz_gradient_stops(fuzz, pos, colorCount);
340 }
341 GradStorage storage;
342 auto g = storage.grad(colorCount, colors, usePos ? pos : nullptr, SkTileMode::kClamp);
343 return SkShaders::SweepGradient({cx, cy}, g, useMatrix ? &matrix : nullptr);
344 }
345 case 14: {
346 SkScalar baseFrequencyX, baseFrequencyY, seed;
347 int numOctaves;
348 SkISize tileSize;
349 bool useTileSize, turbulence;
350 fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
351 if (useTileSize) {
352 fuzz->next(&tileSize);
353 }
354 fuzz->nextRange(&numOctaves, 2, 7);
355 if (turbulence) {
356 return SkShaders::MakeTurbulence(baseFrequencyX,
357 baseFrequencyY,
358 numOctaves,
359 seed,
360 useTileSize ? &tileSize : nullptr);
361 } else {
362 return SkShaders::MakeFractalNoise(baseFrequencyX,
363 baseFrequencyY,
364 numOctaves,
365 seed,
366 useTileSize ? &tileSize : nullptr);
367 }
368 }
369 default:
370 SkDEBUGFAILF("Unknown shader type %d", shaderType);
371 break;
372 }
373 return nullptr;
374 }
375
376 static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
377 if (depth <= 0) {
378 return nullptr;
379 }
380 uint8_t pathEffectType;
381 fuzz->nextRange(&pathEffectType, 0, 8);
382 switch (pathEffectType) {
383 case 0: {
384 return nullptr;
385 }
386 case 1: {
387 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
388 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
389 return SkPathEffect::MakeSum(std::move(first), std::move(second));
390 }
391 case 2: {
392 sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
393 sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
394 return SkPathEffect::MakeCompose(std::move(first), std::move(second));
395 }
396 case 3: {
397 SkPath path = FuzzNicePath(fuzz, 20);
398 SkScalar advance, phase;
399 fuzz->next(&advance, &phase);
400 SkPath1DPathEffect::Style style;
401 fuzz->nextEnum(&style, SkPath1DPathEffect::kLastEnum_Style);
402 return SkPath1DPathEffect::Make(path, advance, phase, style);
403 }
404 case 4: {
405 SkScalar width;
406 SkMatrix matrix;
407 fuzz->next(&width);
408 FuzzNiceMatrix(fuzz, &matrix);
409 return SkLine2DPathEffect::Make(width, matrix);
410 }
411 case 5: {
412 SkPath path = FuzzNicePath(fuzz, 20);
413 SkMatrix matrix;
414 FuzzNiceMatrix(fuzz, &matrix);
415 return SkPath2DPathEffect::Make(matrix, path);
416 }
417 case 6: {
418 SkScalar radius;
419 fuzz->next(&radius);
420 return SkCornerPathEffect::Make(radius);
421 }
422 case 7: {
423 SkScalar phase;
424 fuzz->next(&phase);
425 SkScalar intervals[20];
426 int count;
427 fuzz->nextRange(&count, 0, (int)std::size(intervals));
428 fuzz->nextN(intervals, count);
429 return SkDashPathEffect::Make({intervals, (size_t)count}, phase);
430 }
431 case 8: {
432 SkScalar segLength, dev;
433 uint32_t seed;
434 fuzz->next(&segLength, &dev, &seed);
435 return SkDiscretePathEffect::Make(segLength, dev, seed);
436 }
437 default:
438 SkASSERT(false);
439 return nullptr;
440 }
441 }
442
443 static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
444 int maskfilterType;
445 fuzz->nextRange(&maskfilterType, 0, 1);
446 switch (maskfilterType) {
447 case 0:
448 return nullptr;
449 case 1: {
450 SkBlurStyle blurStyle;
451 fuzz->nextEnum(&blurStyle, kLastEnum_SkBlurStyle);
452 SkScalar sigma;
453 fuzz->next(&sigma);
454 bool respectCTM;
455 fuzz->next(&respectCTM);
456 return SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM);
457 }
458 default:
459 SkASSERT(false);
460 return nullptr;
461 }
462 }
463
464 static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
465 if (make_fuzz_t<bool>(fuzz)) {
466 return nullptr;
467 }
468 sk_sp<SkFontMgr> mgr = ToolUtils::TestFontMgr();
469 int familyCount = mgr->countFamilies();
470 int i, j;
471 fuzz->nextRange(&i, 0, familyCount - 1);
472 sk_sp<SkFontStyleSet> family(mgr->createStyleSet(i));
473 int styleCount = family->count();
474 fuzz->nextRange(&j, 0, styleCount - 1);
475 return sk_sp<SkTypeface>(family->createTypeface(j));
476 }
477
478 static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
479 if (depth <= 0) {
480 return nullptr;
481 }
482 uint8_t imageFilterType;
483 fuzz->nextRange(&imageFilterType, 1, 6);
484 SkPoint3 p, q;
485 SkColor lightColor;
486 SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
487 sk_sp<SkImageFilter> input;
488 SkIRect cropRect;
489 bool useCropRect;
490 fuzz->next(&useCropRect);
491 if (useCropRect) {
492 fuzz->next(&cropRect);
493 }
494 switch (imageFilterType) {
495 case 1:
496 fuzz->next(&p, &lightColor, &surfaceScale, &k);
497 input = MakeFuzzImageFilter(fuzz, depth - 1);
498 return SkImageFilters::DistantLitDiffuse(p, lightColor, surfaceScale, k,
499 std::move(input),
500 useCropRect ? &cropRect : nullptr);
501 case 2:
502 fuzz->next(&p, &lightColor, &surfaceScale, &k);
503 input = MakeFuzzImageFilter(fuzz, depth - 1);
504 return SkImageFilters::PointLitDiffuse(p, lightColor, surfaceScale, k,
505 std::move(input),
506 useCropRect ? &cropRect : nullptr);
507 case 3:
508 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
509 input = MakeFuzzImageFilter(fuzz, depth - 1);
510 return SkImageFilters::SpotLitDiffuse(
511 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
512 std::move(input), useCropRect ? &cropRect : nullptr);
513 case 4:
514 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
515 input = MakeFuzzImageFilter(fuzz, depth - 1);
516 return SkImageFilters::DistantLitSpecular(p, lightColor, surfaceScale, k,
517 shininess, std::move(input),
518 useCropRect ? &cropRect : nullptr);
519 case 5:
520 fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
521 input = MakeFuzzImageFilter(fuzz, depth - 1);
522 return SkImageFilters::PointLitSpecular(p, lightColor, surfaceScale, k,
523 shininess, std::move(input),
524 useCropRect ? &cropRect : nullptr);
525 case 6:
526 fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
527 &shininess);
528 input = MakeFuzzImageFilter(fuzz, depth - 1);
529 return SkImageFilters::SpotLitSpecular(
530 p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
531 std::move(input), useCropRect ? &cropRect : nullptr);
532 default:
533 SkASSERT(false);
534 return nullptr;
535 }
536 }
537
538 static SkSamplingOptions next_sampling(Fuzz* fuzz) {
539 if (fuzz->nextBool()) {
540 float B, C;
541 fuzz->next(&B, &C);
542 return SkSamplingOptions({B, C});
543 } else {
544 SkFilterMode fm;
545 SkMipmapMode mm;
546 fuzz->nextEnum(&fm, SkFilterMode::kLast);
547 fuzz->nextEnum(&mm, SkMipmapMode::kLast);
548 return SkSamplingOptions(fm, mm);
549 }
550 }
551
552 sk_sp<SkImageFilter> MakeFuzzImageFilter(Fuzz* fuzz, int depth) {
553 if (depth <= 0 || fuzz->exhausted()) {
554 return nullptr;
555 }
556 uint8_t imageFilterType;
557 fuzz->nextRange(&imageFilterType, 0, 22);
558 switch (imageFilterType) {
559 case 0:
560 return nullptr;
561 case 1: {
562 SkScalar sigmaX, sigmaY;
563 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
564 bool useCropRect;
565 fuzz->next(&sigmaX, &sigmaY, &useCropRect);
566 SkIRect cropRect;
567 if (useCropRect) {
568 fuzz->next(&cropRect);
569 }
570 return SkImageFilters::Blur(sigmaX, sigmaY, std::move(input),
571 useCropRect ? &cropRect : nullptr);
572 }
573 case 2: {
574 SkMatrix matrix;
575 FuzzNiceMatrix(fuzz, &matrix);
576 const auto sampling = next_sampling(fuzz);
577 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
578 return SkImageFilters::MatrixTransform(matrix, sampling, std::move(input));
579 }
580 case 3: {
581 float k1, k2, k3, k4;
582 bool enforcePMColor;
583 bool useCropRect;
584 fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
585 sk_sp<SkImageFilter> background = MakeFuzzImageFilter(fuzz, depth - 1);
586 sk_sp<SkImageFilter> foreground = MakeFuzzImageFilter(fuzz, depth - 1);
587 SkIRect cropRect;
588 if (useCropRect) {
589 fuzz->next(&cropRect);
590 }
591 return SkImageFilters::Arithmetic(k1, k2, k3, k4, enforcePMColor,
592 std::move(background), std::move(foreground),
593 useCropRect ? &cropRect : nullptr);
594 }
595 case 4: {
596 sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
597 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
598 bool useCropRect;
599 SkIRect cropRect;
600 fuzz->next(&useCropRect);
601 if (useCropRect) {
602 fuzz->next(&cropRect);
603 }
604 return SkImageFilters::ColorFilter(std::move(cf), std::move(input),
605 useCropRect ? &cropRect : nullptr);
606 }
607 case 5: {
608 sk_sp<SkImageFilter> ifo = MakeFuzzImageFilter(fuzz, depth - 1);
609 sk_sp<SkImageFilter> ifi = MakeFuzzImageFilter(fuzz, depth - 1);
610 return SkImageFilters::Compose(std::move(ifo), std::move(ifi));
611 }
612 case 6: {
613 SkColorChannel xChannelSelector, yChannelSelector;
614 fuzz->nextEnum(&xChannelSelector, SkColorChannel::kLastEnum);
615 fuzz->nextEnum(&yChannelSelector, SkColorChannel::kLastEnum);
616 SkScalar scale;
617 bool useCropRect;
618 fuzz->next(&scale, &useCropRect);
619 SkIRect cropRect;
620 if (useCropRect) {
621 fuzz->next(&cropRect);
622 }
623 sk_sp<SkImageFilter> displacement = MakeFuzzImageFilter(fuzz, depth - 1);
624 sk_sp<SkImageFilter> color = MakeFuzzImageFilter(fuzz, depth - 1);
625 return SkImageFilters::DisplacementMap(xChannelSelector, yChannelSelector, scale,
626 std::move(displacement), std::move(color),
627 useCropRect ? &cropRect : nullptr);
628 }
629 case 7: {
630 SkScalar dx, dy, sigmaX, sigmaY;
631 SkColor color;
632 bool shadowOnly, useCropRect;
633 fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &shadowOnly, &useCropRect);
634 SkIRect cropRect;
635 if (useCropRect) {
636 fuzz->next(&cropRect);
637 }
638 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
639 if (shadowOnly) {
640 return SkImageFilters::DropShadowOnly(dx, dy, sigmaX, sigmaY, color,
641 std::move(input),
642 useCropRect ? &cropRect : nullptr);
643 } else {
644 return SkImageFilters::DropShadow(dx, dy, sigmaX, sigmaY, color, std::move(input),
645 useCropRect ? &cropRect : nullptr);
646 }
647 }
648 case 8:
649 return SkImageFilters::Image(make_fuzz_image(fuzz), SkCubicResampler::Mitchell());
650 case 9: {
651 sk_sp<SkImage> image = make_fuzz_image(fuzz);
652 SkRect srcRect, dstRect;
653 fuzz->next(&srcRect, &dstRect);
654 return SkImageFilters::Image(std::move(image), srcRect, dstRect, next_sampling(fuzz));
655 }
656 case 10:
657 return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
658 case 11: {
659 SkRect lensBounds;
660 SkScalar zoomAmount;
661 SkScalar inset;
662 bool useCropRect;
663 SkIRect cropRect;
664 fuzz->next(&lensBounds, &zoomAmount, &inset, &useCropRect);
665 if (useCropRect) {
666 fuzz->next(&cropRect);
667 }
668 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
669 const auto sampling = next_sampling(fuzz);
670 return SkImageFilters::Magnifier(lensBounds, zoomAmount, inset, sampling,
671 std::move(input), useCropRect ? &cropRect : nullptr);
672 }
673 case 12: {
674 constexpr int kMaxKernelSize = 5;
675 int32_t n, m;
676 fuzz->nextRange(&n, 1, kMaxKernelSize);
677 fuzz->nextRange(&m, 1, kMaxKernelSize);
678 SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
679 fuzz->nextN(kernel, n * m);
680 int32_t offsetX, offsetY;
681 fuzz->nextRange(&offsetX, 0, n - 1);
682 fuzz->nextRange(&offsetY, 0, m - 1);
683 SkScalar gain, bias;
684 bool convolveAlpha, useCropRect;
685 fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
686 SkTileMode tileMode;
687 fuzz->nextEnum(&tileMode, SkTileMode::kLastTileMode);
688 SkIRect cropRect;
689 if (useCropRect) {
690 fuzz->next(&cropRect);
691 }
692 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
693 return SkImageFilters::MatrixConvolution(
694 SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
695 convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
696 }
697 case 13: {
698 sk_sp<SkImageFilter> first = MakeFuzzImageFilter(fuzz, depth - 1);
699 sk_sp<SkImageFilter> second = MakeFuzzImageFilter(fuzz, depth - 1);
700 bool useCropRect;
701 fuzz->next(&useCropRect);
702 SkIRect cropRect;
703 if (useCropRect) {
704 fuzz->next(&cropRect);
705 }
706 return SkImageFilters::Merge(std::move(first), std::move(second),
707 useCropRect ? &cropRect : nullptr);
708 }
709 case 14: {
710 constexpr int kMaxCount = 4;
711 sk_sp<SkImageFilter> ifs[kMaxCount];
712 int count;
713 fuzz->nextRange(&count, 1, kMaxCount);
714 for (int i = 0; i < count; ++i) {
715 ifs[i] = MakeFuzzImageFilter(fuzz, depth - 1);
716 }
717 bool useCropRect;
718 fuzz->next(&useCropRect);
719 SkIRect cropRect;
720 if (useCropRect) {
721 fuzz->next(&cropRect);
722 }
723 return SkImageFilters::Merge(ifs, count, useCropRect ? &cropRect : nullptr);
724 }
725 case 15: {
726 int rx, ry;
727 fuzz->next(&rx, &ry);
728 bool useCropRect;
729 fuzz->next(&useCropRect);
730 SkIRect cropRect;
731 if (useCropRect) {
732 fuzz->next(&cropRect);
733 }
734 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
735 return SkImageFilters::Dilate(rx, ry, std::move(input),
736 useCropRect ? &cropRect : nullptr);
737 }
738 case 16: {
739 int rx, ry;
740 fuzz->next(&rx, &ry);
741 bool useCropRect;
742 fuzz->next(&useCropRect);
743 SkIRect cropRect;
744 if (useCropRect) {
745 fuzz->next(&cropRect);
746 }
747 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
748 return SkImageFilters::Erode(rx, ry, std::move(input),
749 useCropRect ? &cropRect : nullptr);
750 }
751 case 17: {
752 SkScalar dx, dy;
753 fuzz->next(&dx, &dy);
754 bool useCropRect;
755 fuzz->next(&useCropRect);
756 SkIRect cropRect;
757 if (useCropRect) {
758 fuzz->next(&cropRect);
759 }
760 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
761 return SkImageFilters::Offset(dx, dy, std::move(input),
762 useCropRect ? &cropRect : nullptr);
763 }
764 case 18: {
765 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
766 return SkImageFilters::Picture(std::move(picture));
767 }
768 case 19: {
769 SkRect cropRect;
770 fuzz->next(&cropRect);
771 sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
772 return SkImageFilters::Picture(std::move(picture), cropRect);
773 }
774 case 20: {
775 SkRect src, dst;
776 fuzz->next(&src, &dst);
777 sk_sp<SkImageFilter> input = MakeFuzzImageFilter(fuzz, depth - 1);
778 return SkImageFilters::Tile(src, dst, std::move(input));
779 }
780 case 21: {
781 SkBlendMode blendMode;
782 bool useCropRect;
783 fuzz->next(&useCropRect);
784 fuzz->nextEnum(&blendMode, SkBlendMode::kLastMode);
785 SkIRect cropRect;
786 if (useCropRect) {
787 fuzz->next(&cropRect);
788 }
789 sk_sp<SkImageFilter> bg = MakeFuzzImageFilter(fuzz, depth - 1);
790 sk_sp<SkImageFilter> fg = MakeFuzzImageFilter(fuzz, depth - 1);
791 return SkImageFilters::Blend(blendMode, std::move(bg), std::move(fg),
792 useCropRect ? &cropRect : nullptr);
793 }
794 case 22: {
795 sk_sp<SkShader> shader = make_fuzz_shader(fuzz, depth - 1);
796 bool useCropRect;
797 fuzz->next(&useCropRect);
798 SkIRect cropRect;
799 if (useCropRect) {
800 fuzz->next(&cropRect);
801 }
802 return SkImageFilters::Shader(std::move(shader), useCropRect ? &cropRect : nullptr);
803 }
804 default:
805 SkASSERT(false);
806 return nullptr;
807 }
808 }
809
810 static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
811 int w, h;
812 fuzz->nextRange(&w, 1, 1024);
813 fuzz->nextRange(&h, 1, 1024);
814 AutoTMalloc<SkPMColor> data(w * h);
815 SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
816 int n = w * h;
817 for (int i = 0; i < n; ++i) {
818 SkColor c;
819 fuzz->next(&c);
820 data[i] = SkPreMultiplyColor(c);
821 }
822 (void)data.release();
823 return SkImages::RasterFromPixmap(
824 pixmap, [](const void* p, void*) { sk_free(const_cast<void*>(p)); }, nullptr);
825 }
826
827 template <typename T>
828 static T make_fuzz_enum_range(Fuzz* fuzz, T maxv) {
829 T value;
830 fuzz->nextEnum(&value, maxv);
831 return value;
832 }
833
834 void FuzzPaint(Fuzz* fuzz, SkPaint* paint, int depth) {
835 if (!fuzz || !paint || depth <= 0 || fuzz->exhausted()) {
836 return;
837 }
838
839 paint->setAntiAlias( make_fuzz_t<bool>(fuzz));
840 paint->setDither( make_fuzz_t<bool>(fuzz));
841 paint->setColor( make_fuzz_t<SkColor>(fuzz));
842 paint->setBlendMode( make_fuzz_enum_range<SkBlendMode>(fuzz, SkBlendMode::kLastMode));
843 paint->setStyle( make_fuzz_enum_range<SkPaint::Style>(fuzz,
844 SkPaint::Style::kStrokeAndFill_Style));
845 paint->setShader( make_fuzz_shader(fuzz, depth - 1));
846 paint->setPathEffect( make_fuzz_patheffect(fuzz, depth - 1));
847 paint->setMaskFilter( make_fuzz_maskfilter(fuzz));
848 paint->setImageFilter( MakeFuzzImageFilter(fuzz, depth - 1));
849 paint->setColorFilter( make_fuzz_colorfilter(fuzz, depth - 1));
850
851 if (paint->getStyle() != SkPaint::kFill_Style) {
852 paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
853 paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
854 paint->setStrokeCap( make_fuzz_enum_range<SkPaint::Cap>(fuzz, SkPaint::kLast_Cap));
855 paint->setStrokeJoin( make_fuzz_enum_range<SkPaint::Join>(fuzz, SkPaint::kLast_Join));
856 }
857 }
858
859 static SkFont fuzz_font(Fuzz* fuzz) {
860 SkFont font;
861 font.setTypeface( make_fuzz_typeface(fuzz));
862 font.setSize( make_fuzz_t<SkScalar>(fuzz));
863 font.setScaleX( make_fuzz_t<SkScalar>(fuzz));
864 font.setSkewX( make_fuzz_t<SkScalar>(fuzz));
865 font.setLinearMetrics( make_fuzz_t<bool>(fuzz));
866 font.setSubpixel( make_fuzz_t<bool>(fuzz));
867 font.setEmbeddedBitmaps( make_fuzz_t<bool>(fuzz));
868 font.setForceAutoHinting( make_fuzz_t<bool>(fuzz));
869 font.setEmbolden( make_fuzz_t<bool>(fuzz));
870 font.setHinting( make_fuzz_enum_range<SkFontHinting>(fuzz, SkFontHinting::kFull));
871 font.setEdging( make_fuzz_enum_range<SkFont::Edging>(fuzz,
872 SkFont::Edging::kSubpixelAntiAlias));
873 return font;
874 }
875
876 static SkTextEncoding FuzzPaint_text_encoding(Fuzz* fuzz) {
877 return make_fuzz_enum_range<SkTextEncoding>(fuzz, SkTextEncoding::kUTF32);
878 }
879
880 constexpr int kMaxGlyphCount = 30;
881
882 static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkFont& font, SkTextEncoding encoding) {
883 SkTDArray<uint8_t> array;
884 if (SkTextEncoding::kGlyphID == encoding) {
885 int glyphRange = font.getTypeface()->countGlyphs();
886 if (glyphRange == 0) {
887 // Some fuzzing environments have no fonts, so empty array is the best
888 // we can do.
889 return array;
890 }
891 int glyphCount;
892 fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
893 SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
894 for (int i = 0; i < glyphCount; ++i) {
895 fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
896 }
897 return array;
898 }
899 static const SkUnichar ranges[][2] = {
900 {0x0020, 0x007F},
901 {0x00A1, 0x0250},
902 {0x0400, 0x0500},
903 };
904 int32_t count = 0;
905 for (size_t i = 0; i < std::size(ranges); ++i) {
906 count += (ranges[i][1] - ranges[i][0]);
907 }
908 constexpr int kMaxLength = kMaxGlyphCount;
909 SkUnichar buffer[kMaxLength];
910 int length;
911 fuzz->nextRange(&length, 1, kMaxLength);
912 for (int j = 0; j < length; ++j) {
913 int32_t value;
914 fuzz->nextRange(&value, 0, count - 1);
915 for (size_t i = 0; i < std::size(ranges); ++i) {
916 if (value + ranges[i][0] < ranges[i][1]) {
917 buffer[j] = value + ranges[i][0];
918 break;
919 } else {
920 value -= (ranges[i][1] - ranges[i][0]);
921 }
922 }
923 }
924 switch (encoding) {
925 case SkTextEncoding::kUTF8: {
926 size_t utf8len = 0;
927 for (int j = 0; j < length; ++j) {
928 utf8len += SkUTF::ToUTF8(buffer[j], nullptr);
929 }
930 char* ptr = (char*)array.append(utf8len);
931 for (int j = 0; j < length; ++j) {
932 ptr += SkUTF::ToUTF8(buffer[j], ptr);
933 }
934 } break;
935 case SkTextEncoding::kUTF16: {
936 size_t utf16len = 0;
937 for (int j = 0; j < length; ++j) {
938 utf16len += SkUTF::ToUTF16(buffer[j]);
939 }
940 uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
941 for (int j = 0; j < length; ++j) {
942 ptr += SkUTF::ToUTF16(buffer[j], ptr);
943 }
944 } break;
945 case SkTextEncoding::kUTF32:
946 memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
947 break;
948 default:
949 SkASSERT(false);
950 break;
951 }
952 return array;
953 }
954
955 static std::string make_fuzz_string(Fuzz* fuzz) {
956 int len;
957 fuzz->nextRange(&len, 0, kMaxGlyphCount);
958 std::string str(len, 0);
959 for (int i = 0; i < len; i++) {
960 fuzz->next(&str[i]);
961 }
962 return str;
963 }
964
965 static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
966 SkTextBlobBuilder textBlobBuilder;
967 int8_t runCount;
968 fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
969 while (runCount-- > 0) {
970 SkFont font = ToolUtils::DefaultFont();
971 SkTextEncoding encoding = FuzzPaint_text_encoding(fuzz);
972 font.setEdging(make_fuzz_t<bool>(fuzz) ? SkFont::Edging::kAlias : SkFont::Edging::kAntiAlias);
973 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
974 int glyphCount = font.countText(text.begin(), SkToSizeT(text.size()), encoding);
975 SkASSERT(glyphCount <= kMaxGlyphCount);
976 SkScalar x, y;
977 const SkTextBlobBuilder::RunBuffer* buffer;
978 uint8_t runType;
979 fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
980 const void* textPtr = text.begin();
981 size_t textLen = SkToSizeT(text.size());
982 switch (runType) {
983 case 0:
984 fuzz->next(&x, &y);
985 // TODO: Test other variations of this.
986 buffer = &textBlobBuilder.allocRun(font, glyphCount, x, y);
987 (void)font.textToGlyphs(textPtr, textLen, encoding,
988 {buffer->glyphs, (size_t)glyphCount});
989 break;
990 case 1:
991 fuzz->next(&y);
992 // TODO: Test other variations of this.
993 buffer = &textBlobBuilder.allocRunPosH(font, glyphCount, y);
994 (void)font.textToGlyphs(textPtr, textLen, encoding,
995 {buffer->glyphs, (size_t)glyphCount});
996 fuzz->nextN(buffer->pos, glyphCount);
997 break;
998 case 2:
999 // TODO: Test other variations of this.
1000 buffer = &textBlobBuilder.allocRunPos(font, glyphCount);
1001 (void)font.textToGlyphs(textPtr, textLen, encoding,
1002 {buffer->glyphs, (size_t)glyphCount});
1003 fuzz->nextN(buffer->pos, glyphCount * 2);
1004 break;
1005 default:
1006 SkASSERT(false);
1007 break;
1008 }
1009 }
1010 return textBlobBuilder.make();
1011 }
1012
1013 void FuzzCanvas(Fuzz* fuzz, SkCanvas* canvas, int depth) {
1014 if (!fuzz || !canvas || depth <= 0 || fuzz->exhausted()) {
1015 return;
1016 }
1017 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
1018 unsigned N;
1019 fuzz->nextRange(&N, 0, 2000);
1020 for (unsigned loop = 0; loop < N; ++loop) {
1021 if (fuzz->exhausted()) {
1022 return;
1023 }
1024 SkPaint paint;
1025 SkFont font = ToolUtils::DefaultFont();
1026 unsigned drawCommand;
1027 fuzz->nextRange(&drawCommand, 0, 62);
1028 switch (drawCommand) {
1029 case 0:
1030 #if defined(SK_GANESH)
1031 if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
1032 dContext->flushAndSubmit();
1033 }
1034 #endif
1035 break;
1036 case 1:
1037 canvas->save();
1038 break;
1039 case 2: {
1040 SkRect bounds;
1041 fuzz->next(&bounds);
1042 FuzzPaint(fuzz, &paint, depth - 1);
1043 canvas->saveLayer(&bounds, &paint);
1044 break;
1045 }
1046 case 3: {
1047 SkRect bounds;
1048 fuzz->next(&bounds);
1049 canvas->saveLayer(&bounds, nullptr);
1050 break;
1051 }
1052 case 4:
1053 FuzzPaint(fuzz, &paint, depth - 1);
1054 canvas->saveLayer(nullptr, &paint);
1055 break;
1056 case 5:
1057 canvas->saveLayer(nullptr, nullptr);
1058 break;
1059 case 6: {
1060 uint8_t alpha;
1061 fuzz->next(&alpha);
1062 canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
1063 break;
1064 }
1065 case 7: {
1066 SkRect bounds;
1067 uint8_t alpha;
1068 fuzz->next(&bounds, &alpha);
1069 canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
1070 break;
1071 }
1072 case 8: {
1073 SkCanvas::SaveLayerRec saveLayerRec;
1074 SkRect bounds;
1075 if (make_fuzz_t<bool>(fuzz)) {
1076 fuzz->next(&bounds);
1077 saveLayerRec.fBounds = &bounds;
1078 }
1079 if (make_fuzz_t<bool>(fuzz)) {
1080 FuzzPaint(fuzz, &paint, depth - 1);
1081 saveLayerRec.fPaint = &paint;
1082 }
1083 sk_sp<SkImageFilter> imageFilter;
1084 if (make_fuzz_t<bool>(fuzz)) {
1085 imageFilter = MakeFuzzImageFilter(fuzz, depth - 1);
1086 saveLayerRec.fBackdrop = imageFilter.get();
1087 }
1088 // _DumpCanvas can't handle this.
1089 // if (make_fuzz_t<bool>(fuzz)) {
1090 // saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
1091 // }
1092
1093 canvas->saveLayer(saveLayerRec);
1094 break;
1095 }
1096 case 9:
1097 canvas->restore();
1098 break;
1099 case 10: {
1100 int saveCount;
1101 fuzz->next(&saveCount);
1102 canvas->restoreToCount(saveCount);
1103 break;
1104 }
1105 case 11: {
1106 SkScalar x, y;
1107 fuzz->next(&x, &y);
1108 canvas->translate(x, y);
1109 break;
1110 }
1111 case 12: {
1112 SkScalar x, y;
1113 fuzz->next(&x, &y);
1114 canvas->scale(x, y);
1115 break;
1116 }
1117 case 13: {
1118 SkScalar v;
1119 fuzz->next(&v);
1120 canvas->rotate(v);
1121 break;
1122 }
1123 case 14: {
1124 SkScalar x, y, v;
1125 fuzz->next(&x, &y, &v);
1126 canvas->rotate(v, x, y);
1127 break;
1128 }
1129 case 15: {
1130 SkScalar x, y;
1131 fuzz->next(&x, &y);
1132 canvas->skew(x, y);
1133 break;
1134 }
1135 case 16: {
1136 SkMatrix mat;
1137 FuzzNiceMatrix(fuzz, &mat);
1138 canvas->concat(mat);
1139 break;
1140 }
1141 case 17: {
1142 SkMatrix mat;
1143 FuzzNiceMatrix(fuzz, &mat);
1144 canvas->setMatrix(mat);
1145 break;
1146 }
1147 case 18:
1148 canvas->resetMatrix();
1149 break;
1150 case 19: {
1151 SkRect r;
1152 int op;
1153 bool doAntiAlias;
1154 fuzz->next(&r, &doAntiAlias);
1155 fuzz->nextRange(&op, 0, 1);
1156 r.sort();
1157 canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
1158 break;
1159 }
1160 case 20: {
1161 SkRRect rr;
1162 int op;
1163 bool doAntiAlias;
1164 FuzzNiceRRect(fuzz, &rr);
1165 fuzz->next(&doAntiAlias);
1166 fuzz->nextRange(&op, 0, 1);
1167 canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
1168 break;
1169 }
1170 case 21: {
1171 SkPath path = FuzzNicePath(fuzz, 30);
1172 int op;
1173 bool doAntiAlias;
1174 fuzz->next(&doAntiAlias);
1175 fuzz->nextRange(&op, 0, 1);
1176 canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
1177 break;
1178 }
1179 case 22: {
1180 SkRegion region;
1181 int op;
1182 fuzz->next(&region);
1183 fuzz->nextRange(&op, 0, 1);
1184 canvas->clipRegion(region, (SkClipOp)op);
1185 break;
1186 }
1187 case 23:
1188 FuzzPaint(fuzz, &paint, depth - 1);
1189 canvas->drawPaint(paint);
1190 break;
1191 case 24: {
1192 FuzzPaint(fuzz, &paint, depth - 1);
1193 SkCanvas::PointMode pointMode;
1194 fuzz->nextRange(&pointMode,
1195 SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
1196 size_t count;
1197 constexpr int kMaxCount = 30;
1198 fuzz->nextRange(&count, 0, kMaxCount);
1199 SkPoint pts[kMaxCount];
1200 fuzz->nextN(pts, count);
1201 canvas->drawPoints(pointMode, {pts, count}, paint);
1202 break;
1203 }
1204 case 25: {
1205 FuzzPaint(fuzz, &paint, depth - 1);
1206 SkRect r;
1207 fuzz->next(&r);
1208 if (!r.isFinite()) {
1209 break;
1210 }
1211 canvas->drawRect(r, paint);
1212 break;
1213 }
1214 case 26: {
1215 FuzzPaint(fuzz, &paint, depth - 1);
1216 SkRegion region;
1217 fuzz->next(&region);
1218 canvas->drawRegion(region, paint);
1219 break;
1220 }
1221 case 27: {
1222 FuzzPaint(fuzz, &paint, depth - 1);
1223 SkRect r;
1224 fuzz->next(&r);
1225 if (!r.isFinite()) {
1226 break;
1227 }
1228 canvas->drawOval(r, paint);
1229 break;
1230 }
1231 case 28: break; // must have deleted this some time earlier
1232 case 29: {
1233 FuzzPaint(fuzz, &paint, depth - 1);
1234 SkRRect rr;
1235 FuzzNiceRRect(fuzz, &rr);
1236 canvas->drawRRect(rr, paint);
1237 break;
1238 }
1239 case 30: {
1240 FuzzPaint(fuzz, &paint, depth - 1);
1241 SkRRect orr, irr;
1242 FuzzNiceRRect(fuzz, &orr);
1243 FuzzNiceRRect(fuzz, &irr);
1244 if (orr.getBounds().contains(irr.getBounds())) {
1245 canvas->drawDRRect(orr, irr, paint);
1246 }
1247 break;
1248 }
1249 case 31: {
1250 FuzzPaint(fuzz, &paint, depth - 1);
1251 SkRect r;
1252 SkScalar start, sweep;
1253 bool useCenter;
1254 fuzz->next(&r, &start, &sweep, &useCenter);
1255 canvas->drawArc(r, start, sweep, useCenter, paint);
1256 break;
1257 }
1258 case 32: {
1259 FuzzPaint(fuzz, &paint, depth - 1);
1260 SkPath path = FuzzNicePath(fuzz, 60);
1261 canvas->drawPath(path, paint);
1262 break;
1263 }
1264 case 33: {
1265 sk_sp<SkImage> img = make_fuzz_image(fuzz);
1266 SkScalar left, top;
1267 bool usePaint;
1268 fuzz->next(&left, &top, &usePaint);
1269 if (usePaint) {
1270 FuzzPaint(fuzz, &paint, depth - 1);
1271 }
1272 canvas->drawImage(img.get(), left, top, SkSamplingOptions(),
1273 usePaint ? &paint : nullptr);
1274 break;
1275 }
1276 case 35: {
1277 auto img = make_fuzz_image(fuzz);
1278 SkIRect src;
1279 SkRect dst;
1280 bool usePaint;
1281 fuzz->next(&src, &dst, &usePaint);
1282 if (usePaint) {
1283 FuzzPaint(fuzz, &paint, depth - 1);
1284 }
1285 SkCanvas::SrcRectConstraint constraint =
1286 make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
1287 : SkCanvas::kFast_SrcRectConstraint;
1288 canvas->drawImageRect(img.get(), SkRect::Make(src), dst, SkSamplingOptions(),
1289 usePaint ? &paint : nullptr, constraint);
1290 break;
1291 }
1292 case 37: {
1293 auto img = make_fuzz_image(fuzz);
1294 SkIRect center;
1295 SkRect dst;
1296 bool usePaint;
1297 fuzz->next(&usePaint);
1298 if (usePaint) {
1299 FuzzPaint(fuzz, &paint, depth - 1);
1300 }
1301 if (make_fuzz_t<bool>(fuzz)) {
1302 fuzz->next(&center);
1303 } else { // Make valid center, see SkLatticeIter::Valid().
1304 fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
1305 fuzz->nextRange(&center.fTop, 0, img->height() - 1);
1306 fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
1307 fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
1308 }
1309 fuzz->next(&dst);
1310 canvas->drawImageNine(img.get(), center, dst, SkFilterMode::kNearest,
1311 usePaint ? &paint : nullptr);
1312 break;
1313 }
1314 case 44: {
1315 auto img = make_fuzz_image(fuzz);
1316 bool usePaint;
1317 SkRect dst;
1318 fuzz->next(&usePaint, &dst);
1319 if (usePaint) {
1320 FuzzPaint(fuzz, &paint, depth - 1);
1321 }
1322 constexpr int kMax = 6;
1323 int xDivs[kMax], yDivs[kMax];
1324 SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
1325 fuzz->nextRange(&lattice.fXCount, 2, kMax);
1326 fuzz->nextRange(&lattice.fYCount, 2, kMax);
1327 fuzz->nextN(xDivs, lattice.fXCount);
1328 fuzz->nextN(yDivs, lattice.fYCount);
1329 canvas->drawImageLattice(img.get(), lattice, dst, SkFilterMode::kLinear,
1330 usePaint ? &paint : nullptr);
1331 break;
1332 }
1333 case 45: {
1334 FuzzPaint(fuzz, &paint, depth - 1);
1335 font = fuzz_font(fuzz);
1336 SkTextEncoding encoding = FuzzPaint_text_encoding(fuzz);
1337 SkScalar x, y;
1338 fuzz->next(&x, &y);
1339 SkTDArray<uint8_t> text = make_fuzz_text(fuzz, font, encoding);
1340 canvas->drawSimpleText(text.begin(), SkToSizeT(text.size()), encoding, x, y,
1341 font, paint);
1342 break;
1343 }
1344 case 46: {
1345 // was drawPosText
1346 break;
1347 }
1348 case 47: {
1349 // was drawPosTextH
1350 break;
1351 }
1352 case 48: {
1353 // was drawtextonpath
1354 break;
1355 }
1356 case 49: {
1357 // was drawtextonpath
1358 break;
1359 }
1360 case 50: {
1361 // was drawTextRSXform
1362 break;
1363 }
1364 case 51: {
1365 sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
1366 FuzzPaint(fuzz, &paint, depth - 1);
1367 SkScalar x, y;
1368 fuzz->next(&x, &y);
1369 canvas->drawTextBlob(blob, x, y, paint);
1370 break;
1371 }
1372 case 52: {
1373 SkMatrix matrix;
1374 bool usePaint, useMatrix;
1375 fuzz->next(&usePaint, &useMatrix);
1376 if (usePaint) {
1377 FuzzPaint(fuzz, &paint, depth - 1);
1378 }
1379 if (useMatrix) {
1380 FuzzNiceMatrix(fuzz, &matrix);
1381 }
1382 auto pic = make_fuzz_picture(fuzz, depth - 1);
1383 canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
1384 usePaint ? &paint : nullptr);
1385 break;
1386 }
1387 case 53: {
1388 FuzzPaint(fuzz, &paint, depth - 1);
1389 SkVertices::VertexMode vertexMode;
1390 SkBlendMode blendMode;
1391 fuzz->nextRange(&vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
1392 fuzz->nextRange(&blendMode, 0, SkBlendMode::kLastMode);
1393 constexpr int kMaxCount = 100;
1394 int vertexCount;
1395 SkPoint vertices[kMaxCount];
1396 SkPoint texs[kMaxCount];
1397 SkColor colors[kMaxCount];
1398 fuzz->nextRange(&vertexCount, 3, kMaxCount);
1399 fuzz->nextN(vertices, vertexCount);
1400 bool useTexs, useColors;
1401 fuzz->next(&useTexs, &useColors);
1402 if (useTexs) {
1403 fuzz->nextN(texs, vertexCount);
1404 }
1405 if (useColors) {
1406 fuzz->nextN(colors, vertexCount);
1407 }
1408 int indexCount = 0;
1409 uint16_t indices[kMaxCount * 2];
1410 if (make_fuzz_t<bool>(fuzz)) {
1411 fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
1412 for (int index = 0; index < indexCount; ++index) {
1413 fuzz->nextRange(&indices[index], 0, vertexCount - 1);
1414 }
1415 }
1416 canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
1417 useTexs ? texs : nullptr,
1418 useColors ? colors : nullptr,
1419 indexCount, indices),
1420 blendMode, paint);
1421 break;
1422 }
1423 case 54: {
1424 SkColor color;
1425 SkBlendMode blendMode;
1426 fuzz->nextRange(&blendMode, 0, SkBlendMode::kSrcOver);
1427 fuzz->next(&color);
1428 canvas->drawColor(color, blendMode);
1429 break;
1430 }
1431 case 55: {
1432 SkColor4f color;
1433 SkBlendMode blendMode;
1434 float R, G, B, Alpha;
1435 fuzz->nextRange(&blendMode, 0, SkBlendMode::kSrcOver);
1436 fuzz->nextRange(&R, -1, 2);
1437 fuzz->nextRange(&G, -1, 2);
1438 fuzz->nextRange(&B, -1, 2);
1439 fuzz->nextRange(&Alpha, 0, 1);
1440 color = {R, G, B, Alpha};
1441 canvas->drawColor(color, blendMode);
1442 break;
1443 }
1444 case 56: {
1445 FuzzPaint(fuzz, &paint, depth - 1);
1446 SkPoint p0, p1;
1447 fuzz->next(&p0, &p1);
1448 canvas->drawLine(p0, p1, paint);
1449 break;
1450 }
1451 case 57: {
1452 FuzzPaint(fuzz, &paint, depth - 1);
1453 SkIRect r;
1454 fuzz->next(&r);
1455 canvas->drawIRect(r, paint);
1456 break;
1457 }
1458 case 58: {
1459 FuzzPaint(fuzz, &paint, depth - 1);
1460 SkScalar radius;
1461 SkPoint center;
1462 fuzz->next(&radius, &center);
1463 canvas->drawCircle(center, radius, paint);
1464 break;
1465 }
1466 case 59: {
1467 FuzzPaint(fuzz, &paint, depth - 1);
1468 SkRect oval;
1469 SkScalar startAngle, sweepAngle;
1470 bool useCenter;
1471 fuzz->next(&oval, &startAngle, &sweepAngle, &useCenter);
1472 canvas->drawArc(oval, startAngle, sweepAngle, useCenter, paint);
1473 break;
1474 }
1475 case 60: {
1476 FuzzPaint(fuzz, &paint, depth - 1);
1477 SkRect rect;
1478 SkScalar rx, ry;
1479 fuzz->next(&rect, &rx, &ry);
1480 canvas->drawRoundRect(rect, rx, ry, paint);
1481 break;
1482 }
1483 case 61: {
1484 FuzzPaint(fuzz, &paint, depth - 1);
1485 font = fuzz_font(fuzz);
1486 std::string str = make_fuzz_string(fuzz);
1487 SkScalar x, y;
1488 fuzz->next(&x, &y);
1489 canvas->drawString(str.c_str(), x, y, font, paint);
1490 break;
1491 }
1492 case 62: {
1493 FuzzPaint(fuzz, &paint, depth - 1);
1494 SkPoint cubics[12];
1495 SkColor colors[4];
1496 SkPoint texCoords[4];
1497 bool useTexCoords;
1498 fuzz->nextN(cubics, 12);
1499 fuzz->nextN(colors, 4);
1500 fuzz->next(&useTexCoords);
1501 if (useTexCoords) {
1502 fuzz->nextN(texCoords, 4);
1503 }
1504 SkBlendMode mode;
1505 fuzz->nextEnum(&mode, SkBlendMode::kLastMode);
1506 canvas->drawPatch(cubics, colors, useTexCoords ? texCoords : nullptr
1507 , mode, paint);
1508 break;
1509 }
1510 default:
1511 SkASSERT(false);
1512 break;
1513 }
1514 }
1515 }
1516
1517 static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
1518 SkScalar w, h;
1519 fuzz->next(&w, &h);
1520 SkPictureRecorder pictureRecorder;
1521 FuzzCanvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
1522 return pictureRecorder.finishRecordingAsPicture();
1523 }
1524