1 /*
2  * Copyright (c) 2024 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 "gpu_semaphore_vk.h"
17 
18 #include <cinttypes>
19 #include <vulkan/vulkan_core.h>
20 
21 #include <render/namespace.h>
22 
23 #include "device/device.h"
24 #include "vulkan/device_vk.h"
25 #include "vulkan/validate_vk.h"
26 
RENDER_BEGIN_NAMESPACE()27 RENDER_BEGIN_NAMESPACE()
28 GpuSemaphoreVk::GpuSemaphoreVk(Device& device) : device_(device)
29 {
30     constexpr VkSemaphoreCreateFlags semaphoreCreateFlags { 0 };
31     constexpr VkSemaphoreCreateInfo semaphoreCreateInfo {
32         VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, // sType
33         nullptr,                                 // pNext
34         semaphoreCreateFlags,                    // flags
35     };
36 
37     const VkDevice vkDev = ((const DevicePlatformDataVk&)device_.GetPlatformData()).device;
38     VALIDATE_VK_RESULT(vkCreateSemaphore(vkDev, // device
39         &semaphoreCreateInfo,                   // pCreateInfo
40         nullptr,                                // pAllocator
41         &plat_.semaphore));                     // pSemaphore
42 }
43 
GpuSemaphoreVk(Device & device,const uint64_t handle)44 GpuSemaphoreVk::GpuSemaphoreVk(Device& device, const uint64_t handle) : device_(device), ownsResources_(false)
45 {
46     // do not let invalid inputs in
47     PLUGIN_ASSERT(handle != 0);
48     if (handle) {
49         plat_.semaphore = VulkanHandleCast<VkSemaphore>(handle);
50     }
51 }
52 
~GpuSemaphoreVk()53 GpuSemaphoreVk::~GpuSemaphoreVk()
54 {
55     if (ownsResources_ && plat_.semaphore) {
56         const VkDevice device = ((const DevicePlatformDataVk&)device_.GetPlatformData()).device;
57 
58         vkDestroySemaphore(device, // device
59             plat_.semaphore,       // semaphore
60             nullptr);              // pAllocator
61     }
62     plat_.semaphore = VK_NULL_HANDLE;
63 }
64 
GetHandle() const65 uint64_t GpuSemaphoreVk::GetHandle() const
66 {
67     return VulkanHandleCast<uint64_t>(plat_.semaphore);
68 }
69 
GetPlatformData() const70 const GpuSemaphorePlatformDataVk& GpuSemaphoreVk::GetPlatformData() const
71 {
72     return plat_;
73 }
74 RENDER_END_NAMESPACE()
75