[Mesa-dev] [PATCH 8/8] egl: add EGL_platform_device support

Emil Velikov emil.l.velikov at gmail.com
Thu May 16 17:01:40 UTC 2019


This new 'platform' is added by default with no guards.

It is effectively a copy of the surfaceless one, with updated function
names and brand new probe function.

Due to the reuse, some of the ifdef HAVE_SURFACELESS_PLATFORM guards
have been dropped.

A worthy mention are the changes in _egFindDisplay, since the original
and dup'd fd are required, we make use of the plat_opt argument.

Note that no hacks for eglGetDisplay are added - the API works only with
the eglGetPlatformDisplay* API.

v2:
 - s/_eglCompareDeviceDisplay/_eglSameDeviceDisplay/ (Eric)
 - let ^^ return bool (Eric)
 - fixup meson build, move files() further up (Eric)
 - copy from plat. surfaceless w/o the visual cleanups
 - close and free when destroying the dpy
 - sprinkle a few _eglDeviceSupports
 - split fd handling into separate function
 - use directly the render node if no FD is given (Mathias)

v3:
 - s/dpy/disp/g
 - drop swap_buffers* callbacks
 - drop loader_set_logger()
 - drop local define
 - re-introduce _eglGetDRMDeviceRenderNode()
 - EGL_WARN on ForceSoftware with HW device - continue using the HW device
 - bail out for "EGL_MESA_device_software" until it's fixed
 - wire-up the Android build

v4:
 - use new style _eglFindDisplay()
 - split hw vs sw code paths
 - don't close the internal fd (already handled in FiniDisplay())
 - make swrast work (bit hacky bit will do for now)
 - Android for real, drop autotools
 - Correct HW + LIBGL_ALWAYS_SOFTWARE check
 - use the dri2_create_drawable() helper

v5:
 - enhance comment around fd checks (Mathias)
 - rebase for dri2_init_surface() changes

Cc: Mathias Fröhlich <Mathias.Froehlich at gmx.net>
Acked-by: Marek Olšák <marek.olsak at amd.com> (v4)
Signed-off-by: Emil Velikov <emil.velikov at collabora.com>
---
 src/egl/Android.mk                     |   1 +
 src/egl/drivers/dri2/egl_dri2.c        |   3 +
 src/egl/drivers/dri2/egl_dri2.h        |  13 +-
 src/egl/drivers/dri2/platform_device.c | 435 +++++++++++++++++++++++++
 src/egl/main/eglapi.c                  |  13 +-
 src/egl/main/egldevice.c               |  16 +
 src/egl/main/egldevice.h               |   3 +
 src/egl/main/egldisplay.c              |  67 ++++
 src/egl/main/egldisplay.h              |   7 +-
 src/egl/main/eglglobals.c              |   1 +
 src/egl/meson.build                    |   1 +
 11 files changed, 549 insertions(+), 11 deletions(-)
 create mode 100644 src/egl/drivers/dri2/platform_device.c

diff --git a/src/egl/Android.mk b/src/egl/Android.mk
index a9319f56ae7..01c33298ee7 100644
--- a/src/egl/Android.mk
+++ b/src/egl/Android.mk
@@ -36,6 +36,7 @@ include $(CLEAR_VARS)
 LOCAL_SRC_FILES := \
 	$(LIBEGL_C_FILES) \
 	$(dri2_backend_core_FILES) \
+	drivers/dri2/platform_device.c \
 	drivers/dri2/platform_android.c
 
 LOCAL_CFLAGS := \
diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
index 17a0176b646..7427f199e8a 100644
--- a/src/egl/drivers/dri2/egl_dri2.c
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -874,6 +874,9 @@ dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp)
    case _EGL_PLATFORM_SURFACELESS:
       ret = dri2_initialize_surfaceless(drv, disp);
       break;
+   case _EGL_PLATFORM_DEVICE:
+      ret = dri2_initialize_device(drv, disp);
+      break;
    case _EGL_PLATFORM_X11:
       ret = dri2_initialize_x11(drv, disp);
       break;
