mesa: refactor: move GetProcAddress code from glapi.c into new glapi_getproc.c file
[mesa.git] / src / mesa / glapi / glapi.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * This file manages the OpenGL API dispatch layer.
28 * The dispatch table (struct _glapi_table) is basically just a list
29 * of function pointers.
30 * There are functions to set/get the current dispatch table for the
31 * current thread and to manage registration/dispatch of dynamically
32 * added extension functions.
33 *
34 * It's intended that this file and the other glapi*.[ch] files are
35 * flexible enough to be reused in several places: XFree86, DRI-
36 * based libGL.so, and perhaps the SGI SI.
37 *
38 * NOTE: There are no dependencies on Mesa in this code.
39 *
40 * Versions (API changes):
41 * 2000/02/23 - original version for Mesa 3.3 and XFree86 4.0
42 * 2001/01/16 - added dispatch override feature for Mesa 3.5
43 * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1.
44 * 2002/10/01 - _glapi_get_proc_address() will now generate new entrypoints
45 * itself (using offset ~0). _glapi_add_entrypoint() can be
46 * called afterward and it'll fill in the correct dispatch
47 * offset. This allows DRI libGL to avoid probing for DRI
48 * drivers! No changes to the public glapi interface.
49 */
50
51
52
53 #ifdef HAVE_DIX_CONFIG_H
54
55 #include <dix-config.h>
56 #define PUBLIC
57
58 #else
59
60 #include "main/glheader.h"
61
62 #endif
63
64 #include <stdlib.h>
65 #include <string.h>
66 #ifdef DEBUG
67 #include <assert.h>
68 #endif
69
70 #include "glapi.h"
71 #include "glapioffsets.h"
72 #include "glapitable.h"
73
74
75 /***** BEGIN NO-OP DISPATCH *****/
76
77 static GLboolean WarnFlag = GL_FALSE;
78 static _glapi_warning_func warning_func;
79
80 /*
81 * Enable/disable printing of warning messages.
82 */
83 PUBLIC void
84 _glapi_noop_enable_warnings(GLboolean enable)
85 {
86 WarnFlag = enable;
87 }
88
89 /*
90 * Register a callback function for reporting errors.
91 */
92 PUBLIC void
93 _glapi_set_warning_func( _glapi_warning_func func )
94 {
95 warning_func = func;
96 }
97
98 static GLboolean
99 warn(void)
100 {
101 if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
102 && warning_func) {
103 return GL_TRUE;
104 }
105 else {
106 return GL_FALSE;
107 }
108 }
109
110
111 #define KEYWORD1 static
112 #define KEYWORD1_ALT static
113 #define KEYWORD2 GLAPIENTRY
114 #define NAME(func) NoOp##func
115
116 #define F NULL
117
118 #define DISPATCH(func, args, msg) \
119 if (warn()) { \
120 warning_func(NULL, "GL User Error: called without context: %s", #func); \
121 }
122
123 #define RETURN_DISPATCH(func, args, msg) \
124 if (warn()) { \
125 warning_func(NULL, "GL User Error: called without context: %s", #func); \
126 } \
127 return 0
128
129 #define DISPATCH_TABLE_NAME __glapi_noop_table
130 #define UNUSED_TABLE_NAME __unused_noop_functions
131
132 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
133
134 static GLint NoOpUnused(void)
135 {
136 if (warn()) {
137 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
138 }
139 return 0;
140 }
141
142 #include "glapitemp.h"
143
144 /***** END NO-OP DISPATCH *****/
145
146
147
148 /**
149 * \name Current dispatch and current context control variables
150 *
151 * Depending on whether or not multithreading is support, and the type of
152 * support available, several variables are used to store the current context
153 * pointer and the current dispatch table pointer. In the non-threaded case,
154 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
155 * purpose.
156 *
157 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
158 * \c _glapi_Context will be \c NULL if an application is detected as being
159 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
160 * and \c _glapi_Context just like the case without any threading support.
161 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
162 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
163 * static dispatch functions access these variables via \c _glapi_get_dispatch
164 * and \c _glapi_get_context.
165 *
166 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
167 * possible for the original thread to be setting it at the same instant a new
168 * thread, perhaps running on a different processor, is clearing it. Because
169 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
170 * used to determine whether or not the application is multithreaded.
171 *
172 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
173 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
174 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
175 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
176 * between TLS enabled loaders and non-TLS DRI drivers.
177 */
178 /*@{*/
179 #if defined(GLX_USE_TLS)
180
181 PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
182 __attribute__((tls_model("initial-exec")))
183 = (struct _glapi_table *) __glapi_noop_table;
184
185 PUBLIC __thread void * _glapi_tls_Context
186 __attribute__((tls_model("initial-exec")));
187
188 PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
189 PUBLIC const void *_glapi_Context = NULL;
190
191 #else
192
193 #if defined(THREADS)
194
195 static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
196 _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
197 static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
198
199 #if defined(WIN32_THREADS)
200 void FreeTSD(_glthread_TSD *p);
201 void FreeAllTSD(void)
202 {
203 FreeTSD(&_gl_DispatchTSD);
204 FreeTSD(&ContextTSD);
205 }
206 #endif /* defined(WIN32_THREADS) */
207
208 #endif /* defined(THREADS) */
209
210 PUBLIC struct _glapi_table *_glapi_Dispatch =
211 (struct _glapi_table *) __glapi_noop_table;
212 PUBLIC void *_glapi_Context = NULL;
213
214 #endif /* defined(GLX_USE_TLS) */
215 /*@}*/
216
217
218
219
220 /**
221 * We should call this periodically from a function such as glXMakeCurrent
222 * in order to test if multiple threads are being used.
223 */
224 void
225 _glapi_check_multithread(void)
226 {
227 #if defined(THREADS) && !defined(GLX_USE_TLS)
228 if (!ThreadSafe) {
229 static unsigned long knownID;
230 static GLboolean firstCall = GL_TRUE;
231 if (firstCall) {
232 knownID = _glthread_GetID();
233 firstCall = GL_FALSE;
234 }
235 else if (knownID != _glthread_GetID()) {
236 ThreadSafe = GL_TRUE;
237 _glapi_set_dispatch(NULL);
238 _glapi_set_context(NULL);
239 }
240 }
241 else if (!_glapi_get_dispatch()) {
242 /* make sure that this thread's dispatch pointer isn't null */
243 _glapi_set_dispatch(NULL);
244 }
245 #endif
246 }
247
248
249
250 /**
251 * Set the current context pointer for this thread.
252 * The context pointer is an opaque type which should be cast to
253 * void from the real context pointer type.
254 */
255 PUBLIC void
256 _glapi_set_context(void *context)
257 {
258 (void) __unused_noop_functions; /* silence a warning */
259 #if defined(GLX_USE_TLS)
260 _glapi_tls_Context = context;
261 #elif defined(THREADS)
262 _glthread_SetTSD(&ContextTSD, context);
263 _glapi_Context = (ThreadSafe) ? NULL : context;
264 #else
265 _glapi_Context = context;
266 #endif
267 }
268
269
270
271 /**
272 * Get the current context pointer for this thread.
273 * The context pointer is an opaque type which should be cast from
274 * void to the real context pointer type.
275 */
276 PUBLIC void *
277 _glapi_get_context(void)
278 {
279 #if defined(GLX_USE_TLS)
280 return _glapi_tls_Context;
281 #elif defined(THREADS)
282 if (ThreadSafe) {
283 return _glthread_GetTSD(&ContextTSD);
284 }
285 else {
286 return _glapi_Context;
287 }
288 #else
289 return _glapi_Context;
290 #endif
291 }
292
293
294
295 #if defined(PTHREADS) || defined(GLX_USE_TLS)
296 /**
297 * Perform platform-specific GL API entry-point fixups.
298 */
299 static void
300 init_glapi_relocs( void )
301 {
302 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
303 extern unsigned long _x86_get_dispatch(void);
304 char run_time_patch[] = {
305 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
306 };
307 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
308 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
309 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
310
311 *offset = _x86_get_dispatch();
312 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
313 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
314 curr_func += DISPATCH_FUNCTION_SIZE;
315 }
316 #endif
317 }
318 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
319
320
321 /**
322 * Set the global or per-thread dispatch table pointer.
323 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
324 * table (__glapi_noop_table).
325 */
326 PUBLIC void
327 _glapi_set_dispatch(struct _glapi_table *dispatch)
328 {
329 #if defined(PTHREADS) || defined(GLX_USE_TLS)
330 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
331 pthread_once( & once_control, init_glapi_relocs );
332 #endif
333
334 if (!dispatch) {
335 /* use the no-op functions */
336 dispatch = (struct _glapi_table *) __glapi_noop_table;
337 }
338 #ifdef DEBUG
339 else {
340 _glapi_check_table(dispatch);
341 }
342 #endif
343
344 #if defined(GLX_USE_TLS)
345 _glapi_tls_Dispatch = dispatch;
346 #elif defined(THREADS)
347 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
348 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
349 #else /*THREADS*/
350 _glapi_Dispatch = dispatch;
351 #endif /*THREADS*/
352 }
353
354
355
356 /**
357 * Return pointer to current dispatch table for calling thread.
358 */
359 PUBLIC struct _glapi_table *
360 _glapi_get_dispatch(void)
361 {
362 struct _glapi_table * api;
363 #if defined(GLX_USE_TLS)
364 api = _glapi_tls_Dispatch;
365 #elif defined(THREADS)
366 api = (ThreadSafe)
367 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
368 : _glapi_Dispatch;
369 #else
370 api = _glapi_Dispatch;
371 #endif
372 return api;
373 }
374
375
376
377
378 /*
379 * The dispatch table size (number of entries) is the size of the
380 * _glapi_table struct plus the number of dynamic entries we can add.
381 * The extra slots can be filled in by DRI drivers that register new extension
382 * functions.
383 */
384 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
385
386
387 /**
388 * Return size of dispatch table struct as number of functions (or
389 * slots).
390 */
391 PUBLIC GLuint
392 _glapi_get_dispatch_table_size(void)
393 {
394 return DISPATCH_TABLE_SIZE;
395 }
396
397
398 /**
399 * Make sure there are no NULL pointers in the given dispatch table.
400 * Intended for debugging purposes.
401 */
402 void
403 _glapi_check_table(const struct _glapi_table *table)
404 {
405 #ifdef EXTRA_DEBUG
406 const GLuint entries = _glapi_get_dispatch_table_size();
407 const void **tab = (const void **) table;
408 GLuint i;
409 for (i = 1; i < entries; i++) {
410 assert(tab[i]);
411 }
412
413 /* Do some spot checks to be sure that the dispatch table
414 * slots are assigned correctly.
415 */
416 {
417 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
418 char *BeginFunc = (char*) &table->Begin;
419 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
420 assert(BeginOffset == _gloffset_Begin);
421 assert(BeginOffset == offset);
422 }
423 {
424 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
425 char *viewportFunc = (char*) &table->Viewport;
426 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
427 assert(viewportOffset == _gloffset_Viewport);
428 assert(viewportOffset == offset);
429 }
430 {
431 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
432 char *VertexPointerFunc = (char*) &table->VertexPointer;
433 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
434 assert(VertexPointerOffset == _gloffset_VertexPointer);
435 assert(VertexPointerOffset == offset);
436 }
437 {
438 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
439 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
440 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
441 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
442 assert(ResetMinMaxOffset == offset);
443 }
444 {
445 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
446 char *blendColorFunc = (char*) &table->BlendColor;
447 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
448 assert(blendColorOffset == _gloffset_BlendColor);
449 assert(blendColorOffset == offset);
450 }
451 {
452 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
453 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
454 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
455 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
456 assert(secondaryColor3fOffset == offset);
457 }
458 {
459 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
460 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
461 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
462 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
463 assert(pointParameterivOffset == offset);
464 }
465 {
466 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
467 char *setFenceFunc = (char*) &table->SetFenceNV;
468 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
469 assert(setFenceOffset == _gloffset_SetFenceNV);
470 assert(setFenceOffset == offset);
471 }
472 #else
473 (void) table;
474 #endif
475 }