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