1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #include "SampleCode.h"
8 #include "Sk1DPathEffect.h"
9 #include "Sk2DPathEffect.h"
10 #include "SkAlphaThresholdFilter.h"
11 #include "SkArcToPathEffect.h"
12 #include "SkBlurImageFilter.h"
13 #include "SkBlurMaskFilter.h"
14 #include "SkCanvas.h"
15 #include "SkColorFilter.h"
16 #include "SkColorFilterImageFilter.h"
17 #include "SkColorMatrixFilter.h"
18 #include "SkComposeImageFilter.h"
19 #include "SkCornerPathEffect.h"
20 #include "SkDashPathEffect.h"
21 #include "SkData.h"
22 #include "SkDiscretePathEffect.h"
23 #include "SkDisplacementMapEffect.h"
24 #include "SkDropShadowImageFilter.h"
25 #include "SkEmbossMaskFilter.h"
26 #include "SkFlattenableSerialization.h"
27 #include "SkImageSource.h"
28 #include "SkLayerRasterizer.h"
29 #include "SkLightingImageFilter.h"
30 #include "SkLumaColorFilter.h"
31 #include "SkMagnifierImageFilter.h"
32 #include "SkMatrixConvolutionImageFilter.h"
33 #include "SkMergeImageFilter.h"
34 #include "SkMorphologyImageFilter.h"
35 #include "SkOffsetImageFilter.h"
36 #include "SkPaintImageFilter.h"
37 #include "SkPerlinNoiseShader.h"
38 #include "SkPictureImageFilter.h"
39 #include "SkPictureRecorder.h"
40 #include "SkPoint3.h"
41 #include "SkRandom.h"
42 #include "SkTableColorFilter.h"
43 #include "SkTileImageFilter.h"
44 #include "SkTypeface.h"
45 #include "SkView.h"
46 #include "SkXfermodeImageFilter.h"
47 #include <stdio.h>
48 #include <time.h>
49 
50 //#define SK_ADD_RANDOM_BIT_FLIPS
51 //#define SK_FUZZER_IS_VERBOSE
52 
53 static const uint32_t kSeed = (uint32_t)(time(nullptr));
54 static SkRandom gRand(kSeed);
55 static bool return_large = false;
56 static bool return_undef = false;
57 
58 static const int kBitmapSize = 24;
59 
R(float x)60 static int R(float x) {
61     return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
62 }
63 
64 #if defined _WIN32
65 #pragma warning ( push )
66 // we are intentionally causing an overflow here
67 //      (warning C4756: overflow in constant arithmetic)
68 #pragma warning ( disable : 4756 )
69 #endif
70 
huge()71 static float huge() {
72     double d = 1e100;
73     float f = (float)d;
74     return f;
75 }
76 
77 #if defined _WIN32
78 #pragma warning ( pop )
79 #endif
80 
make_number(bool positiveOnly)81 static float make_number(bool positiveOnly) {
82     float f = positiveOnly ? 1.0f : 0.0f;
83     float v = f;
84     int sel;
85 
86     if (return_large) sel = R(6); else sel = R(4);
87     if (!return_undef && sel == 0) sel = 1;
88 
89     if (R(2) == 1) v = (float)(R(100)+f); else
90 
91     switch (sel) {
92         case 0: break;
93         case 1: v = f; break;
94         case 2: v = 0.000001f; break;
95         case 3: v = 10000.0f; break;
96         case 4: v = 2000000000.0f; break;
97         case 5: v = huge(); break;
98     }
99 
100     if (!positiveOnly && (R(4) == 1)) v = -v;
101     return v;
102 }
103 
make_scalar(bool positiveOnly=false)104 static SkScalar make_scalar(bool positiveOnly = false) {
105     return make_number(positiveOnly);
106 }
107 
make_string()108 static SkString make_string() {
109     int length = R(1000);
110     SkString str(length);
111     for (int i = 0; i < length; ++i) {
112         str[i] = static_cast<char>(R(256));
113     }
114     return str;
115 }
116 
make_font_name()117 static SkString make_font_name() {
118     int sel = R(8);
119 
120     switch(sel) {
121         case 0: return SkString("Courier New");
122         case 1: return SkString("Helvetica");
123         case 2: return SkString("monospace");
124         case 3: return SkString("sans-serif");
125         case 4: return SkString("serif");
126         case 5: return SkString("Times");
127         case 6: return SkString("Times New Roman");
128         case 7:
129         default:
130             return make_string();
131     }
132 }
133 
make_bool()134 static bool make_bool() {
135     return R(2) == 1;
136 }
137 
make_rect()138 static SkRect make_rect() {
139     return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))),
140                           SkIntToScalar(R(static_cast<float>(kBitmapSize))));
141 }
142 
make_region()143 static SkRegion make_region() {
144     SkIRect iRegion = SkIRect::MakeXYWH(R(static_cast<float>(kBitmapSize)),
145                                         R(static_cast<float>(kBitmapSize)),
146                                         R(static_cast<float>(kBitmapSize)),
147                                         R(static_cast<float>(kBitmapSize)));
148     return SkRegion(iRegion);
149 }
150 
make_matrix()151 static SkMatrix make_matrix() {
152     SkMatrix m;
153     for (int i = 0; i < 9; ++i) {
154         m[i] = make_scalar();
155     }
156     return m;
157 }
158 
make_xfermode()159 static SkBlendMode make_xfermode() {
160     return static_cast<SkBlendMode>(R((int)SkBlendMode::kLastMode+1));
161 }
162 
make_paint_align()163 static SkPaint::Align make_paint_align() {
164     return static_cast<SkPaint::Align>(R(SkPaint::kRight_Align+1));
165 }
166 
make_paint_hinting()167 static SkPaint::Hinting make_paint_hinting() {
168     return static_cast<SkPaint::Hinting>(R(SkPaint::kFull_Hinting+1));
169 }
170 
make_paint_style()171 static SkPaint::Style make_paint_style() {
172     return static_cast<SkPaint::Style>(R(SkPaint::kStrokeAndFill_Style+1));
173 }
174 
make_paint_cap()175 static SkPaint::Cap make_paint_cap() {
176     return static_cast<SkPaint::Cap>(R(SkPaint::kDefault_Cap+1));
177 }
178 
make_paint_join()179 static SkPaint::Join make_paint_join() {
180     return static_cast<SkPaint::Join>(R(SkPaint::kDefault_Join+1));
181 }
182 
make_paint_text_encoding()183 static SkPaint::TextEncoding make_paint_text_encoding() {
184     return static_cast<SkPaint::TextEncoding>(R(SkPaint::kGlyphID_TextEncoding+1));
185 }
186 
make_blur_style()187 static SkBlurStyle make_blur_style() {
188     return static_cast<SkBlurStyle>(R(kLastEnum_SkBlurStyle+1));
189 }
190 
make_blur_mask_filter_flag()191 static SkBlurMaskFilter::BlurFlags make_blur_mask_filter_flag() {
192     return static_cast<SkBlurMaskFilter::BlurFlags>(R(SkBlurMaskFilter::kAll_BlurFlag+1));
193 }
194 
make_filter_quality()195 static SkFilterQuality make_filter_quality() {
196     return static_cast<SkFilterQuality>(R(kHigh_SkFilterQuality+1));
197 }
198 
make_typeface_style()199 static SkFontStyle make_typeface_style() {
200     return SkFontStyle::FromOldStyle(SkTypeface::kBoldItalic+1);
201 }
202 
make_path_1d_path_effect_style()203 static SkPath1DPathEffect::Style make_path_1d_path_effect_style() {
204     return static_cast<SkPath1DPathEffect::Style>(R((int)SkPath1DPathEffect::kLastEnum_Style + 1));
205 }
206 
make_color()207 static SkColor make_color() {
208     return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090;
209 }
210 
make_shadow_mode()211 static SkDropShadowImageFilter::ShadowMode make_shadow_mode() {
212     return (R(2) == 1) ? SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode :
213                          SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode;
214 }
215 
make_point()216 static SkPoint3 make_point() {
217     return SkPoint3::Make(make_scalar(), make_scalar(), make_scalar(true));
218 }
219 
make_channel_selector_type()220 static SkDisplacementMapEffect::ChannelSelectorType make_channel_selector_type() {
221     return static_cast<SkDisplacementMapEffect::ChannelSelectorType>(R(4)+1);
222 }
223 
valid_for_raster_canvas(const SkImageInfo & info)224 static bool valid_for_raster_canvas(const SkImageInfo& info) {
225     switch (info.colorType()) {
226         case kAlpha_8_SkColorType:
227         case kRGB_565_SkColorType:
228             return true;
229         case kN32_SkColorType:
230             return kPremul_SkAlphaType == info.alphaType() ||
231                    kOpaque_SkAlphaType == info.alphaType();
232         default:
233             break;
234     }
235     return false;
236 }
237 
rand_colortype()238 static SkColorType rand_colortype() {
239     return (SkColorType)R(kLastEnum_SkColorType + 1);
240 }
241 
rand_bitmap_for_canvas(SkBitmap * bitmap)242 static void rand_bitmap_for_canvas(SkBitmap* bitmap) {
243     SkImageInfo info;
244     do {
245         info = SkImageInfo::Make(kBitmapSize, kBitmapSize, rand_colortype(),
246                                  kPremul_SkAlphaType);
247     } while (!valid_for_raster_canvas(info) || !bitmap->tryAllocPixels(info));
248 }
249 
make_g_bitmap(SkBitmap & bitmap)250 static void make_g_bitmap(SkBitmap& bitmap) {
251     rand_bitmap_for_canvas(&bitmap);
252 
253     SkCanvas canvas(bitmap);
254     canvas.clear(0x00000000);
255     SkPaint paint;
256     paint.setAntiAlias(true);
257     paint.setColor(0xFF884422);
258     paint.setTextSize(SkIntToScalar(kBitmapSize/2));
259     const char* str = "g";
260     canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8),
261                     SkIntToScalar(kBitmapSize/4), paint);
262 }
263 
make_checkerboard_bitmap(SkBitmap & bitmap)264 static void make_checkerboard_bitmap(SkBitmap& bitmap) {
265     rand_bitmap_for_canvas(&bitmap);
266 
267     SkCanvas canvas(bitmap);
268     canvas.clear(0x00000000);
269     SkPaint darkPaint;
270     darkPaint.setColor(0xFF804020);
271     SkPaint lightPaint;
272     lightPaint.setColor(0xFF244484);
273     const int i = kBitmapSize / 8;
274     const SkScalar f = SkIntToScalar(i);
275     for (int y = 0; y < kBitmapSize; y += i) {
276         for (int x = 0; x < kBitmapSize; x += i) {
277             canvas.save();
278             canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
279             canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
280             canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
281             canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
282             canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
283             canvas.restore();
284         }
285     }
286 }
287 
make_bitmap()288 static const SkBitmap& make_bitmap() {
289     static SkBitmap bitmap[2];
290     static bool initialized = false;
291     if (!initialized) {
292         make_g_bitmap(bitmap[0]);
293         make_checkerboard_bitmap(bitmap[1]);
294         initialized = true;
295     }
296     return bitmap[R(2)];
297 }
298 
make_3Dlut(int * cubeDimension,bool invR,bool invG,bool invB)299 static sk_sp<SkData> make_3Dlut(int* cubeDimension, bool invR, bool invG, bool invB) {
300     int size = 4 << R(5);
301     auto data = SkData::MakeUninitialized(sizeof(SkColor) * size * size * size);
302     SkColor* pixels = (SkColor*)(data->writable_data());
303     SkAutoTMalloc<uint8_t> lutMemory(size);
304     SkAutoTMalloc<uint8_t> invLutMemory(size);
305     uint8_t* lut = lutMemory.get();
306     uint8_t* invLut = invLutMemory.get();
307     const int maxIndex = size - 1;
308     for (int i = 0; i < size; i++) {
309         lut[i] = (i * 255) / maxIndex;
310         invLut[i] = ((maxIndex - i) * 255) / maxIndex;
311     }
312     for (int r = 0; r < size; ++r) {
313         for (int g = 0; g < size; ++g) {
314             for (int b = 0; b < size; ++b) {
315                 pixels[(size * ((size * b) + g)) + r] = SkColorSetARGB(0xFF,
316                         invR ? invLut[r] : lut[r],
317                         invG ? invLut[g] : lut[g],
318                         invB ? invLut[b] : lut[b]);
319             }
320         }
321     }
322     if (cubeDimension) {
323         *cubeDimension = size;
324     }
325     return data;
326 }
327 
drawSomething(SkCanvas * canvas)328 static void drawSomething(SkCanvas* canvas) {
329     SkPaint paint;
330 
331     canvas->save();
332     canvas->scale(0.5f, 0.5f);
333     canvas->drawBitmap(make_bitmap(), 0, 0, nullptr);
334     canvas->restore();
335 
336     paint.setAntiAlias(true);
337 
338     paint.setColor(SK_ColorRED);
339     canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
340     paint.setColor(SK_ColorBLACK);
341     paint.setTextSize(SkIntToScalar(kBitmapSize/3));
342     canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
343 }
344 
rand_color_table(uint8_t * table)345 static void rand_color_table(uint8_t* table) {
346     for (int i = 0; i < 256; ++i) {
347         table[i] = R(256);
348     }
349 }
350 
make_color_filter()351 static sk_sp<SkColorFilter> make_color_filter() {
352     switch (R(6)) {
353         case 0: {
354             SkScalar array[20];
355             for (int i = 0; i < 20; ++i) {
356                 array[i] = make_scalar();
357             }
358             return SkColorFilter::MakeMatrixFilterRowMajor255(array);
359         }
360         case 1:
361             return SkLumaColorFilter::Make();
362         case 2: {
363             uint8_t tableA[256];
364             uint8_t tableR[256];
365             uint8_t tableG[256];
366             uint8_t tableB[256];
367             rand_color_table(tableA);
368             rand_color_table(tableR);
369             rand_color_table(tableG);
370             rand_color_table(tableB);
371             return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
372         }
373         case 3:
374             return SkColorFilter::MakeModeFilter(make_color(), make_xfermode());
375         case 4:
376             return SkColorMatrixFilter::MakeLightingFilter(make_color(), make_color());
377         case 5:
378         default:
379             break;
380     }
381     return nullptr;
382 }
383 
make_path()384 static SkPath make_path() {
385     SkPath path;
386     int numOps = R(30);
387     for (int i = 0; i < numOps; ++i) {
388         switch (R(6)) {
389             case 0:
390                 path.moveTo(make_scalar(), make_scalar());
391                 break;
392             case 1:
393                 path.lineTo(make_scalar(), make_scalar());
394                 break;
395             case 2:
396                 path.quadTo(make_scalar(), make_scalar(), make_scalar(), make_scalar());
397                 break;
398             case 3:
399                 path.conicTo(make_scalar(), make_scalar(), make_scalar(), make_scalar(), make_scalar());
400                 break;
401             case 4:
402                 path.cubicTo(make_scalar(), make_scalar(), make_scalar(),
403                              make_scalar(), make_scalar(), make_scalar());
404                 break;
405             case 5:
406             default:
407                 path.arcTo(make_scalar(), make_scalar(), make_scalar(), make_scalar(), make_scalar());
408                 break;
409 
410         }
411     }
412     path.close();
413     return path;
414 }
415 
make_path_effect(bool canBeNull=true)416 static sk_sp<SkPathEffect> make_path_effect(bool canBeNull = true) {
417     sk_sp<SkPathEffect> pathEffect;
418     if (canBeNull && (R(3) == 1)) { return pathEffect; }
419 
420     switch (R(9)) {
421         case 0:
422             pathEffect = SkArcToPathEffect::Make(make_scalar(true));
423             break;
424         case 1:
425             pathEffect = SkPathEffect::MakeCompose(make_path_effect(false),
426                                                    make_path_effect(false));
427             break;
428         case 2:
429             pathEffect = SkCornerPathEffect::Make(make_scalar());
430             break;
431         case 3: {
432             int count = R(10);
433             SkScalar intervals[10];
434             for (int i = 0; i < count; ++i) {
435                 intervals[i] = make_scalar();
436             }
437             pathEffect = SkDashPathEffect::Make(intervals, count, make_scalar());
438             break;
439         }
440         case 4:
441             pathEffect = SkDiscretePathEffect::Make(make_scalar(), make_scalar());
442             break;
443         case 5:
444             pathEffect = SkPath1DPathEffect::Make(make_path(), make_scalar(), make_scalar(),
445                                                   make_path_1d_path_effect_style());
446             break;
447         case 6:
448             pathEffect = SkLine2DPathEffect::Make(make_scalar(), make_matrix());
449             break;
450         case 7:
451             pathEffect = SkPath2DPathEffect::Make(make_matrix(), make_path());
452             break;
453         case 8:
454         default:
455             pathEffect = SkPathEffect::MakeSum(make_path_effect(false),
456                                                make_path_effect(false));
457             break;
458     }
459     return pathEffect;
460 }
461 
make_mask_filter()462 static sk_sp<SkMaskFilter> make_mask_filter() {
463     sk_sp<SkMaskFilter> maskFilter;
464     switch (R(3)) {
465         case 0:
466             maskFilter = SkBlurMaskFilter::Make(make_blur_style(), make_scalar(),
467                                                 make_blur_mask_filter_flag());
468         case 1: {
469             SkEmbossMaskFilter::Light light;
470             for (int i = 0; i < 3; ++i) {
471                 light.fDirection[i] = make_scalar();
472             }
473             light.fPad = R(65536);
474             light.fAmbient = R(256);
475             light.fSpecular = R(256);
476             maskFilter = SkEmbossMaskFilter::Make(make_scalar(), light);
477         }
478         case 2:
479         default:
480             break;
481     }
482     return maskFilter;
483 }
484 
485 static sk_sp<SkImageFilter> make_image_filter(bool canBeNull = true);
486 
make_paint()487 static SkPaint make_paint() {
488     SkPaint paint;
489     paint.setHinting(make_paint_hinting());
490     paint.setAntiAlias(make_bool());
491     paint.setDither(make_bool());
492     paint.setLinearText(make_bool());
493     paint.setSubpixelText(make_bool());
494     paint.setLCDRenderText(make_bool());
495     paint.setEmbeddedBitmapText(make_bool());
496     paint.setAutohinted(make_bool());
497     paint.setVerticalText(make_bool());
498     paint.setFakeBoldText(make_bool());
499     paint.setDevKernText(make_bool());
500     paint.setFilterQuality(make_filter_quality());
501     paint.setStyle(make_paint_style());
502     paint.setColor(make_color());
503     paint.setStrokeWidth(make_scalar());
504     paint.setStrokeMiter(make_scalar());
505     paint.setStrokeCap(make_paint_cap());
506     paint.setStrokeJoin(make_paint_join());
507     paint.setColorFilter(make_color_filter());
508     paint.setBlendMode(make_xfermode());
509     paint.setPathEffect(make_path_effect());
510     paint.setMaskFilter(make_mask_filter());
511 
512     if (false) {
513         // our validating buffer does not support typefaces yet, so skip this for now
514         paint.setTypeface(SkTypeface::MakeFromName(make_font_name().c_str(),
515                                                    make_typeface_style()));
516     }
517 
518     SkLayerRasterizer::Builder rasterizerBuilder;
519     SkPaint paintForRasterizer;
520     if (R(2) == 1) {
521         paintForRasterizer = make_paint();
522     }
523     rasterizerBuilder.addLayer(paintForRasterizer);
524     paint.setRasterizer(rasterizerBuilder.detach());
525     paint.setImageFilter(make_image_filter());
526     sk_sp<SkData> data(make_3Dlut(nullptr, make_bool(), make_bool(), make_bool()));
527     paint.setTextAlign(make_paint_align());
528     paint.setTextSize(make_scalar());
529     paint.setTextScaleX(make_scalar());
530     paint.setTextSkewX(make_scalar());
531     paint.setTextEncoding(make_paint_text_encoding());
532     return paint;
533 }
534 
make_image_filter(bool canBeNull)535 static sk_sp<SkImageFilter> make_image_filter(bool canBeNull) {
536     sk_sp<SkImageFilter> filter;
537 
538     // Add a 1 in 3 chance to get a nullptr input
539     if (canBeNull && (R(3) == 1)) {
540         return filter;
541     }
542 
543     enum { ALPHA_THRESHOLD, MERGE, COLOR, BLUR, MAGNIFIER,
544            XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
545            DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
546            MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS };
547 
548     switch (R(NUM_FILTERS)) {
549     case ALPHA_THRESHOLD:
550         filter = SkAlphaThresholdFilter::Make(make_region(),
551                                               make_scalar(),
552                                               make_scalar(),
553                                               make_image_filter());
554         break;
555     case MERGE:
556         filter = SkMergeImageFilter::Make(make_image_filter(),
557                                           make_image_filter(),
558                                           make_xfermode());
559         break;
560     case COLOR: {
561         sk_sp<SkColorFilter> cf(make_color_filter());
562         filter = cf ? SkColorFilterImageFilter::Make(std::move(cf), make_image_filter())
563                     : nullptr;
564         break;
565     }
566     case BLUR:
567         filter = SkBlurImageFilter::Make(make_scalar(true),
568                                          make_scalar(true),
569                                          make_image_filter());
570         break;
571     case MAGNIFIER:
572         filter = SkMagnifierImageFilter::Make(make_rect(),
573                                               make_scalar(true),
574                                               make_image_filter());
575         break;
576     case XFERMODE:
577         filter = SkXfermodeImageFilter::Make(make_xfermode(),
578                                              make_image_filter(),
579                                              make_image_filter(),
580                                              nullptr);
581         break;
582     case OFFSET:
583         filter = SkOffsetImageFilter::Make(make_scalar(), make_scalar(), make_image_filter());
584         break;
585     case MATRIX:
586         filter = SkImageFilter::MakeMatrixFilter(make_matrix(),
587                                                  (SkFilterQuality)R(4),
588                                                  make_image_filter());
589         break;
590     case MATRIX_CONVOLUTION: {
591         SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
592                                                      SkIntToScalar(kBitmapSize)));
593         SkISize size = SkISize::Make(R(10)+1, R(10)+1);
594         int arraySize = size.width() * size.height();
595         SkTArray<SkScalar> kernel(arraySize);
596         for (int i = 0; i < arraySize; ++i) {
597             kernel.push_back() = make_scalar();
598         }
599         SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
600                                                R(SkIntToScalar(size.height())));
601 
602         filter = SkMatrixConvolutionImageFilter::Make(size,
603                                                       kernel.begin(),
604                                                       make_scalar(),
605                                                       make_scalar(),
606                                                       kernelOffset,
607                                                       (SkMatrixConvolutionImageFilter::TileMode)R(3),
608                                                       R(2) == 1,
609                                                       make_image_filter(),
610                                                       &cropR);
611         break;
612     }
613     case COMPOSE:
614         filter = SkComposeImageFilter::Make(make_image_filter(), make_image_filter());
615         break;
616     case DISTANT_LIGHT:
617         filter = (R(2) == 1)
618                  ? SkLightingImageFilter::MakeDistantLitDiffuse(make_point(), make_color(),
619                                                                 make_scalar(), make_scalar(),
620                                                                 make_image_filter())
621                  : SkLightingImageFilter::MakeDistantLitSpecular(make_point(), make_color(),
622                                                                  make_scalar(), make_scalar(),
623                                                                  SkIntToScalar(R(10)),
624                                                                  make_image_filter());
625         break;
626     case POINT_LIGHT:
627         filter = (R(2) == 1)
628                  ? SkLightingImageFilter::MakePointLitDiffuse(make_point(), make_color(),
629                                                               make_scalar(), make_scalar(),
630                                                               make_image_filter())
631                  : SkLightingImageFilter::MakePointLitSpecular(make_point(), make_color(),
632                                                                make_scalar(), make_scalar(),
633                                                                SkIntToScalar(R(10)),
634                                                                make_image_filter());
635         break;
636     case SPOT_LIGHT:
637         filter = (R(2) == 1)
638                  ? SkLightingImageFilter::MakeSpotLitDiffuse(SkPoint3::Make(0, 0, 0),
639                                                              make_point(), make_scalar(),
640                                                              make_scalar(), make_color(),
641                                                              make_scalar(), make_scalar(),
642                                                              make_image_filter())
643                  : SkLightingImageFilter::MakeSpotLitSpecular(SkPoint3::Make(0, 0, 0),
644                                                               make_point(), make_scalar(),
645                                                               make_scalar(), make_color(),
646                                                               make_scalar(), make_scalar(),
647                                                               SkIntToScalar(R(10)),
648                                                               make_image_filter());
649         break;
650     case NOISE: {
651         sk_sp<SkShader> shader((R(2) == 1)
652                 ? SkPerlinNoiseShader::MakeFractalNoise(make_scalar(true), make_scalar(true),
653                                                         R(10.0f), make_scalar())
654                 : SkPerlinNoiseShader::MakeTurbulence(make_scalar(true), make_scalar(true),
655                                                       R(10.0f), make_scalar()));
656         SkPaint paint;
657         paint.setShader(shader);
658         SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
659                                                      SkIntToScalar(kBitmapSize)));
660         filter = SkPaintImageFilter::Make(paint, &cropR);
661         break;
662     }
663     case DROP_SHADOW:
664         filter = SkDropShadowImageFilter::Make(make_scalar(),
665                                                make_scalar(),
666                                                make_scalar(true),
667                                                make_scalar(true),
668                                                make_color(),
669                                                make_shadow_mode(),
670                                                make_image_filter(),
671                                                nullptr);
672         break;
673     case MORPHOLOGY:
674         if (R(2) == 1) {
675             filter = SkDilateImageFilter::Make(R(static_cast<float>(kBitmapSize)),
676                                                R(static_cast<float>(kBitmapSize)),
677                                                make_image_filter());
678         } else {
679             filter = SkErodeImageFilter::Make(R(static_cast<float>(kBitmapSize)),
680                                               R(static_cast<float>(kBitmapSize)),
681                                               make_image_filter());
682         }
683         break;
684     case BITMAP: {
685         sk_sp<SkImage> image(SkImage::MakeFromBitmap(make_bitmap()));
686         if (R(2) == 1) {
687             filter = SkImageSource::Make(std::move(image),
688                                          make_rect(),
689                                          make_rect(),
690                                          kHigh_SkFilterQuality);
691         } else {
692             filter = SkImageSource::Make(std::move(image));
693         }
694         break;
695     }
696     case DISPLACE:
697         filter = SkDisplacementMapEffect::Make(make_channel_selector_type(),
698                                                make_channel_selector_type(),
699                                                make_scalar(),
700                                                make_image_filter(false),
701                                                make_image_filter());
702         break;
703     case TILE:
704         filter = SkTileImageFilter::Make(make_rect(), make_rect(), make_image_filter(false));
705         break;
706     case PICTURE: {
707         SkRTreeFactory factory;
708         SkPictureRecorder recorder;
709         SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize),
710                                                             SkIntToScalar(kBitmapSize),
711                                                             &factory, 0);
712         drawSomething(recordingCanvas);
713         sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
714         filter = SkPictureImageFilter::Make(pict, make_rect());
715         break;
716     }
717     case PAINT: {
718         SkImageFilter::CropRect cropR(make_rect());
719         filter = SkPaintImageFilter::Make(make_paint(), &cropR);
720         break;
721     }
722     default:
723         break;
724     }
725     return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
726 }
727 
make_serialized_image_filter()728 static sk_sp<SkImageFilter> make_serialized_image_filter() {
729     sk_sp<SkImageFilter> filter(make_image_filter(false));
730     sk_sp<SkData> data(SkValidatingSerializeFlattenable(filter.get()));
731     const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
732     size_t len = data->size();
733 #ifdef SK_ADD_RANDOM_BIT_FLIPS
734     unsigned char* p = const_cast<unsigned char*>(ptr);
735     for (size_t i = 0; i < len; ++i, ++p) {
736         if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
737             if (R(10) == 1) { // Then 10% of the time, change a whole byte
738                 switch(R(3)) {
739                 case 0:
740                     *p ^= 0xFF; // Flip entire byte
741                     break;
742                 case 1:
743                     *p = 0xFF; // Set all bits to 1
744                     break;
745                 case 2:
746                     *p = 0x00; // Set all bits to 0
747                     break;
748                 }
749             } else {
750                 *p ^= (1 << R(8));
751             }
752         }
753     }
754 #endif // SK_ADD_RANDOM_BIT_FLIPS
755     return SkValidatingDeserializeImageFilter(ptr, len);
756 }
757 
drawClippedBitmap(SkCanvas * canvas,int x,int y,const SkPaint & paint)758 static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
759     canvas->save();
760     canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
761         SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
762     canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint);
763     canvas->restore();
764 }
765 
do_fuzz(SkCanvas * canvas)766 static void do_fuzz(SkCanvas* canvas) {
767     sk_sp<SkImageFilter> filter = make_serialized_image_filter();
768 
769 #ifdef SK_FUZZER_IS_VERBOSE
770     static uint32_t numFilters = 0;
771     static uint32_t numValidFilters = 0;
772     if (0 == numFilters) {
773         printf("Fuzzing with %u\n", kSeed);
774     }
775     numFilters++;
776     if (filter) {
777         numValidFilters++;
778     }
779     printf("Filter no : %u. Valid filters so far : %u\r", numFilters, numValidFilters);
780     fflush(stdout);
781 #endif
782 
783     SkPaint paint;
784     paint.setImageFilter(filter);
785     drawClippedBitmap(canvas, 0, 0, paint);
786 }
787 
788 //////////////////////////////////////////////////////////////////////////////
789 
790 class ImageFilterFuzzView : public SampleView {
791 public:
ImageFilterFuzzView()792     ImageFilterFuzzView() {
793         this->setBGColor(0xFFDDDDDD);
794     }
795 
796 protected:
797     // overrides from SkEventSink
onQuery(SkEvent * evt)798     virtual bool onQuery(SkEvent* evt) {
799         if (SampleCode::TitleQ(*evt)) {
800             SampleCode::TitleR(evt, "ImageFilterFuzzer");
801             return true;
802         }
803         return this->INHERITED::onQuery(evt);
804     }
805 
drawBG(SkCanvas * canvas)806     void drawBG(SkCanvas* canvas) {
807         canvas->drawColor(0xFFDDDDDD);
808     }
809 
onDrawContent(SkCanvas * canvas)810     virtual void onDrawContent(SkCanvas* canvas) {
811         do_fuzz(canvas);
812         this->inval(0);
813     }
814 
815 private:
816     typedef SkView INHERITED;
817 };
818 
819 //////////////////////////////////////////////////////////////////////////////
820 
MyFactory()821 static SkView* MyFactory() { return new ImageFilterFuzzView; }
822 static SkViewRegister reg(MyFactory);
823