[vulkan] host coherent: take codegen (guest)

bug: 111137294
Fixes: 121377497

This hooks everything up, enabling host coherent mappings when
GLDirectMem feature is enabled.

Change-Id: Idfc7471dc767b5872def14aff6e8437c7b54fdf5
diff --git a/system/vulkan/func_table.cpp b/system/vulkan/func_table.cpp
index ea87560..724190e 100644
--- a/system/vulkan/func_table.cpp
+++ b/system/vulkan/func_table.cpp
@@ -3320,6 +3320,18 @@
     vkEnc->vkGetQueueCheckpointDataNV(queue, pCheckpointDataCount, pCheckpointData);
 }
 #endif
+#ifdef VK_GOOGLE_address_space
+static VkResult entry_vkMapMemoryIntoAddressSpaceGOOGLE(
+    VkDevice device,
+    VkDeviceMemory memory,
+    uint64_t* pAddress)
+{
+    auto vkEnc = HostConnection::get()->vkEncoder();
+    VkResult vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return = (VkResult)0;
+    vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return = vkEnc->vkMapMemoryIntoAddressSpaceGOOGLE(device, memory, pAddress);
+    return vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return;
+}
+#endif
 void* goldfish_vulkan_get_proc_address(const char* name){
 #ifdef VK_VERSION_1_0
     if (!strcmp(name, "vkCreateInstance"))
@@ -4715,6 +4727,12 @@
         return (void*)entry_vkGetQueueCheckpointDataNV;
     }
 #endif
+#ifdef VK_GOOGLE_address_space
+    if (!strcmp(name, "vkMapMemoryIntoAddressSpaceGOOGLE"))
+    {
+        return (void*)entry_vkMapMemoryIntoAddressSpaceGOOGLE;
+    }
+#endif
     return nullptr;
 }
 
diff --git a/system/vulkan/func_table.h b/system/vulkan/func_table.h
index 358eec4..894c908 100644
--- a/system/vulkan/func_table.h
+++ b/system/vulkan/func_table.h
@@ -276,6 +276,8 @@
 #endif
 #ifdef VK_NV_device_diagnostic_checkpoints
 #endif
+#ifdef VK_GOOGLE_address_space
+#endif
 void* goldfish_vulkan_get_proc_address(const char* name);
 
 } // namespace goldfish_vk
diff --git a/system/vulkan_enc/Android.mk b/system/vulkan_enc/Android.mk
index 7a146dc..71a52b6 100644
--- a/system/vulkan_enc/Android.mk
+++ b/system/vulkan_enc/Android.mk
@@ -35,6 +35,7 @@
 LOCAL_CFLAGS += \
     -DLOG_TAG=\"goldfish_vulkan\" \
     -DVK_ANDROID_native_buffer \
+    -DVK_GOOGLE_address_space \
     -Wno-missing-field-initializers \
     -Werror \
     -fstrict-aliasing \
diff --git a/system/vulkan_enc/CMakeLists.txt b/system/vulkan_enc/CMakeLists.txt
index 0f43f29..464fd18 100644
--- a/system/vulkan_enc/CMakeLists.txt
+++ b/system/vulkan_enc/CMakeLists.txt
@@ -1,10 +1,10 @@
 # This is an autogenerated file! Do not edit!
 # instead run make from .../device/generic/goldfish-opengl
 # which will re-generate this file.
-android_validate_sha256("${GOLDFISH_DEVICE_ROOT}/system/vulkan_enc/Android.mk" "aad370b1675d05b89d55d471376175c21c2d4567f65a593dabe3038f8806219c")
+android_validate_sha256("${GOLDFISH_DEVICE_ROOT}/system/vulkan_enc/Android.mk" "5937b2e0fd6fa1eac630a5af77306148a1faf05a51af061f8567c897a90bdda5")
 set(vulkan_enc_src Resources.cpp Validation.cpp VulkanStream.cpp VulkanHandleMapping.cpp ResourceTracker.cpp VkEncoder.cpp VkEventHandler.cpp goldfish_vk_extension_structs_guest.cpp goldfish_vk_marshaling_guest.cpp goldfish_vk_deepcopy_guest.cpp goldfish_vk_handlemap_guest.cpp)
 android_add_shared_library(vulkan_enc)
 target_include_directories(vulkan_enc PRIVATE ${GOLDFISH_DEVICE_ROOT}/android-emu ${GOLDFISH_DEVICE_ROOT}/shared/OpenglCodecCommon ${GOLDFISH_DEVICE_ROOT}/system/vulkan_enc ${GOLDFISH_DEVICE_ROOT}/./host/include/libOpenglRender ${GOLDFISH_DEVICE_ROOT}/./system/include ${GOLDFISH_DEVICE_ROOT}/./../../../external/qemu/android/android-emugl/guest ${GOLDFISH_DEVICE_ROOT}/./../../../external/qemu/android/android-emugl/host/include ${GOLDFISH_DEVICE_ROOT}/./../../../external/qemu/android/android-emugl/host/include/vulkan)
