Merge "Mac build fix"
diff --git a/system/vulkan_enc/HostVisibleMemoryVirtualization.cpp b/system/vulkan_enc/HostVisibleMemoryVirtualization.cpp
index c51a722..6d2c2b1 100644
--- a/system/vulkan_enc/HostVisibleMemoryVirtualization.cpp
+++ b/system/vulkan_enc/HostVisibleMemoryVirtualization.cpp
@@ -25,12 +25,17 @@
 
 #include <set>
 
-#ifdef ANDROID
+#if defined(__ANDROID__) || defined(__linux__)
 #include <unistd.h>
 #include <errno.h>
 #endif
+
 #include <sys/mman.h>
 
+#if !defined(HOST_BUILD) && defined(VIRTIO_GPU)
+#include <xf86drm.h>
+#endif
+
 using android::base::guest::SubAllocator;
 
 namespace goldfish_vk {
@@ -260,27 +265,38 @@
     VkEncoder* enc,
     VkDevice device,
     HostMemAlloc* toDestroy) {
+#if !defined(HOST_BUILD) && defined(VIRTIO_GPU)
+    if (toDestroy->rendernodeFd >= 0) {
+
+        if (toDestroy->memoryAddr) {
+            int ret = munmap((void*)toDestroy->memoryAddr, toDestroy->memorySize);
+            if (ret != 0) {
+                ALOGE("%s: fail to unmap addr = 0x%" PRIx64", size = %d, ret = "
+                      "%d, errno = %d", __func__, toDestroy->memoryAddr,
+                      (int32_t)toDestroy->memorySize, ret, errno);
+            }
+        }
+
+        if (toDestroy->boCreated) {
+            ALOGV("%s: trying to destroy bo = %u\n", __func__,
+                  toDestroy->boHandle);
+            struct drm_gem_close drmGemClose = {};
+            drmGemClose.handle = toDestroy->boHandle;
+            int ret = drmIoctl(toDestroy->rendernodeFd, DRM_IOCTL_GEM_CLOSE, &drmGemClose);
+            if (ret != 0) {
+                ALOGE("%s: fail to close gem = %u, ret = %d, errno = %d\n",
+                      __func__, toDestroy->boHandle, ret, errno);
+            } else {
+                ALOGV("%s: successfully close gem = %u, ret = %d\n", __func__,
+                      toDestroy->boHandle, ret);
+            }
+        }
+    }
+#endif
 
     if (toDestroy->initResult != VK_SUCCESS) return;
     if (!toDestroy->initialized) return;
 
-#ifdef ANDROID
-    if (toDestroy->fd > 0) {
-
-        if (toDestroy->memoryAddr) {
-            int ret = munmap((void*)toDestroy->memoryAddr, toDestroy->memorySize);
-            ALOGE("%s: trying to unmap addr = 0x%" PRIx64", size = %d, ret = %d, errno = %d\n", __func__, toDestroy->memoryAddr, (int32_t)toDestroy->memorySize, ret, errno);
-        }
-
-        ALOGE("%s: trying to close fd = %d\n", __func__, toDestroy->fd);
-        int ret = close(toDestroy->fd);
-        if (ret != 0) {
-            ALOGE("%s: fail to close fd = %d, ret = %d, errno = %d\n", __func__, toDestroy->fd, ret, errno);
-        } else {
-            ALOGE("%s: successfully close fd = %d, ret = %d\n", __func__, toDestroy->fd, ret);
-        }
-    }
-#endif
 
     if (freeMemorySyncSupported) {
         enc->vkFreeMemorySyncGOOGLE(device, toDestroy->memory, nullptr, false /* no lock */);
diff --git a/system/vulkan_enc/HostVisibleMemoryVirtualization.h b/system/vulkan_enc/HostVisibleMemoryVirtualization.h
index 6501ab5..1a52c5a 100644
--- a/system/vulkan_enc/HostVisibleMemoryVirtualization.h
+++ b/system/vulkan_enc/HostVisibleMemoryVirtualization.h
@@ -83,7 +83,9 @@
     VkDeviceSize mappedSize = 0;
     uint8_t* mappedPtr = nullptr;
     android::base::guest::SubAllocator* subAlloc = nullptr;
-    int fd = -1;
+    int rendernodeFd = -1;
+    bool boCreated = false;
+    uint32_t boHandle = 0;
     uint64_t memoryAddr = 0;
     size_t memorySize = 0;
     bool isDeviceAddressMemoryAllocation = false;
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index 0bf1a5b..00d161c 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -3718,9 +3718,6 @@
 #if !defined(HOST_BUILD) && defined(VIRTIO_GPU)
                 uint64_t hvaSizeId[3];
 
-                int rendernodeFdForMem = drmOpenRender(128 /* RENDERNODE_MINOR */);
-                ALOGE("%s: render fd = %d\n", __func__, rendernodeFdForMem);
-
                 mLock.unlock();
                 enc->vkGetMemoryHostAddressInfoGOOGLE(
                         device, hostMemAlloc.memory,
@@ -3738,19 +3735,21 @@
                 drm_rc_blob.size = hvaSizeId[1];
 
                 int res = drmIoctl(
-                    rendernodeFdForMem, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &drm_rc_blob);
+                    mRendernodeFd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &drm_rc_blob);
 
                 if (res) {
-                    ALOGE("%s: Failed to resource create v2: sterror: %s errno: %d\n", __func__,
+                    ALOGE("%s: Failed to resource create blob: sterror: %s errno: %d\n", __func__,
                             strerror(errno), errno);
                     abort();
                 }
+                hostMemAlloc.boCreated = true;
+                hostMemAlloc.boHandle = drm_rc_blob.bo_handle;
 
                 drm_virtgpu_map map_info;
                 memset(&map_info, 0, sizeof(map_info));
                 map_info.handle = drm_rc_blob.bo_handle;
 
-                res = drmIoctl(rendernodeFdForMem, DRM_IOCTL_VIRTGPU_MAP, &map_info);
+                res = drmIoctl(mRendernodeFd, DRM_IOCTL_VIRTGPU_MAP, &map_info);
                 if (res) {
                     ALOGE("%s: Failed to virtgpu map: sterror: %s errno: %d\n", __func__,
                             strerror(errno), errno);
@@ -3758,21 +3757,22 @@
                 }
 
                 directMappedAddr = (uint64_t)(uintptr_t)
-                    mmap64(0, hvaSizeId[1], PROT_WRITE, MAP_SHARED, rendernodeFdForMem, map_info.offset);
+                    mmap64(0, hvaSizeId[1], PROT_WRITE, MAP_SHARED, mRendernodeFd, map_info.offset);
 
                 if ((void*)directMappedAddr == MAP_FAILED) {
                     ALOGE("%s: mmap of virtio gpu resource failed\n", __func__);
                     abort();
                 }
 
+                // Does not take  ownership for the device.
+                hostMemAlloc.rendernodeFd = mRendernodeFd;
                 hostMemAlloc.memoryAddr = directMappedAddr;
                 hostMemAlloc.memorySize = hvaSizeId[1];
 
                 // add the host's page offset
                 directMappedAddr += (uint64_t)(uintptr_t)(hvaSizeId[0]) & (PAGE_SIZE - 1);
-				directMapResult = VK_SUCCESS;
+                directMapResult = VK_SUCCESS;
 
-                hostMemAlloc.fd = rendernodeFdForMem;
 #endif // VK_USE_PLATFORM_ANDROID_KHR
             }