senorblanco | d4bb991 | 2015-03-20 08:54:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 8 | #include "sk_tool_utils.h" |
| 9 | #include "SkBitmapSource.h" |
| 10 | #include "SkBlurImageFilter.h" |
| 11 | #include "SkColor.h" |
| 12 | #include "SkDisplacementMapEffect.h" |
| 13 | #include "SkDropShadowImageFilter.h" |
| 14 | #include "SkGradientShader.h" |
| 15 | #include "SkMorphologyImageFilter.h" |
| 16 | #include "SkScalar.h" |
| 17 | #include "gm.h" |
| 18 | |
| 19 | namespace skiagm { |
| 20 | |
| 21 | // This GM draws image filters with a CTM containing shearing / rotation. |
| 22 | // It checks that the scale portion of the CTM is correctly extracted |
| 23 | // and applied to the image inputs separately from the non-scale portion. |
| 24 | |
| 25 | class ImageFiltersTransformedGM : public GM { |
| 26 | public: |
| 27 | ImageFiltersTransformedGM() { |
| 28 | this->setBGColor(SK_ColorBLACK); |
| 29 | } |
| 30 | |
| 31 | protected: |
| 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 33 | SkString onShortName() override { return SkString("imagefilterstransformed"); } |
senorblanco | d4bb991 | 2015-03-20 08:54:32 -0700 | [diff] [blame] | 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 35 | SkISize onISize() override { return SkISize::Make(420, 240); } |
senorblanco | d4bb991 | 2015-03-20 08:54:32 -0700 | [diff] [blame] | 36 | |
| 37 | void makeGradientCircle(int width, int height) { |
| 38 | SkScalar x = SkIntToScalar(width / 2); |
| 39 | SkScalar y = SkIntToScalar(height / 2); |
| 40 | SkScalar radius = SkMinScalar(x, y) * 0.8f; |
| 41 | fGradientCircle.allocN32Pixels(width, height); |
| 42 | SkCanvas canvas(fGradientCircle); |
| 43 | canvas.clear(0x00000000); |
| 44 | SkColor colors[2]; |
| 45 | colors[0] = SK_ColorWHITE; |
| 46 | colors[1] = SK_ColorBLACK; |
| 47 | SkAutoTUnref<SkShader> shader( |
| 48 | SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2, |
| 49 | SkShader::kClamp_TileMode) |
| 50 | ); |
| 51 | SkPaint paint; |
| 52 | paint.setShader(shader); |
| 53 | canvas.drawCircle(x, y, radius, paint); |
| 54 | } |
| 55 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 56 | void onOnceBeforeDraw() override { |
senorblanco | d4bb991 | 2015-03-20 08:54:32 -0700 | [diff] [blame] | 57 | fCheckerboard.allocN32Pixels(64, 64); |
| 58 | SkCanvas checkerboardCanvas(fCheckerboard); |
| 59 | sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8); |
| 60 | |
| 61 | this->makeGradientCircle(64, 64); |
| 62 | } |
| 63 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 64 | void onDraw(SkCanvas* canvas) override { |
senorblanco | d4bb991 | 2015-03-20 08:54:32 -0700 | [diff] [blame] | 65 | SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCircle)); |
| 66 | SkAutoTUnref<SkImageFilter> checkerboard(SkBitmapSource::Create(fCheckerboard)); |
| 67 | SkImageFilter* filters[] = { |
| 68 | SkBlurImageFilter::Create(12, 0), |
| 69 | SkDropShadowImageFilter::Create(0, 15, 8, 0, SK_ColorGREEN, |
| 70 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
| 71 | SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 72 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 73 | 12, |
| 74 | gradient.get(), |
| 75 | checkerboard.get()), |
| 76 | SkDilateImageFilter::Create(2, 2, checkerboard.get()), |
| 77 | SkErodeImageFilter::Create(2, 2, checkerboard.get()), |
| 78 | }; |
| 79 | |
| 80 | const SkScalar margin = SkIntToScalar(20); |
| 81 | const SkScalar size = SkIntToScalar(60); |
| 82 | |
| 83 | for (size_t j = 0; j < 3; j++) { |
| 84 | canvas->save(); |
| 85 | canvas->translate(margin, 0); |
| 86 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 87 | SkPaint paint; |
| 88 | paint.setColor(SK_ColorWHITE); |
| 89 | paint.setImageFilter(filters[i]); |
| 90 | paint.setAntiAlias(true); |
| 91 | canvas->save(); |
| 92 | canvas->translate(size * SK_ScalarHalf, size * SK_ScalarHalf); |
| 93 | canvas->scale(SkDoubleToScalar(0.8), SkDoubleToScalar(0.8)); |
| 94 | if (j == 1) { |
| 95 | canvas->rotate(SkIntToScalar(45)); |
| 96 | } else if (j == 2) { |
| 97 | canvas->skew(SkDoubleToScalar(0.5), SkDoubleToScalar(0.2)); |
| 98 | } |
| 99 | canvas->translate(-size * SK_ScalarHalf, -size * SK_ScalarHalf); |
| 100 | canvas->drawOval(SkRect::MakeXYWH(0, size * SkDoubleToScalar(0.1), |
| 101 | size, size * SkDoubleToScalar(0.6)), paint); |
| 102 | canvas->restore(); |
| 103 | canvas->translate(size + margin, 0); |
| 104 | } |
| 105 | canvas->restore(); |
| 106 | canvas->translate(0, size + margin); |
| 107 | } |
| 108 | |
| 109 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 110 | SkSafeUnref(filters[i]); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | SkBitmap fCheckerboard; |
| 116 | SkBitmap fGradientCircle; |
| 117 | typedef GM INHERITED; |
| 118 | }; |
| 119 | |
| 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | DEF_GM( return new ImageFiltersTransformedGM; ) |
| 123 | |
| 124 | } |