[vulkan] Refactor all custom logic into ResourceTracker

bug: 111137294

Taking new generated code that lets us consolidate all custom logic into
ResourceTracker. Resources.cpp now contains only trivial definitions.

Change-Id: I5f7ef0237004ccb7b7229c9eedc852f7d0490079
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index 0bb42b2..2a572fe 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -21,9 +21,14 @@
 #include "android/base/AlignedBuf.h"
 #include "android/base/synchronization/AndroidLock.h"
 
+#include "gralloc_cb.h"
+#include "goldfish_vk_private_defs.h"
+
 #include <unordered_map>
 
 #include <log/log.h>
+#include <stdlib.h>
+#include <sync/sync.h>
 
 #define RESOURCE_TRACKER_DEBUG 0
 
@@ -110,8 +115,10 @@
     };
 
     struct VkDeviceMemory_Info {
-        VkDeviceSize allocationSize;
-        uint32_t memoryTypeIndex;
+        VkDeviceSize allocationSize = 0;
+        VkDeviceSize mappedSize = 0;
+        uint8_t* mappedPtr = nullptr;
+        uint32_t memoryTypeIndex = 0;
     };
 
 #define HANDLE_REGISTER_IMPL_IMPL(type) \
@@ -120,12 +127,31 @@
         AutoLock lock(mLock); \
         info_##type[obj] = type##_Info(); \
     } \
+
+#define HANDLE_UNREGISTER_IMPL_IMPL(type) \
     void unregister_##type(type obj) { \
         AutoLock lock(mLock); \
         info_##type.erase(obj); \
     } \
 
     GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_IMPL_IMPL)
+    GOLDFISH_VK_LIST_TRIVIAL_HANDLE_TYPES(HANDLE_UNREGISTER_IMPL_IMPL)
+
+    void unregister_VkDevice(VkDevice device) {
+        AutoLock lock(mLock);
+        info_VkDevice.erase(device);
+    }
+
+    void unregister_VkDeviceMemory(VkDeviceMemory mem) {
+        AutoLock lock(mLock);
+        auto it = info_VkDeviceMemory.find(mem);
+        if (it == info_VkDeviceMemory.end()) return;
+
+        auto& memInfo = it->second;
+        if (memInfo.mappedPtr) {
+            aligned_buf_free(memInfo.mappedPtr);
+        }
+    }
 
     void setDeviceInfo(VkDevice device,
                        VkPhysicalDevice physdev,
@@ -138,6 +164,19 @@
         info.memProps = memProps;
     }
 
+    void setDeviceMemoryInfo(VkDeviceMemory memory,
+                             VkDeviceSize allocationSize,
+                             VkDeviceSize mappedSize,
+                             uint8_t* ptr,
+                             uint32_t memoryTypeIndex) {
+        AutoLock lock(mLock);
+        auto& info = info_VkDeviceMemory[memory];
+        info.allocationSize = allocationSize;
+        info.mappedSize = mappedSize;
+        info.mappedPtr = ptr;
+        info.memoryTypeIndex = memoryTypeIndex;
+    }
+
     bool isMemoryTypeHostVisible(VkDevice device, uint32_t typeIndex) const {
         AutoLock lock(mLock);
         const auto it = info_VkDevice.find(device);
@@ -145,10 +184,28 @@
         if (it == info_VkDevice.end()) return false;
 
         const auto& info = it->second;
-        return info.memProps.memoryTypes[typeIndex].propertyFlags & 
+        return info.memProps.memoryTypes[typeIndex].propertyFlags &
                VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
     }
 
+    uint8_t* getMappedPointer(VkDeviceMemory memory) {
+        AutoLock lock(mLock);
+        const auto it = info_VkDeviceMemory.find(memory);
+        if (it == info_VkDeviceMemory.end()) return nullptr;
+
+        const auto& info = it->second;
+        return info.mappedPtr;
+    }
+
+    VkDeviceSize getMappedSize(VkDeviceMemory memory) {
+        AutoLock lock(mLock);
+        const auto it = info_VkDeviceMemory.find(memory);
+        if (it == info_VkDeviceMemory.end()) return 0;
+
+        const auto& info = it->second;
+        return info.mappedSize;
+    }
+
     VkDeviceSize getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const {
         AutoLock lock(mLock);
         const auto it = info_VkDevice.find(device);
@@ -162,6 +219,52 @@
         return atoms * nonCoherentAtomSize;
     }
 
+    bool isValidMemoryRange(const VkMappedMemoryRange& range) const {
+        AutoLock lock(mLock);
+        const auto it = info_VkDeviceMemory.find(range.memory);
+        if (it == info_VkDeviceMemory.end()) return false;
+        const auto& info = it->second;
+
+        if (!info.mappedPtr) return false;
+
+        VkDeviceSize offset = range.offset;
+        VkDeviceSize size = range.size;
+
+        if (size == VK_WHOLE_SIZE) {
+            return offset <= info.mappedSize;
+        }
+
+        return offset + size <= info.mappedSize;
+    }
+
+    VkResult on_vkEnumerateInstanceVersion(
+        void*,
+        VkResult,
+        uint32_t* apiVersion) {
+        if (apiVersion) {
+            *apiVersion = VK_MAKE_VERSION(1, 0, 0);
+        }
+        return VK_SUCCESS;
+    }
+
+    VkResult on_vkEnumerateDeviceExtensionProperties(
+        void*,
+        VkResult,
+        VkPhysicalDevice,
+        const char*,
+        uint32_t* pPropertyCount,
+        VkExtensionProperties*) {
+        *pPropertyCount = 0;
+        return VK_SUCCESS;
+    }
+
+    void on_vkGetPhysicalDeviceProperties2(
+        void*,
+        VkPhysicalDevice,
+        VkPhysicalDeviceProperties2*) {
+        // no-op
+    }
+
     VkResult on_vkCreateDevice(
         void* context,
         VkResult input_result,
@@ -194,30 +297,97 @@
 
         if (input_result != VK_SUCCESS) return input_result;
 
-        // Assumes pMemory has already been allocated.
-        goldfish_VkDeviceMemory* mem = as_goldfish_VkDeviceMemory(*pMemory);
-        VkDeviceSize size = pAllocateInfo->allocationSize;
+        VkDeviceSize allocationSize = pAllocateInfo->allocationSize;
+        VkDeviceSize mappedSize = getNonCoherentExtendedSize(device, allocationSize);
+        uint8_t* mappedPtr = nullptr;
 
-        // assume memory is not host visible.
-        mem->ptr = nullptr;
-        mem->size = size;
-        mem->mappedSize = getNonCoherentExtendedSize(device, size);
-
-        if (!isMemoryTypeHostVisible(device, pAllocateInfo->memoryTypeIndex)) {
-            return input_result;
+        if (isMemoryTypeHostVisible(device, pAllocateInfo->memoryTypeIndex)) {
+            mappedPtr = (uint8_t*)aligned_buf_alloc(4096, mappedSize);
+            D("host visible alloc: size 0x%llx host ptr %p mapped size 0x%llx",
+              (unsigned long long)size, mem->ptr,
+              (unsigned long long)mem->mappedSize);
         }
 
-        // This is a strict alignment; we do not expect any
-        // actual device to have more stringent requirements
-        // than this.
-        mem->ptr = (uint8_t*)aligned_buf_alloc(4096, mem->mappedSize);
-        D("host visible alloc: size 0x%llx host ptr %p mapped size 0x%llx",
-          (unsigned long long)size, mem->ptr,
-          (unsigned long long)mem->mappedSize);
+        setDeviceMemoryInfo(
+            *pMemory, allocationSize, mappedSize, mappedPtr,
+            pAllocateInfo->memoryTypeIndex);
 
         return input_result;
     }
 
+    VkResult on_vkMapMemory(
+        void*,
+        VkResult host_result,
+        VkDevice,
+        VkDeviceMemory memory,
+        VkDeviceSize offset,
+        VkDeviceSize size,
+        VkMemoryMapFlags,
+        void** ppData) {
+
+        if (host_result != VK_SUCCESS) return host_result;
+
+        AutoLock lock(mLock);
+
+        auto it = info_VkDeviceMemory.find(memory);
+        if (it == info_VkDeviceMemory.end()) return VK_ERROR_MEMORY_MAP_FAILED;
+
+        auto& info = it->second;
+
+        if (!info.mappedPtr) return VK_ERROR_MEMORY_MAP_FAILED;
+
+        if (size != VK_WHOLE_SIZE &&
+            (info.mappedPtr + offset + size > info.mappedPtr + info.allocationSize)) {
+            return VK_ERROR_MEMORY_MAP_FAILED;
+        }
+
+        *ppData = info.mappedPtr + offset;
+
+        return host_result;
+    }
+
+    void on_vkUnmapMemory(
+        void*,
+        VkDevice,
+        VkDeviceMemory) {
+        // no-op
+    }
+
+    void unwrap_VkNativeBufferANDROID(
+        const VkImageCreateInfo* pCreateInfo,
+        VkImageCreateInfo* local_pCreateInfo) {
+
+        if (!pCreateInfo->pNext) return;
+
+        const VkNativeBufferANDROID* nativeInfo =
+            reinterpret_cast<const VkNativeBufferANDROID*>(pCreateInfo->pNext);
+
+        if (VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID != nativeInfo->sType) {
+            return;
+        }
+
+        const cb_handle_t* cb_handle =
+            reinterpret_cast<const cb_handle_t*>(nativeInfo->handle);
+
+        if (!cb_handle) return;
+
+        VkNativeBufferANDROID* nativeInfoOut =
+            reinterpret_cast<VkNativeBufferANDROID*>(local_pCreateInfo);
+
+        if (!nativeInfoOut->handle) {
+            ALOGE("FATAL: Local native buffer info not properly allocated!");
+            abort();
+        }
+
+        *(uint32_t*)(nativeInfoOut->handle) = cb_handle->hostHandle;
+    }
+
+    void unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int*) {
+        if (fd != -1) {
+            sync_wait(fd, 3000);
+        }
+    }
+
 private:
     mutable Lock mLock;
 
@@ -269,10 +439,47 @@
     return mImpl->isMemoryTypeHostVisible(device, typeIndex);
 }
 
+uint8_t* ResourceTracker::getMappedPointer(VkDeviceMemory memory) {
+    return mImpl->getMappedPointer(memory);
+}
+
+VkDeviceSize ResourceTracker::getMappedSize(VkDeviceMemory memory) {
+    return mImpl->getMappedSize(memory);
+}
+
 VkDeviceSize ResourceTracker::getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const {
     return mImpl->getNonCoherentExtendedSize(device, basicSize);
 }
 
+bool ResourceTracker::isValidMemoryRange(const VkMappedMemoryRange& range) const {
+    return mImpl->isValidMemoryRange(range);
+}
+
+VkResult ResourceTracker::on_vkEnumerateInstanceVersion(
+    void* context,
+    VkResult input_result,
+    uint32_t* apiVersion) {
+    return mImpl->on_vkEnumerateInstanceVersion(context, input_result, apiVersion);
+}
+
+VkResult ResourceTracker::on_vkEnumerateDeviceExtensionProperties(
+    void* context,
+    VkResult input_result,
+    VkPhysicalDevice physicalDevice,
+    const char* pLayerName,
+    uint32_t* pPropertyCount,
+    VkExtensionProperties* pProperties) {
+    return mImpl->on_vkEnumerateDeviceExtensionProperties(
+        context, input_result, physicalDevice, pLayerName, pPropertyCount, pProperties);
+}
+
+void ResourceTracker::on_vkGetPhysicalDeviceProperties2(
+    void* context,
+    VkPhysicalDevice physicalDevice,
+    VkPhysicalDeviceProperties2* pProperties) {
+    mImpl->on_vkGetPhysicalDeviceProperties2(context, physicalDevice, pProperties);
+}
+
 VkResult ResourceTracker::on_vkCreateDevice(
     void* context,
     VkResult input_result,
@@ -295,4 +502,34 @@
         context, input_result, device, pAllocateInfo, pAllocator, pMemory);
 }
 
+VkResult ResourceTracker::on_vkMapMemory(
+    void* context,
+    VkResult input_result,
+    VkDevice device,
+    VkDeviceMemory memory,
+    VkDeviceSize offset,
+    VkDeviceSize size,
+    VkMemoryMapFlags flags,
+    void** ppData) {
+    return mImpl->on_vkMapMemory(
+        context, input_result, device, memory, offset, size, flags, ppData);
+}
+
+void ResourceTracker::on_vkUnmapMemory(
+    void* context,
+    VkDevice device,
+    VkDeviceMemory memory) {
+    mImpl->on_vkUnmapMemory(context, device, memory);
+}
+
+void ResourceTracker::unwrap_VkNativeBufferANDROID(
+    const VkImageCreateInfo* pCreateInfo,
+    VkImageCreateInfo* local_pCreateInfo) {
+    mImpl->unwrap_VkNativeBufferANDROID(pCreateInfo, local_pCreateInfo);
+}
+
+void ResourceTracker::unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out) {
+    mImpl->unwrap_vkAcquireImageANDROID_nativeFenceFd(fd, fd_out);
+}
+
 } // namespace goldfish_vk
