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" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 11 | #include "SkView.h" |
| 12 | |
| 13 | #include "SkBlurMaskFilter.h" |
| 14 | #include "SkColorMatrixFilter.h" |
| 15 | #include "SkDiscretePathEffect.h" |
| 16 | #include "SkGradientShader.h" |
| 17 | |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 18 | #include "SkEdgeClipper.h" |
| 19 | |
| 20 | static void test_edgeclipper() { |
| 21 | SkPoint pts[] = { |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 22 | { -8.38822452e+21f, -7.69721471e+19f }, |
| 23 | { 1.57645875e+23f, 1.44634003e+21f }, |
| 24 | { 1.61519691e+23f, 1.48208059e+21f }, |
| 25 | { 3.13963584e+23f, 2.88057438e+21f } |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 26 | }; |
| 27 | SkRect clip = { 0, 0, 300, 200 }; |
| 28 | |
| 29 | SkEdgeClipper clipper; |
| 30 | clipper.clipCubic(pts, clip); |
| 31 | } |
| 32 | |
| 33 | /////////// |
| 34 | |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 35 | //#define COLOR 0xFFFF8844 |
| 36 | #define COLOR 0xFF888888 |
| 37 | |
| 38 | static void paint_proc0(SkPaint* paint) { |
| 39 | } |
| 40 | |
| 41 | static void paint_proc1(SkPaint* paint) { |
| 42 | paint->setMaskFilter(SkBlurMaskFilter::Create(2, |
| 43 | SkBlurMaskFilter::kNormal_BlurStyle))->unref(); |
| 44 | } |
| 45 | |
| 46 | static void paint_proc2(SkPaint* paint) { |
| 47 | SkScalar dir[3] = { 1, 1, 1}; |
| 48 | paint->setMaskFilter( |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 49 | SkBlurMaskFilter::CreateEmboss(dir, 0.1f, 0.05f, 1))->unref(); |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void paint_proc3(SkPaint* paint) { |
| 53 | SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE }; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 54 | SkPoint pts[] = { { 3, 0 }, { 7, 5 } }; |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 55 | paint->setShader(SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), |
| 56 | SkShader::kMirror_TileMode))->unref(); |
| 57 | } |
| 58 | |
| 59 | static void paint_proc5(SkPaint* paint) { |
| 60 | paint_proc3(paint); |
| 61 | paint_proc2(paint); |
| 62 | } |
| 63 | |
| 64 | typedef void (*PaintProc)(SkPaint*); |
| 65 | const PaintProc gPaintProcs[] = { |
| 66 | paint_proc0, |
| 67 | paint_proc1, |
| 68 | paint_proc2, |
| 69 | paint_proc3, |
| 70 | paint_proc5, |
| 71 | }; |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 75 | class EffectsView : public SampleView { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 76 | public: |
| 77 | SkPath fPath; |
| 78 | SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)]; |
| 79 | |
| 80 | EffectsView() { |
| 81 | size_t i; |
| 82 | const float pts[] = { |
| 83 | 0, 0, |
| 84 | 10, 0, |
| 85 | 10, 5, |
| 86 | 20, -5, |
| 87 | 10, -15, |
| 88 | 10, -10, |
| 89 | 0, -10 |
| 90 | }; |
| 91 | fPath.moveTo(pts[0], pts[1]); |
| 92 | for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) { |
| 93 | fPath.lineTo(pts[i], pts[i+1]); |
| 94 | } |
| 95 | |
| 96 | for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) { |
| 97 | fPaint[i].setAntiAlias(true); |
| 98 | fPaint[i].setColor(COLOR); |
| 99 | gPaintProcs[i](&fPaint[i]); |
| 100 | } |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 101 | |
| 102 | test_edgeclipper(); |
| 103 | SkColorMatrix cm; |
| 104 | cm.setRotate(SkColorMatrix::kG_Axis, 180); |
| 105 | cm.setIdentity(); |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 106 | |
| 107 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | protected: |
| 111 | // overrides from SkEventSink |
| 112 | virtual bool onQuery(SkEvent* evt) { |
| 113 | if (SampleCode::TitleQ(*evt)) { |
| 114 | SampleCode::TitleR(evt, "Effects"); |
| 115 | return true; |
| 116 | } |
| 117 | return this->INHERITED::onQuery(evt); |
| 118 | } |
| 119 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 120 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 121 | canvas->scale(3, 3); |
| 122 | canvas->translate(10, 30); |
| 123 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) { |
| 124 | canvas->drawPath(fPath, fPaint[i]); |
| 125 | canvas->translate(32, 0); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 130 | typedef SampleView INHERITED; |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | static SkView* MyFactory() { return new EffectsView; } |
| 136 | static SkViewRegister reg(MyFactory); |