blob: 234a6294bd5fc4e29a2876952b149d1b81e64187 [file] [log] [blame]
Jason Macnaka296e682022-10-27 16:21:13 -07001// 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
19namespace gfxstream {
Jason Macnaked0c9e62023-03-30 15:58:24 -070020namespace vk {
Jason Macnaka296e682022-10-27 16:21:13 -070021
22/*static*/
23std::unique_ptr<BufferVk> BufferVk::create(uint32_t handle, uint64_t size, bool vulkanOnly) {
Jason Macnaked0c9e62023-03-30 15:58:24 -070024 if (!setupVkBuffer(size, handle, vulkanOnly, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) {
Jason Macnaka296e682022-10-27 16:21:13 -070025 ERR("Failed to create BufferVk:%d", handle);
26 return nullptr;
27 }
28
29 return std::unique_ptr<BufferVk>(new BufferVk(handle));
30}
31
32BufferVk::BufferVk(uint32_t handle) : mHandle(handle) {}
33
34BufferVk::~BufferVk() {
Jason Macnaked0c9e62023-03-30 15:58:24 -070035 if (!teardownVkBuffer(mHandle)) {
Jason Macnaka296e682022-10-27 16:21:13 -070036 ERR("Failed to destroy BufferVk:%d", mHandle);
37 }
38}
39
40void BufferVk::readToBytes(uint64_t offset, uint64_t size, void* outBytes) {
Jason Macnaked0c9e62023-03-30 15:58:24 -070041 readBufferToBytes(mHandle, offset, size, outBytes);
Jason Macnaka296e682022-10-27 16:21:13 -070042}
43
44bool BufferVk::updateFromBytes(uint64_t offset, uint64_t size, const void* bytes) {
Jason Macnaked0c9e62023-03-30 15:58:24 -070045 return updateBufferFromBytes(mHandle, offset, size, bytes);
Jason Macnaka296e682022-10-27 16:21:13 -070046}
47
Gurchetan Singh619646f2024-06-28 09:30:12 -070048std::optional<BlobDescriptorInfo> BufferVk::exportBlob() {
Gurchetan Singhb86aa8b2024-06-24 17:19:56 -070049 uint32_t streamHandleType = 0;
50 auto vkHandle = getBufferExtMemoryHandle(mHandle, &streamHandleType);
51 if (vkHandle != VK_EXT_MEMORY_HANDLE_INVALID) {
52 ManagedDescriptor descriptor(dupExternalMemory(vkHandle));
Gurchetan Singh619646f2024-06-28 09:30:12 -070053 return BlobDescriptorInfo{
Gurchetan Singhb86aa8b2024-06-24 17:19:56 -070054 .descriptor = std::move(descriptor),
55 .handleType = streamHandleType,
56 .caching = 0,
57 .vulkanInfoOpt = std::nullopt,
58 };
59 } else {
60 return std::nullopt;
61 }
62}
63
Jason Macnaked0c9e62023-03-30 15:58:24 -070064} // namespace vk
Gurchetan Singhb86aa8b2024-06-24 17:19:56 -070065} // namespace gfxstream