SDL: Android: prevent error EGL_BAD_DISPLAY while getting egl version without display

From 6be9c00970098930b36806827c919a84263a1005 Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Tue, 20 Apr 2021 13:46:25 +0200
Subject: [PATCH] Android: prevent error EGL_BAD_DISPLAY while getting egl
 version without display

There is an error "E libEGL  : validate_display:91 error 3008 (EGL_BAD_DISPLAY)"
that occurs when calling "eglQueryString(display, EGL_VERSION)", with EGL_NO_DISPLAY.

Khronos says "EGL_BAD_DISPLAY is generated if display is not an EGL display connection, unless display is EGL_NO_DISPLAY and name is EGL_EXTENSIONS."
but this was added in SDL with "EGL 1.5 allows querying for client version"
( https://github.com/libsdl-org/SDL/commit/56363ebf6124b345e1cfbd14fb6c0e654837910c )

In fact:
- it actually doesn't work on Android that has 1.5 egl client
- it works on desktop X11 (using SDL_VIDEO_X11_FORCE_EGL=1)

The commit moves the version call where it's used, eg inside the "if (platform) {"
and checks that "eglGetPlatformDisplay" has been correctly loaded.
---
 src/video/SDL_egl.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/src/video/SDL_egl.c b/src/video/SDL_egl.c
index 0eb4c85fc..ca40cb8ba 100644
--- a/src/video/SDL_egl.c
+++ b/src/video/SDL_egl.c
@@ -484,26 +484,28 @@ SDL_EGL_GetVersion(_THIS) {
 int
 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
 {
-    int egl_version_major, egl_version_minor;
     int library_load_retcode = SDL_EGL_LoadLibraryOnly(_this, egl_path);
     if (library_load_retcode != 0) {
         return library_load_retcode;
     }
 
-    /* EGL 1.5 allows querying for client version with EGL_NO_DISPLAY */
-    SDL_EGL_GetVersion(_this);
-
-    egl_version_major = _this->egl_data->egl_version_major;
-    egl_version_minor = _this->egl_data->egl_version_minor;
-
-    if (egl_version_major == 1 && egl_version_minor == 5) {
-        LOAD_FUNC(eglGetPlatformDisplay);
-    }
-
     _this->egl_data->egl_display = EGL_NO_DISPLAY;
+
 #if !defined(__WINRT__)
     if (platform) {
-        if (egl_version_major == 1 && egl_version_minor == 5) {
+        /* EGL 1.5 allows querying for client version with EGL_NO_DISPLAY
+         * --
+         * Khronos doc: "EGL_BAD_DISPLAY is generated if display is not an EGL display connection, unless display is EGL_NO_DISPLAY and name is EGL_EXTENSIONS."
+         * Therefore SDL_EGL_GetVersion() shouldn't work with uninitialized display.
+         * - it actually doesn't work on Android that has 1.5 egl client
+         * - it works on desktop X11 (using SDL_VIDEO_X11_FORCE_EGL=1) */
+        SDL_EGL_GetVersion(_this);
+
+        if (_this->egl_data->egl_version_major == 1 && _this->egl_data->egl_version_minor == 5) {
+            LOAD_FUNC(eglGetPlatformDisplay);
+        }
+
+        if (_this->egl_data->eglGetPlatformDisplay) {
             _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
         } else {
             if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
@@ -523,7 +525,7 @@ SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_displa
         *_this->gl_config.driver_path = '\0';
         return SDL_SetError("Could not get EGL display");
     }
-    
+
     if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
         _this->gl_config.driver_loaded = 0;
         *_this->gl_config.driver_path = '\0';