diff --git a/system/vulkan_enc/ResourceTracker.h b/system/vulkan_enc/ResourceTracker.h
index 65a1221..9be37c8 100644
--- a/system/vulkan_enc/ResourceTracker.h
+++ b/system/vulkan_enc/ResourceTracker.h
@@ -39,6 +39,22 @@
 
     GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_DECL)
 
+    VkResult on_vkEnumerateInstanceVersion(
+        void* context,
+        VkResult input_result,
+        uint32_t* apiVersion) override;
+    VkResult on_vkEnumerateDeviceExtensionProperties(
+        void* context,
+        VkResult input_result,
+        VkPhysicalDevice physicalDevice,
+        const char* pLayerName,
+        uint32_t* pPropertyCount,
+        VkExtensionProperties* pProperties) override;
+    void on_vkGetPhysicalDeviceProperties2(
+        void* context,
+        VkPhysicalDevice physicalDevice,
+        VkPhysicalDeviceProperties2* pProperties) override;
+
     VkResult on_vkCreateDevice(
         void* context,
         VkResult input_result,
@@ -55,12 +71,33 @@
         const VkAllocationCallbacks* pAllocator,
         VkDeviceMemory* pMemory) override;
 
+    VkResult on_vkMapMemory(
+        void* context,
+        VkResult input_result,
+        VkDevice device,
+        VkDeviceMemory memory,
+        VkDeviceSize offset,
+        VkDeviceSize size,
+        VkMemoryMapFlags,
+        void** ppData);
+
+    void on_vkUnmapMemory(
+        void* context,
+        VkDevice device,
+        VkDeviceMemory memory) override;
+
+    void unwrap_VkNativeBufferANDROID(
+        const VkImageCreateInfo* pCreateInfo,
+        VkImageCreateInfo* local_pCreateInfo);
+    void unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out);
+
     void setDeviceInfo(VkDevice device, VkPhysicalDevice physdev, VkPhysicalDeviceProperties props, VkPhysicalDeviceMemoryProperties memProps);
     bool isMemoryTypeHostVisible(VkDevice device, uint32_t typeIndex) const;
+    uint8_t* getMappedPointer(VkDeviceMemory memory);
+    VkDeviceSize getMappedSize(VkDeviceMemory memory);
     VkDeviceSize getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const;
-    bool isValidMemoryRange(
-        VkDevice device,
-        const VkMappedMemoryRange& range) const;
+    bool isValidMemoryRange(const VkMappedMemoryRange& range) const;
+
   private:
     class Impl;
     std::unique_ptr<Impl> mImpl;
diff --git a/system/vulkan_enc/Resources.cpp b/system/vulkan_enc/Resources.cpp
index c2b972e..a0af80d 100644
--- a/system/vulkan_enc/Resources.cpp
+++ b/system/vulkan_enc/Resources.cpp
@@ -12,20 +12,9 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 #include "Resources.h"
-#include "ResourceTracker.h"
-#include "VkEncoder.h"
-
-#include "gralloc_cb.h"
-#include "goldfish_vk_private_defs.h"
-
-#include "android/base/AlignedBuf.h"
 
 #include <log/log.h>
 #include <stdlib.h>
-#include <sync/sync.h>
-
-using android::aligned_buf_alloc;
-using android::aligned_buf_free;
 
 #define GOLDFISH_VK_OBJECT_DEBUG 0
 
@@ -37,9 +26,6 @@
 #endif
 #endif
 
-using goldfish_vk::ResourceTracker;
-using goldfish_vk::VkEncoder;
-
 extern "C" {
 
 #define GOLDFISH_VK_NEW_DISPATCHABLE_FROM_HOST_IMPL(type) \
@@ -116,7 +102,6 @@
         return as_goldfish->underlying; \
     } \
 
-
 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_DISPATCHABLE_FROM_HOST_IMPL)
 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_AS_GOLDFISH_IMPL)
 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_IMPL)
@@ -129,210 +114,8 @@
 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_IMPL)
 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_IDENTITY_IMPL)
 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_U64_IMPL)
