1 #ifndef _VKTEXTERNALMEMORYUTIL_HPP
2 #define _VKTEXTERNALMEMORYUTIL_HPP
3 /*-------------------------------------------------------------------------
4 * Vulkan Conformance Tests
5 * ------------------------
7 * Copyright (c) 2016 Google Inc.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Vulkan external memory utilities
22 *//*--------------------------------------------------------------------*/
24 #include "tcuDefs.hpp"
26 #include "vkPlatform.hpp"
27 #include "vkRefUtil.hpp"
34 namespace ExternalMemoryUtil
42 WIN32HANDLETYPE_NT = 0,
49 NativeHandle (const NativeHandle& other);
50 NativeHandle (int fd);
51 NativeHandle (Win32HandleType type, vk::pt::Win32Handle handle);
52 NativeHandle (vk::pt::AndroidHardwareBufferPtr buffer);
55 NativeHandle& operator= (int fd);
56 NativeHandle& operator= (vk::pt::AndroidHardwareBufferPtr buffer);
58 void setWin32Handle (Win32HandleType type, vk::pt::Win32Handle handle);
59 vk::pt::Win32Handle getWin32Handle (void) const;
60 void setHostPtr (void* hostPtr);
61 void* getHostPtr (void) const;
62 int getFd (void) const;
63 vk::pt::AndroidHardwareBufferPtr getAndroidHardwareBuffer (void) const;
69 Win32HandleType m_win32HandleType;
70 vk::pt::Win32Handle m_win32Handle;
71 vk::pt::AndroidHardwareBufferPtr m_androidHardwareBuffer;
75 NativeHandle& operator= (const NativeHandle&);
78 class AndroidHardwareBufferExternalApi
83 * getInstance obtains the object, that provides an interface to AHB system APIs .
84 * If the AHB system API is not supported or if it is not built as supported with the CTS,
85 * then this function would return a null object.
87 static AndroidHardwareBufferExternalApi* getInstance();
89 /* Is AndroidHardwareBuffer supported? */
90 static bool supportsAhb();
92 /* Are Cube maps supported on current api level? */
93 static bool supportsCubeMap();
96 * Allocates a buffer that backs an AHardwareBuffer using the passed parameter as follows:
97 * width; - width in pixels
98 * height; - height in pixels
99 * layers; - number of images
100 * format; - One of AHARDWAREBUFFER_FORMAT_*
101 * usage; - Combination of AHARDWAREBUFFER_USAGE_*
103 * Returns a valid AndroidHardwareBufferPtr object on success, or an null AndroidHardwareBufferPtr if
104 * the allocation fails for any reason.
106 virtual vk::pt::AndroidHardwareBufferPtr allocate(deUint32 width, deUint32 height, deUint32 layers, deUint32 format, deUint64 usage) = 0;
109 * Acquire a reference on the given AHardwareBuffer object. This prevents the
110 * object from being deleted until the last reference is removed.
112 virtual void acquire(vk::pt::AndroidHardwareBufferPtr buffer) = 0;
115 * Remove a reference that was previously acquired with
116 * AHardwareBuffer_acquire().
118 virtual void release(vk::pt::AndroidHardwareBufferPtr buffer) = 0;
121 * Return a description of the AHardwareBuffer in the passed in the following fields, if not NULL:
122 * width; - width in pixels
123 * height; - height in pixels
124 * layers; - number of images
125 * format; - One of AHARDWAREBUFFER_FORMAT_*
126 * usage; - Combination of AHARDWAREBUFFER_USAGE_*
129 virtual void describe(const vk::pt::AndroidHardwareBufferPtr buffer,
135 deUint32* stride) = 0;
138 virtual deUint64 vkUsageToAhbUsage(vk::VkImageUsageFlagBits vkFlag) = 0;
139 virtual deUint64 vkCreateToAhbUsage(vk::VkImageCreateFlagBits vkFlag) = 0;
140 virtual deUint32 vkFormatToAhbFormat(vk::VkFormat vkFormat) = 0;
141 virtual deUint64 mustSupportAhbUsageFlags() = 0;
142 virtual bool ahbFormatIsBlob(deUint32 format) = 0;
144 virtual ~AndroidHardwareBufferExternalApi();
147 // Protected Constructor
148 AndroidHardwareBufferExternalApi();
151 // Stop the compiler generating methods of copy the object
152 AndroidHardwareBufferExternalApi(AndroidHardwareBufferExternalApi const& copy); // Not Implemented
153 AndroidHardwareBufferExternalApi& operator=(AndroidHardwareBufferExternalApi const& copy); // Not Implemented
155 static bool loadAhbDynamicApis(deInt32 sdkVersion);
158 const char* externalSemaphoreTypeToName (vk::VkExternalSemaphoreHandleTypeFlagBits type);
159 const char* externalFenceTypeToName (vk::VkExternalFenceHandleTypeFlagBits type);
160 const char* externalMemoryTypeToName (vk::VkExternalMemoryHandleTypeFlagBits type);
164 PERMANENCE_PERMANENT = 0,
170 TRANSFERENCE_COPY = 0,
171 TRANSFERENCE_REFERENCE
174 struct ExternalHostMemory
176 ExternalHostMemory(vk::VkDeviceSize aSize, vk::VkDeviceSize aAlignment)
177 : size(deAlignSize(static_cast<size_t>(aSize), static_cast<size_t>(aAlignment)))
179 data = deAlignedMalloc(this->size, static_cast<size_t>(aAlignment));
182 ~ExternalHostMemory()
194 bool isSupportedPermanence (vk::VkExternalSemaphoreHandleTypeFlagBits type,
195 Permanence permanence);
196 Transference getHandelTypeTransferences (vk::VkExternalSemaphoreHandleTypeFlagBits type);
198 bool isSupportedPermanence (vk::VkExternalFenceHandleTypeFlagBits type,
199 Permanence permanence);
200 Transference getHandelTypeTransferences (vk::VkExternalFenceHandleTypeFlagBits type);
202 int getMemoryFd (const vk::DeviceInterface& vkd,
204 vk::VkDeviceMemory memory,
205 vk::VkExternalMemoryHandleTypeFlagBits externalType);
207 void getMemoryNative (const vk::DeviceInterface& vkd,
209 vk::VkDeviceMemory memory,
210 vk::VkExternalMemoryHandleTypeFlagBits externalType,
211 NativeHandle& nativeHandle);
213 vk::Move<vk::VkSemaphore> createExportableSemaphore (const vk::DeviceInterface& vkd,
215 vk::VkExternalSemaphoreHandleTypeFlagBits externalType);
217 vk::Move<vk::VkSemaphore> createExportableSemaphoreType (const vk::DeviceInterface& vkd,
219 vk::VkSemaphoreType semaphoreType,
220 vk::VkExternalSemaphoreHandleTypeFlagBits externalType);
222 int getSemaphoreFd (const vk::DeviceInterface& vkd,
224 vk::VkSemaphore semaphore,
225 vk::VkExternalSemaphoreHandleTypeFlagBits externalType);
227 void getSemaphoreNative (const vk::DeviceInterface& vkd,
229 vk::VkSemaphore semaphore,
230 vk::VkExternalSemaphoreHandleTypeFlagBits externalType,
231 NativeHandle& nativeHandle);
233 void importSemaphore (const vk::DeviceInterface& vkd,
234 const vk::VkDevice device,
235 const vk::VkSemaphore semaphore,
236 vk::VkExternalSemaphoreHandleTypeFlagBits externalType,
237 NativeHandle& handle,
238 vk::VkSemaphoreImportFlags flags);
240 vk::Move<vk::VkSemaphore> createAndImportSemaphore (const vk::DeviceInterface& vkd,
241 const vk::VkDevice device,
242 vk::VkExternalSemaphoreHandleTypeFlagBits externalType,
243 NativeHandle& handle,
244 vk::VkSemaphoreImportFlags flags);
246 vk::Move<vk::VkFence> createExportableFence (const vk::DeviceInterface& vkd,
248 vk::VkExternalFenceHandleTypeFlagBits externalType);
250 int getFenceFd (const vk::DeviceInterface& vkd,
253 vk::VkExternalFenceHandleTypeFlagBits externalType);
255 void getFenceNative (const vk::DeviceInterface& vkd,
258 vk::VkExternalFenceHandleTypeFlagBits externalType,
259 NativeHandle& nativeHandle,
260 bool expectFenceUnsignaled = true);
262 void importFence (const vk::DeviceInterface& vkd,
263 const vk::VkDevice device,
264 const vk::VkFence fence,
265 vk::VkExternalFenceHandleTypeFlagBits externalType,
266 NativeHandle& handle,
267 vk::VkFenceImportFlags flags);
269 vk::Move<vk::VkFence> createAndImportFence (const vk::DeviceInterface& vkd,
270 const vk::VkDevice device,
271 vk::VkExternalFenceHandleTypeFlagBits externalType,
272 NativeHandle& handle,
273 vk::VkFenceImportFlags flags);
275 deUint32 chooseMemoryType (deUint32 bits);
277 deUint32 chooseHostVisibleMemoryType (deUint32 bits,
278 const vk::VkPhysicalDeviceMemoryProperties properties);
280 // If buffer is not null use dedicated allocation
281 vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::DeviceInterface& vkd,
283 vk::VkDeviceSize allocationSize,
284 deUint32 memoryTypeIndex,
285 vk::VkExternalMemoryHandleTypeFlagBits externalType,
286 vk::VkBuffer buffer);
288 // If image is not null use dedicated allocation
289 vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::DeviceInterface& vkd,
291 vk::VkDeviceSize allocationSize,
292 deUint32 memoryTypeIndex,
293 vk::VkExternalMemoryHandleTypeFlagBits externalType,
297 // \note hostVisible argument is strict. Setting it to false will cause NotSupportedError to be thrown if non-host visible memory doesn't exist.
298 // If buffer is not null use dedicated allocation
299 vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::InstanceInterface& vki,
300 vk::VkPhysicalDevice physicalDevice,
301 const vk::DeviceInterface& vkd,
303 const vk::VkMemoryRequirements& requirements,
304 vk::VkExternalMemoryHandleTypeFlagBits externalType,
307 deUint32& exportedMemoryTypeIndex);
310 vk::Move<vk::VkDeviceMemory> importMemory (const vk::DeviceInterface& vkd,
312 const vk::VkMemoryRequirements& requirements,
313 vk::VkExternalMemoryHandleTypeFlagBits externalType,
314 deUint32 memoryTypeIndex,
315 NativeHandle& handle);
317 vk::Move<vk::VkDeviceMemory> importDedicatedMemory (const vk::DeviceInterface& vkd,
320 const vk::VkMemoryRequirements& requirements,
321 vk::VkExternalMemoryHandleTypeFlagBits externalType,
322 deUint32 memoryTypeIndex,
323 NativeHandle& handle);
325 vk::Move<vk::VkDeviceMemory> importDedicatedMemory (const vk::DeviceInterface& vkd,
328 const vk::VkMemoryRequirements& requirements,
329 vk::VkExternalMemoryHandleTypeFlagBits externalType,
330 deUint32 memoryTypeIndex,
331 NativeHandle& handle);
333 vk::Move<vk::VkBuffer> createExternalBuffer (const vk::DeviceInterface& vkd,
335 deUint32 queueFamilyIndex,
336 vk::VkExternalMemoryHandleTypeFlagBits externalType,
337 vk::VkDeviceSize size,
338 vk::VkBufferCreateFlags createFlags,
339 vk::VkBufferUsageFlags usageFlags);
341 vk::Move<vk::VkImage> createExternalImage (const vk::DeviceInterface& vkd,
343 deUint32 queueFamilyIndex,
344 vk::VkExternalMemoryHandleTypeFlagBits externalType,
348 vk::VkImageTiling tiling,
349 vk::VkImageCreateFlags createFlags,
350 vk::VkImageUsageFlags usageFlags,
351 deUint32 mipLevels = 1u,
352 deUint32 arrayLayers = 1u);
354 vk::VkPhysicalDeviceExternalMemoryHostPropertiesEXT getPhysicalDeviceExternalMemoryHostProperties(const vk::InstanceInterface& vki,
355 vk::VkPhysicalDevice physicalDevice);
357 } // ExternalMemoryUtil
360 #endif // _VKTEXTERNALMEMORYUTIL_HPP