From 84ecb32c98be5f2df04e2c386fb1d0bb0f6d01d0 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Tue, 30 Jan 2024 15:51:37 +0100 Subject: [PATCH] Fix OpenGL context caching on macOS (#737) --- FL/mac.H | 1 + src/Fl_Gl_Choice.cxx | 10 ++++++++++ src/Fl_cocoa.mm | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/FL/mac.H b/FL/mac.H index 8c4e3b994..6d7a51293 100644 --- a/FL/mac.H +++ b/FL/mac.H @@ -214,6 +214,7 @@ public: static void GLcontext_flushbuffer(NSOpenGLContext*); static void GLcontext_release(NSOpenGLContext*); static void GLcontext_makecurrent(NSOpenGLContext*); + static NSOpenGLContext* GLcontext_getcurrent(); static void GL_cleardrawable(void); static void gl_start(NSOpenGLContext*); void destroy(void); diff --git a/src/Fl_Gl_Choice.cxx b/src/Fl_Gl_Choice.cxx index fffeda387..071a1a790 100644 --- a/src/Fl_Gl_Choice.cxx +++ b/src/Fl_Gl_Choice.cxx @@ -409,6 +409,11 @@ static GLContext cached_context; static Fl_Window* cached_window; void fl_set_gl_context(Fl_Window* w, GLContext context) { +#ifdef __APPLE_QUARTZ__ + // macOS may switch buffers during swapping, so we override the + // cached context with the actual current context (#737) + cached_context = Fl_X::GLcontext_getcurrent(); +#endif if (context != cached_context || w != cached_window) { cached_context = context; cached_window = w; @@ -439,6 +444,11 @@ void fl_no_gl_context() { } void fl_delete_gl_context(GLContext context) { +#ifdef __APPLE_QUARTZ__ + // macOS may switch buffers during swapping, so we override the + // cached context with the actual current context (#737) + cached_context = Fl_X::GLcontext_getcurrent(); +#endif if (cached_context == context) fl_no_gl_context(); # if defined(USE_X11) glXDestroyContext(fl_display, context); diff --git a/src/Fl_cocoa.mm b/src/Fl_cocoa.mm index 630412125..2b8a3dab1 100644 --- a/src/Fl_cocoa.mm +++ b/src/Fl_cocoa.mm @@ -3094,6 +3094,11 @@ void Fl_X::GLcontext_makecurrent(NSOpenGLContext* ctxt) [ctxt makeCurrentContext]; } +NSOpenGLContext* Fl_X::GLcontext_getcurrent() +{ + return [NSOpenGLContext currentContext]; +} + void Fl_Window::fullscreen_x() { _set_fullscreen(); if (fl_mac_os_version < 101000) {