diff --git a/src/egl/drivers/dri2/egl_dri2.h b/src/egl/drivers/dri2/egl_dri2.h
index 5555bcea3ab..e122f31a539 100644
--- a/src/egl/drivers/dri2/egl_dri2.h
+++ b/src/egl/drivers/dri2/egl_dri2.h
@@ -331,10 +331,10 @@ struct dri2_egl_surface
    } color_buffers[3], *back;
 #endif
 
-#if defined(HAVE_SURFACELESS_PLATFORM)
-      __DRIimage           *front;
-      unsigned int         visual;
-#endif
+   /* surfaceless and device */
+   __DRIimage           *front;
+   unsigned int         visual;
+
    int out_fence_fd;
    EGLBoolean enable_out_fence;
 };
@@ -492,6 +492,11 @@ dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp)
 }
 #endif
 
+EGLBoolean
+dri2_initialize_device(_EGLDriver *drv, _EGLDisplay *disp);
+static inline void
+dri2_teardown_device(struct dri2_egl_display *dri2_dpy) { /* noop */ }
+
 void
 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw);
 
diff --git a/src/egl/drivers/dri2/platform_device.c b/src/egl/drivers/dri2/platform_device.c
new file mode 100644
index 00000000000..81725ae7de9
--- /dev/null
+++ b/src/egl/drivers/dri2/platform_device.c
@@ -0,0 +1,435 @@
+/*
+ * Mesa 3-D graphics library
+ *
+ * Copyright 2018 Collabora
+ *
+ * Based on platform_surfaceless, which has:
+ *
+ * Copyright (c) 2014 The Chromium OS Authors.
+ * Copyright © 2011 Intel Corporation
+ *
+ * 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 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.
+ */
+#ifdef HAVE_LIBDRM
+#include <xf86drm.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <dlfcn.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "egl_dri2.h"
+#include "egl_dri2_fallbacks.h"
+#include "loader.h"
+
+static __DRIimage*
+device_alloc_image(struct dri2_egl_display *dri2_dpy,
+                   struct dri2_egl_surface *dri2_surf)
+{
+   return dri2_dpy->image->createImage(
+            dri2_dpy->dri_screen,
+            dri2_surf->base.Width,
+            dri2_surf->base.Height,
+            dri2_surf->visual,
+            0,
+            NULL);
+}
+
+static void
+device_free_images(struct dri2_egl_surface *dri2_surf)
+{
+   struct dri2_egl_display *dri2_dpy =
+      dri2_egl_display(dri2_surf->base.Resource.Display);
+
+   if (dri2_surf->front) {
+      dri2_dpy->image->destroyImage(dri2_surf->front);
+      dri2_surf->front = NULL;
+   }
+}
+
+static int
+device_image_get_buffers(__DRIdrawable *driDrawable,
+                         unsigned int format,
+                         uint32_t *stamp,
+                         void *loaderPrivate,
+                         uint32_t buffer_mask,
+                         struct __DRIimageList *buffers)
+{
+   struct dri2_egl_surface *dri2_surf = loaderPrivate;
+   struct dri2_egl_display *dri2_dpy =
+      dri2_egl_display(dri2_surf->base.Resource.Display);
+
+   buffers->image_mask = 0;
+   buffers->front = NULL;
+   buffers->back = NULL;
+
+   /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
+    * the spec states that they have a back buffer but no front buffer, in
+    * contrast to pixmaps, which have a front buffer but no back buffer.
+    *
+    * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
+    * from the spec, following the precedent of Mesa's EGL X11 platform. The
+    * X11 platform correctly assigns pbuffers to single-buffered configs, but
+    * assigns the pbuffer a front buffer instead of a back buffer.
+    *
+    * Pbuffers in the X11 platform mostly work today, so let's just copy its
+    * behavior instead of trying to fix (and hence potentially breaking) the
+    * world.
+    */
+
+   if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
+
+      if (!dri2_surf->front)
+         dri2_surf->front =
+            device_alloc_image(dri2_dpy, dri2_surf);
+
+      buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
+      buffers->front = dri2_surf->front;
+   }
+
+   return 1;
+}
+
+static _EGLSurface *
+dri2_device_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
+                           _EGLConfig *conf, const EGLint *attrib_list)
+{
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
+   struct dri2_egl_surface *dri2_surf;
+   const __DRIconfig *config;
+
+   /* Make sure to calloc so all pointers
+    * are originally NULL.
+    */
+   dri2_surf = calloc(1, sizeof *dri2_surf);
+
+   if (!dri2_surf) {
+      _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
+      return NULL;
+   }
+
+   if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
+                          false, NULL))
+      goto cleanup_surface;
+
+   config = dri2_get_dri_config(dri2_conf, type,
+                                dri2_surf->base.GLColorspace);
+
+   if (!config) {
+      _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
+      goto cleanup_surface;
+   }
+
+   if (!dri2_create_drawable(dri2_dpy, config, dri2_surf))
+      goto cleanup_surface;
+
+   if (conf->RedSize == 5)
+      dri2_surf->visual = __DRI_IMAGE_FORMAT_RGB565;
+   else if (conf->AlphaSize == 0)
+      dri2_surf->visual = __DRI_IMAGE_FORMAT_XRGB8888;
+   else
+      dri2_surf->visual = __DRI_IMAGE_FORMAT_ARGB8888;
+
+   return &dri2_surf->base;
+
+   cleanup_surface:
+      free(dri2_surf);
+      return NULL;
+}
+
+static EGLBoolean
+device_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
+{
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
+
+   device_free_images(dri2_surf);
+
+   dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
+
+   dri2_fini_surface(surf);
+   free(dri2_surf);
+   return EGL_TRUE;
+}
+
+static _EGLSurface *
+dri2_device_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
+                                   _EGLConfig *conf, const EGLint *attrib_list)
+{
+   return dri2_device_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
+                                     attrib_list);
+}
+
+static EGLBoolean
+device_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
+{
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   static const struct {
+      const char *format_name;
+      unsigned int rgba_masks[4];
+   } visuals[] = {
+      { "ARGB8888", { 0xff0000, 0xff00, 0xff, 0xff000000 } },
+      { "RGB888",   { 0xff0000, 0xff00, 0xff, 0x0 } },
+      { "RGB565",   { 0x00f800, 0x07e0, 0x1f, 0x0 } },
+   };
+   unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
+   unsigned int config_count = 0;
+
+   for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
+      for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
+         struct dri2_egl_config *dri2_conf;
+
+         dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
+               config_count + 1, EGL_PBUFFER_BIT, NULL,
+               visuals[j].rgba_masks);
+
+         if (dri2_conf) {
+            if (dri2_conf->base.ConfigID == config_count + 1)
+               config_count++;
+            format_count[j]++;
+         }
+      }
+   }
+
+   for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
+      if (!format_count[i]) {
+         _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
+               visuals[i].format_name);
+      }
+   }
+
+   return (config_count != 0);
+}
+
+static const struct dri2_egl_display_vtbl dri2_device_display_vtbl = {
+   .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
+   .create_pbuffer_surface = dri2_device_create_pbuffer_surface,
+   .destroy_surface = device_destroy_surface,
+   .create_image = dri2_create_image_khr,
+   .swap_buffers_region = dri2_fallback_swap_buffers_region,
+   .set_damage_region = dri2_fallback_set_damage_region,
+   .post_sub_buffer = dri2_fallback_post_sub_buffer,
+   .copy_buffers = dri2_fallback_copy_buffers,
+   .query_buffer_age = dri2_fallback_query_buffer_age,
+   .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
+   .get_sync_values = dri2_fallback_get_sync_values,
+   .get_dri_drawable = dri2_surface_get_dri_drawable,
+};
+
+static void
+device_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
+{
+}
+
+static const __DRIimageLoaderExtension image_loader_extension = {
+   .base             = { __DRI_IMAGE_LOADER, 1 },
+   .getBuffers       = device_image_get_buffers,
+   .flushFrontBuffer = device_flush_front_buffer,
+};
+
+static void
+device_get_drawable_info(__DRIdrawable * draw,
+                         int *x, int *y, int *w, int *h,
+                         void *loaderPrivate)
+{
+   struct dri2_egl_surface *dri2_surf = loaderPrivate;
+
+   *x = *y = 0;
+   *w = dri2_surf->base.Width;
+   *h = dri2_surf->base.Height;
+}
+
+/* HACK: technically we should have swrast_null, instead of these. We
+ * get away since only pbuffers are supported, thus the callbacks are
+ * unused.
+ */
+static const __DRIswrastLoaderExtension swrast_loader_extension = {
+   .base            = { __DRI_SWRAST_LOADER, 1 },
+   .getDrawableInfo = device_get_drawable_info,
+   .putImage        = NULL,
+   .getImage        = NULL,
+};
+
+static const __DRIextension *image_loader_extensions[] = {
+   &image_loader_extension.base,
+   &image_lookup_extension.base,
+   &use_invalidate.base,
+   NULL,
+};
+
+/* HACK: second part of the hack above. */
+static const __DRIextension *swrast_loader_extensions[] = {
+   &swrast_loader_extension.base,
+   &image_lookup_extension.base,
+   &use_invalidate.base,
+   NULL,
+};
+
+static int
+device_get_fd(_EGLDisplay *disp, _EGLDevice *dev)
+{
+#ifdef HAVE_LIBDRM
+   int fd = disp->Options.fd;
+   /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
+    * and invalid one is 0.
+    */
+   if (fd) {
+      /* According to the spec - if the FD does not match the EGLDevice
+       * behaviour is undefined.
+       *
+       * Add a trivial sanity check since it doesn't cost us anything.
+       */
+      if (dev != _eglAddDevice(fd, false))
+         return -1;
+
+      /* No EGL_EXT_output* extensions are supported, hence no master perms
+       * are needed. Get the render one - otherwise drivers might error out.
+       */
+      char *node = drmGetRenderDeviceNameFromFd(fd);
+
+      /* Don't close the internal fd, get render node one based on it. */
+      fd = loader_open_device(node);
+      free(node);
+      return fd;
+   }
+   const char *node = _eglGetDRMDeviceRenderNode(dev);
+   return loader_open_device(node);
+#else
+   _eglLog(_EGL_FATAL, "Driver bug: Built without libdrm, yet using a HW device");
+   return -1;
+#endif
+}
+
+static bool
+device_probe_device(_EGLDisplay *disp)
+{
+   struct dri2_egl_display *dri2_dpy = disp->DriverData;
+
+   if (disp->Options.ForceSoftware)
+      _eglLog(_EGL_WARNING, "Not allowed to force software rendering when "
+                            "API explicitly selects a hardware device.");
+   dri2_dpy->fd = device_get_fd(disp, disp->Device);
+   if (dri2_dpy->fd < 0)
+      return false;
+
+   dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
+   if (!dri2_dpy->driver_name)
+      goto err_name;
+
+   if (!dri2_load_driver_dri3(disp))
+      goto err_load;
+
+   dri2_dpy->loader_extensions = image_loader_extensions;
+   return true;
+
+err_load:
+   free(dri2_dpy->driver_name);
+   dri2_dpy->driver_name = NULL;
+
+err_name:
+   close(dri2_dpy->fd);
+   dri2_dpy->fd = -1;
+   return false;
+
+}
+
+static bool
+device_probe_device_sw(_EGLDisplay *disp)
+{
+   struct dri2_egl_display *dri2_dpy = disp->DriverData;
+
+   dri2_dpy->fd = -1;
+   dri2_dpy->driver_name = strdup("swrast");
+   if (!dri2_dpy->driver_name)
+      return false;
+
+   /* HACK: should be driver_swrast_null */
+   if (!dri2_load_driver_swrast(disp)) {
+      free(dri2_dpy->driver_name);
+      dri2_dpy->driver_name = NULL;
+      return false;
+   }
+
+   dri2_dpy->loader_extensions = swrast_loader_extensions;
+   return true;
+}
+
+EGLBoolean
+dri2_initialize_device(_EGLDriver *drv, _EGLDisplay *disp)
+{
+   _EGLDevice *dev;
+   struct dri2_egl_display *dri2_dpy;
+   const char* err;
+
+   dri2_dpy = calloc(1, sizeof *dri2_dpy);
+   if (!dri2_dpy)
+      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
+
+   /* Extension requires a PlatformDisplay - the EGLDevice. */
+   dev = disp->PlatformDisplay;
+
+   dri2_dpy->fd = -1;
+   disp->Device = dev;
+   disp->DriverData = (void *) dri2_dpy;
+   err = "DRI2: failed to load driver";
+   if (_eglDeviceSupports(dev, _EGL_DEVICE_DRM)) {
+      if (!device_probe_device(disp))
+         goto cleanup;
+   } else if (_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE)) {
+      if (!device_probe_device_sw(disp))
+         goto cleanup;
+   } else {
+      _eglLog(_EGL_FATAL, "Driver bug: exposed device is neither DRM nor SOFTWARE one");
+      return EGL_FALSE;
+   }
+
+   if (!dri2_create_screen(disp)) {
+      err = "DRI2: failed to create screen";
+      goto cleanup;
+   }
+
+   if (!dri2_setup_extensions(disp)) {
+      err = "DRI2: failed to find required DRI extensions";
+      goto cleanup;
+   }
+
+   dri2_setup_screen(disp);
+
+   if (!device_add_configs_for_visuals(drv, disp)) {
+      err = "DRI2: failed to add configs";
+      goto cleanup;
+   }
+
+   /* Fill vtbl last to prevent accidentally calling virtual function during
+    * initialization.
+    */
+   dri2_dpy->vtbl = &dri2_device_display_vtbl;
+
+   return EGL_TRUE;
+
+cleanup:
+   dri2_display_destroy(disp);
+   return _eglError(EGL_NOT_INITIALIZED, err);
+}
diff --git a/src/egl/main/eglapi.c b/src/egl/main/eglapi.c
index 05c529ba0a7..ed160e65b20 100644
--- a/src/egl/main/eglapi.c
+++ b/src/egl/main/eglapi.c
@@ -406,6 +406,9 @@ _eglGetPlatformDisplayCommon(EGLenum platform, void *native_display,
       disp = _eglGetSurfacelessDisplay(native_display, attrib_list);
       break;
 #endif
+   case EGL_PLATFORM_DEVICE_EXT:
+      disp = _eglGetDeviceDisplay(native_display, attrib_list);
+      break;
    default:
       RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, NULL);
    }
