Re: [Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-12-12 Thread Lionel Landwerlin

On 12/12/2018 08:34, Tapani Pälli wrote:



On 12/11/18 3:05 PM, Lionel Landwerlin wrote:

On 27/11/2018 10:53, Tapani Pälli wrote:

v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)
v5: rebase to b43f955037c changes,
 initialize bo flags as ANV_BO_EXTERNAL (Lionel)

Signed-off-by: Tapani Pälli 



Just a couple of suggestions below, otherwise :


Reviewed-by: Lionel Landwerlin 



---
  src/intel/vulkan/anv_android.c   | 123 
+++

  src/intel/vulkan/anv_android.h   |  10 +++
  src/intel/vulkan/anv_android_stubs.c |  16 
  src/intel/vulkan/anv_device.c    |  45 +-
  src/intel/vulkan/anv_private.h   |   5 ++
  5 files changed, 197 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c 
b/src/intel/vulkan/anv_android.c

index f2dd16212c1..bdc720214c4 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -233,6 +233,129 @@ anv_ahw_usage_from_vk_usage(const 
VkImageCreateFlags vk_create,

 return ahw_usage;
  }
+VkResult
+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+    *
+    * "If the device memory was created by importing an Android 
hardware
+    * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return 
that same

+    * Android hardware buffer object."
+    *
+    * 
"VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID 
must
+    * have been included in 
VkExportMemoryAllocateInfoKHR::handleTypes when

+    * memory was created."
+    */
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_OUT_OF_HOST_MEMORY;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const 
VkImportAndroidHardwareBufferInfoANDROID *info)

+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;



Maybe assert(handle->numFds == 1) so we don't forget to update this 
when we add support for multiple BOs later.


Perhaps I should have added some comment or warning here ... I'm 
actually letting in buffers with multiple fds now because thing is 
that we do get 2 fds with 'HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL' as we 
have 2 logical planes but really just one buffer (both point to the 
same). I was considering to add some special case/check for that but 
maybe just a comment for now?



I see, where is the offset for each plane?








+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = ANV_BO_EXTERNAL;
+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   VkResult result = anv_bo_cache_import(device, >bo_cache,
+    dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation 
must
+    * acquire a reference to the imported hardware buffer, which it 
must
+    * release when the device memory object is freed. If the 
command fails,

+    * the implementation must not retain a reference."
+    */
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return VK_SUCCESS;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, 
image->usage);

+   } else if (dedicated_info && dedicated_info->buffer) {
+  

Re: [Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-12-12 Thread Tapani Pälli



On 12/11/18 3:05 PM, Lionel Landwerlin wrote:

On 27/11/2018 10:53, Tapani Pälli wrote:

v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)
v5: rebase to b43f955037c changes,
 initialize bo flags as ANV_BO_EXTERNAL (Lionel)

Signed-off-by: Tapani Pälli 



Just a couple of suggestions below, otherwise :


Reviewed-by: Lionel Landwerlin 



---
  src/intel/vulkan/anv_android.c   | 123 +++
  src/intel/vulkan/anv_android.h   |  10 +++
  src/intel/vulkan/anv_android_stubs.c |  16 
  src/intel/vulkan/anv_device.c    |  45 +-
  src/intel/vulkan/anv_private.h   |   5 ++
  5 files changed, 197 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c 
b/src/intel/vulkan/anv_android.c

index f2dd16212c1..bdc720214c4 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -233,6 +233,129 @@ anv_ahw_usage_from_vk_usage(const 
VkImageCreateFlags vk_create,

 return ahw_usage;
  }
+VkResult
+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+    *
+    * "If the device memory was created by importing an Android hardware
+    * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return 
that same

+    * Android hardware buffer object."
+    *
+    * 
"VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+    * have been included in 
VkExportMemoryAllocateInfoKHR::handleTypes when

+    * memory was created."
+    */
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_OUT_OF_HOST_MEMORY;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkImportAndroidHardwareBufferInfoANDROID 
*info)

+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;



Maybe assert(handle->numFds == 1) so we don't forget to update this when 
we add support for multiple BOs later.


Perhaps I should have added some comment or warning here ... I'm 
actually letting in buffers with multiple fds now because thing is that 
we do get 2 fds with 'HAL_PIXEL_FORMAT_NV12_Y_TILED_INTEL' as we have 2 
logical planes but really just one buffer (both point to the same). I 
was considering to add some special case/check for that but maybe just a 
comment for now?






