diff --git a/src/amd/vulkan/radv_android.c b/src/amd/vulkan/radv_android.c index 6ea51ffbe8e..4ead3284c03 100644 --- a/src/amd/vulkan/radv_android.c +++ b/src/amd/vulkan/radv_android.c @@ -377,81 +377,6 @@ radv_GetSwapchainGrallocUsage2ANDROID(VkDevice device_h, VkFormat format, #endif } -VkResult -radv_AcquireImageANDROID(VkDevice device_h, VkImage image_h, int nativeFenceFd, VkSemaphore semaphore, - VkFence fence) -{ - RADV_FROM_HANDLE(radv_device, device, device_h); - VkResult result = VK_SUCCESS; - - /* From https://source.android.com/devices/graphics/implement-vulkan : - * - * "The driver takes ownership of the fence file descriptor and closes - * the fence file descriptor when no longer needed. The driver must do - * so even if neither a semaphore or fence object is provided, or even - * if vkAcquireImageANDROID fails and returns an error." - * - * The Vulkan spec for VkImportFence/SemaphoreFdKHR(), however, requires - * the file descriptor to be left alone on failure. - */ - int semaphore_fd = -1, fence_fd = -1; - if (nativeFenceFd >= 0) { - if (semaphore != VK_NULL_HANDLE && fence != VK_NULL_HANDLE) { - /* We have both so we have to import the sync file twice. One of - * them needs to be a dup. - */ - semaphore_fd = nativeFenceFd; - fence_fd = dup(nativeFenceFd); - if (fence_fd < 0) { - VkResult err = (errno == EMFILE) ? VK_ERROR_TOO_MANY_OBJECTS : - VK_ERROR_OUT_OF_HOST_MEMORY; - close(nativeFenceFd); - return vk_error(device, err); - } - } else if (semaphore != VK_NULL_HANDLE) { - semaphore_fd = nativeFenceFd; - } else if (fence != VK_NULL_HANDLE) { - fence_fd = nativeFenceFd; - } else { - /* Nothing to import into so we have to close the file */ - close(nativeFenceFd); - } - } - - if (semaphore != VK_NULL_HANDLE) { - const VkImportSemaphoreFdInfoKHR info = { - .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR, - .semaphore = semaphore, - .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT, - .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT, - .fd = semaphore_fd, - }; - result = device->vk.dispatch_table.ImportSemaphoreFdKHR(device_h, &info); - if (result == VK_SUCCESS) - semaphore_fd = -1; /* RADV took ownership */ - } - - if (result == VK_SUCCESS && fence != VK_NULL_HANDLE) { - const VkImportFenceFdInfoKHR info = { - .sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR, - .fence = fence, - .flags = VK_FENCE_IMPORT_TEMPORARY_BIT, - .handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT, - .fd = fence_fd, - }; - result = device->vk.dispatch_table.ImportFenceFdKHR(device_h, &info); - if (result == VK_SUCCESS) - fence_fd = -1; /* RADV took ownership */ - } - - if (semaphore_fd >= 0) - close(semaphore_fd); - if (fence_fd >= 0) - close(fence_fd); - - return result; -} - VkResult radv_QueueSignalReleaseImageANDROID(VkQueue _queue, uint32_t waitSemaphoreCount, const VkSemaphore *pWaitSemaphores, VkImage image, diff --git a/src/broadcom/vulkan/v3dv_android.c b/src/broadcom/vulkan/v3dv_android.c index 7ebc5a01adc..726ae554d45 100644 --- a/src/broadcom/vulkan/v3dv_android.c +++ b/src/broadcom/vulkan/v3dv_android.c @@ -424,84 +424,6 @@ v3dv_GetSwapchainGrallocUsage2ANDROID( } #endif -VKAPI_ATTR VkResult VKAPI_CALL -v3dv_AcquireImageANDROID(VkDevice device_h, - VkImage image_h, - int nativeFenceFd, - VkSemaphore semaphore_h, - VkFence fence_h) -{ - V3DV_FROM_HANDLE(v3dv_device, device, device_h); - VkResult result = VK_SUCCESS; - - /* From https://source.android.com/devices/graphics/implement-vulkan : - * - * "The driver takes ownership of the fence file descriptor and closes - * the fence file descriptor when no longer needed. The driver must do - * so even if neither a semaphore or fence object is provided, or even - * if vkAcquireImageANDROID fails and returns an error." - * - * The Vulkan spec for VkImportFence/SemaphoreFdKHR(), however, requires - * the file descriptor to be left alone on failure. - */ - int semaphore_fd = -1, fence_fd = -1; - if (nativeFenceFd >= 0) { - if (semaphore_h != VK_NULL_HANDLE && fence_h != VK_NULL_HANDLE) { - /* We have both so we have to import the sync file twice. One of - * them needs to be a dup. - */ - semaphore_fd = nativeFenceFd; - fence_fd = os_dupfd_cloexec(nativeFenceFd); - if (fence_fd < 0) { - VkResult err = (errno == EMFILE) ? VK_ERROR_TOO_MANY_OBJECTS - : VK_ERROR_OUT_OF_HOST_MEMORY; - close(nativeFenceFd); - return vk_error(device, err); - } - } else if (semaphore_h != VK_NULL_HANDLE) { - semaphore_fd = nativeFenceFd; - } else if (fence_h != VK_NULL_HANDLE) { - fence_fd = nativeFenceFd; - } else { - /* Nothing to import into so we have to close the file */ - close(nativeFenceFd); - } - } - - if (semaphore_h != VK_NULL_HANDLE) { - const VkImportSemaphoreFdInfoKHR info = { - .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR, - .semaphore = semaphore_h, - .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT, - .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT, - .fd = semaphore_fd, - }; - result = v3dv_ImportSemaphoreFdKHR(device_h, &info); - if (result == VK_SUCCESS) - semaphore_fd = -1; /* VK took ownership */ - } - - if (result == VK_SUCCESS && fence_h != VK_NULL_HANDLE) { - const VkImportFenceFdInfoKHR info = { - .sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR, - .fence = fence_h, - .flags = VK_FENCE_IMPORT_TEMPORARY_BIT, - .handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT, - .fd = fence_fd, - }; - result = v3dv_ImportFenceFdKHR(device_h, &info); - if (result == VK_SUCCESS) - fence_fd = -1; /* VK took ownership */ - } - - if (semaphore_fd >= 0) - close(semaphore_fd); - if (fence_fd >= 0) - close(fence_fd); - - return result; -} - VkResult v3dv_QueueSignalReleaseImageANDROID(VkQueue queue, uint32_t waitSemaphoreCount, diff --git a/src/intel/vulkan/anv_android.c b/src/intel/vulkan/anv_android.c index 13806814611..dee28182d29 100644 --- a/src/intel/vulkan/anv_android.c +++ b/src/intel/vulkan/anv_android.c @@ -792,85 +792,6 @@ VkResult anv_GetSwapchainGrallocUsageANDROID( return setup_gralloc0_usage(device, format, imageUsage, grallocUsage); } -VkResult -anv_AcquireImageANDROID( - VkDevice device_h, - VkImage image_h, - int nativeFenceFd, - VkSemaphore semaphore_h, - VkFence fence_h) -{ - ANV_FROM_HANDLE(anv_device, device, device_h); - VkResult result = VK_SUCCESS; - - /* From https://source.android.com/devices/graphics/implement-vulkan : - * - * "The driver takes ownership of the fence file descriptor and closes - * the fence file descriptor when no longer needed. The driver must do - * so even if neither a semaphore or fence object is provided, or even - * if vkAcquireImageANDROID fails and returns an error." - * - * The Vulkan spec for VkImportFence/SemaphoreFdKHR(), however, requires - * the file descriptor to be left alone on failure. - */ - int semaphore_fd = -1, fence_fd = -1; - if (nativeFenceFd >= 0) { - if (semaphore_h != VK_NULL_HANDLE && fence_h != VK_NULL_HANDLE) { - /* We have both so we have to import the sync file twice. One of - * them needs to be a dup. - */ - semaphore_fd = nativeFenceFd; - fence_fd = dup(nativeFenceFd); - if (fence_fd < 0) { - VkResult err = (errno == EMFILE) ? VK_ERROR_TOO_MANY_OBJECTS : - VK_ERROR_OUT_OF_HOST_MEMORY; - close(nativeFenceFd); - return vk_error(device, err); - } - } else if (semaphore_h != VK_NULL_HANDLE) { - semaphore_fd = nativeFenceFd; - } else if (fence_h != VK_NULL_HANDLE) { - fence_fd = nativeFenceFd; - } else { - /* Nothing to import into so we have to close the file */ - close(nativeFenceFd); - } - } - - if (semaphore_h != VK_NULL_HANDLE) { - const VkImportSemaphoreFdInfoKHR info = { - .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR, - .semaphore = semaphore_h, - .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT, - .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT, - .fd = semaphore_fd, - }; - result = vk_common_ImportSemaphoreFdKHR(device_h, &info); - if (result == VK_SUCCESS) - semaphore_fd = -1; /* ANV took ownership */ - } - - if (result == VK_SUCCESS && fence_h != VK_NULL_HANDLE) { - const VkImportFenceFdInfoKHR info = { - .sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR, - .fence = fence_h, - .flags = VK_FENCE_IMPORT_TEMPORARY_BIT, - .handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT, - .fd = fence_fd, - }; - result = vk_common_ImportFenceFdKHR(device_h, &info); - if (result == VK_SUCCESS) - fence_fd = -1; /* ANV took ownership */ - } - - if (semaphore_fd >= 0) - close(semaphore_fd); - if (fence_fd >= 0) - close(fence_fd); - - return result; -} - VkResult anv_QueueSignalReleaseImageANDROID( VkQueue queue, diff --git a/src/vulkan/runtime/meson.build b/src/vulkan/runtime/meson.build index 46d47d0ec56..6026b175c47 100644 --- a/src/vulkan/runtime/meson.build +++ b/src/vulkan/runtime/meson.build @@ -79,6 +79,10 @@ if dep_libdrm.found() vulkan_runtime_deps += dep_libdrm endif +if with_platform_android + vulkan_runtime_files += files('vk_android.c') +endif + vk_common_entrypoints = custom_target( 'vk_common_entrypoints', input : [vk_entrypoints_gen, vk_api_xml], diff --git a/src/vulkan/runtime/vk_android.c b/src/vulkan/runtime/vk_android.c new file mode 100644 index 00000000000..88790f3381d --- /dev/null +++ b/src/vulkan/runtime/vk_android.c @@ -0,0 +1,106 @@ +/* + * Copyright © 2022 Jason Ekstrand + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "vk_common_entrypoints.h" +#include "vk_device.h" +#include "vk_log.h" + +#include + +VKAPI_ATTR VkResult VKAPI_CALL +vk_common_AcquireImageANDROID(VkDevice _device, + VkImage image, + int nativeFenceFd, + VkSemaphore semaphore, + VkFence fence) +{ + VK_FROM_HANDLE(vk_device, device, _device); + VkResult result = VK_SUCCESS; + + /* From https://source.android.com/devices/graphics/implement-vulkan : + * + * "The driver takes ownership of the fence file descriptor and closes + * the fence file descriptor when no longer needed. The driver must do + * so even if neither a semaphore or fence object is provided, or even + * if vkAcquireImageANDROID fails and returns an error." + * + * The Vulkan spec for VkImportFence/SemaphoreFdKHR(), however, requires + * the file descriptor to be left alone on failure. + */ + int semaphore_fd = -1, fence_fd = -1; + if (nativeFenceFd >= 0) { + if (semaphore != VK_NULL_HANDLE && fence != VK_NULL_HANDLE) { + /* We have both so we have to import the sync file twice. One of + * them needs to be a dup. + */ + semaphore_fd = nativeFenceFd; + fence_fd = dup(nativeFenceFd); + if (fence_fd < 0) { + VkResult err = (errno == EMFILE) ? VK_ERROR_TOO_MANY_OBJECTS : + VK_ERROR_OUT_OF_HOST_MEMORY; + close(nativeFenceFd); + return vk_error(device, err); + } + } else if (semaphore != VK_NULL_HANDLE) { + semaphore_fd = nativeFenceFd; + } else if (fence != VK_NULL_HANDLE) { + fence_fd = nativeFenceFd; + } else { + /* Nothing to import into so we have to close the file */ + close(nativeFenceFd); + } + } + + if (semaphore != VK_NULL_HANDLE) { + const VkImportSemaphoreFdInfoKHR info = { + .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR, + .semaphore = semaphore, + .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT, + .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT, + .fd = semaphore_fd, + }; + result = device->dispatch_table.ImportSemaphoreFdKHR(_device, &info); + if (result == VK_SUCCESS) + semaphore_fd = -1; /* The driver took ownership */ + } + + if (result == VK_SUCCESS && fence != VK_NULL_HANDLE) { + const VkImportFenceFdInfoKHR info = { + .sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR, + .fence = fence, + .flags = VK_FENCE_IMPORT_TEMPORARY_BIT, + .handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT, + .fd = fence_fd, + }; + result = device->dispatch_table.ImportFenceFdKHR(_device, &info); + if (result == VK_SUCCESS) + fence_fd = -1; /* The driver took ownership */ + } + + if (semaphore_fd >= 0) + close(semaphore_fd); + if (fence_fd >= 0) + close(fence_fd); + + return result; +}