1/*
2 * Copyright © 2022 Jason Ekstrand
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "vk_common_entrypoints.h"
25#include "vk_device.h"
26#include "vk_log.h"
27#include "vk_queue.h"
28
29#include "util/libsync.h"
30
31#include <unistd.h>
32
33VKAPI_ATTR VkResult VKAPI_CALL
34vk_common_AcquireImageANDROID(VkDevice _device,
35                              VkImage image,
36                              int nativeFenceFd,
37                              VkSemaphore semaphore,
38                              VkFence fence)
39{
40   VK_FROM_HANDLE(vk_device, device, _device);
41   VkResult result = VK_SUCCESS;
42
43   /* From https://source.android.com/devices/graphics/implement-vulkan :
44    *
45    *    "The driver takes ownership of the fence file descriptor and closes
46    *    the fence file descriptor when no longer needed. The driver must do
47    *    so even if neither a semaphore or fence object is provided, or even
48    *    if vkAcquireImageANDROID fails and returns an error."
49    *
50    * The Vulkan spec for VkImportFence/SemaphoreFdKHR(), however, requires
51    * the file descriptor to be left alone on failure.
52    */
53   int semaphore_fd = -1, fence_fd = -1;
54   if (nativeFenceFd >= 0) {
55      if (semaphore != VK_NULL_HANDLE && fence != VK_NULL_HANDLE) {
56         /* We have both so we have to import the sync file twice. One of
57          * them needs to be a dup.
58          */
59         semaphore_fd = nativeFenceFd;
60         fence_fd = dup(nativeFenceFd);
61         if (fence_fd < 0) {
62            VkResult err = (errno == EMFILE) ? VK_ERROR_TOO_MANY_OBJECTS :
63                                               VK_ERROR_OUT_OF_HOST_MEMORY;
64            close(nativeFenceFd);
65            return vk_error(device, err);
66         }
67      } else if (semaphore != VK_NULL_HANDLE) {
68         semaphore_fd = nativeFenceFd;
69      } else if (fence != VK_NULL_HANDLE) {
70         fence_fd = nativeFenceFd;
71      } else {
72         /* Nothing to import into so we have to close the file */
73         close(nativeFenceFd);
74      }
75   }
76
77   if (semaphore != VK_NULL_HANDLE) {
78      const VkImportSemaphoreFdInfoKHR info = {
79         .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
80         .semaphore = semaphore,
81         .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT,
82         .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT,
83         .fd = semaphore_fd,
84      };
85      result = device->dispatch_table.ImportSemaphoreFdKHR(_device, &info);
86      if (result == VK_SUCCESS)
87         semaphore_fd = -1; /* The driver took ownership */
88   }
89
90   if (result == VK_SUCCESS && fence != VK_NULL_HANDLE) {
91      const VkImportFenceFdInfoKHR info = {
92         .sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR,
93         .fence = fence,
94         .flags = VK_FENCE_IMPORT_TEMPORARY_BIT,
95         .handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT,
96         .fd = fence_fd,
97      };
98      result = device->dispatch_table.ImportFenceFdKHR(_device, &info);
99      if (result == VK_SUCCESS)
100         fence_fd = -1; /* The driver took ownership */
101   }
102
103   if (semaphore_fd >= 0)
104      close(semaphore_fd);
105   if (fence_fd >= 0)
106      close(fence_fd);
107
108   return result;
109}
110
111
112VKAPI_ATTR VkResult VKAPI_CALL
113vk_common_QueueSignalReleaseImageANDROID(VkQueue _queue,
114                                         uint32_t waitSemaphoreCount,
115                                         const VkSemaphore *pWaitSemaphores,
116                                         VkImage image,
117                                         int *pNativeFenceFd)
118{
119   VK_FROM_HANDLE(vk_queue, queue, _queue);
120   struct vk_device *device = queue->base.device;
121   VkResult result = VK_SUCCESS;
122
123   if (waitSemaphoreCount == 0) {
124      if (pNativeFenceFd)
125         *pNativeFenceFd = -1;
126      return VK_SUCCESS;
127   }
128
129   int fd = -1;
130
131   for (uint32_t i = 0; i < waitSemaphoreCount; ++i) {
132      const VkSemaphoreGetFdInfoKHR get_fd = {
133         .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
134         .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT,
135         .semaphore = pWaitSemaphores[i],
136      };
137      int tmp_fd;
138      result = device->dispatch_table.GetSemaphoreFdKHR(vk_device_to_handle(device),
139                                                        &get_fd, &tmp_fd);
140      if (result != VK_SUCCESS) {
141         if (fd >= 0)
142            close(fd);
143         return result;
144      }
145
146      if (fd < 0) {
147         fd = tmp_fd;
148      } else if (tmp_fd >= 0) {
149         sync_accumulate("vulkan", &fd, tmp_fd);
150         close(tmp_fd);
151      }
152   }
153
154   if (pNativeFenceFd) {
155      *pNativeFenceFd = fd;
156   } else if (fd >= 0) {
157      close(fd);
158      /* We still need to do the exports, to reset the semaphores, but
159       * otherwise we don't wait on them. */
160   }
161   return VK_SUCCESS;
162}
163