<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Tue, Aug 21, 2018 at 3:27 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">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>
---<br>
 src/intel/vulkan/anv_android.c | 149 +++++++++++++++++++++++++++++++++++++++++<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 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 hardware<br>
+    * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same<br>
+    * Android hardware buffer object."<br>
+    *<br>
+    * "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must<br>
+    * have been included in 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 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 must<br>
+    * acquire a reference to the imported hardware buffer, which it must<br>
+    * release when the device memory object is freed. If the 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></blockquote><div><br></div><div>If the image is linear (not sure if that's possible), we could give them a valid stride.<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 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>
+       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>
+                           IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID);<br>
<br>
    const VkImportMemoryFdInfoKHR *fd_info =<br>
       vk_find_struct_const(pAllocateInfo->pNext, 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 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 the struct is<br>
     * ignored.<br>
     */<br>
-   if (fd_info && fd_info->handleType) {<br>
+#endif<br>
+   } else if (fd_info && fd_info->handleType) {<br></blockquote><div><br></div><div>Two comments here.  First, I think this would be cleaner if we only had one #ifdef:</div><div><br></div><div>if (android_export || ahw_info) {</div><div>#ifdef ANDROID</div><div>#else</div><div>    assert(!"Mesa was not built with android support");<br></div><div>#endif</div><div>}</div><div><br></div><div>Second, I think you want the order the other way around so it's</div><div><br></div><div>if (ahw_info) {</div><div>} else if (android_export) {</div><div>} else {</div><div>   unreachable();</div><div>}</div><div><br></div><div>Otherwise, if they import a ANativeHardwareBuffer that they intend to re-export, they'll end up with both ahw_info != NULL and android_export and it'll choose to create a new one instead of importing.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
       /* 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 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 device_h,<br>
                                 const VkNativeBufferANDROID *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 VkImportAndroidHardwareBufferInfoANDROID *info);<br>
+<br>
+VkResult anv_create_ahw_memory(VkDevice device_h,<br>
+                               struct anv_device_memory *mem,<br>
+                               const VkMemoryAllocateInfo *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><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>
</blockquote></div></div>