2
* Copyright © 2022 Jason Ekstrand
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:
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
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
24
#include "vk_common_entrypoints.h"
25
#include "vk_device.h"
29
#include "util/libsync.h"
33
VKAPI_ATTR VkResult VKAPI_CALL
34
vk_common_AcquireImageANDROID(VkDevice _device,
37
VkSemaphore semaphore,
40
VK_FROM_HANDLE(vk_device, device, _device);
41
VkResult result = VK_SUCCESS;
43
/* From https://source.android.com/devices/graphics/implement-vulkan :
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."
50
* The Vulkan spec for VkImportFence/SemaphoreFdKHR(), however, requires
51
* the file descriptor to be left alone on failure.
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.
59
semaphore_fd = nativeFenceFd;
60
fence_fd = dup(nativeFenceFd);
62
VkResult err = (errno == EMFILE) ? VK_ERROR_TOO_MANY_OBJECTS :
63
VK_ERROR_OUT_OF_HOST_MEMORY;
65
return vk_error(device, err);
67
} else if (semaphore != VK_NULL_HANDLE) {
68
semaphore_fd = nativeFenceFd;
69
} else if (fence != VK_NULL_HANDLE) {
70
fence_fd = nativeFenceFd;
72
/* Nothing to import into so we have to close the file */
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,
85
result = device->dispatch_table.ImportSemaphoreFdKHR(_device, &info);
86
if (result == VK_SUCCESS)
87
semaphore_fd = -1; /* The driver took ownership */
90
if (result == VK_SUCCESS && fence != VK_NULL_HANDLE) {
91
const VkImportFenceFdInfoKHR info = {
92
.sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR,
94
.flags = VK_FENCE_IMPORT_TEMPORARY_BIT,
95
.handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT,
98
result = device->dispatch_table.ImportFenceFdKHR(_device, &info);
99
if (result == VK_SUCCESS)
100
fence_fd = -1; /* The driver took ownership */
103
if (semaphore_fd >= 0)
112
VKAPI_ATTR VkResult VKAPI_CALL
113
vk_common_QueueSignalReleaseImageANDROID(VkQueue _queue,
114
uint32_t waitSemaphoreCount,
115
const VkSemaphore *pWaitSemaphores,
119
VK_FROM_HANDLE(vk_queue, queue, _queue);
120
struct vk_device *device = queue->base.device;
121
VkResult result = VK_SUCCESS;
123
if (waitSemaphoreCount == 0) {
125
*pNativeFenceFd = -1;
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],
138
result = device->dispatch_table.GetSemaphoreFdKHR(vk_device_to_handle(device),
140
if (result != VK_SUCCESS) {
148
} else if (tmp_fd >= 0) {
149
sync_accumulate("vulkan", &fd, tmp_fd);
154
if (pNativeFenceFd) {
155
*pNativeFenceFd = fd;
156
} else if (fd >= 0) {
158
/* We still need to do the exports, to reset the semaphores, but
159
* otherwise we don't wait on them. */