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@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 8 | #include "SkBlurMask.h" |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 9 | #include "SkBlurMaskFilter.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 11 | #include "SkColorMatrixFilter.h" |
| 12 | #include "SkDiscretePathEffect.h" |
| 13 | #include "SkGradientShader.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
| 15 | #include "SkView.h" |
| 16 | |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 17 | |
| 18 | //#define COLOR 0xFFFF8844 |
| 19 | #define COLOR 0xFF888888 |
| 20 | |
sugoi@google.com | 93c7ee3 | 2013-03-12 14:36:57 +0000 | [diff] [blame] | 21 | static void paint_proc0(SkPaint*) { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static void paint_proc1(SkPaint* paint) { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 25 | paint->setMaskFilter(SkBlurMaskFilter::Make( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 26 | kNormal_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 27 | SkBlurMask::ConvertRadiusToSigma(2))); |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static void paint_proc2(SkPaint* paint) { |
| 31 | SkScalar dir[3] = { 1, 1, 1}; |
| 32 | paint->setMaskFilter( |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 33 | SkBlurMaskFilter::MakeEmboss(SkBlurMask::ConvertRadiusToSigma(1), dir, 0.1f, 0.05f)); |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static void paint_proc3(SkPaint* paint) { |
| 37 | SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE }; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 38 | SkPoint pts[] = { { 3, 0 }, { 7, 5 } }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 39 | paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 40 | SkShader::kMirror_TileMode)); |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void paint_proc5(SkPaint* paint) { |
| 44 | paint_proc3(paint); |
| 45 | paint_proc2(paint); |
| 46 | } |
| 47 | |
| 48 | typedef void (*PaintProc)(SkPaint*); |
| 49 | const PaintProc gPaintProcs[] = { |
| 50 | paint_proc0, |
| 51 | paint_proc1, |
| 52 | paint_proc2, |
| 53 | paint_proc3, |
| 54 | paint_proc5, |
| 55 | }; |
| 56 | |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 59 | class EffectsView : public SampleView { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 60 | public: |
| 61 | SkPath fPath; |
| 62 | SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)]; |
| 63 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | EffectsView() { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 65 | size_t i; |
| 66 | const float pts[] = { |
| 67 | 0, 0, |
| 68 | 10, 0, |
| 69 | 10, 5, |
| 70 | 20, -5, |
| 71 | 10, -15, |
| 72 | 10, -10, |
| 73 | 0, -10 |
| 74 | }; |
| 75 | fPath.moveTo(pts[0], pts[1]); |
| 76 | for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) { |
| 77 | fPath.lineTo(pts[i], pts[i+1]); |
| 78 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 79 | |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 80 | for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) { |
| 81 | fPaint[i].setAntiAlias(true); |
| 82 | fPaint[i].setColor(COLOR); |
| 83 | gPaintProcs[i](&fPaint[i]); |
| 84 | } |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 85 | |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 86 | SkColorMatrix cm; |
| 87 | cm.setRotate(SkColorMatrix::kG_Axis, 180); |
| 88 | cm.setIdentity(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 90 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 91 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 93 | protected: |
| 94 | // overrides from SkEventSink |
| 95 | virtual bool onQuery(SkEvent* evt) { |
| 96 | if (SampleCode::TitleQ(*evt)) { |
| 97 | SampleCode::TitleR(evt, "Effects"); |
| 98 | return true; |
| 99 | } |
| 100 | return this->INHERITED::onQuery(evt); |
| 101 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 103 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 104 | canvas->scale(3, 3); |
| 105 | canvas->translate(10, 30); |
| 106 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) { |
| 107 | canvas->drawPath(fPath, fPaint[i]); |
| 108 | canvas->translate(32, 0); |
| 109 | } |
| 110 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 112 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 113 | typedef SampleView INHERITED; |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /////////////////////////////////////////////////////////////////////////////// |
| 117 | |
| 118 | static SkView* MyFactory() { return new EffectsView; } |
| 119 | static SkViewRegister reg(MyFactory); |