From 784dcfa2094f3fb235ca4b60395708a1f87c0b1b Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Wed, 25 Dec 2019 08:28:19 +0100
Subject: Bug 1156 Regression (Bug 1417): Probe whether
 'eglGetPlatformDisplay(..)' is available before using

commit f4281b5ee80d7674134bfee357695a98382884a3 for Bug 1156 (DRM/GBM)
introduced the call to 'eglGetPlatformDisplay(..)' for known EGL-platforms.

However, 'eglGetPlatformDisplay(..)' is only available for EGL versions >= 1.5
or 'eglGetPlatformDisplayEXT(..)' if EGL extension 'EGL_EXT_platform_base' is available.

This patch adds a singular EGL version probe and a secondary extension fallback test
at first call using EGL_NO_DISPLAY on both EGL_VERSION and EGL_EXTENSION eglQueryString(..) calls.

If 'eglGetPlatformDisplay*(..)' is not available, simply use 'eglGetDisplay(..)'.

This regression also impacted Bug 1417 (Android bringup using current SDK + NDK),
i.e. disabled most Android devices as their EGL version is often 1.4.
---
 .../classes/jogamp/opengl/egl/EGLDisplayUtil.java  | 38 +++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

(limited to 'src/jogl')

diff --git a/src/jogl/classes/jogamp/opengl/egl/EGLDisplayUtil.java b/src/jogl/classes/jogamp/opengl/egl/EGLDisplayUtil.java
index 4dd09ebbd..868fb4f85 100644
--- a/src/jogl/classes/jogamp/opengl/egl/EGLDisplayUtil.java
+++ b/src/jogl/classes/jogamp/opengl/egl/EGLDisplayUtil.java
@@ -38,10 +38,12 @@ import com.jogamp.nativewindow.ToolkitLock;
 import com.jogamp.opengl.GLException;
 
 import jogamp.opengl.Debug;
+import jogamp.opengl.GLVersionNumber;
 
 import com.jogamp.common.ExceptionUtils;
 import com.jogamp.common.nio.Buffers;
 import com.jogamp.common.util.LongObjectHashMap;
+import com.jogamp.common.util.VersionNumber;
 import com.jogamp.nativewindow.egl.EGLGraphicsDevice;
 import com.jogamp.opengl.egl.EGL;
 import com.jogamp.opengl.egl.EGLExt;
@@ -226,6 +228,10 @@ public class EGLDisplayUtil {
         return eglPlatform;
     }
 
+    private static boolean eglGetPlatformDisplayProbed = false;
+    private static boolean eglGetPlatformDisplayAvail = false;
+    private static VersionNumber eglGetPlatformDisplayMinVersion = new VersionNumber(1, 5, 0);
+
     private static synchronized long eglGetDisplay(final long nativeDisplay_id)  {
         if( useSingletonEGLDisplay && null != singletonEGLDisplay ) {
             if(DEBUG) {
@@ -236,9 +242,39 @@ public class EGLDisplayUtil {
             return singletonEGLDisplay.eglDisplay;
         }
 
+        if( !eglGetPlatformDisplayProbed ) {
+            boolean viaVersion = false;
+            boolean viaExtension = false;
+            // A display of EGL_NO_DISPLAY is supported only if the EGL version is 1.5 or greater.
+            final GLVersionNumber eglVersion = GLVersionNumber.create( EGL.eglQueryString(EGL.EGL_NO_DISPLAY, EGL.EGL_VERSION) );
+            final int eglVersionErr = EGL.eglGetError();
+            eglGetPlatformDisplayAvail = EGL.EGL_SUCCESS == eglVersionErr &&
+                                         eglVersion.isValid() &&
+                                         eglVersion.compareTo(eglGetPlatformDisplayMinVersion) >= 0;
+            viaVersion = eglGetPlatformDisplayAvail;
+            final int eglExtsErr;
+            if( !eglGetPlatformDisplayAvail ) {
+                final String eglExts = EGL.eglQueryString(EGL.EGL_NO_DISPLAY, EGL.EGL_EXTENSIONS);
+                eglExtsErr = EGL.eglGetError();
+                if( EGL.EGL_SUCCESS == eglExtsErr && null != eglExts && eglExts.length() > 0 ) {
+                    if( eglExts.indexOf("EGL_EXT_platform_base") >= 0 ) {
+                        eglGetPlatformDisplayAvail = true;
+                        viaExtension = true;
+                    }
+                }
+            } else {
+                eglExtsErr = EGL.EGL_SUCCESS;
+            }
+            eglGetPlatformDisplayProbed = true;
+            if(DEBUG) {
+                System.err.println("EGLDisplayUtil.eglGetDisplay.p: eglGetPlatformDisplay available: "+eglGetPlatformDisplayAvail+
+                                   ", eglClientVersion '"+eglVersion+"' via[Version "+viaVersion+", err 0x"+Integer.toHexString(eglVersionErr)+
+                                                                              " / Extension "+viaExtension+", err 0x"+Integer.toHexString(eglExtsErr)+"]");
+            }
+        }
         final int eglPlatform = getEGLPlatformType(true);
         final long eglDisplay;
-        if( 0 != eglPlatform ) {
+        if( eglGetPlatformDisplayAvail && 0 != eglPlatform ) {
             eglDisplay = EGL.eglGetPlatformDisplay(eglPlatform, nativeDisplay_id, null);
         } else {
             eglDisplay = EGL.eglGetDisplay(nativeDisplay_id);
-- 
cgit v1.2.3