SDL: egl: explicitly call eglBindAPI during SDL_GL_MakeCurrent.

From 0ad4956c069459afb66601ea2a0446b654dae0ea Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 23 Sep 2021 00:00:46 -0400
Subject: [PATCH] egl: explicitly call eglBindAPI during SDL_GL_MakeCurrent.

The EGL API binding must be specified per-thread, per the docs.

Fixes #1820.
---
 src/video/SDL_egl.c   | 10 ++++++++--
 src/video/SDL_egl_c.h |  5 ++---
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/video/SDL_egl.c b/src/video/SDL_egl.c
index c8af1a55d7..0b8635db29 100644
--- a/src/video/SDL_egl.c
+++ b/src/video/SDL_egl.c
@@ -1027,10 +1027,11 @@ SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
 
     /* Bind the API */
     if (profile_es) {
-        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
+        _this->egl_data->apitype = EGL_OPENGL_ES_API;
     } else {
-        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
+        _this->egl_data->apitype = EGL_OPENGL_API;
     }
+    _this->egl_data->eglBindAPI(_this->egl_data->apitype);
 
     egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
                                       _this->egl_data->egl_config,
@@ -1108,6 +1109,11 @@ SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
         }
     }
 
+    /* Make sure current thread has a valid API bound to it. */
+    if (_this->egl_data->eglBindAPI) {
+        _this->egl_data->eglBindAPI(_this->egl_data->apitype);
+    }
+
     /* The android emulator crashes badly if you try to eglMakeCurrent 
      * with a valid context and invalid surface, so we have to check for both here.
      */
diff --git a/src/video/SDL_egl_c.h b/src/video/SDL_egl_c.h
index e6580647ec..8dd0bdb08d 100644
--- a/src/video/SDL_egl_c.h
+++ b/src/video/SDL_egl_c.h
@@ -115,9 +115,8 @@ typedef struct SDL_EGL_VideoData
 
     /* Atomic functions end */
 
-
-    /* whether EGL display was offscreen */
-    SDL_bool is_offscreen;
+    SDL_bool is_offscreen;  /* whether EGL display was offscreen */
+    EGLenum apitype;  /* EGL_OPENGL_ES_API, EGL_OPENGL_API, etc */
 } SDL_EGL_VideoData;
 
 /* OpenGLES functions */