aboutsummaryrefslogtreecommitdiffhomepage
path: root/fuzz/FuzzCanvas.cpp
blob: 7cfcc132d54fcef44a9c494e975b6b094ee32aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Fuzz.h"
#include "FuzzCommon.h"

// CORE
#include "SkCanvas.h"
#include "SkColorFilter.h"
#include "SkDebugCanvas.h"
#include "SkDocument.h"
#include "SkFontMgr.h"
#include "SkImageFilter.h"
#include "SkMaskFilter.h"
#include "SkNullCanvas.h"
#include "SkPathEffect.h"
#include "SkPictureRecorder.h"
#include "SkPoint3.h"
#include "SkRSXform.h"
#include "SkRegion.h"
#include "SkSurface.h"
#include "SkTypeface.h"
#include "SkOSFile.h"

// EFFECTS
#include "Sk1DPathEffect.h"
#include "Sk2DPathEffect.h"
#include "SkAlphaThresholdFilter.h"
#include "SkArithmeticImageFilter.h"
#include "SkBlurImageFilter.h"
#include "SkBlurMaskFilter.h"
#include "SkColorFilterImageFilter.h"
#include "SkColorMatrixFilter.h"
#include "SkComposeImageFilter.h"
#include "SkCornerPathEffect.h"
#include "SkDashPathEffect.h"
#include "SkDiscretePathEffect.h"
#include "SkDisplacementMapEffect.h"
#include "SkDropShadowImageFilter.h"
#include "SkGradientShader.h"
#include "SkHighContrastFilter.h"
#include "SkImageSource.h"
#include "SkLightingImageFilter.h"
#include "SkLumaColorFilter.h"
#include "SkMagnifierImageFilter.h"
#include "SkMatrixConvolutionImageFilter.h"
#include "SkMergeImageFilter.h"
#include "SkMorphologyImageFilter.h"
#include "SkOffsetImageFilter.h"
#include "SkPaintImageFilter.h"
#include "SkPerlinNoiseShader.h"
#include "SkPictureImageFilter.h"
#include "SkReadBuffer.h"
#include "SkRRectsGaussianEdgeMaskFilter.h"
#include "SkTableColorFilter.h"
#include "SkTextBlob.h"
#include "SkTileImageFilter.h"
#include "SkXfermodeImageFilter.h"

// SRC
#include "SkUtils.h"

#if SK_SUPPORT_GPU
#include "GrContextFactory.h"
#endif

// MISC

#include <iostream>

// TODO:
//   SkTextBlob with Unicode
//   SkImage: more types

// be careful: `foo(make_fuzz_t<T>(f), make_fuzz_t<U>(f))` is undefined.
// In fact, all make_fuzz_foo() functions have this potential problem.
// Use sequence points!
template <typename T>
inline T make_fuzz_t(Fuzz* fuzz) {
    T t;
    fuzz->next(&t);
    return t;
}

template <>
inline void Fuzz::next(SkShader::TileMode* m) {
    fuzz_enum_range(this, m, 0, SkShader::kTileModeCount - 1);
}

template <>
inline void Fuzz::next(SkFilterQuality* q) {
    fuzz_enum_range(this, q, SkFilterQuality::kNone_SkFilterQuality,
                    SkFilterQuality::kLast_SkFilterQuality);
}

template <>
inline void Fuzz::next(SkMatrix* m) {
    constexpr int kArrayLength = 9;
    SkScalar buffer[kArrayLength];
    int matrixType;
    this->nextRange(&matrixType, 0, 4);
    switch (matrixType) {
        case 0:  // identity
            *m = SkMatrix::I();
            return;
        case 1:  // translate
            this->nextRange(&buffer[0], -4000.0f, 4000.0f);
            this->nextRange(&buffer[1], -4000.0f, 4000.0f);
            *m = SkMatrix::MakeTrans(buffer[0], buffer[1]);
            return;
        case 2:  // translate + scale
            this->nextRange(&buffer[0], -400.0f, 400.0f);
            this->nextRange(&buffer[1], -400.0f, 400.0f);
            this->nextRange(&buffer[2], -4000.0f, 4000.0f);
            this->nextRange(&buffer[3], -4000.0f, 4000.0f);
            *m = SkMatrix::MakeScale(buffer[0], buffer[1]);
            m->postTranslate(buffer[2], buffer[3]);
            return;
        case 3:  // affine
            this->nextN(buffer, 6);
            m->setAffine(buffer);
            return;
        case 4:  // perspective
            this->nextN(buffer, kArrayLength);
            m->set9(buffer);
            return;
        default:
            SkASSERT(false);
            return;
    }
}

template <>
inline void Fuzz::next(SkRRect* rr) {
    SkRect r;
    SkVector radii[4];
    this->next(&r);
    r.sort();
    for (SkVector& vec : radii) {
        this->nextRange(&vec.fX, 0.0f, 1.0f);
        vec.fX *= 0.5f * r.width();
        this->nextRange(&vec.fY, 0.0f, 1.0f);
        vec.fY *= 0.5f * r.height();
    }
    rr->setRectRadii(r, radii);
}

template <>
inline void Fuzz::next(SkBlendMode* mode) {
    fuzz_enum_range(this, mode, 0, SkBlendMode::kLastMode);
}

static sk_sp<SkImage> make_fuzz_image(Fuzz*);

static SkBitmap make_fuzz_bitmap(Fuzz*);

static sk_sp<SkPicture> make_fuzz_picture(Fuzz*, int depth);

static sk_sp<SkColorFilter> make_fuzz_colorfilter(Fuzz* fuzz, int depth) {
    if (depth <= 0) {
        return nullptr;
    }
    int colorFilterType;
    fuzz->nextRange(&colorFilterType, 0, 8);
    switch (colorFilterType) {
        case 0:
            return nullptr;
        case 1: {
            SkColor color;
            SkBlendMode mode;
            fuzz->next(&color, &mode);
            return SkColorFilter::MakeModeFilter(color, mode);
        }
        case 2: {
            sk_sp<SkColorFilter> outer = make_fuzz_colorfilter(fuzz, depth - 1);
            if (!outer) {
                return nullptr;
            }
            sk_sp<SkColorFilter> inner = make_fuzz_colorfilter(fuzz, depth - 1);
            // makeComposed should be able to handle nullptr.
            return outer->makeComposed(std::move(inner));
        }
        case 3: {
            SkScalar array[20];
            fuzz->nextN(array, SK_ARRAY_COUNT(array));
            return SkColorFilter::MakeMatrixFilterRowMajor255(array);
        }
        case 4: {
            SkColor mul, add;
            fuzz->next(&mul, &add);
            return SkColorMatrixFilter::MakeLightingFilter(mul, add);
        }
        case 5: {
            bool grayscale;
            int invertStyle;
            float contrast;
            fuzz->next(&grayscale);
            fuzz->nextRange(&invertStyle, 0, 2);
            fuzz->nextRange(&contrast, -1.0f, 1.0f);
            return SkHighContrastFilter::Make(SkHighContrastConfig(
                    grayscale, SkHighContrastConfig::InvertStyle(invertStyle), contrast));
        }
        case 6:
            return SkLumaColorFilter::Make();
        case 7: {
            uint8_t table[256];
            fuzz->nextN(table, SK_ARRAY_COUNT(table));
            return SkTableColorFilter::Make(table);
        }
        case 8: {
            uint8_t tableA[256];
            uint8_t tableR[256];
            uint8_t tableG[256];
            uint8_t tableB[256];
            fuzz->nextN(tableA, SK_ARRAY_COUNT(tableA));
            fuzz->nextN(tableR, SK_ARRAY_COUNT(tableR));
            fuzz->nextN(tableG, SK_ARRAY_COUNT(tableG));
            fuzz->nextN(tableB, SK_ARRAY_COUNT(tableB));
            return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
        }
        default:
            SkASSERT(false);
            break;
    }
    return nullptr;
}

static void fuzz_gradient_stops(Fuzz* fuzz, SkScalar* pos, int colorCount) {
    SkScalar totalPos = 0;
    for (int i = 0; i < colorCount; ++i) {
        fuzz->nextRange(&pos[i], 1.0f, 1024.0f);
        totalPos += pos[i];
    }
    totalPos = 1.0f / totalPos;
    for (int i = 0; i < colorCount; ++i) {
        pos[i] *= totalPos;
    }
    // SkASSERT(fabs(pos[colorCount - 1] - 1.0f) < 0.00001f);
    pos[colorCount - 1] = 1.0f;
}

