blob: f2df4895aa7d9f34eecc761bcd66bb564f44e4be [file] [log] [blame]
fmalitae55c3122015-11-19 09:47:12 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPaint.h"
11#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkPathEffect.h"
13#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/effects/SkDashPathEffect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/private/SkTArray.h"
21#include "include/private/SkTemplates.h"
fmalitae55c3122015-11-19 09:47:12 -080022
23namespace skiagm {
24
25class ContourStartGM : public GM {
26public:
27 ContourStartGM() {
28 const SkScalar kMaxDashLen = 100;
29 const SkScalar kDashGrowth = 1.2f;
30
31 SkSTArray<100, SkScalar> intervals;
32 for (SkScalar len = 1; len < kMaxDashLen; len *= kDashGrowth) {
33 intervals.push_back(len);
34 intervals.push_back(len);
35 }
36
fmalitae55c3122015-11-19 09:47:12 -080037 fDashPaint.setAntiAlias(true);
38 fDashPaint.setStyle(SkPaint::kStroke_Style);
39 fDashPaint.setStrokeWidth(6);
40 fDashPaint.setColor(0xff008000);
reeda4393342016-03-18 11:22:57 -070041 fDashPaint.setPathEffect(SkDashPathEffect::Make(intervals.begin(), intervals.count(), 0));
fmalitae55c3122015-11-19 09:47:12 -080042
43 fPointsPaint.setColor(0xff800000);
44 fPointsPaint.setStrokeWidth(3);
45
46 fRect = SkRect::MakeLTRB(10, 10, 100, 70);
47 }
48
49protected:
50 SkString onShortName() override {
51 return SkString("contour_start");
52 }
53
54 SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
55
56 void onDraw(SkCanvas* canvas) override {
57
Mike Reed30bc5272019-11-22 18:34:02 +000058 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
Mike Reed92f6eb12020-08-25 11:48:41 -040059 return SkPath::Rect(rect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080060 });
61
Mike Reed30bc5272019-11-22 18:34:02 +000062 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
Mike Reedad5494d2020-08-25 17:36:28 -040063 return SkPath::Oval(rect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080064 });
65
Mike Reed30bc5272019-11-22 18:34:02 +000066 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080067 SkRRect rrect;
68 const SkVector radii[4] = { {15, 15}, {15, 15}, {15, 15}, {15, 15}};
69 rrect.setRectRadii(rect, radii);
Mike Reedad5494d2020-08-25 17:36:28 -040070 return SkPath::RRect(rrect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080071 });
72
Mike Reed30bc5272019-11-22 18:34:02 +000073 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080074 SkRRect rrect;
75 rrect.setRect(rect);
Mike Reedad5494d2020-08-25 17:36:28 -040076 return SkPath::RRect(rrect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080077 });
78
Mike Reed30bc5272019-11-22 18:34:02 +000079 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080080 SkRRect rrect;
81 rrect.setOval(rect);
Mike Reedad5494d2020-08-25 17:36:28 -040082 return SkPath::RRect(rrect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080083 });
84
85 }
86
87private:
mtkleindbfd7ab2016-09-01 11:24:54 -070088 static constexpr int kImageWidth = 1200;
89 static constexpr int kImageHeight = 600;
fmalitae55c3122015-11-19 09:47:12 -080090
91 SkPaint fDashPaint, fPointsPaint;
92 SkRect fRect;
93
94 void drawDirs(SkCanvas* canvas,
Mike Reed30bc5272019-11-22 18:34:02 +000095 SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const {
96 drawOneColumn(canvas, SkPathDirection::kCW, makePath);
fmalitae55c3122015-11-19 09:47:12 -080097 canvas->translate(kImageWidth / 10, 0);
Mike Reed30bc5272019-11-22 18:34:02 +000098 drawOneColumn(canvas, SkPathDirection::kCCW, makePath);
fmalitae55c3122015-11-19 09:47:12 -080099 canvas->translate(kImageWidth / 10, 0);
100 }
101
Mike Reed30bc5272019-11-22 18:34:02 +0000102 void drawOneColumn(SkCanvas* canvas, SkPathDirection dir,
103 SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const {
fmalitae55c3122015-11-19 09:47:12 -0800104 SkAutoCanvasRestore acr(canvas, true);
105
106 for (unsigned i = 0; i < 8; ++i) {
107 const SkPath path = makePath(fRect, dir, i);
108 canvas->drawPath(path, fDashPaint);
109
110 const int n = path.countPoints();
111 SkAutoTArray<SkPoint> points(n);
112 path.getPoints(points.get(), n);
113 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), fPointsPaint);
114
115 canvas->translate(0, kImageHeight / 8);
116 }
117 }
118
John Stiles7571f9e2020-09-02 22:42:33 -0400119 using INHERITED = GM;
fmalitae55c3122015-11-19 09:47:12 -0800120};
121
122DEF_GM( return new ContourStartGM(); )
123
124} // namespace skiagm