glx: Lift sending the MakeCurrent request to top-level code
[mesa.git] / src / glx / glxcurrent.c
1 /*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
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:
11 *
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.
16 *
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
23 * SOFTWARE.
24 *
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.
29 */
30
31 /**
32 * \file glxcurrent.c
33 * Client-side GLX interface for current context management.
34 */
35
36 #include <pthread.h>
37
38 #include "glxclient.h"
39 #include "glapi.h"
40 #include "glx_error.h"
41
42 /*
43 ** We setup some dummy structures here so that the API can be used
44 ** even if no context is current.
45 */
46
47 static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
48 static struct glx_context_vtable dummyVtable;
49 /*
50 ** Dummy context used by small commands when there is no current context.
51 ** All the
52 ** gl and glx entry points are designed to operate as nop's when using
53 ** the dummy context structure.
54 */
55 struct glx_context dummyContext = {
56 &dummyBuffer[0],
57 &dummyBuffer[0],
58 &dummyBuffer[0],
59 &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
60 sizeof(dummyBuffer),
61 &dummyVtable
62 };
63
64 /*
65 * Current context management and locking
66 */
67
68 _X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
69
70 # if defined( USE_ELF_TLS )
71
72 /**
73 * Per-thread GLX context pointer.
74 *
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.
78 */
79 __thread void *__glX_tls_Context __attribute__ ((tls_model("initial-exec")))
80 = &dummyContext;
81
82 _X_HIDDEN void
83 __glXSetCurrentContext(struct glx_context * c)
84 {
85 __glX_tls_Context = (c != NULL) ? c : &dummyContext;
86 }
87
88 # else
89
90 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
91
92 /**
93 * Per-thread data key.
94 *
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.
98 */
99 static pthread_key_t ContextTSD;
100
101 /**
102 * Initialize the per-thread data key.
103 *
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.
107 */
108 static void
109 init_thread_data(void)
110 {
111 if (pthread_key_create(&ContextTSD, NULL) != 0) {
112 perror("pthread_key_create");
113 exit(-1);
114 }
115 }
116
117 _X_HIDDEN void
118 __glXSetCurrentContext(struct glx_context * c)
119 {
120 pthread_once(&once_control, init_thread_data);
121 pthread_setspecific(ContextTSD, c);
122 }
123
124 _X_HIDDEN struct glx_context *
125 __glXGetCurrentContext(void)
126 {
127 void *v;
128
129 pthread_once(&once_control, init_thread_data);
130
131 v = pthread_getspecific(ContextTSD);
132 return (v == NULL) ? &dummyContext : (struct glx_context *) v;
133 }
134
135 # endif /* defined( USE_ELF_TLS ) */
136
137
138 _X_HIDDEN void
139 __glXSetCurrentContextNull(void)
140 {
141 __glXSetCurrentContext(&dummyContext);
142 #if defined(GLX_DIRECT_RENDERING)
143 _glapi_set_dispatch(NULL); /* no-op functions */
144 _glapi_set_context(NULL);
145 #endif
146 }
147
148 _GLX_PUBLIC GLXContext
149 glXGetCurrentContext(void)
150 {
151 struct glx_context *cx = __glXGetCurrentContext();
152
153 if (cx == &dummyContext) {
154 return NULL;
155 }
156 else {
157 return (GLXContext) cx;
158 }
159 }
160
161 _GLX_PUBLIC GLXDrawable
162 glXGetCurrentDrawable(void)
163 {
164 struct glx_context *gc = __glXGetCurrentContext();
165 return gc->currentDrawable;
166 }
167
168 static Bool
169 SendMakeCurrentRequest(Display * dpy, GLXContextID gc_id,
170 GLXContextTag gc_tag, GLXDrawable draw,
171 GLXDrawable read, GLXContextTag *out_tag)
172 {
173 xGLXMakeCurrentReply reply;
174 Bool ret;
175 int opcode = __glXSetupForCommand(dpy);
176
177 LockDisplay(dpy);
178
179 if (draw == read) {
180 xGLXMakeCurrentReq *req;
181
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;
188 }
189 else {
190 struct glx_display *priv = __glXInitialize(dpy);
191
192 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
193 xGLXMakeContextCurrentReq *req;
194
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;
202 }
203 else {
204 xGLXVendorPrivateWithReplyReq *vpreq;
205 xGLXMakeCurrentReadSGIReq *req;
206
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;
218 }
219 }
220
221 ret = _XReply(dpy, (xReply *) &reply, 0, False);
222
223
224 if (ret == 1)
225 *out_tag = reply.contextTag;
226
227 UnlockDisplay(dpy);
228 SyncHandle();
229
230 return ret;
231 }
232
233 static void
234 SetGC(struct glx_context *gc, Display *dpy, GLXDrawable draw, GLXDrawable read)
235 {
236 gc->currentDpy = dpy;
237 gc->currentDrawable = draw;
238 gc->currentReadable = read;
239 }
240
241 static Bool
242 should_send(Display *dpy, struct glx_context *gc)
243 {
244 /* always send for indirect contexts */
245 if (!gc->isDirect)
246 return 1;
247
248 /* don't send for broken servers. */
249 if (VendorRelease(dpy) < 12006000 || VendorRelease(dpy) >= 40000000)
250 return 0;
251
252 return 1;
253 }
254
255 static Bool
256 MakeContextCurrent(Display * dpy, GLXDrawable draw,
257 GLXDrawable read, GLXContext gc_user)
258 {
259 struct glx_context *gc = (struct glx_context *) gc_user;
260 struct glx_context *oldGC = __glXGetCurrentContext();
261 Bool ret = GL_FALSE;
262
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
265 * context.
266 */
267 if ((gc != NULL) && (gc->xid == None)) {
268 return GL_FALSE;
269 }
270
271 _glapi_check_multithread();
272 __glXLock();
273
274 if (oldGC == gc &&
275 gc->currentDrawable == draw &&
276 gc->currentReadable == read) {
277 /* Same context and drawables: no op, just return */
278 ret = GL_TRUE;
279 }
280
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)) {
286 goto out;
287 }
288 }
289
290 if (gc->vtable->bind(gc, gc, draw, read) != Success) {
291 __glXSetCurrentContextNull();
292 goto out;
293 }
294 }
295
296 else {
297 /* Different contexts: release the old, bind the new */
298 GLXContextTag oldTag = oldGC->currentContextTag;
299
300 if (oldGC != &dummyContext) {
301
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)) {
307 goto out;
308 }
309
310 oldGC->vtable->unbind(oldGC, gc);
311
312 if (oldGC->xid == None) {
313 /* destroyed context, free it */
314 oldGC->vtable->destroy(oldGC);
315 oldTag = 0;
316 } else {
317 SetGC(oldGC, NULL, None, None);
318 oldTag = oldGC->currentContextTag;
319 }
320 }
321 }
322 __glXSetCurrentContextNull();
323
324 if (gc) {
325 /*
326 * MESA_multithread_makecurrent makes this complicated. We need to
327 * send the request if the new context is
328 *
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
332 */
333 Bool new_drawables = gc->currentReadable != read ||
334 gc->currentDrawable != draw;
335
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)) {
340 goto out;
341 }
342 }
343 }
344
345 if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
346 __glXSendError(dpy, GLXBadContext, None, X_GLXMakeContextCurrent,
347 False);
348 goto out;
349 }
350
351 if (gc->thread_refcount == 0) {
352 SetGC(gc, dpy, draw, read);
353 }
354 gc->thread_refcount++;
355 __glXSetCurrentContext(gc);
356 }
357 }
358 ret = GL_TRUE;
359
360 out:
361 __glXUnlock();
362 return ret;
363 }
364
365
366 _GLX_PUBLIC Bool
367 glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
368 {
369 return MakeContextCurrent(dpy, draw, draw, gc);
370 }
371
372 _GLX_PUBLIC
373 GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
374 (Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
375 (dpy, d, r, ctx), MakeContextCurrent)
376
377 _GLX_PUBLIC
378 GLX_ALIAS(Bool, glXMakeContextCurrent,
379 (Display * dpy, GLXDrawable d, GLXDrawable r,
380 GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent)