xref: /aosp_15_r20/external/skia/gm/pathcontourstart.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 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/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPathEffect.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRRect.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/effects/SkDashPathEffect.h"
20 #include "include/private/base/SkTArray.h"
21 #include "include/private/base/SkTemplates.h"
22 
23 using namespace skia_private;
24 
25 namespace skiagm {
26 
27 class ContourStartGM : public GM {
28 protected:
onOnceBeforeDraw()29     void onOnceBeforeDraw() override {
30         const SkScalar kMaxDashLen = 100;
31         const SkScalar kDashGrowth = 1.2f;
32 
33         STArray<100, SkScalar> intervals;
34         for (SkScalar len = 1; len < kMaxDashLen; len *= kDashGrowth) {
35             intervals.push_back(len);
36             intervals.push_back(len);
37         }
38 
39         fDashPaint.setAntiAlias(true);
40         fDashPaint.setStyle(SkPaint::kStroke_Style);
41         fDashPaint.setStrokeWidth(6);
42         fDashPaint.setColor(0xff008000);
43         fDashPaint.setPathEffect(SkDashPathEffect::Make(intervals.begin(), intervals.size(), 0));
44 
45         fPointsPaint.setColor(0xff800000);
46         fPointsPaint.setStrokeWidth(3);
47 
48         fRect = SkRect::MakeLTRB(10, 10, 100, 70);
49     }
50 
getName() const51     SkString getName() const override { return SkString("contour_start"); }
52 
getISize()53     SkISize getISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
54 
onDraw(SkCanvas * canvas)55     void onDraw(SkCanvas* canvas) override {
56 
57         drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
58             return SkPath::Rect(rect, dir, startIndex);
59         });
60 
61         drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
62             return SkPath::Oval(rect, dir, startIndex);
63         });
64 
65         drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
66             SkRRect rrect;
67             const SkVector radii[4] = { {15, 15}, {15, 15}, {15, 15}, {15, 15}};
68             rrect.setRectRadii(rect, radii);
69             return SkPath::RRect(rrect, dir, startIndex);
70         });
71 
72         drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
73             SkRRect rrect;
74             rrect.setRect(rect);
75             return SkPath::RRect(rrect, dir, startIndex);
76         });
77 
78         drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
79             SkRRect rrect;
80             rrect.setOval(rect);
81             return SkPath::RRect(rrect, dir, startIndex);
82         });
83 
84     }
85 
86 private:
87     inline static constexpr int kImageWidth = 1200;
88     inline static constexpr int kImageHeight = 600;
89 
90     SkPaint fDashPaint, fPointsPaint;
91     SkRect  fRect;
92 
drawDirs(SkCanvas * canvas,SkPath (* makePath)(const SkRect &,SkPathDirection,unsigned)) const93     void drawDirs(SkCanvas* canvas,
94                   SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const {
95         drawOneColumn(canvas, SkPathDirection::kCW, makePath);
96         canvas->translate(kImageWidth / 10, 0);
97         drawOneColumn(canvas, SkPathDirection::kCCW, makePath);
98         canvas->translate(kImageWidth / 10, 0);
99     }
100 
drawOneColumn(SkCanvas * canvas,SkPathDirection dir,SkPath (* makePath)(const SkRect &,SkPathDirection,unsigned)) const101     void drawOneColumn(SkCanvas* canvas, SkPathDirection dir,
102                        SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const {
103         SkAutoCanvasRestore acr(canvas, true);
104 
105         for (unsigned i = 0; i < 8; ++i) {
106             const SkPath path = makePath(fRect, dir, i);
107             canvas->drawPath(path, fDashPaint);
108 
109             const int n = path.countPoints();
110             AutoTArray<SkPoint> points(n);
111             path.getPoints(points.get(), n);
112             canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), fPointsPaint);
113 
114             canvas->translate(0, kImageHeight / 8);
115         }
116     }
117 
118     using INHERITED = GM;
119 };
120 
121 DEF_GM( return new ContourStartGM(); )
122 
123 } // namespace skiagm
124