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 DEALINGS
24 #include <hardware/gralloc.h>
25 #include <hardware/hardware.h>
26 #include <hardware/hwvulkan.h>
27 #include <vulkan/vk_android_native_buffer.h>
28 #include <vulkan/vk_icd.h>
31 #include "radv_private.h"
33 static int radv_hal_open(const struct hw_module_t
* mod
, const char* id
, struct hw_device_t
** dev
);
34 static int radv_hal_close(struct hw_device_t
*dev
);
39 STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC
== ICD_LOADER_MAGIC
);
42 PUBLIC
struct hwvulkan_module_t HAL_MODULE_INFO_SYM
= {
44 .tag
= HARDWARE_MODULE_TAG
,
45 .module_api_version
= HWVULKAN_MODULE_API_VERSION_0_1
,
46 .hal_api_version
= HARDWARE_MAKE_API_VERSION(1, 0),
47 .id
= HWVULKAN_HARDWARE_MODULE_ID
,
48 .name
= "AMD Vulkan HAL",
50 .methods
= &(hw_module_methods_t
) {
51 .open
= radv_hal_open
,
56 /* If any bits in test_mask are set, then unset them and return true. */
58 unmask32(uint32_t *inout_mask
, uint32_t test_mask
)
60 uint32_t orig_mask
= *inout_mask
;
61 *inout_mask
&= ~test_mask
;
62 return *inout_mask
!= orig_mask
;
66 radv_hal_open(const struct hw_module_t
* mod
, const char* id
,
67 struct hw_device_t
** dev
)
69 assert(mod
== &HAL_MODULE_INFO_SYM
.common
);
70 assert(strcmp(id
, HWVULKAN_DEVICE_0
) == 0);
72 hwvulkan_device_t
*hal_dev
= malloc(sizeof(*hal_dev
));
76 *hal_dev
= (hwvulkan_device_t
) {
78 .tag
= HARDWARE_DEVICE_TAG
,
79 .version
= HWVULKAN_DEVICE_API_VERSION_0_1
,
80 .module
= &HAL_MODULE_INFO_SYM
.common
,
81 .close
= radv_hal_close
,
83 .EnumerateInstanceExtensionProperties
= radv_EnumerateInstanceExtensionProperties
,
84 .CreateInstance
= radv_CreateInstance
,
85 .GetInstanceProcAddr
= radv_GetInstanceProcAddr
,
88 *dev
= &hal_dev
->common
;
93 radv_hal_close(struct hw_device_t
*dev
)
95 /* hwvulkan.h claims that hw_device_t::close() is never called. */
100 radv_image_from_gralloc(VkDevice device_h
,
101 const VkImageCreateInfo
*base_info
,
102 const VkNativeBufferANDROID
*gralloc_info
,
103 const VkAllocationCallbacks
*alloc
,
104 VkImage
*out_image_h
)
107 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
108 VkImage image_h
= VK_NULL_HANDLE
;
109 struct radv_image
*image
= NULL
;
110 struct radv_bo
*bo
= NULL
;
113 result
= radv_image_create(device_h
,
114 &(struct radv_image_create_info
) {
115 .vk_info
= base_info
,
117 .no_metadata_planes
= true},
121 if (result
!= VK_SUCCESS
)
124 if (gralloc_info
->handle
->numFds
!= 1) {
125 return vk_errorf(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
,
126 "VkNativeBufferANDROID::handle::numFds is %d, "
127 "expected 1", gralloc_info
->handle
->numFds
);
130 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
131 * must exceed that of the gralloc handle, and we do not own the gralloc
134 int dma_buf
= gralloc_info
->handle
->data
[0];
136 image
= radv_image_from_handle(image_h
);
138 VkDeviceMemory memory_h
;
140 const VkMemoryDedicatedAllocateInfoKHR ded_alloc
= {
141 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
143 .buffer
= VK_NULL_HANDLE
,
147 const VkImportMemoryFdInfoKHR import_info
= {
148 .sType
= VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR
,
150 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR
,
153 /* Find the first VRAM memory type, or GART for PRIME images. */
154 int memory_type_index
= -1;
155 for (int i
= 0; i
< device
->physical_device
->memory_properties
.memoryTypeCount
; ++i
) {
156 bool is_local
= !!(device
->physical_device
->memory_properties
.memoryTypes
[i
].propertyFlags
& VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
);
158 memory_type_index
= i
;
164 if (memory_type_index
== -1)
165 memory_type_index
= 0;
167 result
= radv_AllocateMemory(device_h
,
168 &(VkMemoryAllocateInfo
) {
169 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
170 .pNext
= &import_info
,
171 .allocationSize
= image
->size
,
172 .memoryTypeIndex
= memory_type_index
,
176 if (result
!= VK_SUCCESS
)
177 goto fail_create_image
;
179 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
181 image
->owned_memory
= memory_h
;
182 /* Don't clobber the out-parameter until success is certain. */
183 *out_image_h
= image_h
;
189 radv_DestroyImage(device_h
, image_h
, alloc
);
194 VkResult
radv_GetSwapchainGrallocUsageANDROID(
197 VkImageUsageFlags imageUsage
,
200 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
201 struct radv_physical_device
*phys_dev
= device
->physical_device
;
202 VkPhysicalDevice phys_dev_h
= radv_physical_device_to_handle(phys_dev
);
207 /* WARNING: Android Nougat's libvulkan.so hardcodes the VkImageUsageFlags
208 * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
209 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
211 * TODO(jessehall): I think these are right, but haven't thought hard
212 * about it. Do we need to query the driver for support of any of
215 * Any disagreement between this function and the hardcoded
216 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
217 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
220 const VkPhysicalDeviceImageFormatInfo2KHR image_format_info
= {
221 .sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2_KHR
,
223 .type
= VK_IMAGE_TYPE_2D
,
224 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
228 VkImageFormatProperties2KHR image_format_props
= {
229 .sType
= VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR
,
232 /* Check that requested format and usage are supported. */
233 result
= radv_GetPhysicalDeviceImageFormatProperties2KHR(phys_dev_h
,
234 &image_format_info
, &image_format_props
);
235 if (result
!= VK_SUCCESS
) {
236 return vk_errorf(result
,
237 "radv_GetPhysicalDeviceImageFormatProperties2KHR failed "
238 "inside %s", __func__
);
241 if (unmask32(&imageUsage
, VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
242 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
))
243 *grallocUsage
|= GRALLOC_USAGE_HW_RENDER
;
245 if (unmask32(&imageUsage
, VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
246 VK_IMAGE_USAGE_SAMPLED_BIT
|
247 VK_IMAGE_USAGE_STORAGE_BIT
|
248 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
249 *grallocUsage
|= GRALLOC_USAGE_HW_TEXTURE
;
251 /* All VkImageUsageFlags not explicitly checked here are unsupported for
252 * gralloc swapchains.
254 if (imageUsage
!= 0) {
255 return vk_errorf(VK_ERROR_FORMAT_NOT_SUPPORTED
,
256 "unsupported VkImageUsageFlags(0x%x) for gralloc "
257 "swapchain", imageUsage
);
261 * FINISHME: Advertise all display-supported formats. Mostly
262 * DRM_FORMAT_ARGB2101010 and DRM_FORMAT_ABGR2101010, but need to check
263 * what we need for 30-bit colors.
265 if (format
== VK_FORMAT_B8G8R8A8_UNORM
||
266 format
== VK_FORMAT_B5G6R5_UNORM_PACK16
) {
267 *grallocUsage
|= GRALLOC_USAGE_HW_FB
|
268 GRALLOC_USAGE_HW_COMPOSER
|
269 GRALLOC_USAGE_EXTERNAL_DISP
;
272 if (*grallocUsage
== 0)
273 return VK_ERROR_FORMAT_NOT_SUPPORTED
;
279 radv_AcquireImageANDROID(
283 VkSemaphore semaphore
,
286 VkResult semaphore_result
= VK_SUCCESS
, fence_result
= VK_SUCCESS
;
288 if (semaphore
!= VK_NULL_HANDLE
) {
289 int semaphore_fd
= nativeFenceFd
>= 0 ? dup(nativeFenceFd
) : nativeFenceFd
;
290 semaphore_result
= radv_ImportSemaphoreFdKHR(device
,
291 &(VkImportSemaphoreFdInfoKHR
) {
292 .sType
= VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR
,
293 .flags
= VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR
,
295 .semaphore
= semaphore
,
299 if (fence
!= VK_NULL_HANDLE
) {
300 int fence_fd
= nativeFenceFd
>= 0 ? dup(nativeFenceFd
) : nativeFenceFd
;
301 fence_result
= radv_ImportFenceFdKHR(device
,
302 &(VkImportFenceFdInfoKHR
) {
303 .sType
= VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR
,
304 .flags
= VK_FENCE_IMPORT_TEMPORARY_BIT_KHR
,
310 close(nativeFenceFd
);
312 if (semaphore_result
!= VK_SUCCESS
)
313 return semaphore_result
;
318 radv_QueueSignalReleaseImageANDROID(
320 uint32_t waitSemaphoreCount
,
321 const VkSemaphore
* pWaitSemaphores
,
325 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
326 VkResult result
= VK_SUCCESS
;
328 if (waitSemaphoreCount
== 0) {
330 *pNativeFenceFd
= -1;
336 for (uint32_t i
= 0; i
< waitSemaphoreCount
; ++i
) {
338 result
= radv_GetSemaphoreFdKHR(radv_device_to_handle(queue
->device
),
339 &(VkSemaphoreGetFdInfoKHR
) {
340 .sType
= VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR
,
341 .handleType
= VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR
,
342 .semaphore
= pWaitSemaphores
[i
],
344 if (result
!= VK_SUCCESS
) {
352 else if (tmp_fd
>= 0) {
353 sync_accumulate("radv", &fd
, tmp_fd
);
358 if (pNativeFenceFd
) {
359 *pNativeFenceFd
= fd
;
360 } else if (fd
>= 0) {
362 /* We still need to do the exports, to reset the semaphores, but
363 * otherwise we don't wait on them. */