Port to Fuchsia

Adds magma backend.

Change-Id: I76ef6241834195bf7c606a99eb22bac5a94220e7
diff --git a/README.fuchsia b/README.fuchsia
new file mode 100644
index 0000000..0a06932
--- /dev/null
+++ b/README.fuchsia
@@ -0,0 +1,6 @@
+Name: libva
+URL: https://01.org/linuxmedia/vaapi
+License: MIT
+License File: COPYING
+Upstream Git: https://github.com/intel/libva
+Description: Libva is an implementation for VA-API (Video Acceleration API).
diff --git a/meson.build b/meson.build
index ef1876c..65ba44b 100644
--- a/meson.build
+++ b/meson.build
@@ -75,7 +75,7 @@
 cc = meson.get_compiler('c')
 dl_dep = cc.find_library('dl', required : false)
 
-libdrm_dep = dependency('libdrm', version : '>= 2.4.60')
+libdrm_dep = dependency('libdrm', version : '>= 2.4.60', required: false)
 
 WITH_DRM = not get_option('disable_drm')
 
@@ -110,6 +110,12 @@
   WITH_WAYLAND = wayland_dep.found()
 endif
 
+WITH_MAGMA = false
+if get_option('with_magma') == 'yes'
+  WITH_MAGMA = true
+  libmagma_dep = dependency('magma', version : '>= 1.0', required: true)
+endif
+
 va_c_args = []
 if get_option('enable_va_messaging')
   va_c_args += ['-DENABLE_VA_MESSAGING=1']
@@ -120,8 +126,8 @@
   va_c_args += ['-DHAVE_GNUC_VISIBILITY_ATTRIBUTE']
 endif
 
-if (not WITH_DRM and not WITH_X11 and not WITH_WAYLAND)
-  error('Please install at least one backend dev files (DRM, X11, Wayland)')
+if (not WITH_DRM and not WITH_X11 and not WITH_WAYLAND and not WITH_MAGMA)
+  error('Please install at least one backend dev files (DRM, X11, Wayland, Magma)')
 endif
 
 subdir('va')
diff --git a/meson_options.txt b/meson_options.txt
index 1f9a96f..6753587 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -5,3 +5,4 @@
 option('with_wayland', type : 'combo', choices : ['yes', 'no', 'auto'], value : 'auto')
 option('enable_docs', type : 'boolean', value : false)
 option('enable_va_messaging', type : 'boolean', value : true)
+option('with_magma', type : 'combo', choices : ['yes', 'no'], value : 'no')
diff --git a/va/magma/va_magma.c b/va/magma/va_magma.c
new file mode 100644
index 0000000..e71f51e
--- /dev/null
+++ b/va/magma/va_magma.c
@@ -0,0 +1,79 @@
+/*
+ * 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 INTEL 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 "magma.h"
+#include "sysdeps.h"
+#include "va_backend.h"
+#include "va_internal.h"
+
+static int
+va_DisplayContextIsValid(VADisplayContextP pDisplayContext)
+{
+    return 1;
+}
+
+static void
+va_DisplayContextDestroy(VADisplayContextP pDisplayContext)
+{
+    if (!pDisplayContext)
+        return;
+
+    free(pDisplayContext->pDriverContext);
+    free(pDisplayContext);
+}
+
+static VAStatus
+va_DisplayContextGetDriverName(
+    VADisplayContextP pDisplayContext,
+    char            **driver_name_ptr
+)
+{
+    *driver_name_ptr = strdup("iHD");
+    return VA_STATUS_SUCCESS;
+}
+
+
+VADisplay
+vaGetDisplayMagma(magma_device_t device)
+{
+    // Must be free'd.
+    VADisplayContextP pDisplayContext = va_newDisplayContext();
+    if (!pDisplayContext)
+        return NULL;
+
+    pDisplayContext->vaIsValid = va_DisplayContextIsValid;
+    pDisplayContext->vaDestroy = va_DisplayContextDestroy;
+    pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
+    pDisplayContext->vaGetDriverNameByIndex = NULL;
+    pDisplayContext->vaGetNumCandidates = NULL;
+
+    // Must be free'd.
+    VADriverContextP pDriverContext = va_newDriverContext(pDisplayContext);
+    if (!pDriverContext)
+        return NULL;
+
+    pDriverContext->magma_device = device;
+
+    return pDisplayContext;
+}
diff --git a/va/magma/va_magma.h b/va/magma/va_magma.h
new file mode 100644
index 0000000..15934a4
--- /dev/null
+++ b/va/magma/va_magma.h
@@ -0,0 +1,42 @@
+/*
+ * 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 INTEL 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.
+ */
+
+#ifndef VA_MAGMA_H
+#define VA_MAGMA_H
+
+#include <va/va.h>
+#include <magma.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+VADisplay
+vaGetDisplayMagma(magma_device_t device);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* VA_MAGMA_H */
diff --git a/va/meson.build b/va/meson.build
index 69273de..988e9c0 100644
--- a/va/meson.build
+++ b/va/meson.build
@@ -256,3 +256,33 @@
     include_directories : configinc,
     dependencies : deps)
 endif
