1 #ifndef _VKTEXTERNALMEMORYUTIL_HPP 2 #define _VKTEXTERNALMEMORYUTIL_HPP 3 /*------------------------------------------------------------------------- 4 * Vulkan Conformance Tests 5 * ------------------------ 6 * 7 * Copyright (c) 2016 Google Inc. 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 * \brief Vulkan external memory utilities 22 *//*--------------------------------------------------------------------*/ 23 24 #include "tcuDefs.hpp" 25 26 #include "vkPlatform.hpp" 27 #include "vkRefUtil.hpp" 28 29 #include "deMemory.h" 30 #include "deInt32.h" 31 32 #ifndef CTS_USES_VULKANSC 33 34 namespace vkt 35 { 36 37 namespace ExternalMemoryUtil 38 { 39 40 class NativeHandle 41 { 42 public: 43 enum Win32HandleType 44 { 45 WIN32HANDLETYPE_NT = 0, 46 WIN32HANDLETYPE_KMT, 47 48 WIN32HANDLETYPE_LAST 49 }; 50 51 NativeHandle(void); 52 NativeHandle(const NativeHandle &other); 53 NativeHandle(int fd); 54 NativeHandle(Win32HandleType type, vk::pt::Win32Handle handle); 55 NativeHandle(vk::pt::AndroidHardwareBufferPtr buffer); 56 ~NativeHandle(void); 57 58 NativeHandle &operator=(int fd); 59 NativeHandle &operator=(vk::pt::AndroidHardwareBufferPtr buffer); 60 61 void setZirconHandle(vk::pt::zx_handle_t zirconHandle); 62 void setWin32Handle(Win32HandleType type, vk::pt::Win32Handle handle); 63 vk::pt::Win32Handle getWin32Handle(void) const; 64 void setHostPtr(void *hostPtr); 65 void *getHostPtr(void) const; 66 bool hasValidFd(void) const; 67 int getFd(void) const; 68 vk::pt::AndroidHardwareBufferPtr getAndroidHardwareBuffer(void) const; 69 vk::pt::zx_handle_t getZirconHandle(void) const; 70 void disown(void); 71 void reset(void); 72 73 private: 74 int m_fd; 75 vk::pt::zx_handle_t m_zirconHandle; 76 Win32HandleType m_win32HandleType; 77 vk::pt::Win32Handle m_win32Handle; 78 vk::pt::AndroidHardwareBufferPtr m_androidHardwareBuffer; 79 void *m_hostPtr; 80 81 // Disabled 82 NativeHandle &operator=(const NativeHandle &); 83 }; 84 85 const char *externalSemaphoreTypeToName(vk::VkExternalSemaphoreHandleTypeFlagBits type); 86 const char *externalFenceTypeToName(vk::VkExternalFenceHandleTypeFlagBits type); 87 const char *externalMemoryTypeToName(vk::VkExternalMemoryHandleTypeFlagBits type); 88 89 enum Permanence 90 { 91 PERMANENCE_PERMANENT = 0, 92 PERMANENCE_TEMPORARY 93 }; 94 95 enum Transference 96 { 97 TRANSFERENCE_COPY = 0, 98 TRANSFERENCE_REFERENCE 99 }; 100 101 struct ExternalHostMemory 102 { ExternalHostMemoryvkt::ExternalMemoryUtil::ExternalHostMemory103 ExternalHostMemory(vk::VkDeviceSize aSize, vk::VkDeviceSize aAlignment) 104 : size(deAlignSize(static_cast<size_t>(aSize), static_cast<size_t>(aAlignment))) 105 { 106 data = deAlignedMalloc(this->size, static_cast<size_t>(aAlignment)); 107 } 108 ~ExternalHostMemoryvkt::ExternalMemoryUtil::ExternalHostMemory109 ~ExternalHostMemory() 110 { 111 if (data != DE_NULL) 112 { 113 deAlignedFree(data); 114 } 115 } 116 117 size_t size; 118 void *data; 119 }; 120 121 bool isSupportedPermanence(vk::VkExternalSemaphoreHandleTypeFlagBits type, Permanence permanence); 122 Transference getHandelTypeTransferences(vk::VkExternalSemaphoreHandleTypeFlagBits type); 123 124 bool isSupportedPermanence(vk::VkExternalFenceHandleTypeFlagBits type, Permanence permanence); 125 Transference getHandelTypeTransferences(vk::VkExternalFenceHandleTypeFlagBits type); 126 127 int getMemoryFd(const vk::DeviceInterface &vkd, vk::VkDevice device, vk::VkDeviceMemory memory, 128 vk::VkExternalMemoryHandleTypeFlagBits externalType); 129 130 void getMemoryNative(const vk::DeviceInterface &vkd, vk::VkDevice device, vk::VkDeviceMemory memory, 131 vk::VkExternalMemoryHandleTypeFlagBits externalType, NativeHandle &nativeHandle); 132 133 vk::Move<vk::VkSemaphore> createExportableSemaphore(const vk::DeviceInterface &vkd, vk::VkDevice device, 134 vk::VkExternalSemaphoreHandleTypeFlagBits externalType); 135 136 vk::Move<vk::VkSemaphore> createExportableSemaphoreType(const vk::DeviceInterface &vkd, vk::VkDevice device, 137 vk::VkSemaphoreType semaphoreType, 138 vk::VkExternalSemaphoreHandleTypeFlagBits externalType); 139 140 int getSemaphoreFd(const vk::DeviceInterface &vkd, vk::VkDevice device, vk::VkSemaphore semaphore, 141 vk::VkExternalSemaphoreHandleTypeFlagBits externalType); 142 143 void getSemaphoreNative(const vk::DeviceInterface &vkd, vk::VkDevice device, vk::VkSemaphore semaphore, 144 vk::VkExternalSemaphoreHandleTypeFlagBits externalType, NativeHandle &nativeHandle); 145 146 void importSemaphore(const vk::DeviceInterface &vkd, const vk::VkDevice device, const vk::VkSemaphore semaphore, 147 vk::VkExternalSemaphoreHandleTypeFlagBits externalType, NativeHandle &handle, 148 vk::VkSemaphoreImportFlags flags); 149 150 vk::Move<vk::VkSemaphore> createAndImportSemaphore(const vk::DeviceInterface &vkd, const vk::VkDevice device, 151 vk::VkExternalSemaphoreHandleTypeFlagBits externalType, 152 NativeHandle &handle, vk::VkSemaphoreImportFlags flags); 153 154 vk::Move<vk::VkFence> createExportableFence(const vk::DeviceInterface &vkd, vk::VkDevice device, 155 vk::VkExternalFenceHandleTypeFlagBits externalType); 156 157 int getFenceFd(const vk::DeviceInterface &vkd, vk::VkDevice device, vk::VkFence fence, 158 vk::VkExternalFenceHandleTypeFlagBits externalType); 159 160 void getFenceNative(const vk::DeviceInterface &vkd, vk::VkDevice device, vk::VkFence fence, 161 vk::VkExternalFenceHandleTypeFlagBits externalType, NativeHandle &nativeHandle, 162 bool expectFenceUnsignaled = true); 163 164 void importFence(const vk::DeviceInterface &vkd, const vk::VkDevice device, const vk::VkFence fence, 165 vk::VkExternalFenceHandleTypeFlagBits externalType, NativeHandle &handle, 166 vk::VkFenceImportFlags flags); 167 168 vk::Move<vk::VkFence> createAndImportFence(const vk::DeviceInterface &vkd, const vk::VkDevice device, 169 vk::VkExternalFenceHandleTypeFlagBits externalType, NativeHandle &handle, 170 vk::VkFenceImportFlags flags); 171 172 uint32_t chooseMemoryType(uint32_t bits); 173 174 uint32_t chooseHostVisibleMemoryType(uint32_t bits, const vk::VkPhysicalDeviceMemoryProperties properties); 175 176 vk::VkMemoryRequirements getImageMemoryRequirements(const vk::DeviceInterface &vkd, vk::VkDevice device, 177 vk::VkImage image, 178 vk::VkExternalMemoryHandleTypeFlagBits externalType); 179 180 // If buffer is not null use dedicated allocation 181 vk::Move<vk::VkDeviceMemory> allocateExportableMemory(const vk::DeviceInterface &vkd, vk::VkDevice device, 182 vk::VkDeviceSize allocationSize, uint32_t memoryTypeIndex, 183 vk::VkExternalMemoryHandleTypeFlagBits externalType, 184 vk::VkBuffer buffer); 185 186 // If image is not null use dedicated allocation 187 vk::Move<vk::VkDeviceMemory> allocateExportableMemory(const vk::DeviceInterface &vkd, vk::VkDevice device, 188 vk::VkDeviceSize allocationSize, uint32_t memoryTypeIndex, 189 vk::VkExternalMemoryHandleTypeFlagBits externalType, 190 vk::VkImage image); 191 192 /* 193 // \note hostVisible argument is strict. Setting it to false will cause NotSupportedError to be thrown if non-host visible memory doesn't exist. 194 // If buffer is not null use dedicated allocation 195 vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::InstanceInterface& vki, 196 vk::VkPhysicalDevice physicalDevice, 197 const vk::DeviceInterface& vkd, 198 vk::VkDevice device, 199 const vk::VkMemoryRequirements& requirements, 200 vk::VkExternalMemoryHandleTypeFlagBits externalType, 201 bool hostVisible, 202 vk::VkBuffer buffer, 203 uint32_t& exportedMemoryTypeIndex); 204 */ 205 206 vk::Move<vk::VkDeviceMemory> importMemory(const vk::DeviceInterface &vkd, vk::VkDevice device, 207 const vk::VkMemoryRequirements &requirements, 208 vk::VkExternalMemoryHandleTypeFlagBits externalType, uint32_t memoryTypeIndex, 209 NativeHandle &handle); 210 211 vk::Move<vk::VkDeviceMemory> importDedicatedMemory(const vk::DeviceInterface &vkd, vk::VkDevice device, 212 vk::VkBuffer buffer, const vk::VkMemoryRequirements &requirements, 213 vk::VkExternalMemoryHandleTypeFlagBits externalType, 214 uint32_t memoryTypeIndex, NativeHandle &handle); 215 216 vk::Move<vk::VkDeviceMemory> importDedicatedMemory(const vk::DeviceInterface &vkd, vk::VkDevice device, 217 vk::VkImage image, const vk::VkMemoryRequirements &requirements, 218 vk::VkExternalMemoryHandleTypeFlagBits externalType, 219 uint32_t memoryTypeIndex, NativeHandle &handle); 220 221 vk::Move<vk::VkBuffer> createExternalBuffer(const vk::DeviceInterface &vkd, vk::VkDevice device, 222 uint32_t queueFamilyIndex, 223 vk::VkExternalMemoryHandleTypeFlagBits externalType, vk::VkDeviceSize size, 224 vk::VkBufferCreateFlags createFlags, vk::VkBufferUsageFlags usageFlags); 225 226 vk::Move<vk::VkImage> createExternalImage(const vk::DeviceInterface &vkd, vk::VkDevice device, 227 uint32_t queueFamilyIndex, 228 vk::VkExternalMemoryHandleTypeFlagBits externalType, vk::VkFormat format, 229 uint32_t width, uint32_t height, vk::VkImageTiling tiling, 230 vk::VkImageCreateFlags createFlags, vk::VkImageUsageFlags usageFlags, 231 uint32_t mipLevels = 1u, uint32_t arrayLayers = 1u); 232 233 vk::VkPhysicalDeviceExternalMemoryHostPropertiesEXT getPhysicalDeviceExternalMemoryHostProperties( 234 const vk::InstanceInterface &vki, vk::VkPhysicalDevice physicalDevice); 235 236 } // namespace ExternalMemoryUtil 237 238 } // namespace vkt 239 240 #endif // CTS_USES_VULKANSC 241 242 #include "vktExternalMemoryAndroidHardwareBufferUtil.hpp" 243 244 #endif // _VKTEXTERNALMEMORYUTIL_HPP 245