vulkan-cereal: report preferred memory index to guest

Commit 12c1c391 ("Adds an emulated memory type for AHB") broke
dEQP-VK.memory.* on Intel/Linux.  The problem seems to be the
commit reserves memory index zero for use with ColorBuffers, but
some integrated GPUs have one heap that is both host visible and
device local (at index 0).  This also likely problematic for some
AAOS platforms.

Rather than reserving memory index 0, why not sure report back to
the guest which memory index works best for ColorBuffers?  This
commit partially rolls back 12c1c391, but keeps the
representativeColorBufferMemoryTypeIndex logic and reports it back
to the guest.

BUG=215538934
TEST=check no aborts when getting caps
     dEQP-VK.memory.* passes on iGPUs again

Change-Id: I284be2c166a9a2cf072d00a3cfa49b5f7251007c
diff --git a/stream-servers/virtgpu_gfxstream_protocol.h b/stream-servers/virtgpu_gfxstream_protocol.h
index 1404476..79542a8 100644
--- a/stream-servers/virtgpu_gfxstream_protocol.h
+++ b/stream-servers/virtgpu_gfxstream_protocol.h
@@ -85,7 +85,7 @@
     uint32_t ringSize;
     uint32_t bufferSize;
 
-    uint32_t pad;
+    uint32_t colorBufferMemoryIndex;
     uint32_t padding[16];
     uint32_t deferredMapping;
 };
diff --git a/stream-servers/virtio-gpu-gfxstream-renderer.cpp b/stream-servers/virtio-gpu-gfxstream-renderer.cpp
index e8788a8..82328e8 100644
--- a/stream-servers/virtio-gpu-gfxstream-renderer.cpp
+++ b/stream-servers/virtio-gpu-gfxstream-renderer.cpp
@@ -20,6 +20,7 @@
 #include "FrameBuffer.h"
 #include "GfxStreamAgents.h"
 #include "VirtioGpuTimelines.h"
+#include "VkCommonOperations.h"
 #include "aemu/base/AlignedBuf.h"
 #include "aemu/base/ManagedDescriptor.hpp"
 #include "aemu/base/Metrics.h"
@@ -1316,6 +1317,11 @@
             capset->protocolVersion = 1;
             capset->ringSize = 12288;
             capset->bufferSize = 1048576;
+
+            auto vk_emu = gfxstream::vk::getGlobalVkEmulation();
+            if (vk_emu && vk_emu->live && vk_emu->representativeColorBufferMemoryTypeIndex) {
+                capset->colorBufferMemoryIndex = *vk_emu->representativeColorBufferMemoryTypeIndex;
+            }
         }
     }
 
diff --git a/stream-servers/vulkan/Android.bp b/stream-servers/vulkan/Android.bp
index 99d230f..7eb3f34 100644
--- a/stream-servers/vulkan/Android.bp
+++ b/stream-servers/vulkan/Android.bp
@@ -47,7 +47,6 @@
         "VkDecoderGlobalState.cpp",
         "VkDecoderSnapshot.cpp",
         "VkFormatUtils.cpp",
-        "VkGuestMemoryUtils.cpp",
         "VkReconstruction.cpp",
         "VulkanDispatch.cpp",
         "VulkanHandleMapping.cpp",
@@ -86,27 +85,3 @@
         unit_test: true,
     },
 }
-
-// Run with `atest --host gfxstream_vkguestmemoryutils_tests`
-cc_test_host {
-    name: "gfxstream_vkguestmemoryutils_tests",
-    defaults: [ "gfxstream_defaults" ],
-    srcs: [
-        "VkGuestMemoryUtils_unittest.cpp",
-    ],
-    shared_libs: [
-        "libbase",
-        "liblog",
-    ],
-    static_libs: [
-        "gfxstream_base",
-        "gfxstream_glm",
-        "gfxstream_host_common",
-        "gfxstream_vulkan_server",
-        "libgtest",
-        "libgmock",
-    ],
-    test_options: {
-        unit_test: true,
-    },
-}
diff --git a/stream-servers/vulkan/CMakeLists.txt b/stream-servers/vulkan/CMakeLists.txt
index 5aa27c9..4e0e57a 100644
--- a/stream-servers/vulkan/CMakeLists.txt
+++ b/stream-servers/vulkan/CMakeLists.txt
@@ -16,7 +16,6 @@
             VkDecoderGlobalState.cpp
             VkDecoderSnapshot.cpp
             VkFormatUtils.cpp
