dri: Introduce internal Mesa DRI driver loader extension.
authorEmma Anholt <emma@anholt.net>
Mon, 28 Nov 2022 20:50:59 +0000 (12:50 -0800)
committerMarge Bot <emma+marge@anholt.net>
Mon, 5 Dec 2022 19:26:41 +0000 (19:26 +0000)
All DRI loaders in Mesa (EGL, GLX, gbm) now require this ext and that the
driver come from a matching build.  This will let us use Mesa-internal
types and enums across the loader-driver bounary inside of Mesa.

Reviewed-by: Adam Jackson <ajax@redhat.com>
Acked-by: Eric Engestrom <eric@igalia.com>
LOL-YESed-by: Kristian Høgsberg <krh@bitplanet.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/20069>

18 files changed:
include/GL/internal/mesa_interface.h [new file with mode: 0644]
src/egl/drivers/dri2/egl_dri2.c
src/egl/drivers/dri2/egl_dri2.h
src/egl/meson.build
src/gallium/frontends/dri/dri2.c
src/gallium/frontends/dri/drisw.c
src/gallium/frontends/dri/kopper.c
src/gallium/frontends/dri/meson.build
src/gbm/backends/dri/gbm_dri.c
src/gbm/backends/dri/gbm_driint.h
src/glx/dri2_glx.c
src/glx/dri2_priv.h
src/glx/dri3_glx.c
src/glx/dri3_priv.h
src/glx/drisw_glx.c
src/glx/drisw_priv.h
src/loader/loader.c
src/loader/meson.build

diff --git a/include/GL/internal/mesa_interface.h b/include/GL/internal/mesa_interface.h
new file mode 100644 (file)
index 0000000..8e3ed9c
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright © 2022 Google LLC
+ *
+ * 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, sublicense,
+ * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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 MESA_INTERFACE_H
+#define MESA_INTERFACE_H
+
+#include "dri_interface.h"
+
+/* Mesa-internal interface between the GLX, GBM, and EGL DRI driver loaders, and
+ * the gallium dri_util.c code.
+ */
+
+typedef struct __DRImesaCoreExtensionRec __DRImesaCoreExtension;
+
+#define __DRI_MESA "DRI_Mesa"
+#define __DRI_MESA_VERSION 1
+
+/**  Core struct that appears alongside __DRI_CORE for Mesa-internal usage.
+ * Implemented in the top-level dri/drisw/kopper extension list.
+ */
+struct __DRImesaCoreExtensionRec {
+   __DRIextension base;
+
+   /* Version string for verifying that the DRI driver is from the same build as
+    * the loader.
+    */
+#define MESA_INTERFACE_VERSION_STRING PACKAGE_VERSION MESA_GIT_SHA1
+   const char *version_string;
+};
+
+#endif /* MESA_INTERFACE_H */
index 293cc3f..8be1436 100644 (file)
@@ -704,12 +704,14 @@ const __DRIimageLookupExtension image_lookup_extension = {
 
 static const struct dri_extension_match dri3_driver_extensions[] = {
    { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core), false },
+   { __DRI_MESA, 1, offsetof(struct dri2_egl_display, mesa), false },
    { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver), false },
    { __DRI_CONFIG_OPTIONS, 2, offsetof(struct dri2_egl_display, configOptions), true },
 };
 
 static const struct dri_extension_match dri2_driver_extensions[] = {
    { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core), false },
+   { __DRI_MESA, 1, offsetof(struct dri2_egl_display, mesa), false },
    { __DRI_DRI2, 4, offsetof(struct dri2_egl_display, dri2), false },
    { __DRI_CONFIG_OPTIONS, 2, offsetof(struct dri2_egl_display, configOptions), true },
 };
@@ -722,6 +724,7 @@ static const struct dri_extension_match dri2_core_extensions[] = {
 
 static const struct dri_extension_match swrast_driver_extensions[] = {
    { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core), false },
+   { __DRI_MESA, 1, offsetof(struct dri2_egl_display, mesa), false },
    { __DRI_SWRAST, 4, offsetof(struct dri2_egl_display, swrast), false },
    { __DRI_CONFIG_OPTIONS, 2, offsetof(struct dri2_egl_display, configOptions), true },
 };
