2 * Copyright (c) 2021 Arm Limited.
4 * SPDX-License-Identifier: MIT
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * @brief Contains the implementation for WSI synchronization primitives.
31 #include "synchronization.hpp"
32 #include "layer/private_data.hpp"
37 fence_sync::fence_sync(layer::device_private_data &device, VkFence vk_fence)
39 , has_payload{ false }
44 util::optional<fence_sync> fence_sync::create(layer::device_private_data &device)
46 VkFence fence{ VK_NULL_HANDLE };
47 VkFenceCreateInfo fence_info{ VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, 0 };
49 device.disp.CreateFence(device.device, &fence_info, device.get_allocator().get_original_callbacks(), &fence);
50 if (res != VK_SUCCESS)
54 return fence_sync(device, fence);
57 fence_sync::fence_sync(fence_sync &&rhs)
59 *this = std::move(rhs);
62 fence_sync &fence_sync::operator=(fence_sync &&rhs)
64 std::swap(fence, rhs.fence);
65 std::swap(has_payload, rhs.has_payload);
66 std::swap(payload_finished, rhs.payload_finished);
67 std::swap(dev, rhs.dev);
71 fence_sync::~fence_sync()
73 if (fence != VK_NULL_HANDLE)
75 wait_payload(UINT64_MAX);
76 dev->disp.DestroyFence(dev->device, fence, dev->get_allocator().get_original_callbacks());
80 VkResult fence_sync::wait_payload(uint64_t timeout)
82 VkResult res = VK_SUCCESS;
83 if (has_payload && !payload_finished)
85 res = dev->disp.WaitForFences(dev->device, 1, &fence, VK_TRUE, timeout);
86 if (res == VK_SUCCESS)
88 payload_finished = true;
94 VkResult fence_sync::set_payload(VkQueue queue, const VkSemaphore *sem_payload, uint32_t sem_count)
96 VkResult result = dev->disp.ResetFences(dev->device, 1, &fence);
97 if (result != VK_SUCCESS)
102 /* When the semaphore that comes in is signalled, we know that all work is done. So, we do not
103 * want to block any future Vulkan queue work on it. So, we pass in BOTTOM_OF_PIPE bit as the
106 VkPipelineStageFlags pipeline_stage_flags = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
108 VkSubmitInfo submit_info = {
109 VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, sem_count, sem_payload, &pipeline_stage_flags, 0, NULL, 0, NULL
112 result = dev->disp.QueueSubmit(queue, 1, &submit_info, fence);
113 if (result == VK_SUCCESS)
116 payload_finished = false;
121 bool fence_sync::swap_payload(bool new_payload)
123 bool old_payload = has_payload;
124 has_payload = new_payload;
125 payload_finished = false;
129 sync_fd_fence_sync::sync_fd_fence_sync(layer::device_private_data &device, VkFence vk_fence)
130 : fence_sync{ device, vk_fence }
134 bool sync_fd_fence_sync::is_supported(layer::instance_private_data &instance, VkPhysicalDevice phys_dev)
136 VkPhysicalDeviceExternalFenceInfoKHR external_fence_info = {};
137 external_fence_info.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO;
138 external_fence_info.handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
139 VkExternalFencePropertiesKHR fence_properties = {};
140 fence_properties.sType = VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES;
141 instance.disp.GetPhysicalDeviceExternalFencePropertiesKHR(phys_dev, &external_fence_info, &fence_properties);
142 return fence_properties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_EXPORTABLE_BIT_KHR;
145 util::optional<sync_fd_fence_sync> sync_fd_fence_sync::create(layer::device_private_data &device)
147 VkExportFenceCreateInfo export_fence_create_info = {};
148 export_fence_create_info.sType = VK_STRUCTURE_TYPE_EXPORT_FENCE_CREATE_INFO;
149 export_fence_create_info.handleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
150 VkFenceCreateInfo fence_info = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, &export_fence_create_info, 0 };
151 VkFence fence = VK_NULL_HANDLE;
153 device.disp.CreateFence(device.device, &fence_info, device.get_allocator().get_original_callbacks(), &fence);
154 if (res != VK_SUCCESS)
158 return sync_fd_fence_sync{ device, fence };
161 util::optional<util::fd_owner> sync_fd_fence_sync::export_sync_fd()
163 int exported_fd = -1;
164 VkFenceGetFdInfoKHR fence_fd_info = {};
165 fence_fd_info.sType = VK_STRUCTURE_TYPE_FENCE_GET_FD_INFO_KHR;
166 fence_fd_info.fence = get_fence();
167 fence_fd_info.handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
169 VkResult result = get_device().disp.GetFenceFdKHR(get_device().device, &fence_fd_info, &exported_fd);
170 if (result == VK_SUCCESS)
173 return util::fd_owner(exported_fd);
178 } /* namespace wsi */