Added TLS for current user context and simplified code

This commit is contained in:
Doug Binks 2020-07-15 13:19:14 +01:00
parent a1a1b77150
commit 6539d101f3
8 changed files with 48 additions and 67 deletions

View File

@ -617,6 +617,8 @@ GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle)
_GLFW_REQUIRE_INIT(); _GLFW_REQUIRE_INIT();
_glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
if (window && window->context.client == GLFW_NO_API) if (window && window->context.client == GLFW_NO_API)
{ {
_glfwInputError(GLFW_NO_WINDOW_CONTEXT, _glfwInputError(GLFW_NO_WINDOW_CONTEXT,
@ -773,12 +775,18 @@ GLFWAPI GLFWusercontext* glfwCreateUserContext(GLFWwindow* handle)
GLFWAPI void glfwDestroyUserContext(GLFWusercontext* handle) GLFWAPI void glfwDestroyUserContext(GLFWusercontext* handle)
{ {
_GLFWusercontext* context = (_GLFWusercontext*)handle; _GLFWusercontext* context = (_GLFWusercontext*)handle;
_GLFWusercontext* prev = _glfwPlatformGetTls(&_glfw.usercontextSlot);
_GLFW_REQUIRE_INIT(); _GLFW_REQUIRE_INIT();
if (context) if (context)
{
if(prev==context)
_glfwPlatformSetTls(&_glfw.usercontextSlot,NULL);
context->destroy(context); context->destroy(context);
} }
}
GLFWAPI void glfwMakeUserContextCurrent(GLFWusercontext* handle) GLFWAPI void glfwMakeUserContextCurrent(GLFWusercontext* handle)
{ {
@ -786,6 +794,9 @@ GLFWAPI void glfwMakeUserContextCurrent(GLFWusercontext* handle)
_GLFW_REQUIRE_INIT(); _GLFW_REQUIRE_INIT();
// Call glfwMakeContextCurrent(NULL) to both clear context TLS and set
// context to NULL if required by platform & context, and this
// handles case of calling glfwMakeUserContextCurrent(NULL)
glfwMakeContextCurrent(NULL); glfwMakeContextCurrent(NULL);
if (context) if (context)

View File

@ -816,8 +816,6 @@ GLFWbool _glfwChooseVisualEGL(const _GLFWwndconfig* wndconfig,
#endif // _GLFW_X11 #endif // _GLFW_X11
static void _glfwMakeUserContextCurrentEGL(_GLFWusercontext* context) static void _glfwMakeUserContextCurrentEGL(_GLFWusercontext* context)
{
if(context)
{ {
if (!eglMakeCurrent(_glfw.egl.display, if (!eglMakeCurrent(_glfw.egl.display,
context->window->context.egl.surface, context->window->context.egl.surface,
@ -825,24 +823,12 @@ static void _glfwMakeUserContextCurrentEGL(_GLFWusercontext* context)
context->egl.handle)) context->egl.handle))
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"EGL: Failed to make context current: %s", "EGL: Failed to make user context current: %s",
getEGLErrorString(eglGetError())); getEGLErrorString(eglGetError()));
_glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
return; return;
} }
} _glfwPlatformSetTls(&_glfw.usercontextSlot, context);
else
{
if (!eglMakeCurrent(_glfw.egl.display,
EGL_NO_SURFACE,
EGL_NO_SURFACE,
EGL_NO_CONTEXT))
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"EGL: Failed to clear current context: %s",
getEGLErrorString(eglGetError()));
return;
}
}
} }
static void _glfwDestroyUserContextEGL(_GLFWusercontext* context) static void _glfwDestroyUserContextEGL(_GLFWusercontext* context)

View File

@ -679,23 +679,15 @@ GLFWbool _glfwChooseVisualGLX(const _GLFWwndconfig* wndconfig,
} }
static void _glfwMakeUserContextCurrentGLX(_GLFWusercontext* context) static void _glfwMakeUserContextCurrentGLX(_GLFWusercontext* context)
{
if(context)
{ {
if(!glXMakeCurrent(_glfw.x11.display, context->window->context.glx.window,context->glx.handle)) if(!glXMakeCurrent(_glfw.x11.display, context->window->context.glx.window,context->glx.handle))
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"GLX: Failed to set current user context"); "GLX: Failed to make user context current");
} _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
} return;
else
{
if (!glXMakeCurrent(_glfw.x11.display, None, NULL))
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"GLX: Failed to clear current context");
}
} }
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
} }
static void _glfwDestroyUserContextGLX(_GLFWusercontext* context) static void _glfwDestroyUserContextGLX(_GLFWusercontext* context)

