From 4388a645eefe549f60845d904b63221f090413c2 Mon Sep 17 00:00:00 2001 From: kub Date: Fri, 23 May 2025 18:48:06 +0200 Subject: [PATCH] gl: initialize display only once --- gl.c | 65 +++++++++++++++++++++++++++++++++++---------------- gl.h | 16 +++++++++---- gl_platform.c | 45 +++++++++++++++++++++++++++-------- gl_platform.h | 6 +++-- plat_sdl.c | 21 ++++++++++------- 5 files changed, 109 insertions(+), 44 deletions(-) diff --git a/gl.c b/gl.c index bf40acc..3b201db 100644 --- a/gl.c +++ b/gl.c @@ -43,7 +43,36 @@ static int gles_have_error(const char *name) return 0; } -int gl_init(void *display, void *window, int *quirks, int w, int h) +int gl_init(void *display, int *quirks) +{ + int retval = -1; + int ret; + + ret = gl_platform_init(&display, quirks); + if (ret != 0) { + fprintf(stderr, "gl_platform_init failed with %d\n", ret); + return retval; + } + + edpy = eglGetDisplay((EGLNativeDisplayType)display); + if (edpy == EGL_NO_DISPLAY) { + fprintf(stderr, "Failed to get EGL display\n"); + goto out; + } + + if (!eglInitialize(edpy, NULL, NULL)) { + fprintf(stderr, "Failed to initialize EGL\n"); + goto out; + } + retval = 0; + +out: + if (retval && edpy != EGL_NO_DISPLAY) + gl_shutdown(); + return retval; +} + +int gl_create(void *window, int *quirks, int w, int h) { EGLConfig ecfg = NULL; EGLint num_config; @@ -54,7 +83,7 @@ int gl_init(void *display, void *window, int *quirks, int w, int h) EGL_NONE }; - ret = gl_platform_init(&display, &window, quirks); + ret = gl_platform_create(&window, quirks); if (ret != 0) { fprintf(stderr, "gl_platform_init failed with %d\n", ret); return retval; @@ -72,17 +101,6 @@ int gl_init(void *display, void *window, int *quirks, int w, int h) } memset(tex_mem, 0, tex_w * tex_h * 2); - edpy = eglGetDisplay((EGLNativeDisplayType)display); - if (edpy == EGL_NO_DISPLAY) { - fprintf(stderr, "Failed to get EGL display\n"); - goto out; - } - - if (!eglInitialize(edpy, NULL, NULL)) { - fprintf(stderr, "Failed to initialize EGL\n"); - goto out; - } - if (!eglChooseConfig(edpy, config_attr, &ecfg, 1, &num_config)) { fprintf(stderr, "Failed to choose config (%x)\n", eglGetError()); goto out; @@ -168,7 +186,7 @@ int gl_init(void *display, void *window, int *quirks, int w, int h) retval = 0; out: if (retval && edpy != EGL_NO_DISPLAY) - gl_finish(); + gl_destroy(); return retval; } @@ -250,7 +268,7 @@ void gl_clear(void) gl_have_error("glClear"); } -void gl_finish(void) +void gl_destroy(void) { if (edpy == EGL_NO_DISPLAY) return; // nothing to do @@ -275,16 +293,23 @@ void gl_finish(void) eglDestroySurface(edpy, esfc); esfc = EGL_NO_SURFACE; } + + gl_es_surface = (void *)esfc; + + if (tex_mem) free(tex_mem); + tex_mem = NULL; + + gl_platform_destroy(); +} + +void gl_shutdown(void) +{ if (edpy != EGL_NO_DISPLAY) { eglTerminate(edpy); edpy = EGL_NO_DISPLAY; } gl_es_display = (void *)edpy; - gl_es_surface = (void *)esfc; - - if (tex_mem) free(tex_mem); - tex_mem = NULL; - gl_platform_finish(); + gl_platform_shutdown(); } diff --git a/gl.h b/gl.h index bf8ee0d..b4cbdb0 100644 --- a/gl.h +++ b/gl.h @@ -3,12 +3,14 @@ #ifdef HAVE_GLES -int gl_init(void *display, void *window, int *quirks, int w, int h); +int gl_init(void *display, int *quirks); +int gl_create(void *window, int *quirks, int w, int h); void gl_announce(void); int gl_flip_v(const void *fb, int w, int h, const float *vertices); int gl_flip(const void *fb, int w, int h); void gl_clear(void); -void gl_finish(void); +void gl_destroy(void); +void gl_shutdown(void); /* for external flips */ extern void *gl_es_display; @@ -16,7 +18,12 @@ extern void *gl_es_surface; #else -static __inline int gl_init(void *display, void *window, int *quirks, int w, int h) +static __inline int gl_init(void *display, int *quirks) +{ + return -1; +} + +static __inline int gl_create(void *window, int *quirks, int w, int h) { return -1; } @@ -32,7 +39,8 @@ static __inline int gl_flip(const void *fb, int w, int h) return -1; } static __inline void gl_clear(void) {} -static __inline void gl_finish(void) {} +static __inline void gl_destroy(void) {} +static __inline void gl_shutdown(void) {} #define gl_es_display (void *)0 #define gl_es_surface (void *)0 diff --git a/gl_platform.c b/gl_platform.c index 2540949..5d15de1 100644 --- a/gl_platform.c +++ b/gl_platform.c @@ -93,10 +93,9 @@ static void submit_rect(void) vc_dispmanx_update_submit_sync(m_dispmanUpdate); } -int gl_platform_init(void **display, void **window, int *quirks) +int gl_platform_init(void **display, int *quirks) { x11display = NULL; - x11window = 0; x11lib = dlopen("libX11.so.6", RTLD_LAZY); if (x11lib != NULL) { @@ -107,25 +106,43 @@ int gl_platform_init(void **display, void **window, int *quirks) && pXTranslateCoordinates != NULL) { x11display = *display; - x11window = (Window)*window; } } bcm_host_init(); - submit_rect(); *display = EGL_DEFAULT_DISPLAY; + + return gl_load(); +} + +int gl_platform_create(void **window, int *quirks) +{ + x11window = 0; + + if (x11lib != NULL) { + x11window = (Window)*window; + } + + submit_rect(); + *window = &m_nativeWindow; *quirks |= GL_QUIRK_ACTIVATE_RECREATE; - return gl_load(); + return !x11lib; } -void gl_platform_finish(void) +void gl_platform_destroy(void) +{ + vc_dispmanx_display_close(m_dispmanDisplay); + + x11window = 0; +} + +void gl_platform_shutdown(void) { gl_unload(); - vc_dispmanx_display_close(m_dispmanDisplay); bcm_host_deinit(); if (x11lib) { @@ -134,17 +151,25 @@ void gl_platform_finish(void) } x11display = NULL; - x11window = 0; } #else -int gl_platform_init(void **display, void **window, int *quirks) +int gl_platform_init(void **display, int *quirks) { return gl_load(); } -void gl_platform_finish(void) +int gl_platform_create(void **window, int *quirks) +{ + return 0; +} + +void gl_platform_destroy(void) +{ +} + +void gl_platform_shutdown(void) { gl_unload(); } diff --git a/gl_platform.h b/gl_platform.h index 3e0bbb1..13b135c 100644 --- a/gl_platform.h +++ b/gl_platform.h @@ -1,2 +1,4 @@ -int gl_platform_init(void **display, void **window, int *quirks); -void gl_platform_finish(void); +int gl_platform_init(void **display, int *quirks); +int gl_platform_create(void **window, int *quirks); +void gl_platform_destroy(void); +void gl_platform_shutdown(void); diff --git a/plat_sdl.c b/plat_sdl.c index ffe22d9..582351f 100644 --- a/plat_sdl.c +++ b/plat_sdl.c @@ -110,7 +110,7 @@ int plat_sdl_change_video_mode(int w, int h, int force) plat_sdl_overlay = NULL; } if (plat_sdl_gl_active) { - gl_finish(); + gl_destroy(); plat_sdl_gl_active = 0; } @@ -162,7 +162,7 @@ int plat_sdl_change_video_mode(int w, int h, int force) } else if (plat_target.vout_method == vout_mode_gl) { int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h; - plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0); + plat_sdl_gl_active = (gl_create(window, &gl_quirks, sw, sh) == 0); if (!plat_sdl_gl_active) { fprintf(stderr, "warning: could not init GL.\n"); plat_target.vout_method = 0; @@ -200,8 +200,8 @@ void plat_sdl_event_handler(void *event_) else if (plat_sdl_gl_active) { if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) { int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h; - gl_finish(); - plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0); + gl_destroy(); + plat_sdl_gl_active = (gl_create(window, &gl_quirks, sw, sh) == 0); } gl_flip(NULL, 0, 0); } @@ -322,13 +322,16 @@ int plat_sdl_init(void) if (env) try_gl = atoi(env); if (try_gl) { - update_wm_display_window(); - ret = gl_init(display, window, &gl_quirks, g_menuscreen_w, g_menuscreen_h); + ret = gl_init(display, &gl_quirks); + if (ret == 0) { + update_wm_display_window(); + ret = gl_create(window, &gl_quirks, g_menuscreen_w, g_menuscreen_h); + } } if (ret == 0) { gl_announce(); gl_works = 1; - gl_finish(); + gl_destroy(); } i = 0; @@ -362,9 +365,11 @@ void plat_sdl_finish(void) plat_sdl_overlay = NULL; } if (plat_sdl_gl_active) { - gl_finish(); + gl_destroy(); plat_sdl_gl_active = 0; } + gl_shutdown(); + // restore back to initial resolution // resolves black screen issue on R-Pi if (strcmp(vid_drv_name, "x11") != 0) -- 2.39.5