1 /*
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #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"
18 #include "include/effects/SkDashPathEffect.h"
19 #include "include/effects/SkTrimPathEffect.h"
20 #include "include/private/SkTArray.h"
21 #include "include/utils/SkParsePath.h"
22 #include "tools/timer/TimeUtils.h"
23
24 #include <math.h>
25 #include <utility>
26
27 /*
28 * Inspired by http://code.google.com/p/chromium/issues/detail?id=112145
29 */
flower(SkCanvas * canvas,const SkPath & path,SkScalar intervals[2],SkPaint::Join join)30 static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2],
31 SkPaint::Join join) {
32 SkPaint paint;
33 paint.setAntiAlias(true);
34 paint.setStroke(true);
35 paint.setStrokeJoin(join);
36 paint.setStrokeWidth(42);
37 canvas->drawPath(path, paint);
38
39 paint.setColor(SK_ColorRED);
40 paint.setStrokeWidth(21);
41 paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
42 canvas->drawPath(path, paint);
43
44 paint.setColor(SK_ColorGREEN);
45 paint.setPathEffect(nullptr);
46 paint.setStrokeWidth(0);
47 canvas->drawPath(path, paint);
48 }
49
50 DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) {
51 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);
63 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 }
73 }
74
75 class TrimGM : public skiagm::GM {
76 public:
TrimGM()77 TrimGM() {}
78
onOnceBeforeDraw()79 void onOnceBeforeDraw() override {
80 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 }
103
104 protected:
onShortName()105 SkString onShortName() override { return SkString("trimpatheffect"); }
106
onISize()107 SkISize onISize() override {
108 return SkISize::Make(1400, 1000);
109 }
110
onDraw(SkCanvas * canvas)111 void onDraw(SkCanvas* canvas) override {
112 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 };
121
122 SkPaint hairlinePaint;
123 hairlinePaint.setAntiAlias(true);
124 hairlinePaint.setStroke(true);
125 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);
132
133 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) {
143 using std::swap;
144 swap(start, stop);
145 swap(normalMode, invertedMode);
146 }
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 }
164 }
165
onAnimate(double nanos)166 bool onAnimate(double nanos) override {
167 fOffset = TimeUtils::NanosToMSec(nanos) / 2000.0f;
168 fOffset -= floorf(fOffset);
169 return true;
170 }
171
172 private:
173 SkTArray<SkPath> fPaths;
174 SkScalar fOffset = 0;
175
176 using INHERITED = skiagm::GM;
177 };
178 DEF_GM(return new TrimGM;)
179
180