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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorPriv.h" |
| 10 | #include "include/core/SkPath.h" |
| 11 | #include "include/core/SkPathMeasure.h" |
| 12 | #include "include/core/SkRegion.h" |
| 13 | #include "include/core/SkShader.h" |
| 14 | #include "include/effects/Sk1DPathEffect.h" |
| 15 | #include "include/effects/SkCornerPathEffect.h" |
| 16 | #include "include/effects/SkGradientShader.h" |
| 17 | #include "include/utils/SkRandom.h" |
| 18 | #include "samplecode/Sample.h" |
| 19 | #include "src/utils/SkUTF.h" |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 20 | #include "tools/timer/TimeUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #define CORNER_RADIUS 12 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
| 24 | static const int gXY[] = { |
| 25 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 26 | }; |
| 27 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 28 | static sk_sp<SkPathEffect> make_pe(int flags, SkScalar phase) { |
| 29 | if (flags == 1) { |
| 30 | return SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
| 31 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | |
| 33 | SkPath path; |
| 34 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 35 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 36 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 37 | path.close(); |
| 38 | path.offset(SkIntToScalar(-6), 0); |
| 39 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 40 | auto outer = SkPath1DPathEffect::Make(path, 12, phase, SkPath1DPathEffect::kRotate_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | if (flags == 2) |
| 43 | return outer; |
| 44 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 45 | auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | |
Mike Reed | a07741a | 2017-02-25 22:34:32 -0500 | [diff] [blame] | 47 | return SkPathEffect::MakeCompose(outer, inner); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 50 | static sk_sp<SkPathEffect> make_warp_pe(SkScalar phase) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | SkPath path; |
| 52 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 53 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 55 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | path.close(); |
| 57 | path.offset(SkIntToScalar(-6), 0); |
| 58 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 59 | auto outer = SkPath1DPathEffect::Make( |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 60 | path, 12, phase, SkPath1DPathEffect::kMorph_Style); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 61 | auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | |
Mike Reed | a07741a | 2017-02-25 22:34:32 -0500 | [diff] [blame] | 63 | return SkPathEffect::MakeCompose(outer, inner); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /////////////////////////////////////////////////////////// |
| 67 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 68 | #include "include/core/SkColorFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 70 | class PathEffectView : public Sample { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | SkPath fPath; |
| 72 | SkPoint fClickPt; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 73 | SkScalar fPhase; |
| 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | public: |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 76 | PathEffectView() : fPhase(0) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 80 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 81 | SkRandom rand; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | int steps = 20; |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 83 | SkScalar dist = SkIntToScalar(400); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | SkScalar x = SkIntToScalar(20); |
| 85 | SkScalar y = SkIntToScalar(50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | fPath.moveTo(x, y); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 88 | for (int i = 0; i < steps; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | x += dist/steps; |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 90 | SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25); |
| 91 | if (i == steps/2) { |
| 92 | fPath.moveTo(x, tmpY); |
| 93 | } else { |
| 94 | fPath.lineTo(x, tmpY); |
| 95 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 98 | { |
| 99 | SkRect oval; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 100 | oval.setLTRB(20, 30, 100, 60); |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 101 | oval.offset(x, 0); |
Mike Reed | 4241f5e | 2019-09-14 19:13:23 +0000 | [diff] [blame] | 102 | fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8)); |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 103 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | fClickPt.set(SkIntToScalar(200), SkIntToScalar(200)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 106 | |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 107 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 109 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 110 | SkString name() override { return SkString("PathEffects"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 112 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 114 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 115 | canvas->translate(0, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | paint.setColor(SK_ColorBLUE); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 118 | paint.setPathEffect(make_pe(2, fPhase)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | canvas->drawPath(fPath, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 120 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 121 | canvas->translate(0, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | paint.setARGB(0xFF, 0, 0xBB, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 124 | paint.setPathEffect(make_pe(3, fPhase)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | canvas->drawPath(fPath, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 126 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 127 | canvas->translate(0, 50); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | |
| 129 | paint.setARGB(0xFF, 0, 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 130 | paint.setPathEffect(make_warp_pe(fPhase)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | canvas->drawPath(fPath, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 133 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 134 | bool onAnimate(double nanos) override { |
| 135 | fPhase = TimeUtils::Scaled(1e-9 * nanos, 40); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 140 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | ////////////////////////////////////////////////////////////////////////////// |
| 144 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 145 | DEF_SAMPLE( return new PathEffectView(); ) |