Switch to goldfish address space device

Use goldfish address space device on fuchsia.

Test: none
Bug: 111137294
Change-Id: I1b340f21f745075848df613d2e47e5f169e8b802
Merged-In: Ibf86ba488c08c2ea5825fd271ae1a1dcc5a604da
diff --git a/BUILD.gn b/BUILD.gn
index 385ae8d..a6da1bd 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -99,16 +99,15 @@
     ]
 
     deps = [
-      "//zircon/public/fidl/fuchsia-hardware-goldfish:fuchsia-hardware-goldfish_c",
+      "//zircon/public/fidl/fuchsia-hardware-goldfish-address-space:fuchsia-hardware-goldfish-address-space_c",
       "//zircon/public/fidl/fuchsia-sysmem",
       "//zircon/public/lib/fdio",
-      "//zircon/public/lib/fzl",
       "//zircon/public/lib/trace",
     ]
 
     defines += [
-      "QEMU_PIPE_PATH=\"/dev/sys/platform/acpi/goldfish/goldfish-pipe\"",
-      "GOLDFISH_ADDRESS_SPACE_DEVICE_NAME=\"/dev/sys/platform/acpi/goldfish/goldfish-pipe\"",
+      "QEMU_PIPE_PATH=\"/dev/class/goldfish-pipe/000\"",
+      "GOLDFISH_ADDRESS_SPACE_DEVICE_NAME=\"/dev/class/goldfish-address-space/000\"",
     ]
   }
 }
diff --git a/shared/OpenglCodecCommon/goldfish_address_space.cpp b/shared/OpenglCodecCommon/goldfish_address_space.cpp
index afa6b85..bbc59c6 100644
--- a/shared/OpenglCodecCommon/goldfish_address_space.cpp
+++ b/shared/OpenglCodecCommon/goldfish_address_space.cpp
@@ -127,8 +127,9 @@
 }
 #elif __Fuchsia__
 #include <fcntl.h>
-#include <fuchsia/hardware/goldfish/c/fidl.h>
-#include <lib/fzl/fdio.h>
+#include <fuchsia/hardware/goldfish/address/space/c/fidl.h>
+#include <lib/fdio/fdio.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -136,22 +137,23 @@
 #include <zircon/syscalls.h>
 #include <zircon/syscalls/object.h>
 
-GoldfishAddressSpaceBlockProvider::GoldfishAddressSpaceBlockProvider()
-    : m_fd(::open(GOLDFISH_ADDRESS_SPACE_DEVICE_NAME, O_RDWR)) {}
+GoldfishAddressSpaceBlockProvider::GoldfishAddressSpaceBlockProvider() {
+    fdio_get_service_handle(::open(GOLDFISH_ADDRESS_SPACE_DEVICE_NAME, O_RDWR), &m_channel);
+}
 
 GoldfishAddressSpaceBlockProvider::~GoldfishAddressSpaceBlockProvider()
 {
-    ::close(m_fd);
+    zx_handle_close(m_channel);
 }
 
 GoldfishAddressSpaceBlock::GoldfishAddressSpaceBlock()
     : m_vmo(ZX_HANDLE_INVALID)
+    , m_channel(ZX_HANDLE_INVALID)
     , m_mmaped_ptr(NULL)
     , m_phys_addr(0)
     , m_host_addr(0)
     , m_offset(0)
-    , m_size(0)
-    , m_fd(-1) {}
+    , m_size(0) {}
 
 GoldfishAddressSpaceBlock::~GoldfishAddressSpaceBlock()
 {
@@ -166,7 +168,7 @@
     m_host_addr = rhs.m_host_addr;
     m_offset = rhs.m_offset;
     m_size = rhs.m_size;
-    m_fd = rhs.m_fd;
+    m_channel = rhs.m_channel;
 
     return *this;
 }
@@ -182,36 +184,13 @@
         return false;
     }
 
