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