@@ -923,8 +926,8 @@ _eglCreateWindowSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
    if (native_window == NULL)
       RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
 
-#ifdef HAVE_SURFACELESS_PLATFORM
-   if (disp && disp->Platform == _EGL_PLATFORM_SURFACELESS) {
+   if (disp && (disp->Platform == _EGL_PLATFORM_SURFACELESS ||
+                disp->Platform == _EGL_PLATFORM_DEVICE)) {
       /* From the EGL_MESA_platform_surfaceless spec (v1):
        *
        *    eglCreatePlatformWindowSurface fails when called with a <display>
@@ -939,7 +942,6 @@ _eglCreateWindowSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
        */
       RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
    }
-#endif
 
    _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
 
@@ -1050,8 +1052,8 @@ _eglCreatePixmapSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
    _EGLSurface *surf;
    EGLSurface ret;
 
-#if HAVE_SURFACELESS_PLATFORM
-   if (disp && disp->Platform == _EGL_PLATFORM_SURFACELESS) {
+   if (disp && (disp->Platform == _EGL_PLATFORM_SURFACELESS ||
+                disp->Platform == _EGL_PLATFORM_DEVICE)) {
       /* From the EGL_MESA_platform_surfaceless spec (v1):
        *
        *   [Like eglCreatePlatformWindowSurface,] eglCreatePlatformPixmapSurface
@@ -1064,7 +1066,6 @@ _eglCreatePixmapSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
        */
       RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
    }
-#endif
 
    _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
 
diff --git a/src/egl/main/egldevice.c b/src/egl/main/egldevice.c
index 82af1f47fed..76b8960fa5b 100644
--- a/src/egl/main/egldevice.c
+++ b/src/egl/main/egldevice.c
@@ -202,6 +202,22 @@ _eglDeviceSupports(_EGLDevice *dev, _EGLDeviceExtension ext)
    };
 }
 
