Flutter Engine
The Flutter Engine
vertex_buffer_builder.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_RENDERER_VERTEX_BUFFER_BUILDER_H_
6#define FLUTTER_IMPELLER_RENDERER_VERTEX_BUFFER_BUILDER_H_
7
8#include <initializer_list>
9#include <vector>
10
17
18namespace impeller {
19
20template <class VertexType_, class IndexType_ = uint16_t>
22 public:
23 using VertexType = VertexType_;
24 using IndexType = IndexType_;
25
27
29
31 if (indices_.size() == 0) {
33 }
34 if constexpr (sizeof(IndexType) == 2) {
36 }
37 if (sizeof(IndexType) == 4) {
39 }
41 }
42
43 void SetLabel(const std::string& label) { label_ = label; }
44
45 void Reserve(size_t count) { return vertices_.reserve(count); }
46
47 void ReserveIndices(size_t count) { return indices_.reserve(count); }
48
49 bool HasVertices() const { return !vertices_.empty(); }
50
51 size_t GetVertexCount() const { return vertices_.size(); }
52
53 size_t GetIndexCount() const {
54 return indices_.size() > 0 ? indices_.size() : vertices_.size();
55 }
56
57 const VertexType& Last() const {
58 FML_DCHECK(!vertices_.empty());
59 return vertices_.back();
60 }
61
62 VertexBufferBuilder& AppendVertex(VertexType_ vertex) {
63 vertices_.emplace_back(std::move(vertex));
64 return *this;
65 }
66
68 std::initializer_list<VertexType_> vertices) {
69 vertices_.reserve(vertices.size());
70 for (auto& vertex : vertices) {
71 vertices_.emplace_back(std::move(vertex));
72 }
73 return *this;
74 }
75
76 VertexBufferBuilder& AppendIndex(IndexType_ index) {
77 indices_.emplace_back(index);
78 return *this;
79 }
80
83 buffer.vertex_buffer = CreateVertexBufferView(host_buffer);
84 buffer.index_buffer = CreateIndexBufferView(host_buffer);
85 buffer.vertex_count = GetIndexCount();
86 buffer.index_type = GetIndexType();
87 return buffer;
88 };
89
90 VertexBuffer CreateVertexBuffer(Allocator& device_allocator) const {
92 // This can be merged into a single allocation.
93 buffer.vertex_buffer = CreateVertexBufferView(device_allocator);
94 buffer.index_buffer = CreateIndexBufferView(device_allocator);
95 buffer.vertex_count = GetIndexCount();
96 buffer.index_type = GetIndexType();
97 return buffer;
98 };
99
100 void IterateVertices(const std::function<void(VertexType&)>& iterator) {
101 for (auto& vertex : vertices_) {
102 iterator(vertex);
103 }
104 }
105
106 private:
107 std::vector<VertexType> vertices_;
108 std::vector<IndexType> indices_;
109 std::string label_;
110
111 BufferView CreateVertexBufferView(HostBuffer& buffer) const {
112 return buffer.Emplace(vertices_.data(),
113 vertices_.size() * sizeof(VertexType),
114 alignof(VertexType));
115 }
116
117 BufferView CreateVertexBufferView(Allocator& allocator) const {
118 auto buffer = allocator.CreateBufferWithCopy(
119 reinterpret_cast<const uint8_t*>(vertices_.data()),
120 vertices_.size() * sizeof(VertexType));
121 if (!buffer) {
122 return {};
123 }
124 if (!label_.empty()) {
125 buffer->SetLabel(SPrintF("%s Vertices", label_.c_str()));
126 }
128 }
129
130 std::vector<IndexType> CreateIndexBuffer() const { return indices_; }
131
132 BufferView CreateIndexBufferView(HostBuffer& buffer) const {
133 const auto index_buffer = CreateIndexBuffer();
134 if (index_buffer.size() == 0) {
135 return {};
136 }
137 return buffer.Emplace(index_buffer.data(),
138 index_buffer.size() * sizeof(IndexType),
139 alignof(IndexType));
140 }
141
142 BufferView CreateIndexBufferView(Allocator& allocator) const {
143 const auto index_buffer = CreateIndexBuffer();
144 if (index_buffer.size() == 0) {
145 return {};
146 }
147 auto buffer = allocator.CreateBufferWithCopy(
148 reinterpret_cast<const uint8_t*>(index_buffer.data()),
149 index_buffer.size() * sizeof(IndexType));
150 if (!buffer) {
151 return {};
152 }
153 if (!label_.empty()) {
154 buffer->SetLabel(SPrintF("%s Indices", label_.c_str()));
155 }
157 }
158};
159
160} // namespace impeller
161
162#endif // FLUTTER_IMPELLER_RENDERER_VERTEX_BUFFER_BUILDER_H_
int count
Definition: FontMgrTest.cpp:50
An object that allocates device memory.
Definition: allocator.h:22
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
VertexBufferBuilder & AppendVertex(VertexType_ vertex)
const VertexType & Last() const
void IterateVertices(const std::function< void(VertexType &)> &iterator)
VertexBuffer CreateVertexBuffer(Allocator &device_allocator) const
VertexBufferBuilder & AppendIndex(IndexType_ index)
void SetLabel(const std::string &label)
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
constexpr impeller::IndexType GetIndexType() const
#define FML_DCHECK(condition)
Definition: logging.h:103
Dart_NativeFunction function
Definition: fuchsia.cc:51
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
@ kNone
Does not use the index buffer.
std::string SPrintF(const char *format,...)
Definition: strings.cc:12