Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dashcubics.cpp
Go to the documentation of this file.
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"
12#include "include/core/SkPath.h"
15#include "include/core/SkSize.h"
23
24#include <math.h>
25#include <utility>
26
27using namespace skia_private;
28
29/*
30 * Inspired by http://code.google.com/p/chromium/issues/detail?id=112145
31 */
32static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2],
33 SkPaint::Join join) {
35 paint.setAntiAlias(true);
36 paint.setStroke(true);
37 paint.setStrokeJoin(join);
38 paint.setStrokeWidth(42);
39 canvas->drawPath(path, paint);
40
41 paint.setColor(SK_ColorRED);
42 paint.setStrokeWidth(21);
43 paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
44 canvas->drawPath(path, paint);
45
46 paint.setColor(SK_ColorGREEN);
47 paint.setPathEffect(nullptr);
48 paint.setStrokeWidth(0);
49 canvas->drawPath(path, paint);
50}
51
52DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) {
53 SkPath path;
54 const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212"
55 "C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231"
56 "C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250"
57 "C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269"
58 "C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288"
59 "C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269"
60 "C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250"
61 "C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231"
62 "C 283,231 344,195 338,98";
63
65 canvas->translate(-35.f, -55.f);
66 for (int x = 0; x < 2; ++x) {
67 for (int y = 0; y < 2; ++y) {
68 canvas->save();
69 canvas->translate(x * 430.f, y * 355.f);
70 SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 };
71 flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join);
72 canvas->restore();
73 }
74 }
75}
76
77class TrimGM : public skiagm::GM {
78public:
79 TrimGM() {}
80
81 void onOnceBeforeDraw() override {
83 "M 0,100 C 10, 50 190, 50 200,100"
84 "M 200,100 C 210,150 390,150 400,100"
85 "M 400,100 C 390, 50 210, 50 200,100"
86 "M 200,100 C 190,150 10,150 0,100",
87 &fPaths.push_back()));
88
90 "M 0, 75 L 200, 75"
91 "M 200, 91 L 200, 91"
92 "M 200,108 L 200,108"
93 "M 200,125 L 400,125",
94 &fPaths.push_back()));
95
97 "M 0,100 L 50, 50"
98 "M 50, 50 L 150,150"
99 "M 150,150 L 250, 50"
100 "M 250, 50 L 350,150"
101 "M 350,150 L 400,100",
102 &fPaths.push_back()));
103
104 }
105
106protected:
107 SkString getName() const override { return SkString("trimpatheffect"); }
108
109 SkISize getISize() override { return SkISize::Make(1400, 1000); }
110
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,
139 if (fOffset) {
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
166 bool onAnimate(double nanos) override {
167 fOffset = TimeUtils::NanosToMSec(nanos) / 2000.0f;
168 fOffset -= floorf(fOffset);
169 return true;
170 }
171
172private:
173 TArray<SkPath> fPaths;
174 SkScalar fOffset = 0;
175
176 using INHERITED = skiagm::GM;
177};
178DEF_GM(return new TrimGM;)
179
#define SkAssertResult(cond)
Definition SkAssert.h:123
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
#define SkScalarFloorToScalar(x)
Definition SkScalar.h:30
void translate(SkScalar dx, SkScalar dy)
void drawPath(const SkPath &path, const SkPaint &paint)
static sk_sp< SkPathEffect > Make(const SkScalar intervals[], int count, SkScalar phase)
@ kRound_Cap
adds circle
Definition SkPaint.h:335
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition SkPaint.h:170
void setStrokeCap(Cap cap)
Definition SkPaint.cpp:179
void setPathEffect(sk_sp< SkPathEffect > pathEffect)
@ kDefault_Join
equivalent to kMiter_Join
Definition SkPaint.h:363
@ kRound_Join
adds circle
Definition SkPaint.h:360
void setStroke(bool)
Definition SkPaint.cpp:115
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
static bool FromSVGString(const char str[], SkPath *)
static sk_sp< SkPathEffect > Make(SkScalar startT, SkScalar stopT, Mode=Mode::kNormal)
bool onAnimate(double nanos) override
SkString getName() const override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
void onOnceBeforeDraw() override
const Paint & paint
static void flower(SkCanvas *canvas, const SkPath &path, SkScalar intervals[2], SkPaint::Join join)
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
float SkScalar
Definition extension.cpp:12
const int kCellSize
#define DEF_GM(CODE)
Definition gm.h:40
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
double y
double x
static SkMSec NanosToMSec(double nanos)
Definition TimeUtils.h:16
static void swap(TArray< T, M > &a, TArray< T, M > &b)
Definition SkTArray.h:737
Point offset
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20