From 703caed4b39734c259894713691d20f7678b3b8e Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Mon, 11 Nov 2024 15:04:36 +0100 Subject: [PATCH 1/2] WGL: Avoid loading EGL when not needed Avoid loading EGL when not needed when resolving functions common to GL/EGL and a WGL context is current. There's already a similar check for GLX, we add one for WGL too. --- src/dispatch_common.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/dispatch_common.c b/src/dispatch_common.c index 9f3dac4..c15895b 100644 --- a/src/dispatch_common.c +++ b/src/dispatch_common.c @@ -817,6 +817,11 @@ epoxy_get_bootstrap_proc_address(const char *name) return epoxy_gl_dlsym(name); #endif +#ifdef _WIN32 + if (api.gl_handle && wglGetCurrentContext()) + return epoxy_gl_dlsym(name); +#endif + /* If epoxy hasn't loaded any API-specific library yet, try to * figure out what API the context is using and use that library, * since future calls will also use that API (this prevents a From fcb99135597b40849a061cea3e8bb48a97c88366 Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Mon, 11 Nov 2024 15:41:22 +0100 Subject: [PATCH 2/2] EGL: Check for EGL_NO_DISPLAY before calling eglQueryContext According to the reference documentation that could lead to undefined behavior. Reference: https://registry.khronos.org/EGL/sdk/docs/man/html/eglGetCurrentDisplay.xhtml --- src/dispatch_common.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/dispatch_common.c b/src/dispatch_common.c index c15895b..ee42ce3 100644 --- a/src/dispatch_common.c +++ b/src/dispatch_common.c @@ -781,12 +781,16 @@ epoxy_get_core_proc_address(const char *name, int core_version) static EGLenum epoxy_egl_get_current_gl_context_api(void) { + EGLDisplay *egl_display = eglGetCurrentDisplay(); + EGLContext *egl_context = eglGetCurrentContext(); EGLint curapi; if (!api.egl_handle) return EGL_NONE; - if (eglQueryContext(eglGetCurrentDisplay(), eglGetCurrentContext(), + if (egl_display == EGL_NO_DISPLAY || + eglQueryContext(egl_display, + egl_context, EGL_CONTEXT_CLIENT_TYPE, &curapi) == EGL_FALSE) { (void)eglGetError(); return EGL_NONE; @@ -835,8 +839,13 @@ epoxy_get_bootstrap_proc_address(const char *name) case EGL_OPENGL_API: return epoxy_gl_dlsym(name); case EGL_OPENGL_ES_API: - if (eglQueryContext(eglGetCurrentDisplay(), - eglGetCurrentContext(), + { + EGLDisplay *egl_display = eglGetCurrentDisplay(); + EGLContext *egl_context = eglGetCurrentContext(); + + if (egl_display != EGL_NO_DISPLAY && + eglQueryContext(egl_display, + egl_context, EGL_CONTEXT_CLIENT_VERSION, &version)) { if (version >= 2) @@ -845,6 +854,7 @@ epoxy_get_bootstrap_proc_address(const char *name) return epoxy_gles1_dlsym(name); } } + } } #endif /* PLATFORM_HAS_EGL */