Flutter Impeller
device_buffer_vk.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 "flutter/flutter_vma/flutter_vma.h"
9 
10 namespace impeller {
11 
13  std::weak_ptr<Context> context,
14  UniqueBufferVMA buffer,
15  VmaAllocationInfo info,
16  bool is_host_coherent)
17  : DeviceBuffer(desc),
18  context_(std::move(context)),
19  resource_(ContextVK::Cast(*context_.lock().get()).GetResourceManager(),
21  std::move(buffer), //
22  info //
23  }),
24  is_host_coherent_(is_host_coherent) {}
25 
27 
28 uint8_t* DeviceBufferVK::OnGetContents() const {
29  return static_cast<uint8_t*>(resource_->info.pMappedData);
30 }
31 
32 bool DeviceBufferVK::OnCopyHostBuffer(const uint8_t* source,
33  Range source_range,
34  size_t offset) {
35  uint8_t* dest = OnGetContents();
36 
37  if (!dest) {
38  return false;
39  }
40 
41  if (source) {
42  ::memmove(dest + offset, source + source_range.offset, source_range.length);
43  }
44  ::vmaFlushAllocation(resource_->buffer.get().allocator,
45  resource_->buffer.get().allocation, offset,
46  source_range.length);
47 
48  return true;
49 }
50 
51 bool DeviceBufferVK::SetLabel(std::string_view label) {
52 #ifdef IMPELLER_DEBUG
53  auto context = context_.lock();
54  if (!context || !resource_->buffer.is_valid()) {
55  // The context could have died at this point.
56  return false;
57  }
58 
59  ::vmaSetAllocationName(resource_->buffer.get().allocator, //
60  resource_->buffer.get().allocation, //
61  label.data() //
62  );
63 
64  return ContextVK::Cast(*context).SetDebugName(resource_->buffer.get().buffer,
65  label);
66 #else
67  return true;
68 #endif // IMPELLER_DEBUG
69 }
70 
71 void DeviceBufferVK::Flush(std::optional<Range> range) const {
72  if (is_host_coherent_) {
73  return;
74  }
75  auto flush_range = range.value_or(Range{0, GetDeviceBufferDescriptor().size});
76  ::vmaFlushAllocation(resource_->buffer.get().allocator,
77  resource_->buffer.get().allocation, flush_range.offset,
78  flush_range.length);
79 }
80 
81 // Visible for testing.
83  return is_host_coherent_;
84 }
85 
86 void DeviceBufferVK::Invalidate(std::optional<Range> range) const {
87  auto flush_range = range.value_or(Range{0, GetDeviceBufferDescriptor().size});
88  ::vmaInvalidateAllocation(resource_->buffer.get().allocator,
89  resource_->buffer.get().allocation,
90  flush_range.offset, flush_range.length);
91 }
92 
93 bool DeviceBufferVK::SetLabel(std::string_view label, Range range) {
94  // We do not have the ability to name ranges. Just name the whole thing.
95  return SetLabel(label);
96 }
97 
98 vk::Buffer DeviceBufferVK::GetBuffer() const {
99  return resource_->buffer.get().buffer;
100 }
101 
102 } // namespace impeller
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:151
const DeviceBufferDescriptor & GetDeviceBufferDescriptor() const
DeviceBufferVK(DeviceBufferDescriptor desc, std::weak_ptr< Context > context, UniqueBufferVMA buffer, VmaAllocationInfo info, bool is_host_coherent)
vk::Buffer GetBuffer() const
Resource< BufferView > BufferResource
Definition: command.h:54
fml::UniqueObject< BufferVMA, BufferVMATraits > UniqueBufferVMA
Definition: vma.h:98
Definition: comparable.h:95