Reland "vulkan-cereal: modify ASG for external resources""

No changes from the original commit on vulkan-cereal side.

BUG=235485545
TEST=compile

Change-Id: I08addec90ad4178c1dcd5a8675ee0dd425e34565
diff --git a/stream-servers/virtio-gpu-gfxstream-renderer.cpp b/stream-servers/virtio-gpu-gfxstream-renderer.cpp
index 947e197..87818f1 100644
--- a/stream-servers/virtio-gpu-gfxstream-renderer.cpp
+++ b/stream-servers/virtio-gpu-gfxstream-renderer.cpp
@@ -171,6 +171,7 @@
     int fence;
     uint32_t addressSpaceHandle;
     bool hasAddressSpaceHandle;
+    std::unordered_map<VirtioGpuResId, uint32_t> addressSpaceHandles;
 };
 
 enum class ResType {
@@ -587,6 +588,7 @@
             fprintf(stderr, "%s: failed to create hw pipe!\n", __func__);
             return -1;
         }
+        std::unordered_map<uint32_t, uint32_t> map;
 
         PipeCtxEntry res = {
             ctx_id, // ctxId
@@ -594,6 +596,7 @@
             0, // fence
             0, // AS handle
             false, // does not have an AS handle
+            map,   // resourceId --> ASG handle map
         };
 
         VGPLOG("initial host pipe for ctxid %u: %p", ctx_id, hostPipe);
@@ -612,8 +615,9 @@
         }
 
         if (it->second.hasAddressSpaceHandle) {
-            mAddressSpaceDeviceControlOps->destroy_handle(
-                it->second.addressSpaceHandle);
+            for (auto const& [resourceId, handle] : it->second.addressSpaceHandles) {
+                mAddressSpaceDeviceControlOps->destroy_handle(handle);
+            }
         }
 
         auto ops = ensureAndGetServiceOps();
@@ -630,7 +634,8 @@
         return 0;
     }
 