static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
    sk_sp<SkShader> shader1(nullptr), shader2(nullptr);
    sk_sp<SkColorFilter> colorFilter(nullptr);
    SkBitmap bitmap;
    sk_sp<SkImage> img;
    SkShader::TileMode tmX, tmY;
    bool useMatrix;
    SkColor color;
    SkMatrix matrix;
    SkBlendMode blendMode;
    int shaderType;
    if (depth <= 0) {
        return nullptr;
    }
    fuzz->nextRange(&shaderType, 0, 14);
    switch (shaderType) {
        case 0:
            return nullptr;
        case 1:
            return SkShader::MakeEmptyShader();
        case 2:
            fuzz->next(&color);
            return SkShader::MakeColorShader(color);
        case 3:
            img = make_fuzz_image(fuzz);
            fuzz->next(&tmX, &tmY, &useMatrix);
            if (useMatrix) {
                fuzz->next(&matrix);
            }
            return img->makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
        case 4:
            bitmap = make_fuzz_bitmap(fuzz);
            fuzz->next(&tmX, &tmY, &useMatrix);
            if (useMatrix) {
                fuzz->next(&matrix);
            }
            return SkShader::MakeBitmapShader(bitmap, tmX, tmY, useMatrix ? &matrix : nullptr);
        case 5:
            shader1 = make_fuzz_shader(fuzz, depth - 1);  // limit recursion.
            fuzz->next(&matrix);
            return shader1 ? shader1->makeWithLocalMatrix(matrix) : nullptr;
        case 6:
            shader1 = make_fuzz_shader(fuzz, depth - 1);  // limit recursion.
            colorFilter = make_fuzz_colorfilter(fuzz, depth - 1);
            return shader1 ? shader1->makeWithColorFilter(std::move(colorFilter)) : nullptr;
        case 7:
            shader1 = make_fuzz_shader(fuzz, depth - 1);  // limit recursion.
            shader2 = make_fuzz_shader(fuzz, depth - 1);
            fuzz->next(&blendMode);
            return SkShader::MakeComposeShader(std::move(shader1), std::move(shader2), blendMode);
        case 8: {
            auto pic = make_fuzz_picture(fuzz, depth - 1);
            bool useTile;
            SkRect tile;
            fuzz->next(&tmX, &tmY, &useMatrix, &useTile);
            if (useMatrix) {
                fuzz->next(&matrix);
            }
            if (useTile) {
                fuzz->next(&tile);
            }
            return SkShader::MakePictureShader(std::move(pic), tmX, tmY,
                                               useMatrix ? &matrix : nullptr,
                                               useTile ? &tile : nullptr);
        }
        // EFFECTS:
        case 9:
            // Deprecated SkGaussianEdgeShader
            return nullptr;
        case 10: {
            constexpr int kMaxColors = 12;
            SkPoint pts[2];
            SkColor colors[kMaxColors];
            SkScalar pos[kMaxColors];
            int colorCount;
            bool usePos;
            fuzz->nextN(pts, 2);
            fuzz->nextRange(&colorCount, 2, kMaxColors);
            fuzz->nextN(colors, colorCount);
            fuzz->next(&tmX, &useMatrix, &usePos);
            if (useMatrix) {
                fuzz->next(&matrix);
            }
            if (usePos) {
                fuzz_gradient_stops(fuzz, pos, colorCount);
            }
            return SkGradientShader::MakeLinear(pts, colors, usePos ? pos : nullptr, colorCount,
                                                tmX, 0, useMatrix ? &matrix : nullptr);
        }
        case 11: {
            constexpr int kMaxColors = 12;
            SkPoint center;
            SkScalar radius;
            int colorCount;
            bool usePos;
            SkColor colors[kMaxColors];
            SkScalar pos[kMaxColors];
            fuzz->next(&tmX, &useMatrix, &usePos, &center, &radius);
            fuzz->nextRange(&colorCount, 2, kMaxColors);
            fuzz->nextN(colors, colorCount);
            if (useMatrix) {
                fuzz->next(&matrix);
            }
            if (usePos) {
                fuzz_gradient_stops(fuzz, pos, colorCount);
            }
            return SkGradientShader::MakeRadial(center, radius, colors, usePos ? pos : nullptr,
                                                colorCount, tmX, 0, useMatrix ? &matrix : nullptr);
        }
        case 12: {
            constexpr int kMaxColors = 12;
            SkPoint start, end;
            SkScalar startRadius, endRadius;
            int colorCount;
            bool usePos;
            SkColor colors[kMaxColors];
            SkScalar pos[kMaxColors];
            fuzz->next(&tmX, &useMatrix, &usePos, &startRadius, &endRadius, &start, &end);
            fuzz->nextRange(&colorCount, 2, kMaxColors);
            fuzz->nextN(colors, colorCount);
            if (useMatrix) {
                fuzz->next(&matrix);
            }
            if (usePos) {
                fuzz_gradient_stops(fuzz, pos, colorCount);
            }
            return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, colors,
                                                         usePos ? pos : nullptr, colorCount, tmX, 0,
                                                         useMatrix ? &matrix : nullptr);
        }
        case 13: {
            constexpr int kMaxColors = 12;
            SkScalar cx, cy;
            int colorCount;
            bool usePos;
            SkColor colors[kMaxColors];
            SkScalar pos[kMaxColors];
            fuzz->next(&cx, &cy, &useMatrix, &usePos);
            fuzz->nextRange(&colorCount, 2, kMaxColors);
            fuzz->nextN(colors, colorCount);
            if (useMatrix) {
                fuzz->next(&matrix);
            }
            if (usePos) {
                fuzz_gradient_stops(fuzz, pos, colorCount);
            }
            return SkGradientShader::MakeSweep(cx, cy, colors, usePos ? pos : nullptr, colorCount,
                                               0, useMatrix ? &matrix : nullptr);
        }
        case 14: {
            SkScalar baseFrequencyX, baseFrequencyY, seed;
            int numOctaves;
            SkISize tileSize;
            bool useTileSize, turbulence;
            fuzz->next(&baseFrequencyX, &baseFrequencyY, &seed, &useTileSize, &turbulence);
            if (useTileSize) {
                fuzz->next(&tileSize);
            }
            fuzz->nextRange(&numOctaves, 2, 7);
            if (turbulence) {
                return SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY,
                                                           numOctaves, seed,
                                                           useTileSize ? &tileSize : nullptr);
            } else {
                return SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
                                                             numOctaves, seed,
                                                             useTileSize ? &tileSize : nullptr);
            }
        }
        default:
            SkASSERT(false);
            break;
    }
    return nullptr;
}

static sk_sp<SkPathEffect> make_fuzz_patheffect(Fuzz* fuzz, int depth) {
    if (depth <= 0) {
        return nullptr;
    }
    uint8_t pathEffectType;
    fuzz->nextRange(&pathEffectType, 0, 8);
    switch (pathEffectType) {
        case 0: {
            return nullptr;
        }
        case 1: {
            sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
            sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
            return SkPathEffect::MakeSum(std::move(first), std::move(second));
        }
        case 2: {
            sk_sp<SkPathEffect> first = make_fuzz_patheffect(fuzz, depth - 1);
            sk_sp<SkPathEffect> second = make_fuzz_patheffect(fuzz, depth - 1);
            return SkPathEffect::MakeCompose(std::move(first), std::move(second));
        }
        case 3: {
            SkPath path;
            FuzzPath(fuzz, &path, 20);
            SkScalar advance, phase;
            fuzz->next(&advance, &phase);
            SkPath1DPathEffect::Style style;
            fuzz_enum_range(fuzz, &style, 0, SkPath1DPathEffect::kLastEnum_Style);
            return SkPath1DPathEffect::Make(path, advance, phase, style);
        }
        case 4: {
            SkScalar width;
            SkMatrix matrix;
            fuzz->next(&width, &matrix);
            return SkLine2DPathEffect::Make(width, matrix);
        }
        case 5: {
            SkPath path;
            FuzzPath(fuzz, &path, 20);
            SkMatrix matrix;
            fuzz->next(&matrix);
            return SkPath2DPathEffect::Make(matrix, path);
        }
        case 6: {
            SkScalar radius;
            fuzz->next(&radius);
            return SkCornerPathEffect::Make(radius);
        }
        case 7: {
            SkScalar phase;
            fuzz->next(&phase);
            SkScalar intervals[20];
            int count;
            fuzz->nextRange(&count, 0, (int)SK_ARRAY_COUNT(intervals));
            fuzz->nextN(intervals, count);
            return SkDashPathEffect::Make(intervals, count, phase);
        }
        case 8: {
            SkScalar segLength, dev;
            uint32_t seed;
            fuzz->next(&segLength, &dev, &seed);
            return SkDiscretePathEffect::Make(segLength, dev, seed);
        }
        default:
            SkASSERT(false);
            return nullptr;
    }
}