+/* Ideally we'll have an extension which passes the render node,
+ * instead of the card one + magic.
+ *
+ * Then we can move this in _eglQueryDeviceStringEXT below. Until then
+ * keep it separate.
+ */
+const char *
+_eglGetDRMDeviceRenderNode(_EGLDevice *dev)
+{
+#ifdef HAVE_LIBDRM
+   return dev->device->nodes[DRM_NODE_RENDER];
+#else
+   return NULL;
+#endif
+}
+
 EGLBoolean
 _eglQueryDeviceAttribEXT(_EGLDevice *dev, EGLint attribute,
                          EGLAttrib *value)
diff --git a/src/egl/main/egldevice.h b/src/egl/main/egldevice.h
index 883f96f8e30..83a47d5eacc 100644
--- a/src/egl/main/egldevice.h
+++ b/src/egl/main/egldevice.h
@@ -68,6 +68,9 @@ typedef enum _egl_device_extension _EGLDeviceExtension;
 EGLBoolean
 _eglDeviceSupports(_EGLDevice *dev, _EGLDeviceExtension ext);
 
+const char *
+_eglGetDRMDeviceRenderNode(_EGLDevice *dev);
+
 EGLBoolean
 _eglQueryDeviceAttribEXT(_EGLDevice *dev, EGLint attribute,
                          EGLAttrib *value);
