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: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | DrawLooperGM() : fLooper(NULL) { |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 22 | this->setBGColor(0xFFDDDDDD); |
| 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 | virtual ~DrawLooperGM() { |
| 26 | SkSafeUnref(fLooper); |
| 27 | } |
| 28 | |
| 29 | protected: |
rmistry | 465206a | 2015-02-02 12:08:18 -0800 | [diff] [blame] | 30 | virtual SkISize onISize() SK_OVERRIDE { |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 31 | return SkISize::Make(520, 160); |
| 32 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 33 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 34 | SkString onShortName() SK_OVERRIDE { |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 35 | return SkString("drawlooper"); |
| 36 | } |
| 37 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 38 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 39 | this->init(); |
| 40 | |
| 41 | SkPaint paint; |
reed@google.com | 48f31bd | 2014-02-27 14:27:44 +0000 | [diff] [blame] | 42 | paint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 43 | sk_tool_utils::set_portable_typeface(&paint); |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 44 | paint.setTextSize(SkIntToScalar(72)); |
| 45 | paint.setLooper(fLooper); |
| 46 | |
| 47 | canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 48 | SkIntToScalar(30), paint); |
| 49 | |
| 50 | canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50), |
| 51 | SkIntToScalar(200), SkIntToScalar(100), paint); |
| 52 | |
| 53 | canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100), |
| 54 | paint); |
| 55 | } |
| 56 | |
| 57 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | SkLayerDrawLooper* fLooper; |
| 59 | |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 60 | void init() { |
| 61 | if (fLooper) return; |
| 62 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | static const struct { |
| 64 | SkColor fColor; |
| 65 | SkPaint::Style fStyle; |
| 66 | SkScalar fWidth; |
| 67 | SkScalar fOffset; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 68 | SkScalar fBlur; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | } gParams[] = { |
| 70 | { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 }, |
| 71 | { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 }, |
| 72 | { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 }, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 73 | { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 76 | SkLayerDrawLooper::Builder looperBuilder; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 77 | |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 78 | SkLayerDrawLooper::LayerInfo info; |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 79 | info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit; |
| 80 | info.fColorMode = SkXfermode::kSrc_Mode; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 81 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 82 | for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) { |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 83 | info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 84 | SkPaint* paint = looperBuilder.addLayer(info); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | paint->setColor(gParams[i].fColor); |
| 86 | paint->setStyle(gParams[i].fStyle); |
| 87 | paint->setStrokeWidth(gParams[i].fWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | if (gParams[i].fBlur > 0) { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 89 | SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 90 | SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | paint->setMaskFilter(mf)->unref(); |
| 92 | } |
| 93 | } |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 94 | fLooper = looperBuilder.detachLooper(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 96 | |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 97 | typedef GM INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | ////////////////////////////////////////////////////////////////////////////// |
| 101 | |
reed@google.com | 076f4c9 | 2012-07-31 14:32:38 +0000 | [diff] [blame] | 102 | static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; } |
| 103 | static skiagm::GMRegistry reg(MyFactory); |