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