| Flutter Impeller |

host_buffer.cc
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 #include "impeller/core/host_buffer.h"
6
7 #include <algorithm>
8 #include <cstring>
9
10 #include "flutter/fml/logging.h"
11
12 #include "impeller/core/allocator.h"
13 #include "impeller/core/buffer_view.h"
14 #include "impeller/core/device_buffer.h"
15
16 namespace impeller {
17
18 std::shared_ptr<HostBuffer> HostBuffer::Create() {
19 return std::shared_ptr<HostBuffer>(new HostBuffer());
20 }
21
22 HostBuffer::HostBuffer() = default;
23
24 HostBuffer::~HostBuffer() = default;
25
26 void HostBuffer::SetLabel(std::string label) {
27 state_->label = std::move(label);
28 }
29
30 BufferView HostBuffer::Emplace(const void* buffer,
31 size_t length,
32 size_t align) {
33 auto [device_buffer, range] = state_->Emplace(buffer, length, align);
34 if (!device_buffer) {
35 return {};
36 }
37 return BufferView{state_, device_buffer, range};
38 }
39
40 BufferView HostBuffer::Emplace(const void* buffer, size_t length) {
41 auto [device_buffer, range] = state_->Emplace(buffer, length);
42 if (!device_buffer) {
43 return {};
44 }
45 return BufferView{state_, device_buffer, range};
46 }
47
48 BufferView HostBuffer::Emplace(size_t length,
49 size_t align,
50 const EmplaceProc& cb) {
51 auto [buffer, range] = state_->Emplace(length, align, cb);
52 if (!buffer) {
53 return {};
54 }
55 return BufferView{state_, buffer, range};
56 }
57
58 std::shared_ptr<const DeviceBuffer> HostBuffer::GetDeviceBuffer(
59 Allocator& allocator) const {
60 return state_->GetDeviceBuffer(allocator);
61 }
62
63 void HostBuffer::Reset() {
64 state_->Reset();
65 }
66
67 size_t HostBuffer::GetSize() const {
68 return state_->GetReservedLength();
69 }
70
71 size_t HostBuffer::GetLength() const {
72 return state_->GetLength();
73 }
74
75 std::pair<uint8_t*, Range> HostBuffer::HostBufferState::Emplace(
76 size_t length,
77 size_t align,
78 const EmplaceProc& cb) {
79 if (!cb) {
80 return {};
81 }
82 auto old_length = GetLength();
83 if (!Truncate(old_length + length)) {
84 return {};
85 }
86 generation++;
87 cb(GetBuffer() + old_length);
88
89 return std::make_pair(GetBuffer(), Range{old_length, length});
90 }
91
92 std::shared_ptr<const DeviceBuffer>
93 HostBuffer::HostBufferState::GetDeviceBuffer(Allocator& allocator) const {
94 if (generation == device_buffer_generation) {
95 return device_buffer;
96 }
97 auto new_buffer = allocator.CreateBufferWithCopy(GetBuffer(), GetLength());
98 if (!new_buffer) {
99 return nullptr;
100 }
101 new_buffer->SetLabel(label);
102 device_buffer_generation = generation;
103 device_buffer = std::move(new_buffer);
104 return device_buffer;
105 }
106
107 std::pair<uint8_t*, Range> HostBuffer::HostBufferState::Emplace(
108 const void* buffer,
109 size_t length) {
110 auto old_length = GetLength();
111 if (!Truncate(old_length + length)) {
112 return {};
113 }
114 generation++;
115 if (buffer) {
116 ::memmove(GetBuffer() + old_length, buffer, length);
117 }
118 return std::make_pair(GetBuffer(), Range{old_length, length});
119 }
120
121 std::pair<uint8_t*, Range> HostBuffer::HostBufferState::Emplace(
122 const void* buffer,
123 size_t length,
124 size_t align) {
125 if (align == 0 || (GetLength() % align) == 0) {
126 return Emplace(buffer, length);
127 }
128
129 {
130 auto [buffer, range] = Emplace(nullptr, align - (GetLength() % align));
131 if (!buffer) {
132 return {};
133 }
134 }
135
136 return Emplace(buffer, length);
137 }
138
139 void HostBuffer::HostBufferState::Reset() {
140 generation += 1;
141 device_buffer = nullptr;
142 bool did_truncate = Truncate(0);
143 FML_CHECK(did_truncate);
144 }
145
146 } // namespace impeller
impeller::HostBuffer::EmplaceProc
std::function< void(uint8_t *buffer)> EmplaceProc
Definition: host_buffer.h:96
host_buffer.h
device_buffer.h
impeller::HostBuffer
Definition: host_buffer.h:20
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:85
impeller::HostBuffer::GetSize
size_t GetSize() const
Returns the capacity of the HostBuffer in memory in bytes.
Definition: host_buffer.cc:67
impeller::HostBuffer::Create
static std::shared_ptr< HostBuffer > Create()
Definition: host_buffer.cc:18
impeller::HostBuffer::~HostBuffer
virtual ~HostBuffer()
impeller::HostBuffer::GetLength
size_t GetLength() const
Returns the size of the currently allocated HostBuffer memory in bytes.
Definition: host_buffer.cc:71
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:22
allocator.h
impeller::BufferView
Definition: buffer_view.h:13
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:63
impeller::HostBuffer::SetLabel
void SetLabel(std::string label)
Definition: host_buffer.cc:26
impeller
Definition: aiks_context.cc:10