Wrap interactions with sync fds in an interface

... so that upcoming end2end tests can set up a mock impl for
emulating sync fds in single process tests.

Bug: b/292257025
Test: cvd start --gpu_mode=gfxstream
      cvd start --gpu_mode=gfxstream_guest_angle_host_swiftshader
Change-Id: I3024da060200d02c1399ca9fee54dbf74749ed2b
diff --git a/guest/vulkan_enc/ResourceTracker.cpp b/guest/vulkan_enc/ResourceTracker.cpp
index 63ff963..0e53eba 100644
--- a/guest/vulkan_enc/ResourceTracker.cpp
+++ b/guest/vulkan_enc/ResourceTracker.cpp
@@ -100,12 +100,10 @@
 #include <vndk/hardware_buffer.h>
 #include <log/log.h>
 #include <stdlib.h>
-#include <sync/sync.h>
 
 #if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__)
 
 #include <sys/mman.h>
-#include <unistd.h>
 #include <sys/syscall.h>
 
 #ifdef HOST_BUILD
@@ -559,7 +557,8 @@
 
 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
         if (semInfo.syncFd.value_or(-1) >= 0) {
-            close(semInfo.syncFd.value());
+            auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+            syncHelper->close(semInfo.syncFd.value());
         }
 #endif
 
@@ -600,7 +599,8 @@
 
 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
         if (fenceInfo.syncFd >= 0) {
-            close(fenceInfo.syncFd);
+            auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+            syncHelper->close(fenceInfo.syncFd);
         }
 #endif
 
@@ -4543,7 +4543,8 @@
             if (info.syncFd >= 0) {
                 ALOGV("%s: resetting fence. make fd -1\n", __func__);
                 goldfish_sync_signal(info.syncFd);
-                close(info.syncFd);
+                auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+                syncHelper->close(info.syncFd);
                 info.syncFd = -1;
             }
 #endif
@@ -4590,10 +4591,11 @@
 
         auto& info = it->second;
 
+        auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
         if (info.syncFd >= 0) {
             ALOGV("%s: previous sync fd exists, close it\n", __func__);
             goldfish_sync_signal(info.syncFd);
-            close(info.syncFd);
+            syncHelper->close(info.syncFd);
         }
 
         if (pImportFenceFdInfo->fd < 0) {
@@ -4601,8 +4603,8 @@
             info.syncFd = -1;
         } else {
             ALOGV("%s: import actual fd, dup and close()\n", __func__);
-            info.syncFd = dup(pImportFenceFdInfo->fd);
-            close(pImportFenceFdInfo->fd);
+            info.syncFd = syncHelper->dup(pImportFenceFdInfo->fd);
+            syncHelper->close(pImportFenceFdInfo->fd);
         }
         return VK_SUCCESS;
 #else
@@ -4771,7 +4773,8 @@
             for (auto fd : fencesExternalWaitFds) {
                 ALOGV("%s: wait on %d\n", __func__, fd);
                 tasks.push_back([fd] {
-                    sync_wait(fd, 3000);
+                    auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+                    syncHelper->wait(fd, 3000);
                     ALOGV("done waiting on fd %d\n", fd);
                 });
             }
@@ -5089,6 +5092,7 @@
         VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
 
 #ifdef VK_USE_PLATFORM_ANDROID_KHR
+        auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
         {
           AutoLock<RecursiveLock> lock(mLock); // do not guard encoder may cause
                                                // deadlock b/243339973
@@ -5102,12 +5106,12 @@
           if (imageInfoIt != info_VkImage.end()) {
             auto& imageInfo = imageInfoIt->second;
             for (int syncFd : imageInfo.pendingQsriSyncFds) {
-                int syncWaitRet = sync_wait(syncFd, 3000);
+                int syncWaitRet = syncHelper->wait(syncFd, 3000);
                 if (syncWaitRet < 0) {
                     ALOGE("%s: Failed to wait for pending QSRI sync: sterror: %s errno: %d",
                           __func__, strerror(errno), errno);
                 }
-                close(syncFd);
+                syncHelper->close(syncFd);
             }
             imageInfo.pendingQsriSyncFds.clear();
           }
@@ -5570,7 +5574,8 @@
             if (it == info_VkSemaphore.end()) return VK_ERROR_OUT_OF_HOST_MEMORY;
             auto& semInfo = it->second;
             // syncFd is supposed to have value.
-            *pFd = dup(semInfo.syncFd.value_or(-1));
+            auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+            *pFd = syncHelper->dup(semInfo.syncFd.value_or(-1));
             return VK_SUCCESS;
         } else {
             // opaque fd
@@ -5602,6 +5607,8 @@
             return input_result;
         }
 
