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 defintions of WSI synchronization primitives.
33 #include "util/file_descriptor.hpp"
34 #include "util/optional.hpp"
36 #include <vulkan/vulkan.h>
40 class device_private_data;
41 class instance_private_data;
42 } /* namespace layer */
48 * Synchronization using a Vulkan Fence object.
54 * Creates a new fence synchronization object.
56 * @param device The device private data for which to create it.
58 * @return Empty optional on failure or initialized fence.
60 static util::optional<fence_sync> create(layer::device_private_data &device);
62 /** Default constructor provided for use with @ref util::optional */
63 fence_sync() = default;
64 fence_sync(const fence_sync &) = delete;
65 fence_sync &operator=(const fence_sync &) = delete;
67 fence_sync(fence_sync &&rhs);
68 fence_sync &operator=(fence_sync &&rhs);
70 virtual ~fence_sync();
73 * Waits for any pending payload to complete execution.
75 * @note This method is not threadsafe.
77 * @param timeout Timeout for waiting in nanoseconds.
79 * @return VK_SUCCESS on success or if no payload or a completed payload is set.
80 * Other error code on failure or timeout.
82 VkResult wait_payload(uint64_t timeout);
85 * Sets the payload for the fence that would need to complete before operations that wait on it.
87 * @note This method is not threadsafe.
89 * @param queue The Vulkan queue that may be used to submit synchronization commands.
90 * @param[in] sem_payload Array of Vulkan Semaphores that comprise the payload.
91 * @param sem_count Number of elements in @p sem_payload.
93 * @return VK_SUCCESS on success or other error code on failing to set the payload.
95 VkResult set_payload(VkQueue queue, const VkSemaphore *sem_payload, uint32_t sem_count);
99 * Non-public constructor to initialize the object with valid data.
101 * @param device The device private data for the fence.
102 * @param vk_fence The created Vulkan fence.
104 fence_sync(layer::device_private_data &device, VkFence vk_fence);
112 * Swaps current payload. This operation could be performed when exporting or importing external fences.
114 * @param new_payload Whether a new payload is set.
116 * @return If there is an existing payload that is being replaced.
118 bool swap_payload(bool new_payload);
120 layer::device_private_data &get_device()
126 VkFence fence{ VK_NULL_HANDLE };
127 bool has_payload{ false };
128 bool payload_finished{ false };
129 layer::device_private_data *dev{ nullptr };
133 * Synchronization using a Vulkan fence exportable to a native Sync FD object.
135 class sync_fd_fence_sync : public fence_sync
138 /** Default constructor provided for use with @ref util::optional */
139 sync_fd_fence_sync() = default;
142 * Checks if a Vulkan device can support Sync FD fences.
144 * @param instance The instance private data for the physical device.
145 * @param phys_dev The physical device to check support for.
147 * @return true if supported, false otherwise.
149 static bool is_supported(layer::instance_private_data &instance, VkPhysicalDevice phys_dev);
152 * Creates a new fence compatible with Sync FD.
154 * @param device The device private data for which to create the fence.
156 * @return Empty optional on failure or initialized fence.
158 static util::optional<sync_fd_fence_sync> create(layer::device_private_data &device);
161 * Exports the fence to a native Sync FD.
163 * @note This method is not threadsafe.
165 * @return The exported Sync FD on success or empty optional on failure.
167 util::optional<util::fd_owner> export_sync_fd();
171 * Non-public constructor to initialize the object with valid data.
173 * @param device The device private data for the fence.
174 * @param vk_fence The created exportable Vulkan fence.
176 sync_fd_fence_sync(layer::device_private_data &device, VkFence vk_fence);
179 } /* namespace wsi */