diff --git a/src/egl/main/egldisplay.c b/src/egl/main/egldisplay.c
index 422b473844e..91655b73454 100644
--- a/src/egl/main/egldisplay.c
+++ b/src/egl/main/egldisplay.c
@@ -35,12 +35,14 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
 #include "c11/threads.h"
 #include "util/u_atomic.h"
 
 #include "eglcontext.h"
 #include "eglcurrent.h"
 #include "eglsurface.h"
+#include "egldevice.h"
 #include "egldisplay.h"
 #include "egldriver.h"
 #include "eglglobals.h"
@@ -202,6 +204,13 @@ _eglFiniDisplay(void)
          }
       }
 
+
+      /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
+       * and invalid one is 0.
+       */
+      if (disp->Options.fd)
+         close(disp->Options.fd);
+
       free(disp->Options.Attribs);
       free(disp);
    }
@@ -557,3 +566,61 @@ _eglGetSurfacelessDisplay(void *native_display,
                           attrib_list);
 }
 #endif /* HAVE_SURFACELESS_PLATFORM */
+
+
+_EGLDisplay*
+_eglGetDeviceDisplay(void *native_display,
+                     const EGLAttrib *attrib_list)
+{
+   _EGLDevice *dev;
+   _EGLDisplay *display;
+   int fd = -1;
+
+   dev = _eglLookupDevice(native_display);
+   if (!dev) {
+      _eglError(EGL_BAD_PARAMETER, "eglGetPlatformDisplay");
+      return NULL;
+   }
+
+   if (attrib_list) {
+      for (int i = 0; attrib_list[i] != EGL_NONE; i += 2) {
+         EGLAttrib attrib = attrib_list[i];
+         EGLAttrib value = attrib_list[i + 1];
+
+         /* EGL_EXT_platform_device does not recognize any attributes,
+          * EGL_EXT_device_drm adds the optional EGL_DRM_MASTER_FD_EXT.
+          */
+
+         if (!_eglDeviceSupports(dev, _EGL_DEVICE_DRM) ||
+             attrib != EGL_DRM_MASTER_FD_EXT) {
+            _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
+            return NULL;
+         }
+
+         fd = (int) value;
+      }
+   }
+
+   display = _eglFindDisplay(_EGL_PLATFORM_DEVICE, native_display, attrib_list);
+   if (!display) {
+      _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
+      return NULL;
+   }
+
+   /* If the fd is explicitly provided and we did not dup() it yet, do so.
+    * The spec mandates that we do so, since we'll need it past the
+    * eglGetPlatformDispay call.
+    *
+    * The new fd is guaranteed to be 3 or greater.
+    */
+   if (fd != -1 && display->Options.fd == 0) {
+      display->Options.fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+      if (display->Options.fd == -1) {
+         /* Do not (really) need to teardown the display */
+         _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
+         return NULL;
+      }
+   }
+
+   return display;
+}
diff --git a/src/egl/main/egldisplay.h b/src/egl/main/egldisplay.h
index 369ab31187f..f73ff99fbee 100644
--- a/src/egl/main/egldisplay.h
+++ b/src/egl/main/egldisplay.h
@@ -50,6 +50,7 @@ enum _egl_platform_type {
    _EGL_PLATFORM_ANDROID,
    _EGL_PLATFORM_HAIKU,
    _EGL_PLATFORM_SURFACELESS,
+   _EGL_PLATFORM_DEVICE,
 
    _EGL_NUM_PLATFORMS,
    _EGL_INVALID_PLATFORM = -1
@@ -149,7 +150,6 @@ struct _egl_extensions
    EGLBoolean WL_create_wayland_buffer_from_image;
 };
 
