• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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/validate_vk.h"
25 
RENDER_BEGIN_NAMESPACE()26 RENDER_BEGIN_NAMESPACE()
27 GpuSemaphoreVk::GpuSemaphoreVk(Device& device) : device_(device)
28 {
29     constexpr VkSemaphoreCreateFlags semaphoreCreateFlags { 0 };
30     constexpr VkSemaphoreCreateInfo semaphoreCreateInfo {
31         VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, // sType
32         nullptr,                                 // pNext
33         semaphoreCreateFlags,                    // flags
34     };
35 
36     const VkDevice vkDev = ((const DevicePlatformDataVk&)device_.GetPlatformData()).device;
37     VALIDATE_VK_RESULT(vkCreateSemaphore(vkDev, // device
38         &semaphoreCreateInfo,                   // pCreateInfo
39         nullptr,                                // pAllocator
40         &plat_.semaphore));                     // pSemaphore
41 }
42 
GpuSemaphoreVk(Device & device,const uint64_t handle)43 GpuSemaphoreVk::GpuSemaphoreVk(Device& device, const uint64_t handle) : device_(device), ownsResources_(false)
44 {
45     // do not let invalid inputs in
46     PLUGIN_ASSERT(handle != 0);
47     if (handle) {
48         plat_.semaphore = VulkanHandleCast<VkSemaphore>(handle);
49     }
50 }
51 
~GpuSemaphoreVk()52 GpuSemaphoreVk::~GpuSemaphoreVk()
53 {
54     if (ownsResources_ && plat_.semaphore) {
55         const VkDevice device = ((const DevicePlatformDataVk&)device_.GetPlatformData()).device;
56 
57         vkDestroySemaphore(device, // device
58             plat_.semaphore,       // semaphore
59             nullptr);              // pAllocator
60     }
61     plat_.semaphore = VK_NULL_HANDLE;
62 }
63 
GetHandle() const64 uint64_t GpuSemaphoreVk::GetHandle() const
65 {
66     return VulkanHandleCast<uint64_t>(plat_.semaphore);
67 }
68 
GetPlatformData() const69 const GpuSemaphorePlatformDataVk& GpuSemaphoreVk::GetPlatformData() const
70 {
71     return plat_;
72 }
73 RENDER_END_NAMESPACE()
74