2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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"
28 #include <initializer_list>
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;
37 compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
41 paint->setPathEffect(compose);
44 static void hair_pe(SkPaint* paint) {
45 paint->setStrokeWidth(0);
48 static void hair2_pe(SkPaint* paint) {
49 paint->setStrokeWidth(0);
53 static void stroke_pe(SkPaint* paint) {
54 paint->setStrokeWidth(12);
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));
65 constexpr int gXY[] = {
66 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
69 static SkPath scale(const SkPath& path, SkScalar scale) {
71 m.setScale(scale, scale);
72 return path.makeTransform(m);
75 static void one_d_pe(SkPaint* paint) {
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]));
81 b.close().offset(SkIntToScalar(-6), 0);
82 SkPath path = scale(b.detach(), 1.5f);
84 paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
85 SkPath1DPathEffect::kRotate_Style));
89 typedef void (*PE_Proc)(SkPaint*);
90 constexpr PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
92 static void fill_pe(SkPaint* paint) {
93 paint->setStyle(SkPaint::kFill_Style);
94 paint->setPathEffect(nullptr);
97 static void discrete_pe(SkPaint* paint) {
98 paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
101 static sk_sp<SkPathEffect> MakeTileEffect() {
103 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
105 return SkPath2DPathEffect::Make(m, SkPath::Circle(0,0,5));
108 static void tile_pe(SkPaint* paint) {
109 paint->setPathEffect(MakeTileEffect());
112 constexpr PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
114 class PathEffectGM : public GM {
120 SkString onShortName() override {
121 return SkString("patheffect");
124 SkISize onISize() override { return SkISize::Make(800, 600); }
126 void onDraw(SkCanvas* canvas) override {
128 paint.setAntiAlias(true);
129 paint.setStyle(SkPaint::kStroke_Style);
131 SkPath path = SkPath::Polygon({
140 for (size_t i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
142 canvas->drawPath(path, paint);
143 canvas->translate(0, 75);
148 SkRect r = { 0, 0, 250, 120 };
149 path = SkPathBuilder().addOval(r, SkPathDirection::kCW)
150 .addRect(r.makeInset(50, 50), SkPathDirection::kCCW)
153 canvas->translate(320, 20);
154 for (size_t i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
156 canvas->drawPath(path, paint);
157 canvas->translate(0, 160);
160 const SkIRect rect = SkIRect::MakeXYWH(20, 20, 60, 60);
161 for (size_t i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
163 p.setAntiAlias(true);
164 p.setStyle(SkPaint::kFill_Style);
166 canvas->drawIRect(rect, p);
167 canvas->translate(75, 0);
172 using INHERITED = GM;
175 DEF_GM( return new PathEffectGM; )
177 } // namespace skiagm
179 //////////////////////////////////////////////////////////////////////////////
181 class ComboPathEfectsGM : public skiagm::GM {
183 ComboPathEfectsGM() {}
187 SkString onShortName() override {
188 return SkString("combo-patheffects");
191 SkISize onISize() override { return SkISize::Make(360, 630); }
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)
199 sk_sp<SkPathEffect> effects[] = {
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),
213 wireframe.setStyle(SkPaint::kStroke_Style);
214 wireframe.setAntiAlias(true);
217 paint.setColor(0xFF8888FF);
218 paint.setAntiAlias(true);
220 for (const SkPath& path : { path0, path1 }) {
222 for (const sk_sp<SkPathEffect>& pe : effects) {
223 paint.setPathEffect(pe);
224 canvas->drawPath(path, paint);
225 canvas->drawPath(path, wireframe);
227 canvas->translate(0, 150);
230 canvas->translate(180, 0);
235 using INHERITED = GM;
237 DEF_GM(return new ComboPathEfectsGM;)
239 #include "include/effects/SkStrokeAndFillPathEffect.h"
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;
246 typedef SkPath (*Maker)();
247 const Maker makers[] = {
248 []() { return SkPath::Oval({0, 0, 100, 100}, SkPathDirection::kCW); },
249 []() { return SkPath::Oval({0, 0, 100, 100}, SkPathDirection::kCCW); },
251 const SkPoint pts[] = {
252 {0, 0}, {100, 100}, {0, 100}, {100, 0},
254 return SkPath::Polygon(pts, SK_ARRAY_COUNT(pts), true);
259 SkPaint::Style fStyle;
262 bool fExpectStrokeAndFill;
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 },
273 canvas->translate(20, 20);
274 for (auto maker : makers) {
275 const SkPath path = maker();
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);
283 canvas->drawPath(path, paint);
284 canvas->translate(150, 0);
288 canvas->translate(0, 150);
292 //////////////////////////////////////////////////////////////////////////////
294 #include "include/core/SkStrokeRec.h"
295 #include "src/core/SkPathEffectBase.h"
299 * Example path effect using CTM. This "strokes" a single line segment with some stroke width,
300 * and then inflates the result by some number of pixels.
302 class StrokeLineInflated : public SkPathEffectBase {
304 StrokeLineInflated(float strokeWidth, float pxInflate)
305 : fRadius(strokeWidth / 2.f), fPxInflate(pxInflate) {}
307 bool onNeedsCTM() const final { return true; }
309 bool onFilterPath(SkPath* dst,
313 const SkMatrix& ctm) const final {
314 SkASSERT(src.countPoints() == 2);
315 const SkPoint pts[2] = {src.getPoint(0), src.getPoint(1)};
318 if (!ctm.invert(&invCtm)) {
322 // For a line segment, we can just map the (scaled) normal vector to pixel-space,
323 // increase its length by the desired number of pixels, and then map back to canvas space.
324 SkPoint n = {pts[0].fY - pts[1].fY, pts[1].fX - pts[0].fX};
325 if (!n.setLength(fRadius)) {
329 SkPoint mappedN = ctm.mapVector(n.fX, n.fY);
330 if (!mappedN.setLength(mappedN.length() + fPxInflate)) {
333 n = invCtm.mapVector(mappedN.fX, mappedN.fY);
335 dst->moveTo(pts[0] + n);
336 dst->lineTo(pts[1] + n);
337 dst->lineTo(pts[1] - n);
338 dst->lineTo(pts[0] - n);
347 void flatten(SkWriteBuffer&) const final {}
350 SK_FLATTENABLE_HOOKS(StrokeLineInflated)
352 bool computeFastBounds(SkRect* bounds) const final { return false; }
355 const float fPxInflate;
358 sk_sp<SkFlattenable> StrokeLineInflated::CreateProc(SkReadBuffer&) { return nullptr; }
362 class CTMPathEffectGM : public skiagm::GM {
364 SkString onShortName() override { return SkString("ctmpatheffect"); }
366 SkISize onISize() override { return SkISize::Make(800, 600); }
368 // TODO: ctm-aware path effects are currently CPU only
369 DrawResult onGpuSetup(GrDirectContext* dctx, SkString*) override {
370 return dctx == nullptr ? DrawResult::kOk : DrawResult::kSkip;
373 void onDraw(SkCanvas* canvas) override {
374 const float strokeWidth = 16;
375 const float pxInflate = 0.5f;
376 sk_sp<SkPathEffect> pathEffect(new StrokeLineInflated(strokeWidth, pxInflate));
379 path.moveTo(100, 100);
380 path.lineTo(200, 200);
382 // Draw the inflated path, and a scaled version, in blue.
384 paint.setAntiAlias(true);
385 paint.setColor(SkColorSetA(SK_ColorBLUE, 0xff));
386 paint.setPathEffect(pathEffect);
387 canvas->drawPath(path, paint);
389 canvas->translate(150, 0);
390 canvas->scale(2.5, 0.5f);
391 canvas->drawPath(path, paint);
394 // Draw the regular stroked version on top in green.
395 // The inflated version should be visible underneath as a blue "border".
396 paint.setPathEffect(nullptr);
397 paint.setStyle(SkPaint::kStroke_Style);
398 paint.setStrokeWidth(strokeWidth);
399 paint.setColor(SkColorSetA(SK_ColorGREEN, 0xff));
400 canvas->drawPath(path, paint);
402 canvas->translate(150, 0);
403 canvas->scale(2.5, 0.5f);
404 canvas->drawPath(path, paint);
409 using INHERITED = GM;
411 DEF_GM(return new CTMPathEffectGM;)