blob: e194793f368adb62f9bd62a319f86ff598fd9d28 [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"
22#include "SkPixelXorXfermode.h"
23
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#define CORNER_RADIUS 12
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
26static const int gXY[] = {
27 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
28};
29
reedd9adfe62015-02-01 19:01:04 -080030static SkPathEffect* make_pe(int flags, SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 if (flags == 1)
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000032 return SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
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
reedd9adfe62015-02-01 19:01:04 -080041 SkPathEffect* outer = SkPath1DPathEffect::Create(path, 12, phase,
42 SkPath1DPathEffect::kRotate_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 if (flags == 2)
45 return outer;
46
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000047 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000048
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000049 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 outer->unref();
51 inner->unref();
52 return pe;
53}
54
reedd9adfe62015-02-01 19:01:04 -080055static SkPathEffect* make_warp_pe(SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 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.org0a2bf902014-02-20 20:40:19 +000063 SkPathEffect* outer = SkPath1DPathEffect::Create(
reedd9adfe62015-02-01 19:01:04 -080064 path, 12, phase, SkPath1DPathEffect::kMorph_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000065 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000067 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 outer->unref();
69 inner->unref();
70 return pe;
71}
72
73///////////////////////////////////////////////////////////
74
75#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000076#include "SkLayerRasterizer.h"
77
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000078class TestRastBuilder : public SkLayerRasterizer::Builder {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079public:
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000080 TestRastBuilder() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkPaint paint;
82 paint.setAntiAlias(true);
83
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 paint.setAlpha(0x66);
85 this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 paint.setAlpha(0xFF);
88 this->addLayer(paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 }
90};
91
reed@google.come5ff4392011-06-01 11:59:08 +000092class PathEffectView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 SkPath fPath;
94 SkPoint fClickPt;
reedd9adfe62015-02-01 19:01:04 -080095 SkScalar fPhase;
96
reed@android.com8a1c16f2008-12-17 15:59:43 +000097public:
reedd9adfe62015-02-01 19:01:04 -080098 PathEffectView() : fPhase(0) {
caryclark63c684a2015-02-25 09:04:04 -080099 }
100
101protected:
mtklein36352bf2015-03-25 18:17:31 -0700102 void onOnceBeforeDraw() override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000103 SkRandom rand;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 int steps = 20;
reed@android.com92a50ea2009-08-14 19:27:37 +0000105 SkScalar dist = SkIntToScalar(400);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 SkScalar x = SkIntToScalar(20);
107 SkScalar y = SkIntToScalar(50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 fPath.moveTo(x, y);
reed@android.comf2b98d62010-12-20 18:26:13 +0000110 for (int i = 0; i < steps; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 x += dist/steps;
reed@android.com92a50ea2009-08-14 19:27:37 +0000112 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.com8a1c16f2008-12-17 15:59:43 +0000118 }
119
reed@android.com92a50ea2009-08-14 19:27:37 +0000120 {
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.comae933ce2012-08-23 18:19:56 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@google.come5ff4392011-06-01 11:59:08 +0000130 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
mtklein36352bf2015-03-25 18:17:31 -0700133 bool onQuery(SkEvent* evt) override {
reed@android.comf2b98d62010-12-20 18:26:13 +0000134 if (SampleCode::TitleQ(*evt)) {
135 SampleCode::TitleR(evt, "PathEffects");
136 return true;
137 }
138 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
mtklein36352bf2015-03-25 18:17:31 -0700141 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 SkPaint 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.setColor(SK_ColorBLUE);
reedd9adfe62015-02-01 19:01:04 -0800147 paint.setPathEffect(make_pe(2, fPhase))->unref();
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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 paint.setARGB(0xFF, 0, 0xBB, 0);
reedd9adfe62015-02-01 19:01:04 -0800153 paint.setPathEffect(make_pe(3, fPhase))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reedd9adfe62015-02-01 19:01:04 -0800156 canvas->translate(0, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157
158 paint.setARGB(0xFF, 0, 0, 0);
reedd9adfe62015-02-01 19:01:04 -0800159 paint.setPathEffect(make_warp_pe(fPhase))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000160 TestRastBuilder testRastBuilder;
161 paint.setRasterizer(testRastBuilder.detachRasterizer())->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 canvas->drawPath(fPath, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
mtklein36352bf2015-03-25 18:17:31 -0700165 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800166 fPhase = timer.scaled(40);
reedd9adfe62015-02-01 19:01:04 -0800167 return true;
168 }
169
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170private:
reed@google.come5ff4392011-06-01 11:59:08 +0000171 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172};
173
174//////////////////////////////////////////////////////////////////////////////
175
176static SkView* MyFactory() { return new PathEffectView; }
177static SkViewRegister reg(MyFactory);