[Mesa-dev] [RFC 5/5] anv: Implement wsi get_semaphores_fd and import_semaphore

Louis-Francis Ratté-Boulianne lfrb at collabora.com
Thu Aug 31 05:19:39 UTC 2017


Signed-off-by: Louis-Francis Ratté-Boulianne <lfrb at collabora.com>
---
 src/intel/vulkan/anv_wsi.c | 121 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 119 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_wsi.c b/src/intel/vulkan/anv_wsi.c
index 2c6c4db4eb..38eec76a97 100644
--- a/src/intel/vulkan/anv_wsi.c
+++ b/src/intel/vulkan/anv_wsi.c
@@ -286,9 +286,126 @@ anv_wsi_image_free(VkDevice device,
    anv_FreeMemory(device, memory_h, pAllocator);
 }
 
+static VkResult
+anv_wsi_create_exportable_semaphore(VkDevice _device,
+                                    int* pFd)
+{
+   ANV_FROM_HANDLE(anv_device, device, _device);
+   VkSemaphore semaphore;
+   VkResult result;
+   int fd;
+
+   VkExportSemaphoreCreateInfoKHR export_info = {
+       .sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO_KHR,
+       .pNext = NULL,
+       .handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR,
+   };
+   VkSemaphoreCreateInfo semaphoreCreateInfo = {
+       .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
+       .pNext = &export_info,
+       .flags = 0,
+   };
+
+   result = anv_CreateSemaphore(_device, &semaphoreCreateInfo, NULL, &semaphore);
+   if (result != VK_SUCCESS)
+      return result;
+
+   result = anv_cmd_buffer_execbuf(device, NULL, NULL, 0,
+                                   &semaphore, 1, NULL);
+   if (result == VK_SUCCESS) {
+      VkSemaphoreGetFdInfoKHR getFdInfo = {
+         .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
+         .pNext = NULL,
+         .semaphore = semaphore,
+         .handleType = 0,
+      };
+      result = anv_GetSemaphoreFdKHR(_device, &getFdInfo, &fd);
+      if (result == VK_SUCCESS)
+         *pFd = fd;
+   }
+
+   anv_DestroySemaphore(_device, semaphore, NULL);
+   return result;
+}
+
+static VkResult
+anv_wsi_get_semaphores_fd(VkDevice _device,
+                          const VkSemaphore* semaphores,
+                          uint32_t semaphore_count,
+                          int* pFd)
+{
+   ANV_FROM_HANDLE(anv_device, device, _device);
+   int merged_fence_fd = -1;
+   int dummy_fd = -1;
+   VkResult ret = VK_SUCCESS;
+
+   for (uint32_t i = 0; i < semaphore_count; i++) {
+      int fd;
+      int last_merged;
+
+      ANV_FROM_HANDLE(anv_semaphore, semaphore, semaphores[i]);
+      if (semaphore->permanent.type == ANV_SEMAPHORE_TYPE_DUMMY) {
+         /* If semaphore is a dummy one, create an exportable one */
+         if (dummy_fd != -1)
+            continue;
+
+         ret = anv_wsi_create_exportable_semaphore(_device, &dummy_fd);
+         if (ret != VK_SUCCESS)
+            return ret;
+         fd = dummy_fd;
+      } else {
+         VkSemaphoreGetFdInfoKHR getFdInfo = {
+            .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
+            .pNext = NULL,
+            .semaphore = semaphores[i],
+            .handleType = 0,
+         };
+         ret = anv_GetSemaphoreFdKHR(_device, &getFdInfo, &fd);
+         if (ret != VK_SUCCESS) {
+            close(fd);
+            return ret;
+         }
+      }
+
+      if (fd == -1)
+         continue;
+
+      if (merged_fence_fd == -1) {
+         merged_fence_fd = fd;
+         continue;
+      }
+
+      last_merged = merged_fence_fd;
+      merged_fence_fd = anv_gem_sync_file_merge(device, fd, merged_fence_fd);
+      close(last_merged);
+   }
+
+   *pFd = merged_fence_fd;
+   return VK_SUCCESS;
+}
+
+static VkResult
+anv_wsi_import_semaphore_fd(VkDevice device,
+                            VkSemaphore semaphore,
+                            int fd)
+{
+   const VkImportSemaphoreFdInfoKHR import = {
+      .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
+      .pNext = NULL,
+      .semaphore = semaphore,
+      .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR,
+      .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR,
+      .fd = fd,
+   };
+
+   return anv_ImportSemaphoreFdKHR(device, &import);
+}
+
 static const struct wsi_image_fns anv_wsi_image_fns = {
    .create_wsi_image = anv_wsi_image_create,
    .free_wsi_image = anv_wsi_image_free,
+   .get_semaphores_fd = anv_wsi_get_semaphores_fd,
+   .import_semaphore_fd = anv_wsi_import_semaphore_fd,
 };
 
 VkResult anv_CreateSwapchainKHR(
@@ -426,8 +543,8 @@ VkResult anv_QueuePresentKHR(
       anv_QueueSubmit(_queue, 0, NULL, swapchain->fences[0]);
 
       item_result = swapchain->queue_present(swapchain,
-                                             NULL,
-                                             0,
+                                             pPresentInfo->pWaitSemaphores,
+                                             pPresentInfo->waitSemaphoreCount,
                                              pPresentInfo->pImageIndices[i],
                                              region);
       /* TODO: What if one of them returns OUT_OF_DATE? */
-- 
2.13.0



More information about the mesa-dev mailing list