2008-04-18 15:28:34 +01:00
|
|
|
/*
|
Update to SGI FreeB 2.0.
Under the terms of version 1.1, "once Covered Code has been published
under a particular version of the License, Recipient may, for the
duration of the License, continue to use it under the terms of that
version, or choose to use such Covered Code under the terms of any
subsequent version published by SGI."
FreeB 2.0 license refers to "dates of first publication". They are here
taken to be 1991-2000, as noted in the original license text:
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
Official FreeB 2.0 text:
http://oss.sgi.com/projects/FreeB/SGIFreeSWLicB.2.0.pdf
As always, this code has not been tested for conformance with the OpenGL
specification. OpenGL conformance testing is available from
http://khronos.org/ and is required for use of the OpenGL logo in
product advertising and promotion.
2008-09-19 22:16:53 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2008-04-18 15:28:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file glxcurrent.c
|
|
|
|
* Client-side GLX interface for current context management.
|
|
|
|
*/
|
|
|
|
|
2010-04-01 19:01:31 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2008-04-18 15:28:34 +01:00
|
|
|
#include "glxclient.h"
|
2011-06-08 20:03:10 +01:00
|
|
|
#include "glapi.h"
|
2017-11-14 20:13:02 +00:00
|
|
|
#include "glx_error.h"
|
2011-06-08 20:03:10 +01:00
|
|
|
|
2008-04-18 15:28:34 +01:00
|
|
|
/*
|
|
|
|
** 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];
|
2010-09-07 19:32:28 +01:00
|
|
|
static struct glx_context_vtable dummyVtable;
|
2008-04-18 15:28:34 +01:00
|
|
|
/*
|
|
|
|
** 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.
|
|
|
|
*/
|
2010-07-29 23:44:26 +01:00
|
|
|
struct glx_context dummyContext = {
|
2008-10-13 13:07:07 +01:00
|
|
|
&dummyBuffer[0],
|
|
|
|
&dummyBuffer[0],
|
|
|
|
&dummyBuffer[0],
|
|
|
|
&dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
|
|
|
|
sizeof(dummyBuffer),
|
2010-09-07 19:32:28 +01:00
|
|
|
&dummyVtable
|
2008-04-18 15:28:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Current context management and locking
|
|
|
|
*/
|
|
|
|
|
|
|
|
_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Per-thread GLX context pointer.
|
2009-08-12 11:41:22 +01:00
|
|
|
*
|
2008-04-18 15:28:34 +01:00
|
|
|
* \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.
|
|
|
|
*/
|
2021-05-21 21:23:07 +01:00
|
|
|
__THREAD_INITIAL_EXEC void *__glX_tls_Context = &dummyContext;
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2008-10-13 13:07:07 +01:00
|
|
|
_X_HIDDEN void
|
2010-07-28 16:16:00 +01:00
|
|
|
__glXSetCurrentContext(struct glx_context * c)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2008-10-13 13:07:07 +01:00
|
|
|
__glX_tls_Context = (c != NULL) ? c : &dummyContext;
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
2008-10-13 13:07:07 +01:00
|
|
|
_X_HIDDEN void
|
|
|
|
__glXSetCurrentContextNull(void)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2008-10-13 13:07:07 +01:00
|
|
|
__glXSetCurrentContext(&dummyContext);
|
2011-06-08 20:03:10 +01:00
|
|
|
#if defined(GLX_DIRECT_RENDERING)
|
2008-10-13 13:07:07 +01:00
|
|
|
_glapi_set_dispatch(NULL); /* no-op functions */
|
2010-01-17 07:19:19 +00:00
|
|
|
_glapi_set_context(NULL);
|
2008-04-18 15:28:34 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-11 19:01:53 +01:00
|
|
|
_GLX_PUBLIC GLXContext
|
2008-10-13 13:07:07 +01:00
|
|
|
glXGetCurrentContext(void)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2010-07-28 16:16:00 +01:00
|
|
|
struct glx_context *cx = __glXGetCurrentContext();
|
2008-10-13 13:07:07 +01:00
|
|
|
|
|
|
|
if (cx == &dummyContext) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
2010-07-28 16:16:00 +01:00
|
|
|
return (GLXContext) cx;
|
2008-10-13 13:07:07 +01:00
|
|
|
}
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
2016-05-11 19:01:53 +01:00
|
|
|
_GLX_PUBLIC GLXDrawable
|
2008-10-13 13:07:07 +01:00
|
|
|
glXGetCurrentDrawable(void)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2010-07-28 16:16:00 +01:00
|
|
|
struct glx_context *gc = __glXGetCurrentContext();
|
2008-10-13 13:07:07 +01:00
|
|
|
return gc->currentDrawable;
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 19:25:09 +01:00
|
|
|
/**
|
|
|
|
* Make a particular context current.
|
|
|
|
*
|
|
|
|
* \note This is in this file so that it can access dummyContext.
|
|
|
|
*/
|
2008-10-13 13:07:07 +01:00
|
|
|
static Bool
|
|
|
|
MakeContextCurrent(Display * dpy, GLXDrawable draw,
|
2021-04-16 19:25:16 +01:00
|
|
|
GLXDrawable read, GLXContext gc_user,
|
|
|
|
int opcode)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2010-07-28 16:16:00 +01:00
|
|
|
struct glx_context *gc = (struct glx_context *) gc_user;
|
|
|
|
struct glx_context *oldGC = __glXGetCurrentContext();
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2008-10-13 13:07:07 +01:00
|
|
|
/* 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;
|
2009-04-02 10:00:41 +01:00
|
|
|
}
|
|
|
|
|
2008-10-13 13:07:07 +01:00
|
|
|
_glapi_check_multithread();
|
2018-07-06 19:59:21 +01:00
|
|
|
|
2021-04-15 19:25:09 +01:00
|
|
|
__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 &&
|
2021-04-15 19:25:09 +01:00
|
|
|
gc->currentDrawable == draw && gc->currentReadable == read) {
|
|
|
|
__glXUnlock();
|
|
|
|
return True;
|
2019-09-26 16:07:42 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 19:25:09 +01:00
|
|
|
/* can't have only one be 0 */
|
|
|
|
if (!!draw != !!read) {
|
|
|
|
__glXUnlock();
|
2021-04-16 19:25:16 +01:00
|
|
|
__glXSendError(dpy, BadMatch, None, opcode, True);
|
2021-04-15 19:25:09 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldGC != &dummyContext) {
|
2022-07-11 21:11:53 +01:00
|
|
|
oldGC->vtable->unbind(oldGC, gc);
|
|
|
|
oldGC->currentDpy = NULL;
|
2021-04-15 19:25:09 +01:00
|
|
|
}
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2021-04-15 19:25:09 +01:00
|
|
|
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) {
|
2011-06-17 20:24:55 +01:00
|
|
|
__glXSetCurrentContextNull();
|
2021-04-15 19:25:09 +01:00
|
|
|
__glXUnlock();
|
2021-04-16 19:25:16 +01:00
|
|
|
__glXSendError(dpy, GLXBadContext, None, opcode, False);
|
2021-04-15 19:25:09 +01:00
|
|
|
return GL_FALSE;
|
2011-06-17 20:24:55 +01:00
|
|
|
}
|
|
|
|
|
2022-07-11 21:11:53 +01:00
|
|
|
gc->currentDpy = dpy;
|
|
|
|
gc->currentDrawable = draw;
|
|
|
|
gc->currentReadable = read;
|
2021-04-15 19:25:09 +01:00
|
|
|
__glXSetCurrentContext(gc);
|
|
|
|
} else {
|
2010-07-28 20:33:09 +01:00
|
|
|
__glXSetCurrentContextNull();
|
2021-04-15 19:25:09 +01:00
|
|
|
}
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2022-07-11 21:11:53 +01:00
|
|
|
if (oldGC->currentDpy == NULL && oldGC != &dummyContext && oldGC->xid == None) {
|
2021-04-15 19:25:09 +01:00
|
|
|
/* 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);
|
2011-06-17 20:28:05 +01:00
|
|
|
}
|
|
|
|
|
2011-02-04 01:26:02 +00:00
|
|
|
__glXUnlock();
|
2021-04-15 19:25:09 +01:00
|
|
|
|
|
|
|
return GL_TRUE;
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
2016-05-11 19:01:53 +01:00
|
|
|
_GLX_PUBLIC Bool
|
2008-10-13 13:07:07 +01:00
|
|
|
glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2021-04-16 19:25:16 +01:00
|
|
|
return MakeContextCurrent(dpy, draw, draw, gc, X_GLXMakeCurrent);
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 19:25:16 +01:00
|
|
|
_GLX_PUBLIC Bool
|
|
|
|
glXMakeContextCurrent(Display *dpy, GLXDrawable d, GLXDrawable r,
|
|
|
|
GLXContext ctx)
|
|
|
|
{
|
|
|
|
return MakeContextCurrent(dpy, d, r, ctx, X_GLXMakeContextCurrent);
|
|
|
|
}
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2021-04-16 19:25:16 +01:00
|
|
|
_GLX_PUBLIC Bool
|
|
|
|
glXMakeCurrentReadSGI(Display *dpy, GLXDrawable d, GLXDrawable r,
|
|
|
|
GLXContext ctx)
|
|
|
|
{
|
|
|
|
return MakeContextCurrent(dpy, d, r, ctx, X_GLXvop_MakeCurrentReadSGI);
|
|
|
|
}
|