venus: support AHB prop query with host dma_buf size
authorYiwei Zhang <zzyiwei@chromium.org>
Tue, 1 Jun 2021 22:23:53 +0000 (22:23 +0000)
committerYiwei Zhang <zzyiwei@chromium.org>
Thu, 3 Jun 2021 19:23:04 +0000 (19:23 +0000)
Upon instance creation, venus experimental features are cached in the
vn_instance. If memoryResourceAllocationSize feature is supported, chain
VkMemoryResourceAllocationSizeProperties100000MESA to the pNext of
VkMemoryResourcePropertiesMESA to get the host allocation size of the
dma_buf fd.

Signed-off-by: Yiwei Zhang <zzyiwei@chromium.org>
Reviewed-by: Chia-I Wu <olvaffe@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11048>

src/virtio/vulkan/vn_android.c
src/virtio/vulkan/vn_device.c
src/virtio/vulkan/vn_device.h
src/virtio/vulkan/vn_device_memory.c

index c7afcb2..4aaf7a1 100644 (file)
@@ -450,9 +450,11 @@ vn_android_image_from_anb(struct vn_device *dev,
              mem_req.size, alloc_size, mem_req.memoryTypeBits, mem_type_bits);
    }
 
-   /* TODO When alloc_size is fixed to return host storage size, we will
-    * also check alloc_size is not smaller than mem_req.size here.
-    */
+   if (alloc_size < mem_req.size) {
+      result = VK_ERROR_INVALID_EXTERNAL_HANDLE;
+      goto fail;
+   }
+
    mem_type_bits &= mem_req.memoryTypeBits;
    if (!mem_type_bits) {
       result = VK_ERROR_INVALID_EXTERNAL_HANDLE;
@@ -886,21 +888,21 @@ vn_android_device_import_ahb(struct vn_device *dev,
       if (result != VK_SUCCESS)
          return result;
 
-      /* TODO When alloc_size is fixed to return host storage size, we will
-       * also check alloc_size is not smaller than mem_req.size here.
-       */
       VkMemoryRequirements mem_req;
       vn_GetImageMemoryRequirements(device, dedicated_info->image, &mem_req);
+      if (alloc_size < mem_req.size)
+         return VK_ERROR_INVALID_EXTERNAL_HANDLE;
+
       alloc_size = mem_req.size;
    }
 
    if (dedicated_info && dedicated_info->buffer != VK_NULL_HANDLE) {
-      /* TODO When alloc_size is fixed to return host storage size, we will
-       * also check alloc_size is not smaller than mem_req.size here.
-       */
       VkMemoryRequirements mem_req;
       vn_GetBufferMemoryRequirements(device, dedicated_info->buffer,
                                      &mem_req);
+      if (alloc_size < mem_req.size)
+         return VK_ERROR_INVALID_EXTERNAL_HANDLE;
+
       alloc_size = mem_req.size;
    }
 
index a9380ae..6ef024c 100644 (file)
@@ -1453,7 +1453,8 @@ vn_physical_device_get_native_extensions(
        renderer_info->has_dma_buf_import) {
 #ifdef ANDROID
       if (renderer_exts->EXT_image_drm_format_modifier &&
-          renderer_exts->EXT_queue_family_foreign) {
+          renderer_exts->EXT_queue_family_foreign &&
+          instance->experimental.memoryResourceAllocationSize == VK_TRUE) {
          exts->ANDROID_external_memory_android_hardware_buffer = true;
          exts->ANDROID_native_buffer = true;
       }
@@ -1895,6 +1896,16 @@ vn_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
    if (result != VK_SUCCESS)
       goto fail;
 
+   if (instance->renderer_info.vk_mesa_venus_protocol_spec_version ==
+       100000) {
+      size_t size = 0;
+      vn_call_vkGetVenusExperimentalFeatureData100000MESA(instance, &size,
+                                                          NULL);
+      size = MIN2(size, sizeof(instance->experimental));
+      vn_call_vkGetVenusExperimentalFeatureData100000MESA(
+         instance, &size, &instance->experimental);
+   }
+
    result = vn_instance_init_renderer_versions(instance);
    if (result != VK_SUCCESS)
       goto fail;
index 9f01de7..1e79cec 100644 (file)
@@ -13,6 +13,8 @@
 
 #include "vn_common.h"
 
+#include "venus-protocol/vn_protocol_driver_defines.h"
+
 #include "vn_cs.h"
 #include "vn_device_memory.h"
 #include "vn_renderer.h"
@@ -63,6 +65,9 @@ struct vn_instance {
    mtx_t physical_device_mutex;
    struct vn_physical_device *physical_devices;
    uint32_t physical_device_count;
+
+   /* XXX staged features to be merged to core venus protocol */
+   VkVenusExperimentalFeatures100000MESA experimental;
 };
 VK_DEFINE_HANDLE_CASTS(vn_instance,
                        base.base.base,
index bf19970..5e427ee 100644 (file)
@@ -502,9 +502,18 @@ vn_get_memory_dma_buf_properties(struct vn_device *dev,
 
    vn_instance_roundtrip(dev->instance);
 
+   VkMemoryResourceAllocationSizeProperties100000MESA alloc_size_props = {
+      .sType =
+         VK_STRUCTURE_TYPE_MEMORY_RESOURCE_ALLOCATION_SIZE_PROPERTIES_100000_MESA,
+      .pNext = NULL,
+      .allocationSize = 0,
+   };
    VkMemoryResourcePropertiesMESA props = {
       .sType = VK_STRUCTURE_TYPE_MEMORY_RESOURCE_PROPERTIES_MESA,
-      .pNext = NULL,
+      .pNext =
+         dev->instance->experimental.memoryResourceAllocationSize == VK_TRUE
+            ? &alloc_size_props
+            : NULL,
       .memoryTypeBits = 0,
    };
    result = vn_call_vkGetMemoryResourcePropertiesMESA(dev->instance, device,
@@ -513,8 +522,7 @@ vn_get_memory_dma_buf_properties(struct vn_device *dev,
    if (result != VK_SUCCESS)
       return result;
 
-   /* XXX extend VkMemoryResourcePropertiesMESA for host storage size */
-   *out_alloc_size = lseek(fd, 0, SEEK_END);
+   *out_alloc_size = alloc_size_props.allocationSize;
    *out_mem_type_bits = props.memoryTypeBits;
 
    return VK_SUCCESS;