• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // VulkanHelper.h : Helper for vulkan.
7 
8 #ifndef ANGLE_TESTS_TESTUTILS_VULKANHELPER_H_
9 #define ANGLE_TESTS_TESTUTILS_VULKANHELPER_H_
10 
11 #include "common/vulkan/vk_headers.h"
12 #include "vulkan/vulkan_fuchsia_ext.h"
13 
14 namespace angle
15 {
16 
17 class VulkanHelper
18 {
19   public:
20     VulkanHelper();
21     ~VulkanHelper();
22 
23     void initialize(bool useSwiftshader, bool enableValidationLayers);
24     void initializeFromANGLE();
25 
getInstance()26     VkInstance getInstance() const { return mInstance; }
getPhysicalDevice()27     VkPhysicalDevice getPhysicalDevice() const { return mPhysicalDevice; }
getDevice()28     VkDevice getDevice() const { return mDevice; }
getGraphicsQueue()29     VkQueue getGraphicsQueue() const { return mGraphicsQueue; }
30 
31     VkResult createImage2D(VkFormat format,
32                            VkImageCreateFlags createFlags,
33                            VkImageUsageFlags usageFlags,
34                            VkExtent3D extent,
35                            VkImage *imageOut,
36                            VkDeviceMemory *deviceMemoryOut,
37                            VkDeviceSize *deviceMemorySizeOut,
38                            VkImageCreateInfo *imageCreateInfoOut);
39     bool canCreateImageExternal(VkFormat format,
40                                 VkImageType type,
41                                 VkImageTiling tiling,
42                                 VkImageCreateFlags createFlags,
43                                 VkImageUsageFlags usageFlags,
44                                 VkExternalMemoryHandleTypeFlagBits handleType) const;
45     VkResult createImage2DExternal(VkFormat format,
46                                    VkImageCreateFlags createFlags,
47                                    VkImageUsageFlags usageFlags,
48                                    const void *imageCreateInfoPNext,
49                                    VkExtent3D extent,
50                                    VkExternalMemoryHandleTypeFlags handleTypes,
51                                    VkImage *imageOut,
52                                    VkDeviceMemory *deviceMemoryOut,
53                                    VkDeviceSize *deviceMemorySizeOut);
54 
55     // VK_KHR_external_memory_fd
56     bool canCreateImageOpaqueFd(VkFormat format,
57                                 VkImageType type,
58                                 VkImageTiling tiling,
59                                 VkImageCreateFlags createFlags,
60                                 VkImageUsageFlags usageFlags) const;
61     VkResult createImage2DOpaqueFd(VkFormat format,
62                                    VkImageCreateFlags createFlags,
63                                    VkImageUsageFlags usageFlags,
64                                    const void *imageCreateInfoPNext,
65                                    VkExtent3D extent,
66                                    VkImage *imageOut,
67                                    VkDeviceMemory *deviceMemoryOut,
68                                    VkDeviceSize *deviceMemorySizeOut);
69     VkResult exportMemoryOpaqueFd(VkDeviceMemory deviceMemory, int *fd);
70 
71     // VK_FUCHSIA_external_memory
72     bool canCreateImageZirconVmo(VkFormat format,
73                                  VkImageType type,
74                                  VkImageTiling tiling,
75                                  VkImageCreateFlags createFlags,
76                                  VkImageUsageFlags usageFlags) const;
77     VkResult createImage2DZirconVmo(VkFormat format,
78                                     VkImageCreateFlags createFlags,
79                                     VkImageUsageFlags usageFlags,
80                                     const void *imageCreateInfoPNext,
81                                     VkExtent3D extent,
82                                     VkImage *imageOut,
83                                     VkDeviceMemory *deviceMemoryOut,
84                                     VkDeviceSize *deviceMemorySizeOut);
85     VkResult exportMemoryZirconVmo(VkDeviceMemory deviceMemory, zx_handle_t *vmo);
86 
87     // VK_KHR_external_semaphore_fd
88     bool canCreateSemaphoreOpaqueFd() const;
89     VkResult createSemaphoreOpaqueFd(VkSemaphore *semaphore);
90     VkResult exportSemaphoreOpaqueFd(VkSemaphore semaphore, int *fd);
91 
92     // VK_FUCHSIA_external_semaphore
93     bool canCreateSemaphoreZirconEvent() const;
94     VkResult createSemaphoreZirconEvent(VkSemaphore *semaphore);
95     VkResult exportSemaphoreZirconEvent(VkSemaphore semaphore, zx_handle_t *event);
96 
97     // Performs a queue ownership transfer to VK_QUEUE_FAMILY_EXTERNAL on an
98     // image owned by our instance. The current image layout must be |oldLayout|
99     // and will be in |newLayout| after the memory barrier. |semaphore|
100     // will be signaled upon completion of the release operation.
101     void releaseImageAndSignalSemaphore(VkImage image,
102                                         VkImageLayout oldLayout,
103                                         VkImageLayout newLayout,
104                                         VkSemaphore semaphore);
105 
106     // Performs a queue ownership transfer from VK_QUEUE_FAMILY_EXTERNAL on an
107     // image owned by an external instance. The current image layout must be
108     // |oldLayout| and will be in |newLayout| after the memory barrier. The
109     // barrier will wait for |semaphore|.
110     void waitSemaphoreAndAcquireImage(VkImage image,
111                                       VkImageLayout oldLayout,
112                                       VkImageLayout newLayout,
113                                       VkSemaphore semaphore);
114 
115     // Writes pixels into an image. Currently only VK_FORMAT_R8G8B8A8_UNORM
116     // and VK_FORMAT_B8G8R8A8_UNORM formats are supported.
117     void writePixels(VkImage dstImage,
118                      VkImageLayout imageLayout,
119                      VkFormat imageFormat,
120                      VkOffset3D imageOffset,
121                      VkExtent3D imageExtent,
122                      const void *pixels,
123                      size_t pixelsSize);
124 
125     // Copies pixels out of an image. Currently only VK_FORMAT_R8G8B8A8_UNORM
126     // and VK_FORMAT_B8G8R8A8_UNORM formats are supported.
127     void readPixels(VkImage srcImage,
128                     VkImageLayout srcImageLayout,
129                     VkFormat srcImageFormat,
130                     VkOffset3D imageOffset,
131                     VkExtent3D imageExtent,
132                     void *pixels,
133                     size_t pixelsSize);
134 
135   private:
136     bool mInitializedFromANGLE       = false;
137     VkInstance mInstance             = VK_NULL_HANDLE;
138     VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
139     VkDevice mDevice                 = VK_NULL_HANDLE;
140     VkQueue mGraphicsQueue           = VK_NULL_HANDLE;
141     VkCommandPool mCommandPool       = VK_NULL_HANDLE;
142 
143     VkPhysicalDeviceMemoryProperties mMemoryProperties = {};
144 
145     uint32_t mGraphicsQueueFamilyIndex = UINT32_MAX;
146 
147     bool mHasExternalMemoryFd         = false;
148     bool mHasExternalMemoryFuchsia    = false;
149     bool mHasExternalSemaphoreFd      = false;
150     bool mHasExternalSemaphoreFuchsia = false;
151     PFN_vkGetPhysicalDeviceImageFormatProperties2 vkGetPhysicalDeviceImageFormatProperties2 =
152         nullptr;
153     PFN_vkGetMemoryFdKHR vkGetMemoryFdKHR       = nullptr;
154     PFN_vkGetSemaphoreFdKHR vkGetSemaphoreFdKHR = nullptr;
155     PFN_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR
156         vkGetPhysicalDeviceExternalSemaphorePropertiesKHR                   = nullptr;
157     PFN_vkGetMemoryZirconHandleFUCHSIA vkGetMemoryZirconHandleFUCHSIA       = nullptr;
158     PFN_vkGetSemaphoreZirconHandleFUCHSIA vkGetSemaphoreZirconHandleFUCHSIA = nullptr;
159 };
160 
161 }  // namespace angle
162 
163 #endif  // ANGLE_TESTS_TESTUTILS_VULKANHELPER_H_
164