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