2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
33 * Client-side GLX interface for current context management.
40 #include "glxclient.h"
45 ** We setup some dummy structures here so that the API can be used
46 ** even if no context is current.
49 static GLubyte dummyBuffer
[__GLX_BUFFER_LIMIT_SIZE
];
50 static struct glx_context_vtable dummyVtable
;
52 ** Dummy context used by small commands when there is no current context.
54 ** gl and glx entry points are designed to operate as nop's when using
55 ** the dummy context structure.
57 struct glx_context dummyContext
= {
61 &dummyBuffer
[__GLX_BUFFER_LIMIT_SIZE
],
67 * Current context management and locking
70 #if defined( HAVE_PTHREAD )
72 _X_HIDDEN pthread_mutex_t __glXmutex
= PTHREAD_MUTEX_INITIALIZER
;
74 # if defined( GLX_USE_TLS )
77 * Per-thread GLX context pointer.
79 * \c __glXSetCurrentContext is written is such a way that this pointer can
80 * \b never be \c NULL. This is important! Because of this
81 * \c __glXGetCurrentContext can be implemented as trivial macro.
83 __thread
void *__glX_tls_Context
__attribute__ ((tls_model("initial-exec")))
87 __glXSetCurrentContext(struct glx_context
* c
)
89 __glX_tls_Context
= (c
!= NULL
) ? c
: &dummyContext
;
94 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
97 * Per-thread data key.
99 * Once \c init_thread_data has been called, the per-thread data key will
100 * take a value of \c NULL. As each new thread is created the default
101 * value, in that thread, will be \c NULL.
103 static pthread_key_t ContextTSD
;
106 * Initialize the per-thread data key.
108 * This function is called \b exactly once per-process (not per-thread!) to
109 * initialize the per-thread data key. This is ideally done using the
110 * \c pthread_once mechanism.
113 init_thread_data(void)
115 if (pthread_key_create(&ContextTSD
, NULL
) != 0) {
116 perror("pthread_key_create");
122 __glXSetCurrentContext(struct glx_context
* c
)
124 pthread_once(&once_control
, init_thread_data
);
125 pthread_setspecific(ContextTSD
, c
);
128 _X_HIDDEN
struct glx_context
*
129 __glXGetCurrentContext(void)
133 pthread_once(&once_control
, init_thread_data
);
135 v
= pthread_getspecific(ContextTSD
);
136 return (v
== NULL
) ? &dummyContext
: (struct glx_context
*) v
;
139 # endif /* defined( GLX_USE_TLS ) */
143 /* not thread safe */
144 _X_HIDDEN
struct glx_context
*__glXcurrentContext
= &dummyContext
;
150 __glXSetCurrentContextNull(void)
152 __glXSetCurrentContext(&dummyContext
);
153 #if defined(GLX_DIRECT_RENDERING)
154 _glapi_set_dispatch(NULL
); /* no-op functions */
155 _glapi_set_context(NULL
);
160 glXGetCurrentContext(void)
162 struct glx_context
*cx
= __glXGetCurrentContext();
164 if (cx
== &dummyContext
) {
168 return (GLXContext
) cx
;
172 _X_EXPORT GLXDrawable
173 glXGetCurrentDrawable(void)
175 struct glx_context
*gc
= __glXGetCurrentContext();
176 return gc
->currentDrawable
;
180 __glXGenerateError(Display
* dpy
, XID resource
,
181 BYTE errorCode
, CARD16 minorCode
)
185 error
.errorCode
= errorCode
;
186 error
.resourceID
= resource
;
187 error
.sequenceNumber
= dpy
->request
;
188 error
.type
= X_Error
;
189 error
.majorCode
= __glXSetupForCommand(dpy
);
190 error
.minorCode
= minorCode
;
191 _XError(dpy
, &error
);
195 * Make a particular context current.
197 * \note This is in this file so that it can access dummyContext.
200 MakeContextCurrent(Display
* dpy
, GLXDrawable draw
,
201 GLXDrawable read
, GLXContext gc_user
)
203 struct glx_context
*gc
= (struct glx_context
*) gc_user
;
204 struct glx_context
*oldGC
= __glXGetCurrentContext();
206 /* Make sure that the new context has a nonzero ID. In the request,
207 * a zero context ID is used only to mean that we bind to no current
210 if ((gc
!= NULL
) && (gc
->xid
== None
)) {
214 _glapi_check_multithread();
218 gc
->currentDrawable
== draw
&& gc
->currentReadable
== read
) {
223 if (oldGC
!= &dummyContext
) {
224 if (--oldGC
->thread_refcount
== 0) {
225 oldGC
->vtable
->unbind(oldGC
, gc
);
226 oldGC
->currentDpy
= 0;
231 /* Attempt to bind the context. We do this before mucking with
232 * gc and __glXSetCurrentContext to properly handle our state in
235 * If an error occurs, set the Null context since we've already
236 * blown away our old context. The caller is responsible for
237 * figuring out how to handle setting a valid context.
239 if (gc
->vtable
->bind(gc
, oldGC
, draw
, read
) != Success
) {
240 __glXSetCurrentContextNull();
242 __glXGenerateError(dpy
, None
, GLXBadContext
, X_GLXMakeContextCurrent
);
246 if (gc
->thread_refcount
== 0) {
247 gc
->currentDpy
= dpy
;
248 gc
->currentDrawable
= draw
;
249 gc
->currentReadable
= read
;
251 gc
->thread_refcount
++;
252 __glXSetCurrentContext(gc
);
254 __glXSetCurrentContextNull();
257 if (oldGC
->thread_refcount
== 0 && oldGC
!= &dummyContext
&& oldGC
->xid
== None
) {
258 /* We are switching away from a context that was
259 * previously destroyed, so we need to free the memory
260 * for the old handle. */
261 oldGC
->vtable
->destroy(oldGC
);
271 glXMakeCurrent(Display
* dpy
, GLXDrawable draw
, GLXContext gc
)
273 return MakeContextCurrent(dpy
, draw
, draw
, gc
);
277 GLX_ALIAS(Bool
, glXMakeCurrentReadSGI
,
278 (Display
* dpy
, GLXDrawable d
, GLXDrawable r
, GLXContext ctx
),
279 (dpy
, d
, r
, ctx
), MakeContextCurrent
)
282 GLX_ALIAS(Bool
, glXMakeContextCurrent
,
283 (Display
* dpy
, GLXDrawable d
, GLXDrawable r
,
284 GLXContext ctx
), (dpy
, d
, r
, ctx
), MakeContextCurrent
)