vulkan_util_fuchsia.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2022 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. if (!handle.is_valid() ||
  11. handle.vk_handle_type() !=
  12. VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA) {
  13. return VK_NULL_HANDLE;
  14. }
  15. VkSemaphore semaphore = VK_NULL_HANDLE;
  16. VkSemaphoreCreateInfo info = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
  17. VkResult result = vkCreateSemaphore(vk_device, &info, nullptr, &semaphore);
  18. if (result != VK_SUCCESS)
  19. return VK_NULL_HANDLE;
  20. zx::event event = handle.TakeHandle();
  21. VkImportSemaphoreZirconHandleInfoFUCHSIA import = {
  22. VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_ZIRCON_HANDLE_INFO_FUCHSIA};
  23. import.semaphore = semaphore;
  24. import.handleType =
  25. VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA;
  26. import.zirconHandle = event.get();
  27. result = vkImportSemaphoreZirconHandleFUCHSIA(vk_device, &import);
  28. if (result != VK_SUCCESS) {
  29. vkDestroySemaphore(vk_device, semaphore, nullptr);
  30. return VK_NULL_HANDLE;
  31. }
  32. // Vulkan took ownership of the handle.
  33. std::ignore = event.release();
  34. return semaphore;
  35. }
  36. SemaphoreHandle GetVkSemaphoreHandle(
  37. VkDevice vk_device,
  38. VkSemaphore vk_semaphore,
  39. VkExternalSemaphoreHandleTypeFlagBits handle_type) {
  40. DCHECK_EQ(handle_type,
  41. VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA);
  42. // Create VkSemaphoreGetFdInfoKHR structure.
  43. VkSemaphoreGetZirconHandleInfoFUCHSIA info = {
  44. VK_STRUCTURE_TYPE_SEMAPHORE_GET_ZIRCON_HANDLE_INFO_FUCHSIA};
  45. info.semaphore = vk_semaphore;
  46. info.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA;
  47. zx_handle_t handle;
  48. VkResult result =
  49. vkGetSemaphoreZirconHandleFUCHSIA(vk_device, &info, &handle);
  50. if (result != VK_SUCCESS) {
  51. LOG(ERROR) << "vkGetSemaphoreFuchsiaHandleKHR failed : " << result;
  52. return gpu::SemaphoreHandle();
  53. }
  54. return gpu::SemaphoreHandle(
  55. VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA,
  56. zx::event(handle));
  57. }
  58. } // namespace gpu