blob: 73a17d231dec0fdfbdb8dcf7141b4b82000c5ad1 [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"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050020#include "include/private/base/SkTArray.h"
Kevin Lubick46572b42023-01-18 13:11:06 -050021#include "include/private/base/SkTemplates.h"
fmalitae55c3122015-11-19 09:47:12 -080022
Herb Derby3b3bcf02023-01-17 15:12:15 -050023using namespace skia_private;
24
fmalitae55c3122015-11-19 09:47:12 -080025namespace skiagm {
26
27class ContourStartGM : public GM {
John Stilese6a05a42020-11-05 10:05:19 -050028protected:
29 void onOnceBeforeDraw() override {
fmalitae55c3122015-11-19 09:47:12 -080030 const SkScalar kMaxDashLen = 100;
31 const SkScalar kDashGrowth = 1.2f;
32
Herb Derbye324ad72023-04-07 13:10:31 -060033 STArray<100, SkScalar> intervals;
fmalitae55c3122015-11-19 09:47:12 -080034 for (SkScalar len = 1; len < kMaxDashLen; len *= kDashGrowth) {
35 intervals.push_back(len);
36 intervals.push_back(len);
37 }
38
fmalitae55c3122015-11-19 09:47:12 -080039 fDashPaint.setAntiAlias(true);
40 fDashPaint.setStyle(SkPaint::kStroke_Style);
41 fDashPaint.setStrokeWidth(6);
42 fDashPaint.setColor(0xff008000);
Herb Derbyffacce52022-11-09 10:51:34 -050043 fDashPaint.setPathEffect(SkDashPathEffect::Make(intervals.begin(), intervals.size(), 0));
fmalitae55c3122015-11-19 09:47:12 -080044
45 fPointsPaint.setColor(0xff800000);
46 fPointsPaint.setStrokeWidth(3);
47
48 fRect = SkRect::MakeLTRB(10, 10, 100, 70);
49 }
50
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000051 SkString getName() const override { return SkString("contour_start"); }
fmalitae55c3122015-11-19 09:47:12 -080052
Leandro Lovisolo8f023882023-08-15 21:13:52 +000053 SkISize getISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
fmalitae55c3122015-11-19 09:47:12 -080054
55 void onDraw(SkCanvas* canvas) override {
56
Mike Reed30bc5272019-11-22 18:34:02 +000057 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
Mike Reed92f6eb12020-08-25 11:48:41 -040058 return SkPath::Rect(rect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080059 });
60
Mike Reed30bc5272019-11-22 18:34:02 +000061 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
Mike Reedad5494d2020-08-25 17:36:28 -040062 return SkPath::Oval(rect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080063 });
64
Mike Reed30bc5272019-11-22 18:34:02 +000065 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080066 SkRRect rrect;
67 const SkVector radii[4] = { {15, 15}, {15, 15}, {15, 15}, {15, 15}};
68 rrect.setRectRadii(rect, radii);
Mike Reedad5494d2020-08-25 17:36:28 -040069 return SkPath::RRect(rrect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080070 });
71
Mike Reed30bc5272019-11-22 18:34:02 +000072 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080073 SkRRect rrect;
74 rrect.setRect(rect);
Mike Reedad5494d2020-08-25 17:36:28 -040075 return SkPath::RRect(rrect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080076 });
77
Mike Reed30bc5272019-11-22 18:34:02 +000078 drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) {
fmalitae55c3122015-11-19 09:47:12 -080079 SkRRect rrect;
80 rrect.setOval(rect);
Mike Reedad5494d2020-08-25 17:36:28 -040081 return SkPath::RRect(rrect, dir, startIndex);
fmalitae55c3122015-11-19 09:47:12 -080082 });
83
84 }
85
86private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -040087 inline static constexpr int kImageWidth = 1200;
88 inline static constexpr int kImageHeight = 600;
fmalitae55c3122015-11-19 09:47:12 -080089
90 SkPaint fDashPaint, fPointsPaint;
91 SkRect fRect;
92
93 void drawDirs(SkCanvas* canvas,
Mike Reed30bc5272019-11-22 18:34:02 +000094 SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const {
95 drawOneColumn(canvas, SkPathDirection::kCW, makePath);
fmalitae55c3122015-11-19 09:47:12 -080096 canvas->translate(kImageWidth / 10, 0);
Mike Reed30bc5272019-11-22 18:34:02 +000097 drawOneColumn(canvas, SkPathDirection::kCCW, makePath);
fmalitae55c3122015-11-19 09:47:12 -080098 canvas->translate(kImageWidth / 10, 0);
99 }
100
Mike Reed30bc5272019-11-22 18:34:02 +0000101 void drawOneColumn(SkCanvas* canvas, SkPathDirection dir,
102 SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const {
fmalitae55c3122015-11-19 09:47:12 -0800103 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();
Herb Derby3b3bcf02023-01-17 15:12:15 -0500110 AutoTArray<SkPoint> points(n);
fmalitae55c3122015-11-19 09:47:12 -0800111 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
John Stiles7571f9e2020-09-02 22:42:33 -0400118 using INHERITED = GM;
fmalitae55c3122015-11-19 09:47:12 -0800119};
120
121DEF_GM( return new ContourStartGM(); )
122
123} // namespace skiagm