Revert^2 "goldfish-opengl: add deferred resource creation"

aa7b307fdfe75a4bae4b53449ec0248cf8aef30c

Change-Id: I23edaa037064dfa49b53d80a0484172c07b09348
diff --git a/platform/include/virtgpu_gfxstream_protocol.h b/platform/include/virtgpu_gfxstream_protocol.h
index 325fa9c..29fa787 100644
--- a/platform/include/virtgpu_gfxstream_protocol.h
+++ b/platform/include/virtgpu_gfxstream_protocol.h
@@ -73,6 +73,8 @@
     uint32_t bufferSize;
 
     uint32_t pad;
+    uint32_t padding[16];
+    uint32_t deferredMapping;
 };
 
 #endif
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index cf351bb..e1df2f9 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -301,6 +301,7 @@
 
         uint8_t* ptr = nullptr;
 
+        uint64_t blobId = 0;
         uint64_t allocationSize = 0;
         uint32_t memoryTypeIndex = 0;
         uint64_t coherentMemorySize = 0;
@@ -2955,10 +2956,15 @@
 
     VkResult allocateCoherentMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
                                     VkEncoder* enc, VkDeviceMemory* pMemory) {
+        uint64_t blobId = 0;
         uint64_t offset = 0;
         uint8_t *ptr = nullptr;
         VkMemoryAllocateFlagsInfo allocFlagsInfo;
         VkMemoryOpaqueCaptureAddressAllocateInfo opaqueCaptureAddressAllocInfo;
+        VkCreateBlobGOOGLE createBlobInfo;
+
+        memset(&createBlobInfo, 0, sizeof(struct VkCreateBlobGOOGLE));
+        createBlobInfo.sType = VK_STRUCTURE_TYPE_CREATE_BLOB_GOOGLE;
 
         const VkMemoryAllocateFlagsInfo* allocFlagsInfoPtr =
             vk_find_struct<VkMemoryAllocateFlagsInfo>(pAllocateInfo);
@@ -2971,6 +2977,7 @@
              (allocFlagsInfoPtr->flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT));
 
         bool dedicated = deviceAddressMemoryAllocation;
+        if (mCaps.gfxstreamCapset.deferredMapping) dedicated = true;
 
         VkMemoryAllocateInfo hostAllocationInfo = vk_make_orphan_copy(*pAllocateInfo);
         vk_struct_chain_iterator structChainIter = vk_make_chain_iterator(&hostAllocationInfo);
@@ -3001,6 +3008,12 @@
             }
         }
 
+        if (mCaps.gfxstreamCapset.deferredMapping) {
+            createBlobInfo.blobId = ++mBlobId;
+            createBlobInfo.blobMem = kBlobMemHost3d;
+            vk_append_struct(&structChainIter, &createBlobInfo);
+        }
+
         VkDeviceMemory mem = VK_NULL_HANDLE;
         VkResult host_res =
         enc->vkAllocateMemory(device, &hostAllocationInfo, nullptr,
@@ -3009,6 +3022,11 @@
             return host_res;
         }
         struct VkDeviceMemory_Info info;
+        if (mCaps.gfxstreamCapset.deferredMapping) {
+            info.allocationSize = pAllocateInfo->allocationSize;
+            info.blobId = createBlobInfo.blobId;
+        }
+
         info.coherentMemorySize = hostAllocationInfo.allocationSize;
         info.memoryTypeIndex = hostAllocationInfo.memoryTypeIndex;
         info.device = device;
@@ -3019,6 +3037,12 @@
             AutoLock<RecursiveLock> lock(mLock);
             info_VkDeviceMemory[mem] = info;
         }
+
+        if (mCaps.gfxstreamCapset.deferredMapping) {
+            *pMemory = mem;
+            return host_res;
+        }
+
         auto coherentMemory = createCoherentMemory(device, mem, hostAllocationInfo, enc, host_res);
         if(coherentMemory) {
             AutoLock<RecursiveLock> lock(mLock);
@@ -3051,6 +3075,8 @@
                          ((allocFlagsInfoPtr->flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT) ||
                           (allocFlagsInfoPtr->flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT));
 
+        if (mCaps.gfxstreamCapset.deferredMapping) dedicated = true;
+
         CoherentMemoryPtr coherentMemory = nullptr;
         uint8_t *ptr = nullptr;
         uint64_t offset = 0;
@@ -3839,16 +3865,9 @@
         coherentMemory = nullptr;
     }
 