-target_compile_definitions(vulkan_enc PRIVATE "-DWITH_GLES2" "-DPLATFORM_SDK_VERSION=29" "-DGOLDFISH_HIDL_GRALLOC" "-DEMULATOR_OPENGL_POST_O=1" "-DHOST_BUILD" "-DANDROID" "-DGL_GLEXT_PROTOTYPES" "-DPAGE_SIZE=4096" "-DGOLDFISH_VULKAN" "-DLOG_TAG=\"goldfish_vulkan\"" "-DVK_ANDROID_native_buffer" "-DVK_USE_PLATFORM_ANDROID_KHR" "-DVK_NO_PROTOTYPES")
+target_compile_definitions(vulkan_enc PRIVATE "-DWITH_GLES2" "-DPLATFORM_SDK_VERSION=29" "-DGOLDFISH_HIDL_GRALLOC" "-DEMULATOR_OPENGL_POST_O=1" "-DHOST_BUILD" "-DANDROID" "-DGL_GLEXT_PROTOTYPES" "-DPAGE_SIZE=4096" "-DGOLDFISH_VULKAN" "-DLOG_TAG=\"goldfish_vulkan\"" "-DVK_ANDROID_native_buffer" "-DVK_GOOGLE_address_space" "-DVK_USE_PLATFORM_ANDROID_KHR" "-DVK_NO_PROTOTYPES")
 target_compile_options(vulkan_enc PRIVATE "-fvisibility=default" "-Wno-missing-field-initializers" "-Werror" "-fstrict-aliasing")
 target_link_libraries(vulkan_enc PRIVATE gui cutils utils log OpenglCodecCommon_host android-emu-shared)
\ No newline at end of file
diff --git a/system/vulkan_enc/VkEncoder.cpp b/system/vulkan_enc/VkEncoder.cpp
index 22d7777..d8286c8 100644
--- a/system/vulkan_enc/VkEncoder.cpp
+++ b/system/vulkan_enc/VkEncoder.cpp
@@ -1444,21 +1444,24 @@
             marshal_VkMappedMemoryRange(countingStream, (VkMappedMemoryRange*)(local_pMemoryRanges + i));
         }
     }
-    for (uint32_t i = 0; i < memoryRangeCount; ++i)
+    if (!resources->usingDirectMapping())
     {
-        auto range = pMemoryRanges[i];
-        auto memory = pMemoryRanges[i].memory;
-        auto size = pMemoryRanges[i].size;
-        auto offset = pMemoryRanges[i].offset;
-        uint64_t streamSize = 0;
-        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));
-        uint8_t* targetRange = hostPtr + offset;
-        countingStream->write(targetRange, actualSize);
+        for (uint32_t i = 0; i < memoryRangeCount; ++i)
+        {
+            auto range = pMemoryRanges[i];
+            auto memory = pMemoryRanges[i].memory;
+            auto size = pMemoryRanges[i].size;
+            auto offset = pMemoryRanges[i].offset;
+            uint64_t streamSize = 0;
+            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));
+            uint8_t* targetRange = hostPtr + offset;
+            countingStream->write(targetRange, actualSize);
+        }
     }
     uint32_t packetSize_vkFlushMappedMemoryRanges = 4 + 4 + (uint32_t)countingStream->bytesWritten();
     countingStream->rewind();
@@ -1473,21 +1476,24 @@
     {
         marshal_VkMappedMemoryRange(stream, (VkMappedMemoryRange*)(local_pMemoryRanges + i));
     }
