blob: 7d3ff4842d837b68a2f50971a534b901716a5b20 [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) {
fmalitae55c3122015-11-19 09:47:12 -080059 SkPath path;
60 path.addRect(rect, dir, startIndex);
61 return path;
62 });
63
Mike Reed30bc5272019-11-22 18:34:02 +000064 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080065 SkPath path;
66 path.addOval(rect, dir, startIndex);
67 return path;
68 });
69
Mike Reed30bc5272019-11-22 18:34:02 +000070 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080071 SkRRect rrect;
72 const SkVector radii[4] = { {15, 15}, {15, 15}, {15, 15}, {15, 15}};
73 rrect.setRectRadii(rect, radii);
74
75 SkPath path;
76 path.addRRect(rrect, dir, startIndex);
77 return path;
78 });
79
Mike Reed30bc5272019-11-22 18:34:02 +000080 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080081 SkRRect rrect;
82 rrect.setRect(rect);
83
84 SkPath path;
85 path.addRRect(rrect, dir, startIndex);
86 return path;
87 });
88
Mike Reed30bc5272019-11-22 18:34:02 +000089 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080090 SkRRect rrect;
91 rrect.setOval(rect);
92
93 SkPath path;
94 path.addRRect(rrect, dir, startIndex);
95 return path;
96 });
97
98 }
99
100private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700101 static constexpr int kImageWidth = 1200;
102 static constexpr int kImageHeight = 600;
fmalitae55c3122015-11-19 09:47:12 -0800103
104 SkPaint fDashPaint, fPointsPaint;
105 SkRect fRect;
106
107 void drawDirs(SkCanvas* canvas,
Mike Reed30bc5272019-11-22 18:34:02 +0000108 SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const {
109 drawOneColumn(canvas, SkPathDirection::kCW, makePath);
fmalitae55c3122015-11-19 09:47:12 -0800110 canvas->translate(kImageWidth / 10, 0);
Mike Reed30bc5272019-11-22 18:34:02 +0000111 drawOneColumn(canvas, SkPathDirection::kCCW, makePath);
fmalitae55c3122015-11-19 09:47:12 -0800112 canvas->translate(kImageWidth / 10, 0);
113 }
114
Mike Reed30bc5272019-11-22 18:34:02 +0000115 void drawOneColumn(SkCanvas* canvas, SkPathDirection dir,
116 SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const {
fmalitae55c3122015-11-19 09:47:12 -0800117 SkAutoCanvasRestore acr(canvas, true);
118
119 for (unsigned i = 0; i < 8; ++i) {
120 const SkPath path = makePath(fRect, dir, i);
121 canvas->drawPath(path, fDashPaint);
122
123 const int n = path.countPoints();
124 SkAutoTArray<SkPoint> points(n);
125 path.getPoints(points.get(), n);
126 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), fPointsPaint);
127
128 canvas->translate(0, kImageHeight / 8);
129 }
130 }
131
132 typedef GM INHERITED;
133};
134
135DEF_GM( return new ContourStartGM(); )
136
137} // namespace skiagm