Commit 1f752fb1 authored by Doug Binks's avatar Doug Binks Committed by Degrande Samuel
Browse files

Added TLS for current user context and simplified code

parent ec1d0794
......@@ -617,6 +617,8 @@ GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle)
_GLFW_REQUIRE_INIT();
_glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
if (window && window->context.client == GLFW_NO_API)
{
_glfwInputError(GLFW_NO_WINDOW_CONTEXT,
......@@ -773,11 +775,17 @@ GLFWAPI GLFWusercontext* glfwCreateUserContext(GLFWwindow* handle)
GLFWAPI void glfwDestroyUserContext(GLFWusercontext* handle)
{
_GLFWusercontext* context = (_GLFWusercontext*)handle;
_GLFWusercontext* prev = _glfwPlatformGetTls(&_glfw.usercontextSlot);
_GLFW_REQUIRE_INIT();
if (context)
{
if(prev==context)
_glfwPlatformSetTls(&_glfw.usercontextSlot,NULL);
context->destroy(context);
}
}
GLFWAPI void glfwMakeUserContextCurrent(GLFWusercontext* handle)
......@@ -786,6 +794,9 @@ GLFWAPI void glfwMakeUserContextCurrent(GLFWusercontext* handle)
_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);
if (context)
......
......@@ -776,32 +776,18 @@ GLFWbool _glfwChooseVisualEGL(const _GLFWwndconfig* wndconfig,
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))
{
_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",
"EGL: Failed to make user context current: %s",
getEGLErrorString(eglGetError()));
_glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
return;
}
}
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
}
static void _glfwDestroyUserContextEGL(_GLFWusercontext* context)
......
......@@ -680,22 +680,14 @@ GLFWbool _glfwChooseVisualGLX(const _GLFWwndconfig* wndconfig,
static void _glfwMakeUserContextCurrentGLX(_GLFWusercontext* context)
{
if(context)
{
if(!glXMakeCurrent(_glfw.x11.display, context->window->context.glx.window,context->glx.handle))
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"GLX: Failed to set current user context");
}
}
else
{
if (!glXMakeCurrent(_glfw.x11.display, None, NULL))
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"GLX: Failed to clear current context");
}
"GLX: Failed to make user context current");
_glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
return;
}
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
}
static void _glfwDestroyUserContextGLX(_GLFWusercontext* context)
......
......@@ -101,6 +101,7 @@ static void terminate(void)
free(error);
}
_glfwPlatformDestroyTls(&_glfw.usercontextSlot);
_glfwPlatformDestroyTls(&_glfw.contextSlot);
_glfwPlatformDestroyTls(&_glfw.errorSlot);
_glfwPlatformDestroyMutex(&_glfw.errorLock);
......@@ -237,7 +238,8 @@ GLFWAPI int glfwInit(void)
if (!_glfwPlatformCreateMutex(&_glfw.errorLock) ||
!_glfwPlatformCreateTls(&_glfw.errorSlot) ||
!_glfwPlatformCreateTls(&_glfw.contextSlot))
!_glfwPlatformCreateTls(&_glfw.contextSlot) ||
!_glfwPlatformCreateTls(&_glfw.usercontextSlot))
{
terminate();
return GLFW_FALSE;
......
......@@ -557,6 +557,7 @@ struct _GLFWlibrary
_GLFWtls errorSlot;
_GLFWtls contextSlot;
_GLFWtls usercontextSlot;
_GLFWmutex errorLock;
struct {
......
......@@ -360,10 +360,9 @@ static void _glfwMakeUserContextCurrentNSGL(_GLFWusercontext* context)
{
@autoreleasepool {
if (context)
[context->nsgl.object makeCurrentContext];
else
[NSOpenGLContext clearCurrentContext];
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
} // autoreleasepool
}
......@@ -372,7 +371,6 @@ static void _glfwDestroyUserContextNSGL(_GLFWusercontext* context)
{
@autoreleasepool {
if (context->nsgl.object)
[context->nsgl.object release];
} // autoreleasepool
......
......@@ -301,18 +301,17 @@ GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window,
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))
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"OSMesa: Failed to make context current");
"OSMesa: Failed to make user context current");
_glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
return;
}
}
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
}
static void _glfwDestroyUserContextOSMesa(_GLFWusercontext* context)
......
......@@ -792,22 +792,14 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window,
static void _glfwMakeUserContextCurrentWGL(_GLFWusercontext* context)
{
if(context)
{
if(!wglMakeCurrent(context->window->context.wgl.dc,context->wgl.handle))
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"WGL: Failed to set current user context");
}
}
else
{
if (!wglMakeCurrent(NULL, NULL))
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"WGL: Failed to clear current context");
}
"WGL: Failed to make user context current");
_glfwPlatformSetTls(&_glfw.usercontextSlot, NULL);
return;
}
_glfwPlatformSetTls(&_glfw.usercontextSlot, context);
}
static void _glfwDestroyUserContextWGL(_GLFWusercontext* context)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment