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,11 +775,17 @@ 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

@ -817,32 +817,18 @@ GLFWbool _glfwChooseVisualEGL(const _GLFWwndconfig* wndconfig,
static void _glfwMakeUserContextCurrentEGL(_GLFWusercontext* context) static void _glfwMakeUserContextCurrentEGL(_GLFWusercontext* context)
{ {
if(context) if (!eglMakeCurrent(_glfw.egl.display,
context->window->context.egl.surface,
context->window->context.egl.surface,
context->egl.handle))
{ {
if (!eglMakeCurrent(_glfw.egl.display, _glfwInputError(GLFW_PLATFORM_ERROR,
context->window->context.egl.surface, "EGL: Failed to make user context current: %s",
context->window->context.egl.surface, getEGLErrorString(eglGetError()));
context->egl.handle)) _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
{ return;
_glfwInputError(GLFW_PLATFORM_ERROR,
"EGL: Failed to make context current: %s",
getEGLErrorString(eglGetError()));
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 context: %s",
getEGLErrorString(eglGetError()));
return;
}
} }
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
} }
static void _glfwDestroyUserContextEGL(_GLFWusercontext* context) static void _glfwDestroyUserContextEGL(_GLFWusercontext* context)

View File

@ -680,22 +680,14 @@ 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,
{ "GLX: Failed to make user context current");
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
"GLX: Failed to set current user context"); 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

@ -301,18 +301,17 @@ GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window,
static void _glfwMakeUserContextCurrentOSMesa(_GLFWusercontext* context) static void _glfwMakeUserContextCurrentOSMesa(_GLFWusercontext* context)
{ {
if(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 (!OSMesaMakeCurrent(context->osmesa.handle, _glfwInputError(GLFW_PLATFORM_ERROR,
context->window->context.osmesa.buffer, "OSMesa: Failed to make user context current");
GL_UNSIGNED_BYTE, _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
context->window->context.osmesa.width, context->window->context.osmesa.height)) return;
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"OSMesa: Failed to make context current");
return;
}
} }
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
} }
static void _glfwDestroyUserContextOSMesa(_GLFWusercontext* context) static void _glfwDestroyUserContextOSMesa(_GLFWusercontext* context)

View File

@ -792,22 +792,14 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window,
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,
{ "WGL: Failed to make user context current");
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR, _glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
"WGL: Failed to set current user context"); 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)