blob: 7038286be1811fc1b51b20a73af703303948f2c4 [file] [log] [blame]
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +00001/*
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"
ajuma77b6ba32016-01-08 14:58:35 -08008#include "Sk1DPathEffect.h"
9#include "Sk2DPathEffect.h"
commit-bot@chromium.org067fe482014-05-27 13:52:51 +000010#include "SkAlphaThresholdFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000011#include "SkBlurImageFilter.h"
12#include "SkCanvas.h"
13#include "SkColorFilter.h"
14#include "SkColorFilterImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080015#include "SkColorMatrixFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000016#include "SkComposeImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080017#include "SkCornerPathEffect.h"
18#include "SkDashPathEffect.h"
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +000019#include "SkData.h"
ajuma77b6ba32016-01-08 14:58:35 -080020#include "SkDiscretePathEffect.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000021#include "SkDisplacementMapEffect.h"
22#include "SkDropShadowImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080023#include "SkEmbossMaskFilter.h"
fmalita5598b632015-09-15 11:26:13 -070024#include "SkImageSource.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000025#include "SkLightingImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080026#include "SkLumaColorFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000027#include "SkMagnifierImageFilter.h"
commit-bot@chromium.org067fe482014-05-27 13:52:51 +000028#include "SkMatrixConvolutionImageFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000029#include "SkMergeImageFilter.h"
30#include "SkMorphologyImageFilter.h"
31#include "SkOffsetImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080032#include "SkPaintImageFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000033#include "SkPerlinNoiseShader.h"
commit-bot@chromium.org7b8d72d2014-01-16 22:54:41 +000034#include "SkPictureImageFilter.h"
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +000035#include "SkPictureRecorder.h"
robertphillips3d32d762015-07-13 13:16:44 -070036#include "SkPoint3.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000037#include "SkRandom.h"
Mike Reedc090c642017-05-16 10:39:06 -040038#include "SkRegion.h"
ajuma77b6ba32016-01-08 14:58:35 -080039#include "SkTableColorFilter.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000040#include "SkTileImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080041#include "SkTypeface.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000042#include "SkView.h"
43#include "SkXfermodeImageFilter.h"
Jim Van Verthd401da62018-05-03 10:40:30 -040044#if SK_SUPPORT_GPU
45#include "text/GrSDFMaskFilter.h"
46#endif
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000047#include <stdio.h>
48#include <time.h>
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000049
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +000050//#define SK_ADD_RANDOM_BIT_FLIPS
51//#define SK_FUZZER_IS_VERBOSE
52
halcanary96fcdcc2015-08-27 07:41:13 -070053static const uint32_t kSeed = (uint32_t)(time(nullptr));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000054static SkRandom gRand(kSeed);
55static bool return_large = false;
56static bool return_undef = false;
57
58static const int kBitmapSize = 24;
59
60static 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
71static 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
81static 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
104static SkScalar make_scalar(bool positiveOnly = false) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000105 return make_number(positiveOnly);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000106}
107
ajuma77b6ba32016-01-08 14:58:35 -0800108static 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
117static 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
134static bool make_bool() {
135 return R(2) == 1;
136}
137
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000138static SkRect make_rect() {
139 return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))),
140 SkIntToScalar(R(static_cast<float>(kBitmapSize))));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000141}
142
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000143static SkRegion make_region() {
commit-bot@chromium.orgb859fe62014-05-27 17:03:25 +0000144 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)));
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000148 return SkRegion(iRegion);
149}
150
151static 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
reed374772b2016-10-05 17:33:02 -0700159static SkBlendMode make_xfermode() {
Mike Reedd4706732016-11-15 16:44:34 -0500160 return static_cast<SkBlendMode>(R((int)SkBlendMode::kLastMode+1));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000161}
162
ajuma77b6ba32016-01-08 14:58:35 -0800163static SkPaint::Align make_paint_align() {
164 return static_cast<SkPaint::Align>(R(SkPaint::kRight_Align+1));
165}
166
167static SkPaint::Hinting make_paint_hinting() {
168 return static_cast<SkPaint::Hinting>(R(SkPaint::kFull_Hinting+1));
169}
170
171static SkPaint::Style make_paint_style() {
172 return static_cast<SkPaint::Style>(R(SkPaint::kStrokeAndFill_Style+1));
173}
174
175static SkPaint::Cap make_paint_cap() {
176 return static_cast<SkPaint::Cap>(R(SkPaint::kDefault_Cap+1));
177}
178
179static SkPaint::Join make_paint_join() {
180 return static_cast<SkPaint::Join>(R(SkPaint::kDefault_Join+1));
181}
182
183static SkPaint::TextEncoding make_paint_text_encoding() {
184 return static_cast<SkPaint::TextEncoding>(R(SkPaint::kGlyphID_TextEncoding+1));
185}
186
187static SkBlurStyle make_blur_style() {
188 return static_cast<SkBlurStyle>(R(kLastEnum_SkBlurStyle+1));
189}
190
Mike Reed1be1f8d2018-03-14 13:01:17 -0400191static bool make_blur_mask_filter_respectctm() {
192 return static_cast<bool>(R(2));
ajuma77b6ba32016-01-08 14:58:35 -0800193}
194
195static SkFilterQuality make_filter_quality() {
196 return static_cast<SkFilterQuality>(R(kHigh_SkFilterQuality+1));
197}
198
mbocee6a9912016-05-31 11:42:36 -0700199static SkFontStyle make_typeface_style() {
Ben Wagnerc6c10b42017-08-07 09:56:21 -0400200 return SkFontStyle::Normal();
ajuma77b6ba32016-01-08 14:58:35 -0800201}
202
203static SkPath1DPathEffect::Style make_path_1d_path_effect_style() {
reedca726ab2016-02-22 12:50:25 -0800204 return static_cast<SkPath1DPathEffect::Style>(R((int)SkPath1DPathEffect::kLastEnum_Style + 1));
ajuma77b6ba32016-01-08 14:58:35 -0800205}
206
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000207static SkColor make_color() {
208 return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090;
209}
210
sugoi234f0362014-10-23 13:59:52 -0700211static SkDropShadowImageFilter::ShadowMode make_shadow_mode() {
212 return (R(2) == 1) ? SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode :
213 SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode;
214}
215
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000216static SkPoint3 make_point() {
robertphillips3d32d762015-07-13 13:16:44 -0700217 return SkPoint3::Make(make_scalar(), make_scalar(), make_scalar(true));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000218}
219
220static SkDisplacementMapEffect::ChannelSelectorType make_channel_selector_type() {
221 return static_cast<SkDisplacementMapEffect::ChannelSelectorType>(R(4)+1);
222}
223
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000224static 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;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000229 case kN32_SkColorType:
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000230 return kPremul_SkAlphaType == info.alphaType() ||
231 kOpaque_SkAlphaType == info.alphaType();
232 default:
233 break;
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000234 }
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000235 return false;
236}
237
238static SkColorType rand_colortype() {
239 return (SkColorType)R(kLastEnum_SkColorType + 1);
240}
241
242static void rand_bitmap_for_canvas(SkBitmap* bitmap) {
243 SkImageInfo info;
244 do {
245 info = SkImageInfo::Make(kBitmapSize, kBitmapSize, rand_colortype(),
246 kPremul_SkAlphaType);
reed84825042014-09-02 12:50:45 -0700247 } while (!valid_for_raster_canvas(info) || !bitmap->tryAllocPixels(info));
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000248}
249
250static void make_g_bitmap(SkBitmap& bitmap) {
251 rand_bitmap_for_canvas(&bitmap);
252
253 SkCanvas canvas(bitmap);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000254 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";
Cary Clark2a475ea2017-04-28 15:35:12 -0400260 canvas.drawString(str, SkIntToScalar(kBitmapSize/8),
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000261 SkIntToScalar(kBitmapSize/4), paint);
262}
263
264static void make_checkerboard_bitmap(SkBitmap& bitmap) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000265 rand_bitmap_for_canvas(&bitmap);
266
267 SkCanvas canvas(bitmap);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000268 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
288static 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
reedd053ce92016-03-22 10:17:23 -0700299static sk_sp<SkData> make_3Dlut(int* cubeDimension, bool invR, bool invG, bool invB) {
sugoif11c5742014-10-27 08:03:56 -0700300 int size = 4 << R(5);
reedd053ce92016-03-22 10:17:23 -0700301 auto data = SkData::MakeUninitialized(sizeof(SkColor) * size * size * size);
sugoif11c5742014-10-27 08:03:56 -0700302 SkColor* pixels = (SkColor*)(data->writable_data());
scroggo565901d2015-12-10 10:44:13 -0800303 SkAutoTMalloc<uint8_t> lutMemory(size);
304 SkAutoTMalloc<uint8_t> invLutMemory(size);
305 uint8_t* lut = lutMemory.get();
306 uint8_t* invLut = invLutMemory.get();
sugoif11c5742014-10-27 08:03:56 -0700307 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
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000328static void drawSomething(SkCanvas* canvas) {
329 SkPaint paint;
330
331 canvas->save();
332 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700333 canvas->drawBitmap(make_bitmap(), 0, 0, nullptr);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000334 canvas->restore();
335
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000336 paint.setAntiAlias(true);
337
338 paint.setColor(SK_ColorRED);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000339 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000340 paint.setColor(SK_ColorBLACK);
341 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
Cary Clark2a475ea2017-04-28 15:35:12 -0400342 canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000343}
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000344
ajuma77b6ba32016-01-08 14:58:35 -0800345static void rand_color_table(uint8_t* table) {
346 for (int i = 0; i < 256; ++i) {
347 table[i] = R(256);
348 }
349}
350
reedd053ce92016-03-22 10:17:23 -0700351static sk_sp<SkColorFilter> make_color_filter() {
ajuma77b6ba32016-01-08 14:58:35 -0800352 switch (R(6)) {
353 case 0: {
354 SkScalar array[20];
355 for (int i = 0; i < 20; ++i) {
356 array[i] = make_scalar();
357 }
reedd053ce92016-03-22 10:17:23 -0700358 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
ajuma77b6ba32016-01-08 14:58:35 -0800359 }
360 case 1:
reedd053ce92016-03-22 10:17:23 -0700361 return SkLumaColorFilter::Make();
ajuma77b6ba32016-01-08 14:58:35 -0800362 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);
reedd053ce92016-03-22 10:17:23 -0700371 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
ajuma77b6ba32016-01-08 14:58:35 -0800372 }
373 case 3:
reedd053ce92016-03-22 10:17:23 -0700374 return SkColorFilter::MakeModeFilter(make_color(), make_xfermode());
ajuma77b6ba32016-01-08 14:58:35 -0800375 case 4:
reedd053ce92016-03-22 10:17:23 -0700376 return SkColorMatrixFilter::MakeLightingFilter(make_color(), make_color());
ajuma77b6ba32016-01-08 14:58:35 -0800377 case 5:
378 default:
ajuma77b6ba32016-01-08 14:58:35 -0800379 break;
380 }
reedd053ce92016-03-22 10:17:23 -0700381 return nullptr;
ajuma77b6ba32016-01-08 14:58:35 -0800382}
383
384static 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
reeda4393342016-03-18 11:22:57 -0700416static sk_sp<SkPathEffect> make_path_effect(bool canBeNull = true) {
417 sk_sp<SkPathEffect> pathEffect;
ajuma77b6ba32016-01-08 14:58:35 -0800418 if (canBeNull && (R(3) == 1)) { return pathEffect; }
419
Mike Reed40e7e652017-07-22 22:12:59 -0400420 switch (R(8)) {
ajuma77b6ba32016-01-08 14:58:35 -0800421 case 0:
Mike Reed40e7e652017-07-22 22:12:59 -0400422 pathEffect = SkPath2DPathEffect::Make(make_matrix(), make_path());
ajuma77b6ba32016-01-08 14:58:35 -0800423 break;
reeda4393342016-03-18 11:22:57 -0700424 case 1:
Mike Reeda07741a2017-02-25 22:34:32 -0500425 pathEffect = SkPathEffect::MakeCompose(make_path_effect(false),
reeda4393342016-03-18 11:22:57 -0700426 make_path_effect(false));
ajuma77b6ba32016-01-08 14:58:35 -0800427 break;
ajuma77b6ba32016-01-08 14:58:35 -0800428 case 2:
reeda4393342016-03-18 11:22:57 -0700429 pathEffect = SkCornerPathEffect::Make(make_scalar());
ajuma77b6ba32016-01-08 14:58:35 -0800430 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 }
reeda4393342016-03-18 11:22:57 -0700437 pathEffect = SkDashPathEffect::Make(intervals, count, make_scalar());
ajuma77b6ba32016-01-08 14:58:35 -0800438 break;
439 }
440 case 4:
reeda4393342016-03-18 11:22:57 -0700441 pathEffect = SkDiscretePathEffect::Make(make_scalar(), make_scalar());
ajuma77b6ba32016-01-08 14:58:35 -0800442 break;
443 case 5:
reeda4393342016-03-18 11:22:57 -0700444 pathEffect = SkPath1DPathEffect::Make(make_path(), make_scalar(), make_scalar(),
445 make_path_1d_path_effect_style());
ajuma77b6ba32016-01-08 14:58:35 -0800446 break;
447 case 6:
reeda4393342016-03-18 11:22:57 -0700448 pathEffect = SkLine2DPathEffect::Make(make_scalar(), make_matrix());
ajuma77b6ba32016-01-08 14:58:35 -0800449 break;
450 case 7:
ajuma77b6ba32016-01-08 14:58:35 -0800451 default:
Mike Reeda07741a2017-02-25 22:34:32 -0500452 pathEffect = SkPathEffect::MakeSum(make_path_effect(false),
reeda4393342016-03-18 11:22:57 -0700453 make_path_effect(false));
ajuma77b6ba32016-01-08 14:58:35 -0800454 break;
455 }
456 return pathEffect;
457}
458
reedefdfd512016-04-04 10:02:58 -0700459static sk_sp<SkMaskFilter> make_mask_filter() {
460 sk_sp<SkMaskFilter> maskFilter;
Jim Van Verthd401da62018-05-03 10:40:30 -0400461#if SK_SUPPORT_GPU
462 switch (R(4)) {
463#else
ajuma77b6ba32016-01-08 14:58:35 -0800464 switch (R(3)) {
Jim Van Verthd401da62018-05-03 10:40:30 -0400465#endif
ajuma77b6ba32016-01-08 14:58:35 -0800466 case 0:
Mike Reed1be1f8d2018-03-14 13:01:17 -0400467 maskFilter = SkMaskFilter::MakeBlur(make_blur_style(), make_scalar(),
468 make_blur_mask_filter_respectctm());
ajuma77b6ba32016-01-08 14:58:35 -0800469 case 1: {
470 SkEmbossMaskFilter::Light light;
471 for (int i = 0; i < 3; ++i) {
472 light.fDirection[i] = make_scalar();
473 }
474 light.fPad = R(65536);
475 light.fAmbient = R(256);
476 light.fSpecular = R(256);
reedefdfd512016-04-04 10:02:58 -0700477 maskFilter = SkEmbossMaskFilter::Make(make_scalar(), light);
ajuma77b6ba32016-01-08 14:58:35 -0800478 }
Jim Van Verthd401da62018-05-03 10:40:30 -0400479#if SK_SUPPORT_GPU
ajuma77b6ba32016-01-08 14:58:35 -0800480 case 2:
Jim Van Verthd401da62018-05-03 10:40:30 -0400481 maskFilter = GrSDFMaskFilter::Make();
482 case 3:
483#else
484 case 2:
485#endif
ajuma77b6ba32016-01-08 14:58:35 -0800486 default:
ajuma77b6ba32016-01-08 14:58:35 -0800487 break;
488 }
489 return maskFilter;
490}
491
robertphillips372177e2016-03-30 07:32:28 -0700492static sk_sp<SkImageFilter> make_image_filter(bool canBeNull = true);
ajuma77b6ba32016-01-08 14:58:35 -0800493
494static SkPaint make_paint() {
495 SkPaint paint;
496 paint.setHinting(make_paint_hinting());
497 paint.setAntiAlias(make_bool());
498 paint.setDither(make_bool());
499 paint.setLinearText(make_bool());
500 paint.setSubpixelText(make_bool());
501 paint.setLCDRenderText(make_bool());
502 paint.setEmbeddedBitmapText(make_bool());
503 paint.setAutohinted(make_bool());
504 paint.setVerticalText(make_bool());
ajuma77b6ba32016-01-08 14:58:35 -0800505 paint.setFakeBoldText(make_bool());
506 paint.setDevKernText(make_bool());
507 paint.setFilterQuality(make_filter_quality());
508 paint.setStyle(make_paint_style());
509 paint.setColor(make_color());
510 paint.setStrokeWidth(make_scalar());
511 paint.setStrokeMiter(make_scalar());
512 paint.setStrokeCap(make_paint_cap());
513 paint.setStrokeJoin(make_paint_join());
514 paint.setColorFilter(make_color_filter());
reed374772b2016-10-05 17:33:02 -0700515 paint.setBlendMode(make_xfermode());
ajuma77b6ba32016-01-08 14:58:35 -0800516 paint.setPathEffect(make_path_effect());
517 paint.setMaskFilter(make_mask_filter());
reedca726ab2016-02-22 12:50:25 -0800518
519 if (false) {
520 // our validating buffer does not support typefaces yet, so skip this for now
bungeman13b9c952016-05-12 10:09:30 -0700521 paint.setTypeface(SkTypeface::MakeFromName(make_font_name().c_str(),
522 make_typeface_style()));
reedca726ab2016-02-22 12:50:25 -0800523 }
524
ajuma77b6ba32016-01-08 14:58:35 -0800525 paint.setImageFilter(make_image_filter());
reedd053ce92016-03-22 10:17:23 -0700526 sk_sp<SkData> data(make_3Dlut(nullptr, make_bool(), make_bool(), make_bool()));
ajuma77b6ba32016-01-08 14:58:35 -0800527 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
robertphillips372177e2016-03-30 07:32:28 -0700535static sk_sp<SkImageFilter> make_image_filter(bool canBeNull) {
536 sk_sp<SkImageFilter> filter;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000537
halcanary96fcdcc2015-08-27 07:41:13 -0700538 // Add a 1 in 3 chance to get a nullptr input
robertphillips372177e2016-03-30 07:32:28 -0700539 if (canBeNull && (R(3) == 1)) {
540 return filter;
541 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000542
Mike Kleinefaad3c2017-01-20 17:25:34 -0500543 enum { ALPHA_THRESHOLD, MERGE, COLOR, BLUR, MAGNIFIER,
reed81700f62016-06-25 14:30:44 -0700544 XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000545 DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
ajuma77b6ba32016-01-08 14:58:35 -0800546 MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS };
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000547
548 switch (R(NUM_FILTERS)) {
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000549 case ALPHA_THRESHOLD:
robertphillipsf6242042016-04-01 10:34:43 -0700550 filter = SkAlphaThresholdFilter::Make(make_region(),
551 make_scalar(),
552 make_scalar(),
robertphillipsef6a47b2016-04-08 08:01:20 -0700553 make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000554 break;
robertphillips2238c9d2016-03-30 13:34:16 -0700555 case MERGE:
556 filter = SkMergeImageFilter::Make(make_image_filter(),
Mike Reed0bdaf052017-06-18 23:35:57 -0400557 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000558 break;
robertphillips372177e2016-03-30 07:32:28 -0700559 case COLOR: {
560 sk_sp<SkColorFilter> cf(make_color_filter());
robertphillips5605b562016-04-05 11:50:42 -0700561 filter = cf ? SkColorFilterImageFilter::Make(std::move(cf), make_image_filter())
robertphillips372177e2016-03-30 07:32:28 -0700562 : nullptr;
djsollen89057902016-03-29 19:07:07 -0700563 break;
robertphillips372177e2016-03-30 07:32:28 -0700564 }
robertphillips6e7025a2016-04-04 04:31:25 -0700565 case BLUR:
566 filter = SkBlurImageFilter::Make(make_scalar(true),
567 make_scalar(true),
568 make_image_filter());
sugoif11c5742014-10-27 08:03:56 -0700569 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000570 case MAGNIFIER:
robertphillips11171f32016-04-07 07:34:15 -0700571 filter = SkMagnifierImageFilter::Make(make_rect(),
572 make_scalar(true),
573 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000574 break;
robertphillips8c0326d2016-04-05 12:48:34 -0700575 case XFERMODE:
reed374772b2016-10-05 17:33:02 -0700576 filter = SkXfermodeImageFilter::Make(make_xfermode(),
robertphillips8c0326d2016-04-05 12:48:34 -0700577 make_image_filter(),
578 make_image_filter(),
robertphillips372177e2016-03-30 07:32:28 -0700579 nullptr);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000580 break;
robertphillips51a315e2016-03-31 09:05:49 -0700581 case OFFSET:
582 filter = SkOffsetImageFilter::Make(make_scalar(), make_scalar(), make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000583 break;
robertphillipsae8c9332016-04-05 15:09:00 -0700584 case MATRIX:
585 filter = SkImageFilter::MakeMatrixFilter(make_matrix(),
586 (SkFilterQuality)R(4),
587 make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000588 break;
robertphillips372177e2016-03-30 07:32:28 -0700589 case MATRIX_CONVOLUTION: {
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000590 SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
591 SkIntToScalar(kBitmapSize)));
592 SkISize size = SkISize::Make(R(10)+1, R(10)+1);
593 int arraySize = size.width() * size.height();
594 SkTArray<SkScalar> kernel(arraySize);
595 for (int i = 0; i < arraySize; ++i) {
596 kernel.push_back() = make_scalar();
597 }
commit-bot@chromium.orgb859fe62014-05-27 17:03:25 +0000598 SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
599 R(SkIntToScalar(size.height())));
robertphillips372177e2016-03-30 07:32:28 -0700600
robertphillipsef6a47b2016-04-08 08:01:20 -0700601 filter = SkMatrixConvolutionImageFilter::Make(size,
602 kernel.begin(),
603 make_scalar(),
604 make_scalar(),
605 kernelOffset,
606 (SkMatrixConvolutionImageFilter::TileMode)R(3),
607 R(2) == 1,
608 make_image_filter(),
609 &cropR);
robertphillips372177e2016-03-30 07:32:28 -0700610 break;
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000611 }
robertphillips491fb172016-03-30 12:32:58 -0700612 case COMPOSE:
613 filter = SkComposeImageFilter::Make(make_image_filter(), make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000614 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700615 case DISTANT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700616 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700617 ? SkLightingImageFilter::MakeDistantLitDiffuse(make_point(), make_color(),
618 make_scalar(), make_scalar(),
619 make_image_filter())
620 : SkLightingImageFilter::MakeDistantLitSpecular(make_point(), make_color(),
621 make_scalar(), make_scalar(),
622 SkIntToScalar(R(10)),
623 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000624 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700625 case POINT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700626 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700627 ? SkLightingImageFilter::MakePointLitDiffuse(make_point(), make_color(),
628 make_scalar(), make_scalar(),
629 make_image_filter())
630 : SkLightingImageFilter::MakePointLitSpecular(make_point(), make_color(),
631 make_scalar(), make_scalar(),
632 SkIntToScalar(R(10)),
633 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000634 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700635 case SPOT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700636 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700637 ? SkLightingImageFilter::MakeSpotLitDiffuse(SkPoint3::Make(0, 0, 0),
638 make_point(), make_scalar(),
639 make_scalar(), make_color(),
640 make_scalar(), make_scalar(),
641 make_image_filter())
642 : SkLightingImageFilter::MakeSpotLitSpecular(SkPoint3::Make(0, 0, 0),
643 make_point(), make_scalar(),
Mike Kleinefaad3c2017-01-20 17:25:34 -0500644 make_scalar(), make_color(),
robertphillips12fa47d2016-04-08 16:28:09 -0700645 make_scalar(), make_scalar(),
646 SkIntToScalar(R(10)),
647 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000648 break;
robertphillips372177e2016-03-30 07:32:28 -0700649 case NOISE: {
robertphillips12fa47d2016-04-08 16:28:09 -0700650 sk_sp<SkShader> shader((R(2) == 1)
651 ? SkPerlinNoiseShader::MakeFractalNoise(make_scalar(true), make_scalar(true),
652 R(10.0f), make_scalar())
653 : SkPerlinNoiseShader::MakeTurbulence(make_scalar(true), make_scalar(true),
654 R(10.0f), make_scalar()));
ajuma77b6ba32016-01-08 14:58:35 -0800655 SkPaint paint;
656 paint.setShader(shader);
reed@google.com44699382013-10-31 17:28:30 +0000657 SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
658 SkIntToScalar(kBitmapSize)));
robertphillips372177e2016-03-30 07:32:28 -0700659 filter = SkPaintImageFilter::Make(paint, &cropR);
660 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000661 }
robertphillipsc4169122016-04-06 08:40:59 -0700662 case DROP_SHADOW:
663 filter = SkDropShadowImageFilter::Make(make_scalar(),
664 make_scalar(),
665 make_scalar(true),
666 make_scalar(true),
667 make_color(),
668 make_shadow_mode(),
669 make_image_filter(),
670 nullptr);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000671 break;
robertphillipsfc11b0a2016-04-05 09:09:36 -0700672 case MORPHOLOGY:
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000673 if (R(2) == 1) {
robertphillipsfc11b0a2016-04-05 09:09:36 -0700674 filter = SkDilateImageFilter::Make(R(static_cast<float>(kBitmapSize)),
675 R(static_cast<float>(kBitmapSize)),
676 make_image_filter());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000677 } else {
robertphillipsfc11b0a2016-04-05 09:09:36 -0700678 filter = SkErodeImageFilter::Make(R(static_cast<float>(kBitmapSize)),
679 R(static_cast<float>(kBitmapSize)),
680 make_image_filter());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000681 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000682 break;
robertphillips372177e2016-03-30 07:32:28 -0700683 case BITMAP: {
reed9ce9d672016-03-17 10:51:11 -0700684 sk_sp<SkImage> image(SkImage::MakeFromBitmap(make_bitmap()));
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000685 if (R(2) == 1) {
robertphillips549c8992016-04-01 09:28:51 -0700686 filter = SkImageSource::Make(std::move(image),
687 make_rect(),
688 make_rect(),
689 kHigh_SkFilterQuality);
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000690 } else {
robertphillips549c8992016-04-01 09:28:51 -0700691 filter = SkImageSource::Make(std::move(image));
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000692 }
robertphillips372177e2016-03-30 07:32:28 -0700693 break;
fmalita5598b632015-09-15 11:26:13 -0700694 }
robertphillipsbfe11fc2016-04-15 07:17:36 -0700695 case DISPLACE:
696 filter = SkDisplacementMapEffect::Make(make_channel_selector_type(),
697 make_channel_selector_type(),
698 make_scalar(),
699 make_image_filter(false),
700 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000701 break;
robertphillips534c2702016-04-15 07:57:40 -0700702 case TILE:
703 filter = SkTileImageFilter::Make(make_rect(), make_rect(), make_image_filter(false));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000704 break;
robertphillips372177e2016-03-30 07:32:28 -0700705 case PICTURE: {
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +0000706 SkRTreeFactory factory;
707 SkPictureRecorder recorder;
halcanary9d524f22016-03-29 09:03:52 -0700708 SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize),
709 SkIntToScalar(kBitmapSize),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700710 &factory, 0);
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +0000711 drawSomething(recordingCanvas);
reedca2622b2016-03-18 07:25:55 -0700712 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
robertphillips372177e2016-03-30 07:32:28 -0700713 filter = SkPictureImageFilter::Make(pict, make_rect());
robertphillipsa33cf072016-03-29 13:54:26 -0700714 break;
robertphillips372177e2016-03-30 07:32:28 -0700715 }
716 case PAINT: {
djsollen89057902016-03-29 19:07:07 -0700717 SkImageFilter::CropRect cropR(make_rect());
robertphillips372177e2016-03-30 07:32:28 -0700718 filter = SkPaintImageFilter::Make(make_paint(), &cropR);
719 break;
ajuma77b6ba32016-01-08 14:58:35 -0800720 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000721 default:
722 break;
723 }
724 return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
725}
726
Mike Reed5e257172016-11-01 11:22:05 -0400727static sk_sp<SkImageFilter> make_serialized_image_filter() {
robertphillips372177e2016-03-30 07:32:28 -0700728 sk_sp<SkImageFilter> filter(make_image_filter(false));
Mike Reed0331d372018-01-23 11:57:30 -0500729 sk_sp<SkData> data(filter->serialize());
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000730 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
731 size_t len = data->size();
732#ifdef SK_ADD_RANDOM_BIT_FLIPS
733 unsigned char* p = const_cast<unsigned char*>(ptr);
734 for (size_t i = 0; i < len; ++i, ++p) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000735 if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
736 if (R(10) == 1) { // Then 10% of the time, change a whole byte
737 switch(R(3)) {
738 case 0:
739 *p ^= 0xFF; // Flip entire byte
740 break;
741 case 1:
742 *p = 0xFF; // Set all bits to 1
743 break;
744 case 2:
745 *p = 0x00; // Set all bits to 0
746 break;
747 }
748 } else {
749 *p ^= (1 << R(8));
750 }
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000751 }
752 }
753#endif // SK_ADD_RANDOM_BIT_FLIPS
Mike Reed0331d372018-01-23 11:57:30 -0500754 return SkImageFilter::Deserialize(ptr, len);
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000755}
756
sugoi@google.combb8148b2013-09-19 19:51:27 +0000757static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000758 canvas->save();
759 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
760 SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
761 canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint);
762 canvas->restore();
763}
764
765static void do_fuzz(SkCanvas* canvas) {
Mike Reed5e257172016-11-01 11:22:05 -0400766 sk_sp<SkImageFilter> filter = make_serialized_image_filter();
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000767
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000768#ifdef SK_FUZZER_IS_VERBOSE
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000769 static uint32_t numFilters = 0;
770 static uint32_t numValidFilters = 0;
771 if (0 == numFilters) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000772 printf("Fuzzing with %u\n", kSeed);
773 }
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000774 numFilters++;
bsalomon49f085d2014-09-05 13:34:00 -0700775 if (filter) {
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000776 numValidFilters++;
777 }
778 printf("Filter no : %u. Valid filters so far : %u\r", numFilters, numValidFilters);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000779 fflush(stdout);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000780#endif
781
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000782 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400783 paint.setImageFilter(filter);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000784 drawClippedBitmap(canvas, 0, 0, paint);
785}
786
787//////////////////////////////////////////////////////////////////////////////
788
789class ImageFilterFuzzView : public SampleView {
790public:
791 ImageFilterFuzzView() {
792 this->setBGColor(0xFFDDDDDD);
793 }
794
795protected:
796 // overrides from SkEventSink
797 virtual bool onQuery(SkEvent* evt) {
798 if (SampleCode::TitleQ(*evt)) {
799 SampleCode::TitleR(evt, "ImageFilterFuzzer");
800 return true;
801 }
802 return this->INHERITED::onQuery(evt);
803 }
804
805 void drawBG(SkCanvas* canvas) {
806 canvas->drawColor(0xFFDDDDDD);
807 }
808
809 virtual void onDrawContent(SkCanvas* canvas) {
810 do_fuzz(canvas);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000811 }
812
813private:
814 typedef SkView INHERITED;
815};
816
817//////////////////////////////////////////////////////////////////////////////
818
819static SkView* MyFactory() { return new ImageFilterFuzzView; }
820static SkViewRegister reg(MyFactory);