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/effects/SkOpPathEffect.h"
26 #include "include/pathops/SkPathOps.h"
27 
28 #include <initializer_list>
29 
30 namespace skiagm {
31 
compose_pe(SkPaint * paint)32 static void compose_pe(SkPaint* paint) {
33     SkPathEffect* pe = paint->getPathEffect();
34     sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25);
35     sk_sp<SkPathEffect> compose;
36     if (pe) {
37         compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
38     } else {
39         compose = corner;
40     }
41     paint->setPathEffect(compose);
42 }
43 
hair_pe(SkPaint * paint)44 static void hair_pe(SkPaint* paint) {
45     paint->setStrokeWidth(0);
46 }
47 
hair2_pe(SkPaint * paint)48 static void hair2_pe(SkPaint* paint) {
49     paint->setStrokeWidth(0);
50     compose_pe(paint);
51 }
52 
stroke_pe(SkPaint * paint)53 static void stroke_pe(SkPaint* paint) {
54     paint->setStrokeWidth(12);
55     compose_pe(paint);
56 }
57 
dash_pe(SkPaint * paint)58 static void dash_pe(SkPaint* paint) {
59     SkScalar inter[] = { 20, 10, 10, 10 };
60     paint->setStrokeWidth(12);
61     paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0));
62     compose_pe(paint);
63 }
64 
65 constexpr int gXY[] = {
66 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
67 };
68 
scale(const SkPath & path,SkScalar scale)69 static SkPath scale(const SkPath& path, SkScalar scale) {
70     SkMatrix m;
71     m.setScale(scale, scale);
72     return path.makeTransform(m);
73 }
74 
one_d_pe(SkPaint * paint)75 static void one_d_pe(SkPaint* paint) {
76     SkPathBuilder b;
77     b.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
78     for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) {
79         b.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
80     }
81     b.close().offset(SkIntToScalar(-6), 0);
82     SkPath path = scale(b.detach(), 1.5f);
83 
84     paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
85                                                   SkPath1DPathEffect::kRotate_Style));
86     compose_pe(paint);
87 }
88 
89 typedef void (*PE_Proc)(SkPaint*);
90 constexpr PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
91 
fill_pe(SkPaint * paint)92 static void fill_pe(SkPaint* paint) {
93     paint->setStyle(SkPaint::kFill_Style);
94     paint->setPathEffect(nullptr);
95 }
96 
discrete_pe(SkPaint * paint)97 static void discrete_pe(SkPaint* paint) {
98     paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
99 }
100 
MakeTileEffect()101 static sk_sp<SkPathEffect> MakeTileEffect() {
102     SkMatrix m;
103     m.setScale(SkIntToScalar(12), SkIntToScalar(12));
104 
105     return SkPath2DPathEffect::Make(m, SkPath::Circle(0,0,5));
106 }
107 
tile_pe(SkPaint * paint)108 static void tile_pe(SkPaint* paint) {
109     paint->setPathEffect(MakeTileEffect());
110 }
111 
112 constexpr PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
113 
114 class PathEffectGM : public GM {
115 public:
PathEffectGM()116     PathEffectGM() {}
117 
118 protected:
119 
onShortName()120     SkString onShortName() override {
121         return SkString("patheffect");
122     }
123 
onISize()124     SkISize onISize() override { return SkISize::Make(800, 600); }
125 
onDraw(SkCanvas * canvas)126     void onDraw(SkCanvas* canvas) override {
127         SkPaint paint;
128         paint.setAntiAlias(true);
129         paint.setStyle(SkPaint::kStroke_Style);
130 
131         SkPath path = SkPath::Polygon({
132             {20, 20},
133             {70, 120},
134             {120, 30},
135             {170, 80},
136             {240, 50},
137         }, false);
138 
139         canvas->save();
140         for (size_t i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
141             gPE[i](&paint);
142             canvas->drawPath(path, paint);
143             canvas->translate(0, 75);
144         }
145         canvas->restore();
146 
147         path.reset();
148         SkRect r = { 0, 0, 250, 120 };
149         path = SkPathBuilder().addOval(r, SkPathDirection::kCW)
150                               .addRect(r.makeInset(50, 50), SkPathDirection::kCCW)
151                               .detach();
152 
153         canvas->translate(320, 20);
154         for (size_t i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
155             gPE2[i](&paint);
156             canvas->drawPath(path, paint);
157             canvas->translate(0, 160);
158         }
159 
160         const SkIRect rect = SkIRect::MakeXYWH(20, 20, 60, 60);
161         for (size_t i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
162             SkPaint p;
163             p.setAntiAlias(true);
164             p.setStyle(SkPaint::kFill_Style);
165             gPE[i](&p);
166             canvas->drawIRect(rect, p);
167             canvas->translate(75, 0);
168         }
169     }
170 
171 private:
172     using INHERITED = GM;
173 };
174 
175 DEF_GM( return new PathEffectGM; )
176 
177 }  // namespace skiagm
178 
179 //////////////////////////////////////////////////////////////////////////////
180 
181 class ComboPathEfectsGM : public skiagm::GM {
182 public:
ComboPathEfectsGM()183     ComboPathEfectsGM() {}
184 
185 protected:
186 
onShortName()187     SkString onShortName() override {
188         return SkString("combo-patheffects");
189     }
190 
onISize()191     SkISize onISize() override { return SkISize::Make(360, 630); }
192 
onDraw(SkCanvas * canvas)193     void onDraw(SkCanvas* canvas) override {
194         SkPath path0 = SkPath::Circle(100, 100, 60),
195                path1 = SkPathBuilder().moveTo(20, 20)
196                                       .cubicTo(20, 180, 140, 0, 140, 140)
197                                       .detach();
198 
199         sk_sp<SkPathEffect> effects[] = {
200             nullptr,
201             SkStrokePathEffect::Make(20, SkPaint::kRound_Join, SkPaint::kRound_Cap, 0),
202             SkMergePathEffect::Make(nullptr,
203                                     SkStrokePathEffect::Make(20, SkPaint::kRound_Join,
204                                                              SkPaint::kRound_Cap, 0),
205                                     kDifference_SkPathOp),
206             SkMergePathEffect::Make(SkMatrixPathEffect::MakeTranslate(50, 30),
207                                     SkStrokePathEffect::Make(20, SkPaint::kRound_Join,
208                                                              SkPaint::kRound_Cap, 0),
209                                     kReverseDifference_SkPathOp),
210         };
211 
212         SkPaint wireframe;
213         wireframe.setStyle(SkPaint::kStroke_Style);
214         wireframe.setAntiAlias(true);
215 
216         SkPaint paint;
217         paint.setColor(0xFF8888FF);
218         paint.setAntiAlias(true);
219 
220         for (const SkPath& path : { path0, path1 }) {
221             canvas->save();
222             for (const sk_sp<SkPathEffect>& pe : effects) {
223                 paint.setPathEffect(pe);
224                 canvas->drawPath(path, paint);
225                 canvas->drawPath(path, wireframe);
226 
227                 canvas->translate(0, 150);
228             }
229             canvas->restore();
230             canvas->translate(180, 0);
231         }
232     }
233 
234 private:
235     using INHERITED = GM;
236 };
237 DEF_GM(return new ComboPathEfectsGM;)
238 
239 #include "include/effects/SkStrokeAndFillPathEffect.h"
240 
241 // Test that we can replicate SkPaint::kStrokeAndFill_Style
242 // with a patheffect. We expect the 2nd and 3rd columns to draw the same.
243 DEF_SIMPLE_GM(stroke_and_fill_patheffect, canvas, 900, 450) {
244     const float kStrokeWidth = 20;
245 
246     typedef SkPath (*Maker)();
247     const Maker makers[] = {
__anona5324ff60102() 248         []() { return SkPath::Oval({0, 0, 100, 100}, SkPathDirection::kCW); },
__anona5324ff60202() 249         []() { return SkPath::Oval({0, 0, 100, 100}, SkPathDirection::kCCW); },
__anona5324ff60302() 250         []() {
251             const SkPoint pts[] = {
252                 {0, 0}, {100, 100}, {0, 100}, {100, 0},
253             };
254             return SkPath::Polygon(pts, SK_ARRAY_COUNT(pts), true);
255         },
256     };
257 
258     const struct {
259         SkPaint::Style  fStyle;
260         float           fWidth;
261         bool            fUsePE;
262         bool            fExpectStrokeAndFill;
263     } rec[] = {
264         { SkPaint::kStroke_Style,                   0, false, false },
265         { SkPaint::kFill_Style,                     0,  true, false },
266         { SkPaint::kStroke_Style,                   0,  true, false },
267         { SkPaint::kStrokeAndFill_Style, kStrokeWidth, false, true  },
268         { SkPaint::kStroke_Style,        kStrokeWidth,  true, true  },
269         { SkPaint::kStrokeAndFill_Style, kStrokeWidth,  true, true  },
270     };
271 
272     SkPaint paint;
273     canvas->translate(20, 20);
274     for (auto maker : makers) {
275         const SkPath path = maker();
276         canvas->save();
277         for (const auto& r : rec) {
278             paint.setStyle(r.fStyle);
279             paint.setStrokeWidth(r.fWidth);
280             paint.setPathEffect(r.fUsePE ? SkStrokeAndFillPathEffect::Make() : nullptr);
281             paint.setColor(r.fExpectStrokeAndFill ? SK_ColorGRAY : SK_ColorBLACK);
282 
283             canvas->drawPath(path, paint);
284             canvas->translate(150, 0);
285         }
286         canvas->restore();
287 
288         canvas->translate(0, 150);
289     }
290 }
291