Fix formatting issues
[platform/core/uifw/vulkan-wsi-tizen.git] / wsi / synchronization.cpp
1 /*
2  * Copyright (c) 2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
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
22  * SOFTWARE.
23  */
24
25 /**
26  * @file
27  *
28  * @brief Contains the implementation for WSI synchronization primitives.
29  */
30
31 #include "synchronization.hpp"
32 #include "layer/private_data.hpp"
33
34 namespace wsi
35 {
36
37 fence_sync::fence_sync(layer::device_private_data &device, VkFence vk_fence)
38    : fence{ vk_fence }
39    , has_payload{ false }
40    , dev{ &device }
41 {
42 }
43
44 util::optional<fence_sync> fence_sync::create(layer::device_private_data &device)
45 {
46    VkFence fence{ VK_NULL_HANDLE };
47    VkFenceCreateInfo fence_info{ VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, 0 };
48    VkResult res =
49       device.disp.CreateFence(device.device, &fence_info, device.get_allocator().get_original_callbacks(), &fence);
50    if (res != VK_SUCCESS)
51    {
52       return {};
53    }
54    return fence_sync(device, fence);
55 }
56
57 fence_sync::fence_sync(fence_sync &&rhs)
58 {
59    *this = std::move(rhs);
60 }
61
62 fence_sync &fence_sync::operator=(fence_sync &&rhs)
63 {
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);
68    return *this;
69 }
70
71 fence_sync::~fence_sync()
72 {
73    if (fence != VK_NULL_HANDLE)
74    {
75       wait_payload(UINT64_MAX);
76       dev->disp.DestroyFence(dev->device, fence, dev->get_allocator().get_original_callbacks());
77    }
78 }
79
80 VkResult fence_sync::wait_payload(uint64_t timeout)
81 {
82    VkResult res = VK_SUCCESS;
83    if (has_payload && !payload_finished)
84    {
85       res = dev->disp.WaitForFences(dev->device, 1, &fence, VK_TRUE, timeout);
86       if (res == VK_SUCCESS)
87       {
88          payload_finished = true;
89       }
90    }
91    return res;
92 }
93
94 VkResult fence_sync::set_payload(VkQueue queue, const VkSemaphore *sem_payload, uint32_t sem_count)
95 {
96    VkResult result = dev->disp.ResetFences(dev->device, 1, &fence);
97    if (result != VK_SUCCESS)
98    {
99       return result;
100    }
101    has_payload = false;
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
104     * wait flag.
105     */
106    VkPipelineStageFlags pipeline_stage_flags = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
107
108    VkSubmitInfo submit_info = {
109       VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, sem_count, sem_payload, &pipeline_stage_flags, 0, NULL, 0, NULL
110    };
111
112    result = dev->disp.QueueSubmit(queue, 1, &submit_info, fence);
113    if (result == VK_SUCCESS)
114    {
115       has_payload = true;
116       payload_finished = false;
117    }
118    return result;
119 }
120
121 bool fence_sync::swap_payload(bool new_payload)
122 {
123    bool old_payload = has_payload;
124    has_payload = new_payload;
125    payload_finished = false;
126    return old_payload;
127 }
128
129 sync_fd_fence_sync::sync_fd_fence_sync(layer::device_private_data &device, VkFence vk_fence)
130    : fence_sync{ device, vk_fence }
131 {
132 }
133
134 bool sync_fd_fence_sync::is_supported(layer::instance_private_data &instance, VkPhysicalDevice phys_dev)
135 {
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;
143 }
144
145 util::optional<sync_fd_fence_sync> sync_fd_fence_sync::create(layer::device_private_data &device)
146 {
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;
152    VkResult res =
153       device.disp.CreateFence(device.device, &fence_info, device.get_allocator().get_original_callbacks(), &fence);
154    if (res != VK_SUCCESS)
155    {
156       return {};
157    }
158    return sync_fd_fence_sync{ device, fence };
159 }
160
161 util::optional<util::fd_owner> sync_fd_fence_sync::export_sync_fd()
162 {
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;
168
169    VkResult result = get_device().disp.GetFenceFdKHR(get_device().device, &fence_fd_info, &exported_fd);
170    if (result == VK_SUCCESS)
171    {
172       swap_payload(false);
173       return util::fd_owner(exported_fd);
174    }
175    return {};
176 }
177
178 } /* namespace wsi */