Flutter Impeller
path.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_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_PATH_H_
7 
8 #include <functional>
9 #include <optional>
10 #include <tuple>
11 #include <vector>
12 
14 #include "impeller/geometry/rect.h"
15 
16 namespace impeller {
17 
18 enum class Cap {
19  kButt,
20  kRound,
21  kSquare,
22 };
23 
24 enum class Join {
25  kMiter,
26  kRound,
27  kBevel,
28 };
29 
30 enum class FillType {
31  kNonZero, // The default winding order.
32  kOdd,
33 };
34 
35 enum class Convexity {
36  kUnknown,
37  kConvex,
38 };
39 
40 //------------------------------------------------------------------------------
41 /// @brief Paths are lightweight objects that describe a collection of
42 /// linear, quadratic, or cubic segments. These segments may be
43 /// broken up by move commands, which are effectively linear
44 /// commands that pick up the pen rather than continuing to draw.
45 ///
46 /// All shapes supported by Impeller are paths either directly or
47 /// via approximation (in the case of circles).
48 ///
49 /// Paths are externally immutable once created, Creating paths must
50 /// be done using a path builder.
51 ///
52 class Path {
53  public:
54  enum class ComponentType {
55  kLinear,
56  kQuadratic,
57  kCubic,
58  kContour,
59  };
60 
61  struct PolylineContour {
62  struct Component {
64  /// Denotes whether this component is a curve.
65  ///
66  /// This is set to true when this component is generated from
67  /// QuadraticComponent or CubicPathComponent.
68  bool is_curve;
69  };
70  /// Index that denotes the first point of this contour.
71  size_t start_index;
72 
73  /// Denotes whether the last point of this contour is connected to the first
74  /// point of this contour or not.
75  bool is_closed;
76 
77  /// The direction of the contour's start cap.
79  /// The direction of the contour's end cap.
81 
82  /// Distinct components in this contour.
83  ///
84  /// If this contour is generated from multiple path components, each
85  /// path component forms a component in this vector.
86  std::vector<Component> components;
87  };
88 
89  /// One or more contours represented as a series of points and indices in
90  /// the point vector representing the start of a new contour.
91  ///
92  /// Polylines are ephemeral and meant to be used by the tessellator. They do
93  /// not allocate their own point vectors to allow for optimizations around
94  /// allocation and reuse of arenas.
95  struct Polyline {
96  /// The signature of a method called when it is safe to reclaim the point
97  /// buffer provided to the constructor of this object.
98  using PointBufferPtr = std::unique_ptr<std::vector<Point>>;
99  using ReclaimPointBufferCallback = std::function<void(PointBufferPtr)>;
100 
101  /// The buffer will be cleared and returned at the destruction of this
102  /// polyline.
103  Polyline(PointBufferPtr point_buffer, ReclaimPointBufferCallback reclaim);
104 
105  Polyline(Polyline&& other);
106  ~Polyline();
107 
108  /// Points in the polyline, which may represent multiple contours specified
109  /// by indices in |contours|.
111 
112  Point& GetPoint(size_t index) const { return (*points)[index]; }
113 
114  /// Contours are disconnected pieces of a polyline, such as when a MoveTo
115  /// was issued on a PathBuilder.
116  std::vector<PolylineContour> contours;
117 
118  /// Convenience method to compute the start (inclusive) and end (exclusive)
119  /// point of the given contour index.
120  ///
121  /// The contour_index parameter is clamped to contours.size().
122  std::tuple<size_t, size_t> GetContourPointBounds(
123  size_t contour_index) const;
124 
125  private:
126  ReclaimPointBufferCallback reclaim_points_;
127  };
128 
129  Path();
130 
131  ~Path();
132 
133  size_t GetComponentCount(std::optional<ComponentType> type = {}) const;
134 
135  FillType GetFillType() const;
136 
137  bool IsConvex() const;
138 
139  bool IsEmpty() const;
140 
141  template <class T>
142  using Applier = std::function<void(size_t index, const T& component)>;
143  void EnumerateComponents(
144  const Applier<LinearPathComponent>& linear_applier,
145  const Applier<QuadraticPathComponent>& quad_applier,
146  const Applier<CubicPathComponent>& cubic_applier,
147  const Applier<ContourComponent>& contour_applier) const;
148 
149  bool GetLinearComponentAtIndex(size_t index,
150  LinearPathComponent& linear) const;
151 
152  bool GetQuadraticComponentAtIndex(size_t index,
153  QuadraticPathComponent& quadratic) const;
154 
155  bool GetCubicComponentAtIndex(size_t index, CubicPathComponent& cubic) const;
156 
157  bool GetContourComponentAtIndex(size_t index,
158  ContourComponent& contour) const;
159 
160  /// Callers must provide the scale factor for how this path will be
161  /// transformed.
162  ///
163  /// It is suitable to use the max basis length of the matrix used to transform
164  /// the path. If the provided scale is 0, curves will revert to straight
165  /// lines.
167  Scalar scale,
168  Polyline::PointBufferPtr point_buffer =
169  std::make_unique<std::vector<Point>>(),
170  Polyline::ReclaimPointBufferCallback reclaim = nullptr) const;
171 
172  std::optional<Rect> GetBoundingBox() const;
173 
174  std::optional<Rect> GetTransformedBoundingBox(const Matrix& transform) const;
175 
176  /// Generate a polyline into the temporary storage held by the [writer].
177  ///
178  /// It is suitable to use the max basis length of the matrix used to transform
179  /// the path. If the provided scale is 0, curves will revert to straight
180  /// lines.
181  void WritePolyline(Scalar scale, VertexWriter& writer) const;
182 
183  private:
184  friend class PathBuilder;
185 
186  struct ComponentIndexPair {
188  size_t index = 0;
189 
190  ComponentIndexPair() {}
191 
192  ComponentIndexPair(ComponentType a_type, size_t a_index)
193  : type(a_type), index(a_index) {}
194  };
195 
196  // All of the data for the path is stored in this structure which is
197  // held by a shared_ptr. Since they all share the structure, the
198  // copy constructor for Path is very cheap and we don't need to deal
199  // with shared pointers for Path fields and method arguments.
200  //
201  // PathBuilder also uses this structure to accumulate the path data
202  // but the Path constructor used in |TakePath()| will clone the
203  // structure to prevent sharing and future modifications within the
204  // builder from affecting the existing taken paths.
205  struct Data {
206  Data() = default;
207 
208  Data(Data&& other) = default;
209 
210  Data(const Data& other) = default;
211 
212  ~Data() = default;
213 
215  Convexity convexity = Convexity::kUnknown;
216  std::vector<ComponentIndexPair> components;
217  std::vector<Point> points;
218  std::vector<ContourComponent> contours;
219 
220  std::optional<Rect> bounds;
221  };
222 
223  explicit Path(Data data);
224 
225  std::shared_ptr<const Data> data_;
226 };
227 
228 static_assert(sizeof(Path) == sizeof(std::shared_ptr<struct Anonymous>));
229 
230 } // namespace impeller
231 
232 #endif // FLUTTER_IMPELLER_GEOMETRY_PATH_H_
impeller::Cap::kRound
@ kRound
impeller::Cap::kSquare
@ kSquare
impeller::LinearPathComponent
Definition: path_component.h:39
impeller::Path::GetQuadraticComponentAtIndex
bool GetQuadraticComponentAtIndex(size_t index, QuadraticPathComponent &quadratic) const
Definition: path.cc:190
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Path::PolylineContour
Definition: path.h:61
impeller::FillType::kOdd
@ kOdd
impeller::Path::ComponentType::kLinear
@ kLinear
impeller::Path::PolylineContour::start_index
size_t start_index
Index that denotes the first point of this contour.
Definition: path.h:71
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
impeller::Path::PolylineContour::components
std::vector< Component > components
Definition: path.h:86
impeller::Path::ComponentType::kCubic
@ kCubic
impeller::PathBuilder
Definition: path_builder.h:14
impeller::Path::GetBoundingBox
std::optional< Rect > GetBoundingBox() const
Definition: path.cc:405
impeller::Convexity
Convexity
Definition: path.h:35
impeller::Path::ComponentType::kQuadratic
@ kQuadratic
impeller::Path::IsEmpty
bool IsEmpty() const
Definition: path.cc:59
impeller::Cap::kButt
@ kButt
impeller::Path::Polyline::Polyline
Polyline(PointBufferPtr point_buffer, ReclaimPointBufferCallback reclaim)
Definition: path.cc:245
impeller::Join::kMiter
@ kMiter
impeller::Path::~Path
~Path()
impeller::Path::Polyline
Definition: path.h:95
impeller::Convexity::kUnknown
@ kUnknown
impeller::Path::WritePolyline
void WritePolyline(Scalar scale, VertexWriter &writer) const
Definition: path.cc:106
impeller::Path::ComponentType
ComponentType
Definition: path.h:54
impeller::Path::GetCubicComponentAtIndex
bool GetCubicComponentAtIndex(size_t index, CubicPathComponent &cubic) const
Definition: path.cc:210
impeller::Path::Polyline::ReclaimPointBufferCallback
std::function< void(PointBufferPtr)> ReclaimPointBufferCallback
Definition: path.h:99
impeller::Path::EnumerateComponents
void EnumerateComponents(const Applier< LinearPathComponent > &linear_applier, const Applier< QuadraticPathComponent > &quad_applier, const Applier< CubicPathComponent > &cubic_applier, const Applier< ContourComponent > &contour_applier) const
Definition: path.cc:63
impeller::Path::PolylineContour::start_direction
Vector2 start_direction
The direction of the contour's start cap.
Definition: path.h:78
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:52
impeller::Path::Polyline::GetPoint
Point & GetPoint(size_t index) const
Definition: path.h:112
impeller::Path::GetTransformedBoundingBox
std::optional< Rect > GetTransformedBoundingBox(const Matrix &transform) const
Definition: path.cc:409
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:231
impeller::Path::Polyline::GetContourPointBounds
std::tuple< size_t, size_t > GetContourPointBounds(size_t contour_index) const
Definition: path.cc:22
type
GLenum type
Definition: blit_command_gles.cc:126
impeller::Path::PolylineContour::Component
Definition: path.h:62
impeller::FillType
FillType
Definition: path.h:30
impeller::Join::kRound
@ kRound
impeller::Path::PolylineContour::end_direction
Vector2 end_direction
The direction of the contour's end cap.
Definition: path.h:80
impeller::FillType::kNonZero
@ kNonZero
impeller::Path::Path
Path()
Definition: path.cc:16
impeller::Path::GetContourComponentAtIndex
bool GetContourComponentAtIndex(size_t index, ContourComponent &contour) const
Definition: path.cc:229
impeller::CubicPathComponent
Definition: path_component.h:101
impeller::Path::Polyline::points
PointBufferPtr points
Definition: path.h:110
impeller::Join::kBevel
@ kBevel
impeller::VertexWriter
An interface for generating a multi contour polyline as a triangle strip.
Definition: path_component.h:21
impeller::Path::PolylineContour::Component::is_curve
bool is_curve
Definition: path.h:68
impeller::Join
Join
Definition: path.h:24
impeller::Path::Applier
std::function< void(size_t index, const T &component)> Applier
Definition: path.h:142
rect.h
impeller::TPoint< Scalar >
impeller::Path::GetComponentCount
size_t GetComponentCount(std::optional< ComponentType > type={}) const
Definition: path.cc:34
impeller::Path::Polyline::contours
std::vector< PolylineContour > contours
Definition: path.h:116
scale
const Scalar scale
Definition: stroke_path_geometry.cc:308
impeller::Path::Polyline::PointBufferPtr
std::unique_ptr< std::vector< Point > > PointBufferPtr
Definition: path.h:98
impeller::Path::Polyline::~Polyline
~Polyline()
Definition: path.cc:257
impeller::Path::IsConvex
bool IsConvex() const
Definition: path.cc:55
path_component.h
impeller::ContourComponent
Definition: path_component.h:151
impeller::Convexity::kConvex
@ kConvex
impeller::Path::PolylineContour::Component::component_start_index
size_t component_start_index
Definition: path.h:63
impeller
Definition: aiks_blend_unittests.cc:18
impeller::QuadraticPathComponent
Definition: path_component.h:63
impeller::Path::CreatePolyline
Polyline CreatePolyline(Scalar scale, Polyline::PointBufferPtr point_buffer=std::make_unique< std::vector< Point >>(), Polyline::ReclaimPointBufferCallback reclaim=nullptr) const
Definition: path.cc:264
impeller::Path::GetLinearComponentAtIndex
bool GetLinearComponentAtIndex(size_t index, LinearPathComponent &linear) const
Definition: path.cc:172
impeller::Path::ComponentType::kContour
@ kContour
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Path::PolylineContour::is_closed
bool is_closed
Definition: path.h:75
impeller::Cap
Cap
Definition: path.h:18
impeller::Path::GetFillType
FillType GetFillType() const
Definition: path.cc:51