SDL: egl: add hint to disable eglGetDisplay fallback when eglGetPlatformDisplay fails

From fb1a5812096af54742410e5381648e0cd4f6976e Mon Sep 17 00:00:00 2001
From: Steven Noonan <[EMAIL REDACTED]>
Date: Thu, 25 Aug 2022 21:29:30 -0700
Subject: [PATCH] egl: add hint to disable eglGetDisplay fallback when
 eglGetPlatformDisplay fails

This fallback is undesirable when using ANGLE, because it will end up
using some default configuration (e.g. on Windows it defaults to the
D3D11 backend).
---
 include/SDL_hints.h | 12 ++++++++++++
 src/video/SDL_egl.c |  4 +++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 2175cf11f360..2f4441502fc6 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -1643,6 +1643,18 @@ extern "C" {
  */
 #define SDL_HINT_VIDEO_EGL_ALLOW_TRANSPARENCY "SDL_VIDEO_EGL_ALLOW_TRANSPARENCY"
 
+/**
+ * \brief If eglGetPlatformDisplay fails, fall back to calling eglGetDisplay.
+ *
+ * This variable can be set to one of the following values:
+ *   "0"        - Do not fall back to eglGetDisplay
+ *   "1"        - Fall back to eglGetDisplay if eglGetPlatformDisplay fails.
+ *
+ * By default, SDL will fall back to eglGetDisplay if eglGetPlatformDisplay
+ * fails.
+ */
+#define SDL_HINT_VIDEO_EGL_ALLOW_GETDISPLAY_FALLBACK "SDL_VIDEO_EGL_GETDISPLAY_FALLBACK"
+
 /**
  * \brief A variable controlling whether the graphics context is externally managed.
  *
diff --git a/src/video/SDL_egl.c b/src/video/SDL_egl.c
index a0e78b2bace5..f1a9bc14f718 100644
--- a/src/video/SDL_egl.c
+++ b/src/video/SDL_egl.c
@@ -526,7 +526,9 @@ SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_displa
     }
 #endif
     /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
-    if ((_this->egl_data->egl_display == EGL_NO_DISPLAY) && (_this->egl_data->eglGetDisplay != NULL)) {
+    if ((_this->egl_data->egl_display == EGL_NO_DISPLAY) &&
+        (_this->egl_data->eglGetDisplay != NULL) &&
+        SDL_GetHintBoolean(SDL_HINT_VIDEO_EGL_ALLOW_GETDISPLAY_FALLBACK, SDL_TRUE)) {
         _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
     }
     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {