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