Flutter Engine
 
Loading...
Searching...
No Matches
dl_vertices.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_DISPLAY_LIST_DL_VERTICES_H_
6#define FLUTTER_DISPLAY_LIST_DL_VERTICES_H_
7
8#include <memory>
9
11
12namespace flutter {
13
14//------------------------------------------------------------------------------
15/// @brief Defines the way in which the vertices of a DlVertices object
16/// are separated into triangles into which to render.
17///
18enum class DlVertexMode {
19 /// The vertices are taken 3 at a time to form a triangle.
21
22 /// The vertices are taken in overlapping triplets to form triangles, with
23 /// each triplet sharing 2 of its vertices with the preceding and following
24 /// triplets.
25 /// vertices [ABCDE] yield 3 triangles ABC,BCD,CDE
27
28 /// The vertices are taken in overlapping pairs and combined with the first
29 /// vertex to form triangles that radiate outward from the initial point.
30 /// vertices [ABCDE] yield 3 triangles ABC,ACD,ADE
32};
33
34//------------------------------------------------------------------------------
35/// @brief Holds all of the data (both required and optional) for a
36/// DisplayList drawVertices call.
37///
38/// There are 4 main pices of data:
39/// - vertices():
40/// the points on the rendering surface that define the pixels
41/// being rendered to in a series of triangles. These points
42/// can map to triangles in various ways depending on the
43/// supplied |DlVertexMode|.
44/// - texture_coordinates():
45/// the points in the DlColorSource to map to the coordinates
46/// of the triangles in the vertices(). If missing, the
47/// vertex coordinates themselves will be used to map the
48/// source colors to the vertices.
49/// - colors():
50/// colors to map to each triangle vertex. Note that each
51/// vertex is mapped to exactly 1 color even if the DlVertex
52/// - indices():
53/// An indirection based on indices into the array of vertices
54/// (and by extension, their associated texture_coordinates and
55/// colors). Note that the DLVertexMode will still apply to the
56/// indices in the same way (and instead of the way) that it
57/// would normally be applied to the vertices themselves. The
58/// indices are useful, for example, to fill the vertices with
59/// a grid of points and then use the indices to define a
60/// triangular mesh that covers that grid without having to
61/// repeat the vertex (and texture coordinate and color)
62/// information for the times when a given grid coordinate
63/// gets reused in up to 4 mesh triangles.
64///
65/// Note that each vertex is mapped to exactly 1 texture_coordinate and
66/// color even if the DlVertexMode or indices specify that it contributes
67/// to more than one output triangle.
68///
70 public:
71 /// @brief A utility class to build up a |DlVertices| object
72 /// one set of data at a time.
73 class Builder {
74 public:
75 /// @brief flags to indicate/promise which of the optional
76 /// texture coordinates or colors will be supplied
77 /// during the build phase.
78 union Flags {
79 struct {
81 unsigned has_colors : 1;
82 };
83 uint32_t mask = 0;
84
85 inline Flags operator|(const Flags& rhs) const {
86 return Flags{.mask = (mask | rhs.mask)};
87 }
88
89 inline Flags& operator|=(const Flags& rhs) {
90 mask = mask | rhs.mask;
91 return *this;
92 }
93 };
94 static constexpr Flags kNone = {{false, false}};
95 static constexpr Flags kHasTextureCoordinates = {{true, false}};
96 static constexpr Flags kHasColors = {{false, true}};
97
98 //--------------------------------------------------------------------------
99 /// @brief Constructs a Builder and prepares room for the
100 /// required and optional data.
101 ///
102 /// Vertices are always required. Optional texture coordinates
103 /// and optional colors are reserved depending on the |Flags|.
104 /// Optional indices will be reserved if the index_count is
105 /// positive (>0).
106 ///
107 /// The caller must provide all data that is promised by the
108 /// supplied |flags| and |index_count| parameters before
109 /// calling the |build()| method.
111
112 /// Returns true iff the underlying object was successfully allocated.
113 bool is_valid() const { return vertices_ != nullptr; }
114
115 /// @brief Copies the indicated list of points as vertices.
116 ///
117 /// fails if vertices have already been supplied.
118 void store_vertices(const DlPoint vertices[]);
119
120 /// @brief Copies the indicated list of float pairs as vertices.
121 ///
122 /// fails if vertices have already been supplied.
123 void store_vertices(const float coordinates[]);
124
125 /// @brief Copies the indicated list of points as texture coordinates.
126 ///
127 /// fails if texture coordinates have already been supplied or if they
128 /// were not promised by the flags.has_texture_coordinates.
130
131 /// @brief Copies the indicated list of float pairs as texture coordinates.
132 ///
133 /// fails if texture coordinates have already been supplied or if they
134 /// were not promised by the flags.has_texture_coordinates.
135 void store_texture_coordinates(const float coordinates[]);
136
137 /// @brief Copies the indicated list of colors as vertex colors.
138 ///
139 /// fails if colors have already been supplied or if they were not
140 /// promised by the flags.has_colors.
141 void store_colors(const DlColor colors[]);
142
143 /// @brief Copies the indicated list of unsigned ints as vertex colors
144 /// in the 32-bit RGBA format.
145 ///
146 /// fails if colors have already been supplied or if they were not
147 /// promised by the flags.has_colors.
148 void store_colors(const uint32_t colors[]);
149
150 /// @brief Copies the indicated list of 16-bit indices as vertex indices.
151 ///
152 /// fails if indices have already been supplied or if they were not
153 /// promised by (index_count > 0).
154 void store_indices(const uint16_t indices[]);
155
156 /// @brief Overwrite the internal bounds with a precomputed bounding rect.
157 void store_bounds(DlRect bounds);
158
159 /// @brief Finalizes and the constructed DlVertices object.
160 ///
161 /// fails if any of the optional data promised in the constructor is
162 /// missing.
163 std::shared_ptr<DlVertices> build();
164
165 private:
166 std::shared_ptr<DlVertices> vertices_;
167 bool needs_vertices_ = true;
168 bool needs_texture_coords_;
169 bool needs_colors_;
170 bool needs_indices_;
171 bool needs_bounds_ = true;
172 };
173
174 //--------------------------------------------------------------------------
175 /// @brief Constructs a DlVector with compact inline storage for all
176 /// of its required and optional lists of data.
177 ///
178 /// Vertices are always required. Optional texture coordinates
179 /// and optional colors are stored if the arguments are non-null.
180 /// Optional indices will be stored iff the array argument is
181 /// non-null and the index_count is positive (>0).
182 static std::shared_ptr<DlVertices> Make(DlVertexMode mode,
183 int vertex_count,
184 const DlPoint vertices[],
185 const DlPoint texture_coordinates[],
186 const DlColor colors[],
187 int index_count = 0,
188 const uint16_t indices[] = nullptr,
189 const DlRect* bounds = nullptr);
190
191 /// Returns the size of the object including all of the inlined data.
192 size_t size() const;
193
194 /// Returns the bounds of the vertices.
195 DlRect GetBounds() const { return bounds_; }
196
197 /// Returns the vertex mode that defines how the vertices (or the indices)
198 /// are turned into triangles.
199 DlVertexMode mode() const { return mode_; }
200
201 /// Returns the number of vertices, which also applies to the number of
202 /// texture coordinate and colors if they are provided.
203 int vertex_count() const { return vertex_count_; }
204
205 /// Returns a pointer to the vertex information. Should be non-null.
206 const DlPoint* vertex_data() const {
207 return static_cast<const DlPoint*>(pod(vertices_offset_));
208 }
209
210 /// Returns a pointer to the vertex texture coordinate
211 /// or null if none were provided.
213 return static_cast<const DlPoint*>(pod(texture_coordinates_offset_));
214 }
215
216 /// Returns a pointer to the vertex colors
217 /// or null if none were provided.
218 const DlColor* colors() const {
219 return static_cast<const DlColor*>(pod(colors_offset_));
220 }
221
222 /// Returns a pointer to the count of vertex indices
223 /// or 0 if none were provided.
224 int index_count() const { return index_count_; }
225
226 /// Returns a pointer to the vertex indices
227 /// or null if none were provided.
228 const uint16_t* indices() const {
229 return static_cast<const uint16_t*>(pod(indices_offset_));
230 }
231
232 bool operator==(DlVertices const& other) const;
233
234 private:
235 // Constructors are designed to encapsulate arrays sequentially in memory
236 // which means they can only be called by intantiations that use the
237 // new (ptr) paradigm which precomputes and preallocates the memory for
238 // the class body and all of its arrays, such as in Builder.
240 int vertex_count,
241 const DlPoint vertices[],
242 const DlPoint texture_coordinates[],
243 const DlColor colors[],
244 int index_count,
245 const uint16_t indices[],
246 const DlRect* bounds = nullptr);
247
248 // This constructor is specifically used by the DlVertices::Builder to
249 // establish the object before the copying of data is requested.
251 int vertex_count,
252 Builder::Flags flags,
253 int index_count);
254
255 // The copy constructor has the same memory pre-allocation requirements
256 // as this other constructors. This particular version is used by the
257 // DisplaylistBuilder to copy the instance into pre-allocated pod memory
258 // in the display list buffer.
259 explicit DlVertices(const DlVertices* other);
260
261 DlVertexMode mode_;
262
263 int vertex_count_;
264 size_t vertices_offset_;
265 size_t texture_coordinates_offset_;
266 size_t colors_offset_;
267
268 int index_count_;
269 size_t indices_offset_;
270
271 DlRect bounds_;
272
273 const void* pod(int offset) const {
274 if (offset <= 0) {
275 return nullptr;
276 }
277 const void* base = static_cast<const void*>(this);
278 return static_cast<const char*>(base) + offset;
279 }
280
281 friend class DisplayListBuilder;
282};
283
284} // namespace flutter
285
286#endif // FLUTTER_DISPLAY_LIST_DL_VERTICES_H_
A utility class to build up a |DlVertices| object one set of data at a time.
Definition dl_vertices.h:73
void store_vertices(const DlPoint vertices[])
Copies the indicated list of points as vertices.
void store_indices(const uint16_t indices[])
Copies the indicated list of 16-bit indices as vertex indices.
static constexpr Flags kNone
Definition dl_vertices.h:94
bool is_valid() const
Returns true iff the underlying object was successfully allocated.
void store_texture_coordinates(const DlPoint points[])
Copies the indicated list of points as texture coordinates.
void store_bounds(DlRect bounds)
Overwrite the internal bounds with a precomputed bounding rect.
std::shared_ptr< DlVertices > build()
Finalizes and the constructed DlVertices object.
static constexpr Flags kHasColors
Definition dl_vertices.h:96
void store_colors(const DlColor colors[])
Copies the indicated list of colors as vertex colors.
static constexpr Flags kHasTextureCoordinates
Definition dl_vertices.h:95
Holds all of the data (both required and optional) for a DisplayList drawVertices call.
Definition dl_vertices.h:69
static std::shared_ptr< DlVertices > Make(DlVertexMode mode, int vertex_count, const DlPoint vertices[], const DlPoint texture_coordinates[], const DlColor colors[], int index_count=0, const uint16_t indices[]=nullptr, const DlRect *bounds=nullptr)
Constructs a DlVector with compact inline storage for all of its required and optional lists of data.
DlRect GetBounds() const
Returns the bounds of the vertices.
const DlPoint * texture_coordinate_data() const
const DlPoint * vertex_data() const
Returns a pointer to the vertex information. Should be non-null.
const DlColor * colors() const
int vertex_count() const
DlVertexMode mode() const
int index_count() const
bool operator==(DlVertices const &other) const
size_t size() const
Returns the size of the object including all of the inlined data.
const uint16_t * indices() const
DlVertexMode
Defines the way in which the vertices of a DlVertices object are separated into triangles into which ...
Definition dl_vertices.h:18
@ kTriangles
The vertices are taken 3 at a time to form a triangle.
std::vector< Point > points
flags to indicate/promise which of the optional texture coordinates or colors will be supplied during...
Definition dl_vertices.h:78
Flags operator|(const Flags &rhs) const
Definition dl_vertices.h:85
Flags & operator|=(const Flags &rhs)
Definition dl_vertices.h:89