View File

@ -102,6 +102,7 @@ static void terminate(void)
free(error); free(error);
} }
_glfwPlatformDestroyTls(&_glfw.usercontextSlot);
_glfwPlatformDestroyTls(&_glfw.contextSlot); _glfwPlatformDestroyTls(&_glfw.contextSlot);
_glfwPlatformDestroyTls(&_glfw.errorSlot); _glfwPlatformDestroyTls(&_glfw.errorSlot);
_glfwPlatformDestroyMutex(&_glfw.errorLock); _glfwPlatformDestroyMutex(&_glfw.errorLock);
@ -244,7 +245,8 @@ GLFWAPI int glfwInit(void)
if (!_glfwPlatformCreateMutex(&_glfw.errorLock) || if (!_glfwPlatformCreateMutex(&_glfw.errorLock) ||
!_glfwPlatformCreateTls(&_glfw.errorSlot) || !_glfwPlatformCreateTls(&_glfw.errorSlot) ||
!_glfwPlatformCreateTls(&_glfw.contextSlot)) !_glfwPlatformCreateTls(&_glfw.contextSlot) ||
!_glfwPlatformCreateTls(&_glfw.usercontextSlot))
{ {
terminate(); terminate();
return GLFW_FALSE; return GLFW_FALSE;

View File

@ -564,6 +564,7 @@ struct _GLFWlibrary
_GLFWtls errorSlot; _GLFWtls errorSlot;
_GLFWtls contextSlot; _GLFWtls contextSlot;
_GLFWtls usercontextSlot;
_GLFWmutex errorLock; _GLFWmutex errorLock;
struct { struct {

View File

@ -353,10 +353,9 @@ static void _glfwMakeUserContextCurrentNSGL(_GLFWusercontext* context)
{ {
@autoreleasepool { @autoreleasepool {
if (context)
[context->nsgl.object makeCurrentContext]; [context->nsgl.object makeCurrentContext];
else
[NSOpenGLContext clearCurrentContext]; _glfwPlatformSetTls(&_glfw.usercontextSlot, context);
} // autoreleasepool } // autoreleasepool
} }
@ -365,7 +364,6 @@ static void _glfwDestroyUserContextNSGL(_GLFWusercontext* context)
{ {
@autoreleasepool { @autoreleasepool {
if (context->nsgl.object)
[context->nsgl.object release]; [context->nsgl.object release];
} // autoreleasepool } // autoreleasepool

View File

@ -300,8 +300,6 @@ GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window,
} }
static void _glfwMakeUserContextCurrentOSMesa(_GLFWusercontext* context) static void _glfwMakeUserContextCurrentOSMesa(_GLFWusercontext* context)
{
if(context)
{ {
if (!OSMesaMakeCurrent(context->osmesa.handle, if (!OSMesaMakeCurrent(context->osmesa.handle,
context->window->context.osmesa.buffer, context->window->context.osmesa.buffer,
@ -309,10 +307,11 @@ static void _glfwMakeUserContextCurrentOSMesa(_GLFWusercontext* context)
context->window->context.osmesa.width, context->window->context.osmesa.height)) context->window->context.osmesa.width, context->window->context.osmesa.height))
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"OSMesa: Failed to make context current"); "OSMesa: Failed to make user context current");
_glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
return; return;
} }
} _glfwPlatformSetTls(&_glfw.usercontextSlot, context);
} }
static void _glfwDestroyUserContextOSMesa(_GLFWusercontext* context) static void _glfwDestroyUserContextOSMesa(_GLFWusercontext* context)

View File

@ -791,23 +791,15 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window,
#undef setAttrib #undef setAttrib
static void _glfwMakeUserContextCurrentWGL(_GLFWusercontext* context) static void _glfwMakeUserContextCurrentWGL(_GLFWusercontext* context)
{
if(context)
{ {
if(!wglMakeCurrent(context->window->context.wgl.dc,context->wgl.handle)) if(!wglMakeCurrent(context->window->context.wgl.dc,context->wgl.handle))
{ {
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR, _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"WGL: Failed to set current user context"); "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 context");
}
} }
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
} }
static void _glfwDestroyUserContextWGL(_GLFWusercontext* context) static void _glfwDestroyUserContextWGL(_GLFWusercontext* context)