xref: /aosp_15_r20/external/skia/tools/viewer/PathEffectsSlide.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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  */
7 
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkColorPriv.h"
10 #include "include/core/SkPath.h"
11 #include "include/core/SkPathEffect.h"
12 #include "include/core/SkPathMeasure.h"
13 #include "include/core/SkRegion.h"
14 #include "include/core/SkShader.h"
15 #include "include/effects/Sk1DPathEffect.h"
16 #include "include/effects/SkCornerPathEffect.h"
17 #include "include/effects/SkGradientShader.h"
18 #include "src/base/SkRandom.h"
19 #include "src/base/SkUTF.h"
20 #include "tools/timer/TimeUtils.h"
21 #include "tools/viewer/Slide.h"
22 
23 #define CORNER_RADIUS   12
24 
25 static const int gXY[] = {
26     4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
27 };
28 
make_pe(int flags,SkScalar phase)29 static sk_sp<SkPathEffect> make_pe(int flags, SkScalar phase) {
30     if (flags == 1) {
31         return SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
32     }
33 
34     SkPath  path;
35     path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
36     for (unsigned i = 2; i < std::size(gXY); i += 2)
37         path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
38     path.close();
39     path.offset(SkIntToScalar(-6), 0);
40 
41     auto outer = SkPath1DPathEffect::Make(path, 12, phase, SkPath1DPathEffect::kRotate_Style);
42 
43     if (flags == 2)
44         return outer;
45 
46     auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
47 
48     return SkPathEffect::MakeCompose(outer, inner);
49 }
50 
make_warp_pe(SkScalar phase)51 static sk_sp<SkPathEffect> make_warp_pe(SkScalar phase) {
52     SkPath  path;
53     path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
54     for (unsigned i = 2; i < std::size(gXY); i += 2) {
55         path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
56     }
57     path.close();
58     path.offset(SkIntToScalar(-6), 0);
59 
60     auto outer = SkPath1DPathEffect::Make(
61         path, 12, phase, SkPath1DPathEffect::kMorph_Style);
62     auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
63 
64     return SkPathEffect::MakeCompose(outer, inner);
65 }
66 
67 ///////////////////////////////////////////////////////////
68 
69 #include "include/core/SkColorFilter.h"
70 
71 class PathEffectSlide : public Slide {
72     SkPath  fPath;
73     SkPoint fClickPt;
74     SkScalar fPhase;
75 
76 public:
PathEffectSlide()77     PathEffectSlide() : fPhase(0) { fName = "PathEffects"; }
78 
load(SkScalar w,SkScalar h)79     void load(SkScalar w, SkScalar h) override {
80         SkRandom    rand;
81         int         steps = 20;
82         SkScalar    dist = SkIntToScalar(400);
83         SkScalar    x = SkIntToScalar(20);
84         SkScalar    y = SkIntToScalar(50);
85 
86         fPath.moveTo(x, y);
87         for (int i = 0; i < steps; i++) {
88             x += dist/steps;
89             SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
90             if (i == steps/2) {
91                 fPath.moveTo(x, tmpY);
92             } else {
93                 fPath.lineTo(x, tmpY);
94             }
95         }
96 
97         {
98             SkRect  oval;
99             oval.setLTRB(20, 30, 100, 60);
100             oval.offset(x, 0);
101             fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
102         }
103 
104         fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
105     }
106 
draw(SkCanvas * canvas)107     void draw(SkCanvas* canvas) override {
108         canvas->clear(0xFFDDDDDD);
109 
110         SkPaint paint;
111 
112         canvas->translate(0, 50);
113 
114         paint.setColor(SK_ColorBLUE);
115         paint.setPathEffect(make_pe(2, fPhase));
116         canvas->drawPath(fPath, paint);
117 
118         canvas->translate(0, 50);
119 
120         paint.setARGB(0xFF, 0, 0xBB, 0);
121         paint.setPathEffect(make_pe(3, fPhase));
122         canvas->drawPath(fPath, paint);
123 
124         canvas->translate(0, 50);
125 
126         paint.setARGB(0xFF, 0, 0, 0);
127         paint.setPathEffect(make_warp_pe(fPhase));
128         canvas->drawPath(fPath, paint);
129     }
130 
animate(double nanos)131     bool animate(double nanos) override {
132         fPhase = TimeUtils::Scaled(1e-9 * nanos, 40);
133         return true;
134     }
135 };
136 
137 //////////////////////////////////////////////////////////////////////////////
138 
139 DEF_SLIDE( return new PathEffectSlide(); )
140