| 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
5 #include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
6
7 #include "flutter/flutter_vma/flutter_vma.h"
8 #include "flutter/fml/trace_event.h"
9 #include "impeller/renderer/backend/vulkan/context_vk.h"
10 #include "vulkan/vulkan_core.h"
11
12 namespace impeller {
13
14 DeviceBufferVK::DeviceBufferVK(DeviceBufferDescriptor desc,
15 std::weak_ptr<Context> context,
16 UniqueBufferVMA buffer,
17 VmaAllocationInfo info)
18 : DeviceBuffer(desc),
19 context_(std::move(context)),
20 resource_(ContextVK::Cast(*context_.lock().get()).GetResourceManager(),
21 BufferResource{
22 std::move(buffer), //
23 info //
24 }) {}
25
26 DeviceBufferVK::~DeviceBufferVK() = default;
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(const std::string& label) {
52 auto context = context_.lock();
53 if (!context || !resource_->buffer.is_valid()) {
54 // The context could have died at this point.
55 return false;
56 }
57
58 ::vmaSetAllocationName(resource_->buffer.get().allocator, //
59 resource_->buffer.get().allocation, //
60 label.c_str() //
61 );
62
63 return ContextVK::Cast(*context).SetDebugName(resource_->buffer.get().buffer,
64 label);
65 }
66
67 void DeviceBufferVK::Flush(std::optional<Range> range) const {
68 auto flush_range = range.value_or(Range{0, GetDeviceBufferDescriptor().size});
69 ::vmaFlushAllocation(resource_->buffer.get().allocator,
70 resource_->buffer.get().allocation, flush_range.offset,
71 flush_range.length);
72 }
73
74 void DeviceBufferVK::Invalidate(std::optional<Range> range) const {
75 auto flush_range = range.value_or(Range{0, GetDeviceBufferDescriptor().size});
76 ::vmaInvalidateAllocation(resource_->buffer.get().allocator,
77 resource_->buffer.get().allocation,
78 flush_range.offset, flush_range.length);
79 }
80
81 bool DeviceBufferVK::SetLabel(const std::string& label, Range range) {
82 // We do not have the ability to name ranges. Just name the whole thing.
83 return SetLabel(label);
84 }
85
86 vk::Buffer DeviceBufferVK::GetBuffer() const {
87 return resource_->buffer.get().buffer;
88 }
89
90 } // namespace impeller
impeller::DeviceBufferVK::GetBuffer
vk::Buffer GetBuffer() const
Definition: device_buffer_vk.cc:86
impeller::DeviceBuffer
Definition: device_buffer.h:18
impeller::DeviceBufferVK::DeviceBufferVK
DeviceBufferVK(DeviceBufferDescriptor desc, std::weak_ptr< Context > context, UniqueBufferVMA buffer, VmaAllocationInfo info)
Definition: device_buffer_vk.cc:14
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::DeviceBufferVK::~DeviceBufferVK
~DeviceBufferVK() override
impeller::DeviceBufferDescriptor::size
size_t size
Definition: device_buffer_descriptor.h:16
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:311
impeller::DeviceBuffer::GetDeviceBufferDescriptor
const DeviceBufferDescriptor & GetDeviceBufferDescriptor() const
Definition: device_buffer.cc:25
impeller::ContextVK::SetDebugName
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:108
impeller::ContextVK
Definition: context_vk.h:42
std
Definition: comparable.h:95
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
device_buffer_vk.h
context_vk.h
impeller::UniqueBufferVMA
fml::UniqueObject< BufferVMA, BufferVMATraits > UniqueBufferVMA
Definition: vma.h:98
impeller
Definition: aiks_blend_unittests.cc:18
impeller::BufferResource
Resource< BufferView > BufferResource
Definition: command.h:57