Fuchsia build and Magma display support

Support for tests va-info and mpeg2vldemo.

Change-Id: I35bd4a34a6aa7be1e24f53121aa0543ea61848db
diff --git a/BUILD.gn b/BUILD.gn
new file mode 100644
index 0000000..e495888
--- /dev/null
+++ b/BUILD.gn
@@ -0,0 +1,75 @@
+# Copyright 2021 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+
+libva_utils = "//third_party/intel/libva-utils"
+
+if (is_fuchsia) {
+  # Defines libva
+  import("fuchsia/libva-utils.gni")
+}
+
+config("vautils-common-public-config") {
+  include_dirs = [ "$libva_utils/common" ]
+}
+
+config("vautils-common-config") {
+  cflags = [
+    "-Wno-shorten-64-to-32",
+    "-Wno-unused-but-set-variable",
+  ]
+  if (is_fuchsia) {
+    defines = [ "HAVE_VA_MAGMA=1" ]
+  }
+}
+
+source_set("vautils-common") {
+  public_configs = [ ":vautils-common-public-config" ]
+  configs += [ ":vautils-common-config" ]
+  sources = [
+    "$libva_utils/common/va_display.c",
+    "$libva_utils/common/va_display.h",
+    "$libva_utils/common/va_display_magma.cpp",
+  ]
+  deps = [
+    "//sdk/lib/fdio",
+    "//src/graphics/lib/magma/src/libmagma",
+    libva,
+    "//zircon/system/ulib/zx",
+  ]
+}
+
+config("vainfo-config") {
+  cflags = [
+    "-Wno-sign-compare",
+    "-Wno-incompatible-pointer-types-discards-qualifiers",
+  ]
+}
+
+executable("vainfo-bin") {
+  testonly = true
+  sources = [ "$libva_utils/vainfo/vainfo.c" ]
+  configs += [ ":vainfo-config" ]
+  deps = [
+    ":vautils-common",
+    libva,
+  ]
+}
+
+config("mpeg2vldemo-config") {
+  cflags = [
+    "-Wno-gnu-designator",
+    "-Wno-c99-designator",
+  ]
+}
+
+executable("mpeg2vldemo-bin") {
+  testonly = true
+  sources = [ "$libva_utils/decode/mpeg2vldemo.cpp" ]
+  configs += [ ":mpeg2vldemo-config" ]
+  deps = [
+    ":vautils-common",
+    libva,
+  ]
+}
diff --git a/common/va_display.c b/common/va_display.c
index a6bbade..3d7c620 100644
--- a/common/va_display.c
+++ b/common/va_display.c
@@ -36,6 +36,7 @@
 extern const VADisplayHooks va_display_hooks_wayland;
 extern const VADisplayHooks va_display_hooks_x11;
 extern const VADisplayHooks va_display_hooks_drm;
+extern const VADisplayHooks va_display_hooks_magma;
 
 static const VADisplayHooks *g_display_hooks;
 static const VADisplayHooks *g_display_hooks_available[] = {
@@ -51,6 +52,9 @@
 #ifdef HAVE_VA_DRM
     &va_display_hooks_drm,
 #endif
+#ifdef HAVE_VA_MAGMA
+    &va_display_hooks_magma,
+#endif
 #endif
     NULL
 };
