vulkan_util_win32.cc 2.1 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 <tuple>
  6. #include "base/logging.h"
  7. #include "gpu/vulkan/vulkan_function_pointers.h"
  8. namespace gpu {
  9. VkSemaphore ImportVkSemaphoreHandle(VkDevice vk_device,
  10. SemaphoreHandle handle) {
  11. auto handle_type = handle.vk_handle_type();
  12. if (!handle.is_valid() ||
  13. handle_type != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_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. auto win32_handle = handle.TakeHandle();
  22. VkImportSemaphoreWin32HandleInfoKHR import = {
  23. .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_WIN32_HANDLE_INFO_KHR,
  24. .semaphore = semaphore,
  25. .handleType = handle_type,
  26. .handle = win32_handle.Get(),
  27. };
  28. result = vkImportSemaphoreWin32HandleKHR(vk_device, &import);
  29. if (result != VK_SUCCESS) {
  30. vkDestroySemaphore(vk_device, semaphore, nullptr);
  31. return VK_NULL_HANDLE;
  32. }
  33. // If import is successful, the VkSemaphore takes the ownership of the fd.
  34. std::ignore = win32_handle.Take();
  35. return semaphore;
  36. }
  37. SemaphoreHandle GetVkSemaphoreHandle(
  38. VkDevice vk_device,
  39. VkSemaphore vk_semaphore,
  40. VkExternalSemaphoreHandleTypeFlagBits handle_type) {
  41. VkSemaphoreGetWin32HandleInfoKHR info = {
  42. .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
  43. .semaphore = vk_semaphore,
  44. .handleType = handle_type,
  45. };
  46. HANDLE handle = nullptr;
  47. VkResult result = vkGetSemaphoreWin32HandleKHR(vk_device, &info, &handle);
  48. if (result != VK_SUCCESS) {
  49. LOG(ERROR) << "vkGetSemaphoreFdKHR failed : " << result;
  50. return SemaphoreHandle();
  51. }
  52. return SemaphoreHandle(handle_type, base::win::ScopedHandle(handle));
  53. }
  54. } // namespace gpu