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" |
| 11 | #include "SkArcToPathEffect.h" |
| 12 | #include "SkBlurImageFilter.h" |
| 13 | #include "SkBlurMaskFilter.h" |
| 14 | #include "SkCanvas.h" |
| 15 | #include "SkColorCubeFilter.h" |
| 16 | #include "SkColorFilter.h" |
| 17 | #include "SkColorFilterImageFilter.h" |
| 18 | #include "SkColorMatrixFilter.h" |
| 19 | #include "SkComposeImageFilter.h" |
| 20 | #include "SkCornerPathEffect.h" |
| 21 | #include "SkDashPathEffect.h" |
| 22 | #include "SkData.h" |
| 23 | #include "SkDiscretePathEffect.h" |
| 24 | #include "SkDisplacementMapEffect.h" |
| 25 | #include "SkDropShadowImageFilter.h" |
| 26 | #include "SkEmbossMaskFilter.h" |
| 27 | #include "SkFlattenableSerialization.h" |
| 28 | #include "SkImageSource.h" |
| 29 | #include "SkLayerRasterizer.h" |
| 30 | #include "SkLightingImageFilter.h" |
| 31 | #include "SkLumaColorFilter.h" |
| 32 | #include "SkMagnifierImageFilter.h" |
| 33 | #include "SkMatrixConvolutionImageFilter.h" |
| 34 | #include "SkMergeImageFilter.h" |
| 35 | #include "SkMorphologyImageFilter.h" |
| 36 | #include "SkOffsetImageFilter.h" |
| 37 | #include "SkPaintImageFilter.h" |
| 38 | #include "SkPerlinNoiseShader.h" |
| 39 | #include "SkPictureImageFilter.h" |
| 40 | #include "SkPictureRecorder.h" |
| 41 | #include "SkPoint3.h" |
| 42 | #include "SkRandom.h" |
| 43 | #include "SkTableColorFilter.h" |
| 44 | #include "SkTestImageFilters.h" |
| 45 | #include "SkTileImageFilter.h" |
| 46 | #include "SkTypeface.h" |
| 47 | #include "SkXfermodeImageFilter.h" |
| 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 | |
| 56 | static bool return_large = false; |
| 57 | static bool return_undef = false; |
| 58 | |
| 59 | static int R(float x) { |
| 60 | return (int)floor(SkScalarToFloat(fuzz->nextF1()) * x); |
| 61 | } |
| 62 | |
| 63 | #if defined _WIN32 |
| 64 | #pragma warning ( push ) |
| 65 | // we are intentionally causing an overflow here |
| 66 | // (warning C4756: overflow in constant arithmetic) |
| 67 | #pragma warning ( disable : 4756 ) |
| 68 | #endif |
| 69 | |
| 70 | static float huge() { |
| 71 | double d = 1e100; |
| 72 | float f = (float)d; |
| 73 | return f; |
| 74 | } |
| 75 | |
| 76 | #if defined _WIN32 |
| 77 | #pragma warning ( pop ) |
| 78 | #endif |
| 79 | |
| 80 | static float make_number(bool positiveOnly) { |
| 81 | float f = positiveOnly ? 1.0f : 0.0f; |
| 82 | float v = f; |
| 83 | int sel; |
| 84 | |
| 85 | if (return_large) sel = R(6); else sel = R(4); |
| 86 | if (!return_undef && sel == 0) sel = 1; |
| 87 | |
| 88 | if (R(2) == 1) v = (float)(R(100)+f); else |
| 89 | |
| 90 | switch (sel) { |
| 91 | case 0: break; |
| 92 | case 1: v = f; break; |
| 93 | case 2: v = 0.000001f; break; |
| 94 | case 3: v = 10000.0f; break; |
| 95 | case 4: v = 2000000000.0f; break; |
| 96 | case 5: v = huge(); break; |
| 97 | } |
| 98 | |
| 99 | if (!positiveOnly && (R(4) == 1)) v = -v; |
| 100 | return v; |
| 101 | } |
| 102 | |
| 103 | static SkScalar make_scalar(bool positiveOnly = false) { |
| 104 | return make_number(positiveOnly); |
| 105 | } |
| 106 | |
| 107 | static SkString make_string() { |
| 108 | int length = R(1000); |
| 109 | SkString str(length); |
| 110 | for (int i = 0; i < length; ++i) { |
| 111 | str[i] = static_cast<char>(R(256)); |
| 112 | } |
| 113 | return str; |
| 114 | } |
| 115 | |
| 116 | static SkString make_font_name() { |
| 117 | int sel = R(8); |
| 118 | |
| 119 | switch(sel) { |
| 120 | case 0: return SkString("Courier New"); |
| 121 | case 1: return SkString("Helvetica"); |
| 122 | case 2: return SkString("monospace"); |
| 123 | case 3: return SkString("sans-serif"); |
| 124 | case 4: return SkString("serif"); |
| 125 | case 5: return SkString("Times"); |
| 126 | case 6: return SkString("Times New Roman"); |
| 127 | case 7: |
| 128 | default: |
| 129 | return make_string(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static bool make_bool() { |
| 134 | return R(2) == 1; |
| 135 | } |
| 136 | |
| 137 | static SkRect make_rect() { |
| 138 | return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))), |
| 139 | SkIntToScalar(R(static_cast<float>(kBitmapSize)))); |
| 140 | } |
| 141 | |
| 142 | static SkRegion make_region() { |
| 143 | SkIRect iRegion = SkIRect::MakeXYWH(R(static_cast<float>(kBitmapSize)), |
| 144 | R(static_cast<float>(kBitmapSize)), |
| 145 | R(static_cast<float>(kBitmapSize)), |
| 146 | R(static_cast<float>(kBitmapSize))); |
| 147 | return SkRegion(iRegion); |
| 148 | } |
| 149 | |
| 150 | static SkMatrix make_matrix() { |
| 151 | SkMatrix m; |
| 152 | for (int i = 0; i < 9; ++i) { |
| 153 | m[i] = make_scalar(); |
| 154 | } |
| 155 | return m; |
| 156 | } |
| 157 | |
| 158 | static SkXfermode::Mode make_xfermode() { |
| 159 | return static_cast<SkXfermode::Mode>(R(SkXfermode::kLastMode+1)); |
| 160 | } |
| 161 | |
| 162 | static SkPaint::Align make_paint_align() { |
| 163 | return static_cast<SkPaint::Align>(R(SkPaint::kRight_Align+1)); |
| 164 | } |
| 165 | |
| 166 | static SkPaint::Hinting make_paint_hinting() { |
| 167 | return static_cast<SkPaint::Hinting>(R(SkPaint::kFull_Hinting+1)); |
| 168 | } |
| 169 | |
| 170 | static SkPaint::Style make_paint_style() { |
| 171 | return static_cast<SkPaint::Style>(R(SkPaint::kStrokeAndFill_Style+1)); |
| 172 | } |
| 173 | |
| 174 | static SkPaint::Cap make_paint_cap() { |
| 175 | return static_cast<SkPaint::Cap>(R(SkPaint::kDefault_Cap+1)); |
| 176 | } |
| 177 | |
| 178 | static SkPaint::Join make_paint_join() { |
| 179 | return static_cast<SkPaint::Join>(R(SkPaint::kDefault_Join+1)); |
| 180 | } |
| 181 | |
| 182 | static SkPaint::TextEncoding make_paint_text_encoding() { |
| 183 | return static_cast<SkPaint::TextEncoding>(R(SkPaint::kGlyphID_TextEncoding+1)); |
| 184 | } |
| 185 | |
| 186 | static SkBlurStyle make_blur_style() { |
| 187 | return static_cast<SkBlurStyle>(R(kLastEnum_SkBlurStyle+1)); |
| 188 | } |
| 189 | |
| 190 | static SkBlurMaskFilter::BlurFlags make_blur_mask_filter_flag() { |
| 191 | return static_cast<SkBlurMaskFilter::BlurFlags>(R(SkBlurMaskFilter::kAll_BlurFlag+1)); |
| 192 | } |
| 193 | |
| 194 | static SkFilterQuality make_filter_quality() { |
| 195 | return static_cast<SkFilterQuality>(R(kHigh_SkFilterQuality+1)); |
| 196 | } |
| 197 | |
| 198 | static SkTypeface::Style make_typeface_style() { |
| 199 | return static_cast<SkTypeface::Style>(R(SkTypeface::kBoldItalic+1)); |
| 200 | } |
| 201 | |
| 202 | static SkPath1DPathEffect::Style make_path_1d_path_effect_style() { |
| 203 | return static_cast<SkPath1DPathEffect::Style>(R((int)SkPath1DPathEffect::kLastEnum_Style + 1)); |
| 204 | } |
| 205 | |
| 206 | static SkColor make_color() { |
| 207 | return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090; |
| 208 | } |
| 209 | |
| 210 | static SkDropShadowImageFilter::ShadowMode make_shadow_mode() { |
| 211 | return (R(2) == 1) ? SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode : |
| 212 | SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode; |
| 213 | } |
| 214 | |
| 215 | static SkPoint3 make_point() { |
| 216 | return SkPoint3::Make(make_scalar(), make_scalar(), make_scalar(true)); |
| 217 | } |
| 218 | |
| 219 | static SkDisplacementMapEffect::ChannelSelectorType make_channel_selector_type() { |
| 220 | return static_cast<SkDisplacementMapEffect::ChannelSelectorType>(R(4)+1); |
| 221 | } |
| 222 | |
| 223 | static bool valid_for_raster_canvas(const SkImageInfo& info) { |
| 224 | switch (info.colorType()) { |
| 225 | case kAlpha_8_SkColorType: |
| 226 | case kRGB_565_SkColorType: |
| 227 | return true; |
| 228 | case kN32_SkColorType: |
| 229 | return kPremul_SkAlphaType == info.alphaType() || |
| 230 | kOpaque_SkAlphaType == info.alphaType(); |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | static SkColorType rand_colortype() { |
| 238 | return (SkColorType)R(kLastEnum_SkColorType + 1); |
| 239 | } |
| 240 | |
| 241 | static void rand_bitmap_for_canvas(SkBitmap* bitmap) { |
| 242 | SkImageInfo info; |
| 243 | do { |
| 244 | info = SkImageInfo::Make(kBitmapSize, kBitmapSize, rand_colortype(), |
| 245 | kPremul_SkAlphaType); |
| 246 | } while (!valid_for_raster_canvas(info) || !bitmap->tryAllocPixels(info)); |
| 247 | } |
| 248 | |
| 249 | static void make_g_bitmap(SkBitmap& bitmap) { |
| 250 | rand_bitmap_for_canvas(&bitmap); |
| 251 | |
| 252 | SkCanvas canvas(bitmap); |
| 253 | canvas.clear(0x00000000); |
| 254 | SkPaint paint; |
| 255 | paint.setAntiAlias(true); |
| 256 | paint.setColor(0xFF884422); |
| 257 | paint.setTextSize(SkIntToScalar(kBitmapSize/2)); |
| 258 | const char* str = "g"; |
| 259 | canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8), |
| 260 | SkIntToScalar(kBitmapSize/4), paint); |
| 261 | } |
| 262 | |
| 263 | static void make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 264 | rand_bitmap_for_canvas(&bitmap); |
| 265 | |
| 266 | SkCanvas canvas(bitmap); |
| 267 | canvas.clear(0x00000000); |
| 268 | SkPaint darkPaint; |
| 269 | darkPaint.setColor(0xFF804020); |
| 270 | SkPaint lightPaint; |
| 271 | lightPaint.setColor(0xFF244484); |
| 272 | const int i = kBitmapSize / 8; |
| 273 | const SkScalar f = SkIntToScalar(i); |
| 274 | for (int y = 0; y < kBitmapSize; y += i) { |
| 275 | for (int x = 0; x < kBitmapSize; x += i) { |
| 276 | canvas.save(); |
| 277 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 278 | canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 279 | canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 280 | canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 281 | canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 282 | canvas.restore(); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | static const SkBitmap& make_bitmap() { |
| 288 | static SkBitmap bitmap[2]; |
| 289 | static bool initialized = false; |
| 290 | if (!initialized) { |
| 291 | make_g_bitmap(bitmap[0]); |
| 292 | make_checkerboard_bitmap(bitmap[1]); |
| 293 | initialized = true; |
| 294 | } |
| 295 | return bitmap[R(2)]; |
| 296 | } |
| 297 | |
| 298 | static sk_sp<SkData> make_3Dlut(int* cubeDimension, bool invR, bool invG, bool invB) { |
| 299 | int size = 4 << R(5); |
| 300 | auto data = SkData::MakeUninitialized(sizeof(SkColor) * size * size * size); |
| 301 | SkColor* pixels = (SkColor*)(data->writable_data()); |
| 302 | SkAutoTMalloc<uint8_t> lutMemory(size); |
| 303 | SkAutoTMalloc<uint8_t> invLutMemory(size); |
| 304 | uint8_t* lut = lutMemory.get(); |
| 305 | uint8_t* invLut = invLutMemory.get(); |
| 306 | const int maxIndex = size - 1; |
| 307 | for (int i = 0; i < size; i++) { |
| 308 | lut[i] = (i * 255) / maxIndex; |
| 309 | invLut[i] = ((maxIndex - i) * 255) / maxIndex; |
| 310 | } |
| 311 | for (int r = 0; r < size; ++r) { |
| 312 | for (int g = 0; g < size; ++g) { |
| 313 | for (int b = 0; b < size; ++b) { |
| 314 | pixels[(size * ((size * b) + g)) + r] = SkColorSetARGB(0xFF, |
| 315 | invR ? invLut[r] : lut[r], |
| 316 | invG ? invLut[g] : lut[g], |
| 317 | invB ? invLut[b] : lut[b]); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | if (cubeDimension) { |
| 322 | *cubeDimension = size; |
| 323 | } |
| 324 | return data; |
| 325 | } |
| 326 | |
| 327 | static void drawSomething(SkCanvas* canvas) { |
| 328 | SkPaint paint; |
| 329 | |
| 330 | canvas->save(); |
| 331 | canvas->scale(0.5f, 0.5f); |
| 332 | canvas->drawBitmap(make_bitmap(), 0, 0, nullptr); |
| 333 | canvas->restore(); |
| 334 | |
| 335 | paint.setAntiAlias(true); |
| 336 | |
| 337 | paint.setColor(SK_ColorRED); |
| 338 | canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint); |
| 339 | paint.setColor(SK_ColorBLACK); |
| 340 | paint.setTextSize(SkIntToScalar(kBitmapSize/3)); |
| 341 | canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint); |
| 342 | } |
| 343 | |
| 344 | static void rand_color_table(uint8_t* table) { |
| 345 | for (int i = 0; i < 256; ++i) { |
| 346 | table[i] = R(256); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static sk_sp<SkColorFilter> make_color_filter() { |
| 351 | switch (R(6)) { |
| 352 | case 0: { |
| 353 | SkScalar array[20]; |
| 354 | for (int i = 0; i < 20; ++i) { |
| 355 | array[i] = make_scalar(); |
| 356 | } |
| 357 | return SkColorFilter::MakeMatrixFilterRowMajor255(array); |
| 358 | } |
| 359 | case 1: |
| 360 | return SkLumaColorFilter::Make(); |
| 361 | case 2: { |
| 362 | uint8_t tableA[256]; |
| 363 | uint8_t tableR[256]; |
| 364 | uint8_t tableG[256]; |
| 365 | uint8_t tableB[256]; |
| 366 | rand_color_table(tableA); |
| 367 | rand_color_table(tableR); |
| 368 | rand_color_table(tableG); |
| 369 | rand_color_table(tableB); |
| 370 | return SkTableColorFilter::MakeARGB(tableA, tableR, tableG, tableB); |
| 371 | } |
| 372 | case 3: |
| 373 | return SkColorFilter::MakeModeFilter(make_color(), make_xfermode()); |
| 374 | case 4: |
| 375 | return SkColorMatrixFilter::MakeLightingFilter(make_color(), make_color()); |
| 376 | case 5: |
| 377 | default: |
| 378 | break; |
| 379 | } |
| 380 | return nullptr; |
| 381 | } |
| 382 | |
| 383 | static SkPath make_path() { |
| 384 | SkPath path; |
| 385 | int numOps = R(30); |
| 386 | for (int i = 0; i < numOps; ++i) { |
| 387 | switch (R(6)) { |
| 388 | case 0: |
| 389 | path.moveTo(make_scalar(), make_scalar()); |
| 390 | break; |
| 391 | case 1: |
| 392 | path.lineTo(make_scalar(), make_scalar()); |
| 393 | break; |
| 394 | case 2: |
| 395 | path.quadTo(make_scalar(), make_scalar(), make_scalar(), make_scalar()); |
| 396 | break; |
| 397 | case 3: |
| 398 | path.conicTo(make_scalar(), make_scalar(), make_scalar(), make_scalar(), make_scalar()); |
| 399 | break; |
| 400 | case 4: |
| 401 | path.cubicTo(make_scalar(), make_scalar(), make_scalar(), |
| 402 | make_scalar(), make_scalar(), make_scalar()); |
| 403 | break; |
| 404 | case 5: |
| 405 | default: |
| 406 | path.arcTo(make_scalar(), make_scalar(), make_scalar(), make_scalar(), make_scalar()); |
| 407 | break; |
| 408 | |
| 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; |
| 417 | if (canBeNull && (R(3) == 1)) { return pathEffect; } |
| 418 | |
| 419 | switch (R(9)) { |
| 420 | case 0: |
| 421 | pathEffect = SkArcToPathEffect::Make(make_scalar(true)); |
| 422 | break; |
| 423 | case 1: |
| 424 | pathEffect = SkComposePathEffect::Make(make_path_effect(false), |
| 425 | make_path_effect(false)); |
| 426 | break; |
| 427 | case 2: |
| 428 | pathEffect = SkCornerPathEffect::Make(make_scalar()); |
| 429 | break; |
| 430 | case 3: { |
| 431 | int count = R(10); |
| 432 | SkScalar intervals[10]; |
| 433 | for (int i = 0; i < count; ++i) { |
| 434 | intervals[i] = make_scalar(); |
| 435 | } |
| 436 | pathEffect = SkDashPathEffect::Make(intervals, count, make_scalar()); |
| 437 | break; |
| 438 | } |
| 439 | case 4: |
| 440 | pathEffect = SkDiscretePathEffect::Make(make_scalar(), make_scalar()); |
| 441 | break; |
| 442 | case 5: |
| 443 | pathEffect = SkPath1DPathEffect::Make(make_path(), make_scalar(), make_scalar(), |
| 444 | make_path_1d_path_effect_style()); |
| 445 | break; |
| 446 | case 6: |
| 447 | pathEffect = SkLine2DPathEffect::Make(make_scalar(), make_matrix()); |
| 448 | break; |
| 449 | case 7: |
| 450 | pathEffect = SkPath2DPathEffect::Make(make_matrix(), make_path()); |
| 451 | break; |
| 452 | case 8: |
| 453 | default: |
| 454 | pathEffect = SkSumPathEffect::Make(make_path_effect(false), |
| 455 | make_path_effect(false)); |
| 456 | break; |
| 457 | } |
| 458 | return pathEffect; |
| 459 | } |
| 460 | |
| 461 | static sk_sp<SkMaskFilter> make_mask_filter() { |
| 462 | sk_sp<SkMaskFilter> maskFilter; |
| 463 | switch (R(3)) { |
| 464 | case 0: |
| 465 | maskFilter = SkBlurMaskFilter::Make(make_blur_style(), make_scalar(), |
| 466 | make_blur_mask_filter_flag()); |
| 467 | case 1: { |
| 468 | SkEmbossMaskFilter::Light light; |
| 469 | for (int i = 0; i < 3; ++i) { |
| 470 | light.fDirection[i] = make_scalar(); |
| 471 | } |
| 472 | light.fPad = R(65536); |
| 473 | light.fAmbient = R(256); |
| 474 | light.fSpecular = R(256); |
| 475 | maskFilter = SkEmbossMaskFilter::Make(make_scalar(), light); |
| 476 | } |
| 477 | case 2: |
| 478 | default: |
| 479 | break; |
| 480 | } |
| 481 | return maskFilter; |
| 482 | } |
| 483 | |
| 484 | static sk_sp<SkImageFilter> make_image_filter(bool canBeNull = true); |
| 485 | |
| 486 | static SkPaint make_paint() { |
| 487 | SkPaint paint; |
| 488 | paint.setHinting(make_paint_hinting()); |
| 489 | paint.setAntiAlias(make_bool()); |
| 490 | paint.setDither(make_bool()); |
| 491 | paint.setLinearText(make_bool()); |
| 492 | paint.setSubpixelText(make_bool()); |
| 493 | paint.setLCDRenderText(make_bool()); |
| 494 | paint.setEmbeddedBitmapText(make_bool()); |
| 495 | paint.setAutohinted(make_bool()); |
| 496 | paint.setVerticalText(make_bool()); |
| 497 | paint.setUnderlineText(make_bool()); |
| 498 | paint.setStrikeThruText(make_bool()); |
| 499 | paint.setFakeBoldText(make_bool()); |
| 500 | paint.setDevKernText(make_bool()); |
| 501 | paint.setFilterQuality(make_filter_quality()); |
| 502 | paint.setStyle(make_paint_style()); |
| 503 | paint.setColor(make_color()); |
| 504 | paint.setStrokeWidth(make_scalar()); |
| 505 | paint.setStrokeMiter(make_scalar()); |
| 506 | paint.setStrokeCap(make_paint_cap()); |
| 507 | paint.setStrokeJoin(make_paint_join()); |
| 508 | paint.setColorFilter(make_color_filter()); |
| 509 | paint.setXfermodeMode(make_xfermode()); |
| 510 | paint.setPathEffect(make_path_effect()); |
| 511 | paint.setMaskFilter(make_mask_filter()); |
| 512 | |
| 513 | if (false) { |
| 514 | // our validating buffer does not support typefaces yet, so skip this for now |
| 515 | SkAutoTUnref<SkTypeface> typeface( |
| 516 | SkTypeface::CreateFromName(make_font_name().c_str(), make_typeface_style())); |
| 517 | paint.setTypeface(typeface); |
| 518 | } |
| 519 | |
| 520 | SkLayerRasterizer::Builder rasterizerBuilder; |
| 521 | SkPaint paintForRasterizer; |
| 522 | if (R(2) == 1) { |
| 523 | paintForRasterizer = make_paint(); |
| 524 | } |
| 525 | rasterizerBuilder.addLayer(paintForRasterizer); |
| 526 | paint.setRasterizer(rasterizerBuilder.detach()); |
| 527 | paint.setImageFilter(make_image_filter()); |
| 528 | sk_sp<SkData> data(make_3Dlut(nullptr, make_bool(), make_bool(), make_bool())); |
| 529 | paint.setTextAlign(make_paint_align()); |
| 530 | paint.setTextSize(make_scalar()); |
| 531 | paint.setTextScaleX(make_scalar()); |
| 532 | paint.setTextSkewX(make_scalar()); |
| 533 | paint.setTextEncoding(make_paint_text_encoding()); |
| 534 | return paint; |
| 535 | } |
| 536 | |
| 537 | static sk_sp<SkImageFilter> make_image_filter(bool canBeNull) { |
| 538 | sk_sp<SkImageFilter> filter; |
| 539 | |
| 540 | // Add a 1 in 3 chance to get a nullptr input |
| 541 | if (canBeNull && (R(3) == 1)) { |
| 542 | return filter; |
| 543 | } |
| 544 | |
| 545 | enum { ALPHA_THRESHOLD, MERGE, COLOR, LUT3D, BLUR, MAGNIFIER, |
| 546 | DOWN_SAMPLE, XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE, |
| 547 | DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW, |
| 548 | MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS }; |
| 549 | |
| 550 | switch (R(NUM_FILTERS)) { |
| 551 | case ALPHA_THRESHOLD: |
| 552 | filter = SkAlphaThresholdFilter::Make(make_region(), |
| 553 | make_scalar(), |
| 554 | make_scalar(), |
| 555 | nullptr); |
| 556 | break; |
| 557 | case MERGE: |
| 558 | filter = SkMergeImageFilter::Make(make_image_filter(), |
| 559 | make_image_filter(), |
| 560 | make_xfermode()); |
| 561 | break; |
| 562 | case COLOR: { |
| 563 | sk_sp<SkColorFilter> cf(make_color_filter()); |
| 564 | filter = cf ? SkColorFilterImageFilter::Make(std::move(cf), make_image_filter()) |
| 565 | : nullptr; |
| 566 | break; |
| 567 | } |
| 568 | case LUT3D: { |
| 569 | int cubeDimension; |
| 570 | sk_sp<SkData> lut3D(make_3Dlut(&cubeDimension, (R(2) == 1), (R(2) == 1), (R(2) == 1))); |
| 571 | sk_sp<SkColorFilter> cf(SkColorCubeFilter::Make(std::move(lut3D), cubeDimension)); |
| 572 | filter = cf ? SkColorFilterImageFilter::Make(std::move(cf), make_image_filter()) |
| 573 | : nullptr; |
| 574 | break; |
| 575 | } |
| 576 | case BLUR: |
| 577 | filter = SkBlurImageFilter::Make(make_scalar(true), |
| 578 | make_scalar(true), |
| 579 | make_image_filter()); |
| 580 | break; |
| 581 | case MAGNIFIER: |
| 582 | filter = sk_sp<SkImageFilter>(SkMagnifierImageFilter::Create(make_rect(), |
| 583 | make_scalar(true))); |
| 584 | break; |
| 585 | case DOWN_SAMPLE: |
robertphillips | fc0f44a | 2016-04-06 07:44:01 -0700 | [diff] [blame] | 586 | filter = SkDownSampleImageFilter::Make(make_scalar(), make_image_filter()); |
kjlubick | 4319593 | 2016-04-05 12:48:47 -0700 | [diff] [blame] | 587 | break; |
| 588 | case XFERMODE: |
| 589 | filter = SkXfermodeImageFilter::Make(SkXfermode::Make(make_xfermode()), |
| 590 | make_image_filter(), |
| 591 | make_image_filter(), |
| 592 | nullptr); |
| 593 | break; |
| 594 | case OFFSET: |
| 595 | filter = SkOffsetImageFilter::Make(make_scalar(), make_scalar(), make_image_filter()); |
| 596 | break; |
robertphillips | ae8c933 | 2016-04-05 15:09:00 -0700 | [diff] [blame] | 597 | case MATRIX: |
| 598 | filter = SkImageFilter::MakeMatrixFilter(make_matrix(), |
| 599 | (SkFilterQuality)R(4), |
| 600 | make_image_filter()); |
kjlubick | 4319593 | 2016-04-05 12:48:47 -0700 | [diff] [blame] | 601 | break; |
kjlubick | 4319593 | 2016-04-05 12:48:47 -0700 | [diff] [blame] | 602 | case MATRIX_CONVOLUTION: { |
| 603 | SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 604 | SkIntToScalar(kBitmapSize))); |
| 605 | SkISize size = SkISize::Make(R(10)+1, R(10)+1); |
| 606 | int arraySize = size.width() * size.height(); |
| 607 | SkTArray<SkScalar> kernel(arraySize); |
| 608 | for (int i = 0; i < arraySize; ++i) { |
| 609 | kernel.push_back() = make_scalar(); |
| 610 | } |
| 611 | SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())), |
| 612 | R(SkIntToScalar(size.height()))); |
| 613 | sk_sp<SkImageFilter> subFilter(make_image_filter()); |
| 614 | |
| 615 | filter = sk_sp<SkImageFilter>(SkMatrixConvolutionImageFilter::Create( |
| 616 | size, |
| 617 | kernel.begin(), |
| 618 | make_scalar(), |
| 619 | make_scalar(), |
| 620 | kernelOffset, |
| 621 | (SkMatrixConvolutionImageFilter::TileMode)R(3), |
| 622 | R(2) == 1, |
| 623 | subFilter.get(), |
| 624 | &cropR)); |
| 625 | break; |
| 626 | } |
| 627 | case COMPOSE: |
| 628 | filter = SkComposeImageFilter::Make(make_image_filter(), make_image_filter()); |
| 629 | break; |
| 630 | case DISTANT_LIGHT: { |
| 631 | sk_sp<SkImageFilter> subFilter(make_image_filter()); |
| 632 | |
| 633 | filter = (R(2) == 1) |
| 634 | ? sk_sp<SkImageFilter>(SkLightingImageFilter::CreateDistantLitDiffuse(make_point(), |
| 635 | make_color(), make_scalar(), make_scalar(), |
| 636 | subFilter.get())) |
| 637 | : sk_sp<SkImageFilter>(SkLightingImageFilter::CreateDistantLitSpecular(make_point(), |
| 638 | make_color(), make_scalar(), |
| 639 | make_scalar(), SkIntToScalar(R(10)), |
| 640 | subFilter.get())); |
| 641 | break; |
| 642 | } |
| 643 | case POINT_LIGHT: { |
| 644 | sk_sp<SkImageFilter> subFilter(make_image_filter()); |
| 645 | |
| 646 | filter = (R(2) == 1) |
| 647 | ? sk_sp<SkImageFilter>(SkLightingImageFilter::CreatePointLitDiffuse(make_point(), |
| 648 | make_color(), make_scalar(), make_scalar(), |
| 649 | subFilter.get())) |
| 650 | : sk_sp<SkImageFilter>(SkLightingImageFilter::CreatePointLitSpecular(make_point(), |
| 651 | make_color(), make_scalar(), make_scalar(), |
| 652 | SkIntToScalar(R(10)), |
| 653 | subFilter.get())); |
| 654 | break; |
| 655 | } |
| 656 | case SPOT_LIGHT: { |
| 657 | sk_sp<SkImageFilter> subFilter(make_image_filter()); |
| 658 | |
| 659 | filter = (R(2) == 1) |
| 660 | ? sk_sp<SkImageFilter>(SkLightingImageFilter::CreateSpotLitDiffuse(SkPoint3::Make(0, 0, 0), |
| 661 | make_point(), make_scalar(), make_scalar(), |
| 662 | make_color(), make_scalar(), make_scalar(), |
| 663 | subFilter.get())) |
| 664 | : sk_sp<SkImageFilter>(SkLightingImageFilter::CreateSpotLitSpecular(SkPoint3::Make(0, 0, 0), |
| 665 | make_point(), make_scalar(), make_scalar(), |
| 666 | make_color(), make_scalar(), make_scalar(), |
| 667 | SkIntToScalar(R(10)), subFilter.get())); |
| 668 | break; |
| 669 | } |
| 670 | case NOISE: { |
| 671 | sk_sp<SkShader> shader((R(2) == 1) ? |
| 672 | SkPerlinNoiseShader::MakeFractalNoise( |
| 673 | make_scalar(true), make_scalar(true), R(10.0f), make_scalar()) : |
| 674 | SkPerlinNoiseShader::MakeTurbulence( |
| 675 | make_scalar(true), make_scalar(true), R(10.0f), make_scalar())); |
| 676 | SkPaint paint; |
| 677 | paint.setShader(shader); |
| 678 | SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 679 | SkIntToScalar(kBitmapSize))); |
| 680 | filter = SkPaintImageFilter::Make(paint, &cropR); |
| 681 | break; |
| 682 | } |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame^] | 683 | case DROP_SHADOW: |
| 684 | filter = SkDropShadowImageFilter::Make(make_scalar(), |
| 685 | make_scalar(), |
| 686 | make_scalar(true), |
| 687 | make_scalar(true), |
| 688 | make_color(), |
| 689 | make_shadow_mode(), |
| 690 | make_image_filter(), |
| 691 | nullptr); |
kjlubick | 4319593 | 2016-04-05 12:48:47 -0700 | [diff] [blame] | 692 | break; |
kjlubick | 4319593 | 2016-04-05 12:48:47 -0700 | [diff] [blame] | 693 | case MORPHOLOGY: |
| 694 | if (R(2) == 1) { |
| 695 | filter = SkDilateImageFilter::Make(R(static_cast<float>(kBitmapSize)), |
| 696 | R(static_cast<float>(kBitmapSize)), |
| 697 | make_image_filter()); |
| 698 | } else { |
| 699 | filter = SkErodeImageFilter::Make(R(static_cast<float>(kBitmapSize)), |
| 700 | R(static_cast<float>(kBitmapSize)), |
| 701 | make_image_filter()); |
| 702 | } |
| 703 | break; |
| 704 | case BITMAP: { |
| 705 | sk_sp<SkImage> image(SkImage::MakeFromBitmap(make_bitmap())); |
| 706 | if (R(2) == 1) { |
| 707 | filter = SkImageSource::Make(std::move(image), |
| 708 | make_rect(), |
| 709 | make_rect(), |
| 710 | kHigh_SkFilterQuality); |
| 711 | } else { |
| 712 | filter = SkImageSource::Make(std::move(image)); |
| 713 | } |
| 714 | break; |
| 715 | } |
| 716 | case DISPLACE: { |
| 717 | sk_sp<SkImageFilter> subFilter1(make_image_filter(false)); |
| 718 | sk_sp<SkImageFilter> subFilter2(make_image_filter()); |
| 719 | |
| 720 | filter = sk_sp<SkImageFilter>(SkDisplacementMapEffect::Create(make_channel_selector_type(), |
| 721 | make_channel_selector_type(), |
| 722 | make_scalar(), |
| 723 | subFilter1.get(), |
| 724 | subFilter2.get())); |
| 725 | break; |
| 726 | } |
| 727 | case TILE: { |
| 728 | sk_sp<SkImageFilter> subFilter(make_image_filter(false)); |
| 729 | |
| 730 | filter = sk_sp<SkImageFilter>(SkTileImageFilter::Create(make_rect(), |
| 731 | make_rect(), |
| 732 | subFilter.get())); |
| 733 | break; |
| 734 | } |
| 735 | case PICTURE: { |
| 736 | SkRTreeFactory factory; |
| 737 | SkPictureRecorder recorder; |
| 738 | SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize), |
| 739 | SkIntToScalar(kBitmapSize), |
| 740 | &factory, 0); |
| 741 | drawSomething(recordingCanvas); |
| 742 | sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
| 743 | filter = SkPictureImageFilter::Make(pict, make_rect()); |
| 744 | break; |
| 745 | } |
| 746 | case PAINT: { |
| 747 | SkImageFilter::CropRect cropR(make_rect()); |
| 748 | filter = SkPaintImageFilter::Make(make_paint(), &cropR); |
| 749 | break; |
| 750 | } |
| 751 | default: |
| 752 | break; |
| 753 | } |
| 754 | return (filter || canBeNull) ? filter : make_image_filter(canBeNull); |
| 755 | } |
| 756 | |
| 757 | static SkImageFilter* make_serialized_image_filter() { |
| 758 | sk_sp<SkImageFilter> filter(make_image_filter(false)); |
| 759 | SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter.get())); |
| 760 | const unsigned char* ptr = static_cast<const unsigned char*>(data->data()); |
| 761 | size_t len = data->size(); |
| 762 | #ifdef SK_ADD_RANDOM_BIT_FLIPS |
| 763 | unsigned char* p = const_cast<unsigned char*>(ptr); |
| 764 | for (size_t i = 0; i < len; ++i, ++p) { |
| 765 | if (R(250) == 1) { // 0.4% of the time, flip a bit or byte |
| 766 | if (R(10) == 1) { // Then 10% of the time, change a whole byte |
| 767 | switch(R(3)) { |
| 768 | case 0: |
| 769 | *p ^= 0xFF; // Flip entire byte |
| 770 | break; |
| 771 | case 1: |
| 772 | *p = 0xFF; // Set all bits to 1 |
| 773 | break; |
| 774 | case 2: |
| 775 | *p = 0x00; // Set all bits to 0 |
| 776 | break; |
| 777 | } |
| 778 | } else { |
| 779 | *p ^= (1 << R(8)); |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | #endif // SK_ADD_RANDOM_BIT_FLIPS |
| 784 | SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len, |
| 785 | SkImageFilter::GetFlattenableType()); |
| 786 | return static_cast<SkImageFilter*>(flattenable); |
| 787 | } |
| 788 | |
| 789 | static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 790 | canvas->save(); |
| 791 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 792 | SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize))); |
| 793 | canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 794 | canvas->restore(); |
| 795 | } |
| 796 | |
| 797 | DEF_FUZZ(SerializedImageFilter, f) { |
| 798 | fuzz = f; |
| 799 | SkImageFilter* filter = make_serialized_image_filter(); |
| 800 | |
| 801 | SkPaint paint; |
| 802 | SkSafeUnref(paint.setImageFilter(filter)); |
| 803 | SkBitmap bitmap; |
| 804 | SkCanvas canvas(bitmap); |
| 805 | drawClippedBitmap(&canvas, 0, 0, paint); |
| 806 | } |