Merge "[vulkan] refactor custom create apis as VkEventHandler (guest)"
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index 4def37d..0bb42b2 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -16,11 +16,27 @@
 #include "ResourceTracker.h"
 
 #include "Resources.h"
+#include "VkEncoder.h"
 
+#include "android/base/AlignedBuf.h"
 #include "android/base/synchronization/AndroidLock.h"
 
 #include <unordered_map>
 
+#include <log/log.h>
+
+#define RESOURCE_TRACKER_DEBUG 0
+
+#if RESOURCE_TRACKER_DEBUG
+#define D(fmt,...) ALOGD("%s: " fmt, __func__, ##__VA_ARGS__);
+#else
+#ifndef D
+#define D(fmt,...)
+#endif
+#endif
+
+using android::aligned_buf_alloc;
+using android::aligned_buf_free;
 using android::base::guest::AutoLock;
 using android::base::guest::Lock;
 
@@ -146,6 +162,62 @@
         return atoms * nonCoherentAtomSize;
     }
 
+    VkResult on_vkCreateDevice(
+        void* context,
+        VkResult input_result,
+        VkPhysicalDevice physicalDevice,
+        const VkDeviceCreateInfo*,
+        const VkAllocationCallbacks*,
+        VkDevice* pDevice) {
+
+        if (input_result != VK_SUCCESS) return input_result;
+
+        VkEncoder* enc = (VkEncoder*)context;
+
+        VkPhysicalDeviceProperties props;
+        VkPhysicalDeviceMemoryProperties memProps;
+        enc->vkGetPhysicalDeviceProperties(physicalDevice, &props);
+        enc->vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProps);
+
+        setDeviceInfo(*pDevice, physicalDevice, props, memProps);
+
+        return input_result;
+    }
+
+    VkResult on_vkAllocateMemory(
+        void*,
+        VkResult input_result,
+        VkDevice device,
+        const VkMemoryAllocateInfo* pAllocateInfo,
+        const VkAllocationCallbacks*,
+        VkDeviceMemory* pMemory) {
+
+        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;
+
+        // 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;
+        }
+
+        // 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 input_result;
+    }
+
 private:
     mutable Lock mLock;
 
@@ -201,4 +273,26 @@
     return mImpl->getNonCoherentExtendedSize(device, basicSize);
 }
 
+VkResult ResourceTracker::on_vkCreateDevice(
+    void* context,
+    VkResult input_result,
+    VkPhysicalDevice physicalDevice,
+    const VkDeviceCreateInfo* pCreateInfo,
+    const VkAllocationCallbacks* pAllocator,
+    VkDevice* pDevice) {
+    return mImpl->on_vkCreateDevice(
+        context, input_result, physicalDevice, pCreateInfo, pAllocator, pDevice);
+}
+
+VkResult ResourceTracker::on_vkAllocateMemory(
+    void* context,
+    VkResult input_result,
+    VkDevice device,
+    const VkMemoryAllocateInfo* pAllocateInfo,
+    const VkAllocationCallbacks* pAllocator,
+    VkDeviceMemory* pMemory) {
+    return mImpl->on_vkAllocateMemory(
+        context, input_result, device, pAllocateInfo, pAllocator, pMemory);
+}
+
 } // namespace goldfish_vk
diff --git a/system/vulkan_enc/ResourceTracker.h b/system/vulkan_enc/ResourceTracker.h
index e40e413..65a1221 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_vkCreateDevice(
+        void* context,
+        VkResult input_result,
+        VkPhysicalDevice physicalDevice,
+        const VkDeviceCreateInfo* pCreateInfo,
+        const VkAllocationCallbacks* pAllocator,
+        VkDevice* pDevice) override;
+
+    VkResult on_vkAllocateMemory(
+        void* context,
+        VkResult input_result,
+        VkDevice device,
+        const VkMemoryAllocateInfo* pAllocateInfo,
+        const VkAllocationCallbacks* pAllocator,
+        VkDeviceMemory* pMemory) override;
+
     void setDeviceInfo(VkDevice device, VkPhysicalDevice physdev, VkPhysicalDeviceProperties props, VkPhysicalDeviceMemoryProperties memProps);
     bool isMemoryTypeHostVisible(VkDevice device, uint32_t typeIndex) const;
     VkDeviceSize getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const;
diff --git a/system/vulkan_enc/Resources.h b/system/vulkan_enc/Resources.h
index 37cd882..ea1d07a 100644
--- a/system/vulkan_enc/Resources.h
+++ b/system/vulkan_enc/Resources.h
@@ -91,14 +91,6 @@
     VkPhysicalDevice physicalDevice,
     VkPhysicalDeviceProperties2* pProperties);
 
-VkResult goldfish_vkCreateDevice(
-    void* opaque,
-    VkResult host_return,
-    VkPhysicalDevice physicalDevice,
-    const VkDeviceCreateInfo* pCreateInfo,
-    const VkAllocationCallbacks* pAllocator,
-    VkDevice* pDevice);
-
 struct goldfish_VkDeviceMemory {
     uint64_t underlying;
     uint8_t* ptr;
@@ -106,14 +98,6 @@
     VkDeviceSize mappedSize;
 };
 
-VkResult goldfish_vkAllocateMemory(
-    void* opaque,
-    VkResult host_return,
-    VkDevice device,
-    const VkMemoryAllocateInfo* pAllocateInfo,
-    const VkAllocationCallbacks* pAllocator,
-    VkDeviceMemory* pMemory);
-
 VkResult goldfish_vkMapMemory(
     void* opaque,
     VkResult host_return,
diff --git a/system/vulkan_enc/VkEncoder.cpp b/system/vulkan_enc/VkEncoder.cpp
index 87745f4..16238cc 100644
--- a/system/vulkan_enc/VkEncoder.cpp
+++ b/system/vulkan_enc/VkEncoder.cpp
@@ -735,7 +735,7 @@
     countingStream->clearPool();
     stream->clearPool();
     pool->freeAll();
-    goldfish_vkCreateDevice(this, vkCreateDevice_VkResult_return, physicalDevice, pCreateInfo, pAllocator, pDevice);
+    mImpl->resources()->on_vkCreateDevice(this, vkCreateDevice_VkResult_return, physicalDevice, pCreateInfo, pAllocator, pDevice);
     return vkCreateDevice_VkResult_return;
 }
 
@@ -1325,7 +1325,7 @@
     countingStream->clearPool();
     stream->clearPool();
     pool->freeAll();
-    goldfish_vkAllocateMemory(this, vkAllocateMemory_VkResult_return, device, pAllocateInfo, pAllocator, pMemory);
+    mImpl->resources()->on_vkAllocateMemory(this, vkAllocateMemory_VkResult_return, device, pAllocateInfo, pAllocator, pMemory);
     return vkAllocateMemory_VkResult_return;
 }