blob: 3d9ba9cb362a43708fbe3f3647ecefdd098f9dda [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed76113a92015-02-02 12:55:02 -08007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#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.com8a1c16f2008-12-17 15:59:43 +000017#include "Sk1DPathEffect.h"
18#include "SkCornerPathEffect.h"
19#include "SkPathMeasure.h"
20#include "SkRandom.h"
21#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#define CORNER_RADIUS 12
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
25static const int gXY[] = {
26 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
27};
28
reeda4393342016-03-18 11:22:57 -070029static sk_sp<SkPathEffect> make_pe(int flags, SkScalar phase) {
30 if (flags == 1) {
31 return SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
32 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
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
reeda4393342016-03-18 11:22:57 -070041 auto outer = SkPath1DPathEffect::Make(path, 12, phase, SkPath1DPathEffect::kRotate_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 if (flags == 2)
44 return outer;
45
reeda4393342016-03-18 11:22:57 -070046 auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
Mike Reeda07741a2017-02-25 22:34:32 -050048 return SkPathEffect::MakeCompose(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049}
50
reeda4393342016-03-18 11:22:57 -070051static sk_sp<SkPathEffect> make_warp_pe(SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 SkPath path;
53 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
reeda4393342016-03-18 11:22:57 -070054 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
reeda4393342016-03-18 11:22:57 -070056 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 path.close();
58 path.offset(SkIntToScalar(-6), 0);
59
reeda4393342016-03-18 11:22:57 -070060 auto outer = SkPath1DPathEffect::Make(
reedd9adfe62015-02-01 19:01:04 -080061 path, 12, phase, SkPath1DPathEffect::kMorph_Style);
reeda4393342016-03-18 11:22:57 -070062 auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000063
Mike Reeda07741a2017-02-25 22:34:32 -050064 return SkPathEffect::MakeCompose(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000065}
66
67///////////////////////////////////////////////////////////
68
69#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000070#include "SkLayerRasterizer.h"
71
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000072class TestRastBuilder : public SkLayerRasterizer::Builder {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073public:
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000074 TestRastBuilder() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkPaint paint;
76 paint.setAntiAlias(true);
77
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 paint.setAlpha(0x66);
79 this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 paint.setAlpha(0xFF);
82 this->addLayer(paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 }
84};
85
reed@google.come5ff4392011-06-01 11:59:08 +000086class PathEffectView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkPath fPath;
88 SkPoint fClickPt;
reedd9adfe62015-02-01 19:01:04 -080089 SkScalar fPhase;
90
reed@android.com8a1c16f2008-12-17 15:59:43 +000091public:
reedd9adfe62015-02-01 19:01:04 -080092 PathEffectView() : fPhase(0) {
caryclark63c684a2015-02-25 09:04:04 -080093 }
94
95protected:
mtklein36352bf2015-03-25 18:17:31 -070096 void onOnceBeforeDraw() override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000097 SkRandom rand;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 int steps = 20;
reed@android.com92a50ea2009-08-14 19:27:37 +000099 SkScalar dist = SkIntToScalar(400);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 SkScalar x = SkIntToScalar(20);
101 SkScalar y = SkIntToScalar(50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 fPath.moveTo(x, y);
reed@android.comf2b98d62010-12-20 18:26:13 +0000104 for (int i = 0; i < steps; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 x += dist/steps;
reed@android.com92a50ea2009-08-14 19:27:37 +0000106 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.com8a1c16f2008-12-17 15:59:43 +0000112 }
113
reed@android.com92a50ea2009-08-14 19:27:37 +0000114 {
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.comae933ce2012-08-23 18:19:56 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed@google.come5ff4392011-06-01 11:59:08 +0000124 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
mtklein36352bf2015-03-25 18:17:31 -0700127 bool onQuery(SkEvent* evt) override {
reed@android.comf2b98d62010-12-20 18:26:13 +0000128 if (SampleCode::TitleQ(*evt)) {
129 SampleCode::TitleR(evt, "PathEffects");
130 return true;
131 }
132 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
mtklein36352bf2015-03-25 18:17:31 -0700135 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reedd9adfe62015-02-01 19:01:04 -0800138 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 paint.setColor(SK_ColorBLUE);
reeda4393342016-03-18 11:22:57 -0700141 paint.setPathEffect(make_pe(2, fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reedd9adfe62015-02-01 19:01:04 -0800144 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 paint.setARGB(0xFF, 0, 0xBB, 0);
reeda4393342016-03-18 11:22:57 -0700147 paint.setPathEffect(make_pe(3, fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reedd9adfe62015-02-01 19:01:04 -0800150 canvas->translate(0, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151
152 paint.setARGB(0xFF, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700153 paint.setPathEffect(make_warp_pe(fPhase));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000154 TestRastBuilder testRastBuilder;
reed7b380d02016-03-21 13:25:16 -0700155 paint.setRasterizer(testRastBuilder.detach());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 canvas->drawPath(fPath, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
mtklein36352bf2015-03-25 18:17:31 -0700159 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800160 fPhase = timer.scaled(40);
reedd9adfe62015-02-01 19:01:04 -0800161 return true;
162 }
163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164private:
reed@google.come5ff4392011-06-01 11:59:08 +0000165 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166};
167
168//////////////////////////////////////////////////////////////////////////////
169
170static SkView* MyFactory() { return new PathEffectView; }
171static SkViewRegister reg(MyFactory);