blob: 4c1f028326025f047710c347bd7318f71958708e [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
Ben Wagnerf08d1d02018-06-18 15:11:00 -040017#include <utility>
18
reed@google.com8d850be2012-07-13 15:55:15 +000019/*
20 * Inspired by http://code.google.com/p/chromium/issues/detail?id=112145
21 */
reeda4393342016-03-18 11:22:57 -070022static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2],
23 SkPaint::Join join) {
24 SkPaint paint;
25 paint.setAntiAlias(true);
26 paint.setStyle(SkPaint::kStroke_Style);
27 paint.setStrokeJoin(join);
28 paint.setStrokeWidth(42);
29 canvas->drawPath(path, paint);
caryclark6df8e342015-02-23 12:47:03 -080030
reeda4393342016-03-18 11:22:57 -070031 paint.setColor(SK_ColorRED);
32 paint.setStrokeWidth(21);
33 paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
34 canvas->drawPath(path, paint);
caryclark6df8e342015-02-23 12:47:03 -080035
reeda4393342016-03-18 11:22:57 -070036 paint.setColor(SK_ColorGREEN);
37 paint.setPathEffect(nullptr);
38 paint.setStrokeWidth(0);
39 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070040}
reed@google.com8d850be2012-07-13 15:55:15 +000041
reed6dc14aa2016-04-11 07:46:38 -070042DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) {
reed@google.com8d850be2012-07-13 15:55:15 +000043 SkPath path;
44 const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212"
45 "C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231"
46 "C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250"
47 "C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269"
48 "C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288"
49 "C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269"
50 "C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250"
51 "C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231"
52 "C 283,231 344,195 338,98";
53
54 SkParsePath::FromSVGString(d, &path);
caryclark6df8e342015-02-23 12:47:03 -080055 canvas->translate(-35.f, -55.f);
56 for (int x = 0; x < 2; ++x) {
57 for (int y = 0; y < 2; ++y) {
58 canvas->save();
59 canvas->translate(x * 430.f, y * 355.f);
60 SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 };
61 flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join);
62 canvas->restore();
63 }
64 }
halcanary2a243382015-09-09 08:16:41 -070065}
Mike Reed41232232018-03-07 17:02:47 -050066
Mike Reed41232232018-03-07 17:02:47 -050067class TrimGM : public skiagm::GM {
68public:
Florin Malita827af662018-03-09 16:08:58 -050069 TrimGM() {
70 SkAssertResult(SkParsePath::FromSVGString(
71 "M 0,100 C 10, 50 190, 50 200,100"
72 "M 200,100 C 210,150 390,150 400,100"
73 "M 400,100 C 390, 50 210, 50 200,100"
74 "M 200,100 C 190,150 10,150 0,100",
75 &fPaths.push_back()));
76
77 SkAssertResult(SkParsePath::FromSVGString(
78 "M 0, 75 L 200, 75"
79 "M 200, 91 L 200, 91"
80 "M 200,108 L 200,108"
81 "M 200,125 L 400,125",
82 &fPaths.push_back()));
83
84 SkAssertResult(SkParsePath::FromSVGString(
85 "M 0,100 L 50, 50"
86 "M 50, 50 L 150,150"
87 "M 150,150 L 250, 50"
88 "M 250, 50 L 350,150"
89 "M 350,150 L 400,100",
90 &fPaths.push_back()));
91
92 }
Mike Reed41232232018-03-07 17:02:47 -050093
94protected:
95 SkString onShortName() override { return SkString("trimpatheffect"); }
96
Florin Malita827af662018-03-09 16:08:58 -050097 SkISize onISize() override {
98 return SkISize::Make(1400, 1000);
99 }
Mike Reed41232232018-03-07 17:02:47 -0500100
101 void onDraw(SkCanvas* canvas) override {
Florin Malita827af662018-03-09 16:08:58 -0500102 static constexpr SkSize kCellSize = { 440, 150 };
103 static constexpr SkScalar kOffsets[][2] = {
104 { -0.33f, -0.66f },
105 { 0 , 1 },
106 { 0 , 0.25f},
107 { 0.25f, 0.75f},
108 { 0.75f, 1 },
109 { 1 , 0.75f},
110 };
Mike Reed41232232018-03-07 17:02:47 -0500111
Florin Malita827af662018-03-09 16:08:58 -0500112 SkPaint hairlinePaint;
113 hairlinePaint.setAntiAlias(true);
114 hairlinePaint.setStyle(SkPaint::kStroke_Style);
115 hairlinePaint.setStrokeCap(SkPaint::kRound_Cap);
116 hairlinePaint.setStrokeWidth(2);
117 SkPaint normalPaint = hairlinePaint;
118 normalPaint.setStrokeWidth(10);
119 normalPaint.setColor(0x8000ff00);
120 SkPaint invertedPaint = normalPaint;
121 invertedPaint.setColor(0x80ff0000);
Mike Reed41232232018-03-07 17:02:47 -0500122
Florin Malita827af662018-03-09 16:08:58 -0500123 for (const auto& offset : kOffsets) {
124 auto start = offset[0] + fOffset,
125 stop = offset[1] + fOffset;
126
127 auto normalMode = SkTrimPathEffect::Mode::kNormal,
128 invertedMode = SkTrimPathEffect::Mode::kInverted;
129 if (fOffset) {
130 start -= SkScalarFloorToScalar(start);
131 stop -= SkScalarFloorToScalar(stop);
132 if (start > stop) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400133 using std::swap;
134 swap(start, stop);
135 swap(normalMode, invertedMode);
Florin Malita827af662018-03-09 16:08:58 -0500136 }
137 }
138
139 normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode));
140 invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode));
141
142 {
143 SkAutoCanvasRestore acr(canvas, true);
144 for (const auto& path : fPaths) {
145 canvas->drawPath(path, normalPaint);
146 canvas->drawPath(path, invertedPaint);
147 canvas->drawPath(path, hairlinePaint);
148 canvas->translate(kCellSize.width(), 0);
149 }
150 }
151
152 canvas->translate(0, kCellSize.height());
153 }
Mike Reed41232232018-03-07 17:02:47 -0500154 }
155
Florin Malita827af662018-03-09 16:08:58 -0500156 bool onAnimate(const SkAnimTimer& t) override {
157 fOffset = t.msec() / 2000.0f;
158 fOffset -= floorf(fOffset);
Mike Reed41232232018-03-07 17:02:47 -0500159 return true;
160 }
Florin Malita827af662018-03-09 16:08:58 -0500161
Mike Reed41232232018-03-07 17:02:47 -0500162private:
Florin Malita827af662018-03-09 16:08:58 -0500163 SkTArray<SkPath> fPaths;
164 SkScalar fOffset = 0;
165
Mike Reed41232232018-03-07 17:02:47 -0500166 typedef skiagm::GM INHERITED;
167};
168DEF_GM(return new TrimGM;)
169