1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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 express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <vulkan/vulkan.h>
17
18 #include "vulkan/device_vk.h"
19 #include "vulkan/gpu_buffer_vk.h"
20 #include "vulkan/platform_hardware_buffer_util_vk.h"
21 #include "vulkan/validate_vk.h"
22
RENDER_BEGIN_NAMESPACE()23 RENDER_BEGIN_NAMESPACE()
24 void GpuBufferVk::CreatePlatformHwBuffer()
25 {
26 const auto& deviceVk = static_cast<const DeviceVk&>(device_);
27 if (!plat_.platformHwBuffer || !deviceVk.GetPlatformDeviceExtensions().externalMemoryHardwareBuffer) {
28 return;
29 }
30 const PlatformExtFunctions& extFunctions = deviceVk.GetPlatformExtFunctions();
31 if (!extFunctions.vkGetNativeBufferPropertiesOHOS) {
32 return;
33 }
34 const auto& platData = (const DevicePlatformDataVk&)deviceVk.GetPlatformData();
35 VkDevice device = platData.device;
36
37 VkNativeBufferPropertiesOHOS bufferProperties {};
38 bufferProperties.sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_PROPERTIES_OHOS;
39
40 OH_NativeBuffer* nativeBuffer = static_cast<OH_NativeBuffer*>(reinterpret_cast<void*>(plat_.platformHwBuffer));
41 VALIDATE_VK_RESULT(extFunctions.vkGetNativeBufferPropertiesOHOS(device, // device
42 nativeBuffer, // buffer
43 &bufferProperties)); // pProperties
44
45 PlatformHardwareBufferUtil::HardwareBufferProperties hardwareBufferProperties;
46 hardwareBufferProperties.allocationSize = bufferProperties.allocationSize;
47 hardwareBufferProperties.memoryTypeBits = bufferProperties.memoryTypeBits;
48
49 if (hardwareBufferProperties.allocationSize > 0) {
50 PlatformHardwareBufferUtil::HardwareBufferBuffer hwBufferImage =
51 PlatformHardwareBufferUtil::CreateHwPlatformBuffer(
52 deviceVk, hardwareBufferProperties, desc_, plat_.platformHwBuffer);
53 PLUGIN_ASSERT((hwBufferImage.buffer != VK_NULL_HANDLE) && ((hwBufferImage.deviceMemory != VK_NULL_HANDLE)));
54 plat_.buffer = hwBufferImage.buffer;
55 plat_.bindMemoryByteSize = hardwareBufferProperties.allocationSize;
56 plat_.fullByteSize = hardwareBufferProperties.allocationSize;
57 plat_.currentByteOffset = 0;
58 plat_.usage = static_cast<VkBufferUsageFlags>(desc_.usageFlags);
59 mem_.allocationInfo.deviceMemory = hwBufferImage.deviceMemory;
60 } else {
61 plat_.platformHwBuffer = 0;
62 }
63 }
64
DestroyPlatformHwBuffer()65 void GpuBufferVk::DestroyPlatformHwBuffer()
66 {
67 const auto& deviceVk = (const DeviceVk&)device_;
68 PlatformHardwareBufferUtil::DestroyHwPlatformBuffer(deviceVk, plat_.buffer, mem_.allocationInfo.deviceMemory);
69 }
70 RENDER_END_NAMESPACE()
71