-    for (uint32_t i = 0; i < memoryRangeCount; ++i)
+    if (!resources->usingDirectMapping())
     {
-        auto range = pMemoryRanges[i];
-        auto memory = pMemoryRanges[i].memory;
-        auto size = pMemoryRanges[i].size;
-        auto offset = pMemoryRanges[i].offset;
-        uint64_t streamSize = 0;
-        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));
-        uint8_t* targetRange = hostPtr + offset;
-        stream->write(targetRange, actualSize);
+        for (uint32_t i = 0; i < memoryRangeCount; ++i)
+        {
+            auto range = pMemoryRanges[i];
+            auto memory = pMemoryRanges[i].memory;
+            auto size = pMemoryRanges[i].size;
+            auto offset = pMemoryRanges[i].offset;
+            uint64_t streamSize = 0;
+            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));
+            uint8_t* targetRange = hostPtr + offset;
+            stream->write(targetRange, actualSize);
+        }
     }
     VkResult vkFlushMappedMemoryRanges_VkResult_return = (VkResult)0;
     stream->read(&vkFlushMappedMemoryRanges_VkResult_return, sizeof(VkResult));
@@ -1551,21 +1557,24 @@
     countingStream->clearPool();
     stream->clearPool();
     pool->freeAll();
-    for (uint32_t i = 0; i < memoryRangeCount; ++i)
+    if (!resources->usingDirectMapping())
     {
-        auto range = pMemoryRanges[i];
-        auto memory = pMemoryRanges[i].memory;
-        auto size = pMemoryRanges[i].size;
-        auto offset = pMemoryRanges[i].offset;
-        uint64_t streamSize = 0;
-        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));
-        uint8_t* targetRange = hostPtr + offset;
-        stream->read(targetRange, actualSize);
+        for (uint32_t i = 0; i < memoryRangeCount; ++i)
+        {
+            auto range = pMemoryRanges[i];
+            auto memory = pMemoryRanges[i].memory;
+            auto size = pMemoryRanges[i].size;
+            auto offset = pMemoryRanges[i].offset;
+            uint64_t streamSize = 0;
+            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));
+            uint8_t* targetRange = hostPtr + offset;
+            stream->read(targetRange, actualSize);
+        }
     }
     return vkInvalidateMappedMemoryRanges_VkResult_return;
 }
@@ -18953,5 +18962,76 @@
 }
 
 #endif
+#ifdef VK_GOOGLE_address_space
+VkResult VkEncoder::vkMapMemoryIntoAddressSpaceGOOGLE(
+    VkDevice device,
+    VkDeviceMemory memory,
+    uint64_t* pAddress)
+{
+    mImpl->resources()->on_vkMapMemoryIntoAddressSpaceGOOGLE_pre(this, VK_SUCCESS, device, memory, pAddress);
+    auto stream = mImpl->stream();
+    auto countingStream = mImpl->countingStream();
+    auto resources = mImpl->resources();
+    auto pool = mImpl->pool();
+    stream->setHandleMapping(resources->unwrapMapping());
+    VkDevice local_device;
+    VkDeviceMemory local_memory;
+    local_device = device;
+    local_memory = memory;
+    countingStream->rewind();
+    {
+        uint64_t cgen_var_1461;
+        countingStream->handleMapping()->mapHandles_VkDevice_u64(&local_device, &cgen_var_1461, 1);
+        countingStream->write((uint64_t*)&cgen_var_1461, 1 * 8);
+        uint64_t cgen_var_1462;
+        countingStream->handleMapping()->mapHandles_VkDeviceMemory_u64(&local_memory, &cgen_var_1462, 1);
+        countingStream->write((uint64_t*)&cgen_var_1462, 1 * 8);
+        // WARNING PTR CHECK
+        uint64_t cgen_var_1463 = (uint64_t)(uintptr_t)pAddress;
+        countingStream->putBe64(cgen_var_1463);
+        if (pAddress)
+        {
+            countingStream->write((uint64_t*)pAddress, sizeof(uint64_t));
+        }
+    }
+    uint32_t packetSize_vkMapMemoryIntoAddressSpaceGOOGLE = 4 + 4 + (uint32_t)countingStream->bytesWritten();
+    countingStream->rewind();
+    uint32_t opcode_vkMapMemoryIntoAddressSpaceGOOGLE = OP_vkMapMemoryIntoAddressSpaceGOOGLE;
+    stream->write(&opcode_vkMapMemoryIntoAddressSpaceGOOGLE, sizeof(uint32_t));
+    stream->write(&packetSize_vkMapMemoryIntoAddressSpaceGOOGLE, sizeof(uint32_t));
+    uint64_t cgen_var_1464;
+    stream->handleMapping()->mapHandles_VkDevice_u64(&local_device, &cgen_var_1464, 1);
+    stream->write((uint64_t*)&cgen_var_1464, 1 * 8);
+    uint64_t cgen_var_1465;
+    stream->handleMapping()->mapHandles_VkDeviceMemory_u64(&local_memory, &cgen_var_1465, 1);
+    stream->write((uint64_t*)&cgen_var_1465, 1 * 8);
+    // WARNING PTR CHECK
+    uint64_t cgen_var_1466 = (uint64_t)(uintptr_t)pAddress;
+    stream->putBe64(cgen_var_1466);
+    if (pAddress)
+    {
+        stream->write((uint64_t*)pAddress, sizeof(uint64_t));
+    }
+    // WARNING PTR CHECK
+    uint64_t* check_pAddress;
+    check_pAddress = (uint64_t*)(uintptr_t)stream->getBe64();
+    if (pAddress)
+    {
+        if (!(check_pAddress))
+        {
+            fprintf(stderr, "fatal: pAddress inconsistent between guest and host\n");
+        }
+        stream->read((uint64_t*)pAddress, sizeof(uint64_t));
+    }
+    VkResult vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return = (VkResult)0;
+    stream->read(&vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return, sizeof(VkResult));
+    countingStream->clearPool();
+    stream->clearPool();
+    pool->freeAll();
+    mImpl->resources()->on_vkMapMemoryIntoAddressSpaceGOOGLE(this, vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return, device, memory, pAddress);
+    return vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return;
+}
+
+#endif
 
 } // namespace goldfish_vk