-    void setContextAddressSpaceHandleLocked(VirtioGpuCtxId ctxId, uint32_t handle) {
+    void setContextAddressSpaceHandleLocked(VirtioGpuCtxId ctxId, uint32_t handle,
+                                            uint32_t resourceId) {
         auto ctxIt = mContexts.find(ctxId);
         if (ctxIt == mContexts.end()) {
             GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
@@ -640,9 +645,10 @@
         auto& ctxEntry = ctxIt->second;
         ctxEntry.addressSpaceHandle = handle;
         ctxEntry.hasAddressSpaceHandle = true;
+        ctxEntry.addressSpaceHandles[resourceId] = handle;
     }
 
-    uint32_t getAddressSpaceHandleLocked(VirtioGpuCtxId ctxId) {
+    uint32_t getAddressSpaceHandleLocked(VirtioGpuCtxId ctxId, uint32_t resourceId) {
         auto ctxIt = mContexts.find(ctxId);
         if (ctxIt == mContexts.end()) {
             GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
@@ -651,31 +657,12 @@
 
         auto& ctxEntry = ctxIt->second;
 
-        if (!ctxEntry.hasAddressSpaceHandle) {
+        if (!ctxEntry.addressSpaceHandles.count(resourceId)) {
             GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << "ctx id " << ctxId << " doesn't have address space handle";
+                << "ASG context with resource id " << resourceId << " not found ";
         }
 
-        return ctxEntry.addressSpaceHandle;
-    }
-
-    void writeWordsToFirstIovPageLocked(uint32_t* dwords, size_t dwordCount, uint32_t resId) {
-
-        auto resEntryIt = mResources.find(resId);
-        if (resEntryIt == mResources.end()) {
-            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << " resid " << resId << " not found";
-        }
-
-        auto& resEntry = resEntryIt->second;
-
-        if (!resEntry.iov) {
-            GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
-                << "resid " << resId << " has empty iov ";
-        }
-
-        uint32_t* iovWords = (uint32_t*)(resEntry.iov[0].iov_base);
-        memcpy(iovWords, dwords, sizeof(uint32_t) * dwordCount);
+        return ctxEntry.addressSpaceHandles[resourceId];
     }
 
 #define DECODE(variable, type, input) \
@@ -689,78 +676,39 @@
             case GFXSTREAM_CONTEXT_CREATE: {
                 DECODE(contextCreate, gfxstreamContextCreate, dwords)
 
+                auto resEntryIt = mResources.find(contextCreate.resourceId);
+                if (resEntryIt == mResources.end()) {
+                   GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
+                   << " ASG coherent resource " << contextCreate.resourceId << " not found";
+                }
+
+                auto& resEntry = resEntryIt->second;
                 uint32_t handle = mAddressSpaceDeviceControlOps->gen_handle();
 
-                struct android::emulation::AddressSpaceDevicePingInfo pingInfo = {
-                    .metadata = (uint64_t)contextCreate.deviceType,
+                struct AddressSpaceCreateInfo createInfo = {
+                    .handle = handle,
+                    .type = android::emulation::VirtioGpuGraphics,
+                    .createRenderThread = true,
+                    .externalAddr = resEntry.hva,
+                    .externalAddrSize = resEntry.hvaSize,
                 };
 
-                mAddressSpaceDeviceControlOps->ping_at_hva(handle, &pingInfo);
-
+                mAddressSpaceDeviceControlOps->create_instance(createInfo);
                 AutoLock lock(mLock);
-                setContextAddressSpaceHandleLocked(ctxId, handle);
+                setContextAddressSpaceHandleLocked(ctxId, handle, contextCreate.resourceId);
                 break;
             }
             case GFXSTREAM_CONTEXT_PING: {
                 DECODE(contextPing, gfxstreamContextPing, dwords)
 
-                struct android::emulation::AddressSpaceDevicePingInfo pingInfo = {
-                    .phys_addr = convert32to64(contextPing.phys_addr_lo, contextPing.phys_addr_hi),
-                    .size = convert32to64(contextPing.size_lo, contextPing.size_hi),
-                    .metadata = convert32to64(contextPing.metadata_lo, contextPing.metadata_hi),
-                    .wait_phys_addr = convert32to64(contextPing.wait_phys_addr_lo, contextPing.wait_phys_addr_hi),
-                    .wait_flags = contextPing.wait_flags,
-                    .direction = contextPing.direction,
-                };
-
                 AutoLock lock(mLock);
+
+                struct android::emulation::AddressSpaceDevicePingInfo ping = {0};
+                ping.metadata = ASG_NOTIFY_AVAILABLE;
+
                 mAddressSpaceDeviceControlOps->ping_at_hva(
-                    getAddressSpaceHandleLocked(ctxId),
-                    &pingInfo);
-                break;
-            }
-            case GFXSTREAM_CONTEXT_PING_WITH_RESPONSE: {
-                DECODE(contextPing, gfxstreamContextPingWithResponse, dwords)
-
-                uint32_t resp_resid = contextPing.resp_resid;
-
-                struct android::emulation::AddressSpaceDevicePingInfo pingInfo = {
-                    .phys_addr = convert32to64(contextPing.phys_addr_lo, contextPing.phys_addr_hi),
-                    .size = convert32to64(contextPing.size_lo, contextPing.size_hi),
-                    .metadata = convert32to64(contextPing.metadata_lo, contextPing.metadata_hi),
-                    .wait_phys_addr = convert32to64(contextPing.wait_phys_addr_lo, contextPing.wait_phys_addr_hi),
-                    .wait_flags = contextPing.wait_flags,
-                    .direction = contextPing.direction,
-                };
-
-                AutoLock lock(mLock);
-                mAddressSpaceDeviceControlOps->ping_at_hva(
-                    getAddressSpaceHandleLocked(ctxId),
-                    &pingInfo);
-
-                uint32_t phys_addr_lo = (uint32_t)pingInfo.phys_addr;
-                uint32_t phys_addr_hi = (uint32_t)(pingInfo.phys_addr >> 32);
-                uint32_t size_lo = (uint32_t)(pingInfo.size >> 0);
-                uint32_t size_hi = (uint32_t)(pingInfo.size >> 32);
-                uint32_t metadata_lo = (uint32_t)(pingInfo.metadata >> 0);
-                uint32_t metadata_hi = (uint32_t)(pingInfo.metadata >> 32);
-                uint32_t wait_phys_addr_lo = (uint32_t)(pingInfo.wait_phys_addr >> 0);
-                uint32_t wait_phys_addr_hi = (uint32_t)(pingInfo.wait_phys_addr >> 32);
-                uint32_t wait_flags = (uint32_t)(pingInfo.wait_flags >> 0);
-                uint32_t direction = (uint32_t)(pingInfo.direction >> 0);
-
-                uint32_t response[] = {
-                    phys_addr_lo, phys_addr_hi,
-                    size_lo, size_hi,
-                    metadata_lo, metadata_hi,
-                    wait_phys_addr_lo, wait_phys_addr_hi,
-                    wait_flags, direction,
-                };
-
-                writeWordsToFirstIovPageLocked(
-                    response,
-                    sizeof(response) / sizeof(uint32_t),
-                    resp_resid);
+                    getAddressSpaceHandleLocked(ctxId, contextPing.resourceId),
+                    &ping);
                 break;
             }
             default:
@@ -1658,6 +1606,16 @@
 
         resIt->second.hostPipe = 0;
         resIt->second.ctxId = 0;
+
+        auto ctxIt = mContexts.find(ctxId);
+        if (ctxIt != mContexts.end()) {
+            auto& ctxEntry = ctxIt->second;
+            if (ctxEntry.addressSpaceHandles.count(toUnrefId)) {
+                uint32_t handle = ctxEntry.addressSpaceHandles[toUnrefId];
+                mAddressSpaceDeviceControlOps->destroy_handle(handle);
+                ctxEntry.addressSpaceHandles.erase(toUnrefId);
+            }
+        }
     }
 
     inline const GoldfishPipeServiceOps* ensureAndGetServiceOps() {