Revert "Remove non-createColorBuffer() uses of setupVkColorBuffer"

This reverts commit 9492182faa3ab107a1e222e1905a5fcbf7c0797b.

Reason for revert: Causes failures in the non-ANGLE case as VK color buffer creation is still deferred. Need to reland later with aosp/2298388 and aosp/2264929 first.

Bug: b/259171241
Bug: b/259416209
Change-Id: I06cc7a01cd7493c6c4dc2cdba7f521e7facc2c67
diff --git a/stream-servers/FrameBuffer.cpp b/stream-servers/FrameBuffer.cpp
index c81d3f5..4a230cc 100644
--- a/stream-servers/FrameBuffer.cpp
+++ b/stream-servers/FrameBuffer.cpp
@@ -1176,9 +1176,10 @@
 
     if (m_displayVk || m_guestUsesAngle) {
         if (!goldfish_vk::setupVkColorBuffer(
-                handle,
-                m_guestUsesAngle /* not vulkan only */,
-                VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT /* memory property */)) {
+            handle,
+            m_guestUsesAngle /* not vulkan only */,
+            VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT /* memory property */,
+            nullptr /* exported */)) {
             GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
                 << "Failed to set up color buffer, Format:" << p_internalFormat
                 << " Width:" << p_width << " Height:" << p_height;
diff --git a/stream-servers/RenderControl.cpp b/stream-servers/RenderControl.cpp
index 377a209..20803e2 100644
--- a/stream-servers/RenderControl.cpp
+++ b/stream-servers/RenderControl.cpp
@@ -1400,10 +1400,25 @@
 static int rcSetColorBufferVulkanMode2(uint32_t colorBuffer,
                                        uint32_t mode,
                                        uint32_t memoryProperty) {
+    if (!goldfish_vk::isColorBufferVulkanCompatible(colorBuffer)) {
+        fprintf(stderr,
+                "%s: error: colorBuffer 0x%x is not Vulkan compatible\n",
+                __func__, colorBuffer);
+        return -1;
+    }
+
 #define VULKAN_MODE_VULKAN_ONLY 1
 
     bool modeIsVulkanOnly = mode == VULKAN_MODE_VULKAN_ONLY;
 
+    if (!goldfish_vk::setupVkColorBuffer(colorBuffer, modeIsVulkanOnly,
+                                         memoryProperty)) {
+        fprintf(stderr,
+                "%s: error: failed to create VkImage for colorBuffer 0x%x\n",
+                __func__, colorBuffer);
+        return -1;
+    }
+
     if (!goldfish_vk::setColorBufferVulkanMode(colorBuffer, mode)) {
         fprintf(stderr,
                 "%s: error: failed to set Vulkan mode for colorBuffer 0x%x\n",
diff --git a/stream-servers/tests/Vulkan_unittest.cpp b/stream-servers/tests/Vulkan_unittest.cpp
index ff424ce..d4284fe 100644
--- a/stream-servers/tests/Vulkan_unittest.cpp
+++ b/stream-servers/tests/Vulkan_unittest.cpp
@@ -573,24 +573,17 @@
     }
 
     // Create a color buffer with the target memory property flags.
+    uint32_t allocatedTypeIndex = 0u;
     HandleType colorBuffer = mFb->createColorBuffer(
             mWidth, mHeight, GL_RGBA, FRAMEWORK_FORMAT_GL_COMPATIBLE);
+    ASSERT_NE(colorBuffer, 0u);
     EXPECT_TRUE(goldfish_vk::setupVkColorBuffer(
-            colorBuffer,
-            true, /* vulkanOnly */
-            static_cast<uint32_t>(kTargetMemoryPropertyFlags)));
-
-    uint32_t allocatedTypeIndex = 0u;
-    EXPECT_TRUE(goldfish_vk::getColorBufferAllocationInfo(
-            colorBuffer,
-            nullptr,
-            &allocatedTypeIndex,
-            nullptr));
-
+            colorBuffer, true, /* vulkanOnly */
+            static_cast<uint32_t>(kTargetMemoryPropertyFlags), nullptr, nullptr,
+            &allocatedTypeIndex));
     EXPECT_TRUE(vkEmulation->deviceInfo.memProps.memoryTypes[allocatedTypeIndex]
                         .propertyFlags &
                 kTargetMemoryPropertyFlags);
-
     EXPECT_TRUE(goldfish_vk::teardownVkColorBuffer(colorBuffer));
     mFb->closeColorBuffer(colorBuffer);
 }