-    {
-        fzl::FdioCaller caller{fbl::unique_fd(provider->m_fd)};
-
-        int32_t res = ZX_OK;
-        zx_status_t status =
-            fuchsia_hardware_goldfish_DeviceAllocateVmo(caller.borrow_channel(),
-                                                        size, &res, &m_vmo);
-        if (status != ZX_OK || res != ZX_OK) {
-            ALOGE("%s: allocate vmo failed: %d:%d", __func__, status, res);
-            provider->m_fd = caller.release().release();
-            return false;
-        }
-
-        zx_handle_t vmo_out;
-        status = zx_handle_duplicate(m_vmo, ZX_DEFAULT_VMO_RIGHTS, &vmo_out);
-        if (status != ZX_OK) {
-            ALOGE("%s: vmo dup failed: %d:%d", __func__, status);
-            provider->m_fd = caller.release().release();
-            return false;
-        }
-
-        status = fuchsia_hardware_goldfish_DeviceGetPhysicalAddress(
-            caller.borrow_channel(),
-            vmo_out, &res, &m_phys_addr);
-        provider->m_fd = caller.release().release();
-
-        if (status != ZX_OK || res != ZX_OK) {
-            ALOGE("%s: pin vmo failed: %d:%d", __func__, status, res);
-            return false;
-        }
+    int32_t res = ZX_OK;
+    zx_status_t status =
+        fuchsia_hardware_goldfish_address_space_DeviceAllocateBlock(
+            provider->m_channel, size, &res, &m_phys_addr, &m_vmo);
+    if (status != ZX_OK || res != ZX_OK) {
+        ALOGE("%s: allocate block failed: %d:%d", __func__, status, res);
+        return false;
     }
 
     m_offset = 0;
@@ -221,7 +200,7 @@
           (unsigned long long)m_offset,
           (unsigned long long)m_size);
 
-    m_fd = provider->m_fd;
+    m_channel = provider->m_channel;
     return true;
 }
 
@@ -284,6 +263,14 @@
     if (m_size) {
         zx_handle_close(m_vmo);
         m_vmo = ZX_HANDLE_INVALID;
+        int32_t res = ZX_OK;
+        zx_status_t status =
+            fuchsia_hardware_goldfish_address_space_DeviceDeallocateBlock(
+                m_channel, m_phys_addr, &res);
+        if (status != ZX_OK || res != ZX_OK) {
+            ALOGE("%s: deallocate block failed: %d:%d", __func__, status, res);
+        }
+        m_channel = ZX_HANDLE_INVALID;
         m_phys_addr = 0;
         m_host_addr = 0;
         m_offset = 0;
@@ -303,7 +290,7 @@
 
 bool GoldfishAddressSpaceBlockProvider::is_opened()
 {
-    return m_fd >= 0;
+    return m_channel != ZX_HANDLE_INVALID;
 }
 #else
 #include <linux/types.h>
diff --git a/shared/OpenglCodecCommon/goldfish_address_space.h b/shared/OpenglCodecCommon/goldfish_address_space.h
index 637e20d..7ebb451 100644
--- a/shared/OpenglCodecCommon/goldfish_address_space.h
+++ b/shared/OpenglCodecCommon/goldfish_address_space.h
@@ -45,7 +45,11 @@
    GoldfishAddressSpaceBlockProvider &operator=(const GoldfishAddressSpaceBlockProvider &rhs);
 
    bool is_opened();
+#ifdef __Fuchsia__
+   uint32_t m_channel;
+#else
    int m_fd;
+#endif
 
    friend class GoldfishAddressSpaceBlock;
 };
@@ -75,13 +79,15 @@
 #else
 #ifdef __Fuchsia__
     uint32_t  m_vmo;
+    uint32_t  m_channel;
+#else
+    int       m_fd;
 #endif
     void     *m_mmaped_ptr;
     uint64_t  m_phys_addr;
     uint64_t  m_host_addr;
     uint64_t  m_offset;
     size_t    m_size;
-    int       m_fd;
 #endif
 };
 
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index c8e44b2..d3e476b 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -58,7 +58,6 @@
 #ifdef VK_USE_PLATFORM_FUCHSIA
 
 #include <cutils/native_handle.h>
-#include <fuchsia/hardware/goldfish/c/fidl.h>
 #include <fuchsia/sysmem/cpp/fidl.h>
 #include <lib/fdio/directory.h>
 #include <lib/fdio/fd.h>