static sk_sp<SkMaskFilter> make_fuzz_maskfilter(Fuzz* fuzz) {
    int maskfilterType;
    fuzz->nextRange(&maskfilterType, 0, 2);
    switch (maskfilterType) {
        case 0:
            return nullptr;
        case 1: {
            SkBlurStyle blurStyle;
            fuzz_enum_range(fuzz, &blurStyle, 0, kLastEnum_SkBlurStyle);
            SkScalar sigma;
            fuzz->next(&sigma);
            SkRect occluder{0.0f, 0.0f, 0.0f, 0.0f};
            if (make_fuzz_t<bool>(fuzz)) {
                fuzz->next(&occluder);
            }
            uint32_t flags;
            fuzz->nextRange(&flags, 0, 1);
            bool respectCTM = flags != 0;
            return SkMaskFilter::MakeBlur(blurStyle, sigma, occluder, respectCTM);
        }
        case 2: {
            SkRRect first, second;
            SkScalar radius;
            fuzz->next(&first, &second, &radius);
            return SkRRectsGaussianEdgeMaskFilter::Make(first, second, radius);
        }
        default:
            SkASSERT(false);
            return nullptr;
    }
}

static sk_sp<SkTypeface> make_fuzz_typeface(Fuzz* fuzz) {
    if (make_fuzz_t<bool>(fuzz)) {
        return nullptr;
    }
    auto fontMugger = SkFontMgr::RefDefault();
    SkASSERT(fontMugger);
    int familyCount = fontMugger->countFamilies();
    int i, j;
    fuzz->nextRange(&i, 0, familyCount - 1);
    sk_sp<SkFontStyleSet> family(fontMugger->createStyleSet(i));
    int styleCount = family->count();
    fuzz->nextRange(&j, 0, styleCount - 1);
    return sk_sp<SkTypeface>(family->createTypeface(j));
}

template <>
inline void Fuzz::next(SkImageFilter::CropRect* cropRect) {
    SkRect rect;
    uint8_t flags;
    this->next(&rect);
    this->nextRange(&flags, 0, 0xF);
    *cropRect = SkImageFilter::CropRect(rect, flags);
}

static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth);

static sk_sp<SkImageFilter> make_fuzz_lighting_imagefilter(Fuzz* fuzz, int depth) {
    if (depth <= 0) {
        return nullptr;
    }
    uint8_t imageFilterType;
    fuzz->nextRange(&imageFilterType, 1, 6);
    SkPoint3 p, q;
    SkColor lightColor;
    SkScalar surfaceScale, k, specularExponent, cutoffAngle, shininess;
    sk_sp<SkImageFilter> input;
    SkImageFilter::CropRect cropRect;
    bool useCropRect;
    fuzz->next(&useCropRect);
    if (useCropRect) {
        fuzz->next(&cropRect);
    }
    switch (imageFilterType) {
        case 1:
            fuzz->next(&p, &lightColor, &surfaceScale, &k);
            input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkLightingImageFilter::MakeDistantLitDiffuse(p, lightColor, surfaceScale, k,
                                                                std::move(input),
                                                                useCropRect ? &cropRect : nullptr);
        case 2:
            fuzz->next(&p, &lightColor, &surfaceScale, &k);
            input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkLightingImageFilter::MakePointLitDiffuse(p, lightColor, surfaceScale, k,
                                                              std::move(input),
                                                              useCropRect ? &cropRect : nullptr);
        case 3:
            fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k);
            input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkLightingImageFilter::MakeSpotLitDiffuse(
                    p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k,
                    std::move(input), useCropRect ? &cropRect : nullptr);
        case 4:
            fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
            input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkLightingImageFilter::MakeDistantLitSpecular(p, lightColor, surfaceScale, k,
                                                                 shininess, std::move(input),
                                                                 useCropRect ? &cropRect : nullptr);
        case 5:
            fuzz->next(&p, &lightColor, &surfaceScale, &k, &shininess);
            input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkLightingImageFilter::MakePointLitSpecular(p, lightColor, surfaceScale, k,
                                                               shininess, std::move(input),
                                                               useCropRect ? &cropRect : nullptr);
        case 6:
            fuzz->next(&p, &q, &specularExponent, &cutoffAngle, &lightColor, &surfaceScale, &k,
                       &shininess);
            input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkLightingImageFilter::MakeSpotLitSpecular(
                    p, q, specularExponent, cutoffAngle, lightColor, surfaceScale, k, shininess,
                    std::move(input), useCropRect ? &cropRect : nullptr);
        default:
            SkASSERT(false);
            return nullptr;
    }
}

static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth);

