Snap for 8712537 from a4b7cacb4c605530921c7de1182da29e2238a4c9 to tm-qpr1-release

Change-Id: Ia7f23155fe350e841aef4121001014e55f2f9781
diff --git a/system/vulkan_enc/AndroidHardwareBuffer.cpp b/system/vulkan_enc/AndroidHardwareBuffer.cpp
index 40360a2..c49693d 100644
--- a/system/vulkan_enc/AndroidHardwareBuffer.cpp
+++ b/system/vulkan_enc/AndroidHardwareBuffer.cpp
@@ -75,8 +75,46 @@
         if (!(desc.usage & (gpu_usage))) {
             return VK_ERROR_INVALID_EXTERNAL_HANDLE;
         }
-
-        ahbFormatProps->format = vk_format_from_android(desc.format);
+        switch(desc.format) {
+            case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
+                  ahbFormatProps->format = VK_FORMAT_R8G8B8A8_UNORM;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
+                  ahbFormatProps->format = VK_FORMAT_R8G8B8A8_UNORM;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
+                  ahbFormatProps->format = VK_FORMAT_R8G8B8_UNORM;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
+                  ahbFormatProps->format = VK_FORMAT_R5G6B5_UNORM_PACK16;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
+                  ahbFormatProps->format = VK_FORMAT_R16G16B16A16_SFLOAT;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
+                  ahbFormatProps->format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_D16_UNORM:
+                  ahbFormatProps->format = VK_FORMAT_D16_UNORM;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_D24_UNORM:
+                  ahbFormatProps->format = VK_FORMAT_X8_D24_UNORM_PACK32;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT:
+                  ahbFormatProps->format = VK_FORMAT_D24_UNORM_S8_UINT;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_D32_FLOAT:
+                  ahbFormatProps->format = VK_FORMAT_D32_SFLOAT;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT:
+                  ahbFormatProps->format = VK_FORMAT_D32_SFLOAT_S8_UINT;
+                  break;
+            case AHARDWAREBUFFER_FORMAT_S8_UINT:
+                  ahbFormatProps->format = VK_FORMAT_S8_UINT;
+                  break;
+            default:
+                  ahbFormatProps->format = VK_FORMAT_UNDEFINED;
+        }
         ahbFormatProps->externalFormat = desc.format;
 
         // The formatFeatures member must include
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index a4bf2e7..76e8677 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -362,6 +362,10 @@
         VkDeviceSize currentBackingSize = 0;
         bool baseRequirementsKnown = false;
         VkMemoryRequirements baseRequirements;
+#ifdef VK_USE_PLATFORM_ANDROID_KHR
+        bool hasExternalFormat = false;
+        unsigned androidFormat = 0;
+#endif
 #ifdef VK_USE_PLATFORM_FUCHSIA
         bool isSysmemBackedMemory = false;
 #endif
@@ -5055,6 +5059,11 @@
         info.createInfo = *pCreateInfo;
         info.createInfo.pNext = nullptr;
 
+        if (extFormatAndroidPtr && extFormatAndroidPtr->externalFormat) {
+            info.hasExternalFormat = true;
+            info.androidFormat = extFormatAndroidPtr->externalFormat;
+        }
+
         if (supportsCreateResourcesWithRequirements()) {
             info.baseRequirementsKnown = true;
         }
@@ -5192,7 +5201,8 @@
             vk_find_struct<VkSamplerYcbcrConversionInfo>(pCreateInfo);
         if (samplerYcbcrConversionInfo) {
             if (samplerYcbcrConversionInfo->conversion != VK_YCBCR_CONVERSION_DO_NOTHING) {
-                localVkSamplerYcbcrConversionInfo = vk_make_orphan_copy(*samplerYcbcrConversionInfo);
+                localVkSamplerYcbcrConversionInfo =
+                    vk_make_orphan_copy(*samplerYcbcrConversionInfo);
                 vk_append_struct(&structChainIter, &localVkSamplerYcbcrConversionInfo);
             }
         }
@@ -7575,14 +7585,24 @@
         (void)input_result;
 
         VkImageViewCreateInfo localCreateInfo = vk_make_orphan_copy(*pCreateInfo);
+        vk_struct_chain_iterator structChainIter = vk_make_chain_iterator(&localCreateInfo);
 
 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
-        const VkExternalFormatANDROID* extFormatAndroidPtr =
-            vk_find_struct<VkExternalFormatANDROID>(pCreateInfo);
-        if (extFormatAndroidPtr) {
-            if (extFormatAndroidPtr->externalFormat) {
-                localCreateInfo.format =
-                    vk_format_from_android(extFormatAndroidPtr->externalFormat);
+        if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
+            AutoLock<RecursiveLock> lock(mLock);
+
+            auto it = info_VkImage.find(pCreateInfo->image);
+            if (it != info_VkImage.end() && it->second.hasExternalFormat) {
+                localCreateInfo.format = vk_format_from_android(it->second.androidFormat);
+            }
+        }
+        VkSamplerYcbcrConversionInfo localVkSamplerYcbcrConversionInfo;
+        const VkSamplerYcbcrConversionInfo* samplerYcbcrConversionInfo =
+            vk_find_struct<VkSamplerYcbcrConversionInfo>(pCreateInfo);
+        if (samplerYcbcrConversionInfo) {
+            if (samplerYcbcrConversionInfo->conversion != VK_YCBCR_CONVERSION_DO_NOTHING) {
+                localVkSamplerYcbcrConversionInfo = vk_make_orphan_copy(*samplerYcbcrConversionInfo);
+                vk_append_struct(&structChainIter, &localVkSamplerYcbcrConversionInfo);
             }
         }
 #endif