Query Vulkan image mem requirements after binding

Vulkan spec indicates that we should not call
vkGetImageMemoryRequirements2KHR until after memory is bound
to an image when that image is used with external
Android hardware buffers. Re-ordering code to follow spec.

Bug: 130125365
Test: Manual verification on Pixel 3
Change-Id: I584d269e5aa8eb6084a9aab6f5f9e3146abdc911
Merged-In: I584d269e5aa8eb6084a9aab6f5f9e3146abdc911
diff --git a/tests/tests/graphics/jni/VulkanTestHelpers.cpp b/tests/tests/graphics/jni/VulkanTestHelpers.cpp
index c2dd5b4..17d952f 100644
--- a/tests/tests/graphics/jni/VulkanTestHelpers.cpp
+++ b/tests/tests/graphics/jni/VulkanTestHelpers.cpp
@@ -263,27 +263,6 @@
   };
   VK_CALL(vkCreateImage(mInit->device(), &createInfo, nullptr, &mImage));
 
-  VkImageMemoryRequirementsInfo2 memReqsInfo;
-  memReqsInfo.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2;
-  memReqsInfo.pNext = nullptr;
-  memReqsInfo.image = mImage;
-
-  VkMemoryDedicatedRequirements dedicatedMemReqs;
-  dedicatedMemReqs.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS;
-  dedicatedMemReqs.pNext = nullptr;
-
-  VkMemoryRequirements2 memReqs;
-  memReqs.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
-  memReqs.pNext = &dedicatedMemReqs;
-
-  PFN_vkGetImageMemoryRequirements2KHR getImageMemoryRequirements =
-      (PFN_vkGetImageMemoryRequirements2KHR)vkGetDeviceProcAddr(
-          mInit->device(), "vkGetImageMemoryRequirements2KHR");
-  ASSERT(getImageMemoryRequirements);
-  getImageMemoryRequirements(mInit->device(), &memReqsInfo, &memReqs);
-  ASSERT(VK_TRUE == dedicatedMemReqs.prefersDedicatedAllocation);
-  ASSERT(VK_TRUE == dedicatedMemReqs.requiresDedicatedAllocation);
-
   VkImportAndroidHardwareBufferInfoANDROID androidHardwareBufferInfo{
       .sType = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID,
       .pNext = nullptr,
@@ -317,6 +296,27 @@
   ASSERT(bindImageMemory);
   VK_CALL(bindImageMemory(mInit->device(), 1, &bindImageInfo));
 
+  VkImageMemoryRequirementsInfo2 memReqsInfo;
+  memReqsInfo.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2;
+  memReqsInfo.pNext = nullptr;
+  memReqsInfo.image = mImage;
+
+  VkMemoryDedicatedRequirements dedicatedMemReqs;
+  dedicatedMemReqs.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS;
+  dedicatedMemReqs.pNext = nullptr;
+
+  VkMemoryRequirements2 memReqs;
+  memReqs.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
+  memReqs.pNext = &dedicatedMemReqs;
+
+  PFN_vkGetImageMemoryRequirements2KHR getImageMemoryRequirements =
+      (PFN_vkGetImageMemoryRequirements2KHR)vkGetDeviceProcAddr(
+          mInit->device(), "vkGetImageMemoryRequirements2KHR");
+  ASSERT(getImageMemoryRequirements);
+  getImageMemoryRequirements(mInit->device(), &memReqsInfo, &memReqs);
+  ASSERT(VK_TRUE == dedicatedMemReqs.prefersDedicatedAllocation);
+  ASSERT(VK_TRUE == dedicatedMemReqs.requiresDedicatedAllocation);
+
   if (useExternalFormat /* TODO: || explicit format requires conversion */) {
     VkSamplerYcbcrConversionCreateInfo conversionCreateInfo{
         .sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO,