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>
26 #if ANDROID_API_LEVEL >= 26
27 #include <hardware/gralloc1.h>
28 #include <grallocusage/GrallocUsageConversion.h>
31 #include <hardware/hardware.h>
32 #include <hardware/hwvulkan.h>
33 #include <vulkan/vk_android_native_buffer.h>
34 #include <vulkan/vk_icd.h>
35 #include <sync/sync.h>
37 #include "anv_private.h"
38 #include "vk_format_info.h"
41 static int anv_hal_open(const struct hw_module_t
* mod
, const char* id
, struct hw_device_t
** dev
);
42 static int anv_hal_close(struct hw_device_t
*dev
);
47 STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC
== ICD_LOADER_MAGIC
);
50 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
= "Intel Vulkan HAL",
58 .methods
= &(hw_module_methods_t
) {
64 /* If any bits in test_mask are set, then unset them and return true. */
66 unmask32(uint32_t *inout_mask
, uint32_t test_mask
)
68 uint32_t orig_mask
= *inout_mask
;
69 *inout_mask
&= ~test_mask
;
70 return *inout_mask
!= orig_mask
;
74 anv_hal_open(const struct hw_module_t
* mod
, const char* id
,
75 struct hw_device_t
** dev
)
77 assert(mod
== &HAL_MODULE_INFO_SYM
.common
);
78 assert(strcmp(id
, HWVULKAN_DEVICE_0
) == 0);
80 hwvulkan_device_t
*hal_dev
= malloc(sizeof(*hal_dev
));
84 *hal_dev
= (hwvulkan_device_t
) {
86 .tag
= HARDWARE_DEVICE_TAG
,
87 .version
= HWVULKAN_DEVICE_API_VERSION_0_1
,
88 .module
= &HAL_MODULE_INFO_SYM
.common
,
89 .close
= anv_hal_close
,
91 .EnumerateInstanceExtensionProperties
= anv_EnumerateInstanceExtensionProperties
,
92 .CreateInstance
= anv_CreateInstance
,
93 .GetInstanceProcAddr
= anv_GetInstanceProcAddr
,
96 *dev
= &hal_dev
->common
;
101 anv_hal_close(struct hw_device_t
*dev
)
103 /* hwvulkan.h claims that hw_device_t::close() is never called. */
107 #if ANDROID_API_LEVEL >= 26
109 get_ahw_buffer_format_properties(
111 const struct AHardwareBuffer
*buffer
,
112 VkAndroidHardwareBufferFormatPropertiesANDROID
*pProperties
)
114 ANV_FROM_HANDLE(anv_device
, device
, device_h
);
116 /* Get a description of buffer contents . */
117 AHardwareBuffer_Desc desc
;
118 AHardwareBuffer_describe(buffer
, &desc
);
120 /* Verify description. */
122 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE
|
123 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT
|
124 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER
;
126 /* "Buffer must be a valid Android hardware buffer object with at least
127 * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags."
129 if (!(desc
.usage
& (gpu_usage
)))
130 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
132 /* Fill properties fields based on description. */
133 VkAndroidHardwareBufferFormatPropertiesANDROID
*p
= pProperties
;
135 p
->format
= vk_format_from_android(desc
.format
, desc
.usage
);
137 const struct anv_format
*anv_format
= anv_get_format(p
->format
);
138 p
->externalFormat
= (uint64_t) (uintptr_t) anv_format
;
140 /* Default to OPTIMAL tiling but set to linear in case
141 * of AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER usage.
143 VkImageTiling tiling
= VK_IMAGE_TILING_OPTIMAL
;
145 if (desc
.usage
& AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER
)
146 tiling
= VK_IMAGE_TILING_LINEAR
;
149 anv_get_image_format_features(&device
->info
, p
->format
, anv_format
,
152 /* "Images can be created with an external format even if the Android hardware
153 * buffer has a format which has an equivalent Vulkan format to enable
154 * consistent handling of images from sources that might use either category
155 * of format. However, all images created with an external format are subject
156 * to the valid usage requirements associated with external formats, even if
157 * the Android hardware buffer’s format has a Vulkan equivalent."
159 * "The formatFeatures member *must* include
160 * VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and at least one of
161 * VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT or
162 * VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT"
165 VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT
;
167 /* "Implementations may not always be able to determine the color model,
168 * numerical range, or chroma offsets of the image contents, so the values
169 * in VkAndroidHardwareBufferFormatPropertiesANDROID are only suggestions.
170 * Applications should treat these values as sensible defaults to use in
171 * the absence of more reliable information obtained through some other
174 p
->samplerYcbcrConversionComponents
.r
= VK_COMPONENT_SWIZZLE_IDENTITY
;
175 p
->samplerYcbcrConversionComponents
.g
= VK_COMPONENT_SWIZZLE_IDENTITY
;
176 p
->samplerYcbcrConversionComponents
.b
= VK_COMPONENT_SWIZZLE_IDENTITY
;
177 p
->samplerYcbcrConversionComponents
.a
= VK_COMPONENT_SWIZZLE_IDENTITY
;
179 p
->suggestedYcbcrModel
= VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601
;
180 p
->suggestedYcbcrRange
= VK_SAMPLER_YCBCR_RANGE_ITU_FULL
;
182 p
->suggestedXChromaOffset
= VK_CHROMA_LOCATION_MIDPOINT
;
183 p
->suggestedYChromaOffset
= VK_CHROMA_LOCATION_MIDPOINT
;
189 anv_GetAndroidHardwareBufferPropertiesANDROID(
191 const struct AHardwareBuffer
*buffer
,
192 VkAndroidHardwareBufferPropertiesANDROID
*pProperties
)
194 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
195 struct anv_physical_device
*pdevice
= &dev
->instance
->physicalDevice
;
197 VkAndroidHardwareBufferFormatPropertiesANDROID
*format_prop
=
198 vk_find_struct(pProperties
->pNext
,
199 ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID
);
201 /* Fill format properties of an Android hardware buffer. */
203 get_ahw_buffer_format_properties(device_h
, buffer
, format_prop
);
205 /* NOTE - We support buffers with only one handle but do not error on
206 * multiple handle case. Reason is that we want to support YUV formats
207 * where we have many logical planes but they all point to the same
208 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
210 const native_handle_t
*handle
=
211 AHardwareBuffer_getNativeHandle(buffer
);
212 int dma_buf
= (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
214 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
216 /* All memory types. */
217 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
219 pProperties
->allocationSize
= lseek(dma_buf
, 0, SEEK_END
);
220 pProperties
->memoryTypeBits
= memory_types
;
226 anv_GetMemoryAndroidHardwareBufferANDROID(
228 const VkMemoryGetAndroidHardwareBufferInfoANDROID
*pInfo
,
229 struct AHardwareBuffer
**pBuffer
)
231 ANV_FROM_HANDLE(anv_device_memory
, mem
, pInfo
->memory
);
233 /* Some quotes from Vulkan spec:
235 * "If the device memory was created by importing an Android hardware
236 * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
237 * Android hardware buffer object."
239 * "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
240 * have been included in VkExportMemoryAllocateInfo::handleTypes when
241 * memory was created."
245 /* Increase refcount. */
246 AHardwareBuffer_acquire(mem
->ahw
);
250 return VK_ERROR_OUT_OF_HOST_MEMORY
;
255 /* Construct ahw usage mask from image usage bits, see
256 * 'AHardwareBuffer Usage Equivalence' in Vulkan spec.
259 anv_ahw_usage_from_vk_usage(const VkImageCreateFlags vk_create
,
260 const VkImageUsageFlags vk_usage
)
262 uint64_t ahw_usage
= 0;
263 #if ANDROID_API_LEVEL >= 26
264 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
265 ahw_usage
|= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE
;
267 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
268 ahw_usage
|= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE
;
270 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
271 ahw_usage
|= AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT
;
273 if (vk_create
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
274 ahw_usage
|= AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP
;
276 if (vk_create
& VK_IMAGE_CREATE_PROTECTED_BIT
)
277 ahw_usage
|= AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT
;
279 /* No usage bits set - set at least one GPU usage. */
281 ahw_usage
= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE
;
287 * Called from anv_AllocateMemory when import AHardwareBuffer.
290 anv_import_ahw_memory(VkDevice device_h
,
291 struct anv_device_memory
*mem
,
292 const VkImportAndroidHardwareBufferInfoANDROID
*info
)
294 #if ANDROID_API_LEVEL >= 26
295 ANV_FROM_HANDLE(anv_device
, device
, device_h
);
297 /* Import from AHardwareBuffer to anv_device_memory. */
298 const native_handle_t
*handle
=
299 AHardwareBuffer_getNativeHandle(info
->buffer
);
301 /* NOTE - We support buffers with only one handle but do not error on
302 * multiple handle case. Reason is that we want to support YUV formats
303 * where we have many logical planes but they all point to the same
304 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
306 int dma_buf
= (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
308 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
310 VkResult result
= anv_device_import_bo(device
, dma_buf
, 0,
311 0 /* client_address */,
315 /* "If the vkAllocateMemory command succeeds, the implementation must
316 * acquire a reference to the imported hardware buffer, which it must
317 * release when the device memory object is freed. If the command fails,
318 * the implementation must not retain a reference."
320 AHardwareBuffer_acquire(info
->buffer
);
321 mem
->ahw
= info
->buffer
;
325 return VK_ERROR_EXTENSION_NOT_PRESENT
;
330 anv_create_ahw_memory(VkDevice device_h
,
331 struct anv_device_memory
*mem
,
332 const VkMemoryAllocateInfo
*pAllocateInfo
)
334 #if ANDROID_API_LEVEL >= 26
335 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
337 const VkMemoryDedicatedAllocateInfo
*dedicated_info
=
338 vk_find_struct_const(pAllocateInfo
->pNext
,
339 MEMORY_DEDICATED_ALLOCATE_INFO
);
347 /* If caller passed dedicated information. */
348 if (dedicated_info
&& dedicated_info
->image
) {
349 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
350 w
= image
->extent
.width
;
351 h
= image
->extent
.height
;
352 layers
= image
->array_size
;
353 format
= android_format_from_vk(image
->vk_format
);
354 usage
= anv_ahw_usage_from_vk_usage(image
->create_flags
, image
->usage
);
355 } else if (dedicated_info
&& dedicated_info
->buffer
) {
356 ANV_FROM_HANDLE(anv_buffer
, buffer
, dedicated_info
->buffer
);
358 format
= AHARDWAREBUFFER_FORMAT_BLOB
;
359 usage
= AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
|
360 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN
;
362 w
= pAllocateInfo
->allocationSize
;
363 format
= AHARDWAREBUFFER_FORMAT_BLOB
;
364 usage
= AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
|
365 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN
;
368 struct AHardwareBuffer
*ahw
= NULL
;
369 struct AHardwareBuffer_Desc desc
= {
377 if (AHardwareBuffer_allocate(&desc
, &ahw
) != 0)
378 return VK_ERROR_OUT_OF_HOST_MEMORY
;
383 return VK_ERROR_EXTENSION_NOT_PRESENT
;
389 anv_image_from_external(
391 const VkImageCreateInfo
*base_info
,
392 const struct VkExternalMemoryImageCreateInfo
*create_info
,
393 const VkAllocationCallbacks
*alloc
,
394 VkImage
*out_image_h
)
396 #if ANDROID_API_LEVEL >= 26
397 ANV_FROM_HANDLE(anv_device
, device
, device_h
);
399 const struct VkExternalFormatANDROID
*ext_info
=
400 vk_find_struct_const(base_info
->pNext
, EXTERNAL_FORMAT_ANDROID
);
402 if (ext_info
&& ext_info
->externalFormat
!= 0) {
403 assert(base_info
->format
== VK_FORMAT_UNDEFINED
);
404 assert(base_info
->imageType
== VK_IMAGE_TYPE_2D
);
405 assert(base_info
->usage
== VK_IMAGE_USAGE_SAMPLED_BIT
);
406 assert(base_info
->tiling
== VK_IMAGE_TILING_OPTIMAL
);
409 struct anv_image_create_info anv_info
= {
410 .vk_info
= base_info
,
411 .isl_extra_usage_flags
= ISL_SURF_USAGE_DISABLE_AUX_BIT
,
412 .external_format
= true,
416 VkResult result
= anv_image_create(device_h
, &anv_info
, alloc
, &image_h
);
417 if (result
!= VK_SUCCESS
)
420 *out_image_h
= image_h
;
424 return VK_ERROR_EXTENSION_NOT_PRESENT
;
430 anv_image_from_gralloc(VkDevice device_h
,
431 const VkImageCreateInfo
*base_info
,
432 const VkNativeBufferANDROID
*gralloc_info
,
433 const VkAllocationCallbacks
*alloc
,
434 VkImage
*out_image_h
)
437 ANV_FROM_HANDLE(anv_device
, device
, device_h
);
438 VkImage image_h
= VK_NULL_HANDLE
;
439 struct anv_image
*image
= NULL
;
440 struct anv_bo
*bo
= NULL
;
443 struct anv_image_create_info anv_info
= {
444 .vk_info
= base_info
,
445 .isl_extra_usage_flags
= ISL_SURF_USAGE_DISABLE_AUX_BIT
,
448 if (gralloc_info
->handle
->numFds
!= 1) {
449 return vk_errorf(device
->instance
, device
,
450 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
451 "VkNativeBufferANDROID::handle::numFds is %d, "
452 "expected 1", gralloc_info
->handle
->numFds
);
455 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
456 * must exceed that of the gralloc handle, and we do not own the gralloc
459 int dma_buf
= gralloc_info
->handle
->data
[0];
461 /* We need to set the WRITE flag on window system buffers so that GEM will
462 * know we're writing to them and synchronize uses on other rings (for
463 * example, if the display server uses the blitter ring).
465 * If this function fails and if the imported bo was resident in the cache,
466 * we should avoid updating the bo's flags. Therefore, we defer updating
467 * the flags until success is certain.
470 result
= anv_device_import_bo(device
, dma_buf
,
471 ANV_BO_ALLOC_IMPLICIT_SYNC
|
472 ANV_BO_ALLOC_IMPLICIT_WRITE
,
473 0 /* client_address */,
475 if (result
!= VK_SUCCESS
) {
476 return vk_errorf(device
->instance
, device
, result
,
477 "failed to import dma-buf from VkNativeBufferANDROID");
480 int i915_tiling
= anv_gem_get_tiling(device
, bo
->gem_handle
);
481 switch (i915_tiling
) {
482 case I915_TILING_NONE
:
483 anv_info
.isl_tiling_flags
= ISL_TILING_LINEAR_BIT
;
486 anv_info
.isl_tiling_flags
= ISL_TILING_X_BIT
;
489 anv_info
.isl_tiling_flags
= ISL_TILING_Y0_BIT
;
492 result
= vk_errorf(device
->instance
, device
,
493 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
494 "DRM_IOCTL_I915_GEM_GET_TILING failed for "
495 "VkNativeBufferANDROID");
498 result
= vk_errorf(device
->instance
, device
,
499 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
500 "DRM_IOCTL_I915_GEM_GET_TILING returned unknown "
501 "tiling %d for VkNativeBufferANDROID", i915_tiling
);
505 enum isl_format format
= anv_get_isl_format(&device
->info
,
507 VK_IMAGE_ASPECT_COLOR_BIT
,
509 assert(format
!= ISL_FORMAT_UNSUPPORTED
);
511 anv_info
.stride
= gralloc_info
->stride
*
512 (isl_format_get_layout(format
)->bpb
/ 8);
514 result
= anv_image_create(device_h
, &anv_info
, alloc
, &image_h
);
515 image
= anv_image_from_handle(image_h
);
516 if (result
!= VK_SUCCESS
)
519 if (bo
->size
< image
->size
) {
520 result
= vk_errorf(device
->instance
, device
,
521 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
522 "dma-buf from VkNativeBufferANDROID is too small for "
523 "VkImage: %"PRIu64
"B < %"PRIu64
"B",
524 bo
->size
, image
->size
);
528 assert(image
->n_planes
== 1);
529 assert(image
->planes
[0].address
.offset
== 0);
531 image
->planes
[0].address
.bo
= bo
;
532 image
->planes
[0].bo_is_owned
= true;
534 /* Don't clobber the out-parameter until success is certain. */
535 *out_image_h
= image_h
;
540 anv_DestroyImage(device_h
, image_h
, alloc
);
543 anv_device_release_bo(device
, bo
);
549 format_supported_with_usage(VkDevice device_h
, VkFormat format
,
550 VkImageUsageFlags imageUsage
)
552 ANV_FROM_HANDLE(anv_device
, device
, device_h
);
553 struct anv_physical_device
*phys_dev
= &device
->instance
->physicalDevice
;
554 VkPhysicalDevice phys_dev_h
= anv_physical_device_to_handle(phys_dev
);
557 const VkPhysicalDeviceImageFormatInfo2 image_format_info
= {
558 .sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2
,
560 .type
= VK_IMAGE_TYPE_2D
,
561 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
565 VkImageFormatProperties2 image_format_props
= {
566 .sType
= VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2
,
569 /* Check that requested format and usage are supported. */
570 result
= anv_GetPhysicalDeviceImageFormatProperties2(phys_dev_h
,
571 &image_format_info
, &image_format_props
);
572 if (result
!= VK_SUCCESS
) {
573 return vk_errorf(device
->instance
, device
, result
,
574 "anv_GetPhysicalDeviceImageFormatProperties2 failed "
575 "inside %s", __func__
);
582 setup_gralloc0_usage(VkFormat format
, VkImageUsageFlags imageUsage
,
585 /* WARNING: Android's libvulkan.so hardcodes the VkImageUsageFlags
586 * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
587 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
589 * TODO(jessehall): I think these are right, but haven't thought hard
590 * about it. Do we need to query the driver for support of any of
593 * Any disagreement between this function and the hardcoded
594 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
595 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
598 if (unmask32(&imageUsage
, VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
599 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
))
600 *grallocUsage
|= GRALLOC_USAGE_HW_RENDER
;
602 if (unmask32(&imageUsage
, VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
603 VK_IMAGE_USAGE_SAMPLED_BIT
|
604 VK_IMAGE_USAGE_STORAGE_BIT
|
605 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
))
606 *grallocUsage
|= GRALLOC_USAGE_HW_TEXTURE
;
608 /* All VkImageUsageFlags not explicitly checked here are unsupported for
609 * gralloc swapchains.
611 if (imageUsage
!= 0) {
612 return vk_errorf(device
->instance
, device
, VK_ERROR_FORMAT_NOT_SUPPORTED
,
613 "unsupported VkImageUsageFlags(0x%x) for gralloc "
614 "swapchain", imageUsage
);
617 /* The below formats support GRALLOC_USAGE_HW_FB (that is, display
618 * scanout). This short list of formats is univserally supported on Intel
619 * but is incomplete. The full set of supported formats is dependent on
620 * kernel and hardware.
622 * FINISHME: Advertise all display-supported formats.
625 case VK_FORMAT_B8G8R8A8_UNORM
:
626 case VK_FORMAT_B5G6R5_UNORM_PACK16
:
627 case VK_FORMAT_R8G8B8A8_UNORM
:
628 case VK_FORMAT_R8G8B8A8_SRGB
:
629 *grallocUsage
|= GRALLOC_USAGE_HW_FB
|
630 GRALLOC_USAGE_HW_COMPOSER
|
631 GRALLOC_USAGE_EXTERNAL_DISP
;
634 intel_logw("%s: unsupported format=%d", __func__
, format
);
637 if (*grallocUsage
== 0)
638 return VK_ERROR_FORMAT_NOT_SUPPORTED
;
644 #if ANDROID_API_LEVEL >= 26
645 VkResult
anv_GetSwapchainGrallocUsage2ANDROID(
648 VkImageUsageFlags imageUsage
,
649 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage
,
650 uint64_t* grallocConsumerUsage
,
651 uint64_t* grallocProducerUsage
)
653 ANV_FROM_HANDLE(anv_device
, device
, device_h
);
656 *grallocConsumerUsage
= 0;
657 *grallocProducerUsage
= 0;
658 intel_logd("%s: format=%d, usage=0x%x", __func__
, format
, imageUsage
);
660 result
= format_supported_with_usage(device_h
, format
, imageUsage
);
661 if (result
!= VK_SUCCESS
)
664 int32_t grallocUsage
= 0;
665 result
= setup_gralloc0_usage(format
, imageUsage
, &grallocUsage
);
666 if (result
!= VK_SUCCESS
)
669 android_convertGralloc0To1Usage(grallocUsage
, grallocProducerUsage
,
670 grallocConsumerUsage
);
676 VkResult
anv_GetSwapchainGrallocUsageANDROID(
679 VkImageUsageFlags imageUsage
,
682 ANV_FROM_HANDLE(anv_device
, device
, device_h
);
683 struct anv_physical_device
*phys_dev
= &device
->instance
->physicalDevice
;
684 VkPhysicalDevice phys_dev_h
= anv_physical_device_to_handle(phys_dev
);
688 intel_logd("%s: format=%d, usage=0x%x", __func__
, format
, imageUsage
);
690 result
= format_supported_with_usage(device_h
, format
, imageUsage
);
691 if (result
!= VK_SUCCESS
)
694 return setup_gralloc0_usage(format
, imageUsage
, grallocUsage
);
698 anv_AcquireImageANDROID(
702 VkSemaphore semaphore_h
,
705 ANV_FROM_HANDLE(anv_device
, device
, device_h
);
706 VkResult result
= VK_SUCCESS
;
708 if (nativeFenceFd
!= -1) {
709 /* As a simple, firstpass implementation of VK_ANDROID_native_buffer, we
710 * block on the nativeFenceFd. This may introduce latency and is
711 * definitiely inefficient, yet it's correct.
713 * FINISHME(chadv): Import the nativeFenceFd into the VkSemaphore and
716 if (sync_wait(nativeFenceFd
, /*timeout*/ -1) < 0) {
717 result
= vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
718 "%s: failed to wait on nativeFenceFd=%d",
719 __func__
, nativeFenceFd
);
722 /* From VK_ANDROID_native_buffer's pseudo spec
723 * (https://source.android.com/devices/graphics/implement-vulkan):
725 * The driver takes ownership of the fence fd and is responsible for
726 * closing it [...] even if vkAcquireImageANDROID fails and returns
729 close(nativeFenceFd
);
731 if (result
!= VK_SUCCESS
)
735 if (semaphore_h
|| fence_h
) {
736 /* Thanks to implicit sync, the image is ready for GPU access. But we
737 * must still put the semaphore into the "submit" state; otherwise the
738 * client may get unexpected behavior if the client later uses it as
741 * Because we blocked above on the nativeFenceFd, the image is also
742 * ready for foreign-device access (including CPU access). But we must
743 * still signal the fence; otherwise the client may get unexpected
744 * behavior if the client later waits on it.
746 * For some values of anv_semaphore_type, we must submit the semaphore
747 * to execbuf in order to signal it. Likewise for anv_fence_type.
748 * Instead of open-coding here the signal operation for each
749 * anv_semaphore_type and anv_fence_type, we piggy-back on
752 const VkSubmitInfo submit
= {
753 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
754 .waitSemaphoreCount
= 0,
755 .commandBufferCount
= 0,
756 .signalSemaphoreCount
= (semaphore_h
? 1 : 0),
757 .pSignalSemaphores
= &semaphore_h
,
760 result
= anv_QueueSubmit(anv_queue_to_handle(&device
->queue
), 1,
762 if (result
!= VK_SUCCESS
) {
763 return vk_errorf(device
->instance
, device
, result
,
764 "anv_QueueSubmit failed inside %s", __func__
);
772 anv_QueueSignalReleaseImageANDROID(
774 uint32_t waitSemaphoreCount
,
775 const VkSemaphore
* pWaitSemaphores
,
781 if (waitSemaphoreCount
== 0)
784 result
= anv_QueueSubmit(queue
, 1,
786 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
787 .waitSemaphoreCount
= 1,
788 .pWaitSemaphores
= pWaitSemaphores
,
790 (VkFence
) VK_NULL_HANDLE
);
791 if (result
!= VK_SUCCESS
)
795 if (pNativeFenceFd
) {
796 /* We can rely implicit on sync because above we submitted all
797 * semaphores to the queue.
799 *pNativeFenceFd
= -1;