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