-            VkGuestMemoryUtils.cpp
             VkReconstruction.cpp
             VulkanDispatch.cpp
             VulkanHandleMapping.cpp
diff --git a/stream-servers/vulkan/VkDecoderGlobalState.cpp b/stream-servers/vulkan/VkDecoderGlobalState.cpp
index 7179554..e1acb1e 100644
--- a/stream-servers/vulkan/VkDecoderGlobalState.cpp
+++ b/stream-servers/vulkan/VkDecoderGlobalState.cpp
@@ -26,7 +26,6 @@
 #include "VkCommonOperations.h"
 #include "VkDecoderContext.h"
 #include "VkDecoderSnapshot.h"
-#include "VkGuestMemoryUtils.h"
 #include "VulkanDispatch.h"
 #include "VulkanStream.h"
 #include "aemu/base/ManagedDescriptor.hpp"
@@ -709,13 +708,8 @@
                     physdevInfo.props.apiVersion = kMaxSafeVersion;
                 }
 
-                VkPhysicalDeviceMemoryProperties hostMemoryProperties;
                 vk->vkGetPhysicalDeviceMemoryProperties(validPhysicalDevices[i],
-                                                        &hostMemoryProperties);
-
-                physdevInfo.memoryPropertiesHelper =
-                    std::make_unique<EmulatedPhysicalDeviceMemoryProperties>(
-                        hostMemoryProperties, *m_emu->representativeColorBufferMemoryTypeIndex);
+                                                        &physdevInfo.memoryProperties);
 
                 uint32_t queueFamilyPropCount = 0;
 
@@ -1041,18 +1035,7 @@
         auto physicalDevice = unbox_VkPhysicalDevice(boxed_physicalDevice);
         auto vk = dispatch_VkPhysicalDevice(boxed_physicalDevice);
 
-        {
-            std::lock_guard<std::recursive_mutex> lock(mLock);
-
-            auto* physicalDeviceInfo = android::base::find(mPhysdevInfo, physicalDevice);
-            if (!physicalDeviceInfo) {
-                ERR("Failed to find physical device info.");
-                return;
-            }
-
-            auto& physicalDeviceMemoryHelper = physicalDeviceInfo->memoryPropertiesHelper;
-            *pMemoryProperties = physicalDeviceMemoryHelper->getGuestMemoryProperties();
-        }
+        vk->vkGetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties);
 
         // Pick a max heap size that will work around
         // drivers that give bad suggestions (such as 0xFFFFFFFFFFFFFFFF for the heap size)
@@ -1082,17 +1065,15 @@
         auto physicalDevice = unbox_VkPhysicalDevice(boxed_physicalDevice);
         auto vk = dispatch_VkPhysicalDevice(boxed_physicalDevice);
 
-        auto* physicalDeviceInfo = android::base::find(mPhysdevInfo, physicalDevice);
-        if (!physicalDeviceInfo) {
-            return;
-        }
+        auto* physdevInfo = android::base::find(mPhysdevInfo, physicalDevice);
+        if (!physdevInfo) return;
 
         auto instance = mPhysicalDeviceToInstance[physicalDevice];
         auto* instanceInfo = android::base::find(mInstanceInfo, instance);
         if (!instanceInfo) return;
 
         if (instanceInfo->apiVersion >= VK_MAKE_VERSION(1, 1, 0) &&
-            physicalDeviceInfo->props.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) {
+            physdevInfo->props.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) {
             vk->vkGetPhysicalDeviceMemoryProperties2(physicalDevice, pMemoryProperties);
         } else if (hasInstanceExtension(instance,
                                         VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) {
@@ -1110,12 +1091,10 @@
                 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2,
                 0,
             };
+            vk->vkGetPhysicalDeviceMemoryProperties(physicalDevice,
+                                                    &pMemoryProperties->memoryProperties);
         }
 
-        auto& physicalDeviceMemoryHelper = physicalDeviceInfo->memoryPropertiesHelper;
-        pMemoryProperties->memoryProperties =
-            physicalDeviceMemoryHelper->getGuestMemoryProperties();
-
         // Pick a max heap size that will work around
         // drivers that give bad suggestions (such as 0xFFFFFFFFFFFFFFFF for the heap size)
         // plus won't break the bank on 32-bit userspace.
