• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace vkt
30 {
31 namespace ExternalMemoryUtil
32 {
33 
34 class NativeHandle
35 {
36 public:
37 	enum Win32HandleType
38 	{
39 		WIN32HANDLETYPE_NT = 0,
40 		WIN32HANDLETYPE_KMT,
41 
42 		WIN32HANDLETYPE_LAST
43 	};
44 
45 										NativeHandle				(void);
46 										NativeHandle				(const NativeHandle& other);
47 										NativeHandle				(int fd);
48 										NativeHandle				(Win32HandleType type, vk::pt::Win32Handle handle);
49 										NativeHandle				(vk::pt::AndroidHardwareBufferPtr buffer);
50 										~NativeHandle				(void);
51 
52 	NativeHandle&						operator=					(int fd);
53 	NativeHandle&						operator=					(vk::pt::AndroidHardwareBufferPtr buffer);
54 
55 	void								setWin32Handle				(Win32HandleType type, vk::pt::Win32Handle handle);
56 
57 	vk::pt::Win32Handle					getWin32Handle				(void) const;
58 	int									getFd						(void) const;
59 	vk::pt::AndroidHardwareBufferPtr	getAndroidHardwareBuffer	(void) const;
60 	void								disown						(void);
61 	void								reset						(void);
62 
63 private:
64 	int									m_fd;
65 	Win32HandleType						m_win32HandleType;
66 	vk::pt::Win32Handle					m_win32Handle;
67 	vk::pt::AndroidHardwareBufferPtr	m_androidHardwareBuffer;
68 
69 	// Disabled
70 	NativeHandle&						operator=					(const NativeHandle&);
71 };
72 
73 class AndroidHardwareBufferExternalApi
74 {
75 public:
76 
77 	/**
78 	 * getInstance obtains the object, that provides an interface to AHB system APIs .
79 	 * If the AHB system API is not supported or if it is not built as supported with the CTS,
80 	 * then this function would return a null object.
81 	 */
82 	static AndroidHardwareBufferExternalApi* getInstance();
83 
84 	/* Is AndroidHardwareBuffer supported? */
85 	static bool supportsAhb();
86 
87 	/* Are Cube maps supported on current api level? */
88 	static bool supportsCubeMap();
89 
90 	/**
91 	 * Allocates a buffer that backs an AHardwareBuffer using the passed parameter as follows:
92 	 * width;      - width in pixels
93 	 * height;     - height in pixels
94 	 * layers;     - number of images
95 	 * format;     - One of AHARDWAREBUFFER_FORMAT_*
96 	 * usage;      - Combination of AHARDWAREBUFFER_USAGE_*
97 	 *
98 	 * Returns a valid AndroidHardwareBufferPtr object on success, or an null AndroidHardwareBufferPtr if
99 	 * the allocation fails for any reason.
100 	 */
101 	virtual vk::pt::AndroidHardwareBufferPtr allocate(deUint32 width, deUint32  height, deUint32 layers, deUint32  format, deUint64 usage) = 0;
102 
103 	/**
104 	 * Acquire a reference on the given AHardwareBuffer object.  This prevents the
105 	 * object from being deleted until the last reference is removed.
106 	 */
107 	virtual void acquire(vk::pt::AndroidHardwareBufferPtr buffer) = 0;
108 
109 	/**
110 	 * Remove a reference that was previously acquired with
111 	 * AHardwareBuffer_acquire().
112 	 */
113 	virtual void release(vk::pt::AndroidHardwareBufferPtr buffer) = 0;
114 
115 	/**
116 	 * Return a description of the AHardwareBuffer in the passed in the following fields, if not NULL:
117 	 * width;      - width in pixels
118 	 * height;     - height in pixels
119 	 * layers;     - number of images
120 	 * format;     - One of AHARDWAREBUFFER_FORMAT_*
121 	 * usage;      - Combination of AHARDWAREBUFFER_USAGE_*
122 	 *
123 	 */
124 	virtual void describe(const vk::pt::AndroidHardwareBufferPtr buffer,
125 				  deUint32* width,
126 				  deUint32* height,
127 				  deUint32* layers,
128 				  deUint32* format,
129 				  deUint64* usage,
130 				  deUint32* stride) = 0;
131 
132 
133 	virtual deUint64 vkUsageToAhbUsage(vk::VkImageUsageFlagBits vkFlag) = 0;
134 	virtual deUint64 vkCreateToAhbUsage(vk::VkImageCreateFlagBits vkFlag) = 0;
135 	virtual deUint32 vkFormatToAhbFormat(vk::VkFormat vkFormat) = 0;
136 	virtual deUint64 mustSupportAhbUsageFlags() = 0;
137 	virtual bool     ahbFormatIsBlob(deUint32 format) = 0;
138 
139 	virtual ~AndroidHardwareBufferExternalApi();
140 
141 protected:
142 	// Protected Constructor
143 	AndroidHardwareBufferExternalApi();
144 
145 private:
146 	// Stop the compiler generating methods of copy the object
147 	AndroidHardwareBufferExternalApi(AndroidHardwareBufferExternalApi const& copy);            // Not Implemented
148 	AndroidHardwareBufferExternalApi& operator=(AndroidHardwareBufferExternalApi const& copy); // Not Implemented
149 
150 	static bool loadAhbDynamicApis(deInt32 sdkVersion);
151 };
152 
153 const char*						externalSemaphoreTypeToName	(vk::VkExternalSemaphoreHandleTypeFlagBits	type);
154 const char*						externalFenceTypeToName		(vk::VkExternalFenceHandleTypeFlagBits		type);
155 const char*						externalMemoryTypeToName	(vk::VkExternalMemoryHandleTypeFlagBits		type);
156 
157 enum Permanence
158 {
159 	PERMANENCE_PERMANENT = 0,
160 	PERMANENCE_TEMPORARY
161 };
162 
163 enum Transference
164 {
165 	TRANSFERENCE_COPY = 0,
166 	TRANSFERENCE_REFERENCE
167 };
168 
169 bool							isSupportedPermanence				(vk::VkExternalSemaphoreHandleTypeFlagBits	type,
170 																	 Permanence									permanence);
171 Transference					getHandelTypeTransferences			(vk::VkExternalSemaphoreHandleTypeFlagBits	type);
172 
173 bool							isSupportedPermanence				(vk::VkExternalFenceHandleTypeFlagBits		type,
174 																	 Permanence									permanence);
175 Transference					getHandelTypeTransferences			(vk::VkExternalFenceHandleTypeFlagBits		type);
176 
177 int								getMemoryFd							(const vk::DeviceInterface&					vkd,
178 																	 vk::VkDevice								device,
179 																	 vk::VkDeviceMemory							memory,
180 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType);
181 
182 void							getMemoryNative						(const vk::DeviceInterface&					vkd,
183 																	 vk::VkDevice								device,
184 																	 vk::VkDeviceMemory							memory,
185 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
186 																	 NativeHandle&								nativeHandle);
187 
188 vk::Move<vk::VkSemaphore>		createExportableSemaphore			(const vk::DeviceInterface&					vkd,
189 																	 vk::VkDevice								device,
190 																	 vk::VkExternalSemaphoreHandleTypeFlagBits	externalType);
191 
192 int								getSemaphoreFd						(const vk::DeviceInterface&					vkd,
193 																	 vk::VkDevice								device,
194 																	 vk::VkSemaphore							semaphore,
195 																	 vk::VkExternalSemaphoreHandleTypeFlagBits	externalType);
196 
197 void							getSemaphoreNative					(const vk::DeviceInterface&					vkd,
198 																	 vk::VkDevice								device,
199 																	 vk::VkSemaphore							semaphore,
200 																	 vk::VkExternalSemaphoreHandleTypeFlagBits	externalType,
201 																	 NativeHandle&								nativeHandle);
202 
203 void							importSemaphore						(const vk::DeviceInterface&					vkd,
204 																	 const vk::VkDevice							device,
205 																	 const vk::VkSemaphore						semaphore,
206 																	 vk::VkExternalSemaphoreHandleTypeFlagBits	externalType,
207 																	 NativeHandle&								handle,
208 																	 vk::VkSemaphoreImportFlags					flags);
209 
210 vk::Move<vk::VkSemaphore>		createAndImportSemaphore			(const vk::DeviceInterface&					vkd,
211 																	 const vk::VkDevice							device,
212 																	 vk::VkExternalSemaphoreHandleTypeFlagBits	externalType,
213 																	 NativeHandle&								handle,
214 																	 vk::VkSemaphoreImportFlags					flags);
215 
216 vk::Move<vk::VkFence>			createExportableFence				(const vk::DeviceInterface&					vkd,
217 																	 vk::VkDevice								device,
218 																	 vk::VkExternalFenceHandleTypeFlagBits		externalType);
219 
220 int								getFenceFd							(const vk::DeviceInterface&					vkd,
221 																	 vk::VkDevice								device,
222 																	 vk::VkFence								fence,
223 																	 vk::VkExternalFenceHandleTypeFlagBits		externalType);
224 
225 void							getFenceNative						(const vk::DeviceInterface&					vkd,
226 																	 vk::VkDevice								device,
227 																	 vk::VkFence								fence,
228 																	 vk::VkExternalFenceHandleTypeFlagBits		externalType,
229 																	 NativeHandle&								nativeHandle);
230 
231 void							importFence							(const vk::DeviceInterface&					vkd,
232 																	 const vk::VkDevice							device,
233 																	 const vk::VkFence							fence,
234 																	 vk::VkExternalFenceHandleTypeFlagBits		externalType,
235 																	 NativeHandle&								handle,
236 																	 vk::VkFenceImportFlags						flags);
237 
238 vk::Move<vk::VkFence>			createAndImportFence				(const vk::DeviceInterface&					vkd,
239 																	 const vk::VkDevice							device,
240 																	 vk::VkExternalFenceHandleTypeFlagBits		externalType,
241 																	 NativeHandle&								handle,
242 																	 vk::VkFenceImportFlags						flags);
243 
244 vk::Move<vk::VkDeviceMemory>	allocateExportableMemory			(const vk::DeviceInterface&					vkd,
245 																	 vk::VkDevice								device,
246 																	 const vk::VkMemoryRequirements&			requirements,
247 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
248 																	 deUint32&									exportedMemoryTypeIndex);
249 
250 // If buffer is not null use dedicated allocation
251 vk::Move<vk::VkDeviceMemory>	allocateExportableMemory			(const vk::DeviceInterface&					vkd,
252 																	 vk::VkDevice								device,
253 																	 const vk::VkMemoryRequirements&			requirements,
254 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
255 																	 vk::VkBuffer								buffer,
256 																	 deUint32&									exportedMemoryTypeIndex);
257 
258 // If image is not null use dedicated allocation
259 vk::Move<vk::VkDeviceMemory>	allocateExportableMemory			(const vk::DeviceInterface&					vkd,
260 																	 vk::VkDevice								device,
261 																	 const vk::VkMemoryRequirements&			requirements,
262 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
263 																	 vk::VkImage								image,
264 																	 deUint32&									exportedMemoryTypeIndex);
265 
266 // \note hostVisible argument is strict. Setting it to false will cause NotSupportedError to be thrown if non-host visible memory doesn't exist.
267 // If buffer is not null use dedicated allocation
268 vk::Move<vk::VkDeviceMemory>	allocateExportableMemory			(const vk::InstanceInterface&				vki,
269 																	 vk::VkPhysicalDevice						physicalDevice,
270 																	 const vk::DeviceInterface&					vkd,
271 																	 vk::VkDevice								device,
272 																	 const vk::VkMemoryRequirements&			requirements,
273 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
274 																	 bool										hostVisible,
275 																	 vk::VkBuffer								buffer,
276 																	 deUint32&									exportedMemoryTypeIndex);
277 
278 vk::Move<vk::VkDeviceMemory>	importMemory						(const vk::DeviceInterface&					vkd,
279 																	 vk::VkDevice								device,
280 																	 const vk::VkMemoryRequirements&			requirements,
281 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
282 																	 deUint32									memoryTypeIndex,
283 																	 NativeHandle&								handle);
284 
285 vk::Move<vk::VkDeviceMemory>	importDedicatedMemory				(const vk::DeviceInterface&					vkd,
286 																	 vk::VkDevice								device,
287 																	 vk::VkBuffer								buffer,
288 																	 const vk::VkMemoryRequirements&			requirements,
289 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
290 																	 deUint32									memoryTypeIndex,
291 																	 NativeHandle&								handle);
292 
293 vk::Move<vk::VkDeviceMemory>	importDedicatedMemory				(const vk::DeviceInterface&					vkd,
294 																	 vk::VkDevice								device,
295 																	 vk::VkImage								image,
296 																	 const vk::VkMemoryRequirements&			requirements,
297 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
298 																	 deUint32									memoryTypeIndex,
299 																	 NativeHandle&								handle);
300 
301 vk::Move<vk::VkBuffer>			createExternalBuffer				(const vk::DeviceInterface&					vkd,
302 																	 vk::VkDevice								device,
303 																	 deUint32									queueFamilyIndex,
304 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
305 																	 vk::VkDeviceSize							size,
306 																	 vk::VkBufferCreateFlags					createFlags,
307 																	 vk::VkBufferUsageFlags						usageFlags);
308 
309 vk::Move<vk::VkImage>			createExternalImage					(const vk::DeviceInterface&					vkd,
310 																	 vk::VkDevice								device,
311 																	 deUint32									queueFamilyIndex,
312 																	 vk::VkExternalMemoryHandleTypeFlagBits		externalType,
313 																	 vk::VkFormat								format,
314 																	 deUint32									width,
315 																	 deUint32									height,
316 																	 vk::VkImageTiling							tiling,
317 																	 vk::VkImageCreateFlags						createFlags,
318 																	 vk::VkImageUsageFlags						usageFlags,
319 																	 deUint32									mipLevels = 1u,
320 																	 deUint32									arrayLayers = 1u);
321 
322 } // ExternalMemoryUtil
323 } // vkt
324 
325 #endif // _VKTEXTERNALMEMORYUTIL_HPP
326