-
-GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_IMPL)
-GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_U64_IMPL)
-GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DELETE_GOLDFISH_IMPL)
-
-// Custom definitions///////////////////////////////////////////////////////////
-
-VkResult goldfish_vkEnumerateInstanceVersion(
-    void*,
-    VkResult,
-    uint32_t* apiVersion) {
-    if (apiVersion) {
-        *apiVersion = VK_MAKE_VERSION(1, 0, 0);
-    }
-    return VK_SUCCESS;
-}
-
-VkResult goldfish_vkEnumerateDeviceExtensionProperties(
-    void*,
-    VkResult,
-    VkPhysicalDevice, const char*,
-    uint32_t *pPropertyCount, VkExtensionProperties *) {
-    *pPropertyCount = 0;
-    return VK_SUCCESS;
-}
-
-void goldfish_vkGetPhysicalDeviceProperties2(
-    void*,
-    VkPhysicalDevice,
-    VkPhysicalDeviceProperties2*)
-{
-    // no-op
-}
-
-VkResult goldfish_vkCreateDevice(
-    void* opaque,
-    VkResult host_return,
-    VkPhysicalDevice physicalDevice,
-    const VkDeviceCreateInfo*,
-    const VkAllocationCallbacks*,
-    VkDevice* pDevice) {
-
-    if (host_return != VK_SUCCESS) return host_return;
-
-    VkEncoder* enc = (VkEncoder*)opaque;
-
-    VkPhysicalDeviceProperties props;
-    VkPhysicalDeviceMemoryProperties memProps;
-    enc->vkGetPhysicalDeviceProperties(physicalDevice, &props);
-    enc->vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProps);
-
-    ResourceTracker::get()->setDeviceInfo(*pDevice, physicalDevice, props, memProps);
-
-    return host_return;
-}
-
-VkDeviceMemory new_from_host_VkDeviceMemory(VkDeviceMemory mem) {
-    struct goldfish_VkDeviceMemory *res =
-        (struct goldfish_VkDeviceMemory *)malloc(sizeof(goldfish_VkDeviceMemory));
-
-    if (!res) {
-        ALOGE("FATAL: Failed to alloc VkDeviceMemory handle");
-        abort();
-    }
-
-    memset(res, 0x0, sizeof(goldfish_VkDeviceMemory));
-
-    res->underlying = (uint64_t)(uintptr_t)mem;
-
-    return reinterpret_cast<VkDeviceMemory>(res);
-}
-
-VkDeviceMemory new_from_host_u64_VkDeviceMemory(uint64_t mem) {
-    struct goldfish_VkDeviceMemory *res =
-        (struct goldfish_VkDeviceMemory *)malloc(sizeof(goldfish_VkDeviceMemory));
-
-    if (!res) {
-        ALOGE("FATAL: Failed to alloc VkDeviceMemory handle");
-        abort();
-    }
-
-    memset(res, 0x0, sizeof(goldfish_VkDeviceMemory));
-
-    res->underlying = mem;
-
-    return reinterpret_cast<VkDeviceMemory>(res);
-}
-
-void delete_goldfish_VkDeviceMemory(VkDeviceMemory mem) {
-    struct goldfish_VkDeviceMemory* goldfish_mem =
-        as_goldfish_VkDeviceMemory(mem);
-
-    if (!goldfish_mem) return;
-
-    if (goldfish_mem->ptr) {
-        // TODO: unmap the pointer with address space device
-        aligned_buf_free(goldfish_mem->ptr);
-    }
-
-    free(goldfish_mem);
-}
-
-VkResult goldfish_vkAllocateMemory(
-    void*,
-    VkResult host_return,
-    VkDevice device,
-    const VkMemoryAllocateInfo* pAllocateInfo,
-    const VkAllocationCallbacks*,
-    VkDeviceMemory* pMemory) {
-    
-    if (host_return != VK_SUCCESS) return host_return;
-
-    // Assumes pMemory has already been allocated.
-    goldfish_VkDeviceMemory* mem = as_goldfish_VkDeviceMemory(*pMemory);
-    VkDeviceSize size = pAllocateInfo->allocationSize;
-
-    // assume memory is not host visible.
-    mem->ptr = nullptr;
-    mem->size = size;
-    mem->mappedSize = ResourceTracker::get()->getNonCoherentExtendedSize(device, size);
-
-    if (!ResourceTracker::get()->isMemoryTypeHostVisible(device, pAllocateInfo->memoryTypeIndex)) {
-        return host_return;
-    }
-
-    // This is a strict alignment; we do not expect any
-    // actual device to have more stringent requirements
-    // than this.
-    mem->ptr = (uint8_t*)aligned_buf_alloc(4096, mem->mappedSize);
-    D("host visible alloc: size 0x%llx host ptr %p mapped size 0x%llx",
-      (unsigned long long)size, mem->ptr,
-      (unsigned long long)mem->mappedSize);
-
-    return host_return;
-}
-
-VkResult goldfish_vkMapMemory(
-    void*,
-    VkResult host_result,
-    VkDevice,
-    VkDeviceMemory memory,
-    VkDeviceSize offset,
-    VkDeviceSize size,
-    VkMemoryMapFlags,
-    void** ppData) {
-    
-    if (host_result != VK_SUCCESS) return host_result;
-
-    goldfish_VkDeviceMemory* mem = as_goldfish_VkDeviceMemory(memory);
-
-    if (!mem->ptr) {
-        return VK_ERROR_MEMORY_MAP_FAILED;
-    }
-
-    if (size != VK_WHOLE_SIZE &&
-        (mem->ptr + offset + size > mem->ptr + mem->size)) {
-        return VK_ERROR_MEMORY_MAP_FAILED;
-    }
-
-    *ppData = mem->ptr + offset;
-
-    return host_result;
-}
-
-void goldfish_vkUnmapMemory(
-    void*,
-    VkDevice,
-    VkDeviceMemory) {
-    // no-op
-}
-
-void goldfish_unwrap_VkNativeBufferANDROID(
-    const VkImageCreateInfo* pCreateInfo,
-    VkImageCreateInfo* local_pCreateInfo) {
-
-    if (!pCreateInfo->pNext) return;
-
-    const VkNativeBufferANDROID* nativeInfo =
-        reinterpret_cast<const VkNativeBufferANDROID*>(pCreateInfo->pNext);
-
-    if (VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID != nativeInfo->sType) {
-        return;
-    }
-
-    const cb_handle_t* cb_handle =
-        reinterpret_cast<const cb_handle_t*>(nativeInfo->handle);
-
-    if (!cb_handle) return;
-
-    VkNativeBufferANDROID* nativeInfoOut =
-        reinterpret_cast<VkNativeBufferANDROID*>(local_pCreateInfo);
-
-    if (!nativeInfoOut->handle) {
-        ALOGE("FATAL: Local native buffer info not properly allocated!");
-        abort();
-    }
-
-    *(uint32_t*)(nativeInfoOut->handle) = cb_handle->hostHandle;
-}
-
-void goldfish_unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int*) {
-    if (fd != -1) {
-        sync_wait(fd, 3000);
-    }
-}
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_IMPL)
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_U64_IMPL)
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DELETE_GOLDFISH_IMPL)
 
 } // extern "C"
