2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * 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 * 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.
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.
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.
38 * NOTE: There are no dependencies on Mesa in this code.
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.
53 #ifdef HAVE_DIX_CONFIG_H
55 #include <dix-config.h>
60 #include "main/glheader.h"
71 #include "glapioffsets.h"
72 #include "glapitable.h"
75 /***** BEGIN NO-OP DISPATCH *****/
77 static GLboolean WarnFlag
= GL_FALSE
;
78 static _glapi_warning_func warning_func
;
81 * Enable/disable printing of warning messages.
84 _glapi_noop_enable_warnings(GLboolean enable
)
90 * Register a callback function for reporting errors.
93 _glapi_set_warning_func( _glapi_warning_func func
)
101 #if !defined(_WIN32_WCE)
102 if ((WarnFlag
|| getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
115 #define KEYWORD1 static
116 #define KEYWORD1_ALT static
117 #define KEYWORD2 GLAPIENTRY
118 #define NAME(func) NoOp##func
122 #define DISPATCH(func, args, msg) \
124 warning_func(NULL, "GL User Error: called without context: %s", #func); \
127 #define RETURN_DISPATCH(func, args, msg) \
129 warning_func(NULL, "GL User Error: called without context: %s", #func); \
133 #define DISPATCH_TABLE_NAME __glapi_noop_table
134 #define UNUSED_TABLE_NAME __unused_noop_functions
136 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
138 static GLint
NoOpUnused(void)
141 warning_func(NULL
, "GL User Error: calling extension function without a current context\n");
146 #include "glapitemp.h"
148 /***** END NO-OP DISPATCH *****/
153 * \name Current dispatch and current context control variables
155 * Depending on whether or not multithreading is support, and the type of
156 * support available, several variables are used to store the current context
157 * pointer and the current dispatch table pointer. In the non-threaded case,
158 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
161 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
162 * \c _glapi_Context will be \c NULL if an application is detected as being
163 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
164 * and \c _glapi_Context just like the case without any threading support.
165 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
166 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
167 * static dispatch functions access these variables via \c _glapi_get_dispatch
168 * and \c _glapi_get_context.
170 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
171 * possible for the original thread to be setting it at the same instant a new
172 * thread, perhaps running on a different processor, is clearing it. Because
173 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
174 * used to determine whether or not the application is multithreaded.
176 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
177 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
178 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
179 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
180 * between TLS enabled loaders and non-TLS DRI drivers.
183 #if defined(GLX_USE_TLS)
185 PUBLIC __thread
struct _glapi_table
* _glapi_tls_Dispatch
186 __attribute__((tls_model("initial-exec")))
187 = (struct _glapi_table
*) __glapi_noop_table
;
189 PUBLIC __thread
void * _glapi_tls_Context
190 __attribute__((tls_model("initial-exec")));
192 PUBLIC
const struct _glapi_table
*_glapi_Dispatch
= NULL
;
193 PUBLIC
const void *_glapi_Context
= NULL
;
199 static GLboolean ThreadSafe
= GL_FALSE
; /**< In thread-safe mode? */
200 _glthread_TSD _gl_DispatchTSD
; /**< Per-thread dispatch pointer */
201 static _glthread_TSD ContextTSD
; /**< Per-thread context pointer */
203 #if defined(WIN32_THREADS)
204 void FreeTSD(_glthread_TSD
*p
);
205 void FreeAllTSD(void)
207 FreeTSD(&_gl_DispatchTSD
);
208 FreeTSD(&ContextTSD
);
210 #endif /* defined(WIN32_THREADS) */
212 #endif /* defined(THREADS) */
214 PUBLIC
struct _glapi_table
*_glapi_Dispatch
=
215 (struct _glapi_table
*) __glapi_noop_table
;
216 PUBLIC
void *_glapi_Context
= NULL
;
218 #endif /* defined(GLX_USE_TLS) */
225 * We should call this periodically from a function such as glXMakeCurrent
226 * in order to test if multiple threads are being used.
229 _glapi_check_multithread(void)
231 #if defined(THREADS) && !defined(GLX_USE_TLS)
233 static unsigned long knownID
;
234 static GLboolean firstCall
= GL_TRUE
;
236 knownID
= _glthread_GetID();
237 firstCall
= GL_FALSE
;
239 else if (knownID
!= _glthread_GetID()) {
240 ThreadSafe
= GL_TRUE
;
241 _glapi_set_dispatch(NULL
);
242 _glapi_set_context(NULL
);
245 else if (!_glapi_get_dispatch()) {
246 /* make sure that this thread's dispatch pointer isn't null */
247 _glapi_set_dispatch(NULL
);
255 * Set the current context pointer for this thread.
256 * The context pointer is an opaque type which should be cast to
257 * void from the real context pointer type.
260 _glapi_set_context(void *context
)
262 (void) __unused_noop_functions
; /* silence a warning */
263 #if defined(GLX_USE_TLS)
264 _glapi_tls_Context
= context
;
265 #elif defined(THREADS)
266 _glthread_SetTSD(&ContextTSD
, context
);
267 _glapi_Context
= (ThreadSafe
) ? NULL
: context
;
269 _glapi_Context
= context
;
276 * Get the current context pointer for this thread.
277 * The context pointer is an opaque type which should be cast from
278 * void to the real context pointer type.
281 _glapi_get_context(void)
283 #if defined(GLX_USE_TLS)
284 return _glapi_tls_Context
;
285 #elif defined(THREADS)
287 return _glthread_GetTSD(&ContextTSD
);
290 return _glapi_Context
;
293 return _glapi_Context
;
299 #if defined( GLX_USE_TLS )
300 extern GLubyte gl_dispatch_functions_start
[];
301 extern GLubyte gl_dispatch_functions_end
[];
303 extern const GLubyte gl_dispatch_functions_start
[];
306 #endif /* USE_X86_ASM */
309 #if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
310 # define DISPATCH_FUNCTION_SIZE 16
311 #elif defined(USE_X86_ASM)
312 # if defined(THREADS) && !defined(GLX_USE_TLS)
313 # define DISPATCH_FUNCTION_SIZE 32
315 # define DISPATCH_FUNCTION_SIZE 16
319 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
320 # define NEED_FUNCTION_POINTER
323 #if defined(PTHREADS) || defined(GLX_USE_TLS)
325 * Perform platform-specific GL API entry-point fixups.
328 init_glapi_relocs( void )
330 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
331 extern unsigned long _x86_get_dispatch(void);
332 char run_time_patch
[] = {
333 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
335 GLuint
*offset
= (GLuint
*) &run_time_patch
[2]; /* 32-bits for x86/32 */
336 const GLubyte
* const get_disp
= (const GLubyte
*) run_time_patch
;
337 GLubyte
* curr_func
= (GLubyte
*) gl_dispatch_functions_start
;
339 *offset
= _x86_get_dispatch();
340 while ( curr_func
!= (GLubyte
*) gl_dispatch_functions_end
) {
341 (void) memcpy( curr_func
, get_disp
, sizeof(run_time_patch
));
342 curr_func
+= DISPATCH_FUNCTION_SIZE
;
346 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
350 * Set the global or per-thread dispatch table pointer.
351 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
352 * table (__glapi_noop_table).
355 _glapi_set_dispatch(struct _glapi_table
*dispatch
)
357 #if defined(PTHREADS) || defined(GLX_USE_TLS)
358 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
359 pthread_once( & once_control
, init_glapi_relocs
);
363 /* use the no-op functions */
364 dispatch
= (struct _glapi_table
*) __glapi_noop_table
;
368 _glapi_check_table(dispatch
);
372 #if defined(GLX_USE_TLS)
373 _glapi_tls_Dispatch
= dispatch
;
374 #elif defined(THREADS)
375 _glthread_SetTSD(&_gl_DispatchTSD
, (void *) dispatch
);
376 _glapi_Dispatch
= (ThreadSafe
) ? NULL
: dispatch
;
378 _glapi_Dispatch
= dispatch
;
385 * Return pointer to current dispatch table for calling thread.
387 PUBLIC
struct _glapi_table
*
388 _glapi_get_dispatch(void)
390 struct _glapi_table
* api
;
391 #if defined(GLX_USE_TLS)
392 api
= _glapi_tls_Dispatch
;
393 #elif defined(THREADS)
395 ? (struct _glapi_table
*) _glthread_GetTSD(&_gl_DispatchTSD
)
398 api
= _glapi_Dispatch
;
407 * The dispatch table size (number of entries) is the size of the
408 * _glapi_table struct plus the number of dynamic entries we can add.
409 * The extra slots can be filled in by DRI drivers that register new extension
412 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
416 * Return size of dispatch table struct as number of functions (or
420 _glapi_get_dispatch_table_size(void)
422 return DISPATCH_TABLE_SIZE
;
427 * Make sure there are no NULL pointers in the given dispatch table.
428 * Intended for debugging purposes.
431 _glapi_check_table(const struct _glapi_table
*table
)
434 const GLuint entries
= _glapi_get_dispatch_table_size();
435 const void **tab
= (const void **) table
;
437 for (i
= 1; i
< entries
; i
++) {
441 /* Do some spot checks to be sure that the dispatch table
442 * slots are assigned correctly.
445 GLuint BeginOffset
= _glapi_get_proc_offset("glBegin");
446 char *BeginFunc
= (char*) &table
->Begin
;
447 GLuint offset
= (BeginFunc
- (char *) table
) / sizeof(void *);
448 assert(BeginOffset
== _gloffset_Begin
);
449 assert(BeginOffset
== offset
);
452 GLuint viewportOffset
= _glapi_get_proc_offset("glViewport");
453 char *viewportFunc
= (char*) &table
->Viewport
;
454 GLuint offset
= (viewportFunc
- (char *) table
) / sizeof(void *);
455 assert(viewportOffset
== _gloffset_Viewport
);
456 assert(viewportOffset
== offset
);
459 GLuint VertexPointerOffset
= _glapi_get_proc_offset("glVertexPointer");
460 char *VertexPointerFunc
= (char*) &table
->VertexPointer
;
461 GLuint offset
= (VertexPointerFunc
- (char *) table
) / sizeof(void *);
462 assert(VertexPointerOffset
== _gloffset_VertexPointer
);
463 assert(VertexPointerOffset
== offset
);
466 GLuint ResetMinMaxOffset
= _glapi_get_proc_offset("glResetMinmax");
467 char *ResetMinMaxFunc
= (char*) &table
->ResetMinmax
;
468 GLuint offset
= (ResetMinMaxFunc
- (char *) table
) / sizeof(void *);
469 assert(ResetMinMaxOffset
== _gloffset_ResetMinmax
);
470 assert(ResetMinMaxOffset
== offset
);
473 GLuint blendColorOffset
= _glapi_get_proc_offset("glBlendColor");
474 char *blendColorFunc
= (char*) &table
->BlendColor
;
475 GLuint offset
= (blendColorFunc
- (char *) table
) / sizeof(void *);
476 assert(blendColorOffset
== _gloffset_BlendColor
);
477 assert(blendColorOffset
== offset
);
480 GLuint secondaryColor3fOffset
= _glapi_get_proc_offset("glSecondaryColor3fEXT");
481 char *secondaryColor3fFunc
= (char*) &table
->SecondaryColor3fEXT
;
482 GLuint offset
= (secondaryColor3fFunc
- (char *) table
) / sizeof(void *);
483 assert(secondaryColor3fOffset
== _gloffset_SecondaryColor3fEXT
);
484 assert(secondaryColor3fOffset
== offset
);
487 GLuint pointParameterivOffset
= _glapi_get_proc_offset("glPointParameterivNV");
488 char *pointParameterivFunc
= (char*) &table
->PointParameterivNV
;
489 GLuint offset
= (pointParameterivFunc
- (char *) table
) / sizeof(void *);
490 assert(pointParameterivOffset
== _gloffset_PointParameterivNV
);
491 assert(pointParameterivOffset
== offset
);
494 GLuint setFenceOffset
= _glapi_get_proc_offset("glSetFenceNV");
495 char *setFenceFunc
= (char*) &table
->SetFenceNV
;
496 GLuint offset
= (setFenceFunc
- (char *) table
) / sizeof(void *);
497 assert(setFenceOffset
== _gloffset_SetFenceNV
);
498 assert(setFenceOffset
== offset
);