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
|
|
|
#ifdef PTHREADS
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2008-04-18 15:28:34 +01:00
|
|
|
#include "glxclient.h"
|
2010-04-01 19:01:31 +01:00
|
|
|
#ifdef GLX_USE_APPLEGL
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "apple_glx.h"
|
|
|
|
#include "apple_glx_context.h"
|
|
|
|
#endif
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2011-06-08 20:03:10 +01:00
|
|
|
#include "glapi.h"
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2009-07-23 16:58:08 +01:00
|
|
|
#if defined( PTHREADS )
|
2008-04-18 15:28:34 +01:00
|
|
|
|
|
|
|
_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
# if defined( GLX_USE_TLS )
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2008-10-13 13:07:07 +01:00
|
|
|
__thread void *__glX_tls_Context __attribute__ ((tls_model("initial-exec")))
|
|
|
|
= &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
|
|
|
}
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
|
|
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Per-thread data key.
|
2009-08-12 11:41:22 +01:00
|
|
|
*
|
2008-04-18 15:28:34 +01:00
|
|
|
* Once \c init_thread_data has been called, the per-thread data key will
|
|
|
|
* take a value of \c NULL. As each new thread is created the default
|
|
|
|
* value, in that thread, will be \c NULL.
|
|
|
|
*/
|
|
|
|
static pthread_key_t ContextTSD;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the per-thread data key.
|
2009-08-12 11:41:22 +01:00
|
|
|
*
|
2008-04-18 15:28:34 +01:00
|
|
|
* This function is called \b exactly once per-process (not per-thread!) to
|
|
|
|
* initialize the per-thread data key. This is ideally done using the
|
|
|
|
* \c pthread_once mechanism.
|
|
|
|
*/
|
2008-10-13 13:07:07 +01:00
|
|
|
static void
|
|
|
|
init_thread_data(void)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2008-10-13 13:07:07 +01:00
|
|
|
if (pthread_key_create(&ContextTSD, NULL) != 0) {
|
|
|
|
perror("pthread_key_create");
|
|
|
|
exit(-1);
|
|
|
|
}
|
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
|
|
|
pthread_once(&once_control, init_thread_data);
|
|
|
|
pthread_setspecific(ContextTSD, c);
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
2010-07-28 16:16:00 +01:00
|
|
|
_X_HIDDEN struct glx_context *
|
2008-10-13 13:07:07 +01:00
|
|
|
__glXGetCurrentContext(void)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2008-10-13 13:07:07 +01:00
|
|
|
void *v;
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2008-10-13 13:07:07 +01:00
|
|
|
pthread_once(&once_control, init_thread_data);
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2008-10-13 13:07:07 +01:00
|
|
|
v = pthread_getspecific(ContextTSD);
|
2010-07-28 16:16:00 +01:00
|
|
|
return (v == NULL) ? &dummyContext : (struct glx_context *) v;
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# endif /* defined( GLX_USE_TLS ) */
|
|
|
|
|
|
|
|
#elif defined( THREADS )
|
|
|
|
|
|
|
|
#error Unknown threading method specified.
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* not thread safe */
|
2010-07-28 16:16:00 +01:00
|
|
|
_X_HIDDEN struct glx_context *__glXcurrentContext = &dummyContext;
|
2008-04-18 15:28:34 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2010-07-28 15:20:41 +01:00
|
|
|
_X_EXPORT 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
|
|
|
}
|
|
|
|
|
2010-07-28 15:20:41 +01:00
|
|
|
_X_EXPORT 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
|
|
|
}
|
|
|
|
|
2009-04-02 10:00:41 +01:00
|
|
|
static void
|
2011-03-10 15:50:52 +00:00
|
|
|
__glXGenerateError(Display * dpy, XID resource,
|
2009-08-12 11:41:22 +01:00
|
|
|
BYTE errorCode, CARD16 minorCode)
|
2009-04-02 10:00:41 +01:00
|
|
|
{
|
|
|
|
xError error;
|
|
|
|
|
|
|
|
error.errorCode = errorCode;
|
|
|
|
error.resourceID = resource;
|
|
|
|
error.sequenceNumber = dpy->request;
|
|
|
|
error.type = X_Error;
|
2011-03-10 15:50:52 +00:00
|
|
|
error.majorCode = __glXSetupForCommand(dpy);
|
2009-04-02 10:00:41 +01:00
|
|
|
error.minorCode = minorCode;
|
|
|
|
_XError(dpy, &error);
|
|
|
|
}
|
2008-04-18 15:28:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a particular context current.
|
2009-08-12 11:41:22 +01:00
|
|
|
*
|
2008-04-18 15:28:34 +01:00
|
|
|
* \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,
|
2010-07-28 16:16:00 +01:00
|
|
|
GLXDrawable read, GLXContext gc_user)
|
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();
|
2010-07-28 20:33:09 +01:00
|
|
|
int ret = Success;
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2011-02-04 01:26:02 +00:00
|
|
|
/* XXX: If this is left out, then libGL ends up not having this
|
|
|
|
* symbol, and drivers using it fail to load. Compare the
|
|
|
|
* implementation of this symbol to _glapi_noop_enable_warnings(),
|
|
|
|
* though, which gets into the library despite no callers, the same
|
|
|
|
* prototypes, and the same compile flags to the files containing
|
|
|
|
* them. Moving the definition to glapi_nop.c gets it into the
|
|
|
|
* library, though.
|
|
|
|
*/
|
|
|
|
(void)_glthread_GetID();
|
|
|
|
|
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;
|
|
|
|
}
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2009-04-02 10:00:41 +01:00
|
|
|
if (gc == NULL && (draw != None || read != None)) {
|
2011-03-10 15:50:52 +00:00
|
|
|
__glXGenerateError(dpy, (draw != None) ? draw : read,
|
2009-08-12 11:41:22 +01:00
|
|
|
BadMatch, X_GLXMakeContextCurrent);
|
2009-04-02 10:00:41 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
if (gc != NULL && (draw == None || read == None)) {
|
2011-03-10 15:50:52 +00:00
|
|
|
__glXGenerateError(dpy, None, BadMatch, X_GLXMakeContextCurrent);
|
2009-04-02 10:00:41 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2008-10-13 13:07:07 +01:00
|
|
|
_glapi_check_multithread();
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2011-02-04 01:26:02 +00:00
|
|
|
__glXLock();
|
2010-09-09 18:18:40 +01:00
|
|
|
if (oldGC == gc &&
|
2011-02-04 01:26:02 +00:00
|
|
|
gc->currentDrawable == draw && gc->currentReadable == read) {
|
|
|
|
__glXUnlock();
|
2010-09-09 18:18:40 +01:00
|
|
|
return True;
|
2011-02-04 01:26:02 +00:00
|
|
|
}
|
2010-09-09 18:18:40 +01:00
|
|
|
|
2010-08-27 17:40:11 +01:00
|
|
|
if (oldGC != &dummyContext) {
|
2011-02-04 01:26:02 +00:00
|
|
|
if (--oldGC->thread_refcount == 0) {
|
|
|
|
oldGC->vtable->unbind(oldGC, gc);
|
|
|
|
oldGC->currentDpy = 0;
|
|
|
|
oldGC->currentDrawable = None;
|
|
|
|
oldGC->currentReadable = None;
|
|
|
|
|
|
|
|
if (oldGC->xid == None && oldGC != gc) {
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
2008-10-13 13:07:07 +01:00
|
|
|
}
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2010-07-28 20:33:09 +01:00
|
|
|
if (gc) {
|
2011-02-04 01:26:02 +00:00
|
|
|
if (gc->thread_refcount++ == 0) {
|
|
|
|
gc->currentDpy = dpy;
|
|
|
|
gc->currentDrawable = draw;
|
|
|
|
gc->currentReadable = read;
|
|
|
|
}
|
2010-07-28 20:33:09 +01:00
|
|
|
__glXSetCurrentContext(gc);
|
2010-08-27 17:40:11 +01:00
|
|
|
ret = gc->vtable->bind(gc, oldGC, draw, read);
|
2010-07-28 20:33:09 +01:00
|
|
|
} else {
|
|
|
|
__glXSetCurrentContextNull();
|
2008-10-13 13:07:07 +01:00
|
|
|
}
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2011-02-04 01:26:02 +00:00
|
|
|
__glXUnlock();
|
2010-09-08 19:51:18 +01:00
|
|
|
|
2010-07-28 20:33:09 +01:00
|
|
|
if (ret) {
|
2011-03-10 15:50:52 +00:00
|
|
|
__glXGenerateError(dpy, None, ret, X_GLXMakeContextCurrent);
|
2010-07-28 20:33:09 +01:00
|
|
|
return GL_FALSE;
|
2008-10-13 13:07:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return GL_TRUE;
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-28 15:20:41 +01:00
|
|
|
_X_EXPORT Bool
|
2008-10-13 13:07:07 +01:00
|
|
|
glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
|
2008-04-18 15:28:34 +01:00
|
|
|
{
|
2009-01-20 21:55:18 +00:00
|
|
|
return MakeContextCurrent(dpy, draw, draw, gc);
|
2008-04-18 15:28:34 +01:00
|
|
|
}
|
|
|
|
|
2010-07-28 15:20:41 +01:00
|
|
|
_X_EXPORT
|
2008-10-13 13:07:07 +01:00
|
|
|
GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
|
|
|
|
(Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
|
2009-01-20 21:55:18 +00:00
|
|
|
(dpy, d, r, ctx), MakeContextCurrent)
|
2008-04-18 15:28:34 +01:00
|
|
|
|
2010-07-28 15:20:41 +01:00
|
|
|
_X_EXPORT
|
2009-01-20 21:55:18 +00:00
|
|
|
GLX_ALIAS(Bool, glXMakeContextCurrent,
|
2009-08-12 11:41:22 +01:00
|
|
|
(Display * dpy, GLXDrawable d, GLXDrawable r,
|
|
|
|
GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent)
|