index 2e283cb..e4c1587 100644 (file)
@@ -59,6 +59,7 @@ struct zwp_linux_dmabuf_feedback_v1;
 
 #include <GL/gl.h>
 #include <GL/internal/dri_interface.h>
+#include <GL/internal/mesa_interface.h>
 #include "kopper_interface.h"
 
 #ifdef HAVE_DRM_PLATFORM
@@ -226,6 +227,7 @@ struct dri2_egl_display
    const __DRIconfig **driver_configs;
    void *driver;
    const __DRIcoreExtension *core;
+   const __DRImesaCoreExtension *mesa;
    const __DRIimageDriverExtension *image_driver;
    const __DRIdri2Extension *dri2;
    const __DRIswrastExtension *swrast;
index a7c6471..a90f3ba 100644 (file)
@@ -93,6 +93,7 @@ if with_dri2
     'drivers/dri2/egl_dri2.c',
     'drivers/dri2/egl_dri2.h',
   )
+  files_egl += sha1_h
   deps_for_egl += idep_xmlconfig
   link_for_egl += libloader
   incs_for_egl += inc_loader
index 9b3c4b3..caff7bf 100644 (file)
@@ -29,7 +29,9 @@
  */
 
 #include <xf86drm.h>
+#include "git_sha1.h"
 #include "GL/mesa_glinterop.h"
+#include "GL/internal/mesa_interface.h"
 #include "util/disk_cache.h"
 #include "util/u_memory.h"
 #include "util/u_inlines.h"
@@ -2405,9 +2407,15 @@ static const struct __DRIBackendVtableExtensionRec galliumdrm_vtable = {
    .InitScreen = dri2_init_screen,
 };
 
