senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 9 | #include "SkBitmapSource.h" |
| 10 | #include "SkBlurImageFilter.h" |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 11 | #include "SkColor.h" |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 12 | #include "SkDisplacementMapEffect.h" |
| 13 | #include "SkDropShadowImageFilter.h" |
| 14 | #include "SkGradientShader.h" |
| 15 | #include "SkMorphologyImageFilter.h" |
| 16 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | 47d98c8 | 2014-03-03 14:58:09 +0000 | [diff] [blame] | 17 | #include "SkPerlinNoiseShader.h" |
| 18 | #include "SkRectShaderImageFilter.h" |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 19 | #include "SkScalar.h" |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 20 | #include "gm.h" |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 21 | |
senorblanco@chromium.org | 897b73f | 2014-02-19 22:10:12 +0000 | [diff] [blame] | 22 | #define RESIZE_FACTOR_X SkIntToScalar(2) |
| 23 | #define RESIZE_FACTOR_Y SkIntToScalar(5) |
| 24 | |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 25 | namespace skiagm { |
| 26 | |
| 27 | class ImageFiltersClippedGM : public GM { |
| 28 | public: |
| 29 | ImageFiltersClippedGM() : fInitialized(false) { |
| 30 | this->setBGColor(0x00000000); |
| 31 | } |
| 32 | |
| 33 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | SkString onShortName() override { |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 36 | return SkString("imagefiltersclipped"); |
| 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 40 | return SkISize::Make(860, 500); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 41 | } |
| 42 | |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 43 | void make_gradient_circle(int width, int height) { |
| 44 | SkScalar x = SkIntToScalar(width / 2); |
| 45 | SkScalar y = SkIntToScalar(height / 2); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 46 | SkScalar radius = SkMinScalar(x, y) * 0.8f; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 47 | fGradientCircle.allocN32Pixels(width, height); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 48 | SkCanvas canvas(fGradientCircle); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 49 | canvas.clear(0x00000000); |
| 50 | SkColor colors[2]; |
| 51 | colors[0] = SK_ColorWHITE; |
| 52 | colors[1] = SK_ColorBLACK; |
| 53 | SkAutoTUnref<SkShader> shader( |
| 54 | SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2, |
| 55 | SkShader::kClamp_TileMode) |
| 56 | ); |
| 57 | SkPaint paint; |
| 58 | paint.setShader(shader); |
| 59 | canvas.drawCircle(x, y, radius, paint); |
| 60 | } |
| 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | void onDraw(SkCanvas* canvas) override { |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 63 | if (!fInitialized) { |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 64 | fCheckerboard.allocN32Pixels(64, 64); |
| 65 | SkCanvas checkerboardCanvas(fCheckerboard); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 66 | sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8); |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 67 | |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 68 | this->make_gradient_circle(64, 64); |
| 69 | fInitialized = true; |
| 70 | } |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame^] | 71 | canvas->clear(SK_ColorBLACK); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 72 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 73 | SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCircle)); |
| 74 | SkAutoTUnref<SkImageFilter> checkerboard(SkBitmapSource::Create(fCheckerboard)); |
senorblanco@chromium.org | 47d98c8 | 2014-03-03 14:58:09 +0000 | [diff] [blame] | 75 | SkAutoTUnref<SkShader> noise(SkPerlinNoiseShader::CreateFractalNoise( |
| 76 | SkDoubleToScalar(0.1), SkDoubleToScalar(0.05), 1, 0)); |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 77 | SkMatrix resizeMatrix; |
| 78 | resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 79 | |
| 80 | SkImageFilter* filters[] = { |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 81 | SkBlurImageFilter::Create(SkIntToScalar(12), SkIntToScalar(12)), |
senorblanco | bc9845b | 2014-07-22 12:49:25 -0700 | [diff] [blame] | 82 | SkDropShadowImageFilter::Create(SkIntToScalar(10), SkIntToScalar(10), |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 83 | SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN, |
| 84 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 85 | SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 86 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 87 | SkIntToScalar(12), |
| 88 | gradient.get(), |
| 89 | checkerboard.get()), |
| 90 | SkDilateImageFilter::Create(2, 2, checkerboard.get()), |
| 91 | SkErodeImageFilter::Create(2, 2, checkerboard.get()), |
| 92 | SkOffsetImageFilter::Create(SkIntToScalar(-16), SkIntToScalar(32)), |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 93 | SkImageFilter::CreateMatrixFilter(resizeMatrix, kNone_SkFilterQuality), |
senorblanco@chromium.org | 47d98c8 | 2014-03-03 14:58:09 +0000 | [diff] [blame] | 94 | SkRectShaderImageFilter::Create(noise), |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
| 98 | SkScalar margin = SkIntToScalar(16); |
| 99 | SkRect bounds = r; |
| 100 | bounds.outset(margin, margin); |
| 101 | |
| 102 | for (int xOffset = 0; xOffset < 80; xOffset += 16) { |
| 103 | canvas->save(); |
| 104 | bounds.fLeft = SkIntToScalar(xOffset); |
| 105 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 106 | SkPaint paint; |
| 107 | paint.setColor(SK_ColorWHITE); |
| 108 | paint.setImageFilter(filters[i]); |
| 109 | paint.setAntiAlias(true); |
| 110 | canvas->save(); |
| 111 | canvas->clipRect(bounds); |
senorblanco@chromium.org | 897b73f | 2014-02-19 22:10:12 +0000 | [diff] [blame] | 112 | if (5 == i) { |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 113 | canvas->translate(SkIntToScalar(16), SkIntToScalar(-32)); |
senorblanco@chromium.org | 897b73f | 2014-02-19 22:10:12 +0000 | [diff] [blame] | 114 | } else if (6 == i) { |
| 115 | canvas->scale(SkScalarInvert(RESIZE_FACTOR_X), |
| 116 | SkScalarInvert(RESIZE_FACTOR_Y)); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 117 | } |
| 118 | canvas->drawCircle(r.centerX(), r.centerY(), |
| 119 | SkScalarDiv(r.width()*2, SkIntToScalar(5)), paint); |
| 120 | canvas->restore(); |
| 121 | canvas->translate(r.width() + margin, 0); |
| 122 | } |
| 123 | canvas->restore(); |
| 124 | canvas->translate(0, r.height() + margin); |
| 125 | } |
senorblanco@chromium.org | 6711780 | 2014-02-06 00:36:01 +0000 | [diff] [blame] | 126 | |
| 127 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 128 | SkSafeUnref(filters[i]); |
| 129 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private: |
| 133 | bool fInitialized; |
| 134 | SkBitmap fCheckerboard; |
| 135 | SkBitmap fGradientCircle; |
| 136 | typedef GM INHERITED; |
| 137 | }; |
| 138 | |
| 139 | ////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | static GM* MyFactory(void*) { return new ImageFiltersClippedGM; } |
| 142 | static GMRegistry reg(MyFactory); |
| 143 | |
| 144 | } |