epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 7 | |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 8 | #include "gm.h" |
reed | fb8c1fc | 2015-08-04 18:44:56 -0700 | [diff] [blame] | 9 | #include "SkBlurDrawLooper.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 10 | #include "SkBlurMask.h" |
mtklein | 7a1f45f | 2016-08-04 06:19:33 -0700 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
| 12 | #include "SkMaskFilter.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 13 | #include "SkPath.h" |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 19 | static void setup(SkPaint* paint, SkColor c, SkScalar strokeWidth) { |
| 20 | paint->setColor(c); |
| 21 | if (strokeWidth < 0) { |
| 22 | paint->setStyle(SkPaint::kFill_Style); |
| 23 | } else { |
| 24 | paint->setStyle(SkPaint::kStroke_Style); |
| 25 | paint->setStrokeWidth(strokeWidth); |
| 26 | } |
| 27 | } |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 28 | |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 29 | class ShadowsGM : public GM { |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 30 | public: |
| 31 | SkPath fCirclePath; |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 32 | SkRect fRect; |
caryclark | 500ab75 | 2015-09-04 05:20:32 -0700 | [diff] [blame] | 33 | SkBitmap fBitmap; |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 34 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 35 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 36 | void onOnceBeforeDraw() override { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 37 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 38 | fCirclePath.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(10) ); |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 39 | fRect.set(SkIntToScalar(10), SkIntToScalar(10), |
| 40 | SkIntToScalar(30), SkIntToScalar(30)); |
caryclark | 500ab75 | 2015-09-04 05:20:32 -0700 | [diff] [blame] | 41 | fBitmap.allocPixels(SkImageInfo::Make(20, 20, SkColorType::kAlpha_8_SkColorType, |
| 42 | kPremul_SkAlphaType)); |
| 43 | SkCanvas canvas(fBitmap); |
| 44 | canvas.clear(0x0); |
| 45 | SkPaint p; |
| 46 | canvas.drawRect(SkRect::MakeXYWH(10, 0, 10, 10), p); |
| 47 | canvas.drawRect(SkRect::MakeXYWH(0, 10, 10, 10), p); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 48 | } |
| 49 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 50 | SkString onShortName() override { |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 51 | return SkString("shadows"); |
| 52 | } |
| 53 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 54 | SkISize onISize() override { |
caryclark | c8b350d | 2015-09-04 06:19:48 -0700 | [diff] [blame] | 55 | return SkISize::Make(200, 200); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 56 | } |
| 57 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 58 | void onDraw(SkCanvas* canvas) override { |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 59 | sk_sp<SkDrawLooper> shadowLoopers[] = { |
| 60 | SkBlurDrawLooper::Make(SK_ColorBLUE, |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 61 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)), |
| 62 | SkIntToScalar(5), SkIntToScalar(10), |
| 63 | SkBlurDrawLooper::kIgnoreTransform_BlurFlag | |
| 64 | SkBlurDrawLooper::kOverrideColor_BlurFlag | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 65 | SkBlurDrawLooper::kHighQuality_BlurFlag), |
| 66 | SkBlurDrawLooper::Make(SK_ColorBLUE, |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 67 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)), |
| 68 | SkIntToScalar(5), SkIntToScalar(10), |
| 69 | SkBlurDrawLooper::kIgnoreTransform_BlurFlag | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 70 | SkBlurDrawLooper::kOverrideColor_BlurFlag), |
| 71 | SkBlurDrawLooper::Make(SK_ColorBLACK, |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 72 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
| 73 | SkIntToScalar(5), |
| 74 | SkIntToScalar(10), |
| 75 | SkBlurDrawLooper::kIgnoreTransform_BlurFlag | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 76 | SkBlurDrawLooper::kHighQuality_BlurFlag), |
| 77 | SkBlurDrawLooper::Make(0x7FFF0000, |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 78 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
| 79 | SkIntToScalar(-5), SkIntToScalar(-10), |
| 80 | SkBlurDrawLooper::kIgnoreTransform_BlurFlag | |
| 81 | SkBlurDrawLooper::kOverrideColor_BlurFlag | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 82 | SkBlurDrawLooper::kHighQuality_BlurFlag), |
| 83 | SkBlurDrawLooper::Make(SK_ColorBLACK, SkIntToScalar(0), |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 84 | SkIntToScalar(5), SkIntToScalar(5), |
| 85 | SkBlurDrawLooper::kIgnoreTransform_BlurFlag | |
| 86 | SkBlurDrawLooper::kOverrideColor_BlurFlag | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 87 | SkBlurDrawLooper::kHighQuality_BlurFlag), |
| 88 | }; |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 89 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 90 | constexpr struct { |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 91 | SkColor fColor; |
| 92 | SkScalar fStrokeWidth; |
| 93 | } gRec[] = { |
| 94 | { SK_ColorRED, -SK_Scalar1 }, |
| 95 | { SK_ColorGREEN, SkIntToScalar(4) }, |
| 96 | { SK_ColorBLUE, SkIntToScalar(0)}, |
| 97 | }; |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 98 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 99 | SkPaint paint; |
| 100 | paint.setAntiAlias(true); |
| 101 | for (size_t i = 0; i < SK_ARRAY_COUNT(shadowLoopers); ++i) { |
| 102 | SkAutoCanvasRestore acr(canvas, true); |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 103 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 104 | paint.setLooper(shadowLoopers[i]); |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 105 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 106 | canvas->translate(SkIntToScalar((unsigned int)i*40), SkIntToScalar(0)); |
| 107 | setup(&paint, gRec[0].fColor, gRec[0].fStrokeWidth); |
| 108 | canvas->drawRect(fRect, paint); |
reed@google.com | e3605d0 | 2011-05-26 15:12:35 +0000 | [diff] [blame] | 109 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 110 | canvas->translate(SkIntToScalar(0), SkIntToScalar(40)); |
| 111 | setup(&paint, gRec[1].fColor, gRec[1].fStrokeWidth); |
| 112 | canvas->drawPath(fCirclePath, paint); |
junov@chromium.org | 7c545bd | 2012-04-05 13:20:59 +0000 | [diff] [blame] | 113 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 114 | canvas->translate(SkIntToScalar(0), SkIntToScalar(40)); |
| 115 | setup(&paint, gRec[2].fColor, gRec[2].fStrokeWidth); |
| 116 | canvas->drawPath(fCirclePath, paint); |
caryclark | 500ab75 | 2015-09-04 05:20:32 -0700 | [diff] [blame] | 117 | |
| 118 | // see bug.skia.org/562 (reference, draws correct) |
| 119 | canvas->translate(0, 40); |
| 120 | paint.setColor(SK_ColorBLACK); |
| 121 | canvas->drawBitmap(fBitmap, 10, 10, &paint); |
| 122 | |
| 123 | canvas->translate(0, 40); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 124 | paint.setShader(SkShader::MakeBitmapShader( |
caryclark | 500ab75 | 2015-09-04 05:20:32 -0700 | [diff] [blame] | 125 | fBitmap, SkShader::kRepeat_TileMode, |
| 126 | SkShader::kRepeat_TileMode)); |
| 127 | |
| 128 | // see bug.skia.org/562 (shows bug as reported) |
caryclark | 500ab75 | 2015-09-04 05:20:32 -0700 | [diff] [blame] | 129 | paint.setStyle(SkPaint::kFill_Style); |
| 130 | canvas->drawRect(SkRect::MakeXYWH(10, 10, 20, 20), paint); |
| 131 | paint.setShader(nullptr); |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 132 | } |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 133 | } |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 134 | |
| 135 | private: |
| 136 | typedef GM INHERITED; |
| 137 | }; |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | static GM* MyFactory(void*) { return new ShadowsGM; } |
| 142 | static GMRegistry reg(MyFactory); |
| 143 | |
| 144 | } |