+static const struct __DRImesaCoreExtensionRec mesaCoreExtension = {
+   .base = { __DRI_MESA, 1 },
+   .version_string = MESA_INTERFACE_VERSION_STRING,
+};
+
 /* This is the table of extensions that the loader will dlsym() for. */
 const __DRIextension *galliumdrm_driver_extensions[] = {
     &driCoreExtension.base,
+    &mesaCoreExtension.base,
     &driImageDriverExtension.base,
     &driDRI2Extension.base,
     &gallium_config_options.base,
@@ -2429,6 +2437,7 @@ static const struct __DRIBackendVtableExtensionRec dri_swrast_kms_vtable = {
 
 const __DRIextension *dri_swrast_kms_driver_extensions[] = {
     &driCoreExtension.base,
+    &mesaCoreExtension.base,
     &driImageDriverExtension.base,
     &swkmsDRI2Extension.base,
     &gallium_config_options.base,
index 4ca5134..ade9565 100644 (file)
@@ -26,6 +26,8 @@
  *
  **************************************************************************/
 
+#include "GL/internal/mesa_interface.h"
+#include "git_sha1.h"
 #include "util/format/u_format.h"
 #include "util/u_memory.h"
 #include "util/u_inlines.h"
@@ -631,9 +633,15 @@ const __DRIcopySubBufferExtension driSWCopySubBufferExtension = {
    .copySubBuffer               = driswCopySubBuffer,
 };
 
+static const struct __DRImesaCoreExtensionRec mesaCoreExtension = {
+   .base = { __DRI_MESA, 1 },
+   .version_string = MESA_INTERFACE_VERSION_STRING,
+};
+
 /* This is the table of extensions that the loader will dlsym() for. */
 const __DRIextension *galliumsw_driver_extensions[] = {
     &driCoreExtension.base,
+    &mesaCoreExtension.base,
     &driSWRastExtension.base,
     &driSWCopySubBufferExtension.base,
     &gallium_config_options.base,
index c406da3..4aa05f4 100644 (file)
@@ -21,6 +21,8 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include "GL/internal/mesa_interface.h"
+#include "git_sha1.h"
 #include "util/format/u_format.h"
 #include "util/u_memory.h"
 #include "util/u_inlines.h"
@@ -954,8 +956,14 @@ static const struct __DRIBackendVtableExtensionRec galliumvk_vtable = {
    .InitScreen = kopper_init_screen,
 };
 
+static const struct __DRImesaCoreExtensionRec mesaCoreExtension = {
+   .base = { __DRI_MESA, 1 },
+   .version_string = MESA_INTERFACE_VERSION_STRING,
+};
+
 const __DRIextension *galliumvk_driver_extensions[] = {
    &driCoreExtension.base,
+   &mesaCoreExtension.base,
    &driSWRastExtension.base,
    &driDRI2Extension.base,
    &driImageDriverExtension.base,
index f02aaeb..47dcdfd 100644 (file)
@@ -59,7 +59,7 @@ endif
 
 libdri = static_library(
   'dri',
-  files_libdri,
+  [ files_libdri, sha1_h ],
   include_directories : [
     inc_include, inc_util, inc_mesa, inc_mapi, inc_src, inc_gallium,
     inc_gallium_aux, inc_util,  inc_gallium_drivers,
index 177a1b2..18a2273 100644 (file)
@@ -294,11 +294,13 @@ static struct dri_extension_match dri_core_extensions[] = {
 
 static struct dri_extension_match gbm_dri_device_extensions[] = {
    { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core), false },
+   { __DRI_MESA, 1, offsetof(struct gbm_dri_device, mesa), false },
    { __DRI_DRI2, 4, offsetof(struct gbm_dri_device, dri2), false },
 };
 
 static struct dri_extension_match gbm_swrast_device_extensions[] = {
    { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core), false },
+   { __DRI_MESA, 1, offsetof(struct gbm_dri_device, mesa), false },
    { __DRI_SWRAST, 4, offsetof(struct gbm_dri_device, swrast), false },
    { __DRI_KOPPER, 1, offsetof(struct gbm_dri_device, kopper), true },
 };
index 31f6b67..383a1fc 100644 (file)
@@ -36,6 +36,7 @@
 
 #include <GL/gl.h> /* dri_interface needs GL types */
 #include "GL/internal/dri_interface.h"
+#include "GL/internal/mesa_interface.h"
 #include "kopper_interface.h"
 
 struct gbm_dri_surface;
@@ -71,6 +72,7 @@ struct gbm_dri_device {
    mtx_t mutex;
 
    const __DRIcoreExtension   *core;
+   const __DRImesaCoreExtension   *mesa;
    const __DRIdri2Extension   *dri2;
    const __DRI2fenceExtension *fence;
    const __DRIimageExtension  *image;
index 553ba1d..943c953 100644 (file)
@@ -1092,6 +1092,7 @@ dri2CreateScreen(int screen, struct glx_display * priv)
    static const struct dri_extension_match exts[] = {
        { __DRI_CORE, 1, offsetof(struct dri2_screen, core), false },
        { __DRI_DRI2, 4, offsetof(struct dri2_screen, dri2), false },
+       { __DRI_MESA, 1, offsetof(struct dri2_screen, mesa), false },
    };
    if (!loader_bind_extensions(psc, exts, ARRAY_SIZE(exts), extensions))
       goto handle_error;
index e8f1b64..837709f 100644 (file)
@@ -37,6 +37,8 @@
 extern "C" {
 #endif
 
+#include "GL/internal/mesa_interface.h"
+
 struct dri2_screen {
    struct glx_screen base;
 
@@ -44,6 +46,7 @@ struct dri2_screen {
    __GLXDRIscreen vtable;
    const __DRIdri2Extension *dri2;
    const __DRIcoreExtension *core;
+   const __DRImesaCoreExtension *mesa;
 
    const __DRI2flushExtension *f;
    const __DRI2configQueryExtension *config;
index 3754bf1..ecbefdd 100644 (file)
@@ -878,6 +878,7 @@ dri3_create_screen(int screen, struct glx_display * priv)
    static const struct dri_extension_match exts[] = {
        { __DRI_CORE, 1, offsetof(struct dri3_screen, core), false },
        { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri3_screen, image_driver), false },
+       { __DRI_MESA, 1, offsetof(struct dri3_screen, mesa), false },
    };
    if (!loader_bind_extensions(psc, exts, ARRAY_SIZE(exts), extensions))
       goto handle_error;
index 09dcefd..7820033 100644 (file)
@@ -60,6 +60,7 @@
 #include <xcb/sync.h>
 
 #include "loader_dri3_helper.h"
+#include "GL/internal/mesa_interface.h"
 
 struct dri3_display
 {
@@ -92,6 +93,7 @@ struct dri3_screen {
    const __DRIimageExtension *image;
    const __DRIimageDriverExtension *image_driver;
    const __DRIcoreExtension *core;
+   const __DRImesaCoreExtension *mesa;
    const __DRI2flushExtension *f;
    const __DRI2configQueryExtension *config;
    const __DRItexBufferExtension *texBuffer;
index e760ced..d934e4e 100644 (file)
@@ -960,6 +960,7 @@ driswCreateScreenDriver(int screen, struct glx_display *priv,
        { __DRI_SWRAST, 4, offsetof(struct drisw_screen, swrast), false },
        { __DRI_KOPPER, 1, offsetof(struct drisw_screen, kopper), true },
        { __DRI_COPY_SUB_BUFFER, 1, offsetof(struct drisw_screen, copySubBuffer), true },
+       { __DRI_MESA, 1, offsetof(struct drisw_screen, mesa), false },
    };
    if (!loader_bind_extensions(psc, exts, ARRAY_SIZE(exts), extensions))
       goto handle_error;
index a75f2c5..d1f445b 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <X11/extensions/XShm.h>
 #include "kopper_interface.h"
+#include "GL/internal/mesa_interface.h"
 
 struct drisw_display
 {
@@ -42,6 +43,7 @@ struct drisw_screen
    __DRIscreen *driScreen;
    __GLXDRIscreen vtable;
    const __DRIcoreExtension *core;
+   const __DRImesaCoreExtension *mesa;
    const __DRIswrastExtension *swrast;
    const __DRIkopperExtension *kopper;
    const __DRI2flushExtension *f;
index 5e27f84..dd6f837 100644 (file)
 #endif
 #include <GL/gl.h>
 #include <GL/internal/dri_interface.h>
+#include <GL/internal/mesa_interface.h>
 #include "loader.h"
 #include "util/os_file.h"
 #include "util/os_misc.h"
+#include "git_sha1.h"
 
 #ifdef HAVE_LIBDRM
 #include <xf86drm.h>
@@ -627,6 +629,18 @@ loader_bind_extensions(void *data,
          if (!match->optional)
             ret = false;
       }
+
+      /* The loaders rely on the loaded DRI drivers being from the same Mesa
+       * build so that we can reference the same structs on both sides.
+       */
+      if (strcmp(match->name, __DRI_MESA) == 0) {
+         const __DRImesaCoreExtension *mesa = (const __DRImesaCoreExtension *)*field;
+         if (strcmp(mesa->version_string, MESA_INTERFACE_VERSION_STRING) != 0) {
+            log_(_LOADER_FATAL, "DRI driver not from this Mesa build ('%s' vs '%s')\n",
+                 mesa->version_string, MESA_INTERFACE_VERSION_STRING);
+            ret = false;
+         }
+      }
    }
 
    return ret;
index 6334cb9..bdebcb7 100644 (file)
@@ -42,7 +42,7 @@ loader_c_args = [
 ]
 libloader = static_library(
   'loader',
-  ['loader_dri_helper.c', 'loader.c'],
+  ['loader_dri_helper.c', 'loader.c', sha1_h],
   c_args : loader_c_args,
   gnu_symbol_visibility : 'hidden',
   include_directories : [inc_include, inc_src, inc_util],