From 6545461041c42022cc66811205339098b4c6973b Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Fri, 1 Feb 2019 10:36:19 -0800 Subject: [PATCH] turnip: add support for VK_KHR_external_memory_{fd,dma_buf} --- src/freedreno/vulkan/tu_device.c | 63 ++++++++++++++++++++++++++- src/freedreno/vulkan/tu_extensions.py | 2 + 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/freedreno/vulkan/tu_device.c b/src/freedreno/vulkan/tu_device.c index c728f397d5d..b8f29394c8f 100644 --- a/src/freedreno/vulkan/tu_device.c +++ b/src/freedreno/vulkan/tu_device.c @@ -1390,7 +1390,33 @@ tu_alloc_memory(struct tu_device *device, if (mem == NULL) return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY); - result = tu_bo_init_new(device, &mem->bo, pAllocateInfo->allocationSize); + const VkImportMemoryFdInfoKHR *fd_info = + vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR); + if (fd_info && !fd_info->handleType) + fd_info = NULL; + + if (fd_info) { + assert(fd_info->handleType == + VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT || + fd_info->handleType == + VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT); + + /* + * TODO Importing the same fd twice gives us the same handle without + * reference counting. We need to maintain a per-instance handle-to-bo + * table and add reference count to tu_bo. + */ + result = tu_bo_init_dmabuf(device, &mem->bo, + pAllocateInfo->allocationSize, fd_info->fd); + if (result == VK_SUCCESS) { + /* take ownership and close the fd */ + close(fd_info->fd); + } + } else { + result = + tu_bo_init_new(device, &mem->bo, pAllocateInfo->allocationSize); + } + if (result != VK_SUCCESS) { vk_free2(&device->alloc, pAllocator, mem); return result; @@ -1964,6 +1990,41 @@ vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion) return VK_SUCCESS; } +VkResult +tu_GetMemoryFdKHR(VkDevice _device, + const VkMemoryGetFdInfoKHR *pGetFdInfo, + int *pFd) +{ + TU_FROM_HANDLE(tu_device, device, _device); + TU_FROM_HANDLE(tu_device_memory, memory, pGetFdInfo->memory); + + assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR); + + /* At the moment, we support only the below handle types. */ + assert(pGetFdInfo->handleType == + VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT || + pGetFdInfo->handleType == + VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT); + + int prime_fd = tu_bo_export_dmabuf(device, &memory->bo); + if (prime_fd < 0) + return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY); + + *pFd = prime_fd; + return VK_SUCCESS; +} + +VkResult +tu_GetMemoryFdPropertiesKHR(VkDevice _device, + VkExternalMemoryHandleTypeFlagBits handleType, + int fd, + VkMemoryFdPropertiesKHR *pMemoryFdProperties) +{ + assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT); + pMemoryFdProperties->memoryTypeBits = 1; + return VK_SUCCESS; +} + void tu_GetPhysicalDeviceExternalSemaphoreProperties( VkPhysicalDevice physicalDevice, diff --git a/src/freedreno/vulkan/tu_extensions.py b/src/freedreno/vulkan/tu_extensions.py index 53e69690280..0a45b859e2b 100644 --- a/src/freedreno/vulkan/tu_extensions.py +++ b/src/freedreno/vulkan/tu_extensions.py @@ -73,6 +73,8 @@ EXTENSIONS = [ Extension('VK_EXT_debug_report', 9, True), Extension('VK_KHR_external_memory_capabilities', 1, True), Extension('VK_KHR_external_memory', 1, True), + Extension('VK_KHR_external_memory_fd', 1, True), + Extension('VK_EXT_external_memory_dma_buf', 1, True), ] class VkVersion: -- 2.30.2