blob: 6a4f1f822f7ebe1751edfcf196c383625d1506a5 [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
reedefdfd512016-04-04 10:02:58 -0700464static sk_sp<SkMaskFilter> make_mask_filter() {
465 sk_sp<SkMaskFilter> maskFilter;
ajuma77b6ba32016-01-08 14:58:35 -0800466 switch (R(3)) {
467 case 0:
reedefdfd512016-04-04 10:02:58 -0700468 maskFilter = SkBlurMaskFilter::Make(make_blur_style(), make_scalar(),
469 make_blur_mask_filter_flag());
ajuma77b6ba32016-01-08 14:58:35 -0800470 case 1: {
471 SkEmbossMaskFilter::Light light;
472 for (int i = 0; i < 3; ++i) {
473 light.fDirection[i] = make_scalar();
474 }
475 light.fPad = R(65536);
476 light.fAmbient = R(256);
477 light.fSpecular = R(256);
reedefdfd512016-04-04 10:02:58 -0700478 maskFilter = SkEmbossMaskFilter::Make(make_scalar(), light);
ajuma77b6ba32016-01-08 14:58:35 -0800479 }
480 case 2:
481 default:
ajuma77b6ba32016-01-08 14:58:35 -0800482 break;
483 }
484 return maskFilter;
485}
486
robertphillips372177e2016-03-30 07:32:28 -0700487static sk_sp<SkImageFilter> make_image_filter(bool canBeNull = true);
ajuma77b6ba32016-01-08 14:58:35 -0800488
489static SkPaint make_paint() {
490 SkPaint paint;
491 paint.setHinting(make_paint_hinting());
492 paint.setAntiAlias(make_bool());
493 paint.setDither(make_bool());
494 paint.setLinearText(make_bool());
495 paint.setSubpixelText(make_bool());
496 paint.setLCDRenderText(make_bool());
497 paint.setEmbeddedBitmapText(make_bool());
498 paint.setAutohinted(make_bool());
499 paint.setVerticalText(make_bool());
500 paint.setUnderlineText(make_bool());
501 paint.setStrikeThruText(make_bool());
502 paint.setFakeBoldText(make_bool());
503 paint.setDevKernText(make_bool());
504 paint.setFilterQuality(make_filter_quality());
505 paint.setStyle(make_paint_style());
506 paint.setColor(make_color());
507 paint.setStrokeWidth(make_scalar());
508 paint.setStrokeMiter(make_scalar());
509 paint.setStrokeCap(make_paint_cap());
510 paint.setStrokeJoin(make_paint_join());
511 paint.setColorFilter(make_color_filter());
512 paint.setXfermodeMode(make_xfermode());
513 paint.setPathEffect(make_path_effect());
514 paint.setMaskFilter(make_mask_filter());
reedca726ab2016-02-22 12:50:25 -0800515
516 if (false) {
517 // our validating buffer does not support typefaces yet, so skip this for now
518 SkAutoTUnref<SkTypeface> typeface(
519 SkTypeface::CreateFromName(make_font_name().c_str(), make_typeface_style()));
520 paint.setTypeface(typeface);
521 }
522
ajuma77b6ba32016-01-08 14:58:35 -0800523 SkLayerRasterizer::Builder rasterizerBuilder;
524 SkPaint paintForRasterizer;
525 if (R(2) == 1) {
526 paintForRasterizer = make_paint();
527 }
528 rasterizerBuilder.addLayer(paintForRasterizer);
reed7b380d02016-03-21 13:25:16 -0700529 paint.setRasterizer(rasterizerBuilder.detach());
ajuma77b6ba32016-01-08 14:58:35 -0800530 paint.setImageFilter(make_image_filter());
reedd053ce92016-03-22 10:17:23 -0700531 sk_sp<SkData> data(make_3Dlut(nullptr, make_bool(), make_bool(), make_bool()));
ajuma77b6ba32016-01-08 14:58:35 -0800532 paint.setTextAlign(make_paint_align());
533 paint.setTextSize(make_scalar());
534 paint.setTextScaleX(make_scalar());
535 paint.setTextSkewX(make_scalar());
536 paint.setTextEncoding(make_paint_text_encoding());
537 return paint;
538}
539
robertphillips372177e2016-03-30 07:32:28 -0700540static sk_sp<SkImageFilter> make_image_filter(bool canBeNull) {
541 sk_sp<SkImageFilter> filter;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000542
halcanary96fcdcc2015-08-27 07:41:13 -0700543 // Add a 1 in 3 chance to get a nullptr input
robertphillips372177e2016-03-30 07:32:28 -0700544 if (canBeNull && (R(3) == 1)) {
545 return filter;
546 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000547
sugoif11c5742014-10-27 08:03:56 -0700548 enum { ALPHA_THRESHOLD, MERGE, COLOR, LUT3D, BLUR, MAGNIFIER,
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000549 DOWN_SAMPLE, XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000550 DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
ajuma77b6ba32016-01-08 14:58:35 -0800551 MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS };
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000552
553 switch (R(NUM_FILTERS)) {
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000554 case ALPHA_THRESHOLD:
robertphillipsf6242042016-04-01 10:34:43 -0700555 filter = SkAlphaThresholdFilter::Make(make_region(),
556 make_scalar(),
557 make_scalar(),
robertphillipsef6a47b2016-04-08 08:01:20 -0700558 make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000559 break;
robertphillips2238c9d2016-03-30 13:34:16 -0700560 case MERGE:
561 filter = SkMergeImageFilter::Make(make_image_filter(),
562 make_image_filter(),
563 make_xfermode());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000564 break;
robertphillips372177e2016-03-30 07:32:28 -0700565 case COLOR: {
566 sk_sp<SkColorFilter> cf(make_color_filter());
robertphillips5605b562016-04-05 11:50:42 -0700567 filter = cf ? SkColorFilterImageFilter::Make(std::move(cf), make_image_filter())
robertphillips372177e2016-03-30 07:32:28 -0700568 : nullptr;
djsollen89057902016-03-29 19:07:07 -0700569 break;
robertphillips372177e2016-03-30 07:32:28 -0700570 }
571 case LUT3D: {
sugoif11c5742014-10-27 08:03:56 -0700572 int cubeDimension;
reedd053ce92016-03-22 10:17:23 -0700573 sk_sp<SkData> lut3D(make_3Dlut(&cubeDimension, (R(2) == 1), (R(2) == 1), (R(2) == 1)));
robertphillips5605b562016-04-05 11:50:42 -0700574 sk_sp<SkColorFilter> cf(SkColorCubeFilter::Make(std::move(lut3D), cubeDimension));
575 filter = cf ? SkColorFilterImageFilter::Make(std::move(cf), make_image_filter())
robertphillips372177e2016-03-30 07:32:28 -0700576 : nullptr;
577 break;
sugoif11c5742014-10-27 08:03:56 -0700578 }
robertphillips6e7025a2016-04-04 04:31:25 -0700579 case BLUR:
580 filter = SkBlurImageFilter::Make(make_scalar(true),
581 make_scalar(true),
582 make_image_filter());
sugoif11c5742014-10-27 08:03:56 -0700583 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000584 case MAGNIFIER:
robertphillips11171f32016-04-07 07:34:15 -0700585 filter = SkMagnifierImageFilter::Make(make_rect(),
586 make_scalar(true),
587 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000588 break;
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000589 case DOWN_SAMPLE:
robertphillipsfc0f44a2016-04-06 07:44:01 -0700590 filter = SkDownSampleImageFilter::Make(make_scalar(), make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000591 break;
robertphillips8c0326d2016-04-05 12:48:34 -0700592 case XFERMODE:
reedcfb6bdf2016-03-29 11:32:50 -0700593 filter = SkXfermodeImageFilter::Make(SkXfermode::Make(make_xfermode()),
robertphillips8c0326d2016-04-05 12:48:34 -0700594 make_image_filter(),
595 make_image_filter(),
robertphillips372177e2016-03-30 07:32:28 -0700596 nullptr);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000597 break;
robertphillips51a315e2016-03-31 09:05:49 -0700598 case OFFSET:
599 filter = SkOffsetImageFilter::Make(make_scalar(), make_scalar(), make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000600 break;
robertphillipsae8c9332016-04-05 15:09:00 -0700601 case MATRIX:
602 filter = SkImageFilter::MakeMatrixFilter(make_matrix(),
603 (SkFilterQuality)R(4),
604 make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000605 break;
robertphillips372177e2016-03-30 07:32:28 -0700606 case MATRIX_CONVOLUTION: {
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000607 SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
608 SkIntToScalar(kBitmapSize)));
609 SkISize size = SkISize::Make(R(10)+1, R(10)+1);
610 int arraySize = size.width() * size.height();
611 SkTArray<SkScalar> kernel(arraySize);
612 for (int i = 0; i < arraySize; ++i) {
613 kernel.push_back() = make_scalar();
614 }
commit-bot@chromium.orgb859fe62014-05-27 17:03:25 +0000615 SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
616 R(SkIntToScalar(size.height())));
robertphillips372177e2016-03-30 07:32:28 -0700617
robertphillipsef6a47b2016-04-08 08:01:20 -0700618 filter = SkMatrixConvolutionImageFilter::Make(size,
619 kernel.begin(),
620 make_scalar(),
621 make_scalar(),
622 kernelOffset,
623 (SkMatrixConvolutionImageFilter::TileMode)R(3),
624 R(2) == 1,
625 make_image_filter(),
626 &cropR);
robertphillips372177e2016-03-30 07:32:28 -0700627 break;
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000628 }
robertphillips491fb172016-03-30 12:32:58 -0700629 case COMPOSE:
630 filter = SkComposeImageFilter::Make(make_image_filter(), make_image_filter());
commit-bot@chromium.org067fe482014-05-27 13:52:51 +0000631 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700632 case DISTANT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700633 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700634 ? SkLightingImageFilter::MakeDistantLitDiffuse(make_point(), make_color(),
635 make_scalar(), make_scalar(),
636 make_image_filter())
637 : SkLightingImageFilter::MakeDistantLitSpecular(make_point(), make_color(),
638 make_scalar(), make_scalar(),
639 SkIntToScalar(R(10)),
640 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000641 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700642 case POINT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700643 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700644 ? SkLightingImageFilter::MakePointLitDiffuse(make_point(), make_color(),
645 make_scalar(), make_scalar(),
646 make_image_filter())
647 : SkLightingImageFilter::MakePointLitSpecular(make_point(), make_color(),
648 make_scalar(), make_scalar(),
649 SkIntToScalar(R(10)),
650 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000651 break;
robertphillips12fa47d2016-04-08 16:28:09 -0700652 case SPOT_LIGHT:
robertphillips372177e2016-03-30 07:32:28 -0700653 filter = (R(2) == 1)
robertphillips12fa47d2016-04-08 16:28:09 -0700654 ? SkLightingImageFilter::MakeSpotLitDiffuse(SkPoint3::Make(0, 0, 0),
655 make_point(), make_scalar(),
656 make_scalar(), make_color(),
657 make_scalar(), make_scalar(),
658 make_image_filter())
659 : SkLightingImageFilter::MakeSpotLitSpecular(SkPoint3::Make(0, 0, 0),
660 make_point(), make_scalar(),
661 make_scalar(), make_color(),
662 make_scalar(), make_scalar(),
663 SkIntToScalar(R(10)),
664 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000665 break;
robertphillips372177e2016-03-30 07:32:28 -0700666 case NOISE: {
robertphillips12fa47d2016-04-08 16:28:09 -0700667 sk_sp<SkShader> shader((R(2) == 1)
668 ? SkPerlinNoiseShader::MakeFractalNoise(make_scalar(true), make_scalar(true),
669 R(10.0f), make_scalar())
670 : SkPerlinNoiseShader::MakeTurbulence(make_scalar(true), make_scalar(true),
671 R(10.0f), make_scalar()));
ajuma77b6ba32016-01-08 14:58:35 -0800672 SkPaint paint;
673 paint.setShader(shader);
reed@google.com44699382013-10-31 17:28:30 +0000674 SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
675 SkIntToScalar(kBitmapSize)));
robertphillips372177e2016-03-30 07:32:28 -0700676 filter = SkPaintImageFilter::Make(paint, &cropR);
677 break;
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000678 }
robertphillipsc4169122016-04-06 08:40:59 -0700679 case DROP_SHADOW:
680 filter = SkDropShadowImageFilter::Make(make_scalar(),
681 make_scalar(),
682 make_scalar(true),
683 make_scalar(true),
684 make_color(),
685 make_shadow_mode(),
686 make_image_filter(),
687 nullptr);
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000688 break;
robertphillipsfc11b0a2016-04-05 09:09:36 -0700689 case MORPHOLOGY:
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000690 if (R(2) == 1) {
robertphillipsfc11b0a2016-04-05 09:09:36 -0700691 filter = SkDilateImageFilter::Make(R(static_cast<float>(kBitmapSize)),
692 R(static_cast<float>(kBitmapSize)),
693 make_image_filter());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000694 } else {
robertphillipsfc11b0a2016-04-05 09:09:36 -0700695 filter = SkErodeImageFilter::Make(R(static_cast<float>(kBitmapSize)),
696 R(static_cast<float>(kBitmapSize)),
697 make_image_filter());
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000698 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000699 break;
robertphillips372177e2016-03-30 07:32:28 -0700700 case BITMAP: {
reed9ce9d672016-03-17 10:51:11 -0700701 sk_sp<SkImage> image(SkImage::MakeFromBitmap(make_bitmap()));
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000702 if (R(2) == 1) {
robertphillips549c8992016-04-01 09:28:51 -0700703 filter = SkImageSource::Make(std::move(image),
704 make_rect(),
705 make_rect(),
706 kHigh_SkFilterQuality);
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000707 } else {
robertphillips549c8992016-04-01 09:28:51 -0700708 filter = SkImageSource::Make(std::move(image));
commit-bot@chromium.org43f4a552014-01-15 20:20:24 +0000709 }
robertphillips372177e2016-03-30 07:32:28 -0700710 break;
fmalita5598b632015-09-15 11:26:13 -0700711 }
robertphillipsbfe11fc2016-04-15 07:17:36 -0700712 case DISPLACE:
713 filter = SkDisplacementMapEffect::Make(make_channel_selector_type(),
714 make_channel_selector_type(),
715 make_scalar(),
716 make_image_filter(false),
717 make_image_filter());
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000718 break;
robertphillips534c2702016-04-15 07:57:40 -0700719 case TILE:
720 filter = SkTileImageFilter::Make(make_rect(), make_rect(), make_image_filter(false));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000721 break;
robertphillips372177e2016-03-30 07:32:28 -0700722 case PICTURE: {
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +0000723 SkRTreeFactory factory;
724 SkPictureRecorder recorder;
halcanary9d524f22016-03-29 09:03:52 -0700725 SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize),
726 SkIntToScalar(kBitmapSize),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700727 &factory, 0);
senorblanco@chromium.org97f5fc62014-05-30 20:50:56 +0000728 drawSomething(recordingCanvas);
reedca2622b2016-03-18 07:25:55 -0700729 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
robertphillips372177e2016-03-30 07:32:28 -0700730 filter = SkPictureImageFilter::Make(pict, make_rect());
robertphillipsa33cf072016-03-29 13:54:26 -0700731 break;
robertphillips372177e2016-03-30 07:32:28 -0700732 }
733 case PAINT: {
djsollen89057902016-03-29 19:07:07 -0700734 SkImageFilter::CropRect cropR(make_rect());
robertphillips372177e2016-03-30 07:32:28 -0700735 filter = SkPaintImageFilter::Make(make_paint(), &cropR);
736 break;
ajuma77b6ba32016-01-08 14:58:35 -0800737 }
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000738 default:
739 break;
740 }
741 return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
742}
743
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000744static SkImageFilter* make_serialized_image_filter() {
robertphillips372177e2016-03-30 07:32:28 -0700745 sk_sp<SkImageFilter> filter(make_image_filter(false));
746 SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter.get()));
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000747 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
748 size_t len = data->size();
749#ifdef SK_ADD_RANDOM_BIT_FLIPS
750 unsigned char* p = const_cast<unsigned char*>(ptr);
751 for (size_t i = 0; i < len; ++i, ++p) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000752 if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
753 if (R(10) == 1) { // Then 10% of the time, change a whole byte
754 switch(R(3)) {
755 case 0:
756 *p ^= 0xFF; // Flip entire byte
757 break;
758 case 1:
759 *p = 0xFF; // Set all bits to 1
760 break;
761 case 2:
762 *p = 0x00; // Set all bits to 0
763 break;
764 }
765 } else {
766 *p ^= (1 << R(8));
767 }
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000768 }
769 }
770#endif // SK_ADD_RANDOM_BIT_FLIPS
mtklein3b375452016-04-04 14:57:19 -0700771 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len,
772 SkImageFilter::GetFlattenableType());
commit-bot@chromium.org77e079a2013-10-28 15:52:02 +0000773 return static_cast<SkImageFilter*>(flattenable);
774}
775
sugoi@google.combb8148b2013-09-19 19:51:27 +0000776static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000777 canvas->save();
778 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
779 SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
780 canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint);
781 canvas->restore();
782}
783
784static void do_fuzz(SkCanvas* canvas) {
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000785 SkImageFilter* filter = make_serialized_image_filter();
786
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000787#ifdef SK_FUZZER_IS_VERBOSE
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000788 static uint32_t numFilters = 0;
789 static uint32_t numValidFilters = 0;
790 if (0 == numFilters) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000791 printf("Fuzzing with %u\n", kSeed);
792 }
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000793 numFilters++;
bsalomon49f085d2014-09-05 13:34:00 -0700794 if (filter) {
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000795 numValidFilters++;
796 }
797 printf("Filter no : %u. Valid filters so far : %u\r", numFilters, numValidFilters);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000798 fflush(stdout);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000799#endif
800
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000801 SkPaint paint;
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000802 SkSafeUnref(paint.setImageFilter(filter));
commit-bot@chromium.org22d69f92013-09-19 18:40:49 +0000803 drawClippedBitmap(canvas, 0, 0, paint);
804}
805
806//////////////////////////////////////////////////////////////////////////////
807
808class ImageFilterFuzzView : public SampleView {
809public:
810 ImageFilterFuzzView() {
811 this->setBGColor(0xFFDDDDDD);
812 }
813
814protected:
815 // overrides from SkEventSink
816 virtual bool onQuery(SkEvent* evt) {
817 if (SampleCode::TitleQ(*evt)) {
818 SampleCode::TitleR(evt, "ImageFilterFuzzer");
819 return true;
820 }
821 return this->INHERITED::onQuery(evt);
822 }
823
824 void drawBG(SkCanvas* canvas) {
825 canvas->drawColor(0xFFDDDDDD);
826 }
827
828 virtual void onDrawContent(SkCanvas* canvas) {
829 do_fuzz(canvas);
830 this->inval(0);
831 }
832
833private:
834 typedef SkView INHERITED;
835};
836
837//////////////////////////////////////////////////////////////////////////////
838
839static SkView* MyFactory() { return new ImageFilterFuzzView; }
840static SkViewRegister reg(MyFactory);