+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = ANV_BO_EXTERNAL;
+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   VkResult result = anv_bo_cache_import(device, >bo_cache,
+    dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation must
+    * acquire a reference to the imported hardware buffer, which it must
+    * release when the device memory object is freed. If the command 
fails,

+    * the implementation must not retain a reference."
+    */
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return VK_SUCCESS;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, 
image->usage);

+   } else if (dedicated_info && dedicated_info->buffer) {
+  ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+  w = buffer->size;
+  format = 

Re: [Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-12-11 Thread Lionel Landwerlin

On 27/11/2018 10:53, Tapani Pälli wrote:

v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)
v5: rebase to b43f955037c changes,
 initialize bo flags as ANV_BO_EXTERNAL (Lionel)

Signed-off-by: Tapani Pälli 



Just a couple of suggestions below, otherwise :


Reviewed-by: Lionel Landwerlin 



---
  src/intel/vulkan/anv_android.c   | 123 +++
  src/intel/vulkan/anv_android.h   |  10 +++
  src/intel/vulkan/anv_android_stubs.c |  16 
  src/intel/vulkan/anv_device.c|  45 +-
  src/intel/vulkan/anv_private.h   |   5 ++
  5 files changed, 197 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c b/src/intel/vulkan/anv_android.c
index f2dd16212c1..bdc720214c4 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -233,6 +233,129 @@ anv_ahw_usage_from_vk_usage(const VkImageCreateFlags 
vk_create,
 return ahw_usage;
  }
  
+VkResult

+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+*
+* "If the device memory was created by importing an Android hardware
+* buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
+* Android hardware buffer object."
+*
+* "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+* have been included in VkExportMemoryAllocateInfoKHR::handleTypes when
+* memory was created."
+*/
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_OUT_OF_HOST_MEMORY;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkImportAndroidHardwareBufferInfoANDROID *info)
+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;



Maybe assert(handle->numFds == 1) so we don't forget to update this when 
we add support for multiple BOs later.




+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = ANV_BO_EXTERNAL;
+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   VkResult result = anv_bo_cache_import(device, >bo_cache,
+dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation must
+* acquire a reference to the imported hardware buffer, which it must
+* release when the device memory object is freed. If the command fails,
+* the implementation must not retain a reference."
+*/
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return VK_SUCCESS;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, image->usage);
+   } else if (dedicated_info && dedicated_info->buffer) {
+  ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+  w = buffer->size;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   } else {
+  w = pAllocateInfo->allocationSize;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   }
+
+   struct AHardwareBuffer *ahw = NULL;
+   struct AHardwareBuffer_Desc desc = {
+  

[Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-11-27 Thread Tapani Pälli
v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)
v5: rebase to b43f955037c changes,
initialize bo flags as ANV_BO_EXTERNAL (Lionel)

Signed-off-by: Tapani Pälli 
---
 src/intel/vulkan/anv_android.c   | 123 +++
 src/intel/vulkan/anv_android.h   |  10 +++
 src/intel/vulkan/anv_android_stubs.c |  16 
 src/intel/vulkan/anv_device.c|  45 +-
 src/intel/vulkan/anv_private.h   |   5 ++
 5 files changed, 197 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c b/src/intel/vulkan/anv_android.c
index f2dd16212c1..bdc720214c4 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -233,6 +233,129 @@ anv_ahw_usage_from_vk_usage(const VkImageCreateFlags 
vk_create,
return ahw_usage;
 }
 
+VkResult
+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+*
+* "If the device memory was created by importing an Android hardware
+* buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
+* Android hardware buffer object."
+*
+* "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+* have been included in VkExportMemoryAllocateInfoKHR::handleTypes when
+* memory was created."
+*/
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_OUT_OF_HOST_MEMORY;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkImportAndroidHardwareBufferInfoANDROID *info)
+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = ANV_BO_EXTERNAL;
+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   VkResult result = anv_bo_cache_import(device, >bo_cache,
+dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation must
+* acquire a reference to the imported hardware buffer, which it must
+* release when the device memory object is freed. If the command fails,
+* the implementation must not retain a reference."
+*/
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return VK_SUCCESS;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, image->usage);
+   } else if (dedicated_info && dedicated_info->buffer) {
+  ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+  w = buffer->size;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   } else {
+  w = pAllocateInfo->allocationSize;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   }
+
+   struct AHardwareBuffer *ahw = NULL;
+   struct AHardwareBuffer_Desc desc = {
+  .width = w,
+  .height = h,
+  .layers = layers,
+  .format = format,
+  .usage = usage,
+};
+
+   if (AHardwareBuffer_allocate(, ) != 0)
+  return VK_ERROR_OUT_OF_HOST_MEMORY;
+
+   mem->ahw = ahw;
+
+   return VK_SUCCESS;
+}
+
 

