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