blob: ac77f210de911160b5a00f28b657753e725ea37e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14#include "SkShader.h"
15#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "Sk1DPathEffect.h"
17#include "SkCornerPathEffect.h"
18#include "SkPathMeasure.h"
19#include "SkRandom.h"
20#include "SkColorPriv.h"
21#include "SkPixelXorXfermode.h"
22
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#define CORNER_RADIUS 12
24static SkScalar gPhase;
25
26static const int gXY[] = {
27 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
28};
29
reed@android.comf2b98d62010-12-20 18:26:13 +000030static SkPathEffect* make_pe(int flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 if (flags == 1)
32 return new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
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
41 SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, 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
46 SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
47
48 SkPathEffect* pe = new SkComposePathEffect(outer, inner);
49 outer->unref();
50 inner->unref();
51 return pe;
52}
53
reed@android.comf2b98d62010-12-20 18:26:13 +000054static SkPathEffect* make_warp_pe() {
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
62 SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style);
63 SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
64
65 SkPathEffect* pe = new SkComposePathEffect(outer, inner);
66 outer->unref();
67 inner->unref();
68 return pe;
69}
70
71///////////////////////////////////////////////////////////
72
73#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000074#include "SkLayerRasterizer.h"
75
76class testrast : public SkLayerRasterizer {
77public:
reed@android.comf2b98d62010-12-20 18:26:13 +000078 testrast() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkPaint paint;
80 paint.setAntiAlias(true);
81
rmistry@google.comae933ce2012-08-23 18:19:56 +000082#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 paint.setStyle(SkPaint::kStroke_Style);
84 paint.setStrokeWidth(SK_Scalar1*4);
85 this->addLayer(paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 paint.setStrokeWidth(SK_Scalar1*1);
reed@android.com048522d2009-06-23 12:19:41 +000088 paint.setXfermode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 this->addLayer(paint);
90#else
91 paint.setAlpha(0x66);
92 this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 paint.setAlpha(0xFF);
95 this->addLayer(paint);
96#endif
97 }
98};
99
reed@google.come5ff4392011-06-01 11:59:08 +0000100class PathEffectView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 SkPath fPath;
102 SkPoint fClickPt;
103public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104 PathEffectView() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000105 SkRandom rand;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 int steps = 20;
reed@android.com92a50ea2009-08-14 19:27:37 +0000107 SkScalar dist = SkIntToScalar(400);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 SkScalar x = SkIntToScalar(20);
109 SkScalar y = SkIntToScalar(50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 fPath.moveTo(x, y);
reed@android.comf2b98d62010-12-20 18:26:13 +0000112 for (int i = 0; i < steps; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 x += dist/steps;
reed@android.com92a50ea2009-08-14 19:27:37 +0000114 SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
115 if (i == steps/2) {
116 fPath.moveTo(x, tmpY);
117 } else {
118 fPath.lineTo(x, tmpY);
119 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 }
121
reed@android.com92a50ea2009-08-14 19:27:37 +0000122 {
123 SkRect oval;
124 oval.set(SkIntToScalar(20), SkIntToScalar(30),
125 SkIntToScalar(100), SkIntToScalar(60));
126 oval.offset(x, 0);
127 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
128 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@google.come5ff4392011-06-01 11:59:08 +0000132 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135protected:
136 // overrides from SkEventSink
reed@android.comf2b98d62010-12-20 18:26:13 +0000137 virtual bool onQuery(SkEvent* evt) {
138 if (SampleCode::TitleQ(*evt)) {
139 SampleCode::TitleR(evt, "PathEffects");
140 return true;
141 }
142 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@google.come5ff4392011-06-01 11:59:08 +0000145 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000146 gPhase -= SampleCode::GetAnimSecondsDelta() * 40;
reed@android.com671cd652009-05-22 20:44:12 +0000147 this->inval(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
reed@android.comf2b98d62010-12-20 18:26:13 +0000151#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 paint.setAntiAlias(true);
153 paint.setStyle(SkPaint::kStroke_Style);
154 paint.setStrokeWidth(SkIntToScalar(5));
155 canvas->drawPath(fPath, paint);
156 paint.setStrokeWidth(0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
reed@android.com92a50ea2009-08-14 19:27:37 +0000158 paint.setColor(SK_ColorWHITE);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 paint.setPathEffect(make_pe(1))->unref();
160 canvas->drawPath(fPath, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000161#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 canvas->translate(0, SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 paint.setColor(SK_ColorBLUE);
166 paint.setPathEffect(make_pe(2))->unref();
167 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000168
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 canvas->translate(0, SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 paint.setARGB(0xFF, 0, 0xBB, 0);
172 paint.setPathEffect(make_pe(3))->unref();
173 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 canvas->translate(0, SkIntToScalar(50));
176
177 paint.setARGB(0xFF, 0, 0, 0);
178 paint.setPathEffect(make_warp_pe())->unref();
179 paint.setRasterizer(new testrast)->unref();
180 canvas->drawPath(fPath, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183private:
reed@google.come5ff4392011-06-01 11:59:08 +0000184 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185};
186
187//////////////////////////////////////////////////////////////////////////////
188
189static SkView* MyFactory() { return new PathEffectView; }
190static SkViewRegister reg(MyFactory);