glx: Split indirect and applegl implementations into different files
[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 #ifdef PTHREADS
37 #include <pthread.h>
38 #endif
39
40 #include "glxclient.h"
41 #ifdef GLX_USE_APPLEGL
42 #include <stdlib.h>
43
44 #include "apple_glx.h"
45 #include "apple_glx_context.h"
46 #else
47 #include "glapi.h"
48 #endif
49
50 /*
51 ** We setup some dummy structures here so that the API can be used
52 ** even if no context is current.
53 */
54
55 static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
56
57 /*
58 ** Dummy context used by small commands when there is no current context.
59 ** All the
60 ** gl and glx entry points are designed to operate as nop's when using
61 ** the dummy context structure.
62 */
63 static struct glx_context dummyContext = {
64 &dummyBuffer[0],
65 &dummyBuffer[0],
66 &dummyBuffer[0],
67 &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
68 sizeof(dummyBuffer),
69 };
70
71 /*
72 * Current context management and locking
73 */
74
75 #if defined( PTHREADS )
76
77 _X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
78
79 # if defined( GLX_USE_TLS )
80
81 /**
82 * Per-thread GLX context pointer.
83 *
84 * \c __glXSetCurrentContext is written is such a way that this pointer can
85 * \b never be \c NULL. This is important! Because of this
86 * \c __glXGetCurrentContext can be implemented as trivial macro.
87 */
88 __thread void *__glX_tls_Context __attribute__ ((tls_model("initial-exec")))
89 = &dummyContext;
90
91 _X_HIDDEN void
92 __glXSetCurrentContext(struct glx_context * c)
93 {
94 __glX_tls_Context = (c != NULL) ? c : &dummyContext;
95 }
96
97 # else
98
99 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
100
101 /**
102 * Per-thread data key.
103 *
104 * Once \c init_thread_data has been called, the per-thread data key will
105 * take a value of \c NULL. As each new thread is created the default
106 * value, in that thread, will be \c NULL.
107 */
108 static pthread_key_t ContextTSD;
109
110 /**
111 * Initialize the per-thread data key.
112 *
113 * This function is called \b exactly once per-process (not per-thread!) to
114 * initialize the per-thread data key. This is ideally done using the
115 * \c pthread_once mechanism.
116 */
117 static void
118 init_thread_data(void)
119 {
120 if (pthread_key_create(&ContextTSD, NULL) != 0) {
121 perror("pthread_key_create");
122 exit(-1);
123 }
124 }
125
126 _X_HIDDEN void
127 __glXSetCurrentContext(struct glx_context * c)
128 {
129 pthread_once(&once_control, init_thread_data);
130 pthread_setspecific(ContextTSD, c);
131 }
132
133 _X_HIDDEN struct glx_context *
134 __glXGetCurrentContext(void)
135 {
136 void *v;
137
138 pthread_once(&once_control, init_thread_data);
139
140 v = pthread_getspecific(ContextTSD);
141 return (v == NULL) ? &dummyContext : (struct glx_context *) v;
142 }
143
144 # endif /* defined( GLX_USE_TLS ) */
145
146 #elif defined( THREADS )
147
148 #error Unknown threading method specified.
149
150 #else
151
152 /* not thread safe */
153 _X_HIDDEN struct glx_context *__glXcurrentContext = &dummyContext;
154
155 #endif
156
157
158 _X_HIDDEN void
159 __glXSetCurrentContextNull(void)
160 {
161 __glXSetCurrentContext(&dummyContext);
162 #ifndef GLX_USE_APPLEGL
163 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
164 _glapi_set_dispatch(NULL); /* no-op functions */
165 _glapi_set_context(NULL);
166 #endif
167 #endif
168 }
169
170 _X_EXPORT GLXContext
171 glXGetCurrentContext(void)
172 {
173 struct glx_context *cx = __glXGetCurrentContext();
174
175 if (cx == &dummyContext) {
176 return NULL;
177 }
178 else {
179 return (GLXContext) cx;
180 }
181 }
182
183 _X_EXPORT GLXDrawable
184 glXGetCurrentDrawable(void)
185 {
186 struct glx_context *gc = __glXGetCurrentContext();
187 return gc->currentDrawable;
188 }
189
190 static void
191 __glXGenerateError(Display * dpy, struct glx_context *gc, XID resource,
192 BYTE errorCode, CARD16 minorCode)
193 {
194 xError error;
195
196 error.errorCode = errorCode;
197 error.resourceID = resource;
198 error.sequenceNumber = dpy->request;
199 error.type = X_Error;
200 error.majorCode = gc->majorOpcode;
201 error.minorCode = minorCode;
202 _XError(dpy, &error);
203 }
204
205 /**
206 * Make a particular context current.
207 *
208 * \note This is in this file so that it can access dummyContext.
209 */
210 static Bool
211 MakeContextCurrent(Display * dpy, GLXDrawable draw,
212 GLXDrawable read, GLXContext gc_user)
213 {
214 struct glx_context *gc = (struct glx_context *) gc_user;
215 struct glx_context *oldGC = __glXGetCurrentContext();
216 int ret = Success;
217
218 /* Make sure that the new context has a nonzero ID. In the request,
219 * a zero context ID is used only to mean that we bind to no current
220 * context.
221 */
222 if ((gc != NULL) && (gc->xid == None)) {
223 return GL_FALSE;
224 }
225
226 if (gc == NULL && (draw != None || read != None)) {
227 __glXGenerateError(dpy, gc, (draw != None) ? draw : read,
228 BadMatch, X_GLXMakeContextCurrent);
229 return False;
230 }
231 if (gc != NULL && (draw == None || read == None)) {
232 __glXGenerateError(dpy, gc, None, BadMatch, X_GLXMakeContextCurrent);
233 return False;
234 }
235
236 _glapi_check_multithread();
237
238 if (gc != NULL && gc->thread_id != 0 && gc->thread_id != _glthread_GetID()) {
239 __glXGenerateError(dpy, gc, gc->xid,
240 BadAccess, X_GLXMakeContextCurrent);
241 return False;
242 }
243
244 if (oldGC != &dummyContext && oldGC != gc) {
245 oldGC->vtable->unbind(oldGC, gc);
246 oldGC->currentDpy = 0;
247 oldGC->currentDrawable = None;
248 oldGC->currentReadable = None;
249 oldGC->thread_id = 0;
250 if (oldGC->xid == None)
251 /* We are switching away from a context that was
252 * previously destroyed, so we need to free the memory
253 * for the old handle.
254 */
255 oldGC->vtable->destroy(oldGC);
256 }
257
258 if (gc) {
259 ret = gc->vtable->bind(gc, oldGC, draw, read);
260 gc->currentDpy = dpy;
261 gc->currentDrawable = draw;
262 gc->currentReadable = read;
263 gc->thread_id = _glthread_GetID();
264 __glXSetCurrentContext(gc);
265 } else {
266 __glXSetCurrentContextNull();
267 }
268
269 if (ret) {
270 __glXGenerateError(dpy, gc, None, ret, X_GLXMakeContextCurrent);
271 return GL_FALSE;
272 }
273
274 return GL_TRUE;
275 }
276
277
278 _X_EXPORT Bool
279 glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
280 {
281 return MakeContextCurrent(dpy, draw, draw, gc);
282 }
283
284 _X_EXPORT
285 GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
286 (Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
287 (dpy, d, r, ctx), MakeContextCurrent)
288
289 _X_EXPORT
290 GLX_ALIAS(Bool, glXMakeContextCurrent,
291 (Display * dpy, GLXDrawable d, GLXDrawable r,
292 GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent)