| Flutter Impeller |

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
15 #include "impeller/core/allocator.h"
16 #include "impeller/core/buffer_view.h"
17 #include "impeller/core/platform.h"
18
19 namespace impeller {
20
21 /// Approximately the same size as the max frames in flight.
22 static const constexpr size_t kHostBufferArenaSize = 4u;
23
24 /// The host buffer class manages one more 1024 Kb blocks of device buffer
25 /// allocations.
26 ///
27 /// These are reset per-frame.
28 class HostBuffer {
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>>>
72 [[nodiscard]] BufferView EmplaceStorageBuffer(
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.
129 struct TestStateQuery {
130 size_t current_frame;
131 size_t current_buffer;
132 size_t total_buffer_count;
133 };
134
135 /// @brief Retrieve internal buffer state for test expectations.
136 TestStateQuery GetStateForTest();
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 /// Attempt to create a new internal buffer if the existing capacity is not
151 /// sufficient.
152 ///
153 /// A false return value indicates an unrecoverable allocation failure.
154 [[nodiscard]] bool MaybeCreateNewBuffer();
155
156 const std::shared_ptr<DeviceBuffer>& GetCurrentBuffer() const;
157
158 [[nodiscard]] BufferView Emplace(const void* buffer, size_t length);
159
160 explicit HostBuffer(const std::shared_ptr<Allocator>& allocator);
161
162 HostBuffer(const HostBuffer&) = delete;
163
164 HostBuffer& operator=(const HostBuffer&) = delete;
165
166 std::shared_ptr<Allocator> allocator_;
167 std::array<std::vector<std::shared_ptr<DeviceBuffer>>, kHostBufferArenaSize>
168 device_buffers_;
169 size_t current_buffer_ = 0u;
170 size_t offset_ = 0u;
171 size_t frame_index_ = 0u;
172 std::string label_;
173 };
174
175 } // namespace impeller
176
177 #endif // FLUTTER_IMPELLER_CORE_HOST_BUFFER_H_
impeller::HostBuffer::EmplaceProc
std::function< void(uint8_t *buffer)> EmplaceProc
Definition: host_buffer.h:107
impeller::HostBuffer::Emplace
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
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
impeller::HostBuffer
Definition: host_buffer.h:28
impeller::HostBuffer::Create
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator)
Definition: host_buffer.cc:21
impeller::HostBuffer::TestStateQuery::total_buffer_count
size_t total_buffer_count
Definition: host_buffer.h:132
impeller::HostBuffer::TestStateQuery
Test only internal state.
Definition: host_buffer.h:129
impeller::HostBuffer::TestStateQuery::current_frame
size_t current_frame
Definition: host_buffer.h:130
impeller::HostBuffer::~HostBuffer
virtual ~HostBuffer()
impeller::HostBuffer::TestStateQuery::current_buffer
size_t current_buffer
Definition: host_buffer.h:131
impeller::kHostBufferArenaSize
static const constexpr size_t kHostBufferArenaSize
Approximately the same size as the max frames in flight.
Definition: host_buffer.h:22
platform.h
allocator.h
impeller::BufferView
Definition: buffer_view.h:15
buffer_view.h
impeller::HostBuffer::Reset
void Reset()
Resets the contents of the HostBuffer to nothing so it can be reused.
Definition: host_buffer.cc:208
impeller::HostBuffer::GetStateForTest
TestStateQuery GetStateForTest()
Retrieve internal buffer state for test expectations.
Definition: host_buffer.cc:72
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:50
impeller::HostBuffer::EmplaceStorageBuffer
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
impeller::HostBuffer::SetLabel
void SetLabel(std::string label)
Definition: host_buffer.cc:40
impeller
Definition: allocation.cc:12