From 988e021960eb372be50038fdf0b2874f063c02b6 Mon Sep 17 00:00:00 2001
From: Adam Jackson <ajax@redhat.com>
Date: Tue, 11 Oct 2016 16:16:38 -0400
Subject: [PATCH] egl: Use eglGetPlatformDisplay not eglGetDisplay

The latter requires the implementation to guess which kind of display it
is. Different implementations do different thing, in particular glvnd
does something different from Mesa, and it's better to be explicit about
what we need.

Signed-off-by: Adam Jackson <ajax@redhat.com>
---
 cogl/cogl-egl.h                       |  1 -
 cogl/winsys/cogl-winsys-egl-kms.c     |  3 ++-
 cogl/winsys/cogl-winsys-egl-private.h | 33 +++++++++++++++++++++++++++++++++
 cogl/winsys/cogl-winsys-egl-wayland.c |  3 ++-
 cogl/winsys/cogl-winsys-egl-x11.c     |  2 +-
 5 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/cogl/cogl-egl.h b/cogl/cogl-egl.h
index cea7b10..5dac55f 100644
--- a/cogl/cogl-egl.h
+++ b/cogl/cogl-egl.h
@@ -98,7 +98,6 @@ cogl_egl_context_get_egl_display (CoglContext *context);
 EGLContext
 cogl_egl_context_get_egl_context (CoglContext *context);
 
-
 COGL_END_DECLS
 
 /* The gobject introspection scanner seems to parse public headers in
diff --git a/cogl/winsys/cogl-winsys-egl-kms.c b/cogl/winsys/cogl-winsys-egl-kms.c
index 4da1f14..ae9f6fc 100644
--- a/cogl/winsys/cogl-winsys-egl-kms.c
+++ b/cogl/winsys/cogl-winsys-egl-kms.c
@@ -342,7 +342,8 @@ _cogl_winsys_renderer_connect (CoglRenderer *renderer,
       goto fail;
     }
 
-  egl_renderer->edpy = eglGetDisplay ((EGLNativeDisplayType)kms_renderer->gbm);
+  egl_renderer->edpy = cogl_winsys_egl_get_display(EGL_PLATFORM_GBM_KHR,
+                                                   kms_renderer->gbm);
   if (egl_renderer->edpy == EGL_NO_DISPLAY)
     {
       _cogl_set_error (error, COGL_WINSYS_ERROR,
diff --git a/cogl/winsys/cogl-winsys-egl-private.h b/cogl/winsys/cogl-winsys-egl-private.h
index 5d21b4f..27ac25c 100644
--- a/cogl/winsys/cogl-winsys-egl-private.h
+++ b/cogl/winsys/cogl-winsys-egl-private.h
@@ -200,4 +200,37 @@ CoglBool
 _cogl_winsys_egl_renderer_connect_common (CoglRenderer *renderer,
                                           CoglError **error);
 
+static inline EGLDisplay
+cogl_winsys_egl_get_display (EGLint type, void *native)
+{
+  EGLDisplay dpy = NULL;
+  const char *client_exts = eglQueryString (NULL, EGL_EXTENSIONS);
+
+  if (g_strstr_len (client_exts, -1, "EGL_KHR_platform_base"))
+    {
+      PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display =
+	(void *) eglGetProcAddress ("eglGetPlatformDisplay");
+
+      if (get_platform_display)
+	dpy = get_platform_display (type, native, NULL);
+
+      if (dpy)
+	return dpy;
+    }
+
+  if (g_strstr_len (client_exts, -1, "EGL_EXT_platform_base"))
+    {
+      PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display =
+	(void *) eglGetProcAddress ("eglGetPlatformDisplayEXT");
+
+      if (get_platform_display)
+	dpy = get_platform_display (type, native, NULL);
+
+      if (dpy)
+	return dpy;
+    }
+
+  return eglGetDisplay ((EGLNativeDisplayType) native);
+}
+
 #endif /* __COGL_WINSYS_EGL_PRIVATE_H */
diff --git a/cogl/winsys/cogl-winsys-egl-wayland.c b/cogl/winsys/cogl-winsys-egl-wayland.c
index 2e22052..463041b 100644
--- a/cogl/winsys/cogl-winsys-egl-wayland.c
+++ b/cogl/winsys/cogl-winsys-egl-wayland.c
@@ -289,7 +289,8 @@ _cogl_winsys_renderer_connect (CoglRenderer *renderer,
     }
 
   egl_renderer->edpy =
-    eglGetDisplay ((EGLNativeDisplayType) wayland_renderer->wayland_display);
+    cogl_winsys_egl_get_display (EGL_PLATFORM_WAYLAND_KHR,
+                                 wayland_renderer->wayland_display);
 
   if (!_cogl_winsys_egl_renderer_connect_common (renderer, error))
     goto error;
diff --git a/cogl/winsys/cogl-winsys-egl-x11.c b/cogl/winsys/cogl-winsys-egl-x11.c
index 724a4d0..a7e9c2f 100644
--- a/cogl/winsys/cogl-winsys-egl-x11.c
+++ b/cogl/winsys/cogl-winsys-egl-x11.c
@@ -278,7 +278,7 @@ _cogl_winsys_renderer_connect (CoglRenderer *renderer,
     goto error;
 
   egl_renderer->edpy =
-    eglGetDisplay ((EGLNativeDisplayType) xlib_renderer->xdpy);
+    cogl_winsys_egl_get_display (EGL_PLATFORM_X11_KHR, xlib_renderer->xdpy);
 
   if (!_cogl_winsys_egl_renderer_connect_common (renderer, error))
     goto error;
-- 
2.9.3

