SDL: Don't unload graphics libraries until after the window has been destroyed.

From b0e7b7db6f94e6ae75ba381b0298f5904393f6b2 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 12 Dec 2023 23:17:41 -0800
Subject: [PATCH] Don't unload graphics libraries until after the window has
 been destroyed.

This fixes creating a window after the first window has been destroyed on Android. The graphics library had been unloaded, so eglDestroySurface() wasn't called, leaving a surface attached to the window, which would prevent attaching new EGL surfaces to the window (eglCreateWindowSurface() would fail with BAD_ALLOC)
---
 src/video/SDL_video.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index ecf909568acd..6e4d45f32b13 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -3621,12 +3621,18 @@ void SDL_DestroyWindow(SDL_Window *window)
         }
     }
 
-    /* make no context current if this is the current context window. */
+    /* Make no context current if this is the current context window */
     if (window->flags & SDL_WINDOW_OPENGL) {
         if (_this->current_glwin == window) {
             SDL_GL_MakeCurrent(window, NULL);
         }
     }
+
+    if (_this->DestroyWindow) {
+        _this->DestroyWindow(_this, window);
+    }
+
+    /* Unload the graphics libraries after the window is destroyed, which may clean up EGL surfaces */
     if (window->flags & SDL_WINDOW_OPENGL) {
         SDL_GL_UnloadLibrary();
     }
@@ -3634,10 +3640,6 @@ void SDL_DestroyWindow(SDL_Window *window)
         SDL_Vulkan_UnloadLibrary();
     }
 
-    if (_this->DestroyWindow) {
-        _this->DestroyWindow(_this, window);
-    }
-
     if (_this->grabbed_window == window) {
         _this->grabbed_window = NULL; /* ungrabbing input. */
     }