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