blob: 18806c995022c2dca151367ef16accec8490ae31 [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"
sugoif11c5742014-10-27 08:03:56 -070015#include "SkColorCubeFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000016#include "SkColorFilter.h"
17#include "SkColorFilterImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080018#include "SkColorMatrixFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000019#include "SkComposeImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080020#include "SkCornerPathEffect.h"
21#include "SkDashPathEffect.h"
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +000022#include "SkData.h"
ajuma77b6ba32016-01-08 14:58:35 -080023#include "SkDiscretePathEffect.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000024#include "SkDisplacementMapEffect.h"
25#include "SkDropShadowImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080026#include "SkEmbossMaskFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000027#include "SkFlattenableSerialization.h"
fmalita5598b632015-09-15 11:26:13 -070028#include "SkImageSource.h"
ajuma77b6ba32016-01-08 14:58:35 -080029#include "SkLayerRasterizer.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000030#include "SkLightingImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080031#include "SkLumaColorFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000032#include "SkMagnifierImageFilter.h"
commit-bot@chromium.org067fe482014-05-27 13:52:51 +000033#include "SkMatrixConvolutionImageFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000034#include "SkMergeImageFilter.h"
35#include "SkMorphologyImageFilter.h"
36#include "SkOffsetImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080037#include "SkPaintImageFilter.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000038#include "SkPerlinNoiseShader.h"
commit-bot@chromium.org7b8d72d2014-01-16 22:54:41 +000039#include "SkPictureImageFilter.h"
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +000040#include "SkPictureRecorder.h"
robertphillips3d32d762015-07-13 13:16:44 -070041#include "SkPoint3.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000042#include "SkRandom.h"
ajuma77b6ba32016-01-08 14:58:35 -080043#include "SkTableColorFilter.h"
commit-bot@chromium.org067fe482014-05-27 13:52:51 +000044#include "SkTestImageFilters.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000045#include "SkTileImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -080046#include "SkTypeface.h"
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000047#include "SkView.h"
48#include "SkXfermodeImageFilter.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000049#include <stdio.h>
50#include <time.h>
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000051
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +000052//#define SK_ADD_RANDOM_BIT_FLIPS
53//#define SK_FUZZER_IS_VERBOSE
54
halcanary96fcdcc2015-08-27 07:41:13 -070055static const uint32_t kSeed = (uint32_t)(time(nullptr));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +000056static SkRandom gRand(kSeed);
57static bool return_large = false;
58static bool return_undef = false;
59
60static const int kBitmapSize = 24;
61
62static int R(float x) {
63 return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
64}
65
66#if defined _WIN32
67#pragma warning ( push )
68// we are intentionally causing an overflow here
69// (warning C4756: overflow in constant arithmetic)
70#pragma warning ( disable : 4756 )
71#endif
72
73static float huge() {
74 double d = 1e100;
75 float f = (float)d;
76 return f;
77}
78
79#if defined _WIN32
80#pragma warning ( pop )
81#endif
82
83static float make_number(bool positiveOnly) {
84 float f = positiveOnly ? 1.0f : 0.0f;
85 float v = f;
86 int sel;
87
88 if (return_large) sel = R(6); else sel = R(4);
89 if (!return_undef && sel == 0) sel = 1;
90
91 if (R(2) == 1) v = (float)(R(100)+f); else
92
93 switch (sel) {
94 case 0: break;
95 case 1: v = f; break;
96 case 2: v = 0.000001f; break;
97 case 3: v = 10000.0f; break;
98 case 4: v = 2000000000.0f; break;
99 case 5: v = huge(); break;
100 }
101
102 if (!positiveOnly && (R(4) == 1)) v = -v;
103 return v;
104}
105
106static SkScalar make_scalar(bool positiveOnly = false) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000107 return make_number(positiveOnly);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000108}
109
ajuma77b6ba32016-01-08 14:58:35 -0800110static SkString make_string() {
111 int length = R(1000);
112 SkString str(length);
113 for (int i = 0; i < length; ++i) {
114 str[i] = static_cast<char>(R(256));
115 }
116 return str;
117}
118
119static SkString make_font_name() {
120 int sel = R(8);
121
122 switch(sel) {
123 case 0: return SkString("Courier New");
124 case 1: return SkString("Helvetica");
125 case 2: return SkString("monospace");
126 case 3: return SkString("sans-serif");
127 case 4: return SkString("serif");
128 case 5: return SkString("Times");
129 case 6: return SkString("Times New Roman");
130 case 7:
131 default:
132 return make_string();
133 }
134}
135
136static bool make_bool() {
137 return R(2) == 1;
138}
139
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000140static SkRect make_rect() {
141 return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))),
142 SkIntToScalar(R(static_cast<float>(kBitmapSize))));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000143}
144
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000145static SkRegion make_region() {
commit-bot@chromium.orgb859fe62014-05-27 17:03:25 +0000146 SkIRect iRegion = SkIRect::MakeXYWH(R(static_cast<float>(kBitmapSize)),
147 R(static_cast<float>(kBitmapSize)),
148 R(static_cast<float>(kBitmapSize)),
149 R(static_cast<float>(kBitmapSize)));
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000150 return SkRegion(iRegion);
151}
152
153static SkMatrix make_matrix() {
154 SkMatrix m;
155 for (int i = 0; i < 9; ++i) {
156 m[i] = make_scalar();
157 }
158 return m;
159}
160
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000161static SkXfermode::Mode make_xfermode() {
162 return static_cast<SkXfermode::Mode>(R(SkXfermode::kLastMode+1));
163}
164
ajuma77b6ba32016-01-08 14:58:35 -0800165static SkPaint::Align make_paint_align() {
166 return static_cast<SkPaint::Align>(R(SkPaint::kRight_Align+1));
167}
168
169static SkPaint::Hinting make_paint_hinting() {
170 return static_cast<SkPaint::Hinting>(R(SkPaint::kFull_Hinting+1));
171}
172
173static SkPaint::Style make_paint_style() {
174 return static_cast<SkPaint::Style>(R(SkPaint::kStrokeAndFill_Style+1));
175}
176
177static SkPaint::Cap make_paint_cap() {
178 return static_cast<SkPaint::Cap>(R(SkPaint::kDefault_Cap+1));
179}
180
181static SkPaint::Join make_paint_join() {
182 return static_cast<SkPaint::Join>(R(SkPaint::kDefault_Join+1));
183}
184
185static SkPaint::TextEncoding make_paint_text_encoding() {
186 return static_cast<SkPaint::TextEncoding>(R(SkPaint::kGlyphID_TextEncoding+1));
187}
188
189static SkBlurStyle make_blur_style() {
190 return static_cast<SkBlurStyle>(R(kLastEnum_SkBlurStyle+1));
191}
192
193static SkBlurMaskFilter::BlurFlags make_blur_mask_filter_flag() {
194 return static_cast<SkBlurMaskFilter::BlurFlags>(R(SkBlurMaskFilter::kAll_BlurFlag+1));
195}
196
197static SkFilterQuality make_filter_quality() {
198 return static_cast<SkFilterQuality>(R(kHigh_SkFilterQuality+1));
199}
200
201static SkTypeface::Style make_typeface_style() {
202 return static_cast<SkTypeface::Style>(R(SkTypeface::kBoldItalic+1));
203}
204
205static SkPath1DPathEffect::Style make_path_1d_path_effect_style() {
reedca726ab2016-02-22 12:50:25 -0800206 return static_cast<SkPath1DPathEffect::Style>(R((int)SkPath1DPathEffect::kLastEnum_Style + 1));
ajuma77b6ba32016-01-08 14:58:35 -0800207}
208
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000209static SkColor make_color() {
210 return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090;
211}
212
sugoi234f0362014-10-23 13:59:52 -0700213static SkDropShadowImageFilter::ShadowMode make_shadow_mode() {
214 return (R(2) == 1) ? SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode :
215 SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode;
216}
217
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000218static SkPoint3 make_point() {
robertphillips3d32d762015-07-13 13:16:44 -0700219 return SkPoint3::Make(make_scalar(), make_scalar(), make_scalar(true));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000220}
221
222static SkDisplacementMapEffect::ChannelSelectorType make_channel_selector_type() {
223 return static_cast<SkDisplacementMapEffect::ChannelSelectorType>(R(4)+1);
224}
225
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000226static bool valid_for_raster_canvas(const SkImageInfo& info) {
227 switch (info.colorType()) {
228 case kAlpha_8_SkColorType:
229 case kRGB_565_SkColorType:
230 return true;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000231 case kN32_SkColorType:
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000232 return kPremul_SkAlphaType == info.alphaType() ||
233 kOpaque_SkAlphaType == info.alphaType();
234 default:
235 break;
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000236 }
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000237 return false;
238}
239
240static SkColorType rand_colortype() {
241 return (SkColorType)R(kLastEnum_SkColorType + 1);
242}
243
244static void rand_bitmap_for_canvas(SkBitmap* bitmap) {
245 SkImageInfo info;
246 do {
247 info = SkImageInfo::Make(kBitmapSize, kBitmapSize, rand_colortype(),
248 kPremul_SkAlphaType);
reed84825042014-09-02 12:50:45 -0700249 } while (!valid_for_raster_canvas(info) || !bitmap->tryAllocPixels(info));
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000250}
251
252static void make_g_bitmap(SkBitmap& bitmap) {
253 rand_bitmap_for_canvas(&bitmap);
254
255 SkCanvas canvas(bitmap);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000256 canvas.clear(0x00000000);
257 SkPaint paint;
258 paint.setAntiAlias(true);
259 paint.setColor(0xFF884422);
260 paint.setTextSize(SkIntToScalar(kBitmapSize/2));
261 const char* str = "g";
262 canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8),
263 SkIntToScalar(kBitmapSize/4), paint);
264}
265
266static void make_checkerboard_bitmap(SkBitmap& bitmap) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000267 rand_bitmap_for_canvas(&bitmap);
268
269 SkCanvas canvas(bitmap);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000270 canvas.clear(0x00000000);
271 SkPaint darkPaint;
272 darkPaint.setColor(0xFF804020);
273 SkPaint lightPaint;
274 lightPaint.setColor(0xFF244484);
275 const int i = kBitmapSize / 8;
276 const SkScalar f = SkIntToScalar(i);
277 for (int y = 0; y < kBitmapSize; y += i) {
278 for (int x = 0; x < kBitmapSize; x += i) {
279 canvas.save();
280 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
281 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
282 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
283 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
284 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
285 canvas.restore();
286 }
287 }
288}
289
290static const SkBitmap& make_bitmap() {
291 static SkBitmap bitmap[2];
292 static bool initialized = false;
293 if (!initialized) {
294 make_g_bitmap(bitmap[0]);
295 make_checkerboard_bitmap(bitmap[1]);
296 initialized = true;
297 }
298 return bitmap[R(2)];
299}
300
reedd053ce92016-03-22 10:17:23 -0700301static sk_sp<SkData> make_3Dlut(int* cubeDimension, bool invR, bool invG, bool invB) {
sugoif11c5742014-10-27 08:03:56 -0700302 int size = 4 << R(5);
reedd053ce92016-03-22 10:17:23 -0700303 auto data = SkData::MakeUninitialized(sizeof(SkColor) * size * size * size);
sugoif11c5742014-10-27 08:03:56 -0700304 SkColor* pixels = (SkColor*)(data->writable_data());
scroggo565901d2015-12-10 10:44:13 -0800305 SkAutoTMalloc<uint8_t> lutMemory(size);
306 SkAutoTMalloc<uint8_t> invLutMemory(size);
307 uint8_t* lut = lutMemory.get();
308 uint8_t* invLut = invLutMemory.get();
sugoif11c5742014-10-27 08:03:56 -0700309 const int maxIndex = size - 1;
310 for (int i = 0; i < size; i++) {
311 lut[i] = (i * 255) / maxIndex;
312 invLut[i] = ((maxIndex - i) * 255) / maxIndex;
313 }
314 for (int r = 0; r < size; ++r) {
315 for (int g = 0; g < size; ++g) {
316 for (int b = 0; b < size; ++b) {
317 pixels[(size * ((size * b) + g)) + r] = SkColorSetARGB(0xFF,
318 invR ? invLut[r] : lut[r],
319 invG ? invLut[g] : lut[g],
320 invB ? invLut[b] : lut[b]);
321 }
322 }
323 }
324 if (cubeDimension) {
325 *cubeDimension = size;
326 }
327 return data;
328}
329
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000330static void drawSomething(SkCanvas* canvas) {
331 SkPaint paint;
332
333 canvas->save();
334 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700335 canvas->drawBitmap(make_bitmap(), 0, 0, nullptr);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000336 canvas->restore();
337
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000338 paint.setAntiAlias(true);
339
340 paint.setColor(SK_ColorRED);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000341 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000342 paint.setColor(SK_ColorBLACK);
343 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
344 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
345}
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000346
ajuma77b6ba32016-01-08 14:58:35 -0800347static void rand_color_table(uint8_t* table) {
348 for (int i = 0; i < 256; ++i) {
349 table[i] = R(256);
350 }
351}
352
reedd053ce92016-03-22 10:17:23 -0700353static sk_sp<SkColorFilter> make_color_filter() {
ajuma77b6ba32016-01-08 14:58:35 -0800354 switch (R(6)) {
355 case 0: {
356 SkScalar array[20];
357 for (int i = 0; i < 20; ++i) {
358 array[i] = make_scalar();
359 }
reedd053ce92016-03-22 10:17:23 -0700360 return SkColorFilter::MakeMatrixFilterRowMajor255(array);
ajuma77b6ba32016-01-08 14:58:35 -0800361 }
362 case 1:
reedd053ce92016-03-22 10:17:23 -0700363 return SkLumaColorFilter::Make();
ajuma77b6ba32016-01-08 14:58:35 -0800364 case 2: {
365 uint8_t tableA[256];
366 uint8_t tableR[256];
367 uint8_t tableG[256];
368 uint8_t tableB[256];
369 rand_color_table(tableA);
370 rand_color_table(tableR);
371 rand_color_table(tableG);
372 rand_color_table(tableB);
reedd053ce92016-03-22 10:17:23 -0700373 return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB);
ajuma77b6ba32016-01-08 14:58:35 -0800374 }
375 case 3:
reedd053ce92016-03-22 10:17:23 -0700376 return SkColorFilter::MakeModeFilter(make_color(), make_xfermode());
ajuma77b6ba32016-01-08 14:58:35 -0800377 case 4:
reedd053ce92016-03-22 10:17:23 -0700378 return SkColorMatrixFilter::MakeLightingFilter(make_color(), make_color());
ajuma77b6ba32016-01-08 14:58:35 -0800379 case 5:
380 default:
ajuma77b6ba32016-01-08 14:58:35 -0800381 break;
382 }
reedd053ce92016-03-22 10:17:23 -0700383 return nullptr;
ajuma77b6ba32016-01-08 14:58:35 -0800384}
385
386static SkPath make_path() {
387 SkPath path;
388 int numOps = R(30);
389 for (int i = 0; i < numOps; ++i) {
390 switch (R(6)) {
391 case 0:
392 path.moveTo(make_scalar(), make_scalar());
393 break;
394 case 1:
395 path.lineTo(make_scalar(), make_scalar());
396 break;
397 case 2:
398 path.quadTo(make_scalar(), make_scalar(), make_scalar(), make_scalar());
399 break;
400 case 3:
401 path.conicTo(make_scalar(), make_scalar(), make_scalar(), make_scalar(), make_scalar());
402 break;
403 case 4:
404 path.cubicTo(make_scalar(), make_scalar(), make_scalar(),
405 make_scalar(), make_scalar(), make_scalar());
406 break;
407 case 5:
408 default:
409 path.arcTo(make_scalar(), make_scalar(), make_scalar(), make_scalar(), make_scalar());
410 break;
411
412 }
413 }
414 path.close();
415 return path;
416}
417
reeda4393342016-03-18 11:22:57 -0700418static sk_sp<SkPathEffect> make_path_effect(bool canBeNull = true) {
419 sk_sp<SkPathEffect> pathEffect;
ajuma77b6ba32016-01-08 14:58:35 -0800420 if (canBeNull && (R(3) == 1)) { return pathEffect; }
421
422 switch (R(9)) {
423 case 0:
reeda4393342016-03-18 11:22:57 -0700424 pathEffect = SkArcToPathEffect::Make(make_scalar(true));
ajuma77b6ba32016-01-08 14:58:35 -0800425 break;
reeda4393342016-03-18 11:22:57 -0700426 case 1:
427 pathEffect = SkComposePathEffect::Make(make_path_effect(false),
428 make_path_effect(false));
ajuma77b6ba32016-01-08 14:58:35 -0800429 break;
ajuma77b6ba32016-01-08 14:58:35 -0800430 case 2:
reeda4393342016-03-18 11:22:57 -0700431 pathEffect = SkCornerPathEffect::Make(make_scalar());
ajuma77b6ba32016-01-08 14:58:35 -0800432 break;
433 case 3: {
434 int count = R(10);
435 SkScalar intervals[10];
436 for (int i = 0; i < count; ++i) {
437 intervals[i] = make_scalar();
438 }
reeda4393342016-03-18 11:22:57 -0700439 pathEffect = SkDashPathEffect::Make(intervals, count, make_scalar());
ajuma77b6ba32016-01-08 14:58:35 -0800440 break;
441 }
442 case 4:
reeda4393342016-03-18 11:22:57 -0700443 pathEffect = SkDiscretePathEffect::Make(make_scalar(), make_scalar());
ajuma77b6ba32016-01-08 14:58:35 -0800444 break;
445 case 5:
reeda4393342016-03-18 11:22:57 -0700446 pathEffect = SkPath1DPathEffect::Make(make_path(), make_scalar(), make_scalar(),
447 make_path_1d_path_effect_style());
ajuma77b6ba32016-01-08 14:58:35 -0800448 break;
449 case 6:
reeda4393342016-03-18 11:22:57 -0700450 pathEffect = SkLine2DPathEffect::Make(make_scalar(), make_matrix());
ajuma77b6ba32016-01-08 14:58:35 -0800451 break;
452 case 7:
reeda4393342016-03-18 11:22:57 -0700453 pathEffect = SkPath2DPathEffect::Make(make_matrix(), make_path());
ajuma77b6ba32016-01-08 14:58:35 -0800454 break;
455 case 8:
456 default:
reeda4393342016-03-18 11:22:57 -0700457 pathEffect = SkSumPathEffect::Make(make_path_effect(false),
458 make_path_effect(false));
ajuma77b6ba32016-01-08 14:58:35 -0800459 break;
460 }
461 return pathEffect;
462}
463
464static SkMaskFilter* make_mask_filter() {
465 SkMaskFilter* maskFilter;
466 switch (R(3)) {
467 case 0:
468 maskFilter = SkBlurMaskFilter::Create(make_blur_style(),
469 make_scalar(),
470 make_blur_mask_filter_flag());
471 case 1: {
472 SkEmbossMaskFilter::Light light;
473 for (int i = 0; i < 3; ++i) {
474 light.fDirection[i] = make_scalar();
475 }
476 light.fPad = R(65536);
477 light.fAmbient = R(256);
478 light.fSpecular = R(256);
479 maskFilter = SkEmbossMaskFilter::Create(make_scalar(),
480 light);
481 }
482 case 2:
483 default:
484 maskFilter = nullptr;
485 break;
486 }
487 return maskFilter;
488}
489
490static SkImageFilter* make_image_filter(bool canBeNull = true);
491
492static SkPaint make_paint() {
493 SkPaint paint;
494 paint.setHinting(make_paint_hinting());
495 paint.setAntiAlias(make_bool());
496 paint.setDither(make_bool());
497 paint.setLinearText(make_bool());
498 paint.setSubpixelText(make_bool());
499 paint.setLCDRenderText(make_bool());
500 paint.setEmbeddedBitmapText(make_bool());
501 paint.setAutohinted(make_bool());
502 paint.setVerticalText(make_bool());
503 paint.setUnderlineText(make_bool());
504 paint.setStrikeThruText(make_bool());
505 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());
515 paint.setXfermodeMode(make_xfermode());
516 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
521 SkAutoTUnref<SkTypeface> typeface(
522 SkTypeface::CreateFromName(make_font_name().c_str(), make_typeface_style()));
523 paint.setTypeface(typeface);
524 }
525
ajuma77b6ba32016-01-08 14:58:35 -0800526 SkLayerRasterizer::Builder rasterizerBuilder;
527 SkPaint paintForRasterizer;
528 if (R(2) == 1) {
529 paintForRasterizer = make_paint();
530 }
531 rasterizerBuilder.addLayer(paintForRasterizer);
reed7b380d02016-03-21 13:25:16 -0700532 paint.setRasterizer(rasterizerBuilder.detach());
ajuma77b6ba32016-01-08 14:58:35 -0800533 paint.setImageFilter(make_image_filter());
reedd053ce92016-03-22 10:17:23 -0700534 sk_sp<SkData> data(make_3Dlut(nullptr, make_bool(), make_bool(), make_bool()));
ajuma77b6ba32016-01-08 14:58:35 -0800535 paint.setTextAlign(make_paint_align());
536 paint.setTextSize(make_scalar());
537 paint.setTextScaleX(make_scalar());
538 paint.setTextSkewX(make_scalar());
539 paint.setTextEncoding(make_paint_text_encoding());
540 return paint;
541}
542
543static SkImageFilter* make_image_filter(bool canBeNull) {
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000544 SkImageFilter* filter = 0;
545
halcanary96fcdcc2015-08-27 07:41:13 -0700546 // Add a 1 in 3 chance to get a nullptr input
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000547 if (canBeNull && (R(3) == 1)) { return filter; }
548
sugoif11c5742014-10-27 08:03:56 -0700549 enum { ALPHA_THRESHOLD, MERGE, COLOR, LUT3D, BLUR, MAGNIFIER,
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000550 DOWN_SAMPLE, XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000551 DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
ajuma77b6ba32016-01-08 14:58:35 -0800552 MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS };
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000553
554 switch (R(NUM_FILTERS)) {
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000555 case ALPHA_THRESHOLD:
556 filter = SkAlphaThresholdFilter::Create(make_region(), make_scalar(), make_scalar());
557 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000558 case MERGE:
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000559 filter = SkMergeImageFilter::Create(make_image_filter(), make_image_filter(), make_xfermode());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000560 break;
561 case COLOR:
562 {
reedd053ce92016-03-22 10:17:23 -0700563 sk_sp<SkColorFilter> cf(make_color_filter());
564 filter = cf ? SkColorFilterImageFilter::Create(cf.get(), make_image_filter()) : 0;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000565 }
566 break;
sugoif11c5742014-10-27 08:03:56 -0700567 case LUT3D:
568 {
569 int cubeDimension;
reedd053ce92016-03-22 10:17:23 -0700570 sk_sp<SkData> lut3D(make_3Dlut(&cubeDimension, (R(2) == 1), (R(2) == 1), (R(2) == 1)));
571 sk_sp<SkColorFilter> cf(SkColorCubeFilter::Make(lut3D, cubeDimension));
572 filter = cf ? SkColorFilterImageFilter::Create(cf.get(), make_image_filter()) : 0;
sugoif11c5742014-10-27 08:03:56 -0700573 }
574 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000575 case BLUR:
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000576 filter = SkBlurImageFilter::Create(make_scalar(true), make_scalar(true), make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000577 break;
578 case MAGNIFIER:
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000579 filter = SkMagnifierImageFilter::Create(make_rect(), make_scalar(true));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000580 break;
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000581 case DOWN_SAMPLE:
582 filter = SkDownSampleImageFilter::Create(make_scalar());
583 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000584 case XFERMODE:
585 {
586 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(make_xfermode()));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000587 filter = SkXfermodeImageFilter::Create(mode, make_image_filter(), make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000588 }
589 break;
590 case OFFSET:
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000591 filter = SkOffsetImageFilter::Create(make_scalar(), make_scalar(), make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000592 break;
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000593 case MATRIX:
senorblanco8c874ee2015-03-20 06:38:17 -0700594 filter = SkImageFilter::CreateMatrixFilter(make_matrix(),
595 (SkFilterQuality)R(4),
596 make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000597 break;
598 case MATRIX_CONVOLUTION:
599 {
600 SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
601 SkIntToScalar(kBitmapSize)));
602 SkISize size = SkISize::Make(R(10)+1, R(10)+1);
603 int arraySize = size.width() * size.height();
604 SkTArray<SkScalar> kernel(arraySize);
605 for (int i = 0; i < arraySize; ++i) {
606 kernel.push_back() = make_scalar();
607 }
commit-bot@chromium.orgb859fe62014-05-27 17:03:25 +0000608 SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
609 R(SkIntToScalar(size.height())));
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000610 filter = SkMatrixConvolutionImageFilter::Create(size,
611 kernel.begin(),
612 make_scalar(),
613 make_scalar(),
614 kernelOffset,
615 (SkMatrixConvolutionImageFilter::TileMode)R(3),
616 R(2) == 1,
617 make_image_filter(),
618 &cropR);
619 }
620 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000621 case COMPOSE:
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000622 filter = SkComposeImageFilter::Create(make_image_filter(), make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000623 break;
624 case DISTANT_LIGHT:
625 filter = (R(2) == 1) ?
626 SkLightingImageFilter::CreateDistantLitDiffuse(make_point(),
627 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
628 SkLightingImageFilter::CreateDistantLitSpecular(make_point(),
629 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
630 make_image_filter());
631 break;
632 case POINT_LIGHT:
633 filter = (R(2) == 1) ?
634 SkLightingImageFilter::CreatePointLitDiffuse(make_point(),
635 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
636 SkLightingImageFilter::CreatePointLitSpecular(make_point(),
637 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
638 make_image_filter());
639 break;
640 case SPOT_LIGHT:
641 filter = (R(2) == 1) ?
robertphillips3d32d762015-07-13 13:16:44 -0700642 SkLightingImageFilter::CreateSpotLitDiffuse(SkPoint3::Make(0, 0, 0),
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000643 make_point(), make_scalar(), make_scalar(), make_color(),
644 make_scalar(), make_scalar(), make_image_filter()) :
robertphillips3d32d762015-07-13 13:16:44 -0700645 SkLightingImageFilter::CreateSpotLitSpecular(SkPoint3::Make(0, 0, 0),
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000646 make_point(), make_scalar(), make_scalar(), make_color(),
647 make_scalar(), make_scalar(), SkIntToScalar(R(10)), make_image_filter());
648 break;
649 case NOISE:
650 {
reedfe630452016-03-25 09:08:00 -0700651 sk_sp<SkShader> shader((R(2) == 1) ?
652 SkPerlinNoiseShader::MakeFractalNoise(
sugoi@google.combb8148b2013-09-19 19:51:27 +0000653 make_scalar(true), make_scalar(true), R(10.0f), make_scalar()) :
reedfe630452016-03-25 09:08:00 -0700654 SkPerlinNoiseShader::MakeTurbulence(
sugoi@google.combb8148b2013-09-19 19:51:27 +0000655 make_scalar(true), make_scalar(true), R(10.0f), make_scalar()));
ajuma77b6ba32016-01-08 14:58:35 -0800656 SkPaint paint;
657 paint.setShader(shader);
reed@google.com44699382013-10-31 17:28:30 +0000658 SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
659 SkIntToScalar(kBitmapSize)));
ajuma77b6ba32016-01-08 14:58:35 -0800660 filter = SkPaintImageFilter::Create(paint, &cropR);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000661 }
662 break;
663 case DROP_SHADOW:
sugoi234f0362014-10-23 13:59:52 -0700664 filter = SkDropShadowImageFilter::Create(make_scalar(), make_scalar(), make_scalar(true),
665 make_scalar(true), make_color(), make_shadow_mode(), make_image_filter(),
halcanary96fcdcc2015-08-27 07:41:13 -0700666 nullptr);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000667 break;
668 case MORPHOLOGY:
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000669 if (R(2) == 1) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000670 filter = SkDilateImageFilter::Create(R(static_cast<float>(kBitmapSize)),
sugoi@google.combb8148b2013-09-19 19:51:27 +0000671 R(static_cast<float>(kBitmapSize)), make_image_filter());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000672 } else {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000673 filter = SkErodeImageFilter::Create(R(static_cast<float>(kBitmapSize)),
sugoi@google.combb8148b2013-09-19 19:51:27 +0000674 R(static_cast<float>(kBitmapSize)), make_image_filter());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000675 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000676 break;
677 case BITMAP:
fmalita5598b632015-09-15 11:26:13 -0700678 {
reed9ce9d672016-03-17 10:51:11 -0700679 sk_sp<SkImage> image(SkImage::MakeFromBitmap(make_bitmap()));
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000680 if (R(2) == 1) {
reed9ce9d672016-03-17 10:51:11 -0700681 filter = SkImageSource::Create(image.get(), make_rect(), make_rect(), kHigh_SkFilterQuality);
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000682 } else {
reed9ce9d672016-03-17 10:51:11 -0700683 filter = SkImageSource::Create(image.get());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000684 }
fmalita5598b632015-09-15 11:26:13 -0700685 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000686 break;
687 case DISPLACE:
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000688 filter = SkDisplacementMapEffect::Create(make_channel_selector_type(),
689 make_channel_selector_type(), make_scalar(),
690 make_image_filter(false), make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000691 break;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000692 case TILE:
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000693 filter = SkTileImageFilter::Create(make_rect(), make_rect(), make_image_filter(false));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000694 break;
commit-bot@chromium.org7b8d72d2014-01-16 22:54:41 +0000695 case PICTURE:
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000696 {
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +0000697 SkRTreeFactory factory;
698 SkPictureRecorder recorder;
halcanary9d524f22016-03-29 09:03:52 -0700699 SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize),
700 SkIntToScalar(kBitmapSize),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700701 &factory, 0);
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +0000702 drawSomething(recordingCanvas);
reedca2622b2016-03-18 07:25:55 -0700703 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
robertphillips5ff17b12016-03-28 13:13:42 -0700704 filter = SkPictureImageFilter::Make(pict, make_rect()).release();
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000705 }
commit-bot@chromium.org7b8d72d2014-01-16 22:54:41 +0000706 break;
ajuma77b6ba32016-01-08 14:58:35 -0800707 case PAINT:
708 {
709 SkImageFilter::CropRect cropR(make_rect());
710 filter = SkPaintImageFilter::Create(make_paint(), &cropR);
711 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000712 default:
713 break;
714 }
715 return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
716}
717
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000718static SkImageFilter* make_serialized_image_filter() {
719 SkAutoTUnref<SkImageFilter> filter(make_image_filter(false));
720 SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter));
721 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
722 size_t len = data->size();
723#ifdef SK_ADD_RANDOM_BIT_FLIPS
724 unsigned char* p = const_cast<unsigned char*>(ptr);
725 for (size_t i = 0; i < len; ++i, ++p) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000726 if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
727 if (R(10) == 1) { // Then 10% of the time, change a whole byte
728 switch(R(3)) {
729 case 0:
730 *p ^= 0xFF; // Flip entire byte
731 break;
732 case 1:
733 *p = 0xFF; // Set all bits to 1
734 break;
735 case 2:
736 *p = 0x00; // Set all bits to 0
737 break;
738 }
739 } else {
740 *p ^= (1 << R(8));
741 }
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000742 }
743 }
744#endif // SK_ADD_RANDOM_BIT_FLIPS
745 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len,
746 SkImageFilter::GetFlattenableType());
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000747 return static_cast<SkImageFilter*>(flattenable);
748}
749
sugoi@google.combb8148b2013-09-19 19:51:27 +0000750static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000751 canvas->save();
752 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
753 SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
754 canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint);
755 canvas->restore();
756}
757
758static void do_fuzz(SkCanvas* canvas) {
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000759 SkImageFilter* filter = make_serialized_image_filter();
760
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000761#ifdef SK_FUZZER_IS_VERBOSE
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000762 static uint32_t numFilters = 0;
763 static uint32_t numValidFilters = 0;
764 if (0 == numFilters) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000765 printf("Fuzzing with %u\n", kSeed);
766 }
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000767 numFilters++;
bsalomon49f085d2014-09-05 13:34:00 -0700768 if (filter) {
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000769 numValidFilters++;
770 }
771 printf("Filter no : %u. Valid filters so far : %u\r", numFilters, numValidFilters);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000772 fflush(stdout);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000773#endif
774
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000775 SkPaint paint;
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000776 SkSafeUnref(paint.setImageFilter(filter));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000777 drawClippedBitmap(canvas, 0, 0, paint);
778}
779
780//////////////////////////////////////////////////////////////////////////////
781
782class ImageFilterFuzzView : public SampleView {
783public:
784 ImageFilterFuzzView() {
785 this->setBGColor(0xFFDDDDDD);
786 }
787
788protected:
789 // overrides from SkEventSink
790 virtual bool onQuery(SkEvent* evt) {
791 if (SampleCode::TitleQ(*evt)) {
792 SampleCode::TitleR(evt, "ImageFilterFuzzer");
793 return true;
794 }
795 return this->INHERITED::onQuery(evt);
796 }
797
798 void drawBG(SkCanvas* canvas) {
799 canvas->drawColor(0xFFDDDDDD);
800 }
801
802 virtual void onDrawContent(SkCanvas* canvas) {
803 do_fuzz(canvas);
804 this->inval(0);
805 }
806
807private:
808 typedef SkView INHERITED;
809};
810
811//////////////////////////////////////////////////////////////////////////////
812
813static SkView* MyFactory() { return new ImageFilterFuzzView; }
814static SkViewRegister reg(MyFactory);