2 * Copyright © 2017, Google Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #include "tu_private.h"
26 #include <hardware/gralloc.h>
27 #include <hardware/hardware.h>
28 #include <hardware/hwvulkan.h>
31 #include <vulkan/vk_android_native_buffer.h>
32 #include <vulkan/vk_icd.h>
34 #include "drm-uapi/drm_fourcc.h"
37 tu_hal_open(const struct hw_module_t
*mod
,
39 struct hw_device_t
**dev
);
41 tu_hal_close(struct hw_device_t
*dev
);
46 STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC
== ICD_LOADER_MAGIC
);
49 PUBLIC
struct hwvulkan_module_t HAL_MODULE_INFO_SYM
= {
52 .tag
= HARDWARE_MODULE_TAG
,
53 .module_api_version
= HWVULKAN_MODULE_API_VERSION_0_1
,
54 .hal_api_version
= HARDWARE_MAKE_API_VERSION(1, 0),
55 .id
= HWVULKAN_HARDWARE_MODULE_ID
,
56 .name
= "AMD Vulkan HAL",
59 &(hw_module_methods_t
){
65 /* If any bits in test_mask are set, then unset them and return true. */
67 unmask32(uint32_t *inout_mask
, uint32_t test_mask
)
69 uint32_t orig_mask
= *inout_mask
;
70 *inout_mask
&= ~test_mask
;
71 return *inout_mask
!= orig_mask
;
75 tu_hal_open(const struct hw_module_t
*mod
,
77 struct hw_device_t
**dev
)
79 assert(mod
== &HAL_MODULE_INFO_SYM
.common
);
80 assert(strcmp(id
, HWVULKAN_DEVICE_0
) == 0);
82 hwvulkan_device_t
*hal_dev
= malloc(sizeof(*hal_dev
));
86 *hal_dev
= (hwvulkan_device_t
){
89 .tag
= HARDWARE_DEVICE_TAG
,
90 .version
= HWVULKAN_DEVICE_API_VERSION_0_1
,
91 .module
= &HAL_MODULE_INFO_SYM
.common
,
92 .close
= tu_hal_close
,
94 .EnumerateInstanceExtensionProperties
=
95 tu_EnumerateInstanceExtensionProperties
,
96 .CreateInstance
= tu_CreateInstance
,
97 .GetInstanceProcAddr
= tu_GetInstanceProcAddr
,
100 *dev
= &hal_dev
->common
;
105 tu_hal_close(struct hw_device_t
*dev
)
107 /* hwvulkan.h claims that hw_device_t::close() is never called. */
112 tu_image_from_gralloc(VkDevice device_h
,
113 const VkImageCreateInfo
*base_info
,
114 const VkNativeBufferANDROID
*gralloc_info
,
115 const VkAllocationCallbacks
*alloc
,
116 VkImage
*out_image_h
)
119 TU_FROM_HANDLE(tu_device
, device
, device_h
);
120 VkImage image_h
= VK_NULL_HANDLE
;
121 struct tu_image
*image
= NULL
;
124 result
= tu_image_create(device_h
, base_info
, alloc
, &image_h
,
125 DRM_FORMAT_MOD_LINEAR
, NULL
);
126 if (result
!= VK_SUCCESS
)
129 if (gralloc_info
->handle
->numFds
!= 1) {
130 return vk_errorf(device
->instance
, VK_ERROR_INVALID_EXTERNAL_HANDLE
,
131 "VkNativeBufferANDROID::handle::numFds is %d, "
133 gralloc_info
->handle
->numFds
);
136 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
137 * must exceed that of the gralloc handle, and we do not own the gralloc
140 int dma_buf
= gralloc_info
->handle
->data
[0];
142 image
= tu_image_from_handle(image_h
);
144 VkDeviceMemory memory_h
;
146 const VkMemoryDedicatedAllocateInfo ded_alloc
= {
147 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
149 .buffer
= VK_NULL_HANDLE
,
153 const VkImportMemoryFdInfoKHR import_info
= {
154 .sType
= VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR
,
156 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
,
161 tu_AllocateMemory(device_h
,
162 &(VkMemoryAllocateInfo
) {
163 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
164 .pNext
= &import_info
,
165 .allocationSize
= image
->layout
.size
,
166 .memoryTypeIndex
= 0,
169 if (result
!= VK_SUCCESS
)
170 goto fail_create_image
;
172 tu_BindImageMemory(device_h
, image_h
, memory_h
, 0);
174 image
->owned_memory
= memory_h
;
175 /* Don't clobber the out-parameter until success is certain. */
176 *out_image_h
= image_h
;
181 tu_DestroyImage(device_h
, image_h
, alloc
);
187 tu_GetSwapchainGrallocUsageANDROID(VkDevice device_h
,
189 VkImageUsageFlags imageUsage
,
192 TU_FROM_HANDLE(tu_device
, device
, device_h
);
193 struct tu_physical_device
*phys_dev
= device
->physical_device
;
194 VkPhysicalDevice phys_dev_h
= tu_physical_device_to_handle(phys_dev
);
199 /* WARNING: Android Nougat's libvulkan.so hardcodes the VkImageUsageFlags
200 * returned to applications via
201 * VkSurfaceCapabilitiesKHR::supportedUsageFlags.
202 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
204 * TODO(jessehall): I think these are right, but haven't thought hard
205 * about it. Do we need to query the driver for support of any of
208 * Any disagreement between this function and the hardcoded
209 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
210 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
213 const VkPhysicalDeviceImageFormatInfo2 image_format_info
= {
214 .sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2
,
216 .type
= VK_IMAGE_TYPE_2D
,
217 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
221 VkImageFormatProperties2 image_format_props
= {
222 .sType
= VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2
,
225 /* Check that requested format and usage are supported. */
226 result
= tu_GetPhysicalDeviceImageFormatProperties2(
227 phys_dev_h
, &image_format_info
, &image_format_props
);
228 if (result
!= VK_SUCCESS
) {
229 return vk_errorf(device
->instance
, result
,
230 "tu_GetPhysicalDeviceImageFormatProperties2 failed "
235 if (unmask32(&imageUsage
, VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
236 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
))
237 *grallocUsage
|= GRALLOC_USAGE_HW_RENDER
;
239 if (unmask32(&imageUsage
, VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
240 VK_IMAGE_USAGE_SAMPLED_BIT
|
241 VK_IMAGE_USAGE_STORAGE_BIT
|
242 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
243 *grallocUsage
|= GRALLOC_USAGE_HW_TEXTURE
;
245 /* All VkImageUsageFlags not explicitly checked here are unsupported for
246 * gralloc swapchains.
248 if (imageUsage
!= 0) {
249 return vk_errorf(device
->instance
, VK_ERROR_FORMAT_NOT_SUPPORTED
,
250 "unsupported VkImageUsageFlags(0x%x) for gralloc "
256 * FINISHME: Advertise all display-supported formats. Mostly
257 * DRM_FORMAT_ARGB2101010 and DRM_FORMAT_ABGR2101010, but need to check
258 * what we need for 30-bit colors.
260 if (format
== VK_FORMAT_B8G8R8A8_UNORM
||
261 format
== VK_FORMAT_B5G6R5_UNORM_PACK16
) {
262 *grallocUsage
|= GRALLOC_USAGE_HW_FB
| GRALLOC_USAGE_HW_COMPOSER
|
263 GRALLOC_USAGE_EXTERNAL_DISP
;
266 if (*grallocUsage
== 0)
267 return VK_ERROR_FORMAT_NOT_SUPPORTED
;
273 tu_GetSwapchainGrallocUsage2ANDROID(VkDevice device
,
275 VkImageUsageFlags imageUsage
,
276 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage
,
277 uint64_t *grallocConsumerUsage
,
278 uint64_t *grallocProducerUsage
)
286 tu_AcquireImageANDROID(VkDevice device
,
289 VkSemaphore semaphore
,
292 VkResult semaphore_result
= VK_SUCCESS
, fence_result
= VK_SUCCESS
;
294 if (semaphore
!= VK_NULL_HANDLE
) {
296 nativeFenceFd
>= 0 ? dup(nativeFenceFd
) : nativeFenceFd
;
297 semaphore_result
= tu_ImportSemaphoreFdKHR(
298 device
, &(VkImportSemaphoreFdInfoKHR
) {
299 .sType
= VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR
,
300 .flags
= VK_SEMAPHORE_IMPORT_TEMPORARY_BIT
,
302 .semaphore
= semaphore
,
306 if (fence
!= VK_NULL_HANDLE
) {
307 int fence_fd
= nativeFenceFd
>= 0 ? dup(nativeFenceFd
) : nativeFenceFd
;
308 fence_result
= tu_ImportFenceFdKHR(
309 device
, &(VkImportFenceFdInfoKHR
) {
310 .sType
= VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR
,
311 .flags
= VK_FENCE_IMPORT_TEMPORARY_BIT
,
317 close(nativeFenceFd
);
319 if (semaphore_result
!= VK_SUCCESS
)
320 return semaphore_result
;
325 tu_QueueSignalReleaseImageANDROID(VkQueue _queue
,
326 uint32_t waitSemaphoreCount
,
327 const VkSemaphore
*pWaitSemaphores
,
331 TU_FROM_HANDLE(tu_queue
, queue
, _queue
);
332 VkResult result
= VK_SUCCESS
;
334 if (waitSemaphoreCount
== 0) {
336 *pNativeFenceFd
= -1;
342 for (uint32_t i
= 0; i
< waitSemaphoreCount
; ++i
) {
344 result
= tu_GetSemaphoreFdKHR(
345 tu_device_to_handle(queue
->device
),
346 &(VkSemaphoreGetFdInfoKHR
) {
347 .sType
= VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR
,
348 .handleType
= VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
,
349 .semaphore
= pWaitSemaphores
[i
],
352 if (result
!= VK_SUCCESS
) {
360 else if (tmp_fd
>= 0) {
361 sync_accumulate("tu", &fd
, tmp_fd
);
366 if (pNativeFenceFd
) {
367 *pNativeFenceFd
= fd
;
368 } else if (fd
>= 0) {
370 /* We still need to do the exports, to reset the semaphores, but
371 * otherwise we don't wait on them. */