From 61dd0df958807581df9074a111179112c3ac6b6e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 7 Nov 2025 07:55:45 +1100 Subject: [PATCH] Switch: Use specific EGL code --- src/Core.h | 1 - src/_WindowBase.h | 4 -- src/switch/Window_Switch.c | 91 ++++++++++++++++++++++++++++++++++---- 3 files changed, 83 insertions(+), 13 deletions(-) diff --git a/src/Core.h b/src/Core.h index a4ba85783..b37ec5bdc 100644 --- a/src/Core.h +++ b/src/Core.h @@ -575,7 +575,6 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_CONSOLE #define CC_BUILD_TOUCH #define CC_BUILD_GLES - #define CC_BUILD_EGL #define DEFAULT_NET_BACKEND CC_NET_BACKEND_BUILTIN #define DEFAULT_SSL_BACKEND CC_SSL_BACKEND_BEARSSL #define DEFAULT_GFX_BACKEND CC_GFX_BACKEND_GL2 diff --git a/src/_WindowBase.h b/src/_WindowBase.h index cf379db45..b005c98fe 100644 --- a/src/_WindowBase.h +++ b/src/_WindowBase.h @@ -112,9 +112,6 @@ static EGLSurface ctx_surface; static EGLConfig ctx_config; static cc_uintptr ctx_visualID; -#ifdef CC_BUILD_SWITCH -static void GLContext_InitSurface(void); // replacement in Window_Switch.c for handheld/docked resolution fix -#else static void GLContext_InitSurface(void) { #if defined EGLNativeWindowType EGLNativeWindowType window = (EGLNativeWindowType)Window_Main.Handle.ptr; @@ -127,7 +124,6 @@ static void GLContext_InitSurface(void) { if (!ctx_surface) return; eglMakeCurrent(ctx_display, ctx_surface, ctx_surface, ctx_context); } -#endif static void GLContext_FreeSurface(void) { if (!ctx_surface) return; diff --git a/src/switch/Window_Switch.c b/src/switch/Window_Switch.c index 25e168f9b..79cace312 100644 --- a/src/switch/Window_Switch.c +++ b/src/switch/Window_Switch.c @@ -61,7 +61,6 @@ void Window_PreInit(void) { } void Window_Init(void) { - DisplayInfo.Depth = 4; // 32 bit TODO wrong, this is actually 4 bit DisplayInfo.ScaleX = 1; DisplayInfo.ScaleY = 1; @@ -114,7 +113,7 @@ void Window_RequestClose(void) { *#########################################################################################################################*/ static void ProcessTouchInput(void) { static int prev_touchcount = 0; - HidTouchScreenState state = {0}; + HidTouchScreenState state = { 0 }; hidGetTouchScreenStates(&state, 1); if (state.count) { @@ -227,23 +226,19 @@ void Window_AllocFramebuffer(struct Bitmap* bmp, int width, int height) { } void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) { - // Retrieve the framebuffer cc_uint32 stride; cc_uint32* framebuf = (cc_uint32*)framebufferBegin(&fb, &stride); - // flip upside down - for (cc_uint32 y = r.y; y < r.y + r.height; y++) + for (int y = r.y; y < r.y + r.height; y++) { BitmapCol* src = Bitmap_GetRow(bmp, y); cc_uint32* dst = framebuf + y * stride / sizeof(cc_uint32); - for (cc_uint32 x = r.x; x < r.x + r.width; x++) + for (int x = r.x; x < r.x + r.width; x++) { dst[x] = src[x]; } } - - // We're done rendering, so we end the frame here. framebufferEnd(&fb); } @@ -255,6 +250,12 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { /*########################################################################################################################* *-----------------------------------------------------OpenGL context------------------------------------------------------* *#########################################################################################################################*/ +#include +static EGLDisplay ctx_display; +static EGLContext ctx_context; +static EGLSurface ctx_surface; +static EGLConfig ctx_config; + static void GLContext_InitSurface(void) { NWindow* window = (NWindow*)Window_Main.Handle.ptr; if (!window) return; /* window not created or lost */ @@ -273,6 +274,80 @@ static void GLContext_InitSurface(void) { eglMakeCurrent(ctx_display, ctx_surface, ctx_surface, ctx_context); } +static void GLContext_FreeSurface(void) { + if (!ctx_surface) return; + eglMakeCurrent(ctx_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + eglDestroySurface(ctx_display, ctx_surface); + ctx_surface = NULL; +} + +void GLContext_Create(void) { + static const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; + + static const EGLint attribs[] = { + EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 0, + EGL_DEPTH_SIZE, GLCONTEXT_DEFAULT_DEPTH, + EGL_STENCIL_SIZE, 0, + EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER, + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, // EGL_OPENGL_BIT + EGL_NONE + }; + + ctx_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); + eglInitialize(ctx_display, NULL, NULL); + eglBindAPI(EGL_OPENGL_ES_API); + + EGLConfig configs[64]; + EGLint numConfig = 0; + + eglChooseConfig(ctx_display, attribs, configs, 64, &numConfig); + ctx_config = configs[0]; + + ctx_context = eglCreateContext(ctx_display, ctx_config, EGL_NO_CONTEXT, context_attribs); + if (!ctx_context) Process_Abort2(eglGetError(), "Failed to create EGL context"); + GLContext_InitSurface(); +} + +void GLContext_Update(void) { + GLContext_FreeSurface(); + GLContext_InitSurface(); +} + +cc_bool GLContext_TryRestore(void) { + GLContext_FreeSurface(); + GLContext_InitSurface(); + return ctx_surface != NULL; +} + +void GLContext_Free(void) { + GLContext_FreeSurface(); + eglDestroyContext(ctx_display, ctx_context); + eglTerminate(ctx_display); +} + +void* GLContext_GetAddress(const char* function) { + return (void*)eglGetProcAddress(function); +} + +cc_bool GLContext_SwapBuffers(void) { + if (!ctx_surface) return false; + if (eglSwapBuffers(ctx_display, ctx_surface)) return true; + + EGLint err = eglGetError(); + /* TODO: figure out what errors need to be handled here */ + Process_Abort2(err, "Failed to swap buffers"); + return false; +} + +void GLContext_SetVSync(cc_bool vsync) { + eglSwapInterval(ctx_display, vsync); +} + +void GLContext_GetApiInfo(cc_string* info) { } + + /*########################################################################################################################* *------------------------------------------------------Soft keyboard------------------------------------------------------* *#########################################################################################################################*/