Mesa (main): venus: cache ahb backed buffer memory type bits requirement

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Aug 2 17:13:44 UTC 2021


Module: Mesa
Branch: main
Commit: e08960482a1330f93d6c6c7b0654b08bc29537f0
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=e08960482a1330f93d6c6c7b0654b08bc29537f0

Author: Yiwei Zhang <zzyiwei at chromium.org>
Date:   Mon Aug  2 15:49:55 2021 +0000

venus: cache ahb backed buffer memory type bits requirement

To properly init buffer memory requirement for AHB, memory type bits
from dma_buf fd properties need to be masked. However, creating a test
AHB at buffer creation is too costy. This patch caches the ahb backed
buffer memory type bits at device creation time if the app is requesting
AHB extension.

Cc: 21.2 mesa-stable

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

---

 src/virtio/vulkan/vn_android.c | 22 ++++++++++++++++------
 src/virtio/vulkan/vn_android.h |  9 +++++++++
 src/virtio/vulkan/vn_device.c  |  9 +++++++++
 src/virtio/vulkan/vn_device.h  |  3 +++
 4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/src/virtio/vulkan/vn_android.c b/src/virtio/vulkan/vn_android.c
index 2b0c0056a34..0dbcbc5f036 100644
--- a/src/virtio/vulkan/vn_android.c
+++ b/src/virtio/vulkan/vn_android.c
@@ -1128,10 +1128,7 @@ vn_android_fix_buffer_create_info(
 }
 
 VkResult
-vn_android_buffer_from_ahb(struct vn_device *dev,
-                           const VkBufferCreateInfo *create_info,
-                           const VkAllocationCallbacks *alloc,
-                           struct vn_buffer **out_buf)
+vn_android_init_ahb_buffer_memory_type_bits(struct vn_device *dev)
 {
    const uint32_t format = AHARDWAREBUFFER_FORMAT_BLOB;
    /* ensure dma_buf_memory_type_bits covers host visible usage */
@@ -1142,7 +1139,6 @@ vn_android_buffer_from_ahb(struct vn_device *dev,
    int dma_buf_fd = -1;
    uint64_t alloc_size = 0;
    uint32_t mem_type_bits = 0;
-   struct vn_android_buffer_create_info local_info;
    VkResult result;
 
    ahb = vn_android_ahb_allocate(4096, 1, 1, format, usage);
@@ -1164,6 +1160,20 @@ vn_android_buffer_from_ahb(struct vn_device *dev,
    if (result != VK_SUCCESS)
       return result;
 
+   dev->ahb_buffer_memory_type_bits = mem_type_bits;
+
+   return VK_SUCCESS;
+}
+
+VkResult
+vn_android_buffer_from_ahb(struct vn_device *dev,
+                           const VkBufferCreateInfo *create_info,
+                           const VkAllocationCallbacks *alloc,
+                           struct vn_buffer **out_buf)
+{
+   struct vn_android_buffer_create_info local_info;
+   VkResult result;
+
    create_info = vn_android_fix_buffer_create_info(create_info, &local_info);
    result = vn_buffer_create(dev, create_info, alloc, out_buf);
    if (result != VK_SUCCESS)
@@ -1174,7 +1184,7 @@ vn_android_buffer_from_ahb(struct vn_device *dev,
     * properties.
     */
    (*out_buf)->memory_requirements.memoryRequirements.memoryTypeBits &=
-      mem_type_bits;
+      dev->ahb_buffer_memory_type_bits;
 
    assert((*out_buf)->memory_requirements.memoryRequirements.memoryTypeBits);
 
diff --git a/src/virtio/vulkan/vn_android.h b/src/virtio/vulkan/vn_android.h
index 8389b6dd0fc..038aec35840 100644
--- a/src/virtio/vulkan/vn_android.h
+++ b/src/virtio/vulkan/vn_android.h
@@ -75,6 +75,9 @@ vn_android_buffer_from_ahb(struct vn_device *dev,
                            const VkAllocationCallbacks *alloc,
                            struct vn_buffer **out_buf);
 
+VkResult
+vn_android_init_ahb_buffer_memory_type_bits(struct vn_device *dev);
+
 #else
 
 static inline const VkNativeBufferANDROID *
@@ -157,6 +160,12 @@ vn_android_buffer_from_ahb(UNUSED struct vn_device *dev,
    return VK_ERROR_OUT_OF_HOST_MEMORY;
 }
 
+static inline VkResult
+vn_android_init_ahb_buffer_memory_type_bits(UNUSED struct vn_device *dev)
+{
+   return VK_ERROR_FEATURE_NOT_PRESENT;
+}
+
 #endif /* ANDROID */
 
 #endif /* VN_ANDROID_H */
diff --git a/src/virtio/vulkan/vn_device.c b/src/virtio/vulkan/vn_device.c
index d20a6c6ba04..7e890b42323 100644
--- a/src/virtio/vulkan/vn_device.c
+++ b/src/virtio/vulkan/vn_device.c
@@ -3386,6 +3386,15 @@ vn_CreateDevice(VkPhysicalDevice physicalDevice,
       mtx_init(&pool->mutex, mtx_plain);
    }
 
+   if (dev->base.base.enabled_extensions
+          .ANDROID_external_memory_android_hardware_buffer) {
+      result = vn_android_init_ahb_buffer_memory_type_bits(dev);
+      if (result != VK_SUCCESS) {
+         vn_call_vkDestroyDevice(instance, dev_handle, NULL);
+         goto fail;
+      }
+   }
+
    *pDevice = dev_handle;
 
    if (pCreateInfo == &local_create_info)
diff --git a/src/virtio/vulkan/vn_device.h b/src/virtio/vulkan/vn_device.h
index 1e79cec73ef..b0fc465814a 100644
--- a/src/virtio/vulkan/vn_device.h
+++ b/src/virtio/vulkan/vn_device.h
@@ -139,6 +139,9 @@ struct vn_device {
    uint32_t queue_count;
 
    struct vn_device_memory_pool memory_pools[VK_MAX_MEMORY_TYPES];
+
+   /* cache memory type requirement for AHB backed VkBuffer */
+   uint32_t ahb_buffer_memory_type_bits;
 };
 VK_DEFINE_HANDLE_CASTS(vn_device,
                        base.base.base,



More information about the mesa-commit mailing list