static sk_sp<SkImageFilter> make_fuzz_imageFilter(Fuzz* fuzz, int depth) {
    if (depth <= 0) {
        return nullptr;
    }
    uint8_t imageFilterType;
    fuzz->nextRange(&imageFilterType, 0, 23);
    switch (imageFilterType) {
        case 0:
            return nullptr;
        case 1: {
            SkScalar sigmaX, sigmaY;
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            bool useCropRect;
            fuzz->next(&sigmaX, &sigmaY, &useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            return SkBlurImageFilter::Make(sigmaX, sigmaY, std::move(input),
                                           useCropRect ? &cropRect : nullptr);
        }
        case 2: {
            SkMatrix matrix;
            SkFilterQuality quality;
            fuzz->next(&matrix, &quality);
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkImageFilter::MakeMatrixFilter(matrix, quality, std::move(input));
        }
        case 3: {
            SkRegion region;
            SkScalar innerMin, outerMax;
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            bool useCropRect;
            fuzz->next(&region, &innerMin, &outerMax, &useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            return SkAlphaThresholdFilter::Make(region, innerMin, outerMax, std::move(input),
                                                useCropRect ? &cropRect : nullptr);
        }
        case 4: {
            float k1, k2, k3, k4;
            bool enforcePMColor;
            bool useCropRect;
            fuzz->next(&k1, &k2, &k3, &k4, &enforcePMColor, &useCropRect);
            sk_sp<SkImageFilter> background = make_fuzz_imageFilter(fuzz, depth - 1);
            sk_sp<SkImageFilter> foreground = make_fuzz_imageFilter(fuzz, depth - 1);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            return SkArithmeticImageFilter::Make(k1, k2, k3, k4, enforcePMColor,
                                                 std::move(background), std::move(foreground),
                                                 useCropRect ? &cropRect : nullptr);
        }
        case 5: {
            sk_sp<SkColorFilter> cf = make_fuzz_colorfilter(fuzz, depth - 1);
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            bool useCropRect;
            SkImageFilter::CropRect cropRect;
            fuzz->next(&useCropRect);
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            return SkColorFilterImageFilter::Make(std::move(cf), std::move(input),
                                                  useCropRect ? &cropRect : nullptr);
        }
        case 6: {
            sk_sp<SkImageFilter> ifo = make_fuzz_imageFilter(fuzz, depth - 1);
            sk_sp<SkImageFilter> ifi = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkComposeImageFilter::Make(std::move(ifo), std::move(ifi));
        }
        case 7: {
            SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, yChannelSelector;
            fuzz_enum_range(fuzz, &xChannelSelector, 1, 4);
            fuzz_enum_range(fuzz, &yChannelSelector, 1, 4);
            SkScalar scale;
            bool useCropRect;
            fuzz->next(&scale, &useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            sk_sp<SkImageFilter> displacement = make_fuzz_imageFilter(fuzz, depth - 1);
            sk_sp<SkImageFilter> color = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale,
                                                 std::move(displacement), std::move(color),
                                                 useCropRect ? &cropRect : nullptr);
        }
        case 8: {
            SkScalar dx, dy, sigmaX, sigmaY;
            SkColor color;
            SkDropShadowImageFilter::ShadowMode shadowMode;
            fuzz_enum_range(fuzz, &shadowMode, 0, 1);
            bool useCropRect;
            fuzz->next(&dx, &dy, &sigmaX, &sigmaY, &color, &useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkDropShadowImageFilter::Make(dx, dy, sigmaX, sigmaY, color, shadowMode,
                                                 std::move(input),
                                                 useCropRect ? &cropRect : nullptr);
        }
        case 9:
            return SkImageSource::Make(make_fuzz_image(fuzz));
        case 10: {
            sk_sp<SkImage> image = make_fuzz_image(fuzz);
            SkRect srcRect, dstRect;
            SkFilterQuality filterQuality;
            fuzz->next(&srcRect, &dstRect, &filterQuality);
            return SkImageSource::Make(std::move(image), srcRect, dstRect, filterQuality);
        }
        case 11:
            return make_fuzz_lighting_imagefilter(fuzz, depth - 1);
        case 12: {
            SkRect srcRect;
            SkScalar inset;
            bool useCropRect;
            SkImageFilter::CropRect cropRect;
            fuzz->next(&srcRect, &inset, &useCropRect);
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkMagnifierImageFilter::Make(srcRect, inset, std::move(input),
                                                useCropRect ? &cropRect : nullptr);
        }
        case 13: {
            constexpr int kMaxKernelSize = 5;
            int32_t n, m;
            fuzz->nextRange(&n, 1, kMaxKernelSize);
            fuzz->nextRange(&m, 1, kMaxKernelSize);
            SkScalar kernel[kMaxKernelSize * kMaxKernelSize];
            fuzz->nextN(kernel, n * m);
            int32_t offsetX, offsetY;
            fuzz->nextRange(&offsetX, 0, n - 1);
            fuzz->nextRange(&offsetY, 0, m - 1);
            SkScalar gain, bias;
            bool convolveAlpha, useCropRect;
            fuzz->next(&gain, &bias, &convolveAlpha, &useCropRect);
            SkMatrixConvolutionImageFilter::TileMode tileMode;
            fuzz_enum_range(fuzz, &tileMode, 0, 2);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkMatrixConvolutionImageFilter::Make(
                    SkISize{n, m}, kernel, gain, bias, SkIPoint{offsetX, offsetY}, tileMode,
                    convolveAlpha, std::move(input), useCropRect ? &cropRect : nullptr);
        }
        case 14: {
            sk_sp<SkImageFilter> first = make_fuzz_imageFilter(fuzz, depth - 1);
            sk_sp<SkImageFilter> second = make_fuzz_imageFilter(fuzz, depth - 1);
            bool useCropRect;
            fuzz->next(&useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            return SkMergeImageFilter::Make(std::move(first), std::move(second),
                                            useCropRect ? &cropRect : nullptr);
        }
        case 15: {
            constexpr int kMaxCount = 4;
            sk_sp<SkImageFilter> ifs[kMaxCount];
            int count;
            fuzz->nextRange(&count, 1, kMaxCount);
            for (int i = 0; i < count; ++i) {
                ifs[i] = make_fuzz_imageFilter(fuzz, depth - 1);
            }
            bool useCropRect;
            fuzz->next(&useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            return SkMergeImageFilter::Make(ifs, count, useCropRect ? &cropRect : nullptr);
        }
        case 16: {
            int rx, ry;
            fuzz->next(&rx, &ry);
            bool useCropRect;
            fuzz->next(&useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkDilateImageFilter::Make(rx, ry, std::move(input),
                                             useCropRect ? &cropRect : nullptr);
        }
        case 17: {
            int rx, ry;
            fuzz->next(&rx, &ry);
            bool useCropRect;
            fuzz->next(&useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkErodeImageFilter::Make(rx, ry, std::move(input),
                                            useCropRect ? &cropRect : nullptr);
        }
        case 18: {
            SkScalar dx, dy;
            fuzz->next(&dx, &dy);
            bool useCropRect;
            fuzz->next(&useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkOffsetImageFilter::Make(dx, dy, std::move(input),
                                             useCropRect ? &cropRect : nullptr);
        }
        case 19: {
            SkPaint paint;
            fuzz_paint(fuzz, &paint, depth - 1);
            bool useCropRect;
            fuzz->next(&useCropRect);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            return SkPaintImageFilter::Make(paint, useCropRect ? &cropRect : nullptr);
        }
        case 20: {
            sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
            return SkPictureImageFilter::Make(std::move(picture));
        }
        case 21: {
            SkRect cropRect;
            fuzz->next(&cropRect);
            sk_sp<SkPicture> picture = make_fuzz_picture(fuzz, depth - 1);
            return SkPictureImageFilter::Make(std::move(picture), cropRect);
        }
        case 22: {
            SkRect src, dst;
            fuzz->next(&src, &dst);
            sk_sp<SkImageFilter> input = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkTileImageFilter::Make(src, dst, std::move(input));
        }
        case 23: {
            SkBlendMode blendMode;
            bool useCropRect;
            fuzz->next(&useCropRect, &blendMode);
            SkImageFilter::CropRect cropRect;
            if (useCropRect) {
                fuzz->next(&cropRect);
            }
            sk_sp<SkImageFilter> bg = make_fuzz_imageFilter(fuzz, depth - 1);
            sk_sp<SkImageFilter> fg = make_fuzz_imageFilter(fuzz, depth - 1);
            return SkXfermodeImageFilter::Make(blendMode, std::move(bg), std::move(fg),
                                               useCropRect ? &cropRect : nullptr);
        }
        default:
            SkASSERT(false);
            return nullptr;
    }
}

static sk_sp<SkImage> make_fuzz_image(Fuzz* fuzz) {
    int w, h;
    fuzz->nextRange(&w, 1, 1024);
    fuzz->nextRange(&h, 1, 1024);
    SkAutoTMalloc<SkPMColor> data(w * h);
    SkPixmap pixmap(SkImageInfo::MakeN32Premul(w, h), data.get(), w * sizeof(SkPMColor));
    int n = w * h;
    for (int i = 0; i < n; ++i) {
        SkColor c;
        fuzz->next(&c);
        data[i] = SkPreMultiplyColor(c);
    }
    (void)data.release();
    return SkImage::MakeFromRaster(pixmap, [](const void* p, void*) { sk_free((void*)p); },
                                   nullptr);
}

static SkBitmap make_fuzz_bitmap(Fuzz* fuzz) {
    SkBitmap bitmap;
    int w, h;
    fuzz->nextRange(&w, 1, 1024);
    fuzz->nextRange(&h, 1, 1024);
    if (!bitmap.tryAllocN32Pixels(w, h)) {
        SkDEBUGF(("Could not allocate pixels %d x %d", w, h));
        return bitmap;
    }
    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            SkColor c;
            fuzz->next(&c);
            *bitmap.getAddr32(x, y) = SkPreMultiplyColor(c);
        }
    }
    return bitmap;
}

template <typename T, typename Min, typename Max>
inline T make_fuzz_t_range(Fuzz* fuzz, Min minv, Max maxv) {
    T value;
    fuzz_enum_range(fuzz, &value, minv, maxv);
    return value;
}

static void fuzz_paint(Fuzz* fuzz, SkPaint* paint, int depth) {
    if (!fuzz || !paint || depth <= 0) {
        return;
    }

    paint->setAntiAlias(    make_fuzz_t<bool>(fuzz));
    paint->setDither(       make_fuzz_t<bool>(fuzz));
    paint->setColor(        make_fuzz_t<SkColor>(fuzz));
    paint->setBlendMode(    make_fuzz_t_range<SkBlendMode>(fuzz, 0, SkBlendMode::kLastMode));
    paint->setFilterQuality(make_fuzz_t_range<SkFilterQuality>(fuzz, 0, kLast_SkFilterQuality));
    paint->setStyle(        make_fuzz_t_range<SkPaint::Style>(fuzz, 0, 2));
    paint->setShader(       make_fuzz_shader(fuzz, depth - 1));
    paint->setPathEffect(   make_fuzz_patheffect(fuzz, depth - 1));
    paint->setMaskFilter(   make_fuzz_maskfilter(fuzz));
    paint->setImageFilter(  make_fuzz_imageFilter(fuzz, depth - 1));
    paint->setColorFilter(  make_fuzz_colorfilter(fuzz, depth - 1));

    if (paint->getStyle() != SkPaint::kFill_Style) {
        paint->setStrokeWidth(make_fuzz_t<SkScalar>(fuzz));
        paint->setStrokeMiter(make_fuzz_t<SkScalar>(fuzz));
        paint->setStrokeCap(  make_fuzz_t_range<SkPaint::Cap>(fuzz, 0, SkPaint::kLast_Cap));
        paint->setStrokeJoin( make_fuzz_t_range<SkPaint::Join>(fuzz, 0, SkPaint::kLast_Join));
    }
}

static void fuzz_paint_text(Fuzz* fuzz, SkPaint* paint) {
    paint->setTypeface(          make_fuzz_typeface(fuzz));
    paint->setTextSize(          make_fuzz_t<SkScalar>(fuzz));
    paint->setTextScaleX(        make_fuzz_t<SkScalar>(fuzz));
    paint->setTextSkewX(         make_fuzz_t<SkScalar>(fuzz));
    paint->setLinearText(        make_fuzz_t<bool>(fuzz));
    paint->setSubpixelText(      make_fuzz_t<bool>(fuzz));
    paint->setLCDRenderText(     make_fuzz_t<bool>(fuzz));
    paint->setEmbeddedBitmapText(make_fuzz_t<bool>(fuzz));
    paint->setAutohinted(        make_fuzz_t<bool>(fuzz));
    paint->setVerticalText(      make_fuzz_t<bool>(fuzz));
    paint->setFakeBoldText(      make_fuzz_t<bool>(fuzz));
    paint->setDevKernText(       make_fuzz_t<bool>(fuzz));
    paint->setHinting(           make_fuzz_t_range<SkPaint::Hinting>(fuzz, 0,
                                                                     SkPaint::kFull_Hinting));
    paint->setTextAlign(         make_fuzz_t_range<SkPaint::Align>(fuzz, 0, 2));
}

static void fuzz_paint_text_encoding(Fuzz* fuzz, SkPaint* paint) {
    paint->setTextEncoding(make_fuzz_t_range<SkPaint::TextEncoding>(fuzz, 0, 3));
}

constexpr int kMaxGlyphCount = 30;

static SkTDArray<uint8_t> make_fuzz_text(Fuzz* fuzz, const SkPaint& paint) {
    SkTDArray<uint8_t> array;
    if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
        int glyphRange = paint.getTypeface() ? paint.getTypeface()->countGlyphs()
                                             : SkTypeface::MakeDefault()->countGlyphs();
        if (glyphRange == 0) {
            // Some fuzzing environments have no fonts, so empty array is the best
            // we can do.
            return array;
        }
        int glyphCount;
        fuzz->nextRange(&glyphCount, 1, kMaxGlyphCount);
        SkGlyphID* glyphs = (SkGlyphID*)array.append(glyphCount * sizeof(SkGlyphID));
        for (int i = 0; i < glyphCount; ++i) {
            fuzz->nextRange(&glyphs[i], 0, glyphRange - 1);
        }
        return array;
    }
    static const SkUnichar ranges[][2] = {
        {0x0020, 0x007F},
        {0x00A1, 0x0250},
        {0x0400, 0x0500},
    };
    int32_t count = 0;
    for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
        count += (ranges[i][1] - ranges[i][0]);
    }
    constexpr int kMaxLength = kMaxGlyphCount;
    SkUnichar buffer[kMaxLength];
    int length;
    fuzz->nextRange(&length, 1, kMaxLength);
    for (int j = 0; j < length; ++j) {
        int32_t value;
        fuzz->nextRange(&value, 0, count - 1);
        for (size_t i = 0; i < SK_ARRAY_COUNT(ranges); ++i) {
            if (value + ranges[i][0] < ranges[i][1]) {
                buffer[j] = value + ranges[i][0];
                break;
            } else {
                value -= (ranges[i][1] - ranges[i][0]);
            }
        }
    }
    switch (paint.getTextEncoding()) {
        case SkPaint::kUTF8_TextEncoding: {
            size_t utf8len = 0;
            for (int j = 0; j < length; ++j) {
                utf8len += SkUTF8_FromUnichar(buffer[j], nullptr);
            }
            char* ptr = (char*)array.append(utf8len);
            for (int j = 0; j < length; ++j) {
                ptr += SkUTF8_FromUnichar(buffer[j], ptr);
            }
        } break;
        case SkPaint::kUTF16_TextEncoding: {
            size_t utf16len = 0;
            for (int j = 0; j < length; ++j) {
                utf16len += SkUTF16_FromUnichar(buffer[j]);
            }
            uint16_t* ptr = (uint16_t*)array.append(utf16len * sizeof(uint16_t));
            for (int j = 0; j < length; ++j) {
                ptr += SkUTF16_FromUnichar(buffer[j], ptr);
            }
        } break;
        case SkPaint::kUTF32_TextEncoding:
            memcpy(array.append(length * sizeof(SkUnichar)), buffer, length * sizeof(SkUnichar));
            break;
        default:
            SkASSERT(false);
            break;
    }
    return array;
}

static sk_sp<SkTextBlob> make_fuzz_textblob(Fuzz* fuzz) {
    SkTextBlobBuilder textBlobBuilder;
    int8_t runCount;
    fuzz->nextRange(&runCount, (int8_t)1, (int8_t)8);
    while (runCount-- > 0) {
        SkPaint paint;
        fuzz_paint_text_encoding(fuzz, &paint);
        paint.setAntiAlias(make_fuzz_t<bool>(fuzz));
        paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
        SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
        int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
        SkASSERT(glyphCount <= kMaxGlyphCount);
        SkScalar x, y;
        const SkTextBlobBuilder::RunBuffer* buffer;
        uint8_t runType;
        fuzz->nextRange(&runType, (uint8_t)0, (uint8_t)2);
        switch (runType) {
            case 0:
                fuzz->next(&x, &y);
                // TODO: Test other variations of this.
                buffer = &textBlobBuilder.allocRun(paint, glyphCount, x, y);
                memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
                break;
            case 1:
                fuzz->next(&y);
                // TODO: Test other variations of this.
                buffer = &textBlobBuilder.allocRunPosH(paint, glyphCount, y);
                memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
                fuzz->nextN(buffer->pos, glyphCount);
                break;
            case 2:
                // TODO: Test other variations of this.
                buffer = &textBlobBuilder.allocRunPos(paint, glyphCount);
                memcpy(buffer->glyphs, text.begin(), SkToSizeT(text.count()));
                fuzz->nextN(buffer->pos, glyphCount * 2);
                break;
            default:
                SkASSERT(false);
                break;
        }
    }
    return textBlobBuilder.make();
}

static void fuzz_canvas(Fuzz* fuzz, SkCanvas* canvas, int depth = 9) {
    if (!fuzz || !canvas || depth <= 0) {
        return;
    }
    SkAutoCanvasRestore autoCanvasRestore(canvas, false);
    unsigned N;
    fuzz->nextRange(&N, 0, 2000);
    for (unsigned i = 0; i < N; ++i) {
        if (fuzz->exhausted()) {
            return;
        }
        SkPaint paint;
        SkMatrix matrix;
        unsigned drawCommand;
        fuzz->nextRange(&drawCommand, 0, 53);
        switch (drawCommand) {
            case 0:
                canvas->flush();
                break;
            case 1:
                canvas->save();
                break;
            case 2: {
                SkRect bounds;
                fuzz->next(&bounds);
                fuzz_paint(fuzz, &paint, depth - 1);
                canvas->saveLayer(&bounds, &paint);
                break;
            }
            case 3: {
                SkRect bounds;
                fuzz->next(&bounds);
                canvas->saveLayer(&bounds, nullptr);
                break;
            }
            case 4:
                fuzz_paint(fuzz, &paint, depth - 1);
                canvas->saveLayer(nullptr, &paint);
                break;
            case 5:
                canvas->saveLayer(nullptr, nullptr);
                break;
            case 6: {
                uint8_t alpha;
                fuzz->next(&alpha);
                canvas->saveLayerAlpha(nullptr, (U8CPU)alpha);
                break;
            }
            case 7: {
                SkRect bounds;
                uint8_t alpha;
                fuzz->next(&bounds, &alpha);
                canvas->saveLayerAlpha(&bounds, (U8CPU)alpha);
                break;
            }
            case 8: {
                SkCanvas::SaveLayerRec saveLayerRec;
                SkRect bounds;
                if (make_fuzz_t<bool>(fuzz)) {
                    fuzz->next(&bounds);
                    saveLayerRec.fBounds = &bounds;
                }
                if (make_fuzz_t<bool>(fuzz)) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                    saveLayerRec.fPaint = &paint;
                }
                sk_sp<SkImageFilter> imageFilter;
                if (make_fuzz_t<bool>(fuzz)) {
                    imageFilter = make_fuzz_imageFilter(fuzz, depth - 1);
                    saveLayerRec.fBackdrop = imageFilter.get();
                }
                // _DumpCanvas can't handle this.
                // if (make_fuzz_t<bool>(fuzz)) {
                //     saveLayerRec.fSaveLayerFlags |= SkCanvas::kPreserveLCDText_SaveLayerFlag;
                // }

                canvas->saveLayer(saveLayerRec);
                break;
            }
            case 9:
                canvas->restore();
                break;
            case 10: {
                int saveCount;
                fuzz->next(&saveCount);
                canvas->restoreToCount(saveCount);
                break;
            }
            case 11: {
                SkScalar x, y;
                fuzz->next(&x, &y);
                canvas->translate(x, y);
                break;
            }
            case 12: {
                SkScalar x, y;
                fuzz->next(&x, &y);
                canvas->scale(x, y);
                break;
            }
            case 13: {
                SkScalar v;
                fuzz->next(&v);
                canvas->rotate(v);
                break;
            }
            case 14: {
                SkScalar x, y, v;
                fuzz->next(&x, &y, &v);
                canvas->rotate(v, x, y);
                break;
            }
            case 15: {
                SkScalar x, y;
                fuzz->next(&x, &y);
                canvas->skew(x, y);
                break;
            }
            case 16: {
                SkMatrix mat;
                fuzz->next(&mat);
                canvas->concat(mat);
                break;
            }
            case 17: {
                SkMatrix mat;
                fuzz->next(&mat);
                canvas->setMatrix(mat);
                break;
            }
            case 18:
                canvas->resetMatrix();
                break;
            case 19: {
                SkRect r;
                int op;
                bool doAntiAlias;
                fuzz->next(&r, &doAntiAlias);
                fuzz->nextRange(&op, 0, 1);
                r.sort();
                canvas->clipRect(r, (SkClipOp)op, doAntiAlias);
                break;
            }
            case 20: {
                SkRRect rr;
                int op;
                bool doAntiAlias;
                fuzz->next(&rr);
                fuzz->next(&doAntiAlias);
                fuzz->nextRange(&op, 0, 1);
                canvas->clipRRect(rr, (SkClipOp)op, doAntiAlias);
                break;
            }
            case 21: {
                SkPath path;
                FuzzPath(fuzz, &path, 30);
                int op;
                bool doAntiAlias;
                fuzz->next(&doAntiAlias);
                fuzz->nextRange(&op, 0, 1);
                canvas->clipPath(path, (SkClipOp)op, doAntiAlias);
                break;
            }
            case 22: {
                SkRegion region;
                int op;
                fuzz->next(&region);
                fuzz->nextRange(&op, 0, 1);
                canvas->clipRegion(region, (SkClipOp)op);
                break;
            }
            case 23:
                fuzz_paint(fuzz, &paint, depth - 1);
                canvas->drawPaint(paint);
                break;
            case 24: {
                fuzz_paint(fuzz, &paint, depth - 1);
                SkCanvas::PointMode pointMode;
                fuzz_enum_range(fuzz, &pointMode,
                                SkCanvas::kPoints_PointMode, SkCanvas::kPolygon_PointMode);
                size_t count;
                constexpr int kMaxCount = 30;
                fuzz->nextRange(&count, 0, kMaxCount);
                SkPoint pts[kMaxCount];
                fuzz->nextN(pts, count);
                canvas->drawPoints(pointMode, count, pts, paint);
                break;
            }
            case 25: {
                fuzz_paint(fuzz, &paint, depth - 1);
                SkRect r;
                fuzz->next(&r);
                if (!r.isFinite()) {
                    break;
                }
                canvas->drawRect(r, paint);
                break;
            }
            case 26: {
                fuzz_paint(fuzz, &paint, depth - 1);
                SkRegion region;
                fuzz->next(&region);
                canvas->drawRegion(region, paint);
                break;
            }
            case 27: {
                fuzz_paint(fuzz, &paint, depth - 1);
                SkRect r;
                fuzz->next(&r);
                if (!r.isFinite()) {
                    break;
                }
                canvas->drawOval(r, paint);
                break;
            }
            case 28: break; // must have deleted this some time earlier
            case 29: {
                fuzz_paint(fuzz, &paint, depth - 1);
                SkRRect rr;
                fuzz->next(&rr);
                canvas->drawRRect(rr, paint);
                break;
            }
            case 30: {
                fuzz_paint(fuzz, &paint, depth - 1);
                SkRRect orr, irr;
                fuzz->next(&orr);
                fuzz->next(&irr);
                if (orr.getBounds().contains(irr.getBounds())) {
                    canvas->drawDRRect(orr, irr, paint);
                }
                break;
            }
            case 31: {
                fuzz_paint(fuzz, &paint, depth - 1);
                SkRect r;
                SkScalar start, sweep;
                bool useCenter;
                fuzz->next(&r, &start, &sweep, &useCenter);
                canvas->drawArc(r, start, sweep, useCenter, paint);
                break;
            }
            case 32: {
                SkPath path;
                FuzzPath(fuzz, &path, 60);
                canvas->drawPath(path, paint);
                break;
            }
            case 33: {
                sk_sp<SkImage> img = make_fuzz_image(fuzz);
                SkScalar left, top;
                bool usePaint;
                fuzz->next(&left, &top, &usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                canvas->drawImage(img.get(), left, top, usePaint ? &paint : nullptr);
                break;
            }
            case 34: {
                auto img = make_fuzz_image(fuzz);
                SkRect src, dst;
                bool usePaint;
                fuzz->next(&src, &dst, &usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                SkCanvas::SrcRectConstraint constraint =
                        make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
                                                : SkCanvas::kFast_SrcRectConstraint;
                canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
                break;
            }
            case 35: {
                auto img = make_fuzz_image(fuzz);
                SkIRect src;
                SkRect dst;
                bool usePaint;
                fuzz->next(&src, &dst, &usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                SkCanvas::SrcRectConstraint constraint =
                        make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
                                                : SkCanvas::kFast_SrcRectConstraint;
                canvas->drawImageRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
                break;
            }
            case 36: {
                bool usePaint;
                auto img = make_fuzz_image(fuzz);
                SkRect dst;
                fuzz->next(&dst, &usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                SkCanvas::SrcRectConstraint constraint =
                        make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
                                                : SkCanvas::kFast_SrcRectConstraint;
                canvas->drawImageRect(img, dst, usePaint ? &paint : nullptr, constraint);
                break;
            }
            case 37: {
                auto img = make_fuzz_image(fuzz);
                SkIRect center;
                SkRect dst;
                bool usePaint;
                fuzz->next(&usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                if (make_fuzz_t<bool>(fuzz)) {
                    fuzz->next(&center);
                } else {  // Make valid center, see SkLatticeIter::Valid().
                    fuzz->nextRange(&center.fLeft, 0, img->width() - 1);
                    fuzz->nextRange(&center.fTop, 0, img->height() - 1);
                    fuzz->nextRange(&center.fRight, center.fLeft + 1, img->width());
                    fuzz->nextRange(&center.fBottom, center.fTop + 1, img->height());
                }
                fuzz->next(&dst);
                canvas->drawImageNine(img, center, dst, usePaint ? &paint : nullptr);
                break;
            }
            case 38: {
                SkBitmap bitmap = make_fuzz_bitmap(fuzz);
                SkScalar left, top;
                bool usePaint;
                fuzz->next(&left, &top, &usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                canvas->drawBitmap(bitmap, left, top, usePaint ? &paint : nullptr);
                break;
            }
            case 39: {
                SkBitmap bitmap = make_fuzz_bitmap(fuzz);
                SkRect src, dst;
                bool usePaint;
                fuzz->next(&src, &dst, &usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                SkCanvas::SrcRectConstraint constraint =
                        make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
                                                : SkCanvas::kFast_SrcRectConstraint;
                canvas->drawBitmapRect(bitmap, src, dst, usePaint ? &paint : nullptr, constraint);
                break;
            }
            case 40: {
                SkBitmap img = make_fuzz_bitmap(fuzz);
                SkIRect src;
                SkRect dst;
                bool usePaint;
                fuzz->next(&src, &dst, &usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                SkCanvas::SrcRectConstraint constraint =
                        make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
                                                : SkCanvas::kFast_SrcRectConstraint;
                canvas->drawBitmapRect(img, src, dst, usePaint ? &paint : nullptr, constraint);
                break;
            }
            case 41: {
                SkBitmap img = make_fuzz_bitmap(fuzz);
                SkRect dst;
                bool usePaint;
                fuzz->next(&dst, &usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                SkCanvas::SrcRectConstraint constraint =
                        make_fuzz_t<bool>(fuzz) ? SkCanvas::kStrict_SrcRectConstraint
                                                : SkCanvas::kFast_SrcRectConstraint;
                canvas->drawBitmapRect(img, dst, usePaint ? &paint : nullptr, constraint);
                break;
            }
            case 42: {
                SkBitmap img = make_fuzz_bitmap(fuzz);
                SkIRect center;
                SkRect dst;
                bool usePaint;
                fuzz->next(&usePaint);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                if (make_fuzz_t<bool>(fuzz)) {
                    fuzz->next(&center);
                } else {  // Make valid center, see SkLatticeIter::Valid().
                    if (img.width() == 0 || img.height() == 0) {
                        // bitmap may not have had its pixels initialized.
                        break;
                    }
                    fuzz->nextRange(&center.fLeft, 0, img.width() - 1);
                    fuzz->nextRange(&center.fTop, 0, img.height() - 1);
                    fuzz->nextRange(&center.fRight, center.fLeft + 1, img.width());
                    fuzz->nextRange(&center.fBottom, center.fTop + 1, img.height());
                }
                fuzz->next(&dst);
                canvas->drawBitmapNine(img, center, dst, usePaint ? &paint : nullptr);
                break;
            }
            case 43: {
                SkBitmap img = make_fuzz_bitmap(fuzz);
                bool usePaint;
                SkRect dst;
                fuzz->next(&usePaint, &dst);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                constexpr int kMax = 6;
                int xDivs[kMax], yDivs[kMax];
                SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
                fuzz->nextRange(&lattice.fXCount, 2, kMax);
                fuzz->nextRange(&lattice.fYCount, 2, kMax);
                fuzz->nextN(xDivs, lattice.fXCount);
                fuzz->nextN(yDivs, lattice.fYCount);
                canvas->drawBitmapLattice(img, lattice, dst, usePaint ? &paint : nullptr);
                break;
            }
            case 44: {
                auto img = make_fuzz_image(fuzz);
                bool usePaint;
                SkRect dst;
                fuzz->next(&usePaint, &dst);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                constexpr int kMax = 6;
                int xDivs[kMax], yDivs[kMax];
                SkCanvas::Lattice lattice{xDivs, yDivs, nullptr, 0, 0, nullptr, nullptr};
                fuzz->nextRange(&lattice.fXCount, 2, kMax);
                fuzz->nextRange(&lattice.fYCount, 2, kMax);
                fuzz->nextN(xDivs, lattice.fXCount);
                fuzz->nextN(yDivs, lattice.fYCount);
                canvas->drawImageLattice(img.get(), lattice, dst, usePaint ? &paint : nullptr);
                break;
            }
            case 45: {
                fuzz_paint(fuzz, &paint, depth - 1);
                fuzz_paint_text(fuzz, &paint);
                fuzz_paint_text_encoding(fuzz, &paint);
                SkScalar x, y;
                fuzz->next(&x, &y);
                SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
                canvas->drawText(text.begin(), SkToSizeT(text.count()), x, y, paint);
                break;
            }
            case 46: {
                fuzz_paint(fuzz, &paint, depth - 1);
                fuzz_paint_text(fuzz, &paint);
                fuzz_paint_text_encoding(fuzz, &paint);
                SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
                int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
                if (glyphCount < 1) {
                    break;
                }
                SkAutoTMalloc<SkPoint> pos(glyphCount);
                SkAutoTMalloc<SkScalar> widths(glyphCount);
                paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
                pos[0] = {0, 0};
                for (int i = 1; i < glyphCount; ++i) {
                    float y;
                    fuzz->nextRange(&y, -0.5f * paint.getTextSize(), 0.5f * paint.getTextSize());
                    pos[i] = {pos[i - 1].x() + widths[i - 1], y};
                }
                canvas->drawPosText(text.begin(), SkToSizeT(text.count()), pos.get(), paint);
                break;
            }
            case 47: {
                fuzz_paint(fuzz, &paint, depth - 1);
                fuzz_paint_text(fuzz, &paint);
                fuzz_paint_text_encoding(fuzz, &paint);
                SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
                int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
                SkAutoTMalloc<SkScalar> widths(glyphCount);
                if (glyphCount < 1) {
                    break;
                }
                paint.getTextWidths(text.begin(), SkToSizeT(text.count()), widths.get());
                SkScalar x = widths[0];
                for (int i = 0; i < glyphCount; ++i) {
                    SkTSwap(x, widths[i]);
                    x += widths[i];
                    SkScalar offset;
                    fuzz->nextRange(&offset, -0.125f * paint.getTextSize(),
                                    0.125f * paint.getTextSize());
                    widths[i] += offset;
                }
                SkScalar y;
                fuzz->next(&y);
                canvas->drawPosTextH(text.begin(), SkToSizeT(text.count()), widths.get(), y, paint);
                break;
            }
            case 48: {
                fuzz_paint(fuzz, &paint, depth - 1);
                fuzz_paint_text(fuzz, &paint);
                fuzz_paint_text_encoding(fuzz, &paint);
                SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
                SkPath path;
                FuzzPath(fuzz, &path, 20);
                SkScalar hOffset, vOffset;
                fuzz->next(&hOffset, &vOffset);
                canvas->drawTextOnPathHV(text.begin(), SkToSizeT(text.count()), path, hOffset,
                                         vOffset, paint);
                break;
            }
            case 49: {
                SkMatrix matrix;
                bool useMatrix = make_fuzz_t<bool>(fuzz);
                if (useMatrix) {
                    fuzz->next(&matrix);
                }
                fuzz_paint(fuzz, &paint, depth - 1);
                fuzz_paint_text(fuzz, &paint);
                fuzz_paint_text_encoding(fuzz, &paint);
                SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
                SkPath path;
                FuzzPath(fuzz, &path, 20);
                canvas->drawTextOnPath(text.begin(), SkToSizeT(text.count()), path,
                                       useMatrix ? &matrix : nullptr, paint);
                break;
            }
            case 50: {
                fuzz_paint(fuzz, &paint, depth - 1);
                fuzz_paint_text(fuzz, &paint);
                fuzz_paint_text_encoding(fuzz, &paint);
                SkTDArray<uint8_t> text = make_fuzz_text(fuzz, paint);
                SkRSXform rSXform[kMaxGlyphCount];
                int glyphCount = paint.countText(text.begin(), SkToSizeT(text.count()));
                SkASSERT(glyphCount <= kMaxGlyphCount);
                fuzz->nextN(rSXform, glyphCount);
                SkRect cullRect;
                bool useCullRect;
                fuzz->next(&useCullRect);
                if (useCullRect) {
                    fuzz->next(&cullRect);
                }
                canvas->drawTextRSXform(text.begin(), SkToSizeT(text.count()), rSXform,
                                        useCullRect ? &cullRect : nullptr, paint);
                break;
            }
            case 51: {
                sk_sp<SkTextBlob> blob = make_fuzz_textblob(fuzz);
                fuzz_paint(fuzz, &paint, depth - 1);
                SkScalar x, y;
                fuzz->next(&x, &y);
                canvas->drawTextBlob(blob, x, y, paint);
                break;
            }
            case 52: {
                bool usePaint, useMatrix;
                fuzz->next(&usePaint, &useMatrix);
                if (usePaint) {
                    fuzz_paint(fuzz, &paint, depth - 1);
                }
                if (useMatrix) {
                    fuzz->next(&matrix);
                }
                auto pic = make_fuzz_picture(fuzz, depth - 1);
                canvas->drawPicture(pic, useMatrix ? &matrix : nullptr,
                                    usePaint ? &paint : nullptr);
                break;
            }
            case 53: {
                fuzz_paint(fuzz, &paint, depth - 1);
                SkVertices::VertexMode vertexMode;
                SkBlendMode blendMode;
                fuzz_enum_range(fuzz, &vertexMode, 0, SkVertices::kTriangleFan_VertexMode);
                fuzz->next(&blendMode);
                constexpr int kMaxCount = 100;
                int vertexCount;
                SkPoint vertices[kMaxCount];
                SkPoint texs[kMaxCount];
                SkColor colors[kMaxCount];
                fuzz->nextRange(&vertexCount, 3, kMaxCount);
                fuzz->nextN(vertices, vertexCount);
                bool useTexs, useColors;
                fuzz->next(&useTexs, &useColors);
                if (useTexs) {
                    fuzz->nextN(texs, vertexCount);
                }
                if (useColors) {
                    fuzz->nextN(colors, vertexCount);
                }
                int indexCount = 0;
                uint16_t indices[kMaxCount * 2];
                if (make_fuzz_t<bool>(fuzz)) {
                    fuzz->nextRange(&indexCount, vertexCount, vertexCount + kMaxCount);
                    for (int i = 0; i < indexCount; ++i) {
                        fuzz->nextRange(&indices[i], 0, vertexCount - 1);
                    }
                }
                canvas->drawVertices(SkVertices::MakeCopy(vertexMode, vertexCount, vertices,
                                                          useTexs ? texs : nullptr,
                                                          useColors ? colors : nullptr,
                                                          indexCount, indices),
                                     blendMode, paint);
                break;
            }
            default:
                SkASSERT(false);
                break;
        }
    }
}

static sk_sp<SkPicture> make_fuzz_picture(Fuzz* fuzz, int depth) {
    SkScalar w, h;
    fuzz->next(&w, &h);
    SkPictureRecorder pictureRecorder;
    fuzz_canvas(fuzz, pictureRecorder.beginRecording(w, h), depth - 1);
    return pictureRecorder.finishRecordingAsPicture();
}

DEF_FUZZ(NullCanvas, fuzz) {
    fuzz_canvas(fuzz, SkMakeNullCanvas().get());
}

constexpr SkISize kCanvasSize = {128, 160};

DEF_FUZZ(RasterN32Canvas, fuzz) {
    auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
    if (!surface || !surface->getCanvas()) { fuzz->signalBug(); }
    fuzz_canvas(fuzz, surface->getCanvas());
}

DEF_FUZZ(RasterN32CanvasViaSerialization, fuzz) {
    SkPictureRecorder recorder;
    fuzz_canvas(fuzz, recorder.beginRecording(SkIntToScalar(kCanvasSize.width()),
                                              SkIntToScalar(kCanvasSize.height())));
    sk_sp<SkPicture> pic(recorder.finishRecordingAsPicture());
    if (!pic) { fuzz->signalBug(); }
    sk_sp<SkData> data = pic->serialize();
    if (!data) { fuzz->signalBug(); }
    SkReadBuffer rb(data->data(), data->size());
    auto deserialized = SkPicture::MakeFromBuffer(rb);
    if (!deserialized) { fuzz->signalBug(); }
    auto surface = SkSurface::MakeRasterN32Premul(kCanvasSize.width(), kCanvasSize.height());
    SkASSERT(surface && surface->getCanvas());
    surface->getCanvas()->drawPicture(deserialized);
}

DEF_FUZZ(ImageFilter, fuzz) {
    auto fil = make_fuzz_imageFilter(fuzz, 20);

    SkPaint paint;
    paint.setImageFilter(fil);
    SkBitmap bitmap;
    SkCanvas canvas(bitmap);
    canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
}


//SkRandom _rand;
#define SK_ADD_RANDOM_BIT_FLIPS

DEF_FUZZ(SerializedImageFilter, fuzz) {
    auto filter = make_fuzz_imageFilter(fuzz, 20);
    auto data = filter->serialize();
    const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
    size_t len = data->size();
#ifdef SK_ADD_RANDOM_BIT_FLIPS
    unsigned char* p = const_cast<unsigned char*>(ptr);
    for (size_t i = 0; i < len; ++i, ++p) {
        uint8_t j;
        fuzz->nextRange(&j, 1, 250);
        if (j == 1) { // 0.4% of the time, flip a bit or byte
            uint8_t k;
            fuzz->nextRange(&k, 1, 10);
            if (k == 1) { // Then 10% of the time, change a whole byte
                uint8_t s;
                fuzz->nextRange(&s, 0, 2);
                switch(s) {
                case 0:
                    *p ^= 0xFF; // Flip entire byte
                    break;
                case 1:
                    *p = 0xFF; // Set all bits to 1
                    break;
                case 2:
                    *p = 0x00; // Set all bits to 0
                    break;
                }
            } else {
                uint8_t s;
                fuzz->nextRange(&s, 0, 7);
                *p ^= (1 << 7);
            }
        }
    }
#endif // SK_ADD_RANDOM_BIT_FLIPS
    auto deserializedFil = SkImageFilter::Deserialize(ptr, len);

    // uncomment below to write out a serialized image filter (to make corpus
    // for -t filter_fuzz)
    // SkString s("./serialized_filters/sf");
    // s.appendU32(_rand.nextU());
    // auto file = sk_fopen(s.c_str(), SkFILE_Flags::kWrite_SkFILE_Flag);
    // sk_fwrite(data->bytes(), data->size(), file);
    // sk_fclose(file);

    SkPaint paint;
    paint.setImageFilter(deserializedFil);
    SkBitmap bitmap;
    SkCanvas canvas(bitmap);
    canvas.saveLayer(SkRect::MakeWH(500, 500), &paint);
}

#if SK_SUPPORT_GPU
static void fuzz_ganesh(Fuzz* fuzz, GrContext* context) {
    SkASSERT(context);
    auto surface = SkSurface::MakeRenderTarget(
            context,
            SkBudgeted::kNo,
            SkImageInfo::Make(kCanvasSize.width(), kCanvasSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType));
    SkASSERT(surface && surface->getCanvas());
    fuzz_canvas(fuzz, surface->getCanvas());
}

DEF_FUZZ(NativeGLCanvas, fuzz) {
    sk_gpu_test::GrContextFactory f;
    GrContext* context = f.get(sk_gpu_test::GrContextFactory::kGL_ContextType);
    if (!context) {
        context = f.get(sk_gpu_test::GrContextFactory::kGLES_ContextType);
    }
    fuzz_ganesh(fuzz, context);
}

// This target is deprecated, NullGLContext is not well maintained.
// Please use MockGPUCanvas instead.
DEF_FUZZ(NullGLCanvas, fuzz) {
    sk_gpu_test::GrContextFactory f;
    fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kNullGL_ContextType));
}

// This target is deprecated, DebugGLContext is not well maintained.
// Please use MockGPUCanvas instead.
DEF_FUZZ(DebugGLCanvas, fuzz) {
    sk_gpu_test::GrContextFactory f;
    fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kDebugGL_ContextType));
}

DEF_FUZZ(MockGPUCanvas, fuzz) {
    sk_gpu_test::GrContextFactory f;
    fuzz_ganesh(fuzz, f.get(sk_gpu_test::GrContextFactory::kMock_ContextType));
}
#endif

DEF_FUZZ(PDFCanvas, fuzz) {
    SkNullWStream stream;
    auto doc = SkDocument::MakePDF(&stream);
    fuzz_canvas(fuzz, doc->beginPage(SkIntToScalar(kCanvasSize.width()),
                                     SkIntToScalar(kCanvasSize.height())));
}

// not a "real" thing to fuzz, used to debug errors found while fuzzing.
DEF_FUZZ(_DumpCanvas, fuzz) {
    SkDebugCanvas debugCanvas(kCanvasSize.width(), kCanvasSize.height());
    fuzz_canvas(fuzz, &debugCanvas);
    std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
    UrlDataManager dataManager(SkString("data"));
    Json::Value json = debugCanvas.toJSON(dataManager, debugCanvas.getSize(), nullCanvas.get());
    Json::StyledStreamWriter("  ").write(std::cout, json);
}