diff --git a/stream-servers/vulkan/VkAndroidNativeBuffer.cpp b/stream-servers/vulkan/VkAndroidNativeBuffer.cpp
index 096cc8c..46c7f02 100644
--- a/stream-servers/vulkan/VkAndroidNativeBuffer.cpp
+++ b/stream-servers/vulkan/VkAndroidNativeBuffer.cpp
@@ -134,6 +134,7 @@
     out->stride = nativeBufferANDROID->stride;
     out->colorBufferHandle = *(nativeBufferANDROID->handle);
 
+    bool colorBufferVulkanCompatible = isColorBufferVulkanCompatible(out->colorBufferHandle);
     bool externalMemoryCompatible = false;
 
     auto emu = getGlobalVkEmulation();
@@ -143,12 +144,14 @@
     }
 
     bool colorBufferExportedToGl = false;
-    if (!isColorBufferExportedToGl(out->colorBufferHandle, &colorBufferExportedToGl)) {
-        VK_ANB_ERR("Failed to query if ColorBuffer:%d exported to GL.", out->colorBufferHandle);
-        return VK_ERROR_INITIALIZATION_FAILED;
-    }
-
-    if (externalMemoryCompatible) {
+    if (colorBufferVulkanCompatible && externalMemoryCompatible) {
+        if (!setupVkColorBuffer(out->colorBufferHandle, emu->guestUsesAngle,
+                                0u /* memoryProperty */, &colorBufferExportedToGl)) {
+            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
+                << "Failed to create vk color buffer. format:" << pCreateInfo->format
+                << " width:" << out->extent.width << " height:" << out->extent.height
+                << " depth:" << out->extent.depth;
+        }
         releaseColorBufferForGuestUse(out->colorBufferHandle);
         out->externallyBacked = true;
     }
diff --git a/stream-servers/vulkan/VkCommonOperations.cpp b/stream-servers/vulkan/VkCommonOperations.cpp
index b8d39d6..70929f7 100644
--- a/stream-servers/vulkan/VkCommonOperations.cpp
+++ b/stream-servers/vulkan/VkCommonOperations.cpp
@@ -1486,8 +1486,8 @@
     return true;
 }
 
-static VkFormat glFormat2VkFormat(GLint internalFormat) {
-    switch (internalFormat) {
+static VkFormat glFormat2VkFormat(GLint internalformat) {
+    switch (internalformat) {
         case GL_R8:
         case GL_LUMINANCE:
             return VK_FORMAT_R8_UNORM;
@@ -1521,7 +1521,7 @@
             return VK_FORMAT_R8G8_UNORM;
         default:
             VK_COMMON_ERROR("Unhandled format %d, falling back to VK_FORMAT_R8G8B8A8_UNORM",
-                            internalFormat);
+                            internalformat);
             return VK_FORMAT_R8G8B8A8_UNORM;
     }
 };
@@ -1548,54 +1548,6 @@
     return false;
 }
 