diff --git a/system/vulkan_enc/VkEncoder.h b/system/vulkan_enc/VkEncoder.h
index 2eacb67..77e08ce 100644
--- a/system/vulkan_enc/VkEncoder.h
+++ b/system/vulkan_enc/VkEncoder.h
@@ -1741,6 +1741,12 @@
         uint32_t* pCheckpointDataCount,
         VkCheckpointDataNV* pCheckpointData);
 #endif
+#ifdef VK_GOOGLE_address_space
+    VkResult vkMapMemoryIntoAddressSpaceGOOGLE(
+    VkDevice device,
+        VkDeviceMemory memory,
+        uint64_t* pAddress);
+#endif
 
 private:
     class Impl;
diff --git a/system/vulkan_enc/VkEventHandler.cpp b/system/vulkan_enc/VkEventHandler.cpp
index 0e166f3..2490f1a 100644
--- a/system/vulkan_enc/VkEventHandler.cpp
+++ b/system/vulkan_enc/VkEventHandler.cpp
@@ -3151,5 +3151,17 @@
 {
 }
 #endif
+#ifdef VK_GOOGLE_address_space
+VkResult VkEventHandler::on_vkMapMemoryIntoAddressSpaceGOOGLE(
+    void*,
+    VkResult,
+    VkDevice,
+    VkDeviceMemory,
+    uint64_t*)
+{
+    VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return = (VkResult)0;
+    return on_vkMapMemoryIntoAddressSpaceGOOGLE_VkResult_return;
+}
+#endif
 
 } // namespace goldfish_vk
diff --git a/system/vulkan_enc/VkEventHandler.h b/system/vulkan_enc/VkEventHandler.h
index fcc4192..49f6d7e 100644
--- a/system/vulkan_enc/VkEventHandler.h
+++ b/system/vulkan_enc/VkEventHandler.h
@@ -2212,6 +2212,14 @@
         uint32_t* pCheckpointDataCount,
         VkCheckpointDataNV* pCheckpointData);
 #endif
+#ifdef VK_GOOGLE_address_space
+    virtual VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE(
+    void* context,
+        VkResult input_result,
+        VkDevice device,
+        VkDeviceMemory memory,
+        uint64_t* pAddress);
+#endif
 
 };
 
diff --git a/system/vulkan_enc/goldfish_vk_deepcopy_guest.cpp b/system/vulkan_enc/goldfish_vk_deepcopy_guest.cpp
index c481e3c..7c2bf8c 100644
--- a/system/vulkan_enc/goldfish_vk_deepcopy_guest.cpp
+++ b/system/vulkan_enc/goldfish_vk_deepcopy_guest.cpp
@@ -6344,6 +6344,8 @@
 }
 
 #endif
