vulkan_util_posix.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "gpu/vulkan/vulkan_util.h"
  5. #include "base/logging.h"
  6. #include "gpu/vulkan/vulkan_function_pointers.h"
  7. namespace gpu {
  8. VkSemaphore ImportVkSemaphoreHandle(VkDevice vk_device,
  9. SemaphoreHandle handle) {
  10. auto handle_type = handle.vk_handle_type();
  11. if (!handle.is_valid() ||
  12. (handle_type != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT &&
  13. handle_type != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT)) {
  14. return VK_NULL_HANDLE;
  15. }
  16. VkSemaphore semaphore = VK_NULL_HANDLE;
  17. VkSemaphoreCreateInfo info = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
  18. VkResult result = vkCreateSemaphore(vk_device, &info, nullptr, &semaphore);
  19. if (result != VK_SUCCESS)
  20. return VK_NULL_HANDLE;
  21. base::ScopedFD fd = handle.TakeHandle();
  22. const auto is_sync_fd =
  23. handle_type == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
  24. const VkImportSemaphoreFdInfoKHR import = {
  25. .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
  26. .semaphore = semaphore,
  27. .flags = is_sync_fd ? VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR
  28. : VkSemaphoreImportFlags{0},
  29. .handleType = handle_type,
  30. .fd = fd.release(),
  31. };
  32. result = vkImportSemaphoreFdKHR(vk_device, &import);
  33. if (result != VK_SUCCESS) {
  34. DLOG(ERROR) << "vkImportSemaphoreFdKHR failed: " << result;
  35. vkDestroySemaphore(vk_device, semaphore, nullptr);
  36. // If import failed, we need to close fd manually.
  37. base::ScopedFD close_fd(import.fd);
  38. return VK_NULL_HANDLE;
  39. }
  40. return semaphore;
  41. }
  42. SemaphoreHandle GetVkSemaphoreHandle(
  43. VkDevice vk_device,
  44. VkSemaphore vk_semaphore,
  45. VkExternalSemaphoreHandleTypeFlagBits handle_type) {
  46. VkSemaphoreGetFdInfoKHR info = {VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR};
  47. info.semaphore = vk_semaphore;
  48. info.handleType = handle_type;
  49. int fd = -1;
  50. VkResult result = vkGetSemaphoreFdKHR(vk_device, &info, &fd);
  51. if (result != VK_SUCCESS) {
  52. DLOG(ERROR) << "vkGetSemaphoreFdKHR failed: " << result;
  53. return SemaphoreHandle();
  54. }
  55. return SemaphoreHandle(handle_type, base::ScopedFD(fd));
  56. }
  57. } // namespace gpu