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 | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRegion.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "Sk1DPathEffect.h" |
| 17 | #include "SkCornerPathEffect.h" |
| 18 | #include "SkPathMeasure.h" |
| 19 | #include "SkRandom.h" |
| 20 | #include "SkColorPriv.h" |
| 21 | #include "SkPixelXorXfermode.h" |
| 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #define CORNER_RADIUS 12 |
| 24 | static SkScalar gPhase; |
| 25 | |
| 26 | static const int gXY[] = { |
| 27 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 28 | }; |
| 29 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 30 | static SkPathEffect* make_pe(int flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | if (flags == 1) |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 32 | return SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | |
| 34 | SkPath path; |
| 35 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 36 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 37 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 38 | path.close(); |
| 39 | path.offset(SkIntToScalar(-6), 0); |
| 40 | |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 41 | SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 42 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | if (flags == 2) |
| 44 | return outer; |
| 45 | |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 46 | SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 48 | SkPathEffect* pe = SkComposePathEffect::Create(outer, inner); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | outer->unref(); |
| 50 | inner->unref(); |
| 51 | return pe; |
| 52 | } |
| 53 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 54 | static SkPathEffect* make_warp_pe() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | SkPath path; |
| 56 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 57 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 58 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 59 | path.close(); |
| 60 | path.offset(SkIntToScalar(-6), 0); |
| 61 | |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 62 | SkPathEffect* outer = SkPath1DPathEffect::Create( |
| 63 | path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style); |
| 64 | SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 66 | SkPathEffect* pe = SkComposePathEffect::Create(outer, inner); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | outer->unref(); |
| 68 | inner->unref(); |
| 69 | return pe; |
| 70 | } |
| 71 | |
| 72 | /////////////////////////////////////////////////////////// |
| 73 | |
| 74 | #include "SkColorFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | #include "SkLayerRasterizer.h" |
| 76 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 77 | class TestRastBuilder : public SkLayerRasterizer::Builder { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | public: |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 79 | TestRastBuilder() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | SkPaint paint; |
| 81 | paint.setAntiAlias(true); |
| 82 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | #if 0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | paint.setStyle(SkPaint::kStroke_Style); |
| 85 | paint.setStrokeWidth(SK_Scalar1*4); |
| 86 | this->addLayer(paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | paint.setStrokeWidth(SK_Scalar1*1); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 89 | paint.setXfermode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | this->addLayer(paint); |
| 91 | #else |
| 92 | paint.setAlpha(0x66); |
| 93 | this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | paint.setAlpha(0xFF); |
| 96 | this->addLayer(paint); |
| 97 | #endif |
| 98 | } |
| 99 | }; |
| 100 | |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 101 | class PathEffectView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | SkPath fPath; |
| 103 | SkPoint fClickPt; |
| 104 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | PathEffectView() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 106 | SkRandom rand; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | int steps = 20; |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 108 | SkScalar dist = SkIntToScalar(400); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | SkScalar x = SkIntToScalar(20); |
| 110 | SkScalar y = SkIntToScalar(50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | fPath.moveTo(x, y); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 113 | for (int i = 0; i < steps; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | x += dist/steps; |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 115 | SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25); |
| 116 | if (i == steps/2) { |
| 117 | fPath.moveTo(x, tmpY); |
| 118 | } else { |
| 119 | fPath.lineTo(x, tmpY); |
| 120 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | } |
| 122 | |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 123 | { |
| 124 | SkRect oval; |
| 125 | oval.set(SkIntToScalar(20), SkIntToScalar(30), |
| 126 | SkIntToScalar(100), SkIntToScalar(60)); |
| 127 | oval.offset(x, 0); |
| 128 | fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8)); |
| 129 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | fClickPt.set(SkIntToScalar(200), SkIntToScalar(200)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 132 | |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 133 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 135 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | protected: |
| 137 | // overrides from SkEventSink |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 138 | virtual bool onQuery(SkEvent* evt) { |
| 139 | if (SampleCode::TitleQ(*evt)) { |
| 140 | SampleCode::TitleR(evt, "PathEffects"); |
| 141 | return true; |
| 142 | } |
| 143 | return this->INHERITED::onQuery(evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 145 | |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 146 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 147 | gPhase -= SampleCode::GetAnimSecondsDelta() * 40; |
reed@android.com | 671cd65 | 2009-05-22 20:44:12 +0000 | [diff] [blame] | 148 | this->inval(NULL); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 149 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 151 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 152 | #if 0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | paint.setAntiAlias(true); |
| 154 | paint.setStyle(SkPaint::kStroke_Style); |
| 155 | paint.setStrokeWidth(SkIntToScalar(5)); |
| 156 | canvas->drawPath(fPath, paint); |
| 157 | paint.setStrokeWidth(0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 159 | paint.setColor(SK_ColorWHITE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | paint.setPathEffect(make_pe(1))->unref(); |
| 161 | canvas->drawPath(fPath, paint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 162 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 163 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | canvas->translate(0, SkIntToScalar(50)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | paint.setColor(SK_ColorBLUE); |
| 167 | paint.setPathEffect(make_pe(2))->unref(); |
| 168 | canvas->drawPath(fPath, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 169 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | canvas->translate(0, SkIntToScalar(50)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 171 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | paint.setARGB(0xFF, 0, 0xBB, 0); |
| 173 | paint.setPathEffect(make_pe(3))->unref(); |
| 174 | canvas->drawPath(fPath, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 175 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | canvas->translate(0, SkIntToScalar(50)); |
| 177 | |
| 178 | paint.setARGB(0xFF, 0, 0, 0); |
| 179 | paint.setPathEffect(make_warp_pe())->unref(); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 180 | TestRastBuilder testRastBuilder; |
| 181 | paint.setRasterizer(testRastBuilder.detachRasterizer())->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | canvas->drawPath(fPath, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 184 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | private: |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 186 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | ////////////////////////////////////////////////////////////////////////////// |
| 190 | |
| 191 | static SkView* MyFactory() { return new PathEffectView; } |
| 192 | static SkViewRegister reg(MyFactory); |