blob: 2d2157f7c3d8bc71a739f0dfc4a9540ec6bd1be8 [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);
34 paint.setStyle(SkPaint::kStroke_Style);
35 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:
Florin Malita827af662018-03-09 16:08:58 -050077 TrimGM() {
78 SkAssertResult(SkParsePath::FromSVGString(
79 "M 0,100 C 10, 50 190, 50 200,100"
80 "M 200,100 C 210,150 390,150 400,100"
81 "M 400,100 C 390, 50 210, 50 200,100"
82 "M 200,100 C 190,150 10,150 0,100",
83 &fPaths.push_back()));
84
85 SkAssertResult(SkParsePath::FromSVGString(
86 "M 0, 75 L 200, 75"
87 "M 200, 91 L 200, 91"
88 "M 200,108 L 200,108"
89 "M 200,125 L 400,125",
90 &fPaths.push_back()));
91
92 SkAssertResult(SkParsePath::FromSVGString(
93 "M 0,100 L 50, 50"
94 "M 50, 50 L 150,150"
95 "M 150,150 L 250, 50"
96 "M 250, 50 L 350,150"
97 "M 350,150 L 400,100",
98 &fPaths.push_back()));
99
100 }
Mike Reed41232232018-03-07 17:02:47 -0500101
102protected:
103 SkString onShortName() override { return SkString("trimpatheffect"); }
104
Florin Malita827af662018-03-09 16:08:58 -0500105 SkISize onISize() override {
106 return SkISize::Make(1400, 1000);
107 }
Mike Reed41232232018-03-07 17:02:47 -0500108
109 void onDraw(SkCanvas* canvas) override {
Florin Malita827af662018-03-09 16:08:58 -0500110 static constexpr SkSize kCellSize = { 440, 150 };
111 static constexpr SkScalar kOffsets[][2] = {
112 { -0.33f, -0.66f },
113 { 0 , 1 },
114 { 0 , 0.25f},
115 { 0.25f, 0.75f},
116 { 0.75f, 1 },
117 { 1 , 0.75f},
118 };
Mike Reed41232232018-03-07 17:02:47 -0500119
Florin Malita827af662018-03-09 16:08:58 -0500120 SkPaint hairlinePaint;
121 hairlinePaint.setAntiAlias(true);
122 hairlinePaint.setStyle(SkPaint::kStroke_Style);
123 hairlinePaint.setStrokeCap(SkPaint::kRound_Cap);
124 hairlinePaint.setStrokeWidth(2);
125 SkPaint normalPaint = hairlinePaint;
126 normalPaint.setStrokeWidth(10);
127 normalPaint.setColor(0x8000ff00);
128 SkPaint invertedPaint = normalPaint;
129 invertedPaint.setColor(0x80ff0000);
Mike Reed41232232018-03-07 17:02:47 -0500130
Florin Malita827af662018-03-09 16:08:58 -0500131 for (const auto& offset : kOffsets) {
132 auto start = offset[0] + fOffset,
133 stop = offset[1] + fOffset;
134
135 auto normalMode = SkTrimPathEffect::Mode::kNormal,
136 invertedMode = SkTrimPathEffect::Mode::kInverted;
137 if (fOffset) {
138 start -= SkScalarFloorToScalar(start);
139 stop -= SkScalarFloorToScalar(stop);
140 if (start > stop) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400141 using std::swap;
142 swap(start, stop);
143 swap(normalMode, invertedMode);
Florin Malita827af662018-03-09 16:08:58 -0500144 }
145 }
146
147 normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode));
148 invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode));
149
150 {
151 SkAutoCanvasRestore acr(canvas, true);
152 for (const auto& path : fPaths) {
153 canvas->drawPath(path, normalPaint);
154 canvas->drawPath(path, invertedPaint);
155 canvas->drawPath(path, hairlinePaint);
156 canvas->translate(kCellSize.width(), 0);
157 }
158 }
159
160 canvas->translate(0, kCellSize.height());
161 }
Mike Reed41232232018-03-07 17:02:47 -0500162 }
163
Hal Canary41248072019-07-11 16:32:53 -0400164 bool onAnimate(double nanos) override {
165 fOffset = TimeUtils::NanosToMSec(nanos) / 2000.0f;
Florin Malita827af662018-03-09 16:08:58 -0500166 fOffset -= floorf(fOffset);
Mike Reed41232232018-03-07 17:02:47 -0500167 return true;
168 }
Florin Malita827af662018-03-09 16:08:58 -0500169
Mike Reed41232232018-03-07 17:02:47 -0500170private:
Florin Malita827af662018-03-09 16:08:58 -0500171 SkTArray<SkPath> fPaths;
172 SkScalar fOffset = 0;
173
Mike Reed41232232018-03-07 17:02:47 -0500174 typedef skiagm::GM INHERITED;
175};
176DEF_GM(return new TrimGM;)
177