-
 struct _egl_display
 {
    /* used to link displays */
@@ -168,6 +168,7 @@ struct _egl_display
    struct {
       EGLBoolean ForceSoftware; /**< Use software path only */
       EGLAttrib *Attribs;     /**< Platform-specific options */
+      int fd; /**< plaform device specific, local fd */
    } Options;
 
    /* these fields are set by the driver during init */
@@ -314,6 +315,10 @@ _eglGetSurfacelessDisplay(void *native_display,
                           const EGLAttrib *attrib_list);
 #endif
 
+_EGLDisplay*
+_eglGetDeviceDisplay(void *native_display,
+                     const EGLAttrib *attrib_list);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/egl/main/eglglobals.c b/src/egl/main/eglglobals.c
index 2fc64bc52c3..0d7270333ec 100644
--- a/src/egl/main/eglglobals.c
+++ b/src/egl/main/eglglobals.c
@@ -85,6 +85,7 @@ struct _egl_global _eglGlobal =
 #ifdef HAVE_SURFACELESS_PLATFORM
    " EGL_MESA_platform_surfaceless"
 #endif
+   " EGL_EXT_platform_device"
    "",
 
    .ClientExtensionString = NULL,
diff --git a/src/egl/meson.build b/src/egl/meson.build
index 3a0fd01d244..443693091b4 100644
--- a/src/egl/meson.build
+++ b/src/egl/meson.build
@@ -96,6 +96,7 @@ if with_dri2
   link_for_egl += [libloader, libxmlconfig]
   incs_for_egl += inc_loader
 
+  files_egl += files('drivers/dri2/platform_device.c')
   if with_platform_x11
     files_egl += files('drivers/dri2/platform_x11.c')
     if with_dri3
-- 
2.21.0



More information about the mesa-dev mailing list