SDL: Fixed building offscreen video driver without EGL support

From 6de15ffcd6a5885faf82c94127524673dab9169f Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 15 Sep 2022 12:04:08 -0700
Subject: [PATCH] Fixed building offscreen video driver without EGL support

Also did miscellaneous style cleanup for consistency with other code
---
 src/video/offscreen/SDL_offscreenevents.c     |  1 -
 src/video/offscreen/SDL_offscreenevents_c.h   |  3 -
 .../offscreen/SDL_offscreenframebuffer.c      |  1 -
 .../offscreen/SDL_offscreenframebuffer_c.h    |  1 -
 ...screenopengl.c => SDL_offscreenopengles.c} | 76 +++++++------------
 ...screenopengl.h => SDL_offscreenopengles.h} | 39 ++++------
 src/video/offscreen/SDL_offscreenvideo.c      | 29 ++++---
 src/video/offscreen/SDL_offscreenvideo.h      |  2 -
 src/video/offscreen/SDL_offscreenwindow.c     | 10 ++-
 src/video/offscreen/SDL_offscreenwindow.h     | 13 ++--
 10 files changed, 69 insertions(+), 106 deletions(-)
 rename src/video/offscreen/{SDL_offscreenopengl.c => SDL_offscreenopengles.c} (66%)
 rename src/video/offscreen/{SDL_offscreenopengl.h => SDL_offscreenopengles.h} (53%)

diff --git a/src/video/offscreen/SDL_offscreenevents.c b/src/video/offscreen/SDL_offscreenevents.c
index a14e0b277964..092f19b2b27e 100644
--- a/src/video/offscreen/SDL_offscreenevents.c
+++ b/src/video/offscreen/SDL_offscreenevents.c
@@ -18,7 +18,6 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
-
 #include "../../SDL_internal.h"
 
 #if SDL_VIDEO_DRIVER_OFFSCREEN
diff --git a/src/video/offscreen/SDL_offscreenevents_c.h b/src/video/offscreen/SDL_offscreenevents_c.h
index 3d2614952b3b..6768d3dda987 100644
--- a/src/video/offscreen/SDL_offscreenevents_c.h
+++ b/src/video/offscreen/SDL_offscreenevents_c.h
@@ -18,11 +18,8 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
-
 #include "../../SDL_internal.h"
 
-#include "SDL_offscreenvideo.h"
-
 extern void OFFSCREEN_PumpEvents(_THIS);
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/offscreen/SDL_offscreenframebuffer.c b/src/video/offscreen/SDL_offscreenframebuffer.c
index 3ca57e8975d2..c2d99e51e60e 100644
--- a/src/video/offscreen/SDL_offscreenframebuffer.c
+++ b/src/video/offscreen/SDL_offscreenframebuffer.c
@@ -18,7 +18,6 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
-
 #include "../../SDL_internal.h"
 
 #if SDL_VIDEO_DRIVER_OFFSCREEN
diff --git a/src/video/offscreen/SDL_offscreenframebuffer_c.h b/src/video/offscreen/SDL_offscreenframebuffer_c.h
index 82ae06c926a4..ae1ad7ba943e 100644
--- a/src/video/offscreen/SDL_offscreenframebuffer_c.h
+++ b/src/video/offscreen/SDL_offscreenframebuffer_c.h
@@ -18,7 +18,6 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
-
 #include "../../SDL_internal.h"
 
 extern int SDL_OFFSCREEN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch);
diff --git a/src/video/offscreen/SDL_offscreenopengl.c b/src/video/offscreen/SDL_offscreenopengles.c
similarity index 66%
rename from src/video/offscreen/SDL_offscreenopengl.c
rename to src/video/offscreen/SDL_offscreenopengles.c
index 87641520ebc4..a379ad7b0130 100644
--- a/src/video/offscreen/SDL_offscreenopengl.c
+++ b/src/video/offscreen/SDL_offscreenopengles.c
@@ -18,48 +18,18 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
-
 #include "../../SDL_internal.h"
 
-#if SDL_VIDEO_DRIVER_OFFSCREEN
-
-#include "SDL_offscreenopengl.h"
-
-#include "SDL_opengl.h"
+#if SDL_VIDEO_DRIVER_OFFSCREEN && SDL_VIDEO_OPENGL_EGL
 
-int
-OFFSCREEN_GL_SwapWindow(_THIS, SDL_Window* window)
-{
-    OFFSCREEN_Window* offscreen_wind = window->driverdata;
+#include "SDL_offscreenopengles.h"
+#include "SDL_offscreenvideo.h"
+#include "SDL_offscreenwindow.h"
 
-    SDL_EGL_SwapBuffers(_this, offscreen_wind->egl_surface);
-    return 0;
-}
+/* EGL implementation of SDL OpenGL support */
 
 int
