glapi: add gl_dispatch_functions_start and end
[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 #ifdef USE_X86_ASM
294
295 #if defined( GLX_USE_TLS )
296 extern GLubyte gl_dispatch_functions_start[];
297 extern GLubyte gl_dispatch_functions_end[];
298 #else
299 extern const GLubyte gl_dispatch_functions_start[];
300 #endif
301
302 #endif /* USE_X86_ASM */
303
304
305 #if defined(PTHREADS) || defined(GLX_USE_TLS)
306 /**
307 * Perform platform-specific GL API entry-point fixups.
308 */
309 static void
310 init_glapi_relocs( void )
311 {
312 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
313 extern unsigned long _x86_get_dispatch(void);
314 char run_time_patch[] = {
315 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
316 };
317 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
318 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
319 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
320
321 *offset = _x86_get_dispatch();
322 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
323 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
324 curr_func += DISPATCH_FUNCTION_SIZE;
325 }
326 #endif
327 }
328 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
329
330
331 /**
332 * Set the global or per-thread dispatch table pointer.
333 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
334 * table (__glapi_noop_table).
335 */
336 PUBLIC void
337 _glapi_set_dispatch(struct _glapi_table *dispatch)
338 {
339 #if defined(PTHREADS) || defined(GLX_USE_TLS)
340 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
341 pthread_once( & once_control, init_glapi_relocs );
342 #endif
343
344 if (!dispatch) {
345 /* use the no-op functions */
346 dispatch = (struct _glapi_table *) __glapi_noop_table;
347 }
348 #ifdef DEBUG
349 else {
350 _glapi_check_table(dispatch);
351 }
352 #endif
353
354 #if defined(GLX_USE_TLS)
355 _glapi_tls_Dispatch = dispatch;
356 #elif defined(THREADS)
357 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
358 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
359 #else /*THREADS*/
360 _glapi_Dispatch = dispatch;
361 #endif /*THREADS*/
362 }
363
364
365
366 /**
367 * Return pointer to current dispatch table for calling thread.
368 */
369 PUBLIC struct _glapi_table *
370 _glapi_get_dispatch(void)
371 {
372 struct _glapi_table * api;
373 #if defined(GLX_USE_TLS)
374 api = _glapi_tls_Dispatch;
375 #elif defined(THREADS)
376 api = (ThreadSafe)
377 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
378 : _glapi_Dispatch;
379 #else
380 api = _glapi_Dispatch;
381 #endif
382 return api;
383 }
384
385
386
387
388 /*
389 * The dispatch table size (number of entries) is the size of the
390 * _glapi_table struct plus the number of dynamic entries we can add.
391 * The extra slots can be filled in by DRI drivers that register new extension
392 * functions.
393 */
394 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
395
396
397 /**
398 * Return size of dispatch table struct as number of functions (or
399 * slots).
400 */
401 PUBLIC GLuint
402 _glapi_get_dispatch_table_size(void)
403 {
404 return DISPATCH_TABLE_SIZE;
405 }
406
407
408 /**
409 * Make sure there are no NULL pointers in the given dispatch table.
410 * Intended for debugging purposes.
411 */
412 void
413 _glapi_check_table(const struct _glapi_table *table)
414 {
415 #ifdef EXTRA_DEBUG
416 const GLuint entries = _glapi_get_dispatch_table_size();
417 const void **tab = (const void **) table;
418 GLuint i;
419 for (i = 1; i < entries; i++) {
420 assert(tab[i]);
421 }
422
423 /* Do some spot checks to be sure that the dispatch table
424 * slots are assigned correctly.
425 */
426 {
427 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
428 char *BeginFunc = (char*) &table->Begin;
429 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
430 assert(BeginOffset == _gloffset_Begin);
431 assert(BeginOffset == offset);
432 }
433 {
434 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
435 char *viewportFunc = (char*) &table->Viewport;
436 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
437 assert(viewportOffset == _gloffset_Viewport);
438 assert(viewportOffset == offset);
439 }
440 {
441 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
442 char *VertexPointerFunc = (char*) &table->VertexPointer;
443 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
444 assert(VertexPointerOffset == _gloffset_VertexPointer);
445 assert(VertexPointerOffset == offset);
446 }
447 {
448 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
449 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
450 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
451 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
452 assert(ResetMinMaxOffset == offset);
453 }
454 {
455 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
456 char *blendColorFunc = (char*) &table->BlendColor;
457 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
458 assert(blendColorOffset == _gloffset_BlendColor);
459 assert(blendColorOffset == offset);
460 }
461 {
462 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
463 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
464 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
465 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
466 assert(secondaryColor3fOffset == offset);
467 }
468 {
469 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
470 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
471 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
472 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
473 assert(pointParameterivOffset == offset);
474 }
475 {
476 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
477 char *setFenceFunc = (char*) &table->SetFenceNV;
478 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
479 assert(setFenceOffset == _gloffset_SetFenceNV);
480 assert(setFenceOffset == offset);
481 }
482 #else
483 (void) table;
484 #endif
485 }