+
+if WITH_MAGMA
+  libva_magma_sources = [
+    'magma/va_magma.c',
+  ]
+
+  libva_magma_headers = [
+    'magma/va_magma.h',
+  ]
+  libva_magma_args = []
+
+  deps = [ libmagma_dep ]
+
+  install_headers(libva_magma_headers, subdir : 'va')
+
+  libva_magma = shared_library(
+    'va-magma',
+    sources : libva_magma_sources +
+              libva_magma_headers,
+    soversion : libva_lt_current,
+    version : libva_lt_version,
+    install : true,
+    c_args : libva_magma_args,
+    dependencies : deps + [ libva_dep ])
+
+  libva_magma_dep = declare_dependency(
+    link_with : libva_magma,
+    include_directories : configinc,
+    dependencies : deps)
+endif
diff --git a/va/va_backend.h b/va/va_backend.h
index 149b6a3..093e359 100644
--- a/va/va_backend.h
+++ b/va/va_backend.h
@@ -557,19 +557,22 @@
 
     void *handle;           /* dlopen handle */
 
-    /**
-     * \brief DRM state.
-     *
-     * This field holds driver specific data for DRM-based
-     * drivers. This structure is allocated from libva with
-     * calloc(). Do not deallocate from within VA driver
-     * implementations.
-     *
-     * All structures shall be derived from struct drm_state. So, for
-     * instance, this field holds a dri_state structure for VA/X11
-     * drivers that use the DRM protocol.
-     */
-    void *drm_state;
+	union {
+	    /**
+	     * \brief DRM state.
+	     *
+	     * This field holds driver specific data for DRM-based
+	     * drivers. This structure is allocated from libva with
+	     * calloc(). Do not deallocate from within VA driver
+	     * implementations.
+	     *
+	     * All structures shall be derived from struct drm_state. So, for
+	     * instance, this field holds a dri_state structure for VA/X11
+	     * drivers that use the DRM protocol.
+	     */
+	    void *drm_state;
+	    uintptr_t magma_device;
+	};
 
     void *glx;              /* opaque for GLX code */
 
diff --git a/va/va_trace.c b/va/va_trace.c
index da62221..52a4a7c 100644
--- a/va/va_trace.c
+++ b/va/va_trace.c
@@ -76,6 +76,9 @@
     return getthrid();
 #elif defined(__sun)
     return thr_self();
+#elif defined(__Fuchsia__)
+    // Not implemented; Fuchsia needs 64 bits for thread ID
+    return 0;
 #else
 #warning "Cannot get kernel thread identifier on this platform."
     return (intptr_t)pthread_self();