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 /**
71 * Per-thread GLX context pointer.
72 *
73 * \c __glXSetCurrentContext is written is such a way that this pointer can
74 * \b never be \c NULL. This is important! Because of this
75 * \c __glXGetCurrentContext can be implemented as trivial macro.
76 */
77 __THREAD_INITIAL_EXEC void *__glX_tls_Context = &dummyContext;
78
79 _X_HIDDEN void
__glXSetCurrentContext(struct glx_context * c)80 __glXSetCurrentContext(struct glx_context * c)
81 {
82 __glX_tls_Context = (c != NULL) ? c : &dummyContext;
83 }
84
85 _X_HIDDEN void
__glXSetCurrentContextNull(void)86 __glXSetCurrentContextNull(void)
87 {
88 __glXSetCurrentContext(&dummyContext);
89 #if defined(GLX_DIRECT_RENDERING)
90 _glapi_set_dispatch(NULL); /* no-op functions */
91 _glapi_set_context(NULL);
92 #endif
93 }
94
95 _GLX_PUBLIC GLXContext
glXGetCurrentContext(void)96 glXGetCurrentContext(void)
97 {
98 struct glx_context *cx = __glXGetCurrentContext();
99
100 if (cx == &dummyContext) {
101 return NULL;
102 }
103 else {
104 return (GLXContext) cx;
105 }
106 }
107
108 _GLX_PUBLIC GLXDrawable
glXGetCurrentDrawable(void)109 glXGetCurrentDrawable(void)
110 {
111 struct glx_context *gc = __glXGetCurrentContext();
112 return gc->currentDrawable;
113 }
114
115 /**
116 * Make a particular context current.
117 *
118 * \note This is in this file so that it can access dummyContext.
119 */
120 static Bool
MakeContextCurrent(Display * dpy,GLXDrawable draw,GLXDrawable read,GLXContext gc_user,int opcode)121 MakeContextCurrent(Display * dpy, GLXDrawable draw,
122 GLXDrawable read, GLXContext gc_user,
123 int opcode)
124 {
125 struct glx_context *gc = (struct glx_context *) gc_user;
126 struct glx_context *oldGC = __glXGetCurrentContext();
127
128 /* Make sure that the new context has a nonzero ID. In the request,
129 * a zero context ID is used only to mean that we bind to no current
130 * context.
131 */
132 if ((gc != NULL) && (gc->xid == None)) {
133 return GL_FALSE;
134 }
135
136 _glapi_check_multithread();
137
138 __glXLock();
139 if (oldGC == gc &&
140 gc->currentDrawable == draw && gc->currentReadable == read) {
141 __glXUnlock();
142 return True;
143 }
144
145 /* can't have only one be 0 */
146 if (!!draw != !!read) {
147 __glXUnlock();
148 __glXSendError(dpy, BadMatch, None, opcode, True);
149 return False;
150 }
151
152 if (oldGC != &dummyContext) {
153 oldGC->vtable->unbind(oldGC, gc);
154 oldGC->currentDpy = NULL;
155 }
156
157 if (gc) {
158 /* Attempt to bind the context. We do this before mucking with
159 * gc and __glXSetCurrentContext to properly handle our state in
160 * case of an error.
161 *
162 * If an error occurs, set the Null context since we've already
163 * blown away our old context. The caller is responsible for
164 * figuring out how to handle setting a valid context.
165 */
166 if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
167 __glXSetCurrentContextNull();
168 __glXUnlock();
169 __glXSendError(dpy, GLXBadContext, None, opcode, False);
170 return GL_FALSE;
171 }
172
173 gc->currentDpy = dpy;
174 gc->currentDrawable = draw;
175 gc->currentReadable = read;
176 __glXSetCurrentContext(gc);
177 } else {
178 __glXSetCurrentContextNull();
179 }
180
181 if (oldGC->currentDpy == NULL && oldGC != &dummyContext && oldGC->xid == None) {
182 /* We are switching away from a context that was
183 * previously destroyed, so we need to free the memory
184 * for the old handle. */
185 oldGC->vtable->destroy(oldGC);
186 }
187
188 __glXUnlock();
189
190 return GL_TRUE;
191 }
192
193 _GLX_PUBLIC Bool
glXMakeCurrent(Display * dpy,GLXDrawable draw,GLXContext gc)194 glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
195 {
196 return MakeContextCurrent(dpy, draw, draw, gc, X_GLXMakeCurrent);
197 }
198
199 _GLX_PUBLIC Bool
glXMakeContextCurrent(Display * dpy,GLXDrawable d,GLXDrawable r,GLXContext ctx)200 glXMakeContextCurrent(Display *dpy, GLXDrawable d, GLXDrawable r,
201 GLXContext ctx)
202 {
203 return MakeContextCurrent(dpy, d, r, ctx, X_GLXMakeContextCurrent);
204 }
205
206 _GLX_PUBLIC Bool
glXMakeCurrentReadSGI(Display * dpy,GLXDrawable d,GLXDrawable r,GLXContext ctx)207 glXMakeCurrentReadSGI(Display *dpy, GLXDrawable d, GLXDrawable r,
208 GLXContext ctx)
209 {
210 return MakeContextCurrent(dpy, d, r, ctx, X_GLXvop_MakeCurrentReadSGI);
211 }
212