-OFFSCREEN_GL_MakeCurrent(_THIS, SDL_Window* window, SDL_GLContext context)
-{
-  if (window) {
-      EGLSurface egl_surface = ((OFFSCREEN_Window*)window->driverdata)->egl_surface;
-      return SDL_EGL_MakeCurrent(_this, egl_surface, context);
-  }
-
-  return SDL_EGL_MakeCurrent(_this, NULL, NULL);
-}
-
-SDL_GLContext
-OFFSCREEN_GL_CreateContext(_THIS, SDL_Window* window)
-{
-    OFFSCREEN_Window* offscreen_window = window->driverdata;
-
-    SDL_GLContext context;
-    context = SDL_EGL_CreateContext(_this, offscreen_window->egl_surface);
-
-    return context;
-}
-
-int
-OFFSCREEN_GL_LoadLibrary(_THIS, const char* path)
+OFFSCREEN_GLES_LoadLibrary(_THIS, const char* path)
 {
     int ret = SDL_EGL_LoadLibraryOnly(_this, path);
     if (ret != 0) {
@@ -85,24 +55,36 @@ OFFSCREEN_GL_LoadLibrary(_THIS, const char* path)
     return 0;
 }
 
-void
-OFFSCREEN_GL_UnloadLibrary(_THIS)
+SDL_GLContext
+OFFSCREEN_GLES_CreateContext(_THIS, SDL_Window* window)
 {
-    SDL_EGL_UnloadLibrary(_this);
+    OFFSCREEN_Window* offscreen_window = window->driverdata;
+
+    SDL_GLContext context;
+    context = SDL_EGL_CreateContext(_this, offscreen_window->egl_surface);
+
+    return context;
 }
 
-void*
-OFFSCREEN_GL_GetProcAddress(_THIS, const char* proc)
+int
+OFFSCREEN_GLES_MakeCurrent(_THIS, SDL_Window* window, SDL_GLContext context)
 {
-    void* proc_addr = SDL_EGL_GetProcAddress(_this, proc);
-
-    if (!proc_addr) {
-        SDL_SetError("Failed to find proc address!");
+    if (window) {
+        EGLSurface egl_surface = ((OFFSCREEN_Window*)window->driverdata)->egl_surface;
+        return SDL_EGL_MakeCurrent(_this, egl_surface, context);
+    } else {
+        return SDL_EGL_MakeCurrent(_this, NULL, NULL);
     }
+}
+
+int
+OFFSCREEN_GLES_SwapWindow(_THIS, SDL_Window* window)
+{
+    OFFSCREEN_Window* offscreen_wind = window->driverdata;
 
-    return proc_addr;
+    return SDL_EGL_SwapBuffers(_this, offscreen_wind->egl_surface);
 }
 
-#endif /* SDL_VIDEO_DRIVER_OFFSCREEN */
+#endif /* SDL_VIDEO_DRIVER_OFFSCREEN && SDL_VIDEO_OPENGL_EGL */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/offscreen/SDL_offscreenopengl.h b/src/video/offscreen/SDL_offscreenopengles.h
similarity index 53%
rename from src/video/offscreen/SDL_offscreenopengl.h
rename to src/video/offscreen/SDL_offscreenopengles.h
index 1e2df6940419..6c9e51820025 100644
--- a/src/video/offscreen/SDL_offscreenopengl.h
+++ b/src/video/offscreen/SDL_offscreenopengles.h
@@ -18,37 +18,30 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
+#include "../../SDL_internal.h"
 
-#ifndef _SDL_offscreenopengl_h
-#define _SDL_offscreenopengl_h
+#ifndef _SDL_offscreenopengles_h
+#define _SDL_offscreenopengles_h
 
-#include "SDL_offscreenwindow.h"
+#if SDL_VIDEO_DRIVER_OFFSCREEN && SDL_VIDEO_OPENGL_EGL
 
+#include "../SDL_sysvideo.h"
 #include "../SDL_egl_c.h"
 
-#define OFFSCREEN_GL_DeleteContext   SDL_EGL_DeleteContext
-#define OFFSCREEN_GL_GetSwapInterval SDL_EGL_GetSwapInterval
-#define OFFSCREEN_GL_SetSwapInterval SDL_EGL_SetSwapInterval
+#define OFFSCREEN_GLES_GetProcAddress  SDL_EGL_GetProcAddress
+#define OFFSCREEN_GLES_UnloadLibrary   SDL_EGL_UnloadLibrary
+#define OFFSCREEN_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
+#define OFFSCREEN_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
+#define OFFSCREEN_GLES_DeleteContext   SDL_EGL_DeleteContext
 
-extern int
-OFFSCREEN_GL_SwapWindow(_THIS, SDL_Window* window);
+extern int OFFSCREEN_GLES_LoadLibrary(_THIS, const char *path);
+extern SDL_GLContext OFFSCREEN_GLES_CreateContext(_THIS, SDL_Window *window);
+extern int OFFSCREEN_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context);
+extern int OFFSCREEN_GLES_SwapWindow(_THIS, SDL_Window *window);
 
-extern int
-OFFSCREEN_GL_MakeCurrent(_THIS, SDL_Window* window, SDL_GLContext context);
+#endif /* SDL_VIDEO_DRIVER_OFFSCREEN && SDL_VIDEO_OPENGL_EGL */
 
-extern SDL_GLContext
-OFFSCREEN_GL_CreateContext(_THIS, SDL_Window* window);
-
-extern int
-OFFSCREEN_GL_LoadLibrary(_THIS, const char* path);
-
-extern void
-OFFSCREEN_GL_UnloadLibrary(_THIS);
-
-extern void*
-OFFSCREEN_GL_GetProcAddress(_THIS, const char* proc);
-
-#endif /* _SDL_offscreenopengl_h */
+#endif /* _SDL_offscreenopengles_h */
 
 /* vi: set ts=4 sw=4 expandtab: */
 
diff --git a/src/video/offscreen/SDL_offscreenvideo.c b/src/video/offscreen/SDL_offscreenvideo.c
index 3e1aafe3c1d1..8e62e88fb172 100644
--- a/src/video/offscreen/SDL_offscreenvideo.c
+++ b/src/video/offscreen/SDL_offscreenvideo.c
@@ -18,7 +18,6 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
-
 #include "../../SDL_internal.h"
 
 #if SDL_VIDEO_DRIVER_OFFSCREEN
@@ -32,15 +31,12 @@
  */
 
 #include "SDL_video.h"
-#include "SDL_mouse.h"
-#include "../SDL_sysvideo.h"
-#include "../SDL_pixels_c.h"
-#include "../../events/SDL_events_c.h"
 
 #include "SDL_offscreenvideo.h"
 #include "SDL_offscreenevents_c.h"
 #include "SDL_offscreenframebuffer_c.h"
-#include "SDL_offscreenopengl.h"
+#include "SDL_offscreenopengles.h"
+#include "SDL_offscreenwindow.h"
 
 #define OFFSCREENVID_DRIVER_NAME "offscreen"
 
@@ -79,16 +75,18 @@ OFFSCREEN_CreateDevice(void)
     device->DestroyWindowFramebuffer = SDL_OFFSCREEN_DestroyWindowFramebuffer;
     device->free = OFFSCREEN_DeleteDevice;
 
+#if SDL_VIDEO_OPENGL_EGL
     /* GL context */
-    device->GL_SwapWindow = OFFSCREEN_GL_SwapWindow;
-    device->GL_MakeCurrent = OFFSCREEN_GL_MakeCurrent;
-    device->GL_CreateContext = OFFSCREEN_GL_CreateContext;
-    device->GL_DeleteContext = OFFSCREEN_GL_DeleteContext;
-    device->GL_LoadLibrary = OFFSCREEN_GL_LoadLibrary;
-    device->GL_UnloadLibrary = OFFSCREEN_GL_UnloadLibrary;
-    device->GL_GetProcAddress = OFFSCREEN_GL_GetProcAddress;
-    device->GL_GetSwapInterval = OFFSCREEN_GL_GetSwapInterval;
-    device->GL_SetSwapInterval = OFFSCREEN_GL_SetSwapInterval;
+    device->GL_SwapWindow = OFFSCREEN_GLES_SwapWindow;
+    device->GL_MakeCurrent = OFFSCREEN_GLES_MakeCurrent;
+    device->GL_CreateContext = OFFSCREEN_GLES_CreateContext;
+    device->GL_DeleteContext = OFFSCREEN_GLES_DeleteContext;
+    device->GL_LoadLibrary = OFFSCREEN_GLES_LoadLibrary;
+    device->GL_UnloadLibrary = OFFSCREEN_GLES_UnloadLibrary;
+    device->GL_GetProcAddress = OFFSCREEN_GLES_GetProcAddress;
+    device->GL_GetSwapInterval = OFFSCREEN_GLES_GetSwapInterval;
+    device->GL_SetSwapInterval = OFFSCREEN_GLES_SetSwapInterval;
+#endif
 
     /* "Window" */
     device->CreateSDLWindow = OFFSCREEN_CreateWindow;
@@ -106,7 +104,6 @@ int
 OFFSCREEN_VideoInit(_THIS)
 {
     SDL_DisplayMode mode;
-    SDL_Mouse *mouse = NULL;
 
     /* Use a fake 32-bpp desktop mode */
     mode.format = SDL_PIXELFORMAT_RGB888;
diff --git a/src/video/offscreen/SDL_offscreenvideo.h b/src/video/offscreen/SDL_offscreenvideo.h
index eae512bbd470..b943a8246875 100644
--- a/src/video/offscreen/SDL_offscreenvideo.h
+++ b/src/video/offscreen/SDL_offscreenvideo.h
@@ -18,14 +18,12 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
-
 #include "../../SDL_internal.h"
 
 #ifndef _SDL_offscreenvideo_h
 #define _SDL_offscreenvideo_h
 
 #include "../SDL_sysvideo.h"
-#include "../SDL_egl_c.h"
 
 #endif /* _SDL_offscreenvideo_h */
 
diff --git a/src/video/offscreen/SDL_offscreenwindow.c b/src/video/offscreen/SDL_offscreenwindow.c
index 6697b016aa64..1730308353a1 100644
--- a/src/video/offscreen/SDL_offscreenwindow.c
+++ b/src/video/offscreen/SDL_offscreenwindow.c
@@ -18,7 +18,6 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
-
 #include "../../SDL_internal.h"
 
 #if SDL_VIDEO_DRIVER_OFFSCREEN
@@ -31,7 +30,7 @@
 int
 OFFSCREEN_CreateWindow(_THIS, SDL_Window* window)
 {
-    OFFSCREEN_Window* offscreen_window = SDL_calloc(1, sizeof(OFFSCREEN_Window));
+    OFFSCREEN_Window *offscreen_window = SDL_calloc(1, sizeof(OFFSCREEN_Window));
 
     if (!offscreen_window) {
         return SDL_OutOfMemory();
@@ -49,6 +48,7 @@ OFFSCREEN_CreateWindow(_THIS, SDL_Window* window)
 
     offscreen_window->sdl_window = window;
 
+#if SDL_VIDEO_OPENGL_EGL
     if (window->flags & SDL_WINDOW_OPENGL) {
 
         if (!_this->egl_data) {
@@ -61,10 +61,10 @@ OFFSCREEN_CreateWindow(_THIS, SDL_Window* window)
             return SDL_SetError("Failed to created an offscreen surface (EGL display: %p)",
                                 _this->egl_data->egl_display);
         }
-    }
-    else {
+    } else {
         offscreen_window->egl_surface = EGL_NO_SURFACE;
     }
+#endif /* SDL_VIDEO_OPENGL_EGL */
 
     return 0;
 }
@@ -75,7 +75,9 @@ OFFSCREEN_DestroyWindow(_THIS, SDL_Window* window)
     OFFSCREEN_Window* offscreen_window = window->driverdata;
 
     if (offscreen_window) {
+#if SDL_VIDEO_OPENGL_EGL
         SDL_EGL_DestroySurface(_this, offscreen_window->egl_surface);
+#endif
         SDL_free(offscreen_window);
     }
 
diff --git a/src/video/offscreen/SDL_offscreenwindow.h b/src/video/offscreen/SDL_offscreenwindow.h
index 74c85d26c43a..554079454372 100644
--- a/src/video/offscreen/SDL_offscreenwindow.h
+++ b/src/video/offscreen/SDL_offscreenwindow.h
@@ -18,27 +18,24 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
+#include "../../SDL_internal.h"
 
 #ifndef _SDL_offscreenwindow_h
 #define _SDL_offscreenwindow_h
 
-#include "../SDL_sysvideo.h"
-#include "SDL_syswm.h"
-
 #include "SDL_offscreenvideo.h"
 
 typedef struct {
     SDL_Window* sdl_window;
+#if SDL_VIDEO_OPENGL_EGL
     EGLSurface egl_surface;
+#endif
 
 } OFFSCREEN_Window;
 
 
-extern int
-OFFSCREEN_CreateWindow(_THIS, SDL_Window* window);
-
-extern void
-OFFSCREEN_DestroyWindow(_THIS, SDL_Window* window);
+extern int OFFSCREEN_CreateWindow(_THIS, SDL_Window *window);
+extern void OFFSCREEN_DestroyWindow(_THIS, SDL_Window *window);
 
 #endif /* _SDL_offscreenwindow */