-bool isColorBufferExportedToGl(uint32_t colorBufferHandle, bool* exported) {
-    if (!sVkEmulation || !sVkEmulation->live) {
-        GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-            << "Vulkan emulation not available.";
-    }
-
-    AutoLock lock(sVkEmulationLock);
-
-    auto info = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle);
-    if (!info) {
-        return false;
-    }
-
-    *exported = info->glExported;
-    return true;
-}
-
-bool getColorBufferAllocationInfo(uint32_t colorBufferHandle,
-                                  VkDeviceSize* outSize,
-                                  uint32_t* outMemoryTypeIndex,
-                                  void** outMappedPtr) {
-    if (!sVkEmulation || !sVkEmulation->live) {
-        GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-            << "Vulkan emulation not available.";
-    }
-
-    AutoLock lock(sVkEmulationLock);
-
-    auto info = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle);
-    if (!info) {
-        return false;
-    }
-
-    if (outSize) {
-        *outSize = info->memory.size;
-    }
-
-    if (outMemoryTypeIndex) {
-        *outMemoryTypeIndex = info->memory.typeIndex;
-    }
-
-    if (outMappedPtr) {
-        *outMappedPtr = info->memory.mappedPtr;
-    }
-
-    return true;
-}
-
 static uint32_t lastGoodTypeIndex(uint32_t indices) {
     for (int32_t i = 31; i >= 0; --i) {
         if (indices & (1 << i)) {
@@ -1707,17 +1659,21 @@
 // We should make it so the guest can only allocate external images/
 // buffers of one type index for image and one type index for buffer
 // to begin with, via filtering from the host.
-bool setupVkColorBuffer(uint32_t colorBufferHandle, bool vulkanOnly, uint32_t memoryProperty) {
+bool setupVkColorBuffer(uint32_t colorBufferHandle, bool vulkanOnly, uint32_t memoryProperty,
+                        bool* exported, VkDeviceSize* allocSize, uint32_t* typeIndex,
+                        void** mappedPtr) {
     if (!isColorBufferVulkanCompatible(colorBufferHandle)) return false;
 
+    auto vk = sVkEmulation->dvk;
+
     auto fb = FrameBuffer::getFB();
 
     int width;
     int height;
-    GLint internalFormat;
+    GLint internalformat;
     FrameworkFormat frameworkFormat;
 
-    if (!fb->getColorBufferInfo(colorBufferHandle, &width, &height, &internalFormat,
+    if (!fb->getColorBufferInfo(colorBufferHandle, &width, &height, &internalformat,
                                 &frameworkFormat)) {
         return false;
     }
@@ -1728,6 +1684,18 @@
 
     // Already setup
     if (infoPtr) {
+        // Setting exported is required for on_vkCreateImage backed by
+        // an AHardwareBuffer.
+        if (exported) *exported = infoPtr->glExported;
+        // Update the allocation size to what the host driver wanted, or we
+        // might get VK_ERROR_OUT_OF_DEVICE_MEMORY and a host crash
+        if (allocSize) *allocSize = infoPtr->memory.size;
+        // Update the type index to what the host driver wanted, or we might
+        // get VK_ERROR_DEVICE_LOST
+        if (typeIndex) *typeIndex = infoPtr->memory.typeIndex;
+        // Update the mappedPtr to what the host driver wanted, otherwise we
+        // may map the same memory twice.
+        if (mappedPtr) *mappedPtr = infoPtr->memory.mappedPtr;
         return true;
     }
 
@@ -1735,7 +1703,7 @@
     bool glCompatible = (frameworkFormat == FRAMEWORK_FORMAT_GL_COMPATIBLE);
     switch (frameworkFormat) {
         case FrameworkFormat::FRAMEWORK_FORMAT_GL_COMPATIBLE:
-            vkFormat = glFormat2VkFormat(internalFormat);
+            vkFormat = glFormat2VkFormat(internalformat);
             break;
         case FrameworkFormat::FRAMEWORK_FORMAT_NV12:
             vkFormat = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
@@ -1748,8 +1716,8 @@
             vkFormat = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
             break;
         default:
-            VK_COMMON_ERROR("WARNING: unhandled framework format %d\n", frameworkFormat);
-            vkFormat = glFormat2VkFormat(internalFormat);
+            vkFormat = glFormat2VkFormat(internalformat);
+            fprintf(stderr, "WARNING: unsupported framework format %d\n", frameworkFormat);
             break;
     }
 
@@ -1787,8 +1755,6 @@
 
     imageCi->pNext = extImageCiPtr;
 
-    auto vk = sVkEmulation->dvk;
-
     VkResult createRes =
         vk->vkCreateImage(sVkEmulation->device, imageCi.get(), nullptr, &res.image);
     if (createRes != VK_SUCCESS) {
@@ -1904,6 +1870,11 @@
         res.vulkanMode = VkEmulation::VulkanMode::VulkanOnly;
     }
 
+    if (exported) *exported = res.glExported;
+    if (allocSize) *allocSize = res.memory.size;
+    if (typeIndex) *typeIndex = res.memory.typeIndex;
+    if (mappedPtr) *mappedPtr = res.memory.mappedPtr;
+
     sVkEmulation->colorBuffers[colorBufferHandle] = res;
     return true;
 }
@@ -2454,32 +2425,6 @@
     return memoryInfoPtr->pageOffset;
 }
 
-bool getBufferAllocationInfo(uint32_t bufferHandle,
-                             VkDeviceSize* outSize,
-                             uint32_t* outMemoryTypeIndex) {
-    if (!sVkEmulation || !sVkEmulation->live) {
-        GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-            << "Vulkan emulation not available.";
-    }
-
-    AutoLock lock(sVkEmulationLock);
-
-    auto info = android::base::find(sVkEmulation->buffers, bufferHandle);
-    if (!info) {
-        return false;
-    }
-
-    if (outSize) {
-        *outSize = info->memory.size;
-    }
-
-    if (outMemoryTypeIndex) {
-        *outMemoryTypeIndex = info->memory.typeIndex;
-    }
-
-    return true;
-}
-
 bool setupVkBuffer(uint32_t bufferHandle, bool vulkanOnly, uint32_t memoryProperty, bool* exported,
                    VkDeviceSize* allocSize, uint32_t* typeIndex) {
     if (vulkanOnly == false) {
diff --git a/stream-servers/vulkan/VkCommonOperations.h b/stream-servers/vulkan/VkCommonOperations.h
index d3e6fef..e825816 100644
--- a/stream-servers/vulkan/VkCommonOperations.h
+++ b/stream-servers/vulkan/VkCommonOperations.h
@@ -13,7 +13,6 @@
 // limitations under the License.
 #pragma once
 
-#include <GLES2/gl2.h>
 #include <vulkan/vulkan.h>
 
 #include <atomic>
@@ -26,7 +25,6 @@
 #include "BorrowedImageVk.h"
 #include "CompositorVk.h"
 #include "DisplayVk.h"
-#include "FrameworkFormats.h"
 #include "aemu/base/synchronization/Lock.h"
 #include "aemu/base/ManagedDescriptor.hpp"
 #include "aemu/base/Optional.h"
@@ -402,19 +400,17 @@
 
 // ColorBuffer operations
 
-bool isColorBufferExportedToGl(uint32_t colorBufferHandle, bool* exported);
-
-bool getColorBufferAllocationInfo(uint32_t colorBufferHandle,
-                                  VkDeviceSize* outSize ,
-                                  uint32_t* outMemoryTypeIndex,
-                                  void** outMappedPtr);
+bool isColorBufferVulkanCompatible(uint32_t colorBufferHandle);
 
 std::unique_ptr<VkImageCreateInfo> generateColorBufferVkImageCreateInfo(VkFormat format,
                                                                         uint32_t width,
                                                                         uint32_t height,
                                                                         VkImageTiling tiling);
 
-bool setupVkColorBuffer(uint32_t colorBufferHandle, bool vulkanOnly, uint32_t memoryProperty);
+bool setupVkColorBuffer(uint32_t colorBufferHandle, bool vulkanOnly = false,
+                        uint32_t memoryProperty = 0, bool* exported = nullptr,
+                        VkDeviceSize* allocSize = nullptr, uint32_t* typeIndex = nullptr,
+                        void** mappedPtr = nullptr);
 bool teardownVkColorBuffer(uint32_t colorBufferHandle);
 VkEmulation::ColorBufferInfo getColorBufferInfo(uint32_t colorBufferHandle);
 VK_EXT_MEMORY_HANDLE getColorBufferExtMemoryHandle(uint32_t colorBufferHandle);
@@ -435,9 +431,6 @@
                                       uint32_t w, uint32_t h, const void* pixels);
 
 // Data buffer operations
-bool getBufferAllocationInfo(uint32_t bufferHandle,
-                             VkDeviceSize* outSize,
-                             uint32_t* outMemoryTypeIndex);
 
 bool setupVkBuffer(uint32_t bufferHandle, bool vulkanOnly = false, uint32_t memoryProperty = 0,
                    bool* exported = nullptr, VkDeviceSize* allocSize = nullptr,
diff --git a/stream-servers/vulkan/VkDecoderGlobalState.cpp b/stream-servers/vulkan/VkDecoderGlobalState.cpp
index 0129683..35c0998 100644
--- a/stream-servers/vulkan/VkDecoderGlobalState.cpp
+++ b/stream-servers/vulkan/VkDecoderGlobalState.cpp
@@ -3057,12 +3057,14 @@
         if (importCbInfoPtr) {
             bool vulkanOnly = mGuestUsesAngle;
 
-            if (!getColorBufferAllocationInfo(importCbInfoPtr->colorBuffer,
-                                              &localAllocInfo.allocationSize,
-                                              &localAllocInfo.memoryTypeIndex,
-                                              &mappedPtr)) {
-                ERR("Failed to get ColorBuffer:%d allocation info.", importCbInfoPtr->colorBuffer);
-                return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+            // Ensure color buffer has Vulkan backing.
+            if (!setupVkColorBuffer(
+                importCbInfoPtr->colorBuffer, vulkanOnly, memoryPropertyFlags, nullptr,
+                // Modify the allocation size and type index
+                // to suit the resulting image memory size.
+                &localAllocInfo.allocationSize, &localAllocInfo.memoryTypeIndex, &mappedPtr)) {
+                GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
+                    << "Failed to set up vk color buffer.";
             }
 
             if (!vulkanOnly) {
@@ -3093,12 +3095,12 @@
         }
 
         if (importBufferInfoPtr) {
-            if (!getBufferAllocationInfo(importBufferInfoPtr->buffer,
-                                         &localAllocInfo.allocationSize,
-                                         &localAllocInfo.memoryTypeIndex)) {
-                ERR("Failed to get Buffer:%d allocation info.", importBufferInfoPtr->buffer);
-                return VK_ERROR_OUT_OF_DEVICE_MEMORY;
-            }
+            // Ensure buffer has Vulkan backing.
+            setupVkBuffer(importBufferInfoPtr->buffer, true /* Buffers are Vulkan only */,
+                          memoryPropertyFlags, nullptr,
+                          // Modify the allocation size and type index
+                          // to suit the resulting image memory size.
+                          &localAllocInfo.allocationSize, &localAllocInfo.memoryTypeIndex);
 
             if (m_emu->instanceSupportsExternalMemoryCapabilities) {
                 VK_EXT_MEMORY_HANDLE bufferExtMemoryHandle =
@@ -3606,18 +3608,22 @@
         return VK_SUCCESS;
     }
 
-    VkResult on_vkRegisterImageColorBufferGOOGLE(android::base::BumpPool*, VkDevice, VkImage,
-                                                 uint32_t) {
-        GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-            << "Unimplemented deprecated vkRegisterImageColorBufferGOOGLE() called.";
-        return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+    VkResult on_vkRegisterImageColorBufferGOOGLE(android::base::BumpPool* pool, VkDevice device,
+                                                 VkImage image, uint32_t colorBuffer) {
+        (void)image;
+
+        bool success = setupVkColorBuffer(colorBuffer);
+
+        return success ? VK_SUCCESS : VK_ERROR_OUT_OF_DEVICE_MEMORY;
     }
 
-    VkResult on_vkRegisterBufferColorBufferGOOGLE(android::base::BumpPool* pool, VkDevice,
-                                                  VkBuffer, uint32_t) {
-        GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-            << "Unimplemented deprecated on_vkRegisterBufferColorBufferGOOGLE() called.";
-        return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+    VkResult on_vkRegisterBufferColorBufferGOOGLE(android::base::BumpPool* pool, VkDevice device,
+                                                  VkBuffer buffer, uint32_t colorBuffer) {
+        (void)buffer;
+
+        bool success = setupVkColorBuffer(colorBuffer);
+
+        return success ? VK_SUCCESS : VK_ERROR_OUT_OF_DEVICE_MEMORY;
     }
 
     VkResult on_vkAllocateCommandBuffers(android::base::BumpPool* pool, VkDevice boxed_device,