SDL: Update GL_GetSwapInterval for backends (#6963)

From 023f067903e6d6642dfafed6ea166e805e9857d0 Mon Sep 17 00:00:00 2001
From: Sylvain Becker <[EMAIL REDACTED]>
Date: Mon, 2 Jan 2023 19:15:05 +0100
Subject: [PATCH] Update GL_GetSwapInterval for backends (#6963)

---
 src/video/SDL_egl.c                           |  8 +++---
 src/video/SDL_egl_c.h                         |  2 +-
 src/video/SDL_sysvideo.h                      |  2 +-
 src/video/SDL_video.c                         |  5 +---
 src/video/cocoa/SDL_cocoaopengl.h             |  2 +-
 src/video/cocoa/SDL_cocoaopengl.m             |  9 +++++--
 src/video/emscripten/SDL_emscriptenopengles.c | 13 +++++----
 src/video/emscripten/SDL_emscriptenopengles.h |  2 +-
 src/video/haiku/SDL_bopengl.cc                |  5 ++--
 src/video/haiku/SDL_bopengl.h                 |  2 +-
 src/video/psp/SDL_pspgl.c                     |  5 ++--
 src/video/psp/SDL_pspgl_c.h                   |  2 +-
 src/video/psp/SDL_pspvideo.h                  |  2 +-
 src/video/wayland/SDL_waylandopengles.c       |  8 +++---
 src/video/wayland/SDL_waylandopengles.h       |  2 +-
 src/video/windows/SDL_windowsopengl.c         |  9 ++++---
 src/video/windows/SDL_windowsopengl.h         |  2 +-
 src/video/x11/SDL_x11opengl.c                 | 27 ++++++++++++-------
 src/video/x11/SDL_x11opengl.h                 |  2 +-
 19 files changed, 62 insertions(+), 47 deletions(-)

diff --git a/src/video/SDL_egl.c b/src/video/SDL_egl.c
index 295af297d04b..17c1fcbdc0fc 100644
--- a/src/video/SDL_egl.c
+++ b/src/video/SDL_egl.c
@@ -1180,14 +1180,14 @@ int SDL_EGL_SetSwapInterval(_THIS, int interval)
     return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
 }
 
-int SDL_EGL_GetSwapInterval(_THIS)
+int SDL_EGL_GetSwapInterval(_THIS, int *interval)
 {
     if (!_this->egl_data) {
-        SDL_SetError("EGL not initialized");
-        return 0;
+        return SDL_SetError("EGL not initialized");
     }
 
-    return _this->egl_data->egl_swapinterval;
+    *interval = _this->egl_data->egl_swapinterval;
+    return 0;
 }
 
 int SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
diff --git a/src/video/SDL_egl_c.h b/src/video/SDL_egl_c.h
index d9d2f001f914..ece3ef26bd40 100644
--- a/src/video/SDL_egl_c.h
+++ b/src/video/SDL_egl_c.h
@@ -138,7 +138,7 @@ extern void SDL_EGL_UnloadLibrary(_THIS);
 extern void SDL_EGL_SetRequiredVisualId(_THIS, int visual_id);
 extern int SDL_EGL_ChooseConfig(_THIS);
 extern int SDL_EGL_SetSwapInterval(_THIS, int interval);
-extern int SDL_EGL_GetSwapInterval(_THIS);
+extern int SDL_EGL_GetSwapInterval(_THIS, int *interval);
 extern void SDL_EGL_DeleteContext(_THIS, SDL_GLContext context);
 extern EGLSurface *SDL_EGL_CreateSurface(_THIS, NativeWindowType nw);
 extern void SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface);
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 50e86a2de6d3..19e1d9d2d823 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -271,7 +271,7 @@ struct SDL_VideoDevice
     void (*GL_GetDrawableSize)(_THIS, SDL_Window *window, int *w, int *h);
     SDL_EGLSurface (*GL_GetEGLSurface)(_THIS, SDL_Window *window);
     int (*GL_SetSwapInterval)(_THIS, int interval);