-    VkResult on_vkMapMemory(
-        void*,
-        VkResult host_result,
-        VkDevice,
-        VkDeviceMemory memory,
-        VkDeviceSize offset,
-        VkDeviceSize size,
-        VkMemoryMapFlags,
-        void** ppData) {
-
+    VkResult on_vkMapMemory(void* context, VkResult host_result, VkDevice device,
+                            VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size,
+                            VkMemoryMapFlags, void** ppData) {
         if (host_result != VK_SUCCESS) {
             ALOGE("%s: Host failed to map\n", __func__);
             return host_result;
@@ -3864,6 +3883,39 @@
 
         auto& info = it->second;
 
+        if (info.blobId && !info.coherentMemory) {
+            VkEncoder* enc = (VkEncoder*)context;
+            VirtGpuBlobMappingPtr mapping;
+            VirtGpuDevice& instance = VirtGpuDevice::getInstance();
+
+            uint64_t offset;
+            uint8_t* ptr;
+
+            VkResult vkResult = enc->vkGetBlobGOOGLE(device, memory, false);
+            if (vkResult != VK_SUCCESS) return vkResult;
+
+            struct VirtGpuCreateBlob createBlob = {};
+            createBlob.blobMem = kBlobMemHost3d;
+            createBlob.flags = kBlobFlagMappable;
+            createBlob.blobId = info.blobId;
+            createBlob.size = info.coherentMemorySize;
+
+            auto blob = instance.createBlob(createBlob);
+            if (!blob) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+
+            mapping = blob->createMapping();
+            if (!mapping) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+
+            auto coherentMemory =
+                std::make_shared<CoherentMemory>(mapping, createBlob.size, device, memory);
+
+            coherentMemory->subAllocate(info.allocationSize, &ptr, offset);
+
+            info.coherentMemoryOffset = offset;
+            info.coherentMemory = coherentMemory;
+            info.ptr = ptr;
+        }
+
         if (!info.ptr) {
             ALOGE("%s: ptr null\n", __func__);
             return VK_ERROR_MEMORY_MAP_FAILED;
@@ -7260,6 +7312,8 @@
     std::vector<VkExtensionProperties> mHostInstanceExtensions;
     std::vector<VkExtensionProperties> mHostDeviceExtensions;
 
+    // 32 bits only for now, upper bits may be used later.
+    std::atomic<uint32_t> mBlobId = 0;
 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
     int mSyncDeviceFd = -1;
 #endif
diff --git a/system/vulkan_enc/vk_struct_id.h b/system/vulkan_enc/vk_struct_id.h
index ebc1001..16a0f29 100644
--- a/system/vulkan_enc/vk_struct_id.h
+++ b/system/vulkan_enc/vk_struct_id.h
@@ -65,6 +65,7 @@
 REGISTER_VK_STRUCT_ID(VkFenceCreateInfo, VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
 REGISTER_VK_STRUCT_ID(VkExportFenceCreateInfo, VK_STRUCTURE_TYPE_EXPORT_FENCE_CREATE_INFO);
 REGISTER_VK_STRUCT_ID(VkImportBufferGOOGLE, VK_STRUCTURE_TYPE_IMPORT_BUFFER_GOOGLE);
+REGISTER_VK_STRUCT_ID(VkCreateBlobGOOGLE, VK_STRUCTURE_TYPE_CREATE_BLOB_GOOGLE);
 REGISTER_VK_STRUCT_ID(VkExternalImageFormatProperties, VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES);
 REGISTER_VK_STRUCT_ID(VkPhysicalDeviceImageFormatInfo2, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2);
 REGISTER_VK_STRUCT_ID(VkPhysicalDeviceExternalImageFormatInfo, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO);