diff --git a/src/broadcom/vulkan/v3dv_android.c b/src/broadcom/vulkan/v3dv_android.c index afb691e55d04a..8574823c7d537 100644 --- a/src/broadcom/vulkan/v3dv_android.c +++ b/src/broadcom/vulkan/v3dv_android.c @@ -22,17 +22,11 @@ */ #include "v3dv_private.h" -#include -#if ANDROID_API_LEVEL >= 26 -#include -#endif - -#include "drm-uapi/drm_fourcc.h" +#include #include #include -#include #include #include "vk_android.h" @@ -41,6 +35,7 @@ #include "util/libsync.h" #include "util/log.h" #include "util/os_file.h" +#include "util/u_gralloc/u_gralloc.h" static int v3dv_hal_open(const struct hw_module_t *mod, @@ -67,15 +62,6 @@ PUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = { }, }; -/* If any bits in test_mask are set, then unset them and return true. */ -static inline bool -unmask32(uint32_t *inout_mask, uint32_t test_mask) -{ - uint32_t orig_mask = *inout_mask; - *inout_mask &= ~test_mask; - return *inout_mask != orig_mask; -} - static int v3dv_hal_open(const struct hw_module_t *mod, const char *id, @@ -114,431 +100,3 @@ v3dv_hal_close(struct hw_device_t *dev) /* hwvulkan.h claims that hw_device_t::close() is never called. */ return -1; } - -VkResult -v3dv_gralloc_to_drm_explicit_layout(struct u_gralloc *gralloc, - struct u_gralloc_buffer_handle *in_hnd, - VkImageDrmFormatModifierExplicitCreateInfoEXT *out, - VkSubresourceLayout *out_layouts, - int max_planes) -{ - struct u_gralloc_buffer_basic_info info; - - if (u_gralloc_get_buffer_basic_info(gralloc, in_hnd, &info) != 0) - return VK_ERROR_INVALID_EXTERNAL_HANDLE; - - if (info.num_planes > max_planes) - return VK_ERROR_INVALID_EXTERNAL_HANDLE; - - bool is_disjoint = false; - for (int i = 1; i < info.num_planes; i++) { - if (info.offsets[i] == 0) { - is_disjoint = true; - break; - } - } - - if (is_disjoint) { - /* We don't support disjoint planes yet */ - return VK_ERROR_INVALID_EXTERNAL_HANDLE; - } - - memset(out_layouts, 0, sizeof(*out_layouts) * info.num_planes); - memset(out, 0, sizeof(*out)); - - out->sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT; - out->pPlaneLayouts = out_layouts; - - out->drmFormatModifier = info.modifier; - out->drmFormatModifierPlaneCount = info.num_planes; - for (int i = 0; i < info.num_planes; i++) { - out_layouts[i].offset = info.offsets[i]; - out_layouts[i].rowPitch = info.strides[i]; - } - - if (info.drm_fourcc == DRM_FORMAT_YVU420) { - /* Swap the U and V planes to match the VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM */ - VkSubresourceLayout tmp = out_layouts[1]; - out_layouts[1] = out_layouts[2]; - out_layouts[2] = tmp; - } - - return VK_SUCCESS; -} - -VkResult -v3dv_import_native_buffer_fd(VkDevice device_h, - int native_buffer_fd, - const VkAllocationCallbacks *alloc, - VkImage image_h) -{ - VkResult result; - - VkDeviceMemory memory_h; - - const VkMemoryDedicatedAllocateInfo ded_alloc = { - .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, - .pNext = NULL, - .buffer = VK_NULL_HANDLE, - .image = image_h - }; - - const VkImportMemoryFdInfoKHR import_info = { - .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR, - .pNext = &ded_alloc, - .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT, - .fd = os_dupfd_cloexec(native_buffer_fd), - }; - - result = - v3dv_AllocateMemory(device_h, - &(VkMemoryAllocateInfo) { - .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, - .pNext = &import_info, - .allocationSize = lseek(native_buffer_fd, 0, SEEK_END), - .memoryTypeIndex = 0, - }, - alloc, &memory_h); - - if (result != VK_SUCCESS) - goto fail_create_image; - - VkBindImageMemoryInfo bind_info = { - .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO, - .image = image_h, - .memory = memory_h, - .memoryOffset = 0, - }; - v3dv_BindImageMemory2(device_h, 1, &bind_info); - - return VK_SUCCESS; - -fail_create_image: - close(import_info.fd); - - return result; -} - -static VkResult -format_supported_with_usage(VkDevice device_h, - VkFormat format, - VkImageUsageFlags imageUsage) -{ - V3DV_FROM_HANDLE(v3dv_device, device, device_h); - struct v3dv_physical_device *phys_dev = device->pdevice; - VkPhysicalDevice phys_dev_h = v3dv_physical_device_to_handle(phys_dev); - VkResult result; - - const VkPhysicalDeviceImageFormatInfo2 image_format_info = { - .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2, - .format = format, - .type = VK_IMAGE_TYPE_2D, - .tiling = VK_IMAGE_TILING_OPTIMAL, - .usage = imageUsage, - }; - - VkImageFormatProperties2 image_format_props = { - .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2, - }; - - /* Check that requested format and usage are supported. */ - result = v3dv_GetPhysicalDeviceImageFormatProperties2( - phys_dev_h, &image_format_info, &image_format_props); - if (result != VK_SUCCESS) { - return vk_errorf(device, result, - "v3dv_GetPhysicalDeviceImageFormatProperties2 failed " - "inside %s", - __func__); - } - - return VK_SUCCESS; -} - -static VkResult -setup_gralloc0_usage(struct v3dv_device *device, - VkFormat format, - VkImageUsageFlags imageUsage, - int *grallocUsage) -{ - if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT | - VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) - *grallocUsage |= GRALLOC_USAGE_HW_RENDER; - - if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | - VK_IMAGE_USAGE_SAMPLED_BIT | - VK_IMAGE_USAGE_STORAGE_BIT | - VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) - *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE; - - /* All VkImageUsageFlags not explicitly checked here are unsupported for - * gralloc swapchains. - */ - if (imageUsage != 0) { - return vk_errorf(device, VK_ERROR_FORMAT_NOT_SUPPORTED, - "unsupported VkImageUsageFlags(0x%x) for gralloc " - "swapchain", - imageUsage); - } - - /* Swapchain assumes direct displaying, therefore enable COMPOSER flag, - * In case format is not supported by display controller, gralloc will - * drop this flag and still allocate the buffer in VRAM - */ - *grallocUsage |= GRALLOC_USAGE_HW_COMPOSER; - - if (*grallocUsage == 0) - return VK_ERROR_FORMAT_NOT_SUPPORTED; - - return VK_SUCCESS; -} - -VKAPI_ATTR VkResult VKAPI_CALL -v3dv_GetSwapchainGrallocUsageANDROID(VkDevice device_h, - VkFormat format, - VkImageUsageFlags imageUsage, - int *grallocUsage) -{ - V3DV_FROM_HANDLE(v3dv_device, device, device_h); - VkResult result; - - result = format_supported_with_usage(device_h, format, imageUsage); - if (result != VK_SUCCESS) - return result; - - *grallocUsage = 0; - return setup_gralloc0_usage(device, format, imageUsage, grallocUsage); -} - -#if ANDROID_API_LEVEL >= 26 -VKAPI_ATTR VkResult VKAPI_CALL -v3dv_GetSwapchainGrallocUsage2ANDROID( - VkDevice device_h, - VkFormat format, - VkImageUsageFlags imageUsage, - VkSwapchainImageUsageFlagsANDROID swapchainImageUsage, - uint64_t *grallocConsumerUsage, - uint64_t *grallocProducerUsage) -{ - V3DV_FROM_HANDLE(v3dv_device, device, device_h); - VkResult result; - - *grallocConsumerUsage = 0; - *grallocProducerUsage = 0; - mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage); - - result = format_supported_with_usage(device_h, format, imageUsage); - if (result != VK_SUCCESS) - return result; - - int32_t grallocUsage = 0; - result = setup_gralloc0_usage(device, format, imageUsage, &grallocUsage); - if (result != VK_SUCCESS) - return result; - - /* Setup gralloc1 usage flags from gralloc0 flags. */ - - if (grallocUsage & GRALLOC_USAGE_HW_RENDER) { - *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET; - } - - if (grallocUsage & GRALLOC_USAGE_HW_TEXTURE) { - *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE; - } - - if (grallocUsage & GRALLOC_USAGE_HW_COMPOSER) { - /* GPU composing case */ - *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE; - /* Hardware composing case */ - *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER; - } - - if (swapchainImageUsage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) { - uint64_t front_rendering_usage = 0; - u_gralloc_get_front_rendering_usage(device->gralloc, &front_rendering_usage); - *grallocProducerUsage |= front_rendering_usage; - } - - return VK_SUCCESS; -} -#endif - -/* ----------------------------- AHardwareBuffer --------------------------- */ - -static VkResult -get_ahb_buffer_format_properties2(VkDevice device_h, const struct AHardwareBuffer *buffer, - VkAndroidHardwareBufferFormatProperties2ANDROID *pProperties) -{ - V3DV_FROM_HANDLE(v3dv_device, device, device_h); - - /* Get a description of buffer contents . */ - AHardwareBuffer_Desc desc; - AHardwareBuffer_describe(buffer, &desc); - - /* Verify description. */ - const uint64_t gpu_usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | - AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT | - AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER; - - /* "Buffer must be a valid Android hardware buffer object with at least - * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags." - */ - if (!(desc.usage & (gpu_usage))) - return VK_ERROR_INVALID_EXTERNAL_HANDLE; - - /* Fill properties fields based on description. */ - VkAndroidHardwareBufferFormatProperties2ANDROID *p = pProperties; - - p->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY; - p->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY; - p->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY; - p->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY; - - p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601; - p->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; - - p->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT; - p->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT; - - VkFormatProperties2 format_properties = {.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2}; - - p->format = vk_ahb_format_to_image_format(desc.format); - - VkFormat external_format = p->format; - - if (p->format != VK_FORMAT_UNDEFINED) - goto finish; - - /* External format only case - * - * From vkGetAndroidHardwareBufferPropertiesANDROID spec: - * "If the Android hardware buffer has one of the formats listed in the Format - * Equivalence table (see spec.), then format must have the equivalent Vulkan - * format listed in the table. Otherwise, format may be VK_FORMAT_UNDEFINED, - * indicating the Android hardware buffer can only be used with an external format." - * - * From SKIA source code analysis: p->format MUST be VK_FORMAT_UNDEFINED, if the - * format is not in the Equivalence table. - */ - - struct u_gralloc_buffer_handle gr_handle = { - .handle = AHardwareBuffer_getNativeHandle(buffer), - .pixel_stride = desc.stride, - .hal_format = desc.format, - }; - - struct u_gralloc_buffer_basic_info info; - - if (u_gralloc_get_buffer_basic_info(device->gralloc, &gr_handle, &info) != 0) - return VK_ERROR_INVALID_EXTERNAL_HANDLE; - - switch (info.drm_fourcc) { - case DRM_FORMAT_YVU420: - /* Assuming that U and V planes are swapped earlier */ - external_format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM; - break; - case DRM_FORMAT_NV12: - external_format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM; - break; - default:; - mesa_loge("Unsupported external DRM format: %d", info.drm_fourcc); - return VK_ERROR_INVALID_EXTERNAL_HANDLE; - } - - struct u_gralloc_buffer_color_info color_info; - if (u_gralloc_get_buffer_color_info(device->gralloc, &gr_handle, &color_info) == 0) { - switch (color_info.yuv_color_space) { - case __DRI_YUV_COLOR_SPACE_ITU_REC601: - p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601; - break; - case __DRI_YUV_COLOR_SPACE_ITU_REC709: - p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709; - break; - case __DRI_YUV_COLOR_SPACE_ITU_REC2020: - p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020; - break; - default: - break; - } - - p->suggestedYcbcrRange = (color_info.sample_range == __DRI_YUV_NARROW_RANGE) ? - VK_SAMPLER_YCBCR_RANGE_ITU_NARROW : VK_SAMPLER_YCBCR_RANGE_ITU_FULL; - p->suggestedXChromaOffset = (color_info.horizontal_siting == __DRI_YUV_CHROMA_SITING_0_5) ? - VK_CHROMA_LOCATION_MIDPOINT : VK_CHROMA_LOCATION_COSITED_EVEN; - p->suggestedYChromaOffset = (color_info.vertical_siting == __DRI_YUV_CHROMA_SITING_0_5) ? - VK_CHROMA_LOCATION_MIDPOINT : VK_CHROMA_LOCATION_COSITED_EVEN; - } - -finish: - - v3dv_GetPhysicalDeviceFormatProperties2(v3dv_physical_device_to_handle(device->pdevice), - external_format, &format_properties); - - /* v3dv doesn't support direct sampling from linear images but has a logic to copy - * from linear to tiled images implicitly before sampling. Therefore expose optimal - * features for both linear and optimal tiling. - */ - p->formatFeatures = format_properties.formatProperties.optimalTilingFeatures; - p->externalFormat = external_format; - - /* From vkGetAndroidHardwareBufferPropertiesANDROID spec: - * "The formatFeatures member *must* include - * VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT and at least one of - * VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT or - * VK_FORMAT_FEATURE_2_COSITED_CHROMA_SAMPLES_BIT" - */ - p->formatFeatures |= VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT; - - return VK_SUCCESS; -} - -VkResult -v3dv_GetAndroidHardwareBufferPropertiesANDROID(VkDevice device_h, - const struct AHardwareBuffer *buffer, - VkAndroidHardwareBufferPropertiesANDROID *pProperties) -{ - V3DV_FROM_HANDLE(v3dv_device, dev, device_h); - struct v3dv_physical_device *pdevice = dev->pdevice; - - VkResult result; - - VkAndroidHardwareBufferFormatPropertiesANDROID *format_prop = - vk_find_struct(pProperties->pNext, ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID); - - /* Fill format properties of an Android hardware buffer. */ - if (format_prop) { - VkAndroidHardwareBufferFormatProperties2ANDROID format_prop2 = { - .sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_2_ANDROID, - }; - result = get_ahb_buffer_format_properties2(device_h, buffer, &format_prop2); - if (result != VK_SUCCESS) - return result; - - format_prop->format = format_prop2.format; - format_prop->externalFormat = format_prop2.externalFormat; - format_prop->formatFeatures = - vk_format_features2_to_features(format_prop2.formatFeatures); - format_prop->samplerYcbcrConversionComponents = - format_prop2.samplerYcbcrConversionComponents; - format_prop->suggestedYcbcrModel = format_prop2.suggestedYcbcrModel; - format_prop->suggestedYcbcrRange = format_prop2.suggestedYcbcrRange; - format_prop->suggestedXChromaOffset = format_prop2.suggestedXChromaOffset; - format_prop->suggestedYChromaOffset = format_prop2.suggestedYChromaOffset; - } - - VkAndroidHardwareBufferFormatProperties2ANDROID *format_prop2 = - vk_find_struct(pProperties->pNext, ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_2_ANDROID); - if (format_prop2) { - result = get_ahb_buffer_format_properties2(device_h, buffer, format_prop2); - if (result != VK_SUCCESS) - return result; - } - - const native_handle_t *handle = AHardwareBuffer_getNativeHandle(buffer); - assert(handle && handle->numFds > 0); - pProperties->allocationSize = lseek(handle->data[0], 0, SEEK_END); - - /* All memory types. */ - pProperties->memoryTypeBits = (1u << pdevice->memory.memoryTypeCount) - 1; - - return VK_SUCCESS; -} diff --git a/src/broadcom/vulkan/v3dv_device.c b/src/broadcom/vulkan/v3dv_device.c index 1873a6af4c2c2..f4abc7dae354e 100644 --- a/src/broadcom/vulkan/v3dv_device.c +++ b/src/broadcom/vulkan/v3dv_device.c @@ -44,6 +44,7 @@ #include "compiler/v3d_compiler.h" #include "drm-uapi/v3d_drm.h" +#include "vk_android.h" #include "vk_drm_syncobj.h" #include "vk_util.h" #include "git_sha1.h" @@ -55,6 +56,8 @@ #if DETECT_OS_ANDROID #include "vk_android.h" +#include +#include "util/u_gralloc/u_gralloc.h" #endif #ifdef VK_USE_PLATFORM_XCB_KHR @@ -228,12 +231,14 @@ get_device_extensions(const struct v3dv_physical_device *device, .EXT_texel_buffer_alignment = true, .EXT_tooling_info = true, .EXT_vertex_attribute_divisor = true, -#if DETECT_OS_ANDROID - .ANDROID_external_memory_android_hardware_buffer = true, - .ANDROID_native_buffer = true, - .EXT_queue_family_foreign = true, -#endif }; +#if DETECT_OS_ANDROID + if (vk_android_get_ugralloc() != NULL) { + ext->ANDROID_external_memory_android_hardware_buffer = true; + ext->ANDROID_native_buffer = true; + ext->EXT_queue_family_foreign = true; + } +#endif } static void @@ -585,6 +590,16 @@ v3dv_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false)); +#if DETECT_OS_ANDROID + struct u_gralloc *u_gralloc = vk_android_init_ugralloc(); + + if (u_gralloc && u_gralloc_get_type(u_gralloc) == U_GRALLOC_TYPE_FALLBACK) { + mesa_logw( + "v3dv: Gralloc is not supported. Android extensions are disabled."); + vk_android_destroy_ugralloc(); + } +#endif + *pInstance = v3dv_instance_to_handle(instance); return VK_SUCCESS; @@ -640,6 +655,10 @@ v3dv_DestroyInstance(VkInstance _instance, if (!instance) return; +#if DETECT_OS_ANDROID + vk_android_destroy_ugralloc(); +#endif + VG(VALGRIND_DESTROY_MEMPOOL(instance)); vk_instance_finish(&instance->vk); @@ -1748,11 +1767,6 @@ v3dv_CreateDevice(VkPhysicalDevice physicalDevice, return vk_error(NULL, result); } -#if DETECT_OS_ANDROID - device->gralloc = u_gralloc_create(U_GRALLOC_TYPE_AUTO); - assert(device->gralloc); -#endif - device->instance = instance; device->pdevice = physical_device; @@ -1819,9 +1833,6 @@ fail: v3dv_event_free_resources(device); v3dv_query_free_resources(device); vk_device_finish(&device->vk); -#if DETECT_OS_ANDROID - u_gralloc_destroy(&device->gralloc); -#endif vk_free(&device->vk.alloc, device); return result; @@ -1860,9 +1871,6 @@ v3dv_DestroyDevice(VkDevice _device, mtx_destroy(&device->query_mutex); vk_device_finish(&device->vk); -#if DETECT_OS_ANDROID - u_gralloc_destroy(&device->gralloc); -#endif vk_free2(&device->vk.alloc, pAllocator, device); } @@ -2424,38 +2432,26 @@ v3dv_BindImageMemory2(VkDevice _device, const VkBindImageMemoryInfo *pBindInfos) { for (uint32_t i = 0; i < bindInfoCount; i++) { -#if DETECT_OS_ANDROID - V3DV_FROM_HANDLE(v3dv_device_memory, mem, pBindInfos[i].memory); - V3DV_FROM_HANDLE(v3dv_device, device, _device); - if (mem != NULL && mem->vk.ahardware_buffer) { - AHardwareBuffer_Desc description; - const native_handle_t *handle = AHardwareBuffer_getNativeHandle(mem->vk.ahardware_buffer); + /* This section is removed by the optimizer for non-ANDROID builds */ + V3DV_FROM_HANDLE(v3dv_image, image, pBindInfos[i].image); + if (vk_image_is_android_hardware_buffer(&image->vk)) { + V3DV_FROM_HANDLE(v3dv_device, device, _device); + V3DV_FROM_HANDLE(v3dv_device_memory, mem, pBindInfos[i].memory); - V3DV_FROM_HANDLE(v3dv_image, image, pBindInfos[i].image); - AHardwareBuffer_describe(mem->vk.ahardware_buffer, &description); - - struct u_gralloc_buffer_handle gr_handle = { - .handle = handle, - .pixel_stride = description.stride, - .hal_format = description.format, - }; - - VkResult result = v3dv_gralloc_to_drm_explicit_layout( - device->gralloc, - &gr_handle, - image->android_explicit_layout, - image->android_plane_layouts, - V3DV_MAX_PLANE_COUNT); + VkImageDrmFormatModifierExplicitCreateInfoEXT eci; + VkSubresourceLayout a_plane_layouts[V3DV_MAX_PLANE_COUNT]; + VkResult result = vk_android_get_ahb_layout(mem->vk.ahardware_buffer, + &eci, a_plane_layouts, + V3DV_MAX_PLANE_COUNT); if (result != VK_SUCCESS) return result; - result = v3dv_update_image_layout( - device, image, image->android_explicit_layout->drmFormatModifier, - /* disjoint = */ false, image->android_explicit_layout); + result = v3dv_update_image_layout(device, image, + eci.drmFormatModifier, + /* disjoint = */ false, &eci); if (result != VK_SUCCESS) return result; } -#endif const VkBindImageMemorySwapchainInfoKHR *swapchain_info = vk_find_struct_const(pBindInfos->pNext, diff --git a/src/broadcom/vulkan/v3dv_image.c b/src/broadcom/vulkan/v3dv_image.c index 358c03c555f23..8a0b71a49d18a 100644 --- a/src/broadcom/vulkan/v3dv_image.c +++ b/src/broadcom/vulkan/v3dv_image.c @@ -28,9 +28,7 @@ #include "util/u_math.h" #include "vk_util.h" #include "vulkan/wsi/wsi_common.h" -#if DETECT_OS_ANDROID #include "vk_android.h" -#endif /** * Computes the HW's UIFblock padding for a given height/cpp. @@ -413,13 +411,20 @@ v3dv_image_init(struct v3dv_device *device, uint64_t modifier = DRM_FORMAT_MOD_INVALID; const VkImageDrmFormatModifierListCreateInfoEXT *mod_info = NULL; const VkImageDrmFormatModifierExplicitCreateInfoEXT *explicit_mod_info = NULL; -#if DETECT_OS_ANDROID - if (image->is_native_buffer_memory) { - assert(image->android_explicit_layout); - explicit_mod_info = image->android_explicit_layout; - modifier = explicit_mod_info->drmFormatModifier; + + /* This section is removed by the optimizer for non-ANDROID builds */ + VkImageDrmFormatModifierExplicitCreateInfoEXT eci; + VkSubresourceLayout a_plane_layouts[V3DV_MAX_PLANE_COUNT]; + if (vk_image_is_android_native_buffer(&image->vk)) { + VkResult result = vk_android_get_anb_layout( + pCreateInfo, &eci, a_plane_layouts, V3DV_MAX_PLANE_COUNT); + if (result != VK_SUCCESS) + return result; + + explicit_mod_info = &eci; + modifier = eci.drmFormatModifier; } -#endif + if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) { mod_info = vk_find_struct_const(pCreateInfo->pNext, @@ -494,13 +499,12 @@ v3dv_image_init(struct v3dv_device *device, */ image->vk.create_flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; -#if DETECT_OS_ANDROID /* At this time, an AHB handle is not yet provided. * Image layout will be filled up during vkBindImageMemory2 + * This section is removed by the optimizer for non-ANDROID builds */ - if (image->is_ahb) + if (vk_image_is_android_hardware_buffer(&image->vk)) return VK_SUCCESS; -#endif bool disjoint = image->vk.create_flags & VK_IMAGE_CREATE_DISJOINT_BIT; @@ -521,83 +525,23 @@ create_image(struct v3dv_device *device, if (image == NULL) return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); -#if DETECT_OS_ANDROID - const VkExternalMemoryImageCreateInfo *external_info = - vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_MEMORY_IMAGE_CREATE_INFO); - - const VkNativeBufferANDROID *native_buffer = - vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID); - - if (native_buffer != NULL) - image->is_native_buffer_memory = true; - - image->is_ahb = external_info && (external_info->handleTypes & - VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID); - - assert(!(image->is_ahb && image->is_native_buffer_memory)); - - if (image->is_ahb || image->is_native_buffer_memory) { - image->android_explicit_layout = vk_alloc2(&device->vk.alloc, pAllocator, - sizeof(VkImageDrmFormatModifierExplicitCreateInfoEXT), - 8, - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - if (!image->android_explicit_layout) { - result = vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); - goto fail; - } - - image->android_plane_layouts = vk_alloc2(&device->vk.alloc, pAllocator, - sizeof(VkSubresourceLayout) * V3DV_MAX_PLANE_COUNT, - 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - if (!image->android_plane_layouts) { - result = vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); - goto fail; - } - } - - if (image->is_native_buffer_memory) { - struct u_gralloc_buffer_handle gr_handle = { - .handle = native_buffer->handle, - .hal_format = native_buffer->format, - .pixel_stride = native_buffer->stride, - }; - - result = v3dv_gralloc_to_drm_explicit_layout(device->gralloc, - &gr_handle, - image->android_explicit_layout, - image->android_plane_layouts, - V3DV_MAX_PLANE_COUNT); - if (result != VK_SUCCESS) - goto fail; - } -#endif - result = v3dv_image_init(device, pCreateInfo, pAllocator, image); if (result != VK_SUCCESS) goto fail; -#if DETECT_OS_ANDROID - if (image->is_native_buffer_memory) { - result = v3dv_import_native_buffer_fd(v3dv_device_to_handle(device), - native_buffer->handle->data[0], pAllocator, - v3dv_image_to_handle(image)); + /* This section is removed by the optimizer for non-ANDROID builds */ + if (vk_image_is_android_native_buffer(&image->vk)) { + result = vk_android_import_anb(&device->vk, pCreateInfo, pAllocator, + &image->vk); if (result != VK_SUCCESS) goto fail; } -#endif *pImage = v3dv_image_to_handle(image); return VK_SUCCESS; fail: -#if DETECT_OS_ANDROID - if (image->android_explicit_layout) - vk_free2(&device->vk.alloc, pAllocator, image->android_explicit_layout); - if (image->android_plane_layouts) - vk_free2(&device->vk.alloc, pAllocator, image->android_plane_layouts); -#endif - vk_image_destroy(&device->vk, pAllocator, &image->vk); return result; } @@ -752,18 +696,6 @@ v3dv_DestroyImage(VkDevice _device, image->shadow = NULL; } -#if DETECT_OS_ANDROID - if (image->is_native_buffer_memory) - v3dv_FreeMemory(_device, - v3dv_device_memory_to_handle(image->planes[0].mem), - pAllocator); - - if (image->android_explicit_layout) - vk_free2(&device->vk.alloc, pAllocator, image->android_explicit_layout); - if (image->android_plane_layouts) - vk_free2(&device->vk.alloc, pAllocator, image->android_plane_layouts); -#endif - vk_image_destroy(&device->vk, pAllocator, &image->vk); } diff --git a/src/broadcom/vulkan/v3dv_private.h b/src/broadcom/vulkan/v3dv_private.h index 7d1078f76a291..c56ce24777b35 100644 --- a/src/broadcom/vulkan/v3dv_private.h +++ b/src/broadcom/vulkan/v3dv_private.h @@ -67,11 +67,6 @@ #include "util/detect_os.h" -#if DETECT_OS_ANDROID -#include -#include "util/u_gralloc/u_gralloc.h" -#endif - #include "v3dv_limits.h" #include "common/v3d_device_info.h" @@ -599,10 +594,6 @@ struct v3dv_device { void *device_address_mem_ctx; struct util_dynarray device_address_bo_list; /* Array of struct v3dv_bo * */ - -#if DETECT_OS_ANDROID - struct u_gralloc *gralloc; -#endif }; struct v3dv_device_memory { @@ -740,15 +731,6 @@ struct v3dv_image { * This holds a tiled copy of the image we can use for that purpose. */ struct v3dv_image *shadow; - -#if DETECT_OS_ANDROID - /* Image is backed by VK_ANDROID_native_buffer, */ - bool is_native_buffer_memory; - /* Image is backed by VK_ANDROID_external_memory_android_hardware_buffer */ - bool is_ahb; - VkImageDrmFormatModifierExplicitCreateInfoEXT *android_explicit_layout; - VkSubresourceLayout *android_plane_layouts; -#endif }; VkResult @@ -2697,19 +2679,4 @@ v3dv_compute_ez_state(struct vk_dynamic_graphics_state *dyn, uint32_t v3dv_pipeline_primitive(VkPrimitiveTopology vk_prim); -#if DETECT_OS_ANDROID -VkResult -v3dv_gralloc_to_drm_explicit_layout(struct u_gralloc *gralloc, - struct u_gralloc_buffer_handle *in_hnd, - VkImageDrmFormatModifierExplicitCreateInfoEXT *out, - VkSubresourceLayout *out_layouts, - int max_planes); - -VkResult -v3dv_import_native_buffer_fd(VkDevice device_h, - int dma_buf, - const VkAllocationCallbacks *alloc, - VkImage image_h); -#endif /* DETECT_OS_ANDROID */ - #endif /* V3DV_PRIVATE_H */