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
25 #include <hardware/gralloc.h>
26 #include <hardware/hardware.h>
27 #include <hardware/hwvulkan.h>
28 #include <vndk/hardware_buffer.h>
29 #include <vulkan/vk_android_native_buffer.h>
30 #include <vulkan/vk_icd.h>
34 #include "radv_private.h"
39 static int radv_hal_open(const struct hw_module_t
* mod
, const char* id
, struct hw_device_t
** dev
);
40 static int radv_hal_close(struct hw_device_t
*dev
);
45 STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC
== ICD_LOADER_MAGIC
);
48 PUBLIC
struct hwvulkan_module_t HAL_MODULE_INFO_SYM
= {
50 .tag
= HARDWARE_MODULE_TAG
,
51 .module_api_version
= HWVULKAN_MODULE_API_VERSION_0_1
,
52 .hal_api_version
= HARDWARE_MAKE_API_VERSION(1, 0),
53 .id
= HWVULKAN_HARDWARE_MODULE_ID
,
54 .name
= "AMD Vulkan HAL",
56 .methods
= &(hw_module_methods_t
) {
57 .open
= radv_hal_open
,
62 /* If any bits in test_mask are set, then unset them and return true. */
64 unmask32(uint32_t *inout_mask
, uint32_t test_mask
)
66 uint32_t orig_mask
= *inout_mask
;
67 *inout_mask
&= ~test_mask
;
68 return *inout_mask
!= orig_mask
;
72 radv_hal_open(const struct hw_module_t
* mod
, const char* id
,
73 struct hw_device_t
** dev
)
75 assert(mod
== &HAL_MODULE_INFO_SYM
.common
);
76 assert(strcmp(id
, HWVULKAN_DEVICE_0
) == 0);
78 hwvulkan_device_t
*hal_dev
= malloc(sizeof(*hal_dev
));
82 *hal_dev
= (hwvulkan_device_t
) {
84 .tag
= HARDWARE_DEVICE_TAG
,
85 .version
= HWVULKAN_DEVICE_API_VERSION_0_1
,
86 .module
= &HAL_MODULE_INFO_SYM
.common
,
87 .close
= radv_hal_close
,
89 .EnumerateInstanceExtensionProperties
= radv_EnumerateInstanceExtensionProperties
,
90 .CreateInstance
= radv_CreateInstance
,
91 .GetInstanceProcAddr
= radv_GetInstanceProcAddr
,
94 *dev
= &hal_dev
->common
;
99 radv_hal_close(struct hw_device_t
*dev
)
101 /* hwvulkan.h claims that hw_device_t::close() is never called. */
106 radv_image_from_gralloc(VkDevice device_h
,
107 const VkImageCreateInfo
*base_info
,
108 const VkNativeBufferANDROID
*gralloc_info
,
109 const VkAllocationCallbacks
*alloc
,
110 VkImage
*out_image_h
)
113 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
114 VkImage image_h
= VK_NULL_HANDLE
;
115 struct radv_image
*image
= NULL
;
116 struct radv_bo
*bo
= NULL
;
119 if (gralloc_info
->handle
->numFds
!= 1) {
120 return vk_errorf(device
->instance
, VK_ERROR_INVALID_EXTERNAL_HANDLE
,
121 "VkNativeBufferANDROID::handle::numFds is %d, "
122 "expected 1", gralloc_info
->handle
->numFds
);
125 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
126 * must exceed that of the gralloc handle, and we do not own the gralloc
129 int dma_buf
= gralloc_info
->handle
->data
[0];
131 VkDeviceMemory memory_h
;
133 const VkImportMemoryFdInfoKHR import_info
= {
134 .sType
= VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR
,
135 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
,
139 /* Find the first VRAM memory type, or GART for PRIME images. */
140 int memory_type_index
= -1;
141 for (int i
= 0; i
< device
->physical_device
->memory_properties
.memoryTypeCount
; ++i
) {
142 bool is_local
= !!(device
->physical_device
->memory_properties
.memoryTypes
[i
].propertyFlags
& VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
);
144 memory_type_index
= i
;
150 if (memory_type_index
== -1)
151 memory_type_index
= 0;
153 result
= radv_AllocateMemory(device_h
,
154 &(VkMemoryAllocateInfo
) {
155 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
156 .pNext
= &import_info
,
157 /* Max buffer size, unused for imports */
158 .allocationSize
= 0x7FFFFFFF,
159 .memoryTypeIndex
= memory_type_index
,
163 if (result
!= VK_SUCCESS
)
166 struct radeon_bo_metadata md
;
167 device
->ws
->buffer_get_metadata(radv_device_memory_from_handle(memory_h
)->bo
, &md
);
169 VkImageCreateInfo updated_base_info
= *base_info
;
171 VkExternalMemoryImageCreateInfo external_memory_info
= {
172 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO
,
173 .pNext
= updated_base_info
.pNext
,
174 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
177 updated_base_info
.pNext
= &external_memory_info
;
179 result
= radv_image_create(device_h
,
180 &(struct radv_image_create_info
) {
181 .vk_info
= &updated_base_info
,
182 .no_metadata_planes
= true,
188 if (result
!= VK_SUCCESS
)
189 goto fail_create_image
;
191 image
= radv_image_from_handle(image_h
);
193 radv_image_override_offset_stride(device
, image
, 0, gralloc_info
->stride
);
195 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
197 image
->owned_memory
= memory_h
;
198 /* Don't clobber the out-parameter until success is certain. */
199 *out_image_h
= image_h
;
204 radv_FreeMemory(device_h
, memory_h
, alloc
);
208 VkResult
radv_GetSwapchainGrallocUsageANDROID(
211 VkImageUsageFlags imageUsage
,
214 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
215 struct radv_physical_device
*phys_dev
= device
->physical_device
;
216 VkPhysicalDevice phys_dev_h
= radv_physical_device_to_handle(phys_dev
);
221 /* WARNING: Android Nougat's libvulkan.so hardcodes the VkImageUsageFlags
222 * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
223 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
225 * TODO(jessehall): I think these are right, but haven't thought hard
226 * about it. Do we need to query the driver for support of any of
229 * Any disagreement between this function and the hardcoded
230 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
231 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
234 const VkPhysicalDeviceImageFormatInfo2 image_format_info
= {
235 .sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2
,
237 .type
= VK_IMAGE_TYPE_2D
,
238 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
242 VkImageFormatProperties2 image_format_props
= {
243 .sType
= VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2
,
246 /* Check that requested format and usage are supported. */
247 result
= radv_GetPhysicalDeviceImageFormatProperties2(phys_dev_h
,
248 &image_format_info
, &image_format_props
);
249 if (result
!= VK_SUCCESS
) {
250 return vk_errorf(device
->instance
, result
,
251 "radv_GetPhysicalDeviceImageFormatProperties2 failed "
252 "inside %s", __func__
);
255 if (unmask32(&imageUsage
, VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
256 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
))
257 *grallocUsage
|= GRALLOC_USAGE_HW_RENDER
;
259 if (unmask32(&imageUsage
, VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
260 VK_IMAGE_USAGE_SAMPLED_BIT
|
261 VK_IMAGE_USAGE_STORAGE_BIT
|
262 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
263 *grallocUsage
|= GRALLOC_USAGE_HW_TEXTURE
;
265 /* All VkImageUsageFlags not explicitly checked here are unsupported for
266 * gralloc swapchains.
268 if (imageUsage
!= 0) {
269 return vk_errorf(device
->instance
, VK_ERROR_FORMAT_NOT_SUPPORTED
,
270 "unsupported VkImageUsageFlags(0x%x) for gralloc "
271 "swapchain", imageUsage
);
275 * FINISHME: Advertise all display-supported formats. Mostly
276 * DRM_FORMAT_ARGB2101010 and DRM_FORMAT_ABGR2101010, but need to check
277 * what we need for 30-bit colors.
279 if (format
== VK_FORMAT_B8G8R8A8_UNORM
||
280 format
== VK_FORMAT_B5G6R5_UNORM_PACK16
) {
281 *grallocUsage
|= GRALLOC_USAGE_HW_FB
|
282 GRALLOC_USAGE_HW_COMPOSER
|
283 GRALLOC_USAGE_EXTERNAL_DISP
;
286 if (*grallocUsage
== 0)
287 return VK_ERROR_FORMAT_NOT_SUPPORTED
;
293 radv_AcquireImageANDROID(
297 VkSemaphore semaphore
,
300 VkResult semaphore_result
= VK_SUCCESS
, fence_result
= VK_SUCCESS
;
302 if (semaphore
!= VK_NULL_HANDLE
) {
303 int semaphore_fd
= nativeFenceFd
>= 0 ? dup(nativeFenceFd
) : nativeFenceFd
;
304 semaphore_result
= radv_ImportSemaphoreFdKHR(device
,
305 &(VkImportSemaphoreFdInfoKHR
) {
306 .sType
= VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR
,
307 .flags
= VK_SEMAPHORE_IMPORT_TEMPORARY_BIT
,
309 .semaphore
= semaphore
,
313 if (fence
!= VK_NULL_HANDLE
) {
314 int fence_fd
= nativeFenceFd
>= 0 ? dup(nativeFenceFd
) : nativeFenceFd
;
315 fence_result
= radv_ImportFenceFdKHR(device
,
316 &(VkImportFenceFdInfoKHR
) {
317 .sType
= VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR
,
318 .flags
= VK_FENCE_IMPORT_TEMPORARY_BIT
,
324 close(nativeFenceFd
);
326 if (semaphore_result
!= VK_SUCCESS
)
327 return semaphore_result
;
332 radv_QueueSignalReleaseImageANDROID(
334 uint32_t waitSemaphoreCount
,
335 const VkSemaphore
* pWaitSemaphores
,
339 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
340 VkResult result
= VK_SUCCESS
;
342 if (waitSemaphoreCount
== 0) {
344 *pNativeFenceFd
= -1;
350 for (uint32_t i
= 0; i
< waitSemaphoreCount
; ++i
) {
352 result
= radv_GetSemaphoreFdKHR(radv_device_to_handle(queue
->device
),
353 &(VkSemaphoreGetFdInfoKHR
) {
354 .sType
= VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR
,
355 .handleType
= VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
,
356 .semaphore
= pWaitSemaphores
[i
],
358 if (result
!= VK_SUCCESS
) {
366 else if (tmp_fd
>= 0) {
367 sync_accumulate("radv", &fd
, tmp_fd
);
372 if (pNativeFenceFd
) {
373 *pNativeFenceFd
= fd
;
374 } else if (fd
>= 0) {
376 /* We still need to do the exports, to reset the semaphores, but
377 * otherwise we don't wait on them. */
383 #if RADV_SUPPORT_ANDROID_HARDWARE_BUFFER
386 /* Usage bit equal to GRALLOC_USAGE_HW_CAMERA_MASK */
387 AHARDWAREBUFFER_USAGE_CAMERA_MASK
= 0x00060000U
,
390 static inline VkFormat
391 vk_format_from_android(unsigned android_format
, unsigned android_usage
)
393 switch (android_format
) {
394 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM
:
395 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM
:
396 return VK_FORMAT_R8G8B8A8_UNORM
;
397 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM
:
398 return VK_FORMAT_R8G8B8_UNORM
;
399 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM
:
400 return VK_FORMAT_R5G6B5_UNORM_PACK16
;
401 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT
:
402 return VK_FORMAT_R16G16B16A16_SFLOAT
;
403 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM
:
404 return VK_FORMAT_A2B10G10R10_UNORM_PACK32
;
405 case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420
:
406 return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
;
407 case AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED
:
408 if (android_usage
& AHARDWAREBUFFER_USAGE_CAMERA_MASK
)
409 return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
;
411 return VK_FORMAT_R8G8B8_UNORM
;
412 case AHARDWAREBUFFER_FORMAT_BLOB
:
414 return VK_FORMAT_UNDEFINED
;
418 static inline unsigned
419 android_format_from_vk(unsigned vk_format
)
422 case VK_FORMAT_R8G8B8A8_UNORM
:
423 return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM
;
424 case VK_FORMAT_R8G8B8_UNORM
:
425 return AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM
;
426 case VK_FORMAT_R5G6B5_UNORM_PACK16
:
427 return AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM
;
428 case VK_FORMAT_R16G16B16A16_SFLOAT
:
429 return AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT
;
430 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
431 return AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM
;
432 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
:
433 return AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420
;
435 return AHARDWAREBUFFER_FORMAT_BLOB
;
440 radv_ahb_usage_from_vk_usage(const VkImageCreateFlags vk_create
,
441 const VkImageUsageFlags vk_usage
)
443 uint64_t ahb_usage
= 0;
444 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
445 ahb_usage
|= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE
;
447 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
448 ahb_usage
|= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE
;
450 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
451 ahb_usage
|= AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT
;
453 if (vk_create
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
454 ahb_usage
|= AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP
;
456 if (vk_create
& VK_IMAGE_CREATE_PROTECTED_BIT
)
457 ahb_usage
|= AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT
;
459 /* No usage bits set - set at least one GPU usage. */
461 ahb_usage
= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE
;
466 get_ahb_buffer_format_properties(
468 const struct AHardwareBuffer
*buffer
,
469 VkAndroidHardwareBufferFormatPropertiesANDROID
*pProperties
)
471 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
473 /* Get a description of buffer contents . */
474 AHardwareBuffer_Desc desc
;
475 AHardwareBuffer_describe(buffer
, &desc
);
477 /* Verify description. */
478 const uint64_t gpu_usage
=
479 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE
|
480 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT
|
481 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER
;
483 /* "Buffer must be a valid Android hardware buffer object with at least
484 * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags."
486 if (!(desc
.usage
& (gpu_usage
)))
487 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
489 /* Fill properties fields based on description. */
490 VkAndroidHardwareBufferFormatPropertiesANDROID
*p
= pProperties
;
492 p
->format
= vk_format_from_android(desc
.format
, desc
.usage
);
493 p
->externalFormat
= (uint64_t) (uintptr_t) p
->format
;
495 VkFormatProperties format_properties
;
496 radv_GetPhysicalDeviceFormatProperties(
497 radv_physical_device_to_handle(device
->physical_device
),
498 p
->format
, &format_properties
);
500 if (desc
.usage
& AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER
)
501 p
->formatFeatures
= format_properties
.linearTilingFeatures
;
503 p
->formatFeatures
= format_properties
.optimalTilingFeatures
;
505 /* "Images can be created with an external format even if the Android hardware
506 * buffer has a format which has an equivalent Vulkan format to enable
507 * consistent handling of images from sources that might use either category
508 * of format. However, all images created with an external format are subject
509 * to the valid usage requirements associated with external formats, even if
510 * the Android hardware buffer’s format has a Vulkan equivalent."
512 * "The formatFeatures member *must* include
513 * VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and at least one of
514 * VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT or
515 * VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT"
517 assert(p
->formatFeatures
& VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
);
519 p
->formatFeatures
|= VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT
;
521 /* "Implementations may not always be able to determine the color model,
522 * numerical range, or chroma offsets of the image contents, so the values
523 * in VkAndroidHardwareBufferFormatPropertiesANDROID are only suggestions.
524 * Applications should treat these values as sensible defaults to use in
525 * the absence of more reliable information obtained through some other
528 p
->samplerYcbcrConversionComponents
.r
= VK_COMPONENT_SWIZZLE_IDENTITY
;
529 p
->samplerYcbcrConversionComponents
.g
= VK_COMPONENT_SWIZZLE_IDENTITY
;
530 p
->samplerYcbcrConversionComponents
.b
= VK_COMPONENT_SWIZZLE_IDENTITY
;
531 p
->samplerYcbcrConversionComponents
.a
= VK_COMPONENT_SWIZZLE_IDENTITY
;
533 p
->suggestedYcbcrModel
= VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601
;
534 p
->suggestedYcbcrRange
= VK_SAMPLER_YCBCR_RANGE_ITU_FULL
;
536 p
->suggestedXChromaOffset
= VK_CHROMA_LOCATION_MIDPOINT
;
537 p
->suggestedYChromaOffset
= VK_CHROMA_LOCATION_MIDPOINT
;
543 radv_GetAndroidHardwareBufferPropertiesANDROID(
545 const struct AHardwareBuffer
*buffer
,
546 VkAndroidHardwareBufferPropertiesANDROID
*pProperties
)
548 RADV_FROM_HANDLE(radv_device
, dev
, device_h
);
549 struct radv_physical_device
*pdevice
= dev
->physical_device
;
551 VkAndroidHardwareBufferFormatPropertiesANDROID
*format_prop
=
552 vk_find_struct(pProperties
->pNext
,
553 ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID
);
555 /* Fill format properties of an Android hardware buffer. */
557 get_ahb_buffer_format_properties(device_h
, buffer
, format_prop
);
559 /* NOTE - We support buffers with only one handle but do not error on
560 * multiple handle case. Reason is that we want to support YUV formats
561 * where we have many logical planes but they all point to the same
562 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
564 const native_handle_t
*handle
=
565 AHardwareBuffer_getNativeHandle(buffer
);
566 int dma_buf
= (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
568 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
570 /* All memory types. */
571 uint32_t memory_types
= (1u << pdevice
->memory_properties
.memoryTypeCount
) - 1;
573 pProperties
->allocationSize
= lseek(dma_buf
, 0, SEEK_END
);
574 pProperties
->memoryTypeBits
= memory_types
;
580 radv_GetMemoryAndroidHardwareBufferANDROID(
582 const VkMemoryGetAndroidHardwareBufferInfoANDROID
*pInfo
,
583 struct AHardwareBuffer
**pBuffer
)
585 RADV_FROM_HANDLE(radv_device_memory
, mem
, pInfo
->memory
);
587 /* This should always be set due to the export handle types being set on
589 assert(mem
->android_hardware_buffer
);
591 /* Some quotes from Vulkan spec:
593 * "If the device memory was created by importing an Android hardware
594 * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
595 * Android hardware buffer object."
597 * "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
598 * have been included in VkExportMemoryAllocateInfo::handleTypes when
599 * memory was created."
601 *pBuffer
= mem
->android_hardware_buffer
;
602 /* Increase refcount. */
603 AHardwareBuffer_acquire(mem
->android_hardware_buffer
);
610 radv_select_android_external_format(const void *next
, VkFormat default_format
)
612 #if RADV_SUPPORT_ANDROID_HARDWARE_BUFFER
613 const VkExternalFormatANDROID
*android_format
=
614 vk_find_struct_const(next
, EXTERNAL_FORMAT_ANDROID
);
616 if (android_format
&& android_format
->externalFormat
) {
617 return (VkFormat
)android_format
->externalFormat
;
621 return default_format
;
626 radv_import_ahb_memory(struct radv_device
*device
,
627 struct radv_device_memory
*mem
,
629 const VkImportAndroidHardwareBufferInfoANDROID
*info
)
631 #if RADV_SUPPORT_ANDROID_HARDWARE_BUFFER
632 /* Import from AHardwareBuffer to radv_device_memory. */
633 const native_handle_t
*handle
=
634 AHardwareBuffer_getNativeHandle(info
->buffer
);
636 /* NOTE - We support buffers with only one handle but do not error on
637 * multiple handle case. Reason is that we want to support YUV formats
638 * where we have many logical planes but they all point to the same
639 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
641 int dma_buf
= (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
643 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
645 uint64_t alloc_size
= 0;
646 mem
->bo
= device
->ws
->buffer_from_fd(device
->ws
, dma_buf
,
647 priority
, &alloc_size
);
649 return VK_ERROR_OUT_OF_HOST_MEMORY
;
652 struct radeon_bo_metadata metadata
;
653 device
->ws
->buffer_get_metadata(mem
->bo
, &metadata
);
655 struct radv_image_create_info create_info
= {
656 .no_metadata_planes
= true,
657 .bo_metadata
= &metadata
660 VkResult result
= radv_image_create_layout(device
, create_info
, mem
->image
);
661 if (result
!= VK_SUCCESS
) {
662 device
->ws
->buffer_destroy(mem
->bo
);
667 if (alloc_size
< mem
->image
->size
) {
668 device
->ws
->buffer_destroy(mem
->bo
);
670 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
672 } else if (mem
->buffer
) {
673 if (alloc_size
< mem
->buffer
->size
) {
674 device
->ws
->buffer_destroy(mem
->bo
);
676 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
680 /* "If the vkAllocateMemory command succeeds, the implementation must
681 * acquire a reference to the imported hardware buffer, which it must
682 * release when the device memory object is freed. If the command fails,
683 * the implementation must not retain a reference."
685 AHardwareBuffer_acquire(info
->buffer
);
686 mem
->android_hardware_buffer
= info
->buffer
;
689 #else /* RADV_SUPPORT_ANDROID_HARDWARE_BUFFER */
690 return VK_ERROR_EXTENSION_NOT_PRESENT
;
695 radv_create_ahb_memory(struct radv_device
*device
,
696 struct radv_device_memory
*mem
,
698 const VkMemoryAllocateInfo
*pAllocateInfo
)
700 #if RADV_SUPPORT_ANDROID_HARDWARE_BUFFER
701 const VkMemoryDedicatedAllocateInfo
*dedicated_info
=
702 vk_find_struct_const(pAllocateInfo
->pNext
,
703 MEMORY_DEDICATED_ALLOCATE_INFO
);
711 /* If caller passed dedicated information. */
712 if (dedicated_info
&& dedicated_info
->image
) {
713 RADV_FROM_HANDLE(radv_image
, image
, dedicated_info
->image
);
714 w
= image
->info
.width
;
715 h
= image
->info
.height
;
716 layers
= image
->info
.array_size
;
717 format
= android_format_from_vk(image
->vk_format
);
718 usage
= radv_ahb_usage_from_vk_usage(image
->flags
, image
->usage
);
719 } else if (dedicated_info
&& dedicated_info
->buffer
) {
720 RADV_FROM_HANDLE(radv_buffer
, buffer
, dedicated_info
->buffer
);
722 format
= AHARDWAREBUFFER_FORMAT_BLOB
;
723 usage
= AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
|
724 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN
;
726 w
= pAllocateInfo
->allocationSize
;
727 format
= AHARDWAREBUFFER_FORMAT_BLOB
;
728 usage
= AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
|
729 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN
;
732 struct AHardwareBuffer
*android_hardware_buffer
= NULL
;
733 struct AHardwareBuffer_Desc desc
= {
741 if (AHardwareBuffer_allocate(&desc
, &android_hardware_buffer
) != 0)
742 return VK_ERROR_OUT_OF_HOST_MEMORY
;
744 mem
->android_hardware_buffer
= android_hardware_buffer
;
746 const struct VkImportAndroidHardwareBufferInfoANDROID import_info
= {
747 .buffer
= mem
->android_hardware_buffer
,
750 VkResult result
= radv_import_ahb_memory(device
, mem
, priority
, &import_info
);
751 if (result
!= VK_SUCCESS
)
752 AHardwareBuffer_release(mem
->android_hardware_buffer
);
754 #else /* RADV_SUPPORT_ANDROID_HARDWARE_BUFFER */
755 return VK_ERROR_EXTENSION_NOT_PRESENT
;
759 bool radv_android_gralloc_supports_format(VkFormat format
, VkImageUsageFlagBits usage
) {
760 #if RADV_SUPPORT_ANDROID_HARDWARE_BUFFER
761 /* Ideally we check Gralloc for what it supports and then merge that with the radv
762 format support, but there is no easy gralloc query besides just creating an image.
763 That seems a bit on the expensive side, so just hardcode for now. */
764 /* TODO: Add multi-plane formats after confirming everything works between radeonsi
767 case VK_FORMAT_R8G8B8A8_UNORM
:
768 case VK_FORMAT_R5G6B5_UNORM_PACK16
:
770 case VK_FORMAT_R8_UNORM
:
771 case VK_FORMAT_R8G8_UNORM
:
772 return !(usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);