xref: /aosp_15_r20/external/skia/gm/patheffects.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2012 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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/core/SkPathEffect.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypes.h"
20 #include "include/effects/Sk1DPathEffect.h"
21 #include "include/effects/Sk2DPathEffect.h"
22 #include "include/effects/SkCornerPathEffect.h"
23 #include "include/effects/SkDashPathEffect.h"
24 #include "include/effects/SkDiscretePathEffect.h"
25 #include "include/gpu/ganesh/GrDirectContext.h"
26 
27 #include <initializer_list>
28 
29 namespace skiagm {
30 
compose_pe(SkPaint * paint)31 static void compose_pe(SkPaint* paint) {
32     SkPathEffect* pe = paint->getPathEffect();
33     sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25);
34     sk_sp<SkPathEffect> compose;
35     if (pe) {
36         compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
37     } else {
38         compose = corner;
39     }
40     paint->setPathEffect(compose);
41 }
42 
hair_pe(SkPaint * paint)43 static void hair_pe(SkPaint* paint) {
44     paint->setStrokeWidth(0);
45 }
46 
hair2_pe(SkPaint * paint)47 static void hair2_pe(SkPaint* paint) {
48     paint->setStrokeWidth(0);
49     compose_pe(paint);
50 }
51 
stroke_pe(SkPaint * paint)52 static void stroke_pe(SkPaint* paint) {
53     paint->setStrokeWidth(12);
54     compose_pe(paint);
55 }
56 
dash_pe(SkPaint * paint)57 static void dash_pe(SkPaint* paint) {
58     SkScalar inter[] = { 20, 10, 10, 10 };
59     paint->setStrokeWidth(12);
60     paint->setPathEffect(SkDashPathEffect::Make(inter, std::size(inter), 0));
61     compose_pe(paint);
62 }
63 
64 constexpr int gXY[] = {
65 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
66 };
67 
scale(const SkPath & path,SkScalar scale)68 static SkPath scale(const SkPath& path, SkScalar scale) {
69     SkMatrix m;
70     m.setScale(scale, scale);
71     return path.makeTransform(m);
72 }
73 
one_d_pe(SkPaint * paint)74 static void one_d_pe(SkPaint* paint) {
75     SkPathBuilder b;
76     b.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
77     for (unsigned i = 2; i < std::size(gXY); i += 2) {
78         b.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
79     }
80     b.close().offset(SkIntToScalar(-6), 0);
81     SkPath path = scale(b.detach(), 1.5f);
82 
83     paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
84                                                   SkPath1DPathEffect::kRotate_Style));
85     compose_pe(paint);
86 }
87 
88 typedef void (*PE_Proc)(SkPaint*);
89 constexpr PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
90 
fill_pe(SkPaint * paint)91 static void fill_pe(SkPaint* paint) {
92     paint->setStyle(SkPaint::kFill_Style);
93     paint->setPathEffect(nullptr);
94 }
95 
discrete_pe(SkPaint * paint)96 static void discrete_pe(SkPaint* paint) {
97     paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
98 }
99 
MakeTileEffect()100 static sk_sp<SkPathEffect> MakeTileEffect() {
101     SkMatrix m;
102     m.setScale(SkIntToScalar(12), SkIntToScalar(12));
103 
104     return SkPath2DPathEffect::Make(m, SkPath::Circle(0,0,5));
105 }
106 
tile_pe(SkPaint * paint)107 static void tile_pe(SkPaint* paint) {
108     paint->setPathEffect(MakeTileEffect());
109 }
110 
111 constexpr PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
112 
113 class PathEffectGM : public GM {
114 public:
PathEffectGM()115     PathEffectGM() {}
116 
117 protected:
getName() const118     SkString getName() const override { return SkString("patheffect"); }
119 
getISize()120     SkISize getISize() override { return SkISize::Make(800, 600); }
121 
onDraw(SkCanvas * canvas)122     void onDraw(SkCanvas* canvas) override {
123         SkPaint paint;
124         paint.setAntiAlias(true);
125         paint.setStyle(SkPaint::kStroke_Style);
126 
127         SkPath path = SkPath::Polygon({
128             {20, 20},
129             {70, 120},
130             {120, 30},
131             {170, 80},
132             {240, 50},
133         }, false);
134 
135         canvas->save();
136         for (size_t i = 0; i < std::size(gPE); i++) {
137             gPE[i](&paint);
138             canvas->drawPath(path, paint);
139             canvas->translate(0, 75);
140         }
141         canvas->restore();
142 
143         path.reset();
144         SkRect r = { 0, 0, 250, 120 };
145         path = SkPathBuilder().addOval(r, SkPathDirection::kCW)
146                               .addRect(r.makeInset(50, 50), SkPathDirection::kCCW)
147                               .detach();
148 
149         canvas->translate(320, 20);
150         for (size_t i = 0; i < std::size(gPE2); i++) {
151             gPE2[i](&paint);
152             canvas->drawPath(path, paint);
153             canvas->translate(0, 160);
154         }
155 
156         const SkIRect rect = SkIRect::MakeXYWH(20, 20, 60, 60);
157         for (size_t i = 0; i < std::size(gPE); i++) {
158             SkPaint p;
159             p.setAntiAlias(true);
160             p.setStyle(SkPaint::kFill_Style);
161             gPE[i](&p);
162             canvas->drawIRect(rect, p);
163             canvas->translate(75, 0);
164         }
165     }
166 
167 private:
168     using INHERITED = GM;
169 };
170 
171 DEF_GM( return new PathEffectGM; )
172 
173 }  // namespace skiagm
174 
175 //////////////////////////////////////////////////////////////////////////////
176 
177 #include "include/core/SkStrokeRec.h"
178 #include "src/core/SkPathEffectBase.h"
179 
180 namespace {
181 /**
182  * Example path effect using CTM. This "strokes" a single line segment with some stroke width,
183  * and then inflates the result by some number of pixels.
184  */
185 class StrokeLineInflated : public SkPathEffectBase {
186 public:
StrokeLineInflated(float strokeWidth,float pxInflate)187     StrokeLineInflated(float strokeWidth, float pxInflate)
188             : fRadius(strokeWidth / 2.f), fPxInflate(pxInflate) {}
189 
onNeedsCTM() const190     bool onNeedsCTM() const final { return true; }
191 
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullR,const SkMatrix & ctm) const192     bool onFilterPath(SkPath* dst,
193                       const SkPath& src,
194                       SkStrokeRec* rec,
195                       const SkRect* cullR,
196                       const SkMatrix& ctm) const final {
197         SkASSERT(src.countPoints() == 2);
198         const SkPoint pts[2] = {src.getPoint(0), src.getPoint(1)};
199 
200         SkMatrix invCtm;
201         if (!ctm.invert(&invCtm)) {
202             return false;
203         }
204 
205         // For a line segment, we can just map the (scaled) normal vector to pixel-space,
206         // increase its length by the desired number of pixels, and then map back to canvas space.
207         SkPoint n = {pts[0].fY - pts[1].fY, pts[1].fX - pts[0].fX};
208         if (!n.setLength(fRadius)) {
209             return false;
210         }
211 
212         SkPoint mappedN = ctm.mapVector(n.fX, n.fY);
213         if (!mappedN.setLength(mappedN.length() + fPxInflate)) {
214             return false;
215         }
216         n = invCtm.mapVector(mappedN.fX, mappedN.fY);
217 
218         dst->moveTo(pts[0] + n);
219         dst->lineTo(pts[1] + n);
220         dst->lineTo(pts[1] - n);
221         dst->lineTo(pts[0] - n);
222         dst->close();
223 
224         rec->setFillStyle();
225 
226         return true;
227     }
228 
229 protected:
flatten(SkWriteBuffer &) const230     void flatten(SkWriteBuffer&) const final {}
231 
232 private:
SK_FLATTENABLE_HOOKS(StrokeLineInflated)233     SK_FLATTENABLE_HOOKS(StrokeLineInflated)
234 
235     bool computeFastBounds(SkRect* bounds) const final { return false; }
236 
237     const float fRadius;
238     const float fPxInflate;
239 };
240 
CreateProc(SkReadBuffer &)241 sk_sp<SkFlattenable> StrokeLineInflated::CreateProc(SkReadBuffer&) { return nullptr; }
242 
243 }  // namespace
244 
245 class CTMPathEffectGM : public skiagm::GM {
246 protected:
getName() const247     SkString getName() const override { return SkString("ctmpatheffect"); }
248 
getISize()249     SkISize getISize() override { return SkISize::Make(800, 600); }
250 
251     // CTM-aware path effects are not supported by Ganesh
onGpuSetup(SkCanvas * canvas,SkString *,GraphiteTestContext *)252     DrawResult onGpuSetup(SkCanvas* canvas, SkString*, GraphiteTestContext*) override {
253         auto dctx = GrAsDirectContext(canvas->recordingContext());
254         return dctx == nullptr ? DrawResult::kOk : DrawResult::kSkip;
255     }
256 
onDraw(SkCanvas * canvas)257     void onDraw(SkCanvas* canvas) override {
258         const float strokeWidth = 16;
259         const float pxInflate = 0.5f;
260         sk_sp<SkPathEffect> pathEffect(new StrokeLineInflated(strokeWidth, pxInflate));
261 
262         SkPath path;
263         path.moveTo(100, 100);
264         path.lineTo(200, 200);
265 
266         // Draw the inflated path, and a scaled version, in blue.
267         SkPaint paint;
268         paint.setAntiAlias(true);
269         paint.setColor(SkColorSetA(SK_ColorBLUE, 0xff));
270         paint.setPathEffect(pathEffect);
271         canvas->drawPath(path, paint);
272         canvas->save();
273         canvas->translate(150, 0);
274         canvas->scale(2.5, 0.5f);
275         canvas->drawPath(path, paint);
276         canvas->restore();
277 
278         // Draw the regular stroked version on top in green.
279         // The inflated version should be visible underneath as a blue "border".
280         paint.setPathEffect(nullptr);
281         paint.setStyle(SkPaint::kStroke_Style);
282         paint.setStrokeWidth(strokeWidth);
283         paint.setColor(SkColorSetA(SK_ColorGREEN, 0xff));
284         canvas->drawPath(path, paint);
285         canvas->save();
286         canvas->translate(150, 0);
287         canvas->scale(2.5, 0.5f);
288         canvas->drawPath(path, paint);
289         canvas->restore();
290     }
291 
292 private:
293     using INHERITED = GM;
294 };
295 DEF_GM(return new CTMPathEffectGM;)
296