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