blob: a162cf33ad6a26249e12fdc49dc7645c2e6896fa [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
reedd9adfe62015-02-01 19:01:04 -080029static SkPathEffect* make_pe(int flags, SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 if (flags == 1)
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000031 return SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
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
reedd9adfe62015-02-01 19:01:04 -080040 SkPathEffect* outer = SkPath1DPathEffect::Create(path, 12, phase,
41 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
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000046 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000048 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 outer->unref();
50 inner->unref();
51 return pe;
52}
53
reedd9adfe62015-02-01 19:01:04 -080054static SkPathEffect* make_warp_pe(SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 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.org0a2bf902014-02-20 20:40:19 +000062 SkPathEffect* outer = SkPath1DPathEffect::Create(
reedd9adfe62015-02-01 19:01:04 -080063 path, 12, phase, SkPath1DPathEffect::kMorph_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000064 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000065
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000066 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 outer->unref();
68 inner->unref();
69 return pe;
70}
71
72///////////////////////////////////////////////////////////
73
74#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000075#include "SkLayerRasterizer.h"
76
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000077class TestRastBuilder : public SkLayerRasterizer::Builder {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078public:
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000079 TestRastBuilder() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SkPaint paint;
81 paint.setAntiAlias(true);
82
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 paint.setAlpha(0x66);
84 this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 paint.setAlpha(0xFF);
87 this->addLayer(paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
89};
90
reed@google.come5ff4392011-06-01 11:59:08 +000091class PathEffectView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 SkPath fPath;
93 SkPoint fClickPt;
reedd9adfe62015-02-01 19:01:04 -080094 SkScalar fPhase;
95
reed@android.com8a1c16f2008-12-17 15:59:43 +000096public:
reedd9adfe62015-02-01 19:01:04 -080097 PathEffectView() : fPhase(0) {
caryclark63c684a2015-02-25 09:04:04 -080098 }
99
100protected:
mtklein36352bf2015-03-25 18:17:31 -0700101 void onOnceBeforeDraw() override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000102 SkRandom rand;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 int steps = 20;
reed@android.com92a50ea2009-08-14 19:27:37 +0000104 SkScalar dist = SkIntToScalar(400);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 SkScalar x = SkIntToScalar(20);
106 SkScalar y = SkIntToScalar(50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 fPath.moveTo(x, y);
reed@android.comf2b98d62010-12-20 18:26:13 +0000109 for (int i = 0; i < steps; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 x += dist/steps;
reed@android.com92a50ea2009-08-14 19:27:37 +0000111 SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
112 if (i == steps/2) {
113 fPath.moveTo(x, tmpY);
114 } else {
115 fPath.lineTo(x, tmpY);
116 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 }
118
reed@android.com92a50ea2009-08-14 19:27:37 +0000119 {
120 SkRect oval;
121 oval.set(SkIntToScalar(20), SkIntToScalar(30),
122 SkIntToScalar(100), SkIntToScalar(60));
123 oval.offset(x, 0);
124 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
125 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@google.come5ff4392011-06-01 11:59:08 +0000129 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
mtklein36352bf2015-03-25 18:17:31 -0700132 bool onQuery(SkEvent* evt) override {
reed@android.comf2b98d62010-12-20 18:26:13 +0000133 if (SampleCode::TitleQ(*evt)) {
134 SampleCode::TitleR(evt, "PathEffects");
135 return true;
136 }
137 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
mtklein36352bf2015-03-25 18:17:31 -0700140 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
reedd9adfe62015-02-01 19:01:04 -0800143 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 paint.setColor(SK_ColorBLUE);
reedd9adfe62015-02-01 19:01:04 -0800146 paint.setPathEffect(make_pe(2, fPhase))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reedd9adfe62015-02-01 19:01:04 -0800149 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 paint.setARGB(0xFF, 0, 0xBB, 0);
reedd9adfe62015-02-01 19:01:04 -0800152 paint.setPathEffect(make_pe(3, fPhase))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reedd9adfe62015-02-01 19:01:04 -0800155 canvas->translate(0, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156
157 paint.setARGB(0xFF, 0, 0, 0);
reedd9adfe62015-02-01 19:01:04 -0800158 paint.setPathEffect(make_warp_pe(fPhase))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000159 TestRastBuilder testRastBuilder;
160 paint.setRasterizer(testRastBuilder.detachRasterizer())->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 canvas->drawPath(fPath, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
mtklein36352bf2015-03-25 18:17:31 -0700164 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800165 fPhase = timer.scaled(40);
reedd9adfe62015-02-01 19:01:04 -0800166 return true;
167 }
168
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169private:
reed@google.come5ff4392011-06-01 11:59:08 +0000170 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171};
172
173//////////////////////////////////////////////////////////////////////////////
174
175static SkView* MyFactory() { return new PathEffectView; }
176static SkViewRegister reg(MyFactory);