Flutter Engine
The Flutter Engine
host_buffer.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_CORE_HOST_BUFFER_H_
6#define FLUTTER_IMPELLER_CORE_HOST_BUFFER_H_
7
8#include <algorithm>
9#include <array>
10#include <functional>
11#include <memory>
12#include <string>
13#include <type_traits>
14
18
19namespace impeller {
20
21/// Approximately the same size as the max frames in flight.
22static const constexpr size_t kHostBufferArenaSize = 3u;
23
24/// The host buffer class manages one more 1024 Kb blocks of device buffer
25/// allocations.
26///
27/// These are reset per-frame.
29 public:
30 static std::shared_ptr<HostBuffer> Create(
31 const std::shared_ptr<Allocator>& allocator);
32
33 // |Buffer|
34 virtual ~HostBuffer();
35
36 void SetLabel(std::string label);
37
38 //----------------------------------------------------------------------------
39 /// @brief Emplace uniform data onto the host buffer. Ensure that backend
40 /// specific uniform alignment requirements are respected.
41 ///
42 /// @param[in] uniform The uniform struct to emplace onto the buffer.
43 ///
44 /// @tparam UniformType The type of the uniform struct.
45 ///
46 /// @return The buffer view.
47 ///
48 template <class UniformType,
49 class = std::enable_if_t<std::is_standard_layout_v<UniformType>>>
50 [[nodiscard]] BufferView EmplaceUniform(const UniformType& uniform) {
51 const auto alignment =
52 std::max(alignof(UniformType), DefaultUniformAlignment());
53 return Emplace(reinterpret_cast<const void*>(&uniform), // buffer
54 sizeof(UniformType), // size
55 alignment // alignment
56 );
57 }
58
59 //----------------------------------------------------------------------------
60 /// @brief Emplace storage buffer data onto the host buffer. Ensure that
61 /// backend specific uniform alignment requirements are respected.
62 ///
63 /// @param[in] uniform The storage buffer to emplace onto the buffer.
64 ///
65 /// @tparam StorageBufferType The type of the shader storage buffer.
66 ///
67 /// @return The buffer view.
68 ///
69 template <
70 class StorageBufferType,
71 class = std::enable_if_t<std::is_standard_layout_v<StorageBufferType>>>
73 const StorageBufferType& buffer) {
74 const auto alignment =
75 std::max(alignof(StorageBufferType), DefaultUniformAlignment());
76 return Emplace(&buffer, // buffer
77 sizeof(StorageBufferType), // size
78 alignment // alignment
79 );
80 }
81
82 //----------------------------------------------------------------------------
83 /// @brief Emplace non-uniform data (like contiguous vertices) onto the
84 /// host buffer.
85 ///
86 /// @param[in] buffer The buffer data.
87 /// @param[in] alignment Minimum alignment of the data being emplaced.
88 ///
89 /// @tparam BufferType The type of the buffer data.
90 ///
91 /// @return The buffer view.
92 ///
93 template <class BufferType,
94 class = std::enable_if_t<std::is_standard_layout_v<BufferType>>>
95 [[nodiscard]] BufferView Emplace(const BufferType& buffer,
96 size_t alignment = 0) {
97 return Emplace(reinterpret_cast<const void*>(&buffer), // buffer
98 sizeof(BufferType), // size
99 std::max(alignment, alignof(BufferType)) // alignment
100 );
101 }
102
103 [[nodiscard]] BufferView Emplace(const void* buffer,
104 size_t length,
105 size_t align);
106
107 using EmplaceProc = std::function<void(uint8_t* buffer)>;
108
109 //----------------------------------------------------------------------------
110 /// @brief Emplaces undefined data onto the managed buffer and gives the
111 /// caller a chance to update it using the specified callback. The
112 /// buffer is guaranteed to have enough space for length bytes. It
113 /// is the responsibility of the caller to not exceed the bounds
114 /// of the buffer returned in the EmplaceProc.
115 ///
116 /// @param[in] cb A callback that will be passed a ptr to the
117 /// underlying host buffer.
118 ///
119 /// @return The buffer view.
120 ///
121 BufferView Emplace(size_t length, size_t align, const EmplaceProc& cb);
122
123 //----------------------------------------------------------------------------
124 /// @brief Resets the contents of the HostBuffer to nothing so it can be
125 /// reused.
126 void Reset();
127
128 /// Test only internal state.
133 };
134
135 /// @brief Retrieve internal buffer state for test expectations.
137
138 private:
139 [[nodiscard]] std::tuple<Range, std::shared_ptr<DeviceBuffer>>
140 EmplaceInternal(const void* buffer, size_t length);
141
142 std::tuple<Range, std::shared_ptr<DeviceBuffer>>
143 EmplaceInternal(size_t length, size_t align, const EmplaceProc& cb);
144
145 std::tuple<Range, std::shared_ptr<DeviceBuffer>>
146 EmplaceInternal(const void* buffer, size_t length, size_t align);
147
148 size_t GetLength() const { return offset_; }
149
150 void MaybeCreateNewBuffer();
151
152 const std::shared_ptr<DeviceBuffer>& GetCurrentBuffer() const;
153
154 [[nodiscard]] BufferView Emplace(const void* buffer, size_t length);
155
156 explicit HostBuffer(const std::shared_ptr<Allocator>& allocator);
157
158 HostBuffer(const HostBuffer&) = delete;
159
160 HostBuffer& operator=(const HostBuffer&) = delete;
161
162 std::shared_ptr<Allocator> allocator_;
163 std::array<std::vector<std::shared_ptr<DeviceBuffer>>, kHostBufferArenaSize>
164 device_buffers_;
165 size_t current_buffer_ = 0u;
166 size_t offset_ = 0u;
167 size_t frame_index_ = 0u;
168 std::string label_;
169};
170
171} // namespace impeller
172
173#endif // FLUTTER_IMPELLER_CORE_HOST_BUFFER_H_
void SetLabel(std::string label)
Definition: host_buffer.cc:37
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:95
TestStateQuery GetStateForTest()
Retrieve internal buffer state for test expectations.
Definition: host_buffer.cc:69
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator)
Definition: host_buffer.cc:20
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:50
BufferView EmplaceStorageBuffer(const StorageBufferType &buffer)
Emplace storage buffer data onto the host buffer. Ensure that backend specific uniform alignment requ...
Definition: host_buffer.h:72
std::function< void(uint8_t *buffer)> EmplaceProc
Definition: host_buffer.h:107
void Reset()
Resets the contents of the HostBuffer to nothing so it can be reused.
Definition: host_buffer.cc:195
Dart_NativeFunction function
Definition: fuchsia.cc:51
static float max(float r, float g, float b)
Definition: hsl.cpp:49
size_t length
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
static const constexpr size_t kHostBufferArenaSize
Approximately the same size as the max frames in flight.
Definition: host_buffer.h:22
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
Test only internal state.
Definition: host_buffer.h:129