1 // 2 // Copyright 2019 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // VulkanExternalHelper.h : Helper for allocating & managing vulkan external objects. 7 8 #ifndef ANGLE_TESTS_TESTUTILS_VULKANEXTERNALHELPER_H_ 9 #define ANGLE_TESTS_TESTUTILS_VULKANEXTERNALHELPER_H_ 10 11 #include "libANGLE/renderer/vulkan/vk_headers.h" 12 #include "vulkan/vulkan_fuchsia_ext.h" 13 14 namespace angle 15 { 16 17 class VulkanExternalHelper 18 { 19 public: 20 VulkanExternalHelper(); 21 ~VulkanExternalHelper(); 22 23 void initialize(bool useSwiftshader, bool enableValidationLayers); 24 getInstance()25 VkInstance getInstance() const { return mInstance; } getPhysicalDevice()26 VkPhysicalDevice getPhysicalDevice() const { return mPhysicalDevice; } getDevice()27 VkDevice getDevice() const { return mDevice; } getGraphicsQueue()28 VkQueue getGraphicsQueue() const { return mGraphicsQueue; } 29 30 bool canCreateImageExternal(VkFormat format, 31 VkImageType type, 32 VkImageTiling tiling, 33 VkExternalMemoryHandleTypeFlagBits handleType) const; 34 VkResult createImage2DExternal(VkFormat format, 35 VkExtent3D extent, 36 VkExternalMemoryHandleTypeFlags handleTypes, 37 VkImage *imageOut, 38 VkDeviceMemory *deviceMemoryOut, 39 VkDeviceSize *deviceMemorySizeOut); 40 41 // VK_KHR_external_memory_fd 42 bool canCreateImageOpaqueFd(VkFormat format, VkImageType type, VkImageTiling tiling) const; 43 VkResult createImage2DOpaqueFd(VkFormat format, 44 VkExtent3D extent, 45 VkImage *imageOut, 46 VkDeviceMemory *deviceMemoryOut, 47 VkDeviceSize *deviceMemorySizeOut); 48 VkResult exportMemoryOpaqueFd(VkDeviceMemory deviceMemory, int *fd); 49 50 // VK_FUCHSIA_external_memory 51 bool canCreateImageZirconVmo(VkFormat format, VkImageType type, VkImageTiling tiling) const; 52 VkResult createImage2DZirconVmo(VkFormat format, 53 VkExtent3D extent, 54 VkImage *imageOut, 55 VkDeviceMemory *deviceMemoryOut, 56 VkDeviceSize *deviceMemorySizeOut); 57 VkResult exportMemoryZirconVmo(VkDeviceMemory deviceMemory, zx_handle_t *vmo); 58 59 // VK_KHR_external_semaphore_fd 60 bool canCreateSemaphoreOpaqueFd() const; 61 VkResult createSemaphoreOpaqueFd(VkSemaphore *semaphore); 62 VkResult exportSemaphoreOpaqueFd(VkSemaphore semaphore, int *fd); 63 64 // VK_FUCHSIA_external_semaphore 65 bool canCreateSemaphoreZirconEvent() const; 66 VkResult createSemaphoreZirconEvent(VkSemaphore *semaphore); 67 VkResult exportSemaphoreZirconEvent(VkSemaphore semaphore, zx_handle_t *event); 68 69 // Performs a queue ownership transfer to VK_QUEUE_FAMILY_EXTERNAL on an 70 // image owned by our instance. The current image layout must be |oldLayout| 71 // and will be in |newLayout| after the memory barrier. |semaphore| 72 // will be signaled upon completion of the release operation. 73 void releaseImageAndSignalSemaphore(VkImage image, 74 VkImageLayout oldLayout, 75 VkImageLayout newLayout, 76 VkSemaphore semaphore); 77 78 // Performs a queue ownership transfer from VK_QUEUE_FAMILY_EXTERNAL on an 79 // image owned by an external instance. The current image layout must be 80 // |oldLayout| and will be in |newLayout| after the memory barrier. The 81 // barrier will wait for |semaphore|. 82 void waitSemaphoreAndAcquireImage(VkImage image, 83 VkImageLayout oldLayout, 84 VkImageLayout newLayout, 85 VkSemaphore semaphore); 86 87 // Copies pixels out of an image. Currently only VK_FORMAT_R8G8B8A8_UNORM 88 // and VK_FORMAT_B8G8R8A8_UNORM formats are supported. 89 void readPixels(VkImage srcImage, 90 VkImageLayout srcImageLayout, 91 VkFormat srcImageFormat, 92 VkOffset3D imageOffset, 93 VkExtent3D imageExtent, 94 void *pixels, 95 size_t pixelsSize); 96 97 private: 98 VkInstance mInstance = VK_NULL_HANDLE; 99 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; 100 VkDevice mDevice = VK_NULL_HANDLE; 101 VkQueue mGraphicsQueue = VK_NULL_HANDLE; 102 VkCommandPool mCommandPool = VK_NULL_HANDLE; 103 104 VkPhysicalDeviceMemoryProperties mMemoryProperties = {}; 105 106 uint32_t mGraphicsQueueFamilyIndex = UINT32_MAX; 107 108 bool mHasExternalMemoryFd = false; 109 bool mHasExternalMemoryFuchsia = false; 110 bool mHasExternalSemaphoreFd = false; 111 bool mHasExternalSemaphoreFuchsia = false; 112 PFN_vkGetPhysicalDeviceImageFormatProperties2 vkGetPhysicalDeviceImageFormatProperties2 = 113 nullptr; 114 PFN_vkGetMemoryFdKHR vkGetMemoryFdKHR = nullptr; 115 PFN_vkGetSemaphoreFdKHR vkGetSemaphoreFdKHR = nullptr; 116 PFN_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR 117 vkGetPhysicalDeviceExternalSemaphorePropertiesKHR = nullptr; 118 PFN_vkGetMemoryZirconHandleFUCHSIA vkGetMemoryZirconHandleFUCHSIA = nullptr; 119 PFN_vkGetSemaphoreZirconHandleFUCHSIA vkGetSemaphoreZirconHandleFUCHSIA = nullptr; 120 }; 121 122 } // namespace angle 123 124 #endif // ANGLE_TESTS_TESTUTILS_VULKANEXTERNALHELPER_H_ 125