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 | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkPath.h" |
| 14 | #include "SkRegion.h" |
| 15 | #include "SkShader.h" |
| 16 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "Sk1DPathEffect.h" |
| 18 | #include "SkCornerPathEffect.h" |
| 19 | #include "SkPathMeasure.h" |
| 20 | #include "SkRandom.h" |
| 21 | #include "SkColorPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #define CORNER_RADIUS 12 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | |
| 25 | static const int gXY[] = { |
| 26 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 27 | }; |
| 28 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 29 | static sk_sp<SkPathEffect> make_pe(int flags, SkScalar phase) { |
| 30 | if (flags == 1) { |
| 31 | return SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
| 32 | } |
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 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 41 | auto outer = SkPath1DPathEffect::Make(path, 12, phase, 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 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 46 | auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 48 | return SkComposePathEffect::Make(outer, inner); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | } |
| 50 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 51 | static sk_sp<SkPathEffect> make_warp_pe(SkScalar phase) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | SkPath path; |
| 53 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 54 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 56 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | path.close(); |
| 58 | path.offset(SkIntToScalar(-6), 0); |
| 59 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 60 | auto outer = SkPath1DPathEffect::Make( |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 61 | path, 12, phase, SkPath1DPathEffect::kMorph_Style); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 62 | auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 64 | return SkComposePathEffect::Make(outer, inner); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /////////////////////////////////////////////////////////// |
| 68 | |
| 69 | #include "SkColorFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | #include "SkLayerRasterizer.h" |
| 71 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 72 | class TestRastBuilder : public SkLayerRasterizer::Builder { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | public: |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 74 | TestRastBuilder() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | SkPaint paint; |
| 76 | paint.setAntiAlias(true); |
| 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | paint.setAlpha(0x66); |
| 79 | this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | paint.setAlpha(0xFF); |
| 82 | this->addLayer(paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | } |
| 84 | }; |
| 85 | |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 86 | class PathEffectView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | SkPath fPath; |
| 88 | SkPoint fClickPt; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 89 | SkScalar fPhase; |
| 90 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | public: |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 92 | PathEffectView() : fPhase(0) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 97 | SkRandom rand; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | int steps = 20; |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 99 | SkScalar dist = SkIntToScalar(400); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | SkScalar x = SkIntToScalar(20); |
| 101 | SkScalar y = SkIntToScalar(50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | fPath.moveTo(x, y); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 104 | for (int i = 0; i < steps; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | x += dist/steps; |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 106 | SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25); |
| 107 | if (i == steps/2) { |
| 108 | fPath.moveTo(x, tmpY); |
| 109 | } else { |
| 110 | fPath.lineTo(x, tmpY); |
| 111 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 114 | { |
| 115 | SkRect oval; |
| 116 | oval.set(SkIntToScalar(20), SkIntToScalar(30), |
| 117 | SkIntToScalar(100), SkIntToScalar(60)); |
| 118 | oval.offset(x, 0); |
| 119 | fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8)); |
| 120 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 121 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | fClickPt.set(SkIntToScalar(200), SkIntToScalar(200)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 123 | |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 124 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 126 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 127 | bool onQuery(SkEvent* evt) override { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 128 | if (SampleCode::TitleQ(*evt)) { |
| 129 | SampleCode::TitleR(evt, "PathEffects"); |
| 130 | return true; |
| 131 | } |
| 132 | return this->INHERITED::onQuery(evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 135 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 137 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 138 | canvas->translate(0, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | paint.setColor(SK_ColorBLUE); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 141 | paint.setPathEffect(make_pe(2, fPhase)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | canvas->drawPath(fPath, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 143 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 144 | canvas->translate(0, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | paint.setARGB(0xFF, 0, 0xBB, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 147 | paint.setPathEffect(make_pe(3, fPhase)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | canvas->drawPath(fPath, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 149 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 150 | canvas->translate(0, 50); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | |
| 152 | paint.setARGB(0xFF, 0, 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 153 | paint.setPathEffect(make_warp_pe(fPhase)); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 154 | TestRastBuilder testRastBuilder; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 155 | paint.setRasterizer(testRastBuilder.detach()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | canvas->drawPath(fPath, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 159 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 160 | fPhase = timer.scaled(40); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | private: |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 165 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | ////////////////////////////////////////////////////////////////////////////// |
| 169 | |
| 170 | static SkView* MyFactory() { return new PathEffectView; } |
| 171 | static SkViewRegister reg(MyFactory); |