+        auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+
         if (pImportSemaphoreFdInfo->handleType &
             VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT) {
             VkImportSemaphoreFdInfoKHR tmpInfo = *pImportSemaphoreFdInfo;
@@ -5612,7 +5619,7 @@
             auto& info = semaphoreIt->second;
 
             if (info.syncFd.value_or(-1) >= 0) {
-                close(info.syncFd.value());
+                syncHelper->close(info.syncFd.value());
             }
 
             info.syncFd.emplace(pImportSemaphoreFdInfo->fd);
@@ -5629,7 +5636,7 @@
             VkImportSemaphoreFdInfoKHR tmpInfo = *pImportSemaphoreFdInfo;
             tmpInfo.fd = hostFd;
             VkResult result = enc->vkImportSemaphoreFdKHR(device, &tmpInfo, true /* do lock */);
-            close(fd);
+            syncHelper->close(fd);
             return result;
         }
 #else
@@ -5999,7 +6006,8 @@
                 // fd == -1 is treated as already signaled
                 if (fd != -1) {
                     preSignalTasks.push_back([fd] {
-                        sync_wait(fd, 3000);
+                        auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+                        syncHelper->wait(fd, 3000);
                     });
                 }
             }
@@ -6156,7 +6164,8 @@
         if (fd != -1) {
             AEMU_SCOPED_TRACE("waitNativeFenceInAcquire");
             // Implicit Synchronization
-            sync_wait(fd, 3000);
+            auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+            syncHelper->wait(fd, 3000);
             // From libvulkan's swapchain.cpp:
             // """
             // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
@@ -6168,7 +6177,7 @@
             // failure, or *never* closes it on failure.
             // """
             // Therefore, assume contract where we need to close fd in this driver
-            close(fd);
+            syncHelper->close(fd);
         }
     }
 
@@ -7181,15 +7190,17 @@
         if (imageInfoIt != info_VkImage.end()) {
             auto& imageInfo = imageInfoIt->second;
 
+            auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+
             // Remove any pending QSRI sync fds that are already signaled.
             auto syncFdIt = imageInfo.pendingQsriSyncFds.begin();
             while (syncFdIt != imageInfo.pendingQsriSyncFds.end()) {
                 int syncFd = *syncFdIt;
-                int syncWaitRet = sync_wait(syncFd, /*timeout msecs*/0);
+                int syncWaitRet = syncHelper->wait(syncFd, /*timeout msecs*/0);
                 if (syncWaitRet == 0) {
                     // Sync fd is signaled.
                     syncFdIt = imageInfo.pendingQsriSyncFds.erase(syncFdIt);
-                    close(syncFd);
+                    syncHelper->close(syncFd);
                 } else {
                     if (errno != ETIME) {
                         ALOGE("%s: Failed to wait for pending QSRI sync: sterror: %s errno: %d",
@@ -7199,7 +7210,7 @@
                 }
             }
 
-            int syncFdDup = dup(*fd);
+            int syncFdDup = syncHelper->dup(*fd);
             if (syncFdDup < 0) {
                 ALOGE("%s: Failed to dup() QSRI sync fd : sterror: %s errno: %d",
                       __func__, strerror(errno), errno);
@@ -7248,8 +7259,10 @@
             int syncFd;
             result = exportSyncFdForQSRILocked(image, &syncFd);
 
-            if (syncFd >= 0)
-                close(syncFd);
+            if (syncFd >= 0) {
+                auto* syncHelper = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->syncHelper();
+                syncHelper->close(syncFd);
+            }
         }
 
         return result;