blob: bfff3f36585ab14f320553a326769249a5a08947 [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
8#include "gm.h"
Florin Malita827af662018-03-09 16:08:58 -05009#include "SkAnimTimer.h"
reed@google.com8d850be2012-07-13 15:55:15 +000010#include "SkCanvas.h"
Florin Malita827af662018-03-09 16:08:58 -050011#include "SkDashPathEffect.h"
reed@google.com8d850be2012-07-13 15:55:15 +000012#include "SkPath.h"
13#include "SkParsePath.h"
Florin Malita827af662018-03-09 16:08:58 -050014#include "SkTArray.h"
15#include "SkTrimPathEffect.h"
reed@google.com8d850be2012-07-13 15:55:15 +000016
17/*
18 * Inspired by http://code.google.com/p/chromium/issues/detail?id=112145
19 */
reeda4393342016-03-18 11:22:57 -070020static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2],
21 SkPaint::Join join) {
22 SkPaint paint;
23 paint.setAntiAlias(true);
24 paint.setStyle(SkPaint::kStroke_Style);
25 paint.setStrokeJoin(join);
26 paint.setStrokeWidth(42);
27 canvas->drawPath(path, paint);
caryclark6df8e342015-02-23 12:47:03 -080028
reeda4393342016-03-18 11:22:57 -070029 paint.setColor(SK_ColorRED);
30 paint.setStrokeWidth(21);
31 paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
32 canvas->drawPath(path, paint);
caryclark6df8e342015-02-23 12:47:03 -080033
reeda4393342016-03-18 11:22:57 -070034 paint.setColor(SK_ColorGREEN);
35 paint.setPathEffect(nullptr);
36 paint.setStrokeWidth(0);
37 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070038}
reed@google.com8d850be2012-07-13 15:55:15 +000039
reed6dc14aa2016-04-11 07:46:38 -070040DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) {
reed@google.com8d850be2012-07-13 15:55:15 +000041 SkPath path;
42 const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212"
43 "C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231"
44 "C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250"
45 "C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269"
46 "C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288"
47 "C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269"
48 "C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250"
49 "C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231"
50 "C 283,231 344,195 338,98";
51
52 SkParsePath::FromSVGString(d, &path);
caryclark6df8e342015-02-23 12:47:03 -080053 canvas->translate(-35.f, -55.f);
54 for (int x = 0; x < 2; ++x) {
55 for (int y = 0; y < 2; ++y) {
56 canvas->save();
57 canvas->translate(x * 430.f, y * 355.f);
58 SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 };
59 flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join);
60 canvas->restore();
61 }
62 }
halcanary2a243382015-09-09 08:16:41 -070063}
Mike Reed41232232018-03-07 17:02:47 -050064
Mike Reed41232232018-03-07 17:02:47 -050065class TrimGM : public skiagm::GM {
66public:
Florin Malita827af662018-03-09 16:08:58 -050067 TrimGM() {
68 SkAssertResult(SkParsePath::FromSVGString(
69 "M 0,100 C 10, 50 190, 50 200,100"
70 "M 200,100 C 210,150 390,150 400,100"
71 "M 400,100 C 390, 50 210, 50 200,100"
72 "M 200,100 C 190,150 10,150 0,100",
73 &fPaths.push_back()));
74
75 SkAssertResult(SkParsePath::FromSVGString(
76 "M 0, 75 L 200, 75"
77 "M 200, 91 L 200, 91"
78 "M 200,108 L 200,108"
79 "M 200,125 L 400,125",
80 &fPaths.push_back()));
81
82 SkAssertResult(SkParsePath::FromSVGString(
83 "M 0,100 L 50, 50"
84 "M 50, 50 L 150,150"
85 "M 150,150 L 250, 50"
86 "M 250, 50 L 350,150"
87 "M 350,150 L 400,100",
88 &fPaths.push_back()));
89
90 }
Mike Reed41232232018-03-07 17:02:47 -050091
92protected:
93 SkString onShortName() override { return SkString("trimpatheffect"); }
94
Florin Malita827af662018-03-09 16:08:58 -050095 SkISize onISize() override {
96 return SkISize::Make(1400, 1000);
97 }
Mike Reed41232232018-03-07 17:02:47 -050098
99 void onDraw(SkCanvas* canvas) override {
Florin Malita827af662018-03-09 16:08:58 -0500100 static constexpr SkSize kCellSize = { 440, 150 };
101 static constexpr SkScalar kOffsets[][2] = {
102 { -0.33f, -0.66f },
103 { 0 , 1 },
104 { 0 , 0.25f},
105 { 0.25f, 0.75f},
106 { 0.75f, 1 },
107 { 1 , 0.75f},
108 };
Mike Reed41232232018-03-07 17:02:47 -0500109
Florin Malita827af662018-03-09 16:08:58 -0500110 SkPaint hairlinePaint;
111 hairlinePaint.setAntiAlias(true);
112 hairlinePaint.setStyle(SkPaint::kStroke_Style);
113 hairlinePaint.setStrokeCap(SkPaint::kRound_Cap);
114 hairlinePaint.setStrokeWidth(2);
115 SkPaint normalPaint = hairlinePaint;
116 normalPaint.setStrokeWidth(10);
117 normalPaint.setColor(0x8000ff00);
118 SkPaint invertedPaint = normalPaint;
119 invertedPaint.setColor(0x80ff0000);
Mike Reed41232232018-03-07 17:02:47 -0500120
Florin Malita827af662018-03-09 16:08:58 -0500121 for (const auto& offset : kOffsets) {
122 auto start = offset[0] + fOffset,
123 stop = offset[1] + fOffset;
124
125 auto normalMode = SkTrimPathEffect::Mode::kNormal,
126 invertedMode = SkTrimPathEffect::Mode::kInverted;
127 if (fOffset) {
128 start -= SkScalarFloorToScalar(start);
129 stop -= SkScalarFloorToScalar(stop);
130 if (start > stop) {
131 SkTSwap(start, stop);
132 SkTSwap(normalMode, invertedMode);
133 }
134 }
135
136 normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode));
137 invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode));
138
139 {
140 SkAutoCanvasRestore acr(canvas, true);
141 for (const auto& path : fPaths) {
142 canvas->drawPath(path, normalPaint);
143 canvas->drawPath(path, invertedPaint);
144 canvas->drawPath(path, hairlinePaint);
145 canvas->translate(kCellSize.width(), 0);
146 }
147 }
148
149 canvas->translate(0, kCellSize.height());
150 }
Mike Reed41232232018-03-07 17:02:47 -0500151 }
152
Florin Malita827af662018-03-09 16:08:58 -0500153 bool onAnimate(const SkAnimTimer& t) override {
154 fOffset = t.msec() / 2000.0f;
155 fOffset -= floorf(fOffset);
Mike Reed41232232018-03-07 17:02:47 -0500156 return true;
157 }
Florin Malita827af662018-03-09 16:08:58 -0500158
Mike Reed41232232018-03-07 17:02:47 -0500159private:
Florin Malita827af662018-03-09 16:08:58 -0500160 SkTArray<SkPath> fPaths;
161 SkScalar fOffset = 0;
162
Mike Reed41232232018-03-07 17:02:47 -0500163 typedef skiagm::GM INHERITED;
164};
165DEF_GM(return new TrimGM;)
166