diff --git a/system/vulkan_enc/Resources.h b/system/vulkan_enc/Resources.h
index ea1d07a..0948df6 100644
--- a/system/vulkan_enc/Resources.h
+++ b/system/vulkan_enc/Resources.h
@@ -70,53 +70,6 @@
 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_IDENTITY_DECL)
 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_FROM_HOST_U64_DECL)
 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_U64_DECL)
-
-GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DEFINE_TRIVIAL_NON_DISPATCHABLE_HANDLE_STRUCT)
-
-// Custom definitions///////////////////////////////////////////////////////////
-
-VkResult goldfish_vkEnumerateInstanceVersion(
-    void* opaque,
-    VkResult host_return,
-    uint32_t* apiVersion);
-
-VkResult goldfish_vkEnumerateDeviceExtensionProperties(
-    void* opaque,
-    VkResult host_return,
-    VkPhysicalDevice physicalDevice, const char *pLayerName,
-    uint32_t *pPropertyCount, VkExtensionProperties *pProperties);
-
-void goldfish_vkGetPhysicalDeviceProperties2(
-    void* opaque,
-    VkPhysicalDevice physicalDevice,
-    VkPhysicalDeviceProperties2* pProperties);
-
-struct goldfish_VkDeviceMemory {
-    uint64_t underlying;
-    uint8_t* ptr;
-    VkDeviceSize size;
-    VkDeviceSize mappedSize;
-};
-
-VkResult goldfish_vkMapMemory(
-    void* opaque,
-    VkResult host_return,
-    VkDevice device,
-    VkDeviceMemory memory,
-    VkDeviceSize offset,
-    VkDeviceSize size,
-    VkMemoryMapFlags flags,
-    void** ppData);
-
-void goldfish_vkUnmapMemory(
-    void* opaque,
-    VkDevice device,
-    VkDeviceMemory memory);
-
-void goldfish_unwrap_VkNativeBufferANDROID(
-    const VkImageCreateInfo* pCreateInfo,
-    VkImageCreateInfo* local_pCreateInfo);
-
-void goldfish_unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out);
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DEFINE_TRIVIAL_NON_DISPATCHABLE_HANDLE_STRUCT)
 
 } // extern "C"
diff --git a/system/vulkan_enc/Validation.cpp b/system/vulkan_enc/Validation.cpp
index 7653f76..e50fa27 100644
--- a/system/vulkan_enc/Validation.cpp
+++ b/system/vulkan_enc/Validation.cpp
@@ -14,27 +14,10 @@
 #include "Validation.h"
 
 #include "Resources.h"
