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