diff --git a/src/context.c b/src/context.c index b4675267..234465cb 100644 --- a/src/context.c +++ b/src/context.c @@ -619,8 +619,9 @@ GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle) _GLFWwindow* window = (_GLFWwindow*) handle; _GLFWwindow* previous; + _GLFWusercontext* previousUserContext; - _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); + previousUserContext = _glfwPlatformGetTls(&_glfw.usercontextSlot); previous = _glfwPlatformGetTls(&_glfw.contextSlot); if (window && window->context.client == GLFW_NO_API) @@ -630,6 +631,12 @@ GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle) return; } + if (previousUserContext) + { + assert(previous==NULL); + previousUserContext->makeCurrent(NULL); + } + if (previous) { if (!window || window->context.source != previous->context.source) diff --git a/src/egl_context.c b/src/egl_context.c index a82c3271..36aee66e 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -919,16 +919,32 @@ GLFWbool _glfwChooseVisualEGL(const _GLFWwndconfig* wndconfig, static void _glfwMakeUserContextCurrentEGL(_GLFWusercontext* context) { - if (!eglMakeCurrent(_glfw.egl.display, - context->egl.surface, - context->egl.surface, - context->egl.handle)) + if (context) { - _glfwInputError(GLFW_PLATFORM_ERROR, - "EGL: Failed to make user context current: %s", - getEGLErrorString(eglGetError())); - _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); - return; + if (!eglMakeCurrent(_glfw.egl.display, + context->egl.surface, + context->egl.surface, + context->egl.handle)) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "EGL: Failed to make user context current: %s", + getEGLErrorString(eglGetError())); + _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); + return; + } + } + else + { + if (!eglMakeCurrent(_glfw.egl.display, + EGL_NO_SURFACE, + EGL_NO_SURFACE, + EGL_NO_CONTEXT)) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "EGL: Failed to clear current user context: %s", + getEGLErrorString(eglGetError())); + return; + } } _glfwPlatformSetTls(&_glfw.usercontextSlot, context); } diff --git a/src/glx_context.c b/src/glx_context.c index e70ebfcc..ec8a033a 100644 --- a/src/glx_context.c +++ b/src/glx_context.c @@ -687,12 +687,24 @@ GLFWbool _glfwChooseVisualGLX(const _GLFWwndconfig* wndconfig, static void _glfwMakeUserContextCurrentGLX(_GLFWusercontext* context) { - if(!glXMakeCurrent(_glfw.x11.display, context->window->context.glx.window,context->glx.handle)) + if (context) + { + if(!glXMakeCurrent(_glfw.x11.display, context->window->context.glx.window,context->glx.handle)) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "GLX: Failed to make user context current"); + _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); + return; + } + } + else { - _glfwInputError(GLFW_PLATFORM_ERROR, - "GLX: Failed to make user context current"); - _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); - return; + if (!glXMakeCurrent(_glfw.x11.display, None, NULL)) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "GLX: Failed to clear current user context"); + return; + } } _glfwPlatformSetTls(&_glfw.usercontextSlot, context); } diff --git a/src/nsgl_context.m b/src/nsgl_context.m index 74fbbcd9..dc68d96a 100644 --- a/src/nsgl_context.m +++ b/src/nsgl_context.m @@ -357,9 +357,12 @@ static void _glfwMakeUserContextCurrentNSGL(_GLFWusercontext* context) { @autoreleasepool { + if (context) [context->nsgl.object makeCurrentContext]; + else + [NSOpenGLContext clearCurrentContext]; - _glfwPlatformSetTls(&_glfw.usercontextSlot, context); + _glfwPlatformSetTls(&_glfw.usercontextSlot, context); } // autoreleasepool } @@ -368,7 +371,7 @@ static void _glfwDestroyUserContextNSGL(_GLFWusercontext* context) { @autoreleasepool { - [context->nsgl.object release]; + [context->nsgl.object release]; } // autoreleasepool free(context); diff --git a/src/osmesa_context.c b/src/osmesa_context.c index fcd978c5..f482d0fd 100644 --- a/src/osmesa_context.c +++ b/src/osmesa_context.c @@ -300,15 +300,18 @@ GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window, static void _glfwMakeUserContextCurrentOSMesa(_GLFWusercontext* context) { - if (!OSMesaMakeCurrent(context->osmesa.handle, - context->window->context.osmesa.buffer, - GL_UNSIGNED_BYTE, - context->window->context.osmesa.width, context->window->context.osmesa.height)) + if (context) { - _glfwInputError(GLFW_PLATFORM_ERROR, - "OSMesa: Failed to make user context current"); - _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); - return; + if (!OSMesaMakeCurrent(context->osmesa.handle, + context->window->context.osmesa.buffer, + GL_UNSIGNED_BYTE, + context->window->context.osmesa.width, context->window->context.osmesa.height)) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "OSMesa: Failed to make user context current"); + _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); + return; + } } _glfwPlatformSetTls(&_glfw.usercontextSlot, context); } diff --git a/src/wgl_context.c b/src/wgl_context.c index de2ca0a5..057c6f5a 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -787,13 +787,25 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, static void _glfwMakeUserContextCurrentWGL(_GLFWusercontext* context) { - if(!wglMakeCurrent(context->window->context.wgl.dc,context->wgl.handle)) + if (context) { - _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, - "WGL: Failed to make user context current"); - _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); - return; + if (!wglMakeCurrent(context->window->context.wgl.dc,context->wgl.handle)) + { + _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, + "WGL: Failed to make user context current"); + _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL); + return; + } } + else + { + if (!wglMakeCurrent(NULL, NULL)) + { + _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, + "WGL: Failed to clear current user context"); + } + } + _glfwPlatformSetTls(&_glfw.usercontextSlot, context); }