Re: [Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-11-06 Thread Tapani Pälli



On 11/6/18 12:31 PM, Lionel Landwerlin wrote:

On 30/10/2018 05:26, Tapani Pälli wrote:

v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)

Signed-off-by: Tapani Pälli 
---
  src/intel/vulkan/anv_android.c | 128 +
  src/intel/vulkan/anv_device.c  |  47 +++-
  src/intel/vulkan/anv_private.h |  18 +
  3 files changed, 191 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c 
b/src/intel/vulkan/anv_android.c

index d0a20dd85c5..42fa050af93 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -232,6 +232,134 @@ anv_ahw_usage_from_vk_usage(const 
VkImageCreateFlags vk_create,

 return ahw_usage;
  }
+VkResult
+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+    *
+    * "If the device memory was created by importing an Android hardware
+    * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return 
that same

+    * Android hardware buffer object."
+    *
+    * 
"VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+    * have been included in 
VkExportMemoryAllocateInfoKHR::handleTypes when

+    * memory was created."
+    */
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkImportAndroidHardwareBufferInfoANDROID 
*info)

+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Get a description of buffer contents. */
+   AHardwareBuffer_Desc desc;
+   AHardwareBuffer_describe(info->buffer, );
+   VkResult result = VK_SUCCESS;
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = 0;



I think you need bo_flags = ANV_BO_EXTERNAL here, otherwise we'll use 
the wrong MOCS settings.


Thanks, will fix!




+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   result = anv_bo_cache_import(device, >bo_cache,
+    dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation must
+    * acquire a reference to the imported hardware buffer, which it must
+    * release when the device memory object is freed. If the command 
fails,

+    * the implementation must not retain a reference."
+    */
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return result;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, 
image->usage);

+   } else if (dedicated_info && dedicated_info->buffer) {
+  ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+  w = buffer->size;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   } else {
+  w = pAllocateInfo->allocationSize;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   }
+
+   struct AHardwareBuffer *ahw = NULL;
+   struct AHardwareBuffer_Desc desc = {
+  .width = w,
+  .height = h,
+  .layers = layers,
+  .format = 

Re: [Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-11-06 Thread Tapani Pälli



On 11/6/18 12:07 PM, Lionel Landwerlin wrote:

On 30/10/2018 05:26, Tapani Pälli wrote:

v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)

Signed-off-by: Tapani Pälli 
---
  src/intel/vulkan/anv_android.c | 128 +
  src/intel/vulkan/anv_device.c  |  47 +++-
  src/intel/vulkan/anv_private.h |  18 +
  3 files changed, 191 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c 
b/src/intel/vulkan/anv_android.c

index d0a20dd85c5..42fa050af93 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -232,6 +232,134 @@ anv_ahw_usage_from_vk_usage(const 
VkImageCreateFlags vk_create,

 return ahw_usage;
  }
+VkResult
+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+    *
+    * "If the device memory was created by importing an Android hardware
+    * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return 
that same

+    * Android hardware buffer object."
+    *
+    * 
"VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+    * have been included in 
VkExportMemoryAllocateInfoKHR::handleTypes when

+    * memory was created."
+    */
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkImportAndroidHardwareBufferInfoANDROID 
*info)

+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Get a description of buffer contents. */
+   AHardwareBuffer_Desc desc;
+   AHardwareBuffer_describe(info->buffer, );
+   VkResult result = VK_SUCCESS;
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = 0;
+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   result = anv_bo_cache_import(device, >bo_cache,
+    dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation must
+    * acquire a reference to the imported hardware buffer, which it must
+    * release when the device memory object is freed. If the command 
fails,

+    * the implementation must not retain a reference."
+    */
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return result;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, 
image->usage);

+   } else if (dedicated_info && dedicated_info->buffer) {
+  ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+  w = buffer->size;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   } else {
+  w = pAllocateInfo->allocationSize;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   }
+
+   struct AHardwareBuffer *ahw = NULL;
+   struct AHardwareBuffer_Desc desc = {
+  .width = w,
+  .height = h,
+  .layers = layers,
+  .format = format,
+  .usage = usage,
+    };
+
+   if (AHardwareBuffer_allocate(, ) != 0)
+  return 

