1 // Copyright 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "BufferVk.h"
16
17 #include "VkCommonOperations.h"
18
19 namespace gfxstream {
20 namespace vk {
21
22 /*static*/
create(VkEmulation & vkEmulation,uint32_t handle,uint64_t size,bool vulkanOnly)23 std::unique_ptr<BufferVk> BufferVk::create(VkEmulation& vkEmulation, uint32_t handle, uint64_t size,
24 bool vulkanOnly) {
25 if (!vkEmulation.setupVkBuffer(size, handle, vulkanOnly, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) {
26 ERR("Failed to create BufferVk:%d", handle);
27 return nullptr;
28 }
29
30 return std::unique_ptr<BufferVk>(new BufferVk(vkEmulation, handle));
31 }
32
BufferVk(VkEmulation & vkEmulation,uint32_t handle)33 BufferVk::BufferVk(VkEmulation& vkEmulation, uint32_t handle)
34 : mVkEmulation(vkEmulation), mHandle(handle) {}
35
~BufferVk()36 BufferVk::~BufferVk() {
37 if (!mVkEmulation.teardownVkBuffer(mHandle)) {
38 ERR("Failed to destroy BufferVk:%d", mHandle);
39 }
40 }
41
readToBytes(uint64_t offset,uint64_t size,void * outBytes)42 void BufferVk::readToBytes(uint64_t offset, uint64_t size, void* outBytes) {
43 mVkEmulation.readBufferToBytes(mHandle, offset, size, outBytes);
44 }
45
updateFromBytes(uint64_t offset,uint64_t size,const void * bytes)46 bool BufferVk::updateFromBytes(uint64_t offset, uint64_t size, const void* bytes) {
47 return mVkEmulation.updateBufferFromBytes(mHandle, offset, size, bytes);
48 }
49
exportBlob()50 std::optional<BlobDescriptorInfo> BufferVk::exportBlob() {
51 auto dupHandleInfo = mVkEmulation.dupBufferExtMemoryHandle(mHandle);
52 if (!dupHandleInfo) {
53 return std::nullopt;
54 }
55 return BlobDescriptorInfo{
56 .descriptorInfo =
57 {
58 #ifdef _WIN32
59 .descriptor = ManagedDescriptor(
60 static_cast<DescriptorType>(reinterpret_cast<void*>(dupHandleInfo->handle))),
61 #else
62 .descriptor = ManagedDescriptor(static_cast<DescriptorType>(dupHandleInfo->handle)),
63 #endif
64 .streamHandleType = dupHandleInfo->streamHandleType,
65 },
66 .caching = 0,
67 .vulkanInfoOpt = std::nullopt,
68 };
69 }
70
71 } // namespace vk
72 } // namespace gfxstream
73