blob: 3faceefef3f4c26d789c48f97ac68c0ce927155e [file] [log] [blame]
reed@google.com8d850be2012-07-13 15:55:15 +00001/*
2 * Copyright 2012 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"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkPathEffect.h"
14#include "include/core/SkScalar.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
17#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/effects/SkDashPathEffect.h"
19#include "include/effects/SkTrimPathEffect.h"
20#include "include/private/SkTArray.h"
21#include "include/utils/SkParsePath.h"
Hal Canary41248072019-07-11 16:32:53 -040022#include "tools/timer/TimeUtils.h"
reed@google.com8d850be2012-07-13 15:55:15 +000023
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include <math.h>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040025#include <utility>
26
reed@google.com8d850be2012-07-13 15:55:15 +000027/*
28 * Inspired by http://code.google.com/p/chromium/issues/detail?id=112145
29 */
reeda4393342016-03-18 11:22:57 -070030static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2],
31 SkPaint::Join join) {
32 SkPaint paint;
33 paint.setAntiAlias(true);
Mike Reed19630092020-05-18 21:25:44 -040034 paint.setStroke(true);
reeda4393342016-03-18 11:22:57 -070035 paint.setStrokeJoin(join);
36 paint.setStrokeWidth(42);
37 canvas->drawPath(path, paint);
caryclark6df8e342015-02-23 12:47:03 -080038
reeda4393342016-03-18 11:22:57 -070039 paint.setColor(SK_ColorRED);
40 paint.setStrokeWidth(21);
41 paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
42 canvas->drawPath(path, paint);
caryclark6df8e342015-02-23 12:47:03 -080043
reeda4393342016-03-18 11:22:57 -070044 paint.setColor(SK_ColorGREEN);
45 paint.setPathEffect(nullptr);
46 paint.setStrokeWidth(0);
47 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070048}
reed@google.com8d850be2012-07-13 15:55:15 +000049
reed6dc14aa2016-04-11 07:46:38 -070050DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) {
reed@google.com8d850be2012-07-13 15:55:15 +000051 SkPath path;
52 const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212"
53 "C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231"
54 "C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250"
55 "C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269"
56 "C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288"
57 "C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269"
58 "C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250"
59 "C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231"
60 "C 283,231 344,195 338,98";
61
62 SkParsePath::FromSVGString(d, &path);
caryclark6df8e342015-02-23 12:47:03 -080063 canvas->translate(-35.f, -55.f);
64 for (int x = 0; x < 2; ++x) {
65 for (int y = 0; y < 2; ++y) {
66 canvas->save();
67 canvas->translate(x * 430.f, y * 355.f);
68 SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 };
69 flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join);
70 canvas->restore();
71 }
72 }
halcanary2a243382015-09-09 08:16:41 -070073}
Mike Reed41232232018-03-07 17:02:47 -050074
Mike Reed41232232018-03-07 17:02:47 -050075class TrimGM : public skiagm::GM {
76public:
Mike Reed7585a652020-10-30 16:08:11 -040077 TrimGM() {}
78
79 void onOnceBeforeDraw() override {
Florin Malita827af662018-03-09 16:08:58 -050080 SkAssertResult(SkParsePath::FromSVGString(
81 "M 0,100 C 10, 50 190, 50 200,100"
82 "M 200,100 C 210,150 390,150 400,100"
83 "M 400,100 C 390, 50 210, 50 200,100"
84 "M 200,100 C 190,150 10,150 0,100",
85 &fPaths.push_back()));
86
87 SkAssertResult(SkParsePath::FromSVGString(
88 "M 0, 75 L 200, 75"
89 "M 200, 91 L 200, 91"
90 "M 200,108 L 200,108"
91 "M 200,125 L 400,125",
92 &fPaths.push_back()));
93
94 SkAssertResult(SkParsePath::FromSVGString(
95 "M 0,100 L 50, 50"
96 "M 50, 50 L 150,150"
97 "M 150,150 L 250, 50"
98 "M 250, 50 L 350,150"
99 "M 350,150 L 400,100",
100 &fPaths.push_back()));
101
102 }
Mike Reed41232232018-03-07 17:02:47 -0500103
104protected:
105 SkString onShortName() override { return SkString("trimpatheffect"); }
106
Florin Malita827af662018-03-09 16:08:58 -0500107 SkISize onISize() override {
108 return SkISize::Make(1400, 1000);
109 }
Mike Reed41232232018-03-07 17:02:47 -0500110
111 void onDraw(SkCanvas* canvas) override {
Florin Malita827af662018-03-09 16:08:58 -0500112 static constexpr SkSize kCellSize = { 440, 150 };
113 static constexpr SkScalar kOffsets[][2] = {
114 { -0.33f, -0.66f },
115 { 0 , 1 },
116 { 0 , 0.25f},
117 { 0.25f, 0.75f},
118 { 0.75f, 1 },
119 { 1 , 0.75f},
120 };
Mike Reed41232232018-03-07 17:02:47 -0500121
Florin Malita827af662018-03-09 16:08:58 -0500122 SkPaint hairlinePaint;
123 hairlinePaint.setAntiAlias(true);
Mike Reed19630092020-05-18 21:25:44 -0400124 hairlinePaint.setStroke(true);
Florin Malita827af662018-03-09 16:08:58 -0500125 hairlinePaint.setStrokeCap(SkPaint::kRound_Cap);
126 hairlinePaint.setStrokeWidth(2);
127 SkPaint normalPaint = hairlinePaint;
128 normalPaint.setStrokeWidth(10);
129 normalPaint.setColor(0x8000ff00);
130 SkPaint invertedPaint = normalPaint;
131 invertedPaint.setColor(0x80ff0000);
Mike Reed41232232018-03-07 17:02:47 -0500132
Florin Malita827af662018-03-09 16:08:58 -0500133 for (const auto& offset : kOffsets) {
134 auto start = offset[0] + fOffset,
135 stop = offset[1] + fOffset;
136
137 auto normalMode = SkTrimPathEffect::Mode::kNormal,
138 invertedMode = SkTrimPathEffect::Mode::kInverted;
139 if (fOffset) {
140 start -= SkScalarFloorToScalar(start);
141 stop -= SkScalarFloorToScalar(stop);
142 if (start > stop) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400143 using std::swap;
144 swap(start, stop);
145 swap(normalMode, invertedMode);
Florin Malita827af662018-03-09 16:08:58 -0500146 }
147 }
148
149 normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode));
150 invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode));
151
152 {
153 SkAutoCanvasRestore acr(canvas, true);
154 for (const auto& path : fPaths) {
155 canvas->drawPath(path, normalPaint);
156 canvas->drawPath(path, invertedPaint);
157 canvas->drawPath(path, hairlinePaint);
158 canvas->translate(kCellSize.width(), 0);
159 }
160 }
161
162 canvas->translate(0, kCellSize.height());
163 }
Mike Reed41232232018-03-07 17:02:47 -0500164 }
165
Hal Canary41248072019-07-11 16:32:53 -0400166 bool onAnimate(double nanos) override {
167 fOffset = TimeUtils::NanosToMSec(nanos) / 2000.0f;
Florin Malita827af662018-03-09 16:08:58 -0500168 fOffset -= floorf(fOffset);
Mike Reed41232232018-03-07 17:02:47 -0500169 return true;
170 }
Florin Malita827af662018-03-09 16:08:58 -0500171
Mike Reed41232232018-03-07 17:02:47 -0500172private:
Florin Malita827af662018-03-09 16:08:58 -0500173 SkTArray<SkPath> fPaths;
174 SkScalar fOffset = 0;
175
John Stiles7571f9e2020-09-02 22:42:33 -0400176 using INHERITED = skiagm::GM;
Mike Reed41232232018-03-07 17:02:47 -0500177};
178DEF_GM(return new TrimGM;)
179