+#ifdef VK_GOOGLE_address_space
+#endif
 void deepcopy_extension_struct(
     Pool* pool,
     const void* structExtension,
diff --git a/system/vulkan_enc/goldfish_vk_deepcopy_guest.h b/system/vulkan_enc/goldfish_vk_deepcopy_guest.h
index 07828f5..08600d3 100644
--- a/system/vulkan_enc/goldfish_vk_deepcopy_guest.h
+++ b/system/vulkan_enc/goldfish_vk_deepcopy_guest.h
@@ -2008,5 +2008,7 @@
     VkCheckpointDataNV* to);
 
 #endif
+#ifdef VK_GOOGLE_address_space
+#endif
 
 } // namespace goldfish_vk
diff --git a/system/vulkan_enc/goldfish_vk_extension_structs_guest.cpp b/system/vulkan_enc/goldfish_vk_extension_structs_guest.cpp
index 410ea7f..6947c7e 100644
--- a/system/vulkan_enc/goldfish_vk_extension_structs_guest.cpp
+++ b/system/vulkan_enc/goldfish_vk_extension_structs_guest.cpp
@@ -274,6 +274,8 @@
 #endif
 #ifdef VK_NV_device_diagnostic_checkpoints
 #endif
+#ifdef VK_GOOGLE_address_space
+#endif
 uint32_t goldfish_vk_struct_type(
     const void* structExtension)
 {
diff --git a/system/vulkan_enc/goldfish_vk_extension_structs_guest.h b/system/vulkan_enc/goldfish_vk_extension_structs_guest.h
index 6c5a04c..97d7547 100644
--- a/system/vulkan_enc/goldfish_vk_extension_structs_guest.h
+++ b/system/vulkan_enc/goldfish_vk_extension_structs_guest.h
@@ -291,5 +291,7 @@
 #endif
 #ifdef VK_NV_device_diagnostic_checkpoints
 #endif
+#ifdef VK_GOOGLE_address_space
+#endif
 
 } // namespace goldfish_vk
diff --git a/system/vulkan_enc/goldfish_vk_handlemap_guest.cpp b/system/vulkan_enc/goldfish_vk_handlemap_guest.cpp
index e6ab090..de85c43 100644
--- a/system/vulkan_enc/goldfish_vk_handlemap_guest.cpp
+++ b/system/vulkan_enc/goldfish_vk_handlemap_guest.cpp
@@ -4716,6 +4716,8 @@
 }
 
 #endif
+#ifdef VK_GOOGLE_address_space
+#endif
 void handlemap_extension_struct(
     VulkanHandleMapping* handlemap,
     void* structExtension_out)
diff --git a/system/vulkan_enc/goldfish_vk_handlemap_guest.h b/system/vulkan_enc/goldfish_vk_handlemap_guest.h
index d961965..d056f63 100644
--- a/system/vulkan_enc/goldfish_vk_handlemap_guest.h
+++ b/system/vulkan_enc/goldfish_vk_handlemap_guest.h
@@ -1663,5 +1663,7 @@
     VkCheckpointDataNV* toMap);
 
 #endif
+#ifdef VK_GOOGLE_address_space
+#endif
 
 } // namespace goldfish_vk
diff --git a/system/vulkan_enc/goldfish_vk_marshaling_guest.cpp b/system/vulkan_enc/goldfish_vk_marshaling_guest.cpp
index 1bd5dae..ca08465 100644
--- a/system/vulkan_enc/goldfish_vk_marshaling_guest.cpp
+++ b/system/vulkan_enc/goldfish_vk_marshaling_guest.cpp
@@ -13363,6 +13363,8 @@
 }
 
 #endif
+#ifdef VK_GOOGLE_address_space
+#endif
 void marshal_extension_struct(
     VulkanStream* vkStream,
     const void* structExtension)
diff --git a/system/vulkan_enc/goldfish_vk_marshaling_guest.h b/system/vulkan_enc/goldfish_vk_marshaling_guest.h
index a6ac11f..db92431 100644
--- a/system/vulkan_enc/goldfish_vk_marshaling_guest.h
+++ b/system/vulkan_enc/goldfish_vk_marshaling_guest.h
@@ -3358,5 +3358,8 @@
 #define OP_vkCmdSetCheckpointNV 20315
 #define OP_vkGetQueueCheckpointDataNV 20316
 #endif
+#ifdef VK_GOOGLE_address_space
+#define OP_vkMapMemoryIntoAddressSpaceGOOGLE 20317
+#endif
 
 } // namespace goldfish_vk