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.
38 #include "glxclient.h"
40 #include "glx_error.h"
43 ** We setup some dummy structures here so that the API can be used
44 ** even if no context is current.
47 static GLubyte dummyBuffer
[__GLX_BUFFER_LIMIT_SIZE
];
48 static struct glx_context_vtable dummyVtable
;
50 ** Dummy context used by small commands when there is no current context.
52 ** gl and glx entry points are designed to operate as nop's when using
53 ** the dummy context structure.
55 struct glx_context dummyContext
= {
59 &dummyBuffer
[__GLX_BUFFER_LIMIT_SIZE
],
65 * Current context management and locking
68 _X_HIDDEN pthread_mutex_t __glXmutex
= PTHREAD_MUTEX_INITIALIZER
;
70 # if defined( USE_ELF_TLS )
73 * Per-thread GLX context pointer.
75 * \c __glXSetCurrentContext is written is such a way that this pointer can
76 * \b never be \c NULL. This is important! Because of this
77 * \c __glXGetCurrentContext can be implemented as trivial macro.
79 __thread
void *__glX_tls_Context
__attribute__ ((tls_model("initial-exec")))
83 __glXSetCurrentContext(struct glx_context
* c
)
85 __glX_tls_Context
= (c
!= NULL
) ? c
: &dummyContext
;
90 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
93 * Per-thread data key.
95 * Once \c init_thread_data has been called, the per-thread data key will
96 * take a value of \c NULL. As each new thread is created the default
97 * value, in that thread, will be \c NULL.
99 static pthread_key_t ContextTSD
;
102 * Initialize the per-thread data key.
104 * This function is called \b exactly once per-process (not per-thread!) to
105 * initialize the per-thread data key. This is ideally done using the
106 * \c pthread_once mechanism.
109 init_thread_data(void)
111 if (pthread_key_create(&ContextTSD
, NULL
) != 0) {
112 perror("pthread_key_create");
118 __glXSetCurrentContext(struct glx_context
* c
)
120 pthread_once(&once_control
, init_thread_data
);
121 pthread_setspecific(ContextTSD
, c
);
124 _X_HIDDEN
struct glx_context
*
125 __glXGetCurrentContext(void)
129 pthread_once(&once_control
, init_thread_data
);
131 v
= pthread_getspecific(ContextTSD
);
132 return (v
== NULL
) ? &dummyContext
: (struct glx_context
*) v
;
135 # endif /* defined( USE_ELF_TLS ) */
139 __glXSetCurrentContextNull(void)
141 __glXSetCurrentContext(&dummyContext
);
142 #if defined(GLX_DIRECT_RENDERING)
143 _glapi_set_dispatch(NULL
); /* no-op functions */
144 _glapi_set_context(NULL
);
148 _GLX_PUBLIC GLXContext
149 glXGetCurrentContext(void)
151 struct glx_context
*cx
= __glXGetCurrentContext();
153 if (cx
== &dummyContext
) {
157 return (GLXContext
) cx
;
161 _GLX_PUBLIC GLXDrawable
162 glXGetCurrentDrawable(void)
164 struct glx_context
*gc
= __glXGetCurrentContext();
165 return gc
->currentDrawable
;
169 SendMakeCurrentRequest(Display
* dpy
, GLXContextID gc_id
,
170 GLXContextTag gc_tag
, GLXDrawable draw
,
171 GLXDrawable read
, GLXContextTag
*out_tag
)
173 xGLXMakeCurrentReply reply
;
175 int opcode
= __glXSetupForCommand(dpy
);
180 xGLXMakeCurrentReq
*req
;
182 GetReq(GLXMakeCurrent
, req
);
183 req
->reqType
= opcode
;
184 req
->glxCode
= X_GLXMakeCurrent
;
185 req
->drawable
= draw
;
186 req
->context
= gc_id
;
187 req
->oldContextTag
= gc_tag
;
190 struct glx_display
*priv
= __glXInitialize(dpy
);
192 if ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3)) {
193 xGLXMakeContextCurrentReq
*req
;
195 GetReq(GLXMakeContextCurrent
, req
);
196 req
->reqType
= opcode
;
197 req
->glxCode
= X_GLXMakeContextCurrent
;
198 req
->drawable
= draw
;
199 req
->readdrawable
= read
;
200 req
->context
= gc_id
;
201 req
->oldContextTag
= gc_tag
;
204 xGLXVendorPrivateWithReplyReq
*vpreq
;
205 xGLXMakeCurrentReadSGIReq
*req
;
207 GetReqExtra(GLXVendorPrivateWithReply
,
208 sz_xGLXMakeCurrentReadSGIReq
-
209 sz_xGLXVendorPrivateWithReplyReq
, vpreq
);
210 req
= (xGLXMakeCurrentReadSGIReq
*) vpreq
;
211 req
->reqType
= opcode
;
212 req
->glxCode
= X_GLXVendorPrivateWithReply
;
213 req
->vendorCode
= X_GLXvop_MakeCurrentReadSGI
;
214 req
->drawable
= draw
;
215 req
->readable
= read
;
216 req
->context
= gc_id
;
217 req
->oldContextTag
= gc_tag
;
221 ret
= _XReply(dpy
, (xReply
*) &reply
, 0, False
);
225 *out_tag
= reply
.contextTag
;
234 SetGC(struct glx_context
*gc
, Display
*dpy
, GLXDrawable draw
, GLXDrawable read
)
236 gc
->currentDpy
= dpy
;
237 gc
->currentDrawable
= draw
;
238 gc
->currentReadable
= read
;
242 should_send(Display
*dpy
, struct glx_context
*gc
)
244 /* always send for indirect contexts */
248 /* don't send for broken servers. */
249 if (VendorRelease(dpy
) < 12006000 || VendorRelease(dpy
) >= 40000000)
256 MakeContextCurrent(Display
* dpy
, GLXDrawable draw
,
257 GLXDrawable read
, GLXContext gc_user
)
259 struct glx_context
*gc
= (struct glx_context
*) gc_user
;
260 struct glx_context
*oldGC
= __glXGetCurrentContext();
263 /* Make sure that the new context has a nonzero ID. In the request,
264 * a zero context ID is used only to mean that we bind to no current
267 if ((gc
!= NULL
) && (gc
->xid
== None
)) {
271 _glapi_check_multithread();
275 gc
->currentDrawable
== draw
&&
276 gc
->currentReadable
== read
) {
277 /* Same context and drawables: no op, just return */
281 else if (oldGC
== gc
) {
282 /* Same context and new drawables: update drawable bindings */
283 if (should_send(dpy
, gc
)) {
284 if (!SendMakeCurrentRequest(dpy
, gc
->xid
, gc
->currentContextTag
,
285 draw
, read
, &gc
->currentContextTag
)) {
290 if (gc
->vtable
->bind(gc
, gc
, draw
, read
) != Success
) {
291 __glXSetCurrentContextNull();
297 /* Different contexts: release the old, bind the new */
298 GLXContextTag oldTag
= oldGC
->currentContextTag
;
300 if (oldGC
!= &dummyContext
) {
302 if (--oldGC
->thread_refcount
== 0) {
303 if (oldGC
->xid
!= None
&&
304 should_send(dpy
, oldGC
) &&
305 !SendMakeCurrentRequest(dpy
, None
, oldTag
, None
, None
,
306 &oldGC
->currentContextTag
)) {
310 oldGC
->vtable
->unbind(oldGC
, gc
);
312 if (oldGC
->xid
== None
) {
313 /* destroyed context, free it */
314 oldGC
->vtable
->destroy(oldGC
);
317 SetGC(oldGC
, NULL
, None
, None
);
318 oldTag
= oldGC
->currentContextTag
;
322 __glXSetCurrentContextNull();
326 * MESA_multithread_makecurrent makes this complicated. We need to
327 * send the request if the new context is
329 * a) indirect (may be current to another client), or
330 * b) (direct and) newly being made current, or
331 * c) (direct and) being bound to new drawables
333 Bool new_drawables
= gc
->currentReadable
!= read
||
334 gc
->currentDrawable
!= draw
;
336 if (should_send(dpy
, gc
)) {
337 if (!gc
->isDirect
|| !gc
->thread_refcount
|| new_drawables
) {
338 if (!SendMakeCurrentRequest(dpy
, gc
->xid
, oldTag
, draw
, read
,
339 &gc
->currentContextTag
)) {
345 if (gc
->vtable
->bind(gc
, oldGC
, draw
, read
) != Success
) {
346 __glXSendError(dpy
, GLXBadContext
, None
, X_GLXMakeContextCurrent
,
351 if (gc
->thread_refcount
== 0) {
352 SetGC(gc
, dpy
, draw
, read
);
354 gc
->thread_refcount
++;
355 __glXSetCurrentContext(gc
);
367 glXMakeCurrent(Display
* dpy
, GLXDrawable draw
, GLXContext gc
)
369 return MakeContextCurrent(dpy
, draw
, draw
, gc
);
373 GLX_ALIAS(Bool
, glXMakeCurrentReadSGI
,
374 (Display
* dpy
, GLXDrawable d
, GLXDrawable r
, GLXContext ctx
),
375 (dpy
, d
, r
, ctx
), MakeContextCurrent
)
378 GLX_ALIAS(Bool
, glXMakeContextCurrent
,
379 (Display
* dpy
, GLXDrawable d
, GLXDrawable r
,
380 GLXContext ctx
), (dpy
, d
, r
, ctx
), MakeContextCurrent
)