@@ -1579,22 +1558,11 @@
         VkResult createRes = VK_SUCCESS;
 
         if (nativeBufferANDROID) {
-            auto* physicalDevice = android::base::find(mDeviceToPhysicalDevice, device);
-            if (!physicalDevice) {
-                return VK_ERROR_DEVICE_LOST;
-            }
-
-            auto* physicalDeviceInfo = android::base::find(mPhysdevInfo, *physicalDevice);
-            if (!physicalDeviceInfo) {
-                return VK_ERROR_DEVICE_LOST;
-            }
-
-            const VkPhysicalDeviceMemoryProperties& memoryProperties =
-                physicalDeviceInfo->memoryPropertiesHelper->getHostMemoryProperties();
+            auto memProps = memPropsOfDeviceLocked(device);
 
             createRes =
                 prepareAndroidNativeBufferImage(vk, device, *pool, pCreateInfo, nativeBufferANDROID,
-                                                pAllocator, &memoryProperties, anbInfo.get());
+                                                pAllocator, memProps, anbInfo.get());
             if (createRes == VK_SUCCESS) {
                 *pImage = anbInfo->image;
             }
@@ -2468,6 +2436,7 @@
                                    descriptorCopyCount, pDescriptorCopies);
     }
 
+    // jasonjason
     VkResult on_vkCreateShaderModule(android::base::BumpPool* pool, VkDevice boxed_device,
                                      const VkShaderModuleCreateInfo* pCreateInfo,
                                      const VkAllocationCallbacks* pAllocator,
@@ -2663,24 +2632,8 @@
         auto device = unbox_VkDevice(boxed_device);
         auto vk = dispatch_VkDevice(boxed_device);
         vk->vkGetImageMemoryRequirements(device, image, pMemoryRequirements);
-
         std::lock_guard<std::recursive_mutex> lock(mLock);
         updateImageMemorySizeLocked(device, image, pMemoryRequirements);
-
-        auto* physicalDevice = android::base::find(mDeviceToPhysicalDevice, device);
-        if (!physicalDevice) {
-            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << "No physical device available for " << device;
-        }
-
-        auto* physicalDeviceInfo = android::base::find(mPhysdevInfo, *physicalDevice);
-        if (!physicalDeviceInfo) {
-            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << "No physical device info available for " << *physicalDevice;
-        }
-
-        auto& physicalDeviceMemHelper = physicalDeviceInfo->memoryPropertiesHelper;
-        physicalDeviceMemHelper->transformToGuestMemoryRequirements(pMemoryRequirements);
     }
 
     void on_vkGetImageMemoryRequirements2(android::base::BumpPool* pool, VkDevice boxed_device,
@@ -2690,19 +2643,15 @@
         auto vk = dispatch_VkDevice(boxed_device);
         std::lock_guard<std::recursive_mutex> lock(mLock);
 
-        auto* physicalDevice = android::base::find(mDeviceToPhysicalDevice, device);
-        if (!physicalDevice) {
-            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << "No physical device available for " << device;
+        auto physicalDevice = mDeviceToPhysicalDevice[device];
+        auto* physdevInfo = android::base::find(mPhysdevInfo, physicalDevice);
+        if (!physdevInfo) {
+            // If this fails, we crash, as we assume that the memory properties
+            // map should have the info.
+            // fprintf(stderr, "%s: Could not get image memory requirement for VkPhysicalDevice\n");
         }
 
-        auto* physicalDeviceInfo = android::base::find(mPhysdevInfo, *physicalDevice);
-        if (!physicalDeviceInfo) {
-            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << "No physical device info available for " << *physicalDevice;
-        }
-
-        if ((physicalDeviceInfo->props.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) &&
+        if ((physdevInfo->props.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) &&
             vk->vkGetImageMemoryRequirements2) {
             vk->vkGetImageMemoryRequirements2(device, pInfo, pMemoryRequirements);
         } else if (hasDeviceExtension(device, VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME)) {
@@ -2716,12 +2665,7 @@
             vk->vkGetImageMemoryRequirements(device, pInfo->image,
                                              &pMemoryRequirements->memoryRequirements);
         }
-
         updateImageMemorySizeLocked(device, pInfo->image, &pMemoryRequirements->memoryRequirements);
-
-        auto& physicalDeviceMemHelper = physicalDeviceInfo->memoryPropertiesHelper;
-        physicalDeviceMemHelper->transformToGuestMemoryRequirements(
-            &pMemoryRequirements->memoryRequirements);
     }
 
     void on_vkGetBufferMemoryRequirements(android::base::BumpPool* pool, VkDevice boxed_device,
@@ -2730,23 +2674,6 @@
         auto device = unbox_VkDevice(boxed_device);
         auto vk = dispatch_VkDevice(boxed_device);
         vk->vkGetBufferMemoryRequirements(device, buffer, pMemoryRequirements);
-
-        std::lock_guard<std::recursive_mutex> lock(mLock);
-
-        auto* physicalDevice = android::base::find(mDeviceToPhysicalDevice, device);
-        if (!physicalDevice) {
-            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << "No physical device available for " << device;
-        }
-
-        auto* physicalDeviceInfo = android::base::find(mPhysdevInfo, *physicalDevice);
-        if (!physicalDeviceInfo) {
-            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << "No physical device info available for " << *physicalDevice;
-        }
-
-        auto& physicalDeviceMemHelper = physicalDeviceInfo->memoryPropertiesHelper;
-        physicalDeviceMemHelper->transformToGuestMemoryRequirements(pMemoryRequirements);
     }
 
     void on_vkGetBufferMemoryRequirements2(android::base::BumpPool* pool, VkDevice boxed_device,
@@ -2783,10 +2710,6 @@
             vk->vkGetBufferMemoryRequirements(device, pInfo->buffer,
                                               &pMemoryRequirements->memoryRequirements);
         }
-
-        auto& physicalDeviceMemHelper = physicalDeviceInfo->memoryPropertiesHelper;
-        physicalDeviceMemHelper->transformToGuestMemoryRequirements(
-            &pMemoryRequirements->memoryRequirements);
     }
 
     void on_vkCmdCopyBufferToImage(android::base::BumpPool* pool,
@@ -3092,7 +3015,7 @@
         // originally created with a dedicated allocation.
         bool shouldUseDedicatedAllocInfo = dedicatedAllocInfoPtr != nullptr;
 
-        const VkImportColorBufferGOOGLE* importColorBufferInfoPtr =
+        const VkImportColorBufferGOOGLE* importCbInfoPtr =
             vk_find_struct<VkImportColorBufferGOOGLE>(pAllocateInfo);
         const VkImportBufferGOOGLE* importBufferInfoPtr =
             vk_find_struct<VkImportBufferGOOGLE>(pAllocateInfo);
@@ -3119,17 +3042,17 @@
 
         void* mappedPtr = nullptr;
         ManagedDescriptor externalMemoryHandle;
-        if (importColorBufferInfoPtr) {
+        if (importCbInfoPtr) {
             bool vulkanOnly = mGuestUsesAngle;
 
             bool colorBufferMemoryUsesDedicatedAlloc = false;
-            if (!getColorBufferAllocationInfo(importColorBufferInfoPtr->colorBuffer,
+            if (!getColorBufferAllocationInfo(importCbInfoPtr->colorBuffer,
                                               &localAllocInfo.allocationSize,
                                               &localAllocInfo.memoryTypeIndex,
                                               &colorBufferMemoryUsesDedicatedAlloc, &mappedPtr)) {
                 GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
                     << "Failed to get allocation info for ColorBuffer:"
-                    << importColorBufferInfoPtr->colorBuffer;
+                    << importCbInfoPtr->colorBuffer;
             }
 
             shouldUseDedicatedAllocInfo &= colorBufferMemoryUsesDedicatedAlloc;
@@ -3137,19 +3060,19 @@
             if (!vulkanOnly) {
                 auto fb = FrameBuffer::getFB();
                 if (fb) {
-                    fb->invalidateColorBufferForVk(importColorBufferInfoPtr->colorBuffer);
+                    fb->invalidateColorBufferForVk(importCbInfoPtr->colorBuffer);
                 }
             }
 
             if (m_emu->instanceSupportsExternalMemoryCapabilities) {
                 VK_EXT_MEMORY_HANDLE cbExtMemoryHandle =
-                    getColorBufferExtMemoryHandle(importColorBufferInfoPtr->colorBuffer);
+                    getColorBufferExtMemoryHandle(importCbInfoPtr->colorBuffer);
 
                 if (cbExtMemoryHandle == VK_EXT_MEMORY_HANDLE_INVALID) {
                     fprintf(stderr,
                             "%s: VK_ERROR_OUT_OF_DEVICE_MEMORY: "
                             "colorBuffer 0x%x does not have Vulkan external memory backing\n",
-                            __func__, importColorBufferInfoPtr->colorBuffer);
+                            __func__, importCbInfoPtr->colorBuffer);
                     return VK_ERROR_OUT_OF_DEVICE_MEMORY;
                 }
 
@@ -3162,7 +3085,9 @@
 #endif
                 vk_append_struct(&structChainIter, &importInfo);
             }
-        } else if (importBufferInfoPtr) {
+        }
+
+        if (importBufferInfoPtr) {
             bool bufferMemoryUsesDedicatedAlloc = false;
             if (!getBufferAllocationInfo(
                     importBufferInfoPtr->buffer, &localAllocInfo.allocationSize,
@@ -3201,32 +3126,32 @@
         {
             std::lock_guard<std::recursive_mutex> lock(mLock);
 
-            auto* physicalDevice = android::base::find(mDeviceToPhysicalDevice, device);
-            if (!physicalDevice) {
+            auto* physdev = android::base::find(mDeviceToPhysicalDevice, device);
+            if (!physdev) {
                 // User app gave an invalid VkDevice, but we don't really want to crash here.
                 // We should allow invalid apps.
                 return VK_ERROR_DEVICE_LOST;
             }
-            auto* physicalDeviceInfo = android::base::find(mPhysdevInfo, *physicalDevice);
-            if (!physicalDeviceInfo) {
-                GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                    << "No physical device info available for " << *physicalDevice;
-            }
-            const auto& memoryHelper = physicalDeviceInfo->memoryPropertiesHelper;
 
-            const auto hostMemoryInfoOpt =
-                (importBufferInfoPtr || importColorBufferInfoPtr)
-                    ? memoryHelper->getHostMemoryInfoFromHostMemoryTypeIndex(
-                          localAllocInfo.memoryTypeIndex)
-                    : memoryHelper->getHostMemoryInfoFromGuestMemoryTypeIndex(
-                          localAllocInfo.memoryTypeIndex);
-            if (!hostMemoryInfoOpt) {
+            auto* physdevInfo = android::base::find(mPhysdevInfo, *physdev);
+            if (!physdevInfo) {
+                // If this fails, we crash, as we assume that the memory properties map should have
+                // the info.
+                fprintf(stderr, "Error: Could not get memory properties for VkPhysicalDevice\n");
+            }
+
+            // If the memory was allocated with a type index that corresponds
+            // to a memory type that is host visible, let's also map the entire
+            // thing.
+
+            // First, check validity of the user's type index.
+            if (localAllocInfo.memoryTypeIndex >= physdevInfo->memoryProperties.memoryTypeCount) {
+                // Continue allowing invalid behavior.
                 return VK_ERROR_INCOMPATIBLE_DRIVER;
             }
-            const auto& hostMemoryInfo = *hostMemoryInfoOpt;
-
-            localAllocInfo.memoryTypeIndex = hostMemoryInfo.index;
-            memoryPropertyFlags = hostMemoryInfo.memoryType.propertyFlags;
+            memoryPropertyFlags =
+                physdevInfo->memoryProperties.memoryTypes[localAllocInfo.memoryTypeIndex]
+                    .propertyFlags;
         }
 
         if (shouldUseDedicatedAllocInfo) {
@@ -3351,8 +3276,8 @@
         memoryInfo.device = device;
         memoryInfo.memoryIndex = localAllocInfo.memoryTypeIndex;
 #ifdef VK_MVK_moltenvk
-        if (importColorBufferInfoPtr && m_emu->instanceSupportsMoltenVK) {
-            memoryInfo.mtlTexture = getColorBufferMTLTexture(importColorBufferInfoPtr->colorBuffer);
+        if (importCbInfoPtr && m_emu->instanceSupportsMoltenVK) {
+            memoryInfo.mtlTexture = getColorBufferMTLTexture(importCbInfoPtr->colorBuffer);
         }
 #endif
 
@@ -4257,8 +4182,10 @@
             return bufferCreateRes;
         }
 
-        on_vkGetBufferMemoryRequirements(pool, boxed_device, unbox_VkBuffer(*pBuffer),
-                                         pMemoryRequirements);
+        auto device = unbox_VkDevice(boxed_device);
+        auto vk = dispatch_VkDevice(boxed_device);
+
+        vk->vkGetBufferMemoryRequirements(device, unbox_VkBuffer(*pBuffer), pMemoryRequirements);
 
         return bufferCreateRes;
     }
@@ -5456,6 +5383,16 @@
         return res;
     }
 
+    VkPhysicalDeviceMemoryProperties* memPropsOfDeviceLocked(VkDevice device) {
+        auto* physdev = android::base::find(mDeviceToPhysicalDevice, device);
+        if (!physdev) return nullptr;
+
+        auto* physdevInfo = android::base::find(mPhysdevInfo, *physdev);
+        if (!physdevInfo) return nullptr;
+
+        return &physdevInfo->memoryProperties;
+    }
+
     bool getDefaultQueueForDeviceLocked(VkDevice device, VkQueue* queue, uint32_t* queueFamilyIndex,
                                         Lock** queueLock) {
         auto* deviceInfo = android::base::find(mDeviceInfo, device);
@@ -5962,7 +5899,7 @@
 
     struct PhysicalDeviceInfo {
         VkPhysicalDeviceProperties props;
-        std::unique_ptr<EmulatedPhysicalDeviceMemoryProperties> memoryPropertiesHelper;
+        VkPhysicalDeviceMemoryProperties memoryProperties;
         std::vector<VkQueueFamilyProperties> queueFamilyProperties;
         VkPhysicalDevice boxed = nullptr;
     };
diff --git a/stream-servers/vulkan/VkGuestMemoryUtils.cpp b/stream-servers/vulkan/VkGuestMemoryUtils.cpp
deleted file mode 100644
index cb0d03e..0000000
--- a/stream-servers/vulkan/VkGuestMemoryUtils.cpp
+++ /dev/null
@@ -1,111 +0,0 @@
-// Copyright (C) 2023 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "VkGuestMemoryUtils.h"
-
-#include <algorithm>
-#include <limits>
-
-#include "host-common/GfxstreamFatalError.h"
-
-namespace gfxstream {
-namespace vk {
-namespace {
-
-static constexpr const uint32_t kInvalidMemoryTypeIndex = std::numeric_limits<uint32_t>::max();
-
-}  // namespace
-
-EmulatedPhysicalDeviceMemoryProperties::EmulatedPhysicalDeviceMemoryProperties(
-    const VkPhysicalDeviceMemoryProperties& hostMemoryProperties,
-    const uint32_t hostColorBufferMemoryTypeIndex) {
-    mHostMemoryProperties = hostMemoryProperties;
-    mGuestMemoryProperties = hostMemoryProperties;
-    std::fill_n(mGuestToHostMemoryTypeIndexMap, VK_MAX_MEMORY_TYPES, kInvalidMemoryTypeIndex);
-    std::fill_n(mHostToGuestMemoryTypeIndexMap, VK_MAX_MEMORY_TYPES, kInvalidMemoryTypeIndex);
-
-    // Reserve the first memory type (index 0) for AHB backed buffers and images so
-    // that the host can control its memory properties. This ensures that the guest
-    // only sees `VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT` and will not try to map the
-    // memory.
-    if (mHostMemoryProperties.memoryTypeCount == VK_MAX_MEMORY_TYPES) {
-        GFXSTREAM_ABORT(emugl::FatalError(emugl::ABORT_REASON_OTHER))
-            << "Unable to create reserved AHB memory type.";
-    }
-    for (int32_t i = mHostMemoryProperties.memoryTypeCount; i >= 0; i--) {
-        const uint32_t hostIndex = i;
-        const uint32_t guestIndex = i + 1;
-        mGuestMemoryProperties.memoryTypes[guestIndex] =
-            mGuestMemoryProperties.memoryTypes[hostIndex];
-        mGuestToHostMemoryTypeIndexMap[guestIndex] = hostIndex;
-        mHostToGuestMemoryTypeIndexMap[hostIndex] = guestIndex;
-    }
-    mGuestMemoryProperties.memoryTypeCount += 1;
-    mGuestMemoryProperties.memoryTypes[0].propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
-    mGuestMemoryProperties.memoryTypes[0].heapIndex =
-        mHostMemoryProperties.memoryTypes[hostColorBufferMemoryTypeIndex].heapIndex;
-}
-
-std::optional<EmulatedPhysicalDeviceMemoryProperties::HostMemoryInfo>
-EmulatedPhysicalDeviceMemoryProperties::getHostMemoryInfoFromHostMemoryTypeIndex(
-    uint32_t hostMemoryTypeIndex) const {
-    if (hostMemoryTypeIndex >= mHostMemoryProperties.memoryTypeCount) {
-        return std::nullopt;
-    }
-
-    return HostMemoryInfo{
-        .index = hostMemoryTypeIndex,
-        .memoryType = mHostMemoryProperties.memoryTypes[hostMemoryTypeIndex],
-    };
-}
-
-std::optional<EmulatedPhysicalDeviceMemoryProperties::HostMemoryInfo>
-EmulatedPhysicalDeviceMemoryProperties::getHostMemoryInfoFromGuestMemoryTypeIndex(
-    uint32_t guestMemoryTypeIndex) const {
-    if (guestMemoryTypeIndex >= mGuestMemoryProperties.memoryTypeCount) {
-        return std::nullopt;
-    }
-
-    uint32_t hostMemoryTypeIndex = mGuestToHostMemoryTypeIndexMap[guestMemoryTypeIndex];
-    if (hostMemoryTypeIndex == kInvalidMemoryTypeIndex) {
-        return std::nullopt;
-    }
-
-    return getHostMemoryInfoFromHostMemoryTypeIndex(hostMemoryTypeIndex);
-}
-
-void EmulatedPhysicalDeviceMemoryProperties::transformToGuestMemoryRequirements(
-    VkMemoryRequirements* memoryRequirements) const {
-    uint32_t guestMemoryTypeBits = 0;
-
-    const uint32_t hostMemoryTypeBits = memoryRequirements->memoryTypeBits;
-    for (uint32_t hostMemoryTypeIndex = 0;
-         hostMemoryTypeIndex < mHostMemoryProperties.memoryTypeCount; hostMemoryTypeIndex++) {
-        if (!(hostMemoryTypeBits & (1u << hostMemoryTypeIndex))) {
-            continue;
-        }
-
-        uint32_t guestMemoryTypeIndex = mHostToGuestMemoryTypeIndexMap[hostMemoryTypeIndex];
-        if (guestMemoryTypeIndex == kInvalidMemoryTypeIndex) {
-            continue;
-        }
-
-        guestMemoryTypeBits |= (1u << guestMemoryTypeIndex);
-    }
-
-    memoryRequirements->memoryTypeBits = guestMemoryTypeBits;
-}
-
-}  // namespace vk
-}  // namespace gfxstream
diff --git a/stream-servers/vulkan/VkGuestMemoryUtils.h b/stream-servers/vulkan/VkGuestMemoryUtils.h
deleted file mode 100644
index 55f182b..0000000
--- a/stream-servers/vulkan/VkGuestMemoryUtils.h
+++ /dev/null
@@ -1,59 +0,0 @@
-// Copyright (C) 2023 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#pragma once
-
-#include <vulkan/vulkan.h>
-
-#include <optional>
-
-namespace gfxstream {
-namespace vk {
-
-// A physical device may have memory types that are not desirable or are not
-// supportable by the host renderer. This class helps to track the original
-// host memory types, helps to track the emulated memory types shared with the
-// guest, and helps to convert between both.
-class EmulatedPhysicalDeviceMemoryProperties {
-   public:
-    EmulatedPhysicalDeviceMemoryProperties(const VkPhysicalDeviceMemoryProperties& host,
-                                           uint32_t hostColorBufferMemoryTypeIndex);
-
-    struct HostMemoryInfo {
-        uint32_t index;
-        VkMemoryType memoryType;
-    };
-    std::optional<HostMemoryInfo> getHostMemoryInfoFromHostMemoryTypeIndex(
-        uint32_t hostMemoryTypeIndex) const;
-    std::optional<HostMemoryInfo> getHostMemoryInfoFromGuestMemoryTypeIndex(
-        uint32_t guestMemoryTypeIndex) const;
-
-    const VkPhysicalDeviceMemoryProperties& getGuestMemoryProperties() const {
-        return mGuestMemoryProperties;
-    }
-    const VkPhysicalDeviceMemoryProperties& getHostMemoryProperties() const {
-        return mHostMemoryProperties;
-    }
-
-    void transformToGuestMemoryRequirements(VkMemoryRequirements* hostMemoryRequirements) const;
-
-   private:
-    VkPhysicalDeviceMemoryProperties mGuestMemoryProperties;
-    VkPhysicalDeviceMemoryProperties mHostMemoryProperties;
-    uint32_t mGuestToHostMemoryTypeIndexMap[VK_MAX_MEMORY_TYPES];
-    uint32_t mHostToGuestMemoryTypeIndexMap[VK_MAX_MEMORY_TYPES];
-};
-
-}  // namespace vk
-}  // namespace gfxstream
diff --git a/stream-servers/vulkan/VkGuestMemoryUtils_unittest.cpp b/stream-servers/vulkan/VkGuestMemoryUtils_unittest.cpp
deleted file mode 100644
index 4828992..0000000
--- a/stream-servers/vulkan/VkGuestMemoryUtils_unittest.cpp
+++ /dev/null
@@ -1,137 +0,0 @@
-// Copyright (C) 2023 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-
-#include "VkGuestMemoryUtils.h"
-
-#include <vector>
-
-namespace gfxstream {
-namespace vk {
-namespace {
-
-using ::testing::AllOf;
-using ::testing::ElementsAreArray;
-using ::testing::Eq;
-using ::testing::ExplainMatchResult;
-using ::testing::Field;
-using ::testing::Matcher;
-
-MATCHER_P(EqsVkMemoryHeap, expected, "") {
-    return ExplainMatchResult(AllOf(Field("size", &VkMemoryHeap::size, Eq(expected.size)),
-                                    Field("flags", &VkMemoryHeap::flags, Eq(expected.flags))),
-                              arg, result_listener);
-}
-
-MATCHER_P(EqsVkMemoryType, expected, "") {
-    return ExplainMatchResult(
-        AllOf(Field("propertyFlags", &VkMemoryType::propertyFlags, Eq(expected.propertyFlags)),
-              Field("heapIndex", &VkMemoryType::heapIndex, Eq(expected.heapIndex))),
-        arg, result_listener);
-}
-
-MATCHER_P(EqsVkPhysicalDeviceMemoryProperties, expected, "") {
-    std::vector<Matcher<VkMemoryHeap>> memoryHeapsMatchers;
-    for (uint32_t i = 0; i < VK_MAX_MEMORY_HEAPS; i++) {
-        memoryHeapsMatchers.push_back(EqsVkMemoryHeap(expected.memoryHeaps[i]));
-    }
-
-    std::vector<Matcher<VkMemoryType>> memoryTypesMatchers;
-    for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
-        memoryTypesMatchers.push_back(EqsVkMemoryType(expected.memoryTypes[i]));
-    }
-
-    return ExplainMatchResult(
-        AllOf(Field("memoryTypeCount", &VkPhysicalDeviceMemoryProperties::memoryTypeCount,
-                    Eq(expected.memoryTypeCount)),
-              Field("memoryTypes", &VkPhysicalDeviceMemoryProperties::memoryTypes,
-                    ElementsAreArray(memoryTypesMatchers)),
-              Field("memoryHeapCount", &VkPhysicalDeviceMemoryProperties::memoryHeapCount,
-                    Eq(expected.memoryHeapCount)),
-              Field("memoryHeaps", &VkPhysicalDeviceMemoryProperties::memoryHeaps,
-                    ElementsAreArray(memoryHeapsMatchers))),
-        arg, result_listener);
-}
-
-TEST(VkGuestMemoryUtilsTest, ReserveAHardwareBuffer) {
-    const VkPhysicalDeviceMemoryProperties hostMemoryProperties = {
-        .memoryTypeCount = 2,
-        .memoryTypes =
-            {
-                {
-                    .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
-                    .heapIndex = 0,
-                },
-                {
-                    .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
-                    .heapIndex = 1,
-                },
-            },
-        .memoryHeapCount = 2,
-        .memoryHeaps =
-            {
-                {
-                    .size = 0x1000000,
-                    .flags = 0,
-                },
-                {
-                    .size = 0x200000,
-                    .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
-                },
-            },
-    };
-
-    EmulatedPhysicalDeviceMemoryProperties helper(hostMemoryProperties, 1);
-
-    const VkPhysicalDeviceMemoryProperties expectedGuestMemoryProperties = {
-        .memoryTypeCount = 3,
-        .memoryTypes =
-            {
-                {
-                    .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
-                    .heapIndex = 1,
-                },
-                {
-                    .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
-                    .heapIndex = 0,
-                },
-                {
-                    .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
-                    .heapIndex = 1,
-                },
-            },
-        .memoryHeapCount = 2,
-        .memoryHeaps =
-            {
-                {
-                    .size = 0x1000000,
-                    .flags = 0,
-                },
-                {
-                    .size = 0x200000,
-                    .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
-                },
-            },
-    };
-
-    const auto actualGuestMemoryProperties = helper.getGuestMemoryProperties();
-    EXPECT_THAT(actualGuestMemoryProperties,
-                EqsVkPhysicalDeviceMemoryProperties(expectedGuestMemoryProperties));
-}
-
-}  // namespace
-}  // namespace vk
-}  // namespace gfxstream