Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / patheffects.cpp
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
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
44 static void hair_pe(SkPaint* paint) {
45     paint->setStrokeWidth(0);
46 }
47
48 static void hair2_pe(SkPaint* paint) {
49     paint->setStrokeWidth(0);
50     compose_pe(paint);
51 }
52
53 static void stroke_pe(SkPaint* paint) {
54     paint->setStrokeWidth(12);
55     compose_pe(paint);
56 }
57
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
69 static SkPath scale(const SkPath& path, SkScalar scale) {
70     SkMatrix m;
71     m.setScale(scale, scale);
72     return path.makeTransform(m);
73 }
74
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
92 static void fill_pe(SkPaint* paint) {
93     paint->setStyle(SkPaint::kFill_Style);
94     paint->setPathEffect(nullptr);
95 }
96
97 static void discrete_pe(SkPaint* paint) {
98     paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
99 }
100
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
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:
116     PathEffectGM() {}
117
118 protected:
119
120     SkString onShortName() override {
121         return SkString("patheffect");
122     }
123
124     SkISize onISize() override { return SkISize::Make(800, 600); }
125
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:
183     ComboPathEfectsGM() {}
184
185 protected:
186
187     SkString onShortName() override {
188         return SkString("combo-patheffects");
189     }
190
191     SkISize onISize() override { return SkISize::Make(360, 630); }
192
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[] = {
248         []() { return SkPath::Oval({0, 0, 100, 100}, SkPathDirection::kCW); },
249         []() { return SkPath::Oval({0, 0, 100, 100}, SkPathDirection::kCCW); },
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
292 //////////////////////////////////////////////////////////////////////////////
293
294 #include "include/core/SkStrokeRec.h"
295 #include "src/core/SkPathEffectBase.h"
296
297 namespace {
298 /**
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.
301  */
302 class StrokeLineInflated : public SkPathEffectBase {
303 public:
304     StrokeLineInflated(float strokeWidth, float pxInflate)
305             : fRadius(strokeWidth / 2.f), fPxInflate(pxInflate) {}
306
307     bool onNeedsCTM() const final { return true; }
308
309     bool onFilterPath(SkPath* dst,
310                       const SkPath& src,
311                       SkStrokeRec* rec,
312                       const SkRect* cullR,
313                       const SkMatrix& ctm) const final {
314         SkASSERT(src.countPoints() == 2);
315         const SkPoint pts[2] = {src.getPoint(0), src.getPoint(1)};
316
317         SkMatrix invCtm;
318         if (!ctm.invert(&invCtm)) {
319             return false;
320         }
321
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)) {
326             return false;
327         }
328
329         SkPoint mappedN = ctm.mapVector(n.fX, n.fY);
330         if (!mappedN.setLength(mappedN.length() + fPxInflate)) {
331             return false;
332         }
333         n = invCtm.mapVector(mappedN.fX, mappedN.fY);
334
335         dst->moveTo(pts[0] + n);
336         dst->lineTo(pts[1] + n);
337         dst->lineTo(pts[1] - n);
338         dst->lineTo(pts[0] - n);
339         dst->close();
340
341         rec->setFillStyle();
342
343         return true;
344     }
345
346 protected:
347     void flatten(SkWriteBuffer&) const final {}
348
349 private:
350     SK_FLATTENABLE_HOOKS(StrokeLineInflated)
351
352     bool computeFastBounds(SkRect* bounds) const final { return false; }
353
354     const float fRadius;
355     const float fPxInflate;
356 };
357
358 sk_sp<SkFlattenable> StrokeLineInflated::CreateProc(SkReadBuffer&) { return nullptr; }
359
360 }  // namespace
361
362 class CTMPathEffectGM : public skiagm::GM {
363 protected:
364     SkString onShortName() override { return SkString("ctmpatheffect"); }
365
366     SkISize onISize() override { return SkISize::Make(800, 600); }
367
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;
371     }
372
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));
377
378         SkPath path;
379         path.moveTo(100, 100);
380         path.lineTo(200, 200);
381
382         // Draw the inflated path, and a scaled version, in blue.
383         SkPaint paint;
384         paint.setAntiAlias(true);
385         paint.setColor(SkColorSetA(SK_ColorBLUE, 0xff));
386         paint.setPathEffect(pathEffect);
387         canvas->drawPath(path, paint);
388         canvas->save();
389         canvas->translate(150, 0);
390         canvas->scale(2.5, 0.5f);
391         canvas->drawPath(path, paint);
392         canvas->restore();
393
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);
401         canvas->save();
402         canvas->translate(150, 0);
403         canvas->scale(2.5, 0.5f);
404         canvas->drawPath(path, paint);
405         canvas->restore();
406     }
407
408 private:
409     using INHERITED = GM;
410 };
411 DEF_GM(return new CTMPathEffectGM;)