Jason Macnak | a296e68 | 2022-10-27 16:21:13 -0700 | [diff] [blame] | 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 { |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 20 | namespace vk { |
Jason Macnak | a296e68 | 2022-10-27 16:21:13 -0700 | [diff] [blame] | 21 | |
| 22 | /*static*/ |
| 23 | std::unique_ptr<BufferVk> BufferVk::create(uint32_t handle, uint64_t size, bool vulkanOnly) { |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 24 | if (!setupVkBuffer(size, handle, vulkanOnly, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) { |
Jason Macnak | a296e68 | 2022-10-27 16:21:13 -0700 | [diff] [blame] | 25 | ERR("Failed to create BufferVk:%d", handle); |
| 26 | return nullptr; |
| 27 | } |
| 28 | |
| 29 | return std::unique_ptr<BufferVk>(new BufferVk(handle)); |
| 30 | } |
| 31 | |
| 32 | BufferVk::BufferVk(uint32_t handle) : mHandle(handle) {} |
| 33 | |
| 34 | BufferVk::~BufferVk() { |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 35 | if (!teardownVkBuffer(mHandle)) { |
Jason Macnak | a296e68 | 2022-10-27 16:21:13 -0700 | [diff] [blame] | 36 | ERR("Failed to destroy BufferVk:%d", mHandle); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void BufferVk::readToBytes(uint64_t offset, uint64_t size, void* outBytes) { |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 41 | readBufferToBytes(mHandle, offset, size, outBytes); |
Jason Macnak | a296e68 | 2022-10-27 16:21:13 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool BufferVk::updateFromBytes(uint64_t offset, uint64_t size, const void* bytes) { |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 45 | return updateBufferFromBytes(mHandle, offset, size, bytes); |
Jason Macnak | a296e68 | 2022-10-27 16:21:13 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Gurchetan Singh | 619646f | 2024-06-28 09:30:12 -0700 | [diff] [blame] | 48 | std::optional<BlobDescriptorInfo> BufferVk::exportBlob() { |
Gurchetan Singh | b86aa8b | 2024-06-24 17:19:56 -0700 | [diff] [blame] | 49 | uint32_t streamHandleType = 0; |
| 50 | auto vkHandle = getBufferExtMemoryHandle(mHandle, &streamHandleType); |
| 51 | if (vkHandle != VK_EXT_MEMORY_HANDLE_INVALID) { |
| 52 | ManagedDescriptor descriptor(dupExternalMemory(vkHandle)); |
Gurchetan Singh | 619646f | 2024-06-28 09:30:12 -0700 | [diff] [blame] | 53 | return BlobDescriptorInfo{ |
Gurchetan Singh | b86aa8b | 2024-06-24 17:19:56 -0700 | [diff] [blame] | 54 | .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 Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 64 | } // namespace vk |
Gurchetan Singh | b86aa8b | 2024-06-24 17:19:56 -0700 | [diff] [blame] | 65 | } // namespace gfxstream |