| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any damages |
| 7 | arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, |
| 10 | including commercial applications, and to alter it and redistribute it |
| 11 | freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not |
| 14 | claim that you wrote the original software. If you use this software |
| 15 | in a product, an acknowledgment in the product documentation would be |
| 16 | appreciated but is not required. |
| 17 | 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | misrepresented as being the original software. |
| 19 | 3. This notice may not be removed or altered from any source distribution. |
| 20 | */ |
| 21 | #include "SDL_internal.h" |
| 22 | |
| 23 | #ifndef SDL_egl_h_ |
| 24 | #define SDL_egl_h_ |
| 25 | |
| 26 | #ifdef SDL_VIDEO_OPENGL_EGL |
| 27 | |
| 28 | #include <SDL3/SDL_egl.h> |
| 29 | |
| 30 | #include "SDL_sysvideo.h" |
| 31 | |
| 32 | #define SDL_EGL_MAX_DEVICES 8 |
| 33 | |
| 34 | // For systems that don't define these |
| 35 | typedef intptr_t EGLAttrib; |
| 36 | typedef void *EGLDeviceEXT; |
| 37 | typedef EGLDisplay (EGLAPIENTRYP PFNEGLGETDISPLAYPROC) (EGLNativeDisplayType display_id); |
| 38 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLINITIALIZEPROC) (EGLDisplay dpy, EGLint *major, EGLint *minor); |
| 39 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLTERMINATEPROC) (EGLDisplay dpy); |
| 40 | typedef __eglMustCastToProperFunctionPointerType (EGLAPIENTRYP PFNEGLGETPROCADDRESSPROC) (const char *procname); |
| 41 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLCHOOSECONFIGPROC) (EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config); |
| 42 | typedef EGLContext (EGLAPIENTRYP PFNEGLCREATECONTEXTPROC) (EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list); |
| 43 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYCONTEXTPROC) (EGLDisplay dpy, EGLContext ctx); |
| 44 | typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPBUFFERSURFACEPROC) (EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list); |
| 45 | typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEWINDOWSURFACEPROC) (EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list); |
| 46 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYSURFACEPROC) (EGLDisplay dpy, EGLSurface surface); |
| 47 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLMAKECURRENTPROC) (EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx); |
| 48 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSPROC) (EGLDisplay dpy, EGLSurface surface); |
| 49 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPINTERVALPROC) (EGLDisplay dpy, EGLint interval); |
| 50 | typedef const char *(EGLAPIENTRYP PFNEGLQUERYSTRINGPROC) (EGLDisplay dpy, EGLint name); |
| 51 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLGETCONFIGATTRIBPROC) (EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value); |
| 52 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLWAITNATIVEPROC) (EGLint engine); |
| 53 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLWAITGLPROC) (void); |
| 54 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLBINDAPIPROC) (EGLenum api); |
| 55 | typedef EGLint (EGLAPIENTRYP PFNEGLGETERRORPROC) (void); |
| 56 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLQUERYDEVICESEXTPROC) (EGLint max_devices, EGLDeviceEXT *devices, EGLint *num_devices); |
| 57 | typedef EGLDisplay (EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYPROC) (EGLenum platform, void *native_display, const EGLAttrib *attrib_list); |
| 58 | typedef EGLDisplay (EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform, void *native_display, const EGLint *attrib_list); |
| 59 | typedef EGLSyncKHR (EGLAPIENTRYP PFNEGLCREATESYNCKHRPROC) (EGLDisplay dpy, EGLenum type, const EGLint *attrib_list); |
| 60 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYSYNCKHRPROC) (EGLDisplay dpy, EGLSyncKHR sync); |
| 61 | typedef EGLint (EGLAPIENTRYP PFNEGLDUPNATIVEFENCEFDANDROIDPROC) (EGLDisplay dpy, EGLSyncKHR sync); |
| 62 | typedef EGLint (EGLAPIENTRYP PFNEGLWAITSYNCKHRPROC) (EGLDisplay dpy, EGLSyncKHR sync, EGLint flags); |
| 63 | typedef EGLint (EGLAPIENTRYP PFNEGLCLIENTWAITSYNCKHRPROC) (EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout); |
| 64 | |
| 65 | typedef struct SDL_EGL_VideoData |
| 66 | { |
| 67 | SDL_SharedObject *opengl_dll_handle; |
| 68 | SDL_SharedObject *egl_dll_handle; |
| 69 | EGLDisplay egl_display; |
| 70 | EGLConfig egl_config; |
| 71 | int egl_swapinterval; |
| 72 | int egl_surfacetype; |
| 73 | int egl_version_major, egl_version_minor; |
| 74 | EGLint egl_required_visual_id; |
| 75 | bool is_offscreen; // whether EGL display was offscreen |
| 76 | EGLenum apitype; // EGL_OPENGL_ES_API, EGL_OPENGL_API, etc |
| 77 | |
| 78 | PFNEGLGETDISPLAYPROC eglGetDisplay; |
| 79 | PFNEGLINITIALIZEPROC eglInitialize; |
| 80 | PFNEGLTERMINATEPROC eglTerminate; |
| 81 | PFNEGLGETPROCADDRESSPROC eglGetProcAddress; |
| 82 | PFNEGLCHOOSECONFIGPROC eglChooseConfig; |
| 83 | PFNEGLCREATECONTEXTPROC eglCreateContext; |
| 84 | PFNEGLDESTROYCONTEXTPROC eglDestroyContext; |
| 85 | PFNEGLCREATEPBUFFERSURFACEPROC eglCreatePbufferSurface; |
| 86 | PFNEGLCREATEWINDOWSURFACEPROC eglCreateWindowSurface; |
| 87 | PFNEGLDESTROYSURFACEPROC eglDestroySurface; |
| 88 | PFNEGLMAKECURRENTPROC eglMakeCurrent; |
| 89 | PFNEGLSWAPBUFFERSPROC eglSwapBuffers; |
| 90 | PFNEGLSWAPINTERVALPROC eglSwapInterval; |
| 91 | PFNEGLQUERYSTRINGPROC eglQueryString; |
| 92 | PFNEGLGETCONFIGATTRIBPROC eglGetConfigAttrib; |
| 93 | PFNEGLWAITNATIVEPROC eglWaitNative; |
| 94 | PFNEGLWAITGLPROC eglWaitGL; |
| 95 | PFNEGLBINDAPIPROC eglBindAPI; |
| 96 | PFNEGLGETERRORPROC eglGetError; |
| 97 | PFNEGLQUERYDEVICESEXTPROC eglQueryDevicesEXT; |
| 98 | PFNEGLGETPLATFORMDISPLAYPROC eglGetPlatformDisplay; |
| 99 | PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; |
| 100 | |
| 101 | // Atomic functions |
| 102 | PFNEGLCREATESYNCKHRPROC eglCreateSyncKHR; |
| 103 | PFNEGLDESTROYSYNCKHRPROC eglDestroySyncKHR; |
| 104 | PFNEGLDUPNATIVEFENCEFDANDROIDPROC eglDupNativeFenceFDANDROID; |
| 105 | PFNEGLWAITSYNCKHRPROC eglWaitSyncKHR; |
| 106 | PFNEGLCLIENTWAITSYNCKHRPROC eglClientWaitSyncKHR; |
| 107 | |
| 108 | // Atomic functions end |
| 109 | } SDL_EGL_VideoData; |
| 110 | |
| 111 | // OpenGLES functions |
| 112 | typedef enum SDL_EGL_ExtensionType |
| 113 | { |
| 114 | SDL_EGL_DISPLAY_EXTENSION, |
| 115 | SDL_EGL_CLIENT_EXTENSION |
| 116 | } SDL_EGL_ExtensionType; |
| 117 | |
| 118 | extern bool SDL_EGL_HasExtension(SDL_VideoDevice *_this, SDL_EGL_ExtensionType type, const char *ext); |
| 119 | |
| 120 | extern bool SDL_EGL_GetAttribute(SDL_VideoDevice *_this, SDL_GLAttr attrib, int *value); |
| 121 | /* SDL_EGL_LoadLibrary can get a display for a specific platform (EGL_PLATFORM_*) |
| 122 | * or, if 0 is passed, let the implementation decide. |
| 123 | */ |
| 124 | extern bool SDL_EGL_LoadLibraryOnly(SDL_VideoDevice *_this, const char *path); |
| 125 | extern bool SDL_EGL_LoadLibrary(SDL_VideoDevice *_this, const char *path, NativeDisplayType native_display, EGLenum platform); |
| 126 | extern SDL_FunctionPointer SDL_EGL_GetProcAddressInternal(SDL_VideoDevice *_this, const char *proc); |
| 127 | extern void SDL_EGL_UnloadLibrary(SDL_VideoDevice *_this); |
| 128 | extern void SDL_EGL_SetRequiredVisualId(SDL_VideoDevice *_this, int visual_id); |
| 129 | extern bool SDL_EGL_ChooseConfig(SDL_VideoDevice *_this); |
| 130 | extern bool SDL_EGL_SetSwapInterval(SDL_VideoDevice *_this, int interval); |
| 131 | extern bool SDL_EGL_GetSwapInterval(SDL_VideoDevice *_this, int *interval); |
| 132 | extern bool SDL_EGL_DestroyContext(SDL_VideoDevice *_this, SDL_GLContext context); |
| 133 | extern EGLSurface SDL_EGL_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, NativeWindowType nw); |
| 134 | extern void SDL_EGL_DestroySurface(SDL_VideoDevice *_this, EGLSurface egl_surface); |
| 135 | |
| 136 | extern EGLSurface SDL_EGL_CreateOffscreenSurface(SDL_VideoDevice *_this, int width, int height); |
| 137 | // Assumes that LoadLibraryOnly() has succeeded |
| 138 | extern bool SDL_EGL_InitializeOffscreen(SDL_VideoDevice *_this, int device); |
| 139 | |
| 140 | // These need to be wrapped to get the surface for the window by the platform GLES implementation |
| 141 | extern SDL_GLContext SDL_EGL_CreateContext(SDL_VideoDevice *_this, EGLSurface egl_surface); |
| 142 | extern bool SDL_EGL_MakeCurrent(SDL_VideoDevice *_this, EGLSurface egl_surface, SDL_GLContext context); |
| 143 | extern bool SDL_EGL_SwapBuffers(SDL_VideoDevice *_this, EGLSurface egl_surface); |
| 144 | |
| 145 | // SDL Error-reporting |
| 146 | extern bool SDL_EGL_SetErrorEx(const char *message, const char *eglFunctionName, EGLint eglErrorCode); |
| 147 | #define SDL_EGL_SetError(message, eglFunctionName) SDL_EGL_SetErrorEx(message, eglFunctionName, _this->egl_data->eglGetError()) |
| 148 | |
| 149 | // A few of useful macros |
| 150 | |
| 151 | #define SDL_EGL_SwapWindow_impl(BACKEND) \ |
| 152 | bool BACKEND##_GLES_SwapWindow(SDL_VideoDevice *_this, SDL_Window *window) \ |
| 153 | { \ |
| 154 | return SDL_EGL_SwapBuffers(_this, window->internal->egl_surface); \ |
| 155 | } |
| 156 | |
| 157 | #define SDL_EGL_MakeCurrent_impl(BACKEND) \ |
| 158 | bool BACKEND##_GLES_MakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLContext context) \ |
| 159 | { \ |
| 160 | return SDL_EGL_MakeCurrent(_this, window ? window->internal->egl_surface : EGL_NO_SURFACE, context); \ |
| 161 | } |
| 162 | |
| 163 | #define SDL_EGL_CreateContext_impl(BACKEND) \ |
| 164 | SDL_GLContext BACKEND##_GLES_CreateContext(SDL_VideoDevice *_this, SDL_Window *window) \ |
| 165 | { \ |
| 166 | return SDL_EGL_CreateContext(_this, window->internal->egl_surface); \ |
| 167 | } |
| 168 | |
| 169 | #endif // SDL_VIDEO_OPENGL_EGL |
| 170 | |
| 171 | #endif // SDL_egl_h_ |
| 172 | |