-    int (*GL_GetSwapInterval)(_THIS);
+    int (*GL_GetSwapInterval)(_THIS, int *interval);
     int (*GL_SwapWindow)(_THIS, SDL_Window *window);
     void (*GL_DeleteContext)(_THIS, SDL_GLContext context);
     void (*GL_DefaultProfileConfig)(_THIS, int *mask, int *major, int *minor);
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index b77aff093715..3bec551693c1 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -4066,10 +4066,7 @@ int SDL_GL_GetSwapInterval(int *interval)
     } else if (SDL_GL_GetCurrentContext() == NULL) {
         return SDL_SetError("no current context");;
     } else if (_this->GL_GetSwapInterval) {
-        int val = _this->GL_GetSwapInterval(_this);
-         
-        *interval = val;
-        return 0;
+        return _this->GL_GetSwapInterval(_this, interval);
     } else {
         return SDL_SetError("not implemented");;
     }
diff --git a/src/video/cocoa/SDL_cocoaopengl.h b/src/video/cocoa/SDL_cocoaopengl.h
index 185b2a6c6eb8..ee2ca2295b12 100644
--- a/src/video/cocoa/SDL_cocoaopengl.h
+++ b/src/video/cocoa/SDL_cocoaopengl.h
@@ -76,7 +76,7 @@ extern SDL_GLContext Cocoa_GL_CreateContext(_THIS, SDL_Window *window);
 extern int Cocoa_GL_MakeCurrent(_THIS, SDL_Window *window,
                                 SDL_GLContext context);
 extern int Cocoa_GL_SetSwapInterval(_THIS, int interval);
-extern int Cocoa_GL_GetSwapInterval(_THIS);
+extern int Cocoa_GL_GetSwapInterval(_THIS, int *interval);
 extern int Cocoa_GL_SwapWindow(_THIS, SDL_Window *window);
 extern void Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context);
 
diff --git a/src/video/cocoa/SDL_cocoaopengl.m b/src/video/cocoa/SDL_cocoaopengl.m
index 26d0b6043e6b..cc24f59d995f 100644
--- a/src/video/cocoa/SDL_cocoaopengl.m
+++ b/src/video/cocoa/SDL_cocoaopengl.m
@@ -467,11 +467,16 @@ int Cocoa_GL_SetSwapInterval(_THIS, int interval)
     }
 }
 
-int Cocoa_GL_GetSwapInterval(_THIS)
+int Cocoa_GL_GetSwapInterval(_THIS, int *interval)
 {
     @autoreleasepool {
         SDLOpenGLContext *nscontext = (__bridge SDLOpenGLContext *)SDL_GL_GetCurrentContext();
-        return nscontext ? SDL_AtomicGet(&nscontext->swapIntervalSetting) : 0;
+        if (nscontext) {
+            *interval = SDL_AtomicGet(&nscontext->swapIntervalSetting);
+            return 0;
+        } else {
+            return SDL_SetError("no OpenGL context");
+        }
     }
 }
 
diff --git a/src/video/emscripten/SDL_emscriptenopengles.c b/src/video/emscripten/SDL_emscriptenopengles.c
index 9293765bd65b..2a67e918e50d 100644
--- a/src/video/emscripten/SDL_emscriptenopengles.c
+++ b/src/video/emscripten/SDL_emscriptenopengles.c
@@ -57,16 +57,19 @@ int Emscripten_GLES_SetSwapInterval(_THIS, int interval)
     return 0;
 }
 
-int Emscripten_GLES_GetSwapInterval(_THIS)
+int Emscripten_GLES_GetSwapInterval(_THIS, int *interval)
 {
     int mode, value;
 
     emscripten_get_main_loop_timing(&mode, &value);
 
-    if (mode == EM_TIMING_RAF)
-        return value;
-
-    return 0;
+    if (mode == EM_TIMING_RAF) {
+        *interval = value;
+        return 0;
+    } else {
+        *interval = 0;
+        return 0;
+    }
 }
 
 SDL_GLContext
diff --git a/src/video/emscripten/SDL_emscriptenopengles.h b/src/video/emscripten/SDL_emscriptenopengles.h
index 4d3bb4d3a24f..03b6cca43ed8 100644
--- a/src/video/emscripten/SDL_emscriptenopengles.h
+++ b/src/video/emscripten/SDL_emscriptenopengles.h
@@ -32,7 +32,7 @@ extern int Emscripten_GLES_LoadLibrary(_THIS, const char *path);
 extern void Emscripten_GLES_UnloadLibrary(_THIS);
 extern void *Emscripten_GLES_GetProcAddress(_THIS, const char *proc);
 extern int Emscripten_GLES_SetSwapInterval(_THIS, int interval);
-extern int Emscripten_GLES_GetSwapInterval(_THIS);
+extern int Emscripten_GLES_GetSwapInterval(_THIS, int *interval);
 extern SDL_GLContext Emscripten_GLES_CreateContext(_THIS, SDL_Window *window);
 extern void Emscripten_GLES_DeleteContext(_THIS, SDL_GLContext context);
 extern int Emscripten_GLES_SwapWindow(_THIS, SDL_Window *window);
diff --git a/src/video/haiku/SDL_bopengl.cc b/src/video/haiku/SDL_bopengl.cc
index 940c1ddf67ff..27104a6648aa 100644
--- a/src/video/haiku/SDL_bopengl.cc
+++ b/src/video/haiku/SDL_bopengl.cc
@@ -159,9 +159,8 @@ int HAIKU_GL_SetSwapInterval(_THIS, int interval) {
     return SDL_Unsupported();
 }
 
-int HAIKU_GL_GetSwapInterval(_THIS) {
-    /* TODO: Implement this, if necessary? */
-    return 0;
+int HAIKU_GL_GetSwapInterval(_THIS, int *interval) {
+    return SDL_Unsupported();
 }
 
 
diff --git a/src/video/haiku/SDL_bopengl.h b/src/video/haiku/SDL_bopengl.h
index 00278a2dd27a..c284dfb7c4bb 100644
--- a/src/video/haiku/SDL_bopengl.h
+++ b/src/video/haiku/SDL_bopengl.h
@@ -36,7 +36,7 @@ extern void HAIKU_GL_UnloadLibrary(_THIS);                     /* TODO */
 extern int HAIKU_GL_MakeCurrent(_THIS, SDL_Window *window,
                                 SDL_GLContext context);
 extern int HAIKU_GL_SetSwapInterval(_THIS, int interval); /* TODO */
-extern int HAIKU_GL_GetSwapInterval(_THIS);               /* TODO */
+extern int HAIKU_GL_GetSwapInterval(_THIS, int *interval); /* TODO */
 extern int HAIKU_GL_SwapWindow(_THIS, SDL_Window *window);
 extern SDL_GLContext HAIKU_GL_CreateContext(_THIS, SDL_Window *window);
 extern void HAIKU_GL_DeleteContext(_THIS, SDL_GLContext context);
diff --git a/src/video/psp/SDL_pspgl.c b/src/video/psp/SDL_pspgl.c
index 86d047aa200b..c36278a0b52c 100644
--- a/src/video/psp/SDL_pspgl.c
+++ b/src/video/psp/SDL_pspgl.c
@@ -153,9 +153,10 @@ int PSP_GL_SetSwapInterval(_THIS, int interval)
     return SDL_SetError("Unable to set the EGL swap interval");
 }
 
-int PSP_GL_GetSwapInterval(_THIS)
+int PSP_GL_GetSwapInterval(_THIS, int *interval)
 {
-    return _this->gl_data->swapinterval;
+    *interval = _this->gl_data->swapinterval;
+    return 0;
 }
 
 int PSP_GL_SwapWindow(_THIS, SDL_Window *window)
diff --git a/src/video/psp/SDL_pspgl_c.h b/src/video/psp/SDL_pspgl_c.h
index 6aa01425bc65..13ba12e5ad97 100644
--- a/src/video/psp/SDL_pspgl_c.h
+++ b/src/video/psp/SDL_pspgl_c.h
@@ -45,6 +45,6 @@ extern SDL_GLContext PSP_GL_CreateContext(_THIS, SDL_Window *window);
 extern int PSP_GL_LoadLibrary(_THIS, const char *path);
 extern void PSP_GL_UnloadLibrary(_THIS);
 extern int PSP_GL_SetSwapInterval(_THIS, int interval);
-extern int PSP_GL_GetSwapInterval(_THIS);
+extern int PSP_GL_GetSwapInterval(_THIS, int *interval);
 
 #endif /* SDL_pspgl_c_h_ */
