From b305b7f123ef3b56f08eb47763ef88e572570a50 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 15 May 2020 14:51:41 +0200 Subject: [PATCH] Dup FD in CVulkanTexture::BInit This saves us the wlr_dmabuf_attributes juggle and makes it easier to understand the FDs ownership and lifetime. --- src/rendervulkan.cpp | 13 ++++++++++++- src/steamcompmgr.cpp | 14 +------------- src/wlserver.c | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/rendervulkan.cpp b/src/rendervulkan.cpp index c390629..f877647 100644 --- a/src/rendervulkan.cpp +++ b/src/rendervulkan.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "rendervulkan.hpp" #include "main.hpp" @@ -306,6 +307,16 @@ bool CVulkanTexture::BInit( uint32_t width, uint32_t height, VkFormat format, bo if ( pDMA != nullptr ) { + assert( pDMA->n_planes == 1 ); + + // Importing memory from a FD transfers ownership of the FD + int fd = dup( pDMA->fd[0] ); + if ( fd < 0 ) + { + perror( "dup failed" ); + return false; + } + // We're importing WSI buffers from GL or Vulkan, set implicit_sync wsiAllocInfo.sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA; wsiAllocInfo.implicit_sync = true; @@ -316,7 +327,7 @@ bool CVulkanTexture::BInit( uint32_t width, uint32_t height, VkFormat format, bo // Memory already provided by pDMA importMemoryInfo.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR; importMemoryInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT; - importMemoryInfo.fd = pDMA->fd[0]; + importMemoryInfo.fd = fd; importMemoryInfo.pNext = allocInfo.pNext; allocInfo.pNext = &importMemoryInfo; diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp index 951389a..68520ff 100644 --- a/src/steamcompmgr.cpp +++ b/src/steamcompmgr.cpp @@ -514,21 +514,8 @@ import_commit ( struct wlr_dmabuf_attributes *dmabuf, commit_t &commit ) { if ( BIsNested() == False ) { - // We'll also need a copy for Vulkan to consume below. - - int fdCopy = dup( dmabuf->fd[0] ); - - if ( fdCopy == -1 ) - { - close( dmabuf->fd[0] ); - return false; - } - commit.fb_id = drm_fbid_from_dmabuf( &g_DRM, dmabuf ); assert( commit.fb_id != 0 ); - - close( dmabuf->fd[0] ); - dmabuf->fd[0] = fdCopy; } commit.vulkanTex = vulkan_create_texture_from_dmabuf( dmabuf ); @@ -2031,6 +2018,7 @@ void check_new_wayland_res( void ) commit_t newCommit = {}; bool bSuccess = import_commit( &wayland_commit_queue[ i ].attribs, newCommit ); + wlr_dmabuf_attributes_finish( &wayland_commit_queue[ i ].attribs ); if ( bSuccess == true ) { diff --git a/src/wlserver.c b/src/wlserver.c index b79d60c..0a85a86 100644 --- a/src/wlserver.c +++ b/src/wlserver.c @@ -94,7 +94,7 @@ void xwayland_surface_role_commit(struct wlr_surface *wlr_surface) { } gpuvis_trace_printf( "xwayland_surface_role_commit wlr_surface %p\n", wlr_surface ); - + wayland_commit( wlr_surface, &dmabuf_attribs ); }