Re: [Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-11-06 Thread Lionel Landwerlin

On 30/10/2018 05:26, Tapani Pälli wrote:

v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)

Signed-off-by: Tapani Pälli 
---
  src/intel/vulkan/anv_android.c | 128 +
  src/intel/vulkan/anv_device.c  |  47 +++-
  src/intel/vulkan/anv_private.h |  18 +
  3 files changed, 191 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c b/src/intel/vulkan/anv_android.c
index d0a20dd85c5..42fa050af93 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -232,6 +232,134 @@ anv_ahw_usage_from_vk_usage(const VkImageCreateFlags 
vk_create,
 return ahw_usage;
  }
  
+VkResult

+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+*
+* "If the device memory was created by importing an Android hardware
+* buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
+* Android hardware buffer object."
+*
+* "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+* have been included in VkExportMemoryAllocateInfoKHR::handleTypes when
+* memory was created."
+*/
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkImportAndroidHardwareBufferInfoANDROID *info)
+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Get a description of buffer contents. */
+   AHardwareBuffer_Desc desc;
+   AHardwareBuffer_describe(info->buffer, );
+   VkResult result = VK_SUCCESS;
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = 0;



I think you need bo_flags = ANV_BO_EXTERNAL here, otherwise we'll use 
the wrong MOCS settings.




+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   result = anv_bo_cache_import(device, >bo_cache,
+dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation must
+* acquire a reference to the imported hardware buffer, which it must
+* release when the device memory object is freed. If the command fails,
+* the implementation must not retain a reference."
+*/
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return result;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, image->usage);
+   } else if (dedicated_info && dedicated_info->buffer) {
+  ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+  w = buffer->size;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   } else {
+  w = pAllocateInfo->allocationSize;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   }
+
+   struct AHardwareBuffer *ahw = NULL;
+   struct AHardwareBuffer_Desc desc = {
+  .width = w,
+  .height = h,
+  .layers = layers,
+  .format = format,
+  .usage = usage,
+};
+
+   if (AHardwareBuffer_allocate(, ) != 

Re: [Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-11-06 Thread Lionel Landwerlin

On 30/10/2018 05:26, Tapani Pälli wrote:

v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)

Signed-off-by: Tapani Pälli 
---
  src/intel/vulkan/anv_android.c | 128 +
  src/intel/vulkan/anv_device.c  |  47 +++-
  src/intel/vulkan/anv_private.h |  18 +
  3 files changed, 191 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c b/src/intel/vulkan/anv_android.c
index d0a20dd85c5..42fa050af93 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -232,6 +232,134 @@ anv_ahw_usage_from_vk_usage(const VkImageCreateFlags 
vk_create,
 return ahw_usage;
  }
  
+VkResult

+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+*
+* "If the device memory was created by importing an Android hardware
+* buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
+* Android hardware buffer object."
+*
+* "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+* have been included in VkExportMemoryAllocateInfoKHR::handleTypes when
+* memory was created."
+*/
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkImportAndroidHardwareBufferInfoANDROID *info)
+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Get a description of buffer contents. */
+   AHardwareBuffer_Desc desc;
+   AHardwareBuffer_describe(info->buffer, );
+   VkResult result = VK_SUCCESS;
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = 0;
+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   result = anv_bo_cache_import(device, >bo_cache,
+dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation must
+* acquire a reference to the imported hardware buffer, which it must
+* release when the device memory object is freed. If the command fails,
+* the implementation must not retain a reference."
+*/
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return result;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, image->usage);
+   } else if (dedicated_info && dedicated_info->buffer) {
+  ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+  w = buffer->size;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   } else {
+  w = pAllocateInfo->allocationSize;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   }
+
+   struct AHardwareBuffer *ahw = NULL;
+   struct AHardwareBuffer_Desc desc = {
+  .width = w,
+  .height = h,
+  .layers = layers,
+  .format = format,
+  .usage = usage,
+};
+
+   if (AHardwareBuffer_allocate(, ) != 0)
+  return VK_ERROR_OUT_OF_HOST_MEMORY;
+
+   mem->ahw = ahw;
+
+   return VK_SUCCESS;
+}
+
  

[Mesa-dev] [PATCH 08/15] anv/android: support import/export of AHardwareBuffer objects

2018-10-29 Thread Tapani Pälli
v2: add support for non-image buffers (AHARDWAREBUFFER_FORMAT_BLOB)
v3: properly handle usage bits when creating from image
v4: refactor, code cleanup (Jason)

Signed-off-by: Tapani Pälli 
---
 src/intel/vulkan/anv_android.c | 128 +
 src/intel/vulkan/anv_device.c  |  47 +++-
 src/intel/vulkan/anv_private.h |  18 +
 3 files changed, 191 insertions(+), 2 deletions(-)

diff --git a/src/intel/vulkan/anv_android.c b/src/intel/vulkan/anv_android.c
index d0a20dd85c5..42fa050af93 100644
--- a/src/intel/vulkan/anv_android.c
+++ b/src/intel/vulkan/anv_android.c
@@ -232,6 +232,134 @@ anv_ahw_usage_from_vk_usage(const VkImageCreateFlags 
vk_create,
return ahw_usage;
 }
 
+VkResult
+anv_GetMemoryAndroidHardwareBufferANDROID(
+   VkDevice device_h,
+   const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+   struct AHardwareBuffer **pBuffer)
+{
+   ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+   /* Some quotes from Vulkan spec:
+*
+* "If the device memory was created by importing an Android hardware
+* buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
+* Android hardware buffer object."
+*
+* "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+* have been included in VkExportMemoryAllocateInfoKHR::handleTypes when
+* memory was created."
+*/
+   if (mem->ahw) {
+  *pBuffer = mem->ahw;
+  /* Increase refcount. */
+  AHardwareBuffer_acquire(mem->ahw);
+  return VK_SUCCESS;
+   }
+
+   return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkImportAndroidHardwareBufferInfoANDROID *info)
+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   /* Get a description of buffer contents. */
+   AHardwareBuffer_Desc desc;
+   AHardwareBuffer_describe(info->buffer, );
+   VkResult result = VK_SUCCESS;
+
+   /* Import from AHardwareBuffer to anv_device_memory. */
+   const native_handle_t *handle =
+  AHardwareBuffer_getNativeHandle(info->buffer);
+
+   int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
+   if (dma_buf < 0)
+  return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   uint64_t bo_flags = 0;
+   if (device->instance->physicalDevice.supports_48bit_addresses)
+  bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   if (device->instance->physicalDevice.use_softpin)
+  bo_flags |= EXEC_OBJECT_PINNED;
+
+   result = anv_bo_cache_import(device, >bo_cache,
+dma_buf, bo_flags, >bo);
+   if (result != VK_SUCCESS)
+  return result;
+
+   /* "If the vkAllocateMemory command succeeds, the implementation must
+* acquire a reference to the imported hardware buffer, which it must
+* release when the device memory object is freed. If the command fails,
+* the implementation must not retain a reference."
+*/
+   AHardwareBuffer_acquire(info->buffer);
+   mem->ahw = info->buffer;
+
+   return result;
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+  struct anv_device_memory *mem,
+  const VkMemoryAllocateInfo *pAllocateInfo)
+{
+   ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+   const VkMemoryDedicatedAllocateInfo *dedicated_info =
+  vk_find_struct_const(pAllocateInfo->pNext,
+   MEMORY_DEDICATED_ALLOCATE_INFO);
+
+   uint32_t w = 0;
+   uint32_t h = 1;
+   uint32_t layers = 1;
+   uint32_t format = 0;
+   uint64_t usage = 0;
+
+   /* If caller passed dedicated information. */
+   if (dedicated_info && dedicated_info->image) {
+  ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+  w = image->extent.width;
+  h = image->extent.height;
+  layers = image->array_size;
+  format = android_format_from_vk(image->vk_format);
+  usage = anv_ahw_usage_from_vk_usage(image->create_flags, image->usage);
+   } else if (dedicated_info && dedicated_info->buffer) {
+  ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+  w = buffer->size;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   } else {
+  w = pAllocateInfo->allocationSize;
+  format = AHARDWAREBUFFER_FORMAT_BLOB;
+  usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+  AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+   }
+
+   struct AHardwareBuffer *ahw = NULL;
+   struct AHardwareBuffer_Desc desc = {
+  .width = w,
+  .height = h,
+  .layers = layers,
+  .format = format,
+  .usage = usage,
+};
+
+   if (AHardwareBuffer_allocate(, ) != 0)
+  return VK_ERROR_OUT_OF_HOST_MEMORY;
+
+   mem->ahw = ahw;
+
+   return VK_SUCCESS;
+}
+
 VkResult
 anv_image_from_gralloc(VkDevice device_h,