diff --git a/src/video/psp/SDL_pspvideo.h b/src/video/psp/SDL_pspvideo.h
index 17a462c1f4f0..d836edbfb3b4 100644
--- a/src/video/psp/SDL_pspvideo.h
+++ b/src/video/psp/SDL_pspvideo.h
@@ -75,7 +75,7 @@ void PSP_GL_UnloadLibrary(_THIS);
 SDL_GLContext PSP_GL_CreateContext(_THIS, SDL_Window *window);
 int PSP_GL_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context);
 int PSP_GL_SetSwapInterval(_THIS, int interval);
-int PSP_GL_GetSwapInterval(_THIS);
+int PSP_GL_GetSwapInterval(_THIS, int *interval);
 int PSP_GL_SwapWindow(_THIS, SDL_Window *window);
 void PSP_GL_DeleteContext(_THIS, SDL_GLContext context);
 
diff --git a/src/video/wayland/SDL_waylandopengles.c b/src/video/wayland/SDL_waylandopengles.c
index 980a2947dba6..2a32ed339fa1 100644
--- a/src/video/wayland/SDL_waylandopengles.c
+++ b/src/video/wayland/SDL_waylandopengles.c
@@ -92,14 +92,14 @@ int Wayland_GLES_SetSwapInterval(_THIS, int interval)
     return 0;
 }
 
-int Wayland_GLES_GetSwapInterval(_THIS)
+int Wayland_GLES_GetSwapInterval(_THIS, int *interval)
 {
     if (!_this->egl_data) {
-        SDL_SetError("EGL not initialized");
-        return 0;
+        return SDL_SetError("EGL not initialized");
     }
 
-    return _this->egl_data->egl_swapinterval;
+    *interval =_this->egl_data->egl_swapinterval;
+    return 0;
 }
 
 int Wayland_GLES_SwapWindow(_THIS, SDL_Window *window)
diff --git a/src/video/wayland/SDL_waylandopengles.h b/src/video/wayland/SDL_waylandopengles.h
index 3ace938ee8f2..ae6d4cbd9c4b 100644
--- a/src/video/wayland/SDL_waylandopengles.h
+++ b/src/video/wayland/SDL_waylandopengles.h
@@ -39,7 +39,7 @@ typedef struct SDL_PrivateGLESData
 extern int Wayland_GLES_LoadLibrary(_THIS, const char *path);
 extern SDL_GLContext Wayland_GLES_CreateContext(_THIS, SDL_Window *window);
 extern int Wayland_GLES_SetSwapInterval(_THIS, int interval);
-extern int Wayland_GLES_GetSwapInterval(_THIS);
+extern int Wayland_GLES_GetSwapInterval(_THIS, int *interval);
 extern int Wayland_GLES_SwapWindow(_THIS, SDL_Window *window);
 extern int Wayland_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context);
 extern void Wayland_GLES_DeleteContext(_THIS, SDL_GLContext context);
diff --git a/src/video/windows/SDL_windowsopengl.c b/src/video/windows/SDL_windowsopengl.c
index 46b384bb1d2e..5119a322e347 100644
--- a/src/video/windows/SDL_windowsopengl.c
+++ b/src/video/windows/SDL_windowsopengl.c
@@ -866,13 +866,14 @@ int WIN_GL_SetSwapInterval(_THIS, int interval)
     return 0;
 }
 
-int WIN_GL_GetSwapInterval(_THIS)
+int WIN_GL_GetSwapInterval(_THIS, int *interval)
 {
-    int retval = 0;
     if (_this->gl_data->wglGetSwapIntervalEXT) {
-        retval = _this->gl_data->wglGetSwapIntervalEXT();
+        *interval = _this->gl_data->wglGetSwapIntervalEXT();
+        return 0;
+    } else {
+        return -1;
     }
-    return retval;
 }
 
 int WIN_GL_SwapWindow(_THIS, SDL_Window *window)
diff --git a/src/video/windows/SDL_windowsopengl.h b/src/video/windows/SDL_windowsopengl.h
index 8deeb4fa143f..e3e3fdfa1c55 100644
--- a/src/video/windows/SDL_windowsopengl.h
+++ b/src/video/windows/SDL_windowsopengl.h
@@ -111,7 +111,7 @@ extern SDL_GLContext WIN_GL_CreateContext(_THIS, SDL_Window *window);
 extern int WIN_GL_MakeCurrent(_THIS, SDL_Window *window,
                               SDL_GLContext context);
 extern int WIN_GL_SetSwapInterval(_THIS, int interval);