diff --git a/common/va_display_magma.cpp b/common/va_display_magma.cpp
new file mode 100644
index 0000000..6925a19
--- /dev/null
+++ b/common/va_display_magma.cpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2021 The Fuchsia Authors. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <filesystem>
+#include <lib/zx/channel.h>
+#include <lib/fdio/directory.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <va/va_magma.h>
+#include "va_display.h"
+
+// Use a global here following the pattern for the other implementations.
+static magma_device_t magma_device;
+
+static VADisplay va_open_display_magma(void)
+{
+    const uint64_t kIntelVendorId = 0x8086;
+
+    for (auto& p : std::filesystem::directory_iterator("/dev/class/gpu")) {
+      {
+        zx::channel local, remote;
+        zx_status_t zx_status = zx::channel::create(0 /*flags*/, &local, &remote);
+        assert(zx_status == ZX_OK);
+        zx_status = fdio_service_connect(p.path().c_str(), remote.release());
+        assert(zx_status == ZX_OK);
+        magma_status_t status = magma_device_import(local.release(), &magma_device);
+        assert(status == MAGMA_STATUS_OK);
+        if (status != MAGMA_STATUS_OK)
+            continue;
+      }
+      {
+        uint64_t vendor_id;
+        magma_status_t magma_status = magma_query2(magma_device, MAGMA_QUERY_VENDOR_ID, &vendor_id);
+        if (magma_status == MAGMA_STATUS_OK && vendor_id == kIntelVendorId) {
+          break;
+        }
+      }
+
+      magma_device_release(magma_device);
+      magma_device = {};
+    }
+
+    if (!magma_device)
+        return NULL;
+
+    return vaGetDisplayMagma(magma_device);
+}
+
+static void va_close_display_magma(VADisplay va_dpy)
+{
+    if (magma_device) {
+        magma_device_release(magma_device);
+        magma_device = {};
+    }
+}
+
+static VAStatus va_put_surface_magma(
+    VADisplay          va_dpy,
+    VASurfaceID        surface,
+    const VARectangle *src_rect,
+    const VARectangle *dst_rect
+)
+{
+    return VA_STATUS_ERROR_UNIMPLEMENTED;
+}
+
+extern "C" const VADisplayHooks va_display_hooks_magma = {
+    "magma",
+    va_open_display_magma,
+    va_close_display_magma,
+    va_put_surface_magma,
+};
diff --git a/fuchsia/BUILD.gn b/fuchsia/BUILD.gn
new file mode 100644
index 0000000..3823056
--- /dev/null
+++ b/fuchsia/BUILD.gn
@@ -0,0 +1,24 @@
+# Copyright 2021 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+
+import("//build/components.gni")
+
+fuchsia_test_component("vainfo-component") {
+  deps = [ "..:vainfo-bin" ]
+  manifest = "meta/vainfo.cmx"
+}
+
+fuchsia_test_package("vainfo") {
+  test_components = [ ":vainfo-component" ]
+}
+
+fuchsia_test_component("mpeg2vldemo-component") {
+  deps = [ "..:mpeg2vldemo-bin" ]
+  manifest = "meta/mpeg2vldemo.cmx"
+}
+
+fuchsia_test_package("mpeg2vldemo") {
+  test_components = [ ":mpeg2vldemo-component" ]
+}
diff --git a/fuchsia/libva-utils.gni b/fuchsia/libva-utils.gni
new file mode 100644
index 0000000..147b6e3
--- /dev/null
+++ b/fuchsia/libva-utils.gni
@@ -0,0 +1,6 @@
+# Copyright 2021 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+
+libva = "//third_party/intel/media-driver/fuchsia:va-intel"
diff --git a/fuchsia/meta/mpeg2vldemo.cmx b/fuchsia/meta/mpeg2vldemo.cmx
new file mode 100644
index 0000000..e7f52c6
--- /dev/null
+++ b/fuchsia/meta/mpeg2vldemo.cmx
@@ -0,0 +1,13 @@
+{
+    "include": [
+        "syslog/client.shard.cmx"
+    ],
+    "sandbox": {
+        "dev": [
+          "class/gpu"
+        ]
+    },
+    "program": {
+        "binary": "bin/mpeg2vldemo-bin"
+    }
+}
diff --git a/fuchsia/meta/vainfo.cmx b/fuchsia/meta/vainfo.cmx
new file mode 100644
index 0000000..6c3d7a2
--- /dev/null
+++ b/fuchsia/meta/vainfo.cmx
@@ -0,0 +1,13 @@
+{
+    "include": [
+        "syslog/client.shard.cmx"
+    ],
+    "sandbox": {
+        "dev": [
+          "class/gpu"
+        ]
+    },
+    "program": {
+        "binary": "bin/vainfo-bin"
+    }
+}
diff --git a/vainfo/vainfo.c b/vainfo/vainfo.c
index 915ce31..381449c 100644
--- a/vainfo/vainfo.c
+++ b/vainfo/vainfo.c
@@ -32,7 +32,7 @@
 
 #include "va_display.h"
 
-#ifdef ANDROID
+#if defined(ANDROID) || defined(__Fuchsia__)
 
 /* Macros generated from configure */
 #define LIBVA_VERSION_S "2.0.0"