mesa/src/glx/glxcurrent.c

212 lines
6.1 KiB
C
Raw Permalink Normal View History

/*
* SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
* Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice including the dates of first publication and
* either this permission notice or a reference to
* http://oss.sgi.com/projects/FreeB/
* shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of Silicon Graphics, Inc.
* shall not be used in advertising or otherwise to promote the sale, use or
* other dealings in this Software without prior written authorization from
* Silicon Graphics, Inc.
*/
/**
* \file glxcurrent.c
* Client-side GLX interface for current context management.
*/
#include <pthread.h>
#include "glxclient.h"
#include "glapi.h"
#include "glx_error.h"
/*
** We setup some dummy structures here so that the API can be used
** even if no context is current.
*/
static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
static struct glx_context_vtable dummyVtable;
/*
** Dummy context used by small commands when there is no current context.
** All the
** gl and glx entry points are designed to operate as nop's when using
** the dummy context structure.
*/
struct glx_context dummyContext = {
&dummyBuffer[0],
&dummyBuffer[0],
&dummyBuffer[0],
&dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
sizeof(dummyBuffer),
&dummyVtable
};
/*
* Current context management and locking
*/
_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Per-thread GLX context pointer.
*
* \c __glXSetCurrentContext is written is such a way that this pointer can
* \b never be \c NULL. This is important! Because of this
* \c __glXGetCurrentContext can be implemented as trivial macro.
*/
__THREAD_INITIAL_EXEC void *__glX_tls_Context = &dummyContext;
_X_HIDDEN void
__glXSetCurrentContext(struct glx_context * c)
{
__glX_tls_Context = (c != NULL) ? c : &dummyContext;
}
_X_HIDDEN void
__glXSetCurrentContextNull(void)
{
__glXSetCurrentContext(&dummyContext);
#if defined(GLX_DIRECT_RENDERING)
_glapi_set_dispatch(NULL); /* no-op functions */
_glapi_set_context(NULL);
#endif
}
_GLX_PUBLIC GLXContext
glXGetCurrentContext(void)
{
struct glx_context *cx = __glXGetCurrentContext();
if (cx == &dummyContext) {
return NULL;
}
else {
return (GLXContext) cx;
}
}
_GLX_PUBLIC GLXDrawable
glXGetCurrentDrawable(void)
{
struct glx_context *gc = __glXGetCurrentContext();
return gc->currentDrawable;
}
/**
* Make a particular context current.
*
* \note This is in this file so that it can access dummyContext.
*/
static Bool
MakeContextCurrent(Display * dpy, GLXDrawable draw,
GLXDrawable read, GLXContext gc_user,
int opcode)
{
struct glx_context *gc = (struct glx_context *) gc_user;
struct glx_context *oldGC = __glXGetCurrentContext();
/* Make sure that the new context has a nonzero ID. In the request,
* a zero context ID is used only to mean that we bind to no current
* context.
*/
if ((gc != NULL) && (gc->xid == None)) {
return GL_FALSE;
}
_glapi_check_multithread();
__glXLock();
glx: Lift sending the MakeCurrent request to top-level code Somewhat terrifyingly, we never sent this for direct contexts, which means the server never knew the context/drawable bindings. To handle this sanely, pull the request code up out of the indirect backend, and rewrite the context switch path to call it as appropriate. This attempts to preserve the existing behavior of not calling unbind() on the context if its refcount would not drop to zero. Of course, you can't just do this indiscriminately, because this is GLX and extant X servers have bugs and everything is terrible. To wit: - For 1.20.x prior to 1.20.6, you can bind a direct context once, but the second time you try to modify the context's binding you will get GLXBadContextTag. This includes unbinding the context. And "deleting" the context will leak memory, because it will still appear to be current. - For 1.19 and earlier, glXMakeCurrent(dpy, None, ctx) should be legal for GL 3.0+ contexts, but the server will throw BadMatch. To guard against this, we only send the request for indirect contexts unless the server is known good, and only mention one context at a time in such a request; if switching between contexts, we first unbind the old, and then bind the new. Note that the second VendorRelease() version is to catch XFree86 4.x and Xorg [67].x, which almost certainly have the above bugs. Other servers might report different version numbers here, but we can't do direct rendering against them, so this should be safe. Fixes: mesa/mesa#4418 Acked-By: Mike Blumenkrantz <michael.blumenkrantz@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9992>
2017-11-14 20:13:04 +00:00
if (oldGC == gc &&
gc->currentDrawable == draw && gc->currentReadable == read) {
__glXUnlock();
return True;
}
/* can't have only one be 0 */
if (!!draw != !!read) {
__glXUnlock();
__glXSendError(dpy, BadMatch, None, opcode, True);
return False;
}
if (oldGC != &dummyContext) {
oldGC->vtable->unbind(oldGC, gc);
oldGC->currentDpy = NULL;
}
if (gc) {
/* Attempt to bind the context. We do this before mucking with
* gc and __glXSetCurrentContext to properly handle our state in
* case of an error.
*
* If an error occurs, set the Null context since we've already
* blown away our old context. The caller is responsible for
* figuring out how to handle setting a valid context.
*/
if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
__glXSetCurrentContextNull();
__glXUnlock();
__glXSendError(dpy, GLXBadContext, None, opcode, False);
return GL_FALSE;
}
gc->currentDpy = dpy;
gc->currentDrawable = draw;
gc->currentReadable = read;
__glXSetCurrentContext(gc);
} else {
__glXSetCurrentContextNull();
}
if (oldGC->currentDpy == NULL && oldGC != &dummyContext && oldGC->xid == None) {
/* We are switching away from a context that was
* previously destroyed, so we need to free the memory
* for the old handle. */
oldGC->vtable->destroy(oldGC);
}
__glXUnlock();
return GL_TRUE;
}
_GLX_PUBLIC Bool
glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
{
return MakeContextCurrent(dpy, draw, draw, gc, X_GLXMakeCurrent);
}
_GLX_PUBLIC Bool
glXMakeContextCurrent(Display *dpy, GLXDrawable d, GLXDrawable r,
GLXContext ctx)
{
return MakeContextCurrent(dpy, d, r, ctx, X_GLXMakeContextCurrent);
}
_GLX_PUBLIC Bool
glXMakeCurrentReadSGI(Display *dpy, GLXDrawable d, GLXDrawable r,
GLXContext ctx)
{
return MakeContextCurrent(dpy, d, r, ctx, X_GLXvop_MakeCurrentReadSGI);
}