fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkPathEffect.h" |
| 13 | #include "include/core/SkPoint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkRRect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkScalar.h" |
| 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/effects/SkDashPathEffect.h" |
Kevin Lubick | dc6cc02 | 2023-01-13 11:24:27 -0500 | [diff] [blame] | 20 | #include "include/private/base/SkTArray.h" |
Kevin Lubick | 46572b4 | 2023-01-18 13:11:06 -0500 | [diff] [blame] | 21 | #include "include/private/base/SkTemplates.h" |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 22 | |
Herb Derby | 3b3bcf0 | 2023-01-17 15:12:15 -0500 | [diff] [blame] | 23 | using namespace skia_private; |
| 24 | |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 25 | namespace skiagm { |
| 26 | |
| 27 | class ContourStartGM : public GM { |
John Stiles | e6a05a4 | 2020-11-05 10:05:19 -0500 | [diff] [blame] | 28 | protected: |
| 29 | void onOnceBeforeDraw() override { |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 30 | const SkScalar kMaxDashLen = 100; |
| 31 | const SkScalar kDashGrowth = 1.2f; |
| 32 | |
Herb Derby | e324ad7 | 2023-04-07 13:10:31 -0600 | [diff] [blame] | 33 | STArray<100, SkScalar> intervals; |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 34 | for (SkScalar len = 1; len < kMaxDashLen; len *= kDashGrowth) { |
| 35 | intervals.push_back(len); |
| 36 | intervals.push_back(len); |
| 37 | } |
| 38 | |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 39 | fDashPaint.setAntiAlias(true); |
| 40 | fDashPaint.setStyle(SkPaint::kStroke_Style); |
| 41 | fDashPaint.setStrokeWidth(6); |
| 42 | fDashPaint.setColor(0xff008000); |
Herb Derby | ffacce5 | 2022-11-09 10:51:34 -0500 | [diff] [blame] | 43 | fDashPaint.setPathEffect(SkDashPathEffect::Make(intervals.begin(), intervals.size(), 0)); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 44 | |
| 45 | fPointsPaint.setColor(0xff800000); |
| 46 | fPointsPaint.setStrokeWidth(3); |
| 47 | |
| 48 | fRect = SkRect::MakeLTRB(10, 10, 100, 70); |
| 49 | } |
| 50 | |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 51 | SkString getName() const override { return SkString("contour_start"); } |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 52 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 53 | SkISize getISize() override { return SkISize::Make(kImageWidth, kImageHeight); } |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 54 | |
| 55 | void onDraw(SkCanvas* canvas) override { |
| 56 | |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 57 | drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) { |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 58 | return SkPath::Rect(rect, dir, startIndex); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 59 | }); |
| 60 | |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 61 | drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) { |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 62 | return SkPath::Oval(rect, dir, startIndex); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 63 | }); |
| 64 | |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 65 | drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) { |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 66 | SkRRect rrect; |
| 67 | const SkVector radii[4] = { {15, 15}, {15, 15}, {15, 15}, {15, 15}}; |
| 68 | rrect.setRectRadii(rect, radii); |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 69 | return SkPath::RRect(rrect, dir, startIndex); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 70 | }); |
| 71 | |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 72 | drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) { |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 73 | SkRRect rrect; |
| 74 | rrect.setRect(rect); |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 75 | return SkPath::RRect(rrect, dir, startIndex); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 76 | }); |
| 77 | |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 78 | drawDirs(canvas, [](const SkRect& rect, SkPathDirection dir, unsigned startIndex) { |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 79 | SkRRect rrect; |
| 80 | rrect.setOval(rect); |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 81 | return SkPath::RRect(rrect, dir, startIndex); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 82 | }); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 87 | inline static constexpr int kImageWidth = 1200; |
| 88 | inline static constexpr int kImageHeight = 600; |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 89 | |
| 90 | SkPaint fDashPaint, fPointsPaint; |
| 91 | SkRect fRect; |
| 92 | |
| 93 | void drawDirs(SkCanvas* canvas, |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 94 | SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const { |
| 95 | drawOneColumn(canvas, SkPathDirection::kCW, makePath); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 96 | canvas->translate(kImageWidth / 10, 0); |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 97 | drawOneColumn(canvas, SkPathDirection::kCCW, makePath); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 98 | canvas->translate(kImageWidth / 10, 0); |
| 99 | } |
| 100 | |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 101 | void drawOneColumn(SkCanvas* canvas, SkPathDirection dir, |
| 102 | SkPath (*makePath)(const SkRect&, SkPathDirection, unsigned)) const { |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 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(); |
Herb Derby | 3b3bcf0 | 2023-01-17 15:12:15 -0500 | [diff] [blame] | 110 | AutoTArray<SkPoint> points(n); |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 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 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 118 | using INHERITED = GM; |
fmalita | e55c312 | 2015-11-19 09:47:12 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | DEF_GM( return new ContourStartGM(); ) |
| 122 | |
| 123 | } // namespace skiagm |