blob: ef4eee85209eda298db7d9f32c0457ba8694e6e6 [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"
ajuma77b6ba32016-01-08 14:58:35 -080011#include "SkArcToPathEffect.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000012#include "SkBlurImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080013#include "SkBlurMaskFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000014#include "SkCanvas.h"
15#include "SkColorFilter.h"
16#include "SkColorFilterImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080017#include "SkColorMatrixFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000018#include "SkComposeImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080019#include "SkCornerPathEffect.h"
20#include "SkDashPathEffect.h"
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +000021#include "SkData.h"
ajuma77b6ba32016-01-08 14:58:35 -080022#include "SkDiscretePathEffect.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000023#include "SkDisplacementMapEffect.h"
24#include "SkDropShadowImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080025#include "SkEmbossMaskFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000026#include "SkFlattenableSerialization.h"
fmalita5598b632015-09-15 11:26:13 -070027#include "SkImageSource.h"
ajuma77b6ba32016-01-08 14:58:35 -080028#include "SkLayerRasterizer.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000029#include "SkLightingImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080030#include "SkLumaColorFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000031#include "SkMagnifierImageFilter.h"
commit-bot@chromium.org067fe482014-05-27 13:52:51 +000032#include "SkMatrixConvolutionImageFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000033#include "SkMergeImageFilter.h"
34#include "SkMorphologyImageFilter.h"
35#include "SkOffsetImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080036#include "SkPaintImageFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000037#include "SkPerlinNoiseShader.h"
commit-bot@chromium.org7b8d72d2014-01-16 22:54:41 +000038#include "SkPictureImageFilter.h"
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +000039#include "SkPictureRecorder.h"
robertphillips3d32d762015-07-13 13:16:44 -070040#include "SkPoint3.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000041#include "SkRandom.h"
ajuma77b6ba32016-01-08 14:58:35 -080042#include "SkTableColorFilter.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000043#include "SkTileImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080044#include "SkTypeface.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000045#include "SkView.h"
46#include "SkXfermodeImageFilter.h"
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
191static SkBlurMaskFilter::BlurFlags make_blur_mask_filter_flag() {
192 return static_cast<SkBlurMaskFilter::BlurFlags>(R(SkBlurMaskFilter::kAll_BlurFlag+1));
193}
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() {
200 return SkFontStyle::FromOldStyle(SkTypeface::kBoldItalic+1);
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";
260 canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8),
261 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));
342 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
343}
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
420 switch (R(9)) {
421 case 0:
reeda4393342016-03-18 11:22:57 -0700422 pathEffect = SkArcToPathEffect::Make(make_scalar(true));
ajuma77b6ba32016-01-08 14:58:35 -0800423 break;
reeda4393342016-03-18 11:22:57 -0700424 case 1:
425 pathEffect = SkComposePathEffect::Make(make_path_effect(false),
426 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:
reeda4393342016-03-18 11:22:57 -0700451 pathEffect = SkPath2DPathEffect::Make(make_matrix(), make_path());
ajuma77b6ba32016-01-08 14:58:35 -0800452 break;
453 case 8:
454 default:
reeda4393342016-03-18 11:22:57 -0700455 pathEffect = SkSumPathEffect::Make(make_path_effect(false),
456 make_path_effect(false));
ajuma77b6ba32016-01-08 14:58:35 -0800457 break;
458 }
459 return pathEffect;
460}
461
reedefdfd512016-04-04 10:02:58 -0700462static sk_sp<SkMaskFilter> make_mask_filter() {
463 sk_sp<SkMaskFilter> maskFilter;
ajuma77b6ba32016-01-08 14:58:35 -0800464 switch (R(3)) {
465 case 0:
reedefdfd512016-04-04 10:02:58 -0700466 maskFilter = SkBlurMaskFilter::Make(make_blur_style(), make_scalar(),
467 make_blur_mask_filter_flag());
ajuma77b6ba32016-01-08 14:58:35 -0800468 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);
reedefdfd512016-04-04 10:02:58 -0700476 maskFilter = SkEmbossMaskFilter::Make(make_scalar(), light);
ajuma77b6ba32016-01-08 14:58:35 -0800477 }
478 case 2:
479 default:
ajuma77b6ba32016-01-08 14:58:35 -0800480 break;
481 }
482 return maskFilter;
483}
484
robertphillips372177e2016-03-30 07:32:28 -0700485static sk_sp<SkImageFilter> make_image_filter(bool canBeNull = true);
ajuma77b6ba32016-01-08 14:58:35 -0800486
487static 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.setUnderlineText(make_bool());
499 paint.setStrikeThruText(make_bool());
500 paint.setFakeBoldText(make_bool());
501 paint.setDevKernText(make_bool());
502 paint.setFilterQuality(make_filter_quality());
503 paint.setStyle(make_paint_style());
504 paint.setColor(make_color());
505 paint.setStrokeWidth(make_scalar());
506 paint.setStrokeMiter(make_scalar());
507 paint.setStrokeCap(make_paint_cap());
508 paint.setStrokeJoin(make_paint_join());
509 paint.setColorFilter(make_color_filter());
reed374772b2016-10-05 17:33:02 -0700510 paint.setBlendMode(make_xfermode());
ajuma77b6ba32016-01-08 14:58:35 -0800511 paint.setPathEffect(make_path_effect());
512 paint.setMaskFilter(make_mask_filter());
reedca726ab2016-02-22 12:50:25 -0800513
514 if (false) {
515 // our validating buffer does not support typefaces yet, so skip this for now
bungeman13b9c952016-05-12 10:09:30 -0700516 paint.setTypeface(SkTypeface::MakeFromName(make_font_name().c_str(),
517 make_typeface_style()));
reedca726ab2016-02-22 12:50:25 -0800518 }
519
ajuma77b6ba32016-01-08 14:58:35 -0800520 SkLayerRasterizer::Builder rasterizerBuilder;
521 SkPaint paintForRasterizer;
522 if (R(2) == 1) {
523 paintForRasterizer = make_paint();
524 }
525 rasterizerBuilder.addLayer(paintForRasterizer);
reed7b380d02016-03-21 13:25:16 -0700526 paint.setRasterizer(rasterizerBuilder.detach());
ajuma77b6ba32016-01-08 14:58:35 -0800527 paint.setImageFilter(make_image_filter());
reedd053ce92016-03-22 10:17:23 -0700528 sk_sp<SkData> data(make_3Dlut(nullptr, make_bool(), make_bool(), make_bool()));
ajuma77b6ba32016-01-08 14:58:35 -0800529 paint.setTextAlign(make_paint_align());
530 paint.setTextSize(make_scalar());
531 paint.setTextScaleX(make_scalar());
532 paint.setTextSkewX(make_scalar());
533 paint.setTextEncoding(make_paint_text_encoding());
534 return paint;
535}
536
robertphillips372177e2016-03-30 07:32:28 -0700537static sk_sp<SkImageFilter> make_image_filter(bool canBeNull) {
538 sk_sp<SkImageFilter> filter;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000539
halcanary96fcdcc2015-08-27 07:41:13 -0700540 // Add a 1 in 3 chance to get a nullptr input
robertphillips372177e2016-03-30 07:32:28 -0700541 if (canBeNull && (R(3) == 1)) {
542 return filter;
543 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000544
Mike Kleinefaad3c2017-01-20 17:25:34 -0500545 enum { ALPHA_THRESHOLD, MERGE, COLOR, BLUR, MAGNIFIER,
reed81700f62016-06-25 14:30:44 -0700546 XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000547 DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
ajuma77b6ba32016-01-08 14:58:35 -0800548 MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS };
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000549
550 switch (R(NUM_FILTERS)) {
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000551 case ALPHA_THRESHOLD:
robertphillipsf6242042016-04-01 10:34:43 -0700552 filter = SkAlphaThresholdFilter::Make(make_region(),
553 make_scalar(),
554 make_scalar(),
robertphillipsef6a47b2016-04-08 08:01:20 -0700555 make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000556 break;
robertphillips2238c9d2016-03-30 13:34:16 -0700557 case MERGE:
558 filter = SkMergeImageFilter::Make(make_image_filter(),
559 make_image_filter(),
Mike Reed7d954ad2016-10-28 15:42:34 -0400560 make_xfermode());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000561 break;
robertphillips372177e2016-03-30 07:32:28 -0700562 case COLOR: {
563 sk_sp<SkColorFilter> cf(make_color_filter());
robertphillips5605b562016-04-05 11:50:42 -0700564 filter = cf ? SkColorFilterImageFilter::Make(std::move(cf), make_image_filter())
robertphillips372177e2016-03-30 07:32:28 -0700565 : nullptr;
djsollen89057902016-03-29 19:07:07 -0700566 break;
robertphillips372177e2016-03-30 07:32:28 -0700567 }
robertphillips6e7025a2016-04-04 04:31:25 -0700568 case BLUR:
569 filter = SkBlurImageFilter::Make(make_scalar(true),
570 make_scalar(true),
571 make_image_filter());
sugoif11c5742014-10-27 08:03:56 -0700572 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000573 case MAGNIFIER:
robertphillips11171f32016-04-07 07:34:15 -0700574 filter = SkMagnifierImageFilter::Make(make_rect(),
575 make_scalar(true),
576 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000577 break;
robertphillips8c0326d2016-04-05 12:48:34 -0700578 case XFERMODE:
reed374772b2016-10-05 17:33:02 -0700579 filter = SkXfermodeImageFilter::Make(make_xfermode(),
robertphillips8c0326d2016-04-05 12:48:34 -0700580 make_image_filter(),
581 make_image_filter(),
robertphillips372177e2016-03-30 07:32:28 -0700582 nullptr);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000583 break;
robertphillips51a315e2016-03-31 09:05:49 -0700584 case OFFSET:
585 filter = SkOffsetImageFilter::Make(make_scalar(), make_scalar(), make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000586 break;
robertphillipsae8c9332016-04-05 15:09:00 -0700587 case MATRIX:
588 filter = SkImageFilter::MakeMatrixFilter(make_matrix(),
589 (SkFilterQuality)R(4),
590 make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000591 break;
robertphillips372177e2016-03-30 07:32:28 -0700592 case MATRIX_CONVOLUTION: {
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000593 SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
594 SkIntToScalar(kBitmapSize)));
595 SkISize size = SkISize::Make(R(10)+1, R(10)+1);
596 int arraySize = size.width() * size.height();
597 SkTArray<SkScalar> kernel(arraySize);
598 for (int i = 0; i < arraySize; ++i) {
599 kernel.push_back() = make_scalar();
600 }
commit-bot@chromium.orgb859fe62014-05-27 17:03:25 +0000601 SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
602 R(SkIntToScalar(size.height())));
robertphillips372177e2016-03-30 07:32:28 -0700603
robertphillipsef6a47b2016-04-08 08:01:20 -0700604 filter = SkMatrixConvolutionImageFilter::Make(size,
605 kernel.begin(),
606 make_scalar(),
607 make_scalar(),
608 kernelOffset,
609 (SkMatrixConvolutionImageFilter::TileMode)R(3),
610 R(2) == 1,
611 make_image_filter(),
612 &cropR);
robertphillips372177e2016-03-30 07:32:28 -0700613 break;
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000614 }
robertphillips491fb172016-03-30 12:32:58 -0700615 case COMPOSE:
616 filter = SkComposeImageFilter::Make(make_image_filter(), make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000617 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700618 case DISTANT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700619 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700620 ? SkLightingImageFilter::MakeDistantLitDiffuse(make_point(), make_color(),
621 make_scalar(), make_scalar(),
622 make_image_filter())
623 : SkLightingImageFilter::MakeDistantLitSpecular(make_point(), make_color(),
624 make_scalar(), make_scalar(),
625 SkIntToScalar(R(10)),
626 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000627 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700628 case POINT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700629 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700630 ? SkLightingImageFilter::MakePointLitDiffuse(make_point(), make_color(),
631 make_scalar(), make_scalar(),
632 make_image_filter())
633 : SkLightingImageFilter::MakePointLitSpecular(make_point(), make_color(),
634 make_scalar(), make_scalar(),
635 SkIntToScalar(R(10)),
636 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000637 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700638 case SPOT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700639 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700640 ? SkLightingImageFilter::MakeSpotLitDiffuse(SkPoint3::Make(0, 0, 0),
641 make_point(), make_scalar(),
642 make_scalar(), make_color(),
643 make_scalar(), make_scalar(),
644 make_image_filter())
645 : SkLightingImageFilter::MakeSpotLitSpecular(SkPoint3::Make(0, 0, 0),
646 make_point(), make_scalar(),
Mike Kleinefaad3c2017-01-20 17:25:34 -0500647 make_scalar(), make_color(),
robertphillips12fa47d2016-04-08 16:28:09 -0700648 make_scalar(), make_scalar(),
649 SkIntToScalar(R(10)),
650 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000651 break;
robertphillips372177e2016-03-30 07:32:28 -0700652 case NOISE: {
robertphillips12fa47d2016-04-08 16:28:09 -0700653 sk_sp<SkShader> shader((R(2) == 1)
654 ? SkPerlinNoiseShader::MakeFractalNoise(make_scalar(true), make_scalar(true),
655 R(10.0f), make_scalar())
656 : SkPerlinNoiseShader::MakeTurbulence(make_scalar(true), make_scalar(true),
657 R(10.0f), make_scalar()));
ajuma77b6ba32016-01-08 14:58:35 -0800658 SkPaint paint;
659 paint.setShader(shader);
reed@google.com44699382013-10-31 17:28:30 +0000660 SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
661 SkIntToScalar(kBitmapSize)));
robertphillips372177e2016-03-30 07:32:28 -0700662 filter = SkPaintImageFilter::Make(paint, &cropR);
663 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000664 }
robertphillipsc4169122016-04-06 08:40:59 -0700665 case DROP_SHADOW:
666 filter = SkDropShadowImageFilter::Make(make_scalar(),
667 make_scalar(),
668 make_scalar(true),
669 make_scalar(true),
670 make_color(),
671 make_shadow_mode(),
672 make_image_filter(),
673 nullptr);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000674 break;
robertphillipsfc11b0a2016-04-05 09:09:36 -0700675 case MORPHOLOGY:
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000676 if (R(2) == 1) {
robertphillipsfc11b0a2016-04-05 09:09:36 -0700677 filter = SkDilateImageFilter::Make(R(static_cast<float>(kBitmapSize)),
678 R(static_cast<float>(kBitmapSize)),
679 make_image_filter());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000680 } else {
robertphillipsfc11b0a2016-04-05 09:09:36 -0700681 filter = SkErodeImageFilter::Make(R(static_cast<float>(kBitmapSize)),
682 R(static_cast<float>(kBitmapSize)),
683 make_image_filter());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000684 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000685 break;
robertphillips372177e2016-03-30 07:32:28 -0700686 case BITMAP: {
reed9ce9d672016-03-17 10:51:11 -0700687 sk_sp<SkImage> image(SkImage::MakeFromBitmap(make_bitmap()));
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000688 if (R(2) == 1) {
robertphillips549c8992016-04-01 09:28:51 -0700689 filter = SkImageSource::Make(std::move(image),
690 make_rect(),
691 make_rect(),
692 kHigh_SkFilterQuality);
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000693 } else {
robertphillips549c8992016-04-01 09:28:51 -0700694 filter = SkImageSource::Make(std::move(image));
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000695 }
robertphillips372177e2016-03-30 07:32:28 -0700696 break;
fmalita5598b632015-09-15 11:26:13 -0700697 }
robertphillipsbfe11fc2016-04-15 07:17:36 -0700698 case DISPLACE:
699 filter = SkDisplacementMapEffect::Make(make_channel_selector_type(),
700 make_channel_selector_type(),
701 make_scalar(),
702 make_image_filter(false),
703 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000704 break;
robertphillips534c2702016-04-15 07:57:40 -0700705 case TILE:
706 filter = SkTileImageFilter::Make(make_rect(), make_rect(), make_image_filter(false));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000707 break;
robertphillips372177e2016-03-30 07:32:28 -0700708 case PICTURE: {
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +0000709 SkRTreeFactory factory;
710 SkPictureRecorder recorder;
halcanary9d524f22016-03-29 09:03:52 -0700711 SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize),
712 SkIntToScalar(kBitmapSize),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700713 &factory, 0);
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +0000714 drawSomething(recordingCanvas);
reedca2622b2016-03-18 07:25:55 -0700715 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
robertphillips372177e2016-03-30 07:32:28 -0700716 filter = SkPictureImageFilter::Make(pict, make_rect());
robertphillipsa33cf072016-03-29 13:54:26 -0700717 break;
robertphillips372177e2016-03-30 07:32:28 -0700718 }
719 case PAINT: {
djsollen89057902016-03-29 19:07:07 -0700720 SkImageFilter::CropRect cropR(make_rect());
robertphillips372177e2016-03-30 07:32:28 -0700721 filter = SkPaintImageFilter::Make(make_paint(), &cropR);
722 break;
ajuma77b6ba32016-01-08 14:58:35 -0800723 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000724 default:
725 break;
726 }
727 return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
728}
729
Mike Reed5e257172016-11-01 11:22:05 -0400730static sk_sp<SkImageFilter> make_serialized_image_filter() {
robertphillips372177e2016-03-30 07:32:28 -0700731 sk_sp<SkImageFilter> filter(make_image_filter(false));
bungemanffae30d2016-08-03 13:32:32 -0700732 sk_sp<SkData> data(SkValidatingSerializeFlattenable(filter.get()));
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000733 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
734 size_t len = data->size();
735#ifdef SK_ADD_RANDOM_BIT_FLIPS
736 unsigned char* p = const_cast<unsigned char*>(ptr);
737 for (size_t i = 0; i < len; ++i, ++p) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000738 if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
739 if (R(10) == 1) { // Then 10% of the time, change a whole byte
740 switch(R(3)) {
741 case 0:
742 *p ^= 0xFF; // Flip entire byte
743 break;
744 case 1:
745 *p = 0xFF; // Set all bits to 1
746 break;
747 case 2:
748 *p = 0x00; // Set all bits to 0
749 break;
750 }
751 } else {
752 *p ^= (1 << R(8));
753 }
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000754 }
755 }
756#endif // SK_ADD_RANDOM_BIT_FLIPS
Mike Reed5e257172016-11-01 11:22:05 -0400757 return SkValidatingDeserializeImageFilter(ptr, len);
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000758}
759
sugoi@google.combb8148b2013-09-19 19:51:27 +0000760static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000761 canvas->save();
762 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
763 SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
764 canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint);
765 canvas->restore();
766}
767
768static void do_fuzz(SkCanvas* canvas) {
Mike Reed5e257172016-11-01 11:22:05 -0400769 sk_sp<SkImageFilter> filter = make_serialized_image_filter();
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000770
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000771#ifdef SK_FUZZER_IS_VERBOSE
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000772 static uint32_t numFilters = 0;
773 static uint32_t numValidFilters = 0;
774 if (0 == numFilters) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000775 printf("Fuzzing with %u\n", kSeed);
776 }
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000777 numFilters++;
bsalomon49f085d2014-09-05 13:34:00 -0700778 if (filter) {
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000779 numValidFilters++;
780 }
781 printf("Filter no : %u. Valid filters so far : %u\r", numFilters, numValidFilters);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000782 fflush(stdout);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000783#endif
784
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000785 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400786 paint.setImageFilter(filter);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000787 drawClippedBitmap(canvas, 0, 0, paint);
788}
789
790//////////////////////////////////////////////////////////////////////////////
791
792class ImageFilterFuzzView : public SampleView {
793public:
794 ImageFilterFuzzView() {
795 this->setBGColor(0xFFDDDDDD);
796 }
797
798protected:
799 // overrides from SkEventSink
800 virtual bool onQuery(SkEvent* evt) {
801 if (SampleCode::TitleQ(*evt)) {
802 SampleCode::TitleR(evt, "ImageFilterFuzzer");
803 return true;
804 }
805 return this->INHERITED::onQuery(evt);
806 }
807
808 void drawBG(SkCanvas* canvas) {
809 canvas->drawColor(0xFFDDDDDD);
810 }
811
812 virtual void onDrawContent(SkCanvas* canvas) {
813 do_fuzz(canvas);
814 this->inval(0);
815 }
816
817private:
818 typedef SkView INHERITED;
819};
820
821//////////////////////////////////////////////////////////////////////////////
822
823static SkView* MyFactory() { return new ImageFilterFuzzView; }
824static SkViewRegister reg(MyFactory);