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

Emil Velikov emil.l.velikov at gmail.com
Tue Sep 4 18:33:05 UTC 2018


From: Emil Velikov <emil.velikov at collabora.com>

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)

Signed-off-by: Emil Velikov <emil.velikov at collabora.com>
---
TODO/XXX:
 - fold/reuse more of surfaceless
 - swrast is hacky/broken - needs some DRI changes that I'm working atm.
 - remove local define
---
 src/egl/Makefile.am                    |   3 +
 src/egl/drivers/dri2/egl_dri2.c        |   3 +
 src/egl/drivers/dri2/egl_dri2.h        |  13 +-
 src/egl/drivers/dri2/platform_device.c | 398 +++++++++++++++++++++++++
 src/egl/main/eglapi.c                  |  13 +-
 src/egl/main/egldisplay.c              | 112 ++++++-
 src/egl/main/egldisplay.h              |  11 +
 src/egl/main/eglglobals.c              |   1 +
 src/egl/meson.build                    |   1 +
 9 files changed, 543 insertions(+), 12 deletions(-)
 create mode 100644 src/egl/drivers/dri2/platform_device.c

diff --git a/src/egl/Makefile.am b/src/egl/Makefile.am
index bf7f87015b7..6699f4ea003 100644
--- a/src/egl/Makefile.am
+++ b/src/egl/Makefile.am
@@ -107,6 +107,9 @@ if HAVE_PLATFORM_SURFACELESS
 dri2_backend_FILES += drivers/dri2/platform_surfaceless.c
 endif
 
+# Unconditionally enable platform_device
+dri2_backend_FILES += drivers/dri2/platform_device.c
+
 if HAVE_PLATFORM_ANDROID
 AM_CFLAGS += $(ANDROID_CFLAGS)
 libEGL_common_la_LIBADD += $(ANDROID_LIBS)
diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
index c5fa935657e..7c2521f120f 100644
--- a/src/egl/drivers/dri2/egl_dri2.c
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -904,6 +904,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 4abe1ba1952..1a08b2dfdcc 100644
--- a/src/egl/drivers/dri2/egl_dri2.h
+++ b/src/egl/drivers/dri2/egl_dri2.h
@@ -330,10 +330,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;
 };
@@ -489,6 +489,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..15d69cedfd0
--- /dev/null
+++ b/src/egl/drivers/dri2/platform_device.c
@@ -0,0 +1,398 @@
+/*
+ * 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))
+      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;
+   }
+
+   dri2_surf->dri_drawable =
+      dri2_dpy->image_driver->createNewDrawable(dri2_dpy->dri_screen, config,
+                                                dri2_surf);
+   if (dri2_surf->dri_drawable == NULL) {
+      _eglError(EGL_BAD_ALLOC, "image->createNewDrawable");
+      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_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
+{
+   assert(!surf || surf->Type == EGL_PBUFFER_BIT);
+
+   /* From the EGL 1.5 spec:
+    *    If surface is a [...] pbuffer surface, eglSwapBuffers has no effect.
+    */
+   return EGL_TRUE;
+}
+
+static EGLBoolean
+device_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
+{
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
+   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(dpy, 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 = device_swap_buffers,
+   .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
+   .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 const __DRIextension *image_loader_extensions[] = {
+   &image_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 = ((_EGLDeviceDisplayOptions *)disp->Options.Platform)->internal_fd;
+   if (fd != -1) {
+      /* 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);
+
+      close(fd);
+      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 EGLBoolean
+device_probe_device(_EGLDisplay *disp)
+{
+   _EGLDevice *dev;
+   struct dri2_egl_display *dri2_dpy = disp->DriverData;
+   EGLBoolean swrast;
+   int fd;
+
+   /* Extension requires a PlatformDisplay - the EGLDevice. */
+   dev = disp->PlatformDisplay;
+   if (_eglDeviceSupports(dev, _EGL_DEVICE_DRM)) {
+      swrast = EGL_FALSE;
+      fd = device_get_fd(disp, dev);
+      if (fd < 0)
+         return EGL_FALSE;
+
+      dri2_dpy->driver_name = loader_get_driver_for_fd(fd);
+   } else if (_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE)) {
+      swrast = EGL_TRUE;
+      fd = -1;
+
+      /* XXX: Should really be a "null" version of kms_swrast */
+      dri2_dpy->driver_name = strdup("swrast");
+   } else {
+      assert(0);
+      return EGL_FALSE;
+   }
+
+   if (!dri2_dpy->driver_name)
+      return EGL_FALSE;
+
+   dri2_dpy->fd = fd;
+   disp->Device = dev;
+
+   if (swrast)
+      return dri2_load_driver_swrast(disp);
+
+   return dri2_load_driver_dri3(disp);
+}
+
+EGLBoolean
+dri2_initialize_device(_EGLDriver *drv, _EGLDisplay *disp)
+{
+   struct dri2_egl_display *dri2_dpy;
+   const char* err;
+
+   loader_set_logger(_eglLog);
+
+   dri2_dpy = calloc(1, sizeof *dri2_dpy);
+   if (!dri2_dpy)
+      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
+
+   dri2_dpy->fd = -1;
+   disp->DriverData = (void *) dri2_dpy;
+
+   if (!device_probe_device(disp)) {
+      err = "DRI2: failed to load driver";
+      goto cleanup;
+   }
+
+   dri2_dpy->loader_extensions = image_loader_extensions;
+
+   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 0f018ff62ff..e726fbb78cf 100644
--- a/src/egl/main/eglapi.c
+++ b/src/egl/main/eglapi.c
@@ -404,6 +404,9 @@ _eglGetPlatformDisplayCommon(EGLenum platform, void *native_display,
       dpy = _eglGetSurfacelessDisplay(native_display, attrib_list);
       break;
 #endif
+   case EGL_PLATFORM_DEVICE_EXT:
+      dpy = _eglGetDeviceDisplay(native_display, attrib_list);
+      break;
    default:
       RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, NULL);
    }
@@ -895,8 +898,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>
@@ -911,7 +914,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);
 
@@ -1019,8 +1021,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
@@ -1033,7 +1035,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/egldisplay.c b/src/egl/main/egldisplay.c
index 94f94bb96e6..7b0a6840216 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,11 +204,30 @@ _eglFiniDisplay(void)
          }
       }
 
