<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Fri, Aug 24, 2018 at 12:12 AM Tapani Pälli <<a href="mailto:tapani.palli@intel.com">tapani.palli@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
On 08/22/2018 05:28 PM, Jason Ekstrand wrote:<br>
> On Tue, Aug 21, 2018 at 3:27 AM Tapani Pälli <<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a> <br>
> <mailto:<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a>>> wrote:<br>
> <br>
> v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)<br>
> v3: properly handle usage bits when creating from image<br>
> <br>
> Signed-off-by: Tapani Pälli <<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a><br>
> <mailto:<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a>>><br>
> ---<br>
> src/intel/vulkan/anv_android.c | 149<br>
> +++++++++++++++++++++++++++++++++++++++++<br>
> src/intel/vulkan/anv_device.c | 46 ++++++++++++-<br>
> src/intel/vulkan/anv_private.h | 18 +++++<br>
> 3 files changed, 212 insertions(+), 1 deletion(-)<br>
> <br>
> diff --git a/src/intel/vulkan/anv_android.c<br>
> b/src/intel/vulkan/anv_android.c<br>
> index 7d0eb588e2b..6f90649847d 100644<br>
> --- a/src/intel/vulkan/anv_android.c<br>
> +++ b/src/intel/vulkan/anv_android.c<br>
> @@ -195,6 +195,155 @@ anv_GetAndroidHardwareBufferPropertiesANDROID(<br>
> return VK_SUCCESS;<br>
> }<br>
> <br>
> +VkResult<br>
> +anv_GetMemoryAndroidHardwareBufferANDROID(<br>
> + VkDevice device_h,<br>
> + const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,<br>
> + struct AHardwareBuffer **pBuffer)<br>
> +{<br>
> + ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);<br>
> +<br>
> + /* Some quotes from Vulkan spec:<br>
> + *<br>
> + * "If the device memory was created by importing an Android<br>
> hardware<br>
> + * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return<br>
> that same<br>
> + * Android hardware buffer object."<br>
> + *<br>
> + *<br>
> "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must<br>
> + * have been included in<br>
> VkExportMemoryAllocateInfoKHR::handleTypes when<br>
> + * memory was created."<br>
> + */<br>
> + if (mem->ahw) {<br>
> + *pBuffer = mem->ahw;<br>
> + /* Increase refcount. */<br>
> + AHardwareBuffer_acquire(mem->ahw);<br>
> + return VK_SUCCESS;<br>
> + }<br>
> +<br>
> + return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;<br>
> +}<br>
> +<br>
> +/*<br>
> + * Called from anv_AllocateMemory when import AHardwareBuffer.<br>
> + */<br>
> +VkResult<br>
> +anv_import_ahw_memory(VkDevice device_h,<br>
> + struct anv_device_memory *mem,<br>
> + const<br>
> VkImportAndroidHardwareBufferInfoANDROID *info)<br>
> +{<br>
> + ANV_FROM_HANDLE(anv_device, device, device_h);<br>
> +<br>
> + /* Get a description of buffer contents. */<br>
> + AHardwareBuffer_Desc desc;<br>
> + AHardwareBuffer_describe(info->buffer, &desc);<br>
> + VkResult result = VK_SUCCESS;<br>
> +<br>
> + /* Import from AHardwareBuffer to anv_device_memory. */<br>
> + const native_handle_t *handle =<br>
> + AHardwareBuffer_getNativeHandle(info->buffer);<br>
> +<br>
> + int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;<br>
> + if (dma_buf < 0)<br>
> + return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;<br>
> +<br>
> + uint64_t bo_flags = 0;<br>
> + if (device->instance->physicalDevice.supports_48bit_addresses)<br>
> + bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;<br>
> + if (device->instance->physicalDevice.use_softpin)<br>
> + bo_flags |= EXEC_OBJECT_PINNED;<br>
> +<br>
> + result = anv_bo_cache_import(device, &device->bo_cache,<br>
> + dma_buf, bo_flags, &mem->bo);<br>
> + if (result != VK_SUCCESS)<br>
> + return result;<br>
> +<br>
> + /* "If the vkAllocateMemory command succeeds, the implementation<br>
> must<br>
> + * acquire a reference to the imported hardware buffer, which it<br>
> must<br>
> + * release when the device memory object is freed. If the<br>
> command fails,<br>
> + * the implementation must not retain a reference."<br>
> + */<br>
> + AHardwareBuffer_acquire(info->buffer);<br>
> + mem->ahw = info->buffer;<br>
> +<br>
> + return result;<br>
> +}<br>
> +<br>
> +VkResult<br>
> +anv_create_ahw_memory(VkDevice device_h,<br>
> + struct anv_device_memory *mem,<br>
> + const VkMemoryAllocateInfo *pAllocateInfo)<br>
> +{<br>
> + ANV_FROM_HANDLE(anv_device, dev, device_h);<br>
> +<br>
> + const VkMemoryDedicatedAllocateInfo *dedicated_info =<br>
> + vk_find_struct_const(pAllocateInfo->pNext,<br>
> + MEMORY_DEDICATED_ALLOCATE_INFO);<br>
> +<br>
> + uint32_t w = 0;<br>
> + uint32_t h = 1;<br>
> + uint32_t format = 0;<br>
> + uint64_t usage = 0;<br>
> +<br>
> + /* If caller passed dedicated information. */<br>
> + if (dedicated_info && dedicated_info->image) {<br>
> + ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);<br>
> + w = image->extent.width;<br>
> + h = image->extent.height;<br>
> + format = android_format_from_vk(image->vk_format);<br>
> +<br>
> + /* Construct usage mask from image usage bits, see<br>
> + * 'AHardwareBuffer Usage Equivalence' in spec.<br>
> + */<br>
> + if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT)<br>
> + usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;<br>
> +<br>
> + if (image->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)<br>
> + usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;<br>
> +<br>
> + if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)<br>
> + usage |= AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;<br>
> +<br>
> + if (image->create_flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)<br>
> + usage |= AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP;<br>
> +<br>
> + if (image->create_flags & VK_IMAGE_CREATE_PROTECTED_BIT)<br>
> + usage |= AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT;<br>
> +<br>
> + /* No usage bits set, set at least one GPU usage. */<br>
> + if (usage == 0)<br>
> + usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;<br>
> +<br>
> + } else if (dedicated_info && dedicated_info->buffer) {<br>
> + ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);<br>
> + w = buffer->size;<br>
> + format = AHARDWAREBUFFER_FORMAT_BLOB;<br>
> + usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |<br>
> + AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;<br>
> + } else {<br>
> + w = pAllocateInfo->allocationSize;<br>
> + format = AHARDWAREBUFFER_FORMAT_BLOB;<br>
> + usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |<br>
> + AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;<br>
> + }<br>
> +<br>
> + struct AHardwareBuffer *ahw = NULL;<br>
> + struct AHardwareBuffer_Desc desc = {<br>
> + .width = w,<br>
> + .height = h,<br>
> + .layers = 1,<br>
> + .format = format,<br>
> + .usage = usage,<br>
> + .stride = 0,<br>
> <br>
> <br>
> If the image is linear (not sure if that's possible), we could give them <br>
> a valid stride.<br>
<br>
It seems allocator will always anyway calculate the stride so I could <br>
just drop setting it here.<br></blockquote><div><br></div><div>Fine by me.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> + };<br>
> +<br>
> + if (AHardwareBuffer_allocate(&desc, &ahw) != 0)<br>
> + return VK_ERROR_OUT_OF_HOST_MEMORY;<br>
> +<br>
> + mem->ahw = ahw;<br>
> +<br>
> + return VK_SUCCESS;<br>
> +}<br>
> +<br>
> VkResult<br>
> anv_image_from_gralloc(VkDevice device_h,<br>
> const VkImageCreateInfo *base_info,<br>
> diff --git a/src/intel/vulkan/anv_device.c<br>
> b/src/intel/vulkan/anv_device.c<br>
> index d8b67b54d63..54919112d08 100644<br>
> --- a/src/intel/vulkan/anv_device.c<br>
> +++ b/src/intel/vulkan/anv_device.c<br>
> @@ -2161,14 +2161,53 @@ VkResult anv_AllocateMemory(<br>
> <br>
> if (pdevice->use_softpin)<br>
> bo_flags |= EXEC_OBJECT_PINNED;<br>
> + mem->ahw = NULL;<br>
> +<br>
> + /* Check if we need to support Android HW buffer export. If so,<br>
> + * create AHardwareBuffer and import memory from it.<br>
> + */<br>
> + bool android_export = false;<br>
> + const VkExportMemoryAllocateInfo *export_info =<br>
> + vk_find_struct_const(pAllocateInfo->pNext,<br>
> + EXPORT_MEMORY_ALLOCATE_INFO);<br>
> +<br>
> + if (export_info && export_info->handleTypes &<br>
> + <br>
> VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)<br>
> + android_export = true;<br>
> +<br>
> + /* Android memory import. */<br>
> + const struct VkImportAndroidHardwareBufferInfoANDROID *ahw_info =<br>
> + vk_find_struct_const(pAllocateInfo->pNext,<br>
> + <br>
> IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID);<br>
> <br>
> const VkImportMemoryFdInfoKHR *fd_info =<br>
> vk_find_struct_const(pAllocateInfo->pNext,<br>
> IMPORT_MEMORY_FD_INFO_KHR);<br>
> <br>
> + /* Android export support required. */<br>
> + if (android_export) {<br>
> +#ifdef ANDROID<br>
> + result = anv_create_ahw_memory(_device, mem, pAllocateInfo);<br>
> + if (result != VK_SUCCESS)<br>
> + goto fail;<br>
> +<br>
> + const struct VkImportAndroidHardwareBufferInfoANDROID<br>
> import_info = {<br>
> + .buffer = mem->ahw,<br>
> + };<br>
> + result = anv_import_ahw_memory(_device, mem, &import_info);<br>
> + if (result != VK_SUCCESS)<br>
> + goto fail;<br>
> +#endif<br>
> + } else if (ahw_info) {<br>
> +#ifdef ANDROID<br>
> +<br>
> + result = anv_import_ahw_memory(_device, mem, ahw_info);<br>
> + if (result != VK_SUCCESS)<br>
> + goto fail;<br>
> /* The Vulkan spec permits handleType to be 0, in which case<br>
> the struct is<br>
> * ignored.<br>
> */<br>
> - if (fd_info && fd_info->handleType) {<br>
> +#endif<br>
> + } else if (fd_info && fd_info->handleType) {<br>
> <br>
> <br>
> Two comments here. First, I think this would be cleaner if we only had <br>
> one #ifdef:<br>
> <br>
> if (android_export || ahw_info) {<br>
> #ifdef ANDROID<br>
> #else<br>
> assert(!"Mesa was not built with android support");<br>
> #endif<br>
> }<br>
> <br>
> Second, I think you want the order the other way around so it's<br>
> <br>
> if (ahw_info) {<br>
> } else if (android_export) {<br>
> } else {<br>
> unreachable();<br>
> }<br>
> <br>
> Otherwise, if they import a ANativeHardwareBuffer that they intend to <br>
> re-export, they'll end up with both ahw_info != NULL and android_export <br>
> and it'll choose to create a new one instead of importing.<br>
> <br>
<br>
OK thanks for the comments! I'll refactor this and changes proposed to <br>
later patches and get back.<br>
<br>
<br>
> /* At the moment, we support only the below handle types. */<br>
> assert(fd_info->handleType ==<br>
> VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||<br>
> @@ -2313,6 +2352,11 @@ void anv_FreeMemory(<br>
> <br>
> anv_bo_cache_release(device, &device->bo_cache, mem->bo);<br>
> <br>
> +#ifdef ANDROID<br>
> + if (mem->ahw)<br>
> + AHardwareBuffer_release(mem->ahw);<br>
> +#endif<br>
> +<br>
> vk_free2(&device->alloc, pAllocator, mem);<br>
> }<br>
> <br>
> diff --git a/src/intel/vulkan/anv_private.h<br>
> b/src/intel/vulkan/anv_private.h<br>
> index e6984e84afa..56dee607dbe 100644<br>
> --- a/src/intel/vulkan/anv_private.h<br>
> +++ b/src/intel/vulkan/anv_private.h<br>
> @@ -85,6 +85,11 @@ struct gen_l3_config;<br>
> #include "common/intel_log.h"<br>
> #include "wsi_common.h"<br>
> <br>
> +#ifdef ANDROID<br>
> +#include <vndk/hardware_buffer.h><br>
> +#include "vulkan/vulkan_android.h"<br>
> +#endif<br>
> +<br>
> /* anv Virtual Memory Layout<br>
> * =========================<br>
> *<br>
> @@ -1354,6 +1359,11 @@ struct anv_device_memory {<br>
> struct anv_memory_type * type;<br>
> VkDeviceSize map_size;<br>
> void * map;<br>
> +<br>
> + /* If set, we are holding reference to AHardwareBuffer<br>
> + * which we must release when memory is freed.<br>
> + */<br>
> + struct AHardwareBuffer * ahw;<br>
> };<br>
> <br>
> /**<br>
> @@ -3043,6 +3053,14 @@ VkResult anv_image_from_gralloc(VkDevice<br>
> device_h,<br>
> const VkNativeBufferANDROID<br>
> *gralloc_info,<br>
> const VkAllocationCallbacks *alloc,<br>
> VkImage *pImage);<br>
> +<br>
> +VkResult anv_import_ahw_memory(VkDevice device_h,<br>
> + struct anv_device_memory *mem,<br>
> + const<br>
> VkImportAndroidHardwareBufferInfoANDROID *info);<br>
> +<br>
> +VkResult anv_create_ahw_memory(VkDevice device_h,<br>
> + struct anv_device_memory *mem,<br>
> + const VkMemoryAllocateInfo<br>
> *pAllocateInfo);<br>
> #endif<br>
> <br>
> const struct anv_surface *<br>
> -- <br>
> 2.14.4<br>
> <br>
> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a> <mailto:<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a>><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
> <br>
</blockquote></div></div>