vulkan/wsi: replace all dup() with os_dupfd_cloexec()
[mesa.git] / src / vulkan / wsi / wsi_common.c
index 8d6654cb47d0e3cac08e570e1bf5870a22d34751..d9906091ed9528d944cc18f9aee1a4ad68f6941b 100644 (file)
@@ -24,6 +24,7 @@
 #include "wsi_common_private.h"
 #include "drm-uapi/drm_fourcc.h"
 #include "util/macros.h"
+#include "util/os_file.h"
 #include "util/xmlconfig.h"
 #include "vk_util.h"
 
@@ -206,6 +207,8 @@ wsi_swapchain_init(const struct wsi_device *wsi,
 
    memset(chain, 0, sizeof(*chain));
 
+   vk_object_base_init(NULL, &chain->base, VK_OBJECT_TYPE_SWAPCHAIN_KHR);
+
    chain->wsi = wsi;
    chain->device = device;
    chain->alloc = *pAllocator;
@@ -305,6 +308,8 @@ wsi_swapchain_finish(struct wsi_swapchain *chain)
                                      &chain->alloc);
    }
    vk_free(&chain->alloc, chain->cmd_pools);
+
+   vk_object_base_finish(&chain->base);
 }
 
 static uint32_t
@@ -617,10 +622,10 @@ wsi_create_native_image(const struct wsi_swapchain *chain,
          if (p == 0) {
             image->fds[p] = fd;
          } else {
-            image->fds[p] = dup(fd);
+            image->fds[p] = os_dupfd_cloexec(fd);
             if (image->fds[p] == -1) {
                for (uint32_t i = 0; i < p; i++)
-                  close(image->fds[p]);
+                  close(image->fds[i]);
 
                result = VK_ERROR_OUT_OF_HOST_MEMORY;
                goto fail;
@@ -1061,7 +1066,7 @@ wsi_common_destroy_swapchain(VkDevice device,
                              VkSwapchainKHR _swapchain,
                              const VkAllocationCallbacks *pAllocator)
 {
-   WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
+   VK_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
    if (!swapchain)
       return;
 
@@ -1073,7 +1078,7 @@ wsi_common_get_images(VkSwapchainKHR _swapchain,
                       uint32_t *pSwapchainImageCount,
                       VkImage *pSwapchainImages)
 {
-   WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
+   VK_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
    VK_OUTARRAY_MAKE(images, pSwapchainImages, pSwapchainImageCount);
 
    for (uint32_t i = 0; i < swapchain->image_count; i++) {
@@ -1091,13 +1096,18 @@ wsi_common_acquire_next_image2(const struct wsi_device *wsi,
                                const VkAcquireNextImageInfoKHR *pAcquireInfo,
                                uint32_t *pImageIndex)
 {
-   WSI_FROM_HANDLE(wsi_swapchain, swapchain, pAcquireInfo->swapchain);
+   VK_FROM_HANDLE(wsi_swapchain, swapchain, pAcquireInfo->swapchain);
 
    VkResult result = swapchain->acquire_next_image(swapchain, pAcquireInfo,
                                                    pImageIndex);
    if (result != VK_SUCCESS)
       return result;
 
+   if (wsi->set_memory_ownership) {
+      VkDeviceMemory mem = swapchain->get_wsi_image(swapchain, *pImageIndex)->memory;
+      wsi->set_memory_ownership(swapchain->device, mem, true);
+   }
+
    if (pAcquireInfo->semaphore != VK_NULL_HANDLE &&
        wsi->signal_semaphore_for_memory != NULL) {
       struct wsi_image *image =
@@ -1130,7 +1140,7 @@ wsi_common_queue_present(const struct wsi_device *wsi,
       vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
 
    for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
-      WSI_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
+      VK_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
       uint32_t image_index = pPresentInfo->pImageIndices[i];
       VkResult result;
 
@@ -1219,6 +1229,11 @@ wsi_common_queue_present(const struct wsi_device *wsi,
       if (result != VK_SUCCESS)
          goto fail_present;
 
+      if (wsi->set_memory_ownership) {
+         VkDeviceMemory mem = swapchain->get_wsi_image(swapchain, image_index)->memory;
+         wsi->set_memory_ownership(swapchain->device, mem, false);
+      }
+
    fail_present:
       if (pPresentInfo->pResults != NULL)
          pPresentInfo->pResults[i] = result;