| Flutter Impeller |

path_component.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_
7
8 #include <functional>
9 #include <optional>
10 #include <type_traits>
11 #include <vector>
12
13 #include "impeller/geometry/point.h"
14 #include "impeller/geometry/scalar.h"
15
16 namespace impeller {
17
18 /// @brief An interface for generating a multi contour polyline as a triangle
19 /// strip.
20 class VertexWriter {
21 public:
22 virtual void EndContour() = 0;
23
24 virtual void Write(Point point) = 0;
25 };
26
27 /// @brief A vertex writer that generates a triangle fan and requires primitive
28 /// restart.
29 class FanVertexWriter : public VertexWriter {
30 public:
31 explicit FanVertexWriter(Point* point_buffer, uint16_t* index_buffer);
32
33 ~FanVertexWriter();
34
35 size_t GetIndexCount() const;
36
37 void EndContour() override;
38
39 void Write(Point point) override;
40
41 private:
42 size_t count_ = 0;
43 size_t index_count_ = 0;
44 Point* point_buffer_ = nullptr;
45 uint16_t* index_buffer_ = nullptr;
46 };
47
48 /// @brief A vertex writer that generates a triangle strip and requires
49 /// primitive restart.
50 class StripVertexWriter : public VertexWriter {
51 public:
52 explicit StripVertexWriter(Point* point_buffer, uint16_t* index_buffer);
53
54 ~StripVertexWriter();
55
56 size_t GetIndexCount() const;
57
58 void EndContour() override;
59
60 void Write(Point point) override;
61
62 private:
63 size_t count_ = 0;
64 size_t index_count_ = 0;
65 size_t contour_start_ = 0;
66 Point* point_buffer_ = nullptr;
67 uint16_t* index_buffer_ = nullptr;
68 };
69
70 /// @brief A vertex writer that generates a line strip topology.
71 class LineStripVertexWriter : public VertexWriter {
72 public:
73 explicit LineStripVertexWriter(std::vector<Point>& points);
74
75 ~LineStripVertexWriter() = default;
76
77 void EndContour() override;
78
79 void Write(Point point) override;
80
81 std::pair<size_t, size_t> GetVertexCount() const;
82
83 const std::vector<Point>& GetOversizedBuffer() const;
84
85 private:
86 size_t offset_ = 0u;
87 std::vector<Point>& points_;
88 std::vector<Point> overflow_;
89 };
90
91 /// @brief A vertex writer that has no hardware requirements.
92 class GLESVertexWriter : public VertexWriter {
93 public:
94 explicit GLESVertexWriter(std::vector<Point>& points,
95 std::vector<uint16_t>& indices);
96
97 ~GLESVertexWriter() = default;
98
99 void EndContour() override;
100
101 void Write(Point point) override;
102
103 private:
104 bool previous_contour_odd_points_ = false;
105 size_t contour_start_ = 0u;
106 std::vector<Point>& points_;
107 std::vector<uint16_t>& indices_;
108 };
109
110 struct LinearPathComponent {
111 Point p1;
112 Point p2;
113
114 LinearPathComponent() {}
115
116 LinearPathComponent(Point ap1, Point ap2) : p1(ap1), p2(ap2) {}
117
118 Point Solve(Scalar time) const;
119
120 void AppendPolylinePoints(std::vector<Point>& points) const;
121
122 std::vector<Point> Extrema() const;
123
124 bool operator==(const LinearPathComponent& other) const {
125 return p1 == other.p1 && p2 == other.p2;
126 }
127
128 std::optional<Vector2> GetStartDirection() const;
129
130 std::optional<Vector2> GetEndDirection() const;
131 };
132
133 // A component that represets a Quadratic Bézier curve.
134 struct QuadraticPathComponent {
135 // Start point.
136 Point p1;
137 // Control point.
138 Point cp;
139 // End point.
140 Point p2;
141
142 QuadraticPathComponent() {}
143
144 QuadraticPathComponent(Point ap1, Point acp, Point ap2)
145 : p1(ap1), cp(acp), p2(ap2) {}
146
147 Point Solve(Scalar time) const;
148
149 Point SolveDerivative(Scalar time) const;
150
151 void AppendPolylinePoints(Scalar scale_factor,
152 std::vector<Point>& points) const;
153
154 using PointProc = std::function<void(const Point& point)>;
155
156 void ToLinearPathComponents(Scalar scale_factor, const PointProc& proc) const;
157
158 void ToLinearPathComponents(Scalar scale, VertexWriter& writer) const;
159
160 size_t CountLinearPathComponents(Scalar scale) const;
161
162 std::vector<Point> Extrema() const;
163
164 bool operator==(const QuadraticPathComponent& other) const {
165 return p1 == other.p1 && cp == other.cp && p2 == other.p2;
166 }
167
168 std::optional<Vector2> GetStartDirection() const;
169
170 std::optional<Vector2> GetEndDirection() const;
171 };
172
173 // A component that represets a Cubic Bézier curve.
174 struct CubicPathComponent {
175 // Start point.
176 Point p1;
177 // The first control point.
178 Point cp1;
179 // The second control point.
180 Point cp2;
181 // End point.
182 Point p2;
183
184 CubicPathComponent() {}
185
186 explicit CubicPathComponent(const QuadraticPathComponent& q)
187 : p1(q.p1),
188 cp1(q.p1 + (q.cp - q.p1) * (2.0 / 3.0)),
189 cp2(q.p2 + (q.cp - q.p2) * (2.0 / 3.0)),
190 p2(q.p2) {}
191
192 CubicPathComponent(Point ap1, Point acp1, Point acp2, Point ap2)
193 : p1(ap1), cp1(acp1), cp2(acp2), p2(ap2) {}
194
195 Point Solve(Scalar time) const;
196
197 Point SolveDerivative(Scalar time) const;
198
199 void AppendPolylinePoints(Scalar scale, std::vector<Point>& points) const;
200
201 std::vector<Point> Extrema() const;
202
203 using PointProc = std::function<void(const Point& point)>;
204
205 void ToLinearPathComponents(Scalar scale, const PointProc& proc) const;
206
207 void ToLinearPathComponents(Scalar scale, VertexWriter& writer) const;
208
209 size_t CountLinearPathComponents(Scalar scale) const;
210
211 CubicPathComponent Subsegment(Scalar t0, Scalar t1) const;
212
213 bool operator==(const CubicPathComponent& other) const {
214 return p1 == other.p1 && cp1 == other.cp1 && cp2 == other.cp2 &&
215 p2 == other.p2;
216 }
217
218 std::optional<Vector2> GetStartDirection() const;
219
220 std::optional<Vector2> GetEndDirection() const;
221
222 private:
223 QuadraticPathComponent Lower() const;
224 };
225
226 struct ContourComponent {
227 Point destination;
228
229 // 0, 0 for closed, anything else for open.
230 Point closed = Point(1, 1);
231
232 ContourComponent() {}
233
234 constexpr bool IsClosed() const { return closed == Point{0, 0}; }
235
236 explicit ContourComponent(Point p, Point closed)
237 : destination(p), closed(closed) {}
238
239 bool operator==(const ContourComponent& other) const {
240 return destination == other.destination && IsClosed() == other.IsClosed();
241 }
242 };
243
244 static_assert(!std::is_polymorphic<LinearPathComponent>::value);
245 static_assert(!std::is_polymorphic<QuadraticPathComponent>::value);
246 static_assert(!std::is_polymorphic<CubicPathComponent>::value);
247
248 } // namespace impeller
249
250 #endif // FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_
impeller::FanVertexWriter
A vertex writer that generates a triangle fan and requires primitive restart.
Definition: path_component.h:29
impeller::FanVertexWriter::EndContour
void EndContour() override
Definition: path_component.cc:26
impeller::FanVertexWriter::GetIndexCount
size_t GetIndexCount() const
Definition: path_component.cc:22
impeller::FanVertexWriter::Write
void Write(Point point) override
Definition: path_component.cc:33
impeller::FanVertexWriter::~FanVertexWriter
~FanVertexWriter()
impeller::FanVertexWriter::FanVertexWriter
FanVertexWriter(Point *point_buffer, uint16_t *index_buffer)
Definition: path_component.cc:17
impeller::GLESVertexWriter
A vertex writer that has no hardware requirements.
Definition: path_component.h:92
impeller::GLESVertexWriter::GLESVertexWriter
GLESVertexWriter(std::vector< Point > &points, std::vector< uint16_t > &indices)
Definition: path_component.cc:106
impeller::GLESVertexWriter::Write
void Write(Point point) override
Definition: path_component.cc:160
impeller::GLESVertexWriter::~GLESVertexWriter
~GLESVertexWriter()=default
impeller::GLESVertexWriter::EndContour
void EndContour() override
Definition: path_component.cc:110
impeller::LineStripVertexWriter
A vertex writer that generates a line strip topology.
Definition: path_component.h:71
impeller::LineStripVertexWriter::GetVertexCount
std::pair< size_t, size_t > GetVertexCount() const
Definition: path_component.cc:100
impeller::LineStripVertexWriter::LineStripVertexWriter
LineStripVertexWriter(std::vector< Point > &points)
Definition: path_component.cc:83
impeller::LineStripVertexWriter::Write
void Write(Point point) override
Definition: path_component.cc:88
impeller::LineStripVertexWriter::EndContour
void EndContour() override
Definition: path_component.cc:86
impeller::LineStripVertexWriter::~LineStripVertexWriter
~LineStripVertexWriter()=default
impeller::LineStripVertexWriter::GetOversizedBuffer
const std::vector< Point > & GetOversizedBuffer() const
Definition: path_component.cc:96
impeller::StripVertexWriter
A vertex writer that generates a triangle strip and requires primitive restart.
Definition: path_component.h:50
impeller::StripVertexWriter::Write
void Write(Point point) override
Definition: path_component.cc:77
impeller::StripVertexWriter::~StripVertexWriter
~StripVertexWriter()
impeller::StripVertexWriter::GetIndexCount
size_t GetIndexCount() const
Definition: path_component.cc:46
impeller::StripVertexWriter::StripVertexWriter
StripVertexWriter(Point *point_buffer, uint16_t *index_buffer)
Definition: path_component.cc:40
impeller::StripVertexWriter::EndContour
void EndContour() override
Definition: path_component.cc:50
impeller::VertexWriter
An interface for generating a multi contour polyline as a triangle strip.
Definition: path_component.h:20
impeller::VertexWriter::EndContour
virtual void EndContour()=0
impeller::VertexWriter::Write
virtual void Write(Point point)=0
value
int32_t value
Definition: dl_golden_unittests.cc:444
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
point.h
scalar.h
scale
const Scalar scale
Definition: stroke_path_geometry.cc:316
impeller::ContourComponent
Definition: path_component.h:226
impeller::ContourComponent::operator==
bool operator==(const ContourComponent &other) const
Definition: path_component.h:239
impeller::ContourComponent::ContourComponent
ContourComponent()
Definition: path_component.h:232
impeller::ContourComponent::IsClosed
constexpr bool IsClosed() const
Definition: path_component.h:234
impeller::ContourComponent::ContourComponent
ContourComponent(Point p, Point closed)
Definition: path_component.h:236
impeller::ContourComponent::destination
Point destination
Definition: path_component.h:227
impeller::ContourComponent::closed
Point closed
Definition: path_component.h:230
impeller::CubicPathComponent
Definition: path_component.h:174
impeller::CubicPathComponent::ToLinearPathComponents
void ToLinearPathComponents(Scalar scale, const PointProc &proc) const
Definition: path_component.cc:361
impeller::CubicPathComponent::CountLinearPathComponents
size_t CountLinearPathComponents(Scalar scale) const
Definition: path_component.cc:342
impeller::CubicPathComponent::p1
Point p1
Definition: path_component.h:176
impeller::CubicPathComponent::AppendPolylinePoints
void AppendPolylinePoints(Scalar scale, std::vector< Point > &points) const
Definition: path_component.cc:326
impeller::CubicPathComponent::cp1
Point cp1
Definition: path_component.h:178
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent(Point ap1, Point acp1, Point acp2, Point ap2)
Definition: path_component.h:192
impeller::CubicPathComponent::operator==
bool operator==(const CubicPathComponent &other) const
Definition: path_component.h:213
impeller::CubicPathComponent::cp2
Point cp2
Definition: path_component.h:180
impeller::CubicPathComponent::PointProc
std::function< void(const Point &point)> PointProc
Definition: path_component.h:203
impeller::CubicPathComponent::Subsegment
CubicPathComponent Subsegment(Scalar t0, Scalar t1) const
Definition: path_component.cc:351
impeller::CubicPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:312
impeller::CubicPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:451
impeller::CubicPathComponent::Extrema
std::vector< Point > Extrema() const
Definition: path_component.cc:433
impeller::CubicPathComponent::p2
Point p2
Definition: path_component.h:182
impeller::CubicPathComponent::GetEndDirection
std::optional< Vector2 > GetEndDirection() const
Definition: path_component.cc:464
impeller::CubicPathComponent::SolveDerivative
Point SolveDerivative(Scalar time) const
Definition: path_component.cc:319
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent()
Definition: path_component.h:184
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent(const QuadraticPathComponent &q)
Definition: path_component.h:186
impeller::LinearPathComponent
Definition: path_component.h:110
impeller::LinearPathComponent::GetEndDirection
std::optional< Vector2 > GetEndDirection() const
Definition: path_component.cc:233
impeller::LinearPathComponent::LinearPathComponent
LinearPathComponent(Point ap1, Point ap2)
Definition: path_component.h:116
impeller::LinearPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:226
impeller::LinearPathComponent::Extrema
std::vector< Point > Extrema() const
Definition: path_component.cc:222
impeller::LinearPathComponent::p2
Point p2
Definition: path_component.h:112
impeller::LinearPathComponent::p1
Point p1
Definition: path_component.h:111
impeller::LinearPathComponent::operator==
bool operator==(const LinearPathComponent &other) const
Definition: path_component.h:124
impeller::LinearPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:208
impeller::LinearPathComponent::AppendPolylinePoints
void AppendPolylinePoints(std::vector< Point > &points) const
Definition: path_component.cc:215
impeller::LinearPathComponent::LinearPathComponent
LinearPathComponent()
Definition: path_component.h:114
impeller::QuadraticPathComponent
Definition: path_component.h:134
impeller::QuadraticPathComponent::CountLinearPathComponents
size_t CountLinearPathComponents(Scalar scale) const
Definition: path_component.cc:283
impeller::QuadraticPathComponent::GetEndDirection
std::optional< Vector2 > GetEndDirection() const
Definition: path_component.cc:302
impeller::QuadraticPathComponent::QuadraticPathComponent
QuadraticPathComponent()
Definition: path_component.h:142
impeller::QuadraticPathComponent::QuadraticPathComponent
QuadraticPathComponent(Point ap1, Point acp, Point ap2)
Definition: path_component.h:144
impeller::QuadraticPathComponent::p2
Point p2
Definition: path_component.h:140
impeller::QuadraticPathComponent::AppendPolylinePoints
void AppendPolylinePoints(Scalar scale_factor, std::vector< Point > &points) const
Definition: path_component.cc:264
impeller::QuadraticPathComponent::operator==
bool operator==(const QuadraticPathComponent &other) const
Definition: path_component.h:164
impeller::QuadraticPathComponent::PointProc
std::function< void(const Point &point)> PointProc
Definition: path_component.h:154
impeller::QuadraticPathComponent::Extrema
std::vector< Point > Extrema() const
Definition: path_component.cc:287
impeller::QuadraticPathComponent::SolveDerivative
Point SolveDerivative(Scalar time) const
Definition: path_component.cc:247
impeller::QuadraticPathComponent::cp
Point cp
Definition: path_component.h:138
impeller::QuadraticPathComponent::p1
Point p1
Definition: path_component.h:136
impeller::QuadraticPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:292
impeller::QuadraticPathComponent::ToLinearPathComponents
void ToLinearPathComponents(Scalar scale_factor, const PointProc &proc) const
Definition: path_component.cc:272
impeller::QuadraticPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:240
impeller::TPoint< Scalar >