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

Mathias Fröhlich Mathias.Froehlich at gmx.net
Fri Apr 5 13:57:33 UTC 2019


Hi Emil,

Now that I have been putting together a test case for the the actual use
I do see some issues with the pbuffer code path. Well - still investigating
if the test is wrong or the result.

In the mean time some small comments inline below ...

Mathias

On Thursday, 4 April 2019 17:25:28 CEST Emil Velikov wrote:
> 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.
> 
> 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
> 
> 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)
> 
> Cc: Mathias Fröhlich <Mathias.Froehlich at gmx.net>
> Cc: Marek Olšák <maraeo at gmail.com>
> Signed-off-by: Emil Velikov <emil.velikov at collabora.com>
> ---
>  src/egl/Android.mk                     |   1 +
>  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 | 388 +++++++++++++++++++++++++
>  src/egl/main/eglapi.c                  |  13 +-
>  src/egl/main/egldevice.c               |  16 +
>  src/egl/main/egldevice.h               |   3 +
>  src/egl/main/egldisplay.c              | 109 ++++++-
>  src/egl/main/egldisplay.h              |  11 +
>  src/egl/main/eglglobals.c              |   1 +
>  src/egl/meson.build                    |   1 +
>  12 files changed, 550 insertions(+), 12 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..6c886a5aefb 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_android.c \
>  	drivers/dri2/platform_android.c

That one duplicates the file.
The hunk is probably no longer needed?

>  
>  LOCAL_CFLAGS := \
> diff --git a/src/egl/Makefile.am b/src/egl/Makefile.am
> index 349779feb4e..6a74e692b59 100644
> --- a/src/egl/Makefile.am
> +++ b/src/egl/Makefile.am
> @@ -106,6 +106,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 6acc99aa62a..d0e8a70b48e 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 aa143deb867..a4064f04043 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..6d95614e93c
> --- /dev/null
> +++ b/src/egl/drivers/dri2/platform_device.c
> @@ -0,0 +1,388 @@
> +/*
> + * 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_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 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)) {
> +      if (!disp->Options.ForceSoftware)

if (disp->Options.ForceSoftware)

The '!' in your code appears wrong to me.


> +         _eglLog(_EGL_WARNING, "Not allowed to force software rendering when "
> +                               "API explicitly selects a hardware device.");
> +      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");
> +      /* XXX: bail out until the null swrast mentioned above is fixed */
> +      return EGL_FALSE;
> +   } 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;
> +
> +   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 4696eca8f82..2707df86eb9 100644
> --- a/src/egl/main/eglapi.c
> +++ b/src/egl/main/eglapi.c
> @@ -412,6 +412,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);
>     }
> @@ -903,8 +906,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>
> @@ -919,7 +922,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);
>  
> @@ -1027,8 +1029,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
> @@ -1041,7 +1043,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 c5c9a21273a..dac2e8ed2bd 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 e8ef3d8ed93..59901e9795d 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 (disp->Platform == EGL_PLATFORM_DEVICE_EXT) {
> +         close(((_EGLDeviceDisplayOptions *)disp->Options.Platform)->internal_fd);
> +         free(disp->Options.Platform);
> +      }
> +
>        free(disp);
>     }
>     _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 */
>     disp = _eglGlobal.DisplayList;
>     while (disp) {
> -      if (disp->Platform == plat && disp->PlatformDisplay == plat_dpy)
> -         break;
> +      if (disp->Platform == plat && disp->PlatformDisplay == plat_dpy) {
> +         if (plat != _EGL_PLATFORM_DEVICE)
> +            break;
> +
> +         if (_eglSameDeviceDisplay(disp, plat_opt))
> +            break;
> +      }
>        disp = disp->Next;
>     }
>  
> @@ -544,3 +570,82 @@ _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.
> +          */
> +
> +         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 a7c2641b600..44dfad2f752 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,6 +150,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
>  {
> @@ -301,6 +308,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 38ffb512f1c..d9010f25976 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
> 






More information about the mesa-dev mailing list