-extern int WIN_GL_GetSwapInterval(_THIS);
+extern int WIN_GL_GetSwapInterval(_THIS, int *interval);
 extern int WIN_GL_SwapWindow(_THIS, SDL_Window *window);
 extern void WIN_GL_DeleteContext(_THIS, SDL_GLContext context);
 extern void WIN_GL_InitExtensions(_THIS);
diff --git a/src/video/x11/SDL_x11opengl.c b/src/video/x11/SDL_x11opengl.c
index 08e5444f5657..2c12a43800d0 100644
--- a/src/video/x11/SDL_x11opengl.c
+++ b/src/video/x11/SDL_x11opengl.c
@@ -885,7 +885,8 @@ int X11_GL_SetSwapInterval(_THIS, int interval)
          * it has the wrong value cached. To work around it, we just run a no-op
          * update to the current value.
          */
-        int currentInterval = X11_GL_GetSwapInterval(_this);
+        int currentInterval = 0;
+        X11_GL_GetSwapInterval(_this, &currentInterval);
         _this->gl_data->glXSwapIntervalEXT(display, drawable, currentInterval);
         _this->gl_data->glXSwapIntervalEXT(display, drawable, interval);
 
@@ -911,7 +912,7 @@ int X11_GL_SetSwapInterval(_THIS, int interval)
     return status;
 }
 
-int X11_GL_GetSwapInterval(_THIS)
+int X11_GL_GetSwapInterval(_THIS, int *interval)
 {
     if (_this->gl_data->glXSwapIntervalEXT) {
         Display *display = ((SDL_VideoData *)_this->driverdata)->display;
@@ -920,7 +921,7 @@ int X11_GL_GetSwapInterval(_THIS)
                                                    ->driverdata;
         Window drawable = windowdata->xwindow;
         unsigned int allow_late_swap_tearing = 0;
-        unsigned int interval = 0;
+        unsigned int val = 0;
 
         if (_this->gl_data->HAS_GLX_EXT_swap_control_tear) {
             _this->gl_data->glXQueryDrawable(display, drawable,
@@ -929,17 +930,25 @@ int X11_GL_GetSwapInterval(_THIS)
         }
 
         _this->gl_data->glXQueryDrawable(display, drawable,
-                                         GLX_SWAP_INTERVAL_EXT, &interval);
+                                         GLX_SWAP_INTERVAL_EXT, &val);
 
-        if ((allow_late_swap_tearing) && (interval > 0)) {
-            return -((int)interval);
+        if ((allow_late_swap_tearing) && (val > 0)) {
+            *interval = -((int)val);
+            return 0;
         }
 
-        return (int)interval;
+        *interval = (int)val;
+        return 0;
     } else if (_this->gl_data->glXGetSwapIntervalMESA) {
-        return _this->gl_data->glXGetSwapIntervalMESA();
+        int val = _this->gl_data->glXGetSwapIntervalMESA();
+        if (val == GLX_BAD_CONTEXT) {
+            return SDL_SetError("GLX_BAD_CONTEXT");
+        }
+        *interval = val;
+        return 0;
     } else {
-        return swapinterval;
+        *interval = swapinterval;
+        return 0;
     }
 }
 
diff --git a/src/video/x11/SDL_x11opengl.h b/src/video/x11/SDL_x11opengl.h
index ffad38e97d83..c599f8c75816 100644
--- a/src/video/x11/SDL_x11opengl.h
+++ b/src/video/x11/SDL_x11opengl.h
@@ -75,7 +75,7 @@ extern SDL_GLContext X11_GL_CreateContext(_THIS, SDL_Window *window);
 extern int X11_GL_MakeCurrent(_THIS, SDL_Window *window,
                               SDL_GLContext context);
 extern int X11_GL_SetSwapInterval(_THIS, int interval);
-extern int X11_GL_GetSwapInterval(_THIS);
+extern int X11_GL_GetSwapInterval(_THIS, int *interval);
 extern int X11_GL_SwapWindow(_THIS, SDL_Window *window);
 extern void X11_GL_DeleteContext(_THIS, SDL_GLContext context);