#include "glxclient.h"
#include "glapi.h"
#include "glheader.h"
#include "indirect_init.h"
#ifdef GLX_DIRECT_RENDERING
#include "xf86dri.h"
#endif
static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
static __GLXcontext dummyContext = {
&dummyBuffer[0],
&dummyBuffer[0],
&dummyBuffer[0],
&dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
sizeof(dummyBuffer),
};
static __GLapi *IndirectAPI = NULL;
#if defined( USE_XTHREADS )
static GLboolean TSDinitialized = GL_FALSE;
static xthread_key_t ContextTSD;
_X_HIDDEN __GLXcontext *__glXGetCurrentContext(void)
{
if (!TSDinitialized) {
xthread_key_create(&ContextTSD, NULL);
TSDinitialized = GL_TRUE;
return &dummyContext;
}
else {
void *p;
xthread_get_specific(ContextTSD, &p);
if (!p)
return &dummyContext;
else
return (__GLXcontext *) p;
}
}
_X_HIDDEN void __glXSetCurrentContext(__GLXcontext *c)
{
if (!TSDinitialized) {
xthread_key_create(&ContextTSD, NULL);
TSDinitialized = GL_TRUE;
}
xthread_set_specific(ContextTSD, c);
}
_X_HIDDEN xmutex_rec __glXmutex;
#elif defined( PTHREADS )
_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
# if defined( GLX_USE_TLS )
__thread void * __glX_tls_Context __attribute__((tls_model("initial-exec")))
= &dummyContext;
_X_HIDDEN void __glXSetCurrentContext( __GLXcontext * c )
{
__glX_tls_Context = (c != NULL) ? c : &dummyContext;
}
# else
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
static pthread_key_t ContextTSD;
static void init_thread_data( void )
{
if ( pthread_key_create( & ContextTSD, NULL ) != 0 ) {
perror( "pthread_key_create" );
exit( -1 );
}
}
_X_HIDDEN void __glXSetCurrentContext( __GLXcontext * c )
{
pthread_once( & once_control, init_thread_data );
pthread_setspecific( ContextTSD, c );
}
_X_HIDDEN __GLXcontext * __glXGetCurrentContext( void )
{
void * v;
pthread_once( & once_control, init_thread_data );
v = pthread_getspecific( ContextTSD );
return (v == NULL) ? & dummyContext : (__GLXcontext *) v;
}
# endif
#elif defined( THREADS )
#error Unknown threading method specified.
#else
_X_HIDDEN __GLXcontext *__glXcurrentContext = &dummyContext;
#endif
_X_HIDDEN void __glXSetCurrentContextNull(void)
{
__glXSetCurrentContext(&dummyContext);
#ifdef GLX_DIRECT_RENDERING
_glapi_set_dispatch(NULL);
#endif
}
PUBLIC GLXContext glXGetCurrentContext(void)
{
GLXContext cx = __glXGetCurrentContext();
if (cx == &dummyContext) {
return NULL;
} else {
return cx;
}
}
PUBLIC GLXDrawable glXGetCurrentDrawable(void)
{
GLXContext gc = __glXGetCurrentContext();
return gc->currentDrawable;
}
static Bool SendMakeCurrentRequest(Display *dpy, CARD8 opcode,
GLXContextID gc_id, GLXContextTag gc_tag,
GLXDrawable draw, GLXDrawable read,
xGLXMakeCurrentReply *reply)
{
Bool ret;
LockDisplay(dpy);
if (draw == read) {
xGLXMakeCurrentReq *req;
GetReq(GLXMakeCurrent,req);
req->reqType = opcode;
req->glxCode = X_GLXMakeCurrent;
req->drawable = draw;
req->context = gc_id;
req->oldContextTag = gc_tag;
}
else {
__GLXdisplayPrivate *priv = __glXInitialize(dpy);
if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
xGLXMakeContextCurrentReq *req;
GetReq(GLXMakeContextCurrent,req);
req->reqType = opcode;
req->glxCode = X_GLXMakeContextCurrent;
req->drawable = draw;
req->readdrawable = read;
req->context = gc_id;
req->oldContextTag = gc_tag;
}
else {
xGLXVendorPrivateWithReplyReq *vpreq;
xGLXMakeCurrentReadSGIReq *req;
GetReqExtra(GLXVendorPrivateWithReply,
sz_xGLXMakeCurrentReadSGIReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
req = (xGLXMakeCurrentReadSGIReq *)vpreq;
req->reqType = opcode;
req->glxCode = X_GLXVendorPrivateWithReply;
req->vendorCode = X_GLXvop_MakeCurrentReadSGI;
req->drawable = draw;
req->readable = read;
req->context = gc_id;
req->oldContextTag = gc_tag;
}
}
ret = _XReply(dpy, (xReply*) reply, 0, False);
UnlockDisplay(dpy);
SyncHandle();
return ret;
}
#ifdef GLX_DIRECT_RENDERING
static __GLXDRIdrawable *
FetchDRIDrawable(Display *dpy,
GLXDrawable glxDrawable, GLXContext gc, Bool pre13)
{
__GLXdisplayPrivate * const priv = __glXInitialize(dpy);
__GLXDRIdrawable *pdraw;
__GLXscreenConfigs *psc;
XID drawable;
if (priv == NULL)
return NULL;
psc = &priv->screenConfigs[gc->screen];
if (psc->drawHash == NULL)
return NULL;
if (__glxHashLookup(psc->drawHash, glxDrawable, (void *) &pdraw) == 0)
return pdraw;
if (pre13)
drawable = glxDrawable;
else
drawable = None;
pdraw = psc->driScreen->createDrawable(psc, drawable,
glxDrawable, gc->mode);
if (__glxHashInsert(psc->drawHash, glxDrawable, pdraw)) {
(*pdraw->destroyDrawable)(pdraw);
return NULL;
}
return pdraw;
}
#endif
static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
GLXDrawable read, GLXContext gc,
Bool pre13)
{
xGLXMakeCurrentReply reply;
const GLXContext oldGC = __glXGetCurrentContext();
const CARD8 opcode = __glXSetupForCommand(dpy);
const CARD8 oldOpcode = ((gc == oldGC) || (oldGC == &dummyContext))
? opcode : __glXSetupForCommand(oldGC->currentDpy);
Bool bindReturnValue;
if (!opcode || !oldOpcode) {
return GL_FALSE;
}
if ((gc != NULL) && (gc->xid == None)) {
return GL_FALSE;
}
_glapi_check_multithread();
#ifdef GLX_DIRECT_RENDERING
if (gc && gc->driContext) {
__GLXDRIdrawable *pdraw = FetchDRIDrawable(dpy, draw, gc, pre13);
__GLXDRIdrawable *pread = FetchDRIDrawable(dpy, read, gc, pre13);
bindReturnValue =
(gc->driContext->bindContext) (gc->driContext, pdraw, pread);
} else
#endif
{
bindReturnValue =
SendMakeCurrentRequest(dpy, opcode, gc ? gc->xid : None,
((dpy != oldGC->currentDpy) || oldGC->isDirect)
? None : oldGC->currentContextTag,
draw, read, &reply);
}
if (!bindReturnValue) {
return False;
}
#ifdef GLX_DIRECT_RENDERING
if ((dpy != oldGC->currentDpy || (gc && gc->driContext)) &&
!oldGC->isDirect && oldGC != &dummyContext) {
#else
if ((dpy != oldGC->currentDpy) && oldGC != &dummyContext) {
#endif
xGLXMakeCurrentReply dummy_reply;
(void) SendMakeCurrentRequest(oldGC->currentDpy, oldOpcode, None,
oldGC->currentContextTag, None, None,
& dummy_reply);
}
#ifdef GLX_DIRECT_RENDERING
else if (oldGC->driContext) {
oldGC->driContext->unbindContext(oldGC->driContext);
}
#endif
__glXLock();
if (gc == oldGC) {
gc->currentDrawable = draw;
gc->currentReadable = read;
} else {
if (oldGC != &dummyContext) {
oldGC->currentDpy = 0;
oldGC->currentDrawable = None;
oldGC->currentReadable = None;
oldGC->currentContextTag = 0;
if (oldGC->xid == None) {
#ifdef GLX_DIRECT_RENDERING
if (oldGC->driContext) {
oldGC->driContext->destroyContext(oldGC->driContext,
oldGC->psc,
oldGC->createDpy);
oldGC->driContext = NULL;
}
#endif
__glXFreeContext(oldGC);
}
}
if (gc) {
__glXSetCurrentContext(gc);
gc->currentDpy = dpy;
gc->currentDrawable = draw;
gc->currentReadable = read;
#ifdef GLX_DIRECT_RENDERING
if (!gc->driContext) {
#endif
if (!IndirectAPI)
IndirectAPI = __glXNewIndirectAPI();
_glapi_set_dispatch(IndirectAPI);
#ifdef GLX_USE_APPLEGL
do {
extern void XAppleDRIUseIndirectDispatch(void);
XAppleDRIUseIndirectDispatch();
} while (0);
#endif
__GLXattribute *state =
(__GLXattribute *)(gc->client_state_private);
gc->currentContextTag = reply.contextTag;
if (state->array_state == NULL) {
(void) glGetString(GL_EXTENSIONS);
(void) glGetString(GL_VERSION);
__glXInitVertexArrayState(gc);
}
#ifdef GLX_DIRECT_RENDERING
}
else {
gc->currentContextTag = -1;
}
#endif
} else {
__glXSetCurrentContextNull();
}
}
__glXUnlock();
return GL_TRUE;
}
PUBLIC Bool glXMakeCurrent(Display *dpy, GLXDrawable draw, GLXContext gc)
{
return MakeContextCurrent(dpy, draw, draw, gc, True);
}
PUBLIC GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
(Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
(dpy, d, r, ctx, False), MakeContextCurrent)
PUBLIC GLX_ALIAS(Bool, glXMakeContextCurrent,
(Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
(dpy, d, r, ctx, False), MakeContextCurrent)