panfrost: Rework midgard_pair_load_store() to kill the nested foreach loop
[mesa.git] / src / intel / vulkan / anv_android.c
index caa53478fe4795f1319e56d9075793b04cd47732..a8c1d480bfa5cf4f6e0d54515c02e9ea270b2f76 100644 (file)
  */
 
 #include <hardware/gralloc.h>
+
+#if ANDROID_API_LEVEL >= 26
+#include <hardware/gralloc1.h>
+#include <grallocusage/GrallocUsageConversion.h>
+#endif
+
 #include <hardware/hardware.h>
 #include <hardware/hwvulkan.h>
 #include <vulkan/vk_android_native_buffer.h>
@@ -98,6 +104,7 @@ anv_hal_close(struct hw_device_t *dev)
    return -1;
 }
 
+#if ANDROID_API_LEVEL >= 26
 static VkResult
 get_ahw_buffer_format_properties(
    VkDevice device_h,
@@ -120,12 +127,12 @@ get_ahw_buffer_format_properties(
     * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags."
     */
    if (!(desc.usage & (gpu_usage)))
-      return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+      return VK_ERROR_INVALID_EXTERNAL_HANDLE;
 
    /* Fill properties fields based on description. */
    VkAndroidHardwareBufferFormatPropertiesANDROID *p = pProperties;
 
-   p->format = vk_format_from_android(desc.format);
+   p->format = vk_format_from_android(desc.format, desc.usage);
 
    const struct anv_format *anv_format = anv_get_format(p->format);
    p->externalFormat = (uint64_t) (uintptr_t) anv_format;
@@ -204,7 +211,7 @@ anv_GetAndroidHardwareBufferPropertiesANDROID(
       AHardwareBuffer_getNativeHandle(buffer);
    int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
    if (dma_buf < 0)
-      return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+      return VK_ERROR_INVALID_EXTERNAL_HANDLE;
 
    /* All memory types. */
    uint32_t memory_types = (1ull << pdevice->memory.type_count) - 1;
@@ -215,6 +222,36 @@ anv_GetAndroidHardwareBufferPropertiesANDROID(
    return VK_SUCCESS;
 }
 
+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 VkExportMemoryAllocateInfo::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;
+}
+
+#endif
+
 /* Construct ahw usage mask from image usage bits, see
  * 'AHardwareBuffer Usage Equivalence' in Vulkan spec.
  */
@@ -223,7 +260,7 @@ anv_ahw_usage_from_vk_usage(const VkImageCreateFlags vk_create,
                             const VkImageUsageFlags vk_usage)
 {
    uint64_t ahw_usage = 0;
-
+#if ANDROID_API_LEVEL >= 26
    if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
       ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
 
@@ -242,38 +279,10 @@ anv_ahw_usage_from_vk_usage(const VkImageCreateFlags vk_create,
    /* No usage bits set - set at least one GPU usage. */
    if (ahw_usage == 0)
       ahw_usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
-
+#endif
    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.
  */
@@ -282,6 +291,7 @@ anv_import_ahw_memory(VkDevice device_h,
                       struct anv_device_memory *mem,
                       const VkImportAndroidHardwareBufferInfoANDROID *info)
 {
+#if ANDROID_API_LEVEL >= 26
    ANV_FROM_HANDLE(anv_device, device, device_h);
 
    /* Import from AHardwareBuffer to anv_device_memory. */
@@ -295,7 +305,7 @@ anv_import_ahw_memory(VkDevice device_h,
     */
    int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
    if (dma_buf < 0)
-      return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+      return VK_ERROR_INVALID_EXTERNAL_HANDLE;
 
    uint64_t bo_flags = ANV_BO_EXTERNAL;
    if (device->instance->physicalDevice.supports_48bit_addresses)
@@ -316,6 +326,9 @@ anv_import_ahw_memory(VkDevice device_h,
    mem->ahw = info->buffer;
 
    return VK_SUCCESS;
+#else
+   return VK_ERROR_EXTENSION_NOT_PRESENT;
+#endif
 }
 
 VkResult
@@ -323,6 +336,7 @@ anv_create_ahw_memory(VkDevice device_h,
                       struct anv_device_memory *mem,
                       const VkMemoryAllocateInfo *pAllocateInfo)
 {
+#if ANDROID_API_LEVEL >= 26
    ANV_FROM_HANDLE(anv_device, dev, device_h);
 
    const VkMemoryDedicatedAllocateInfo *dedicated_info =
@@ -369,8 +383,11 @@ anv_create_ahw_memory(VkDevice device_h,
       return VK_ERROR_OUT_OF_HOST_MEMORY;
 
    mem->ahw = ahw;
-
    return VK_SUCCESS;
+#else
+   return VK_ERROR_EXTENSION_NOT_PRESENT;
+#endif
+
 }
 
 VkResult
@@ -381,6 +398,7 @@ anv_image_from_external(
    const VkAllocationCallbacks *alloc,
    VkImage *out_image_h)
 {
+#if ANDROID_API_LEVEL >= 26
    ANV_FROM_HANDLE(anv_device, device, device_h);
 
    const struct VkExternalFormatANDROID *ext_info =
@@ -407,8 +425,12 @@ anv_image_from_external(
    *out_image_h = image_h;
 
    return VK_SUCCESS;
+#else
+   return VK_ERROR_EXTENSION_NOT_PRESENT;
+#endif
 }
 
+
 VkResult
 anv_image_from_gralloc(VkDevice device_h,
                        const VkImageCreateInfo *base_info,
@@ -430,7 +452,7 @@ anv_image_from_gralloc(VkDevice device_h,
 
    if (gralloc_info->handle->numFds != 1) {
       return vk_errorf(device->instance, device,
-                       VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
+                       VK_ERROR_INVALID_EXTERNAL_HANDLE,
                        "VkNativeBufferANDROID::handle::numFds is %d, "
                        "expected 1", gralloc_info->handle->numFds);
    }
@@ -466,13 +488,13 @@ anv_image_from_gralloc(VkDevice device_h,
       break;
    case -1:
       result = vk_errorf(device->instance, device,
-                         VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
+                         VK_ERROR_INVALID_EXTERNAL_HANDLE,
                          "DRM_IOCTL_I915_GEM_GET_TILING failed for "
                          "VkNativeBufferANDROID");
       goto fail_tiling;
    default:
       result = vk_errorf(device->instance, device,
-                         VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
+                         VK_ERROR_INVALID_EXTERNAL_HANDLE,
                          "DRM_IOCTL_I915_GEM_GET_TILING returned unknown "
                          "tiling %d for VkNativeBufferANDROID", i915_tiling);
       goto fail_tiling;
@@ -494,7 +516,7 @@ anv_image_from_gralloc(VkDevice device_h,
 
    if (bo->size < image->size) {
       result = vk_errorf(device->instance, device,
-                         VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
+                         VK_ERROR_INVALID_EXTERNAL_HANDLE,
                          "dma-buf from VkNativeBufferANDROID is too small for "
                          "VkImage: %"PRIu64"B < %"PRIu64"B",
                          bo->size, image->size);
@@ -533,43 +555,25 @@ anv_image_from_gralloc(VkDevice device_h,
    return result;
 }
 
-VkResult anv_GetSwapchainGrallocUsageANDROID(
-    VkDevice            device_h,
-    VkFormat            format,
-    VkImageUsageFlags   imageUsage,
-    int*                grallocUsage)
+VkResult
+format_supported_with_usage(VkDevice device_h, VkFormat format,
+                            VkImageUsageFlags imageUsage)
 {
    ANV_FROM_HANDLE(anv_device, device, device_h);
    struct anv_physical_device *phys_dev = &device->instance->physicalDevice;
    VkPhysicalDevice phys_dev_h = anv_physical_device_to_handle(phys_dev);
    VkResult result;
 
-   *grallocUsage = 0;
-   intel_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
-
-   /* WARNING: Android's libvulkan.so hardcodes the VkImageUsageFlags
-    * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
-    * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
-    *
-    *     TODO(jessehall): I think these are right, but haven't thought hard
-    *     about it. Do we need to query the driver for support of any of
-    *     these?
-    *
-    * Any disagreement between this function and the hardcoded
-    * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
-    * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
-    */
-
-   const VkPhysicalDeviceImageFormatInfo2KHR image_format_info = {
-      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2_KHR,
+   const VkPhysicalDeviceImageFormatInfo2 image_format_info = {
+      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
       .format = format,
       .type = VK_IMAGE_TYPE_2D,
       .tiling = VK_IMAGE_TILING_OPTIMAL,
       .usage = imageUsage,
    };
 
-   VkImageFormatProperties2KHR image_format_props = {
-      .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR,
+   VkImageFormatProperties2 image_format_props = {
+      .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
    };
 
    /* Check that requested format and usage are supported. */
@@ -580,6 +584,26 @@ VkResult anv_GetSwapchainGrallocUsageANDROID(
                        "anv_GetPhysicalDeviceImageFormatProperties2 failed "
                        "inside %s", __func__);
    }
+   return VK_SUCCESS;
+}
+
+
+static VkResult
+setup_gralloc0_usage(VkFormat format, VkImageUsageFlags imageUsage,
+                     int *grallocUsage)
+{
+   /* WARNING: Android's libvulkan.so hardcodes the VkImageUsageFlags
+    * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
+    * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
+    *
+    *     TODO(jessehall): I think these are right, but haven't thought hard
+    *     about it. Do we need to query the driver for support of any of
+    *     these?
+    *
+    * Any disagreement between this function and the hardcoded
+    * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
+    * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
+    */
 
    if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
                              VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
@@ -626,6 +650,60 @@ VkResult anv_GetSwapchainGrallocUsageANDROID(
    return VK_SUCCESS;
 }
 
+
+#if ANDROID_API_LEVEL >= 26
+VkResult anv_GetSwapchainGrallocUsage2ANDROID(
+    VkDevice            device_h,
+    VkFormat            format,
+    VkImageUsageFlags   imageUsage,
+    VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
+    uint64_t*           grallocConsumerUsage,
+    uint64_t*           grallocProducerUsage)
+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+   VkResult result;
+
+   *grallocConsumerUsage = 0;
+   *grallocProducerUsage = 0;
+   intel_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
+
+   result = format_supported_with_usage(device_h, format, imageUsage);
+   if (result != VK_SUCCESS)
+      return result;
+
+   int32_t grallocUsage = 0;
+   result = setup_gralloc0_usage(format, imageUsage, &grallocUsage);
+   if (result != VK_SUCCESS)
+      return result;
+
+   android_convertGralloc0To1Usage(grallocUsage, grallocProducerUsage,
+                                   grallocConsumerUsage);
+
+   return VK_SUCCESS;
+}
+#endif
+
+VkResult anv_GetSwapchainGrallocUsageANDROID(
+    VkDevice            device_h,
+    VkFormat            format,
+    VkImageUsageFlags   imageUsage,
+    int*                grallocUsage)
+{
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+   struct anv_physical_device *phys_dev = &device->instance->physicalDevice;
+   VkPhysicalDevice phys_dev_h = anv_physical_device_to_handle(phys_dev);
+   VkResult result;
+
+   *grallocUsage = 0;
+   intel_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
+
+   result = format_supported_with_usage(device_h, format, imageUsage);
+   if (result != VK_SUCCESS)
+      return result;
+
+   return setup_gralloc0_usage(format, imageUsage, grallocUsage);
+}
+
 VkResult
 anv_AcquireImageANDROID(
       VkDevice            device_h,