+      if (dpy->Platform == EGL_PLATFORM_DEVICE_EXT) {
+         close(((_EGLDeviceDisplayOptions *)dpy->Options.Platform)->internal_fd);
+         free(dpy->Options.Platform);
+      }
+
       free(dpy);
    }
    _eglGlobal.DisplayList = NULL;
 }
 
+static bool
+_eglSameDeviceDisplay(_EGLDisplay *dpy, void *plat_opt)
+{
+   _EGLDeviceDisplayOptions *opt = dpy->Options.Platform;
+
+   /* Both opt and plat_opt are guaranteed to be non-NULL */
+   if (opt->attrib_fd == ((_EGLDeviceDisplayOptions *)plat_opt)->attrib_fd)
+      return true;
+
+   /* Multiple displays for the same native_display are allowed when separate
+    * FDs are used.
+    */
+   return false;
+}
 
 /**
  * Find the display corresponding to the specified native display, or create a
@@ -227,8 +248,13 @@ _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy, void *plat_opt)
    /* search the display list first */
    dpy = _eglGlobal.DisplayList;
    while (dpy) {
-      if (dpy->Platform == plat && dpy->PlatformDisplay == plat_dpy)
-         break;
+      if (dpy->Platform == plat && dpy->PlatformDisplay == plat_dpy) {
+         if (plat != _EGL_PLATFORM_DEVICE)
+            break;
+
+         if (_eglSameDeviceDisplay(dpy, plat_opt))
+            break;
+      }
       dpy = dpy->Next;
    }
 
@@ -544,3 +570,85 @@ _eglGetSurfacelessDisplay(void *native_display,
    return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS, native_display, NULL);
 }
 #endif /* HAVE_SURFACELESS_PLATFORM */
+
+
+_EGLDisplay*
+_eglGetDeviceDisplay(void *native_display,
+                     const EGLAttrib *attrib_list)
+{
+   _EGLDevice *dev;
+   _EGLDisplay *display;
+   _EGLDeviceDisplayOptions *opt;
+   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.
+          */
+
+         /* TODO: add the missing define to the public headers, sync our copy */
+#define EGL_DRM_MASTER_FD_EXT 0x333C
+
+         if (!_eglDeviceSupports(dev, _EGL_DEVICE_DRM) ||
+             attrib != EGL_DRM_MASTER_FD_EXT) {
+            _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
+            return NULL;
+         }
+
+         fd = (int) value;
+      }
+   }
+
+   opt = malloc(sizeof *opt);
+   if (!opt) {
+      _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
+      return NULL;
+   }
+
+   opt->attrib_fd = fd;
+   opt->internal_fd = -1;
+
+   display = _eglFindDisplay(_EGL_PLATFORM_DEVICE, native_display, (void *)opt);
+   if (!display) {
+      free(opt);
+      _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
+      return NULL;
+   }
+
+   /* TODO: Change Options.Platform under a lock, to avoid a race.
+    * X11 handling above need a similar fix.
+    */
+
+   /* Newly allocated display */
+   if (!display->Options.Platform) {
+      display->Options.Platform = opt;
+   } else {
+      free(opt);
+      opt = display->Options.Platform;
+   }
+
+   /* 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.
+    */
+   if (opt->attrib_fd != -1 && opt->internal_fd == -1) {
+      opt->internal_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+      if (opt->internal_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 f43d7b589fa..a64fc07788b 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
@@ -148,6 +149,12 @@ struct _egl_extensions
    EGLBoolean WL_create_wayland_buffer_from_image;
 };
 
+struct _egl_device_display_options
+{
+   int attrib_fd;
+   int internal_fd;
+};
+typedef struct _egl_device_display_options _EGLDeviceDisplayOptions;
 
 struct _egl_display
 {
@@ -300,6 +307,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 33d92533d15..2201673a8a3 100644
--- a/src/egl/main/eglglobals.c
+++ b/src/egl/main/eglglobals.c
@@ -83,6 +83,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 8c0ffea8b40..2b2aca152af 100644
--- a/src/egl/meson.build
+++ b/src/egl/meson.build
@@ -97,6 +97,7 @@ if with_dri2
     '-DDEFAULT_DRIVER_DIR="@0@"'.format(dri_search_path),
   ]
 
+  files_egl += files('drivers/dri2/platform_device.c')
   if with_platform_x11
     files_egl += files('drivers/dri2/platform_x11.c')
     incs_for_egl += inc_loader
-- 
2.18.0



More information about the mesa-dev mailing list