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 | */ |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 7 | |
| 8 | #include "gm.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
| 10 | #include "SkBlurMaskFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkGraphics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkLayerDrawLooper.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 14 | #include "SkRandom.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #define WIDTH 200 |
| 17 | #define HEIGHT 200 |
| 18 | |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 19 | class DrawLooperGM : public skiagm::GM { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | DrawLooperGM() : fLooper(nullptr) { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 22 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 23 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 25 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | virtual SkISize onISize() override { |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 27 | return SkISize::Make(520, 160); |
| 28 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 31 | return SkString("drawlooper"); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 35 | this->init(); |
| 36 | |
| 37 | SkPaint paint; |
reed@google.com | 48f31bd | 2014-02-27 14:27:44 +0000 | [diff] [blame] | 38 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 39 | sk_tool_utils::set_portable_typeface(&paint); |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 40 | paint.setTextSize(SkIntToScalar(72)); |
| 41 | paint.setLooper(fLooper); |
| 42 | |
| 43 | canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 44 | SkIntToScalar(30), paint); |
| 45 | |
| 46 | canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50), |
| 47 | SkIntToScalar(200), SkIntToScalar(100), paint); |
| 48 | |
| 49 | canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100), |
| 50 | paint); |
| 51 | } |
| 52 | |
| 53 | private: |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 54 | sk_sp<SkDrawLooper> fLooper; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 56 | void init() { |
| 57 | if (fLooper) return; |
| 58 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | static const struct { |
| 60 | SkColor fColor; |
| 61 | SkPaint::Style fStyle; |
| 62 | SkScalar fWidth; |
| 63 | SkScalar fOffset; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 64 | SkScalar fBlur; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | } gParams[] = { |
| 66 | { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 }, |
| 67 | { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 }, |
| 68 | { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 }, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 69 | { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 72 | SkLayerDrawLooper::Builder looperBuilder; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 73 | |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 74 | SkLayerDrawLooper::LayerInfo info; |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 75 | info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit; |
| 76 | info.fColorMode = SkXfermode::kSrc_Mode; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 77 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 78 | for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) { |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 79 | info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 80 | SkPaint* paint = looperBuilder.addLayer(info); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | paint->setColor(gParams[i].fColor); |
| 82 | paint->setStyle(gParams[i].fStyle); |
| 83 | paint->setStrokeWidth(gParams[i].fWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | if (gParams[i].fBlur > 0) { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 85 | paint->setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| 86 | SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur))); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 89 | fLooper = looperBuilder.detach(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 91 | |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 92 | typedef GM INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 97 | DEF_GM( return new DrawLooperGM; ) |