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