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 
6 
7 #include <algorithm>
8 #include <cstring>
9 
10 #include "flutter/fml/logging.h"
11 
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  label_ = std::move(label);
28 }
29 
30 BufferView HostBuffer::Emplace(const void* buffer,
31  size_t length,
32  size_t align) {
33  if (align == 0 || (GetLength() % align) == 0) {
34  return Emplace(buffer, length);
35  }
36 
37  {
38  auto pad = Emplace(nullptr, align - (GetLength() % align));
39  if (!pad) {
40  return {};
41  }
42  }
43 
44  return Emplace(buffer, length);
45 }
46 
47 BufferView HostBuffer::Emplace(const void* buffer, size_t length) {
48  auto old_length = GetLength();
49  if (!Truncate(old_length + length)) {
50  return {};
51  }
52  generation_++;
53  if (buffer) {
54  ::memmove(GetBuffer() + old_length, buffer, length);
55  }
56  return BufferView{shared_from_this(), GetBuffer(), Range{old_length, length}};
57 }
58 
60  size_t align,
61  const EmplaceProc& cb) {
62  if (!cb) {
63  return {};
64  }
65  auto old_length = GetLength();
66  if (!Truncate(old_length + length)) {
67  return {};
68  }
69  generation_++;
70  cb(GetBuffer() + old_length);
71 
72  return BufferView{shared_from_this(), GetBuffer(), Range{old_length, length}};
73 }
74 
75 std::shared_ptr<const DeviceBuffer> HostBuffer::GetDeviceBuffer(
76  Allocator& allocator) const {
77  if (generation_ == device_buffer_generation_) {
78  return device_buffer_;
79  }
80  auto new_buffer = allocator.CreateBufferWithCopy(GetBuffer(), GetLength());
81  if (!new_buffer) {
82  return nullptr;
83  }
84  new_buffer->SetLabel(label_);
85  device_buffer_generation_ = generation_;
86  device_buffer_ = std::move(new_buffer);
87  return device_buffer_;
88 }
89 
91  generation_ += 1;
92  device_buffer_ = nullptr;
93  bool did_truncate = Truncate(0);
94  FML_CHECK(did_truncate);
95 }
96 
97 size_t HostBuffer::GetSize() const {
98  return GetReservedLength();
99 }
100 
101 } // namespace impeller
impeller::HostBuffer::EmplaceProc
std::function< void(uint8_t *buffer)> EmplaceProc
Definition: host_buffer.h:98
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:87
impeller::HostBuffer::GetSize
size_t GetSize() const
Returns the size of the HostBuffer in memory in bytes.
Definition: host_buffer.cc:97
impeller::Allocation::GetReservedLength
size_t GetReservedLength() const
Definition: allocation.cc:29
impeller::HostBuffer::Create
static std::shared_ptr< HostBuffer > Create()
Definition: host_buffer.cc:18
impeller::Allocation::GetLength
size_t GetLength() const
Definition: allocation.cc:25
impeller::HostBuffer::~HostBuffer
virtual ~HostBuffer()
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:25
impeller::Allocation::GetBuffer
uint8_t * GetBuffer() const
Definition: allocation.cc:21
impeller::Allocator::CreateBufferWithCopy
std::shared_ptr< DeviceBuffer > CreateBufferWithCopy(const uint8_t *buffer, size_t length)
Definition: allocator.cc:18
allocator.h
impeller::BufferView
Definition: buffer_view.h:13
impeller::Range
Definition: range.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:90
impeller::HostBuffer::SetLabel
void SetLabel(std::string label)
Definition: host_buffer.cc:26
impeller
Definition: aiks_context.cc:10
impeller::Allocation::Truncate
bool Truncate(size_t length, bool npot=true)
Definition: allocation.cc:33