+#include "ResourceTracker.h"
 
 namespace goldfish_vk {
 
-static bool is_range_good(const VkMappedMemoryRange& range) {
-    const goldfish_VkDeviceMemory* mem =
-        as_goldfish_VkDeviceMemory(range.memory);
-
-    if (!mem || !mem->ptr) {
-        return false;
-    }
-
-    VkDeviceSize offset = range.offset;
-    VkDeviceSize size = range.size;
-
-    if (size == VK_WHOLE_SIZE) {
-        return offset <= mem->mappedSize;
-    };
-
-    return offset + size <= mem->mappedSize;
-}
-
 VkResult Validation::on_vkFlushMappedMemoryRanges(
     void*,
     VkResult,
@@ -42,8 +25,10 @@
     uint32_t memoryRangeCount,
     const VkMappedMemoryRange* pMemoryRanges) {
 
+    auto resources = ResourceTracker::get();
+
     for (uint32_t i = 0; i < memoryRangeCount; ++i) {
-        if (!is_range_good(pMemoryRanges[i])) {
+        if (!resources->isValidMemoryRange(pMemoryRanges[i])) {
             return VK_ERROR_OUT_OF_HOST_MEMORY;
         }
     }
@@ -58,8 +43,10 @@
     uint32_t memoryRangeCount,
     const VkMappedMemoryRange* pMemoryRanges) {
 
+    auto resources = ResourceTracker::get();
+
     for (uint32_t i = 0; i < memoryRangeCount; ++i) {
-        if (!is_range_good(pMemoryRanges[i])) {
+        if (!resources->isValidMemoryRange(pMemoryRanges[i])) {
             return VK_ERROR_OUT_OF_HOST_MEMORY;
         }
     }
diff --git a/system/vulkan_enc/VkEncoder.cpp b/system/vulkan_enc/VkEncoder.cpp
index 16238cc..22d7777 100644
--- a/system/vulkan_enc/VkEncoder.cpp
+++ b/system/vulkan_enc/VkEncoder.cpp
@@ -889,7 +889,7 @@
     VkExtensionProperties* pProperties)
 {
     VkResult vkEnumerateDeviceExtensionProperties_VkResult_return = (VkResult)0;
-    vkEnumerateDeviceExtensionProperties_VkResult_return = goldfish_vkEnumerateDeviceExtensionProperties(this, vkEnumerateDeviceExtensionProperties_VkResult_return, physicalDevice, pLayerName, pPropertyCount, pProperties);
+    vkEnumerateDeviceExtensionProperties_VkResult_return = mImpl->resources()->on_vkEnumerateDeviceExtensionProperties(this, VK_SUCCESS, physicalDevice, pLayerName, pPropertyCount, pProperties);
     return vkEnumerateDeviceExtensionProperties_VkResult_return;
 }
 
@@ -1397,7 +1397,7 @@
     void** ppData)
 {
     VkResult vkMapMemory_VkResult_return = (VkResult)0;
-    vkMapMemory_VkResult_return = goldfish_vkMapMemory(this, vkMapMemory_VkResult_return, device, memory, offset, size, flags, ppData);
+    vkMapMemory_VkResult_return = mImpl->resources()->on_vkMapMemory(this, VK_SUCCESS, device, memory, offset, size, flags, ppData);
     return vkMapMemory_VkResult_return;
 }
 
@@ -1405,7 +1405,7 @@
     VkDevice device,
     VkDeviceMemory memory)
 {
-    goldfish_vkUnmapMemory(this, device, memory);
+    mImpl->resources()->on_vkUnmapMemory(this, device, memory);
 }
 
 VkResult VkEncoder::vkFlushMappedMemoryRanges(
@@ -1450,11 +1450,10 @@
         auto memory = pMemoryRanges[i].memory;
         auto size = pMemoryRanges[i].size;
         auto offset = pMemoryRanges[i].offset;
-        auto goldfishMem = as_goldfish_VkDeviceMemory(memory);
         uint64_t streamSize = 0;
-        if (!goldfishMem) { countingStream->write(&streamSize, sizeof(uint64_t)); continue; };
-        auto hostPtr = goldfishMem->ptr;
-        auto actualSize = size == VK_WHOLE_SIZE ? goldfishMem->size : size;
+        if (!memory) { countingStream->write(&streamSize, sizeof(uint64_t)); continue; };
+        auto hostPtr = resources->getMappedPointer(memory);
+        auto actualSize = size == VK_WHOLE_SIZE ? resources->getMappedSize(memory) : size;
         if (!hostPtr) { countingStream->write(&streamSize, sizeof(uint64_t)); continue; };
         streamSize = actualSize;
         countingStream->write(&streamSize, sizeof(uint64_t));
@@ -1480,11 +1479,10 @@
         auto memory = pMemoryRanges[i].memory;
         auto size = pMemoryRanges[i].size;
         auto offset = pMemoryRanges[i].offset;
-        auto goldfishMem = as_goldfish_VkDeviceMemory(memory);
         uint64_t streamSize = 0;
-        if (!goldfishMem) { stream->write(&streamSize, sizeof(uint64_t)); continue; };
-        auto hostPtr = goldfishMem->ptr;
-        auto actualSize = size == VK_WHOLE_SIZE ? goldfishMem->size : size;
+        if (!memory) { stream->write(&streamSize, sizeof(uint64_t)); continue; };
+        auto hostPtr = resources->getMappedPointer(memory);
+        auto actualSize = size == VK_WHOLE_SIZE ? resources->getMappedSize(memory) : size;
         if (!hostPtr) { stream->write(&streamSize, sizeof(uint64_t)); continue; };
         streamSize = actualSize;
         stream->write(&streamSize, sizeof(uint64_t));
@@ -1559,11 +1557,10 @@
         auto memory = pMemoryRanges[i].memory;
         auto size = pMemoryRanges[i].size;
         auto offset = pMemoryRanges[i].offset;
-        auto goldfishMem = as_goldfish_VkDeviceMemory(memory);
         uint64_t streamSize = 0;
-        if (!goldfishMem) { stream->read(&streamSize, sizeof(uint64_t)); continue; };
-        auto hostPtr = goldfishMem->ptr;
-        auto actualSize = size == VK_WHOLE_SIZE ? goldfishMem->size : size;
+        if (!memory) { stream->read(&streamSize, sizeof(uint64_t)); continue; };
+        auto hostPtr = resources->getMappedPointer(memory);
+        auto actualSize = size == VK_WHOLE_SIZE ? resources->getMappedSize(memory) : size;
         if (!hostPtr) { stream->read(&streamSize, sizeof(uint64_t)); continue; };
         streamSize = actualSize;
         stream->read(&streamSize, sizeof(uint64_t));
@@ -3290,7 +3287,7 @@
         local_pAllocator = (VkAllocationCallbacks*)pool->alloc(sizeof(const VkAllocationCallbacks));
         deepcopy_VkAllocationCallbacks(pool, pAllocator, (VkAllocationCallbacks*)(local_pAllocator));
     }
-    goldfish_unwrap_VkNativeBufferANDROID(pCreateInfo, local_pCreateInfo);
+    mImpl->resources()->unwrap_VkNativeBufferANDROID(pCreateInfo, local_pCreateInfo);
     local_pAllocator = nullptr;
     countingStream->rewind();
     {
@@ -8328,7 +8325,7 @@
     uint32_t* pApiVersion)
 {
     VkResult vkEnumerateInstanceVersion_VkResult_return = (VkResult)0;
-    vkEnumerateInstanceVersion_VkResult_return = goldfish_vkEnumerateInstanceVersion(this, vkEnumerateInstanceVersion_VkResult_return, pApiVersion);
+    vkEnumerateInstanceVersion_VkResult_return = mImpl->resources()->on_vkEnumerateInstanceVersion(this, VK_SUCCESS, pApiVersion);
     return vkEnumerateInstanceVersion_VkResult_return;
 }
 
@@ -8880,7 +8877,7 @@
     VkPhysicalDevice physicalDevice,
     VkPhysicalDeviceProperties2* pProperties)
 {
-    goldfish_vkGetPhysicalDeviceProperties2(this, physicalDevice, pProperties);
+    mImpl->resources()->on_vkGetPhysicalDeviceProperties2(this, physicalDevice, pProperties);
 }
 
 void VkEncoder::vkGetPhysicalDeviceFormatProperties2(
@@ -15222,7 +15219,7 @@
     local_nativeFenceFd = nativeFenceFd;
     local_semaphore = semaphore;
     local_fence = fence;
-    goldfish_unwrap_vkAcquireImageANDROID_nativeFenceFd(nativeFenceFd, &local_nativeFenceFd);
+    mImpl->resources()->unwrap_vkAcquireImageANDROID_nativeFenceFd(nativeFenceFd, &local_nativeFenceFd);
     countingStream->rewind();
     {
         uint64_t cgen_var_1170;