Remove the extraneous GL_ prefix from the enums for GL_EXT_timer_query and
[mesa.git] / src / mesa / glapi / glapi.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 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 #include "glheader.h"
54 #include "glapi.h"
55 #include "glapioffsets.h"
56 #include "glapitable.h"
57
58 /***** BEGIN NO-OP DISPATCH *****/
59
60 static GLboolean WarnFlag = GL_FALSE;
61 static _glapi_warning_func warning_func;
62
63 #if defined(PTHREADS) || defined(GLX_USE_TLS)
64 static void init_glapi_relocs(void);
65 #endif
66
67 static _glapi_proc generate_entrypoint(GLuint functionOffset);
68 static void fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset);
69
70 /*
71 * Enable/disable printing of warning messages.
72 */
73 PUBLIC void
74 _glapi_noop_enable_warnings(GLboolean enable)
75 {
76 WarnFlag = enable;
77 }
78
79 /*
80 * Register a callback function for reporting errors.
81 */
82 PUBLIC void
83 _glapi_set_warning_func( _glapi_warning_func func )
84 {
85 warning_func = func;
86 }
87
88 static GLboolean
89 warn(void)
90 {
91 if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
92 && warning_func) {
93 return GL_TRUE;
94 }
95 else {
96 return GL_FALSE;
97 }
98 }
99
100
101 #define KEYWORD1 static
102 #define KEYWORD2 GLAPIENTRY
103 #define NAME(func) NoOp##func
104
105 #define F NULL
106
107 #define DISPATCH(func, args, msg) \
108 if (warn()) { \
109 warning_func(NULL, "GL User Error: called without context: %s", #func); \
110 }
111
112 #define RETURN_DISPATCH(func, args, msg) \
113 if (warn()) { \
114 warning_func(NULL, "GL User Error: called without context: %s", #func); \
115 } \
116 return 0
117
118 #define DISPATCH_TABLE_NAME __glapi_noop_table
119 #define UNUSED_TABLE_NAME __unused_noop_functions
120
121 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
122
123 static GLint NoOpUnused(void)
124 {
125 if (warn()) {
126 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
127 }
128 return 0;
129 }
130
131 #include "glapitemp.h"
132
133 /***** END NO-OP DISPATCH *****/
134
135
136
137 /**
138 * \name Current dispatch and current context control variables
139 *
140 * Depending on whether or not multithreading is support, and the type of
141 * support available, several variables are used to store the current context
142 * pointer and the current dispatch table pointer. In the non-threaded case,
143 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
144 * purpose.
145 *
146 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
147 * \c _glapi_Context will be \c NULL if an application is detected as being
148 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
149 * and \c _glapi_Context just like the case without any threading support.
150 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
151 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
152 * static dispatch functions access these variables via \c _glapi_get_dispatch
153 * and \c _glapi_get_context.
154 *
155 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
156 * possible for the original thread to be setting it at the same instant a new
157 * thread, perhaps running on a different processor, is clearing it. Because
158 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
159 * used to determine whether or not the application is multithreaded.
160 *
161 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
162 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
163 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
164 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
165 * between TLS enabled loaders and non-TLS DRI drivers.
166 */
167 /*@{*/
168 #if defined(GLX_USE_TLS)
169
170 PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
171 __attribute__((tls_model("initial-exec")))
172 = (struct _glapi_table *) __glapi_noop_table;
173
174 PUBLIC __thread void * _glapi_tls_Context
175 __attribute__((tls_model("initial-exec")));
176
177 PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
178 PUBLIC const void *_glapi_Context = NULL;
179
180 #else
181
182 #if defined(THREADS)
183
184 static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
185 _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
186 static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
187
188 #if defined(WIN32_THREADS)
189 void FreeTSD(_glthread_TSD *p);
190 void FreeAllTSD(void)
191 {
192 FreeTSD(&_gl_DispatchTSD);
193 FreeTSD(&ContextTSD);
194 }
195 #endif /* defined(WIN32_THREADS) */
196
197 #endif /* defined(THREADS) */
198
199 PUBLIC struct _glapi_table *_glapi_Dispatch =
200 (struct _glapi_table *) __glapi_noop_table;
201 PUBLIC void *_glapi_Context = NULL;
202
203 #endif /* defined(GLX_USE_TLS) */
204 /*@}*/
205
206
207 /**
208 * strdup() is actually not a standard ANSI C or POSIX routine.
209 * Irix will not define it if ANSI mode is in effect.
210 */
211 static char *
212 str_dup(const char *str)
213 {
214 char *copy;
215 copy = (char*) malloc(strlen(str) + 1);
216 if (!copy)
217 return NULL;
218 strcpy(copy, str);
219 return copy;
220 }
221
222
223
224 /**
225 * We should call this periodically from a function such as glXMakeCurrent
226 * in order to test if multiple threads are being used.
227 */
228 void
229 _glapi_check_multithread(void)
230 {
231 #if defined(THREADS) && !defined(GLX_USE_TLS)
232 if (!ThreadSafe) {
233 static unsigned long knownID;
234 static GLboolean firstCall = GL_TRUE;
235 if (firstCall) {
236 knownID = _glthread_GetID();
237 firstCall = GL_FALSE;
238 }
239 else if (knownID != _glthread_GetID()) {
240 ThreadSafe = GL_TRUE;
241 _glapi_set_dispatch(NULL);
242 }
243 }
244 else if (!_glapi_get_dispatch()) {
245 /* make sure that this thread's dispatch pointer isn't null */
246 _glapi_set_dispatch(NULL);
247 }
248 #endif
249 }
250
251
252
253 /**
254 * Set the current context pointer for this thread.
255 * The context pointer is an opaque type which should be cast to
256 * void from the real context pointer type.
257 */
258 PUBLIC void
259 _glapi_set_context(void *context)
260 {
261 (void) __unused_noop_functions; /* silence a warning */
262 #if defined(GLX_USE_TLS)
263 _glapi_tls_Context = context;
264 #elif defined(THREADS)
265 _glthread_SetTSD(&ContextTSD, context);
266 _glapi_Context = (ThreadSafe) ? NULL : context;
267 #else
268 _glapi_Context = context;
269 #endif
270 }
271
272
273
274 /**
275 * Get the current context pointer for this thread.
276 * The context pointer is an opaque type which should be cast from
277 * void to the real context pointer type.
278 */
279 PUBLIC void *
280 _glapi_get_context(void)
281 {
282 #if defined(GLX_USE_TLS)
283 return _glapi_tls_Context;
284 #elif defined(THREADS)
285 if (ThreadSafe) {
286 return _glthread_GetTSD(&ContextTSD);
287 }
288 else {
289 return _glapi_Context;
290 }
291 #else
292 return _glapi_Context;
293 #endif
294 }
295
296
297
298 /**
299 * Set the global or per-thread dispatch table pointer.
300 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
301 * table (__glapi_noop_table).
302 */
303 PUBLIC void
304 _glapi_set_dispatch(struct _glapi_table *dispatch)
305 {
306 #if defined(PTHREADS) || defined(GLX_USE_TLS)
307 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
308 pthread_once( & once_control, init_glapi_relocs );
309 #endif
310
311 if (!dispatch) {
312 /* use the no-op functions */
313 dispatch = (struct _glapi_table *) __glapi_noop_table;
314 }
315 #ifdef DEBUG
316 else {
317 _glapi_check_table(dispatch);
318 }
319 #endif
320
321 #if defined(GLX_USE_TLS)
322 _glapi_tls_Dispatch = dispatch;
323 #elif defined(THREADS)
324 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
325 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
326 #else /*THREADS*/
327 _glapi_Dispatch = dispatch;
328 #endif /*THREADS*/
329 }
330
331
332
333 /**
334 * Return pointer to current dispatch table for calling thread.
335 */
336 PUBLIC struct _glapi_table *
337 _glapi_get_dispatch(void)
338 {
339 struct _glapi_table * api;
340 #if defined(GLX_USE_TLS)
341 api = _glapi_tls_Dispatch;
342 #elif defined(THREADS)
343 api = (ThreadSafe)
344 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
345 : _glapi_Dispatch;
346 #else
347 api = _glapi_Dispatch;
348 #endif
349 return api;
350 }
351
352
353
354 /***
355 *** The rest of this file is pretty much concerned with GetProcAddress
356 *** functionality.
357 ***/
358
359 #if !defined( USE_X86_ASM ) && !defined( XFree86Server ) && !defined ( XGLServer )
360 #define NEED_FUNCTION_POINTER
361 #endif
362
363 /* The code in this file is auto-generated with Python */
364 #include "glprocs.h"
365
366
367 /**
368 * Search the table of static entrypoint functions for the named function
369 * and return the corresponding glprocs_table_t entry.
370 */
371 static const glprocs_table_t *
372 find_entry( const char * n )
373 {
374 GLuint i;
375 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
376 const char *testName = gl_string_table + static_functions[i].Name_offset;
377 if (strcmp(testName, n) == 0) {
378 return &static_functions[i];
379 }
380 }
381 return NULL;
382 }
383
384
385 /**
386 * Return dispatch table offset of the named static (built-in) function.
387 * Return -1 if function not found.
388 */
389 static GLint
390 get_static_proc_offset(const char *funcName)
391 {
392 const glprocs_table_t * const f = find_entry( funcName );
393 if (f) {
394 return f->Offset;
395 }
396 return -1;
397 }
398
399
400 #if !defined(XFree86Server) && !defined(XGLServer)
401 #ifdef USE_X86_ASM
402
403 #if defined( GLX_USE_TLS )
404 extern GLubyte gl_dispatch_functions_start[];
405 extern GLubyte gl_dispatch_functions_end[];
406 #else
407 extern const GLubyte gl_dispatch_functions_start[];
408 #endif
409
410 # if defined(THREADS) && !defined(GLX_USE_TLS)
411 # define X86_DISPATCH_FUNCTION_SIZE 32
412 # else
413 # define X86_DISPATCH_FUNCTION_SIZE 16
414 # endif
415
416 #endif /* USE_X86_ASM */
417
418
419 /**
420 * Return dispatch function address for the named static (built-in) function.
421 * Return NULL if function not found.
422 */
423 static _glapi_proc
424 get_static_proc_address(const char *funcName)
425 {
426 const glprocs_table_t * const f = find_entry( funcName );
427 if (f) {
428 #ifdef USE_X86_ASM
429 return (_glapi_proc) (gl_dispatch_functions_start
430 + (X86_DISPATCH_FUNCTION_SIZE * f->Offset));
431 #else
432 return f->Address;
433 #endif
434 }
435 else {
436 return NULL;
437 }
438 }
439
440 #endif /* !defined(XFree86Server) && !defined(XGLServer) */
441
442
443
444 /**
445 * Return the name of the function at the given offset in the dispatch
446 * table. For debugging only.
447 */
448 static const char *
449 get_static_proc_name( GLuint offset )
450 {
451 GLuint i;
452 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
453 if (static_functions[i].Offset == offset) {
454 return gl_string_table + static_functions[i].Name_offset;
455 }
456 }
457 return NULL;
458 }
459
460
461
462 /**********************************************************************
463 * Extension function management.
464 */
465
466 /*
467 * Number of extension functions which we can dynamically add at runtime.
468 */
469 #define MAX_EXTENSION_FUNCS 300
470
471
472 /*
473 * The dispatch table size (number of entries) is the size of the
474 * _glapi_table struct plus the number of dynamic entries we can add.
475 * The extra slots can be filled in by DRI drivers that register new extension
476 * functions.
477 */
478 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
479
480
481 /**
482 * Track information about a function added to the GL API.
483 */
484 struct _glapi_function {
485 /**
486 * Name of the function.
487 */
488 const char * name;
489
490
491 /**
492 * Text string that describes the types of the parameters passed to the
493 * named function. Parameter types are converted to characters using the
494 * following rules:
495 * - 'i' for \c GLint, \c GLuint, and \c GLenum
496 * - 'p' for any pointer type
497 * - 'f' for \c GLfloat and \c GLclampf
498 * - 'd' for \c GLdouble and \c GLclampd
499 */
500 const char * parameter_signature;
501
502
503 /**
504 * Offset in the dispatch table where the pointer to the real function is
505 * located. If the driver has not requested that the named function be
506 * added to the dispatch table, this will have the value ~0.
507 */
508 unsigned dispatch_offset;
509
510
511 /**
512 * Pointer to the dispatch stub for the named function.
513 *
514 * \todo
515 * The semantic of this field should be changed slightly. Currently, it
516 * is always expected to be non-\c NULL. However, it would be better to
517 * only allocate the entry-point stub when the application requests the
518 * function via \c glXGetProcAddress. This would save memory for all the
519 * functions that the driver exports but that the application never wants
520 * to call.
521 */
522 _glapi_proc dispatch_stub;
523 };
524
525
526 static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
527 static GLuint NumExtEntryPoints = 0;
528
529 #ifdef USE_SPARC_ASM
530 extern void __glapi_sparc_icache_flush(unsigned int *);
531 #endif
532
533 /**
534 * Generate a dispatch function (entrypoint) which jumps through
535 * the given slot number (offset) in the current dispatch table.
536 * We need assembly language in order to accomplish this.
537 */
538 static _glapi_proc
539 generate_entrypoint(GLuint functionOffset)
540 {
541 #if defined(USE_X86_ASM)
542 /* 32 is chosen as something of a magic offset. For x86, the dispatch
543 * at offset 32 is the first one where the offset in the
544 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
545 */
546 const GLubyte * const template_func = gl_dispatch_functions_start
547 + (X86_DISPATCH_FUNCTION_SIZE * 32);
548 GLubyte * const code = (GLubyte *) malloc( X86_DISPATCH_FUNCTION_SIZE );
549
550
551 if ( code != NULL ) {
552 (void) memcpy( code, template_func, X86_DISPATCH_FUNCTION_SIZE );
553 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
554 }
555
556 return (_glapi_proc) code;
557 #elif defined(USE_SPARC_ASM)
558
559 #ifdef __arch64__
560 static const unsigned int insn_template[] = {
561 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
562 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
563 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
564 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
565 0x8528b020, /* sllx %g2, 32, %g2 */
566 0xc2584002, /* ldx [%g1 + %g2], %g1 */
567 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
568 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
569 0xc6584002, /* ldx [%g1 + %g2], %g3 */
570 0x81c0c000, /* jmpl %g3, %g0 */
571 0x01000000 /* nop */
572 };
573 #else
574 static const unsigned int insn_template[] = {
575 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
576 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
577 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
578 0x81c0c000, /* jmpl %g3, %g0 */
579 0x01000000 /* nop */
580 };
581 #endif /* __arch64__ */
582 unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
583 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
584 if (code) {
585 memcpy(code, insn_template, sizeof(insn_template));
586
587 #ifdef __arch64__
588 code[0] |= (glapi_addr >> (32 + 10));
589 code[1] |= ((glapi_addr & 0xffffffff) >> 10);
590 __glapi_sparc_icache_flush(&code[0]);
591 code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
592 code[3] |= (glapi_addr & ((1 << 10) - 1));
593 __glapi_sparc_icache_flush(&code[2]);
594 code[6] |= ((functionOffset * 8) >> 10);
595 code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
596 __glapi_sparc_icache_flush(&code[6]);
597 #else
598 code[0] |= (glapi_addr >> 10);
599 code[1] |= (glapi_addr & ((1 << 10) - 1));
600 __glapi_sparc_icache_flush(&code[0]);
601 code[2] |= (functionOffset * 4);
602 __glapi_sparc_icache_flush(&code[2]);
603 #endif /* __arch64__ */
604 }
605 return (_glapi_proc) code;
606 #else
607 (void) functionOffset;
608 return NULL;
609 #endif /* USE_*_ASM */
610 }
611
612
613 /**
614 * This function inserts a new dispatch offset into the assembly language
615 * stub that was generated with the preceeding function.
616 */
617 static void
618 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
619 {
620 #if defined(USE_X86_ASM)
621 GLubyte * const code = (GLubyte *) entrypoint;
622
623 #if X86_DISPATCH_FUNCTION_SIZE == 32
624 *((unsigned int *)(code + 11)) = 4 * offset;
625 *((unsigned int *)(code + 22)) = 4 * offset;
626 #elif X86_DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
627 *((unsigned int *)(code + 8)) = 4 * offset;
628 #elif X86_DISPATCH_FUNCTION_SIZE == 16
629 *((unsigned int *)(code + 7)) = 4 * offset;
630 #else
631 # error Invalid X86_DISPATCH_FUNCTION_SIZE!
632 #endif
633
634 #elif defined(USE_SPARC_ASM)
635
636 /* XXX this hasn't been tested! */
637 unsigned int *code = (unsigned int *) entrypoint;
638 #ifdef __arch64__
639 code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
640 code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
641 code[6] |= ((offset * 8) >> 10);
642 code[7] |= ((offset * 8) & ((1 << 10) - 1));
643 __glapi_sparc_icache_flush(&code[6]);
644 #else /* __arch64__ */
645 code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
646 code[2] |= (offset * 4);
647 __glapi_sparc_icache_flush(&code[2]);
648 #endif /* __arch64__ */
649
650 #else
651
652 /* an unimplemented architecture */
653 (void) entrypoint;
654 (void) offset;
655
656 #endif /* USE_*_ASM */
657 }
658
659
660 /**
661 * Generate new entrypoint
662 *
663 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
664 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
665 * never happens, and the user calls this function, he'll segfault. That's
666 * what you get when you try calling a GL function that doesn't really exist.
667 *
668 * \param funcName Name of the function to create an entry-point for.
669 *
670 * \sa _glapi_add_entrypoint
671 */
672
673 static struct _glapi_function *
674 add_function_name( const char * funcName )
675 {
676 struct _glapi_function * entry = NULL;
677
678 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
679 _glapi_proc entrypoint = generate_entrypoint(~0);
680 if (entrypoint != NULL) {
681 entry = & ExtEntryTable[NumExtEntryPoints];
682
683 ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
684 ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
685 ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
686 ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
687 NumExtEntryPoints++;
688 }
689 }
690
691 return entry;
692 }
693
694
695 /**
696 * Fill-in the dispatch stub for the named function.
697 *
698 * This function is intended to be called by a hardware driver. When called,
699 * a dispatch stub may be created created for the function. A pointer to this
700 * dispatch function will be returned by glXGetProcAddress.
701 *
702 * \param function_names Array of pointers to function names that should
703 * share a common dispatch offset.
704 * \param parameter_signature String representing the types of the parameters
705 * passed to the named function. Parameter types
706 * are converted to characters using the following
707 * rules:
708 * - 'i' for \c GLint, \c GLuint, and \c GLenum
709 * - 'p' for any pointer type
710 * - 'f' for \c GLfloat and \c GLclampf
711 * - 'd' for \c GLdouble and \c GLclampd
712 *
713 * \returns
714 * The offset in the dispatch table of the named function. A pointer to the
715 * driver's implementation of the named function should be stored at
716 * \c dispatch_table[\c offset].
717 *
718 * \sa glXGetProcAddress
719 *
720 * \warning
721 * This function can only handle up to 8 names at a time. As far as I know,
722 * the maximum number of names ever associated with an existing GL function is
723 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
724 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
725 * too painful of a limitation.
726 *
727 * \todo
728 * Determine whether or not \c parameter_signature should be allowed to be
729 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
730 * pass in an empty string.
731 *
732 * \todo
733 * Determine if code should be added to reject function names that start with
734 * 'glX'.
735 *
736 * \bug
737 * Add code to compare \c parameter_signature with the parameter signature of
738 * a static function. In order to do that, we need to find a way to \b get
739 * the parameter signature of a static function.
740 */
741
742 PUBLIC int
743 _glapi_add_dispatch( const char * const * function_names,
744 const char * parameter_signature )
745 {
746 static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC;
747 const char * const real_sig = (parameter_signature != NULL)
748 ? parameter_signature : "";
749 struct _glapi_function * entry[8];
750 GLboolean is_static[8];
751 unsigned i;
752 unsigned j;
753 int offset = ~0;
754 int new_offset;
755
756
757 (void) memset( is_static, 0, sizeof( is_static ) );
758 (void) memset( entry, 0, sizeof( entry ) );
759
760 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
761 /* Do some trivial validation on the name of the function.
762 */
763
764 if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
765 return GL_FALSE;
766
767 /* Determine if the named function already exists. If the function does
768 * exist, it must have the same parameter signature as the function
769 * being added.
770 */
771
772 new_offset = get_static_proc_offset(function_names[i]);
773 if (new_offset >= 0) {
774 /* FIXME: Make sure the parameter signatures match! How do we get
775 * FIXME: the parameter signature for static functions?
776 */
777
778 if ( (offset != ~0) && (new_offset != offset) ) {
779 return -1;
780 }
781
782 is_static[i] = GL_TRUE;
783 offset = new_offset;
784 }
785
786
787 for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
788 if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
789 /* The offset may be ~0 if the function name was added by
790 * glXGetProcAddress but never filled in by the driver.
791 */
792
793 if (ExtEntryTable[j].dispatch_offset != ~0) {
794 if (strcmp(real_sig, ExtEntryTable[j].parameter_signature)
795 != 0) {
796 return -1;
797 }
798
799 if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
800 return -1;
801 }
802
803 offset = ExtEntryTable[j].dispatch_offset;
804 }
805
806 entry[i] = & ExtEntryTable[j];
807 break;
808 }
809 }
810 }
811
812 if (offset == ~0) {
813 offset = next_dynamic_offset;
814 next_dynamic_offset++;
815 }
816
817 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
818 if (! is_static[i] ) {
819 if (entry[i] == NULL) {
820 entry[i] = add_function_name( function_names[i] );
821 if (entry[i] == NULL) {
822 /* FIXME: Possible memory leak here.
823 */
824 return -1;
825 }
826 }
827
828 entry[i]->parameter_signature = str_dup(real_sig);
829 fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
830 entry[i]->dispatch_offset = offset;
831 }
832 }
833
834 return offset;
835 }
836
837
838 /**
839 * Return offset of entrypoint for named function within dispatch table.
840 */
841 PUBLIC GLint
842 _glapi_get_proc_offset(const char *funcName)
843 {
844 /* search extension functions first */
845 GLuint i;
846 for (i = 0; i < NumExtEntryPoints; i++) {
847 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
848 return ExtEntryTable[i].dispatch_offset;
849 }
850 }
851 /* search static functions */
852 return get_static_proc_offset(funcName);
853 }
854
855
856
857 /**
858 * Return pointer to the named function. If the function name isn't found
859 * in the name of static functions, try generating a new API entrypoint on
860 * the fly with assembly language.
861 */
862 _glapi_proc
863 _glapi_get_proc_address(const char *funcName)
864 {
865 struct _glapi_function * entry;
866 GLuint i;
867
868 #ifdef MANGLE
869 if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
870 return NULL;
871 #else
872 if (funcName[0] != 'g' || funcName[1] != 'l')
873 return NULL;
874 #endif
875
876 /* search extension functions first */
877 for (i = 0; i < NumExtEntryPoints; i++) {
878 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
879 return ExtEntryTable[i].dispatch_stub;
880 }
881 }
882
883 #if !defined( XFree86Server ) && !defined( XGLServer )
884 /* search static functions */
885 {
886 const _glapi_proc func = get_static_proc_address(funcName);
887 if (func)
888 return func;
889 }
890 #endif /* !defined( XFree86Server ) */
891
892 entry = add_function_name(funcName);
893 return (entry == NULL) ? NULL : entry->dispatch_stub;
894 }
895
896
897
898 /**
899 * Return the name of the function at the given dispatch offset.
900 * This is only intended for debugging.
901 */
902 const char *
903 _glapi_get_proc_name(GLuint offset)
904 {
905 GLuint i;
906 const char * n;
907
908 /* search built-in functions */
909 n = get_static_proc_name(offset);
910 if ( n != NULL ) {
911 return n;
912 }
913
914 /* search added extension functions */
915 for (i = 0; i < NumExtEntryPoints; i++) {
916 if (ExtEntryTable[i].dispatch_offset == offset) {
917 return ExtEntryTable[i].name;
918 }
919 }
920 return NULL;
921 }
922
923
924
925 /**
926 * Return size of dispatch table struct as number of functions (or
927 * slots).
928 */
929 PUBLIC GLuint
930 _glapi_get_dispatch_table_size(void)
931 {
932 return DISPATCH_TABLE_SIZE;
933 }
934
935
936
937 /**
938 * Make sure there are no NULL pointers in the given dispatch table.
939 * Intended for debugging purposes.
940 */
941 void
942 _glapi_check_table(const struct _glapi_table *table)
943 {
944 #ifdef DEBUG
945 const GLuint entries = _glapi_get_dispatch_table_size();
946 const void **tab = (const void **) table;
947 GLuint i;
948 for (i = 1; i < entries; i++) {
949 assert(tab[i]);
950 }
951
952 /* Do some spot checks to be sure that the dispatch table
953 * slots are assigned correctly.
954 */
955 {
956 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
957 char *BeginFunc = (char*) &table->Begin;
958 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
959 assert(BeginOffset == _gloffset_Begin);
960 assert(BeginOffset == offset);
961 }
962 {
963 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
964 char *viewportFunc = (char*) &table->Viewport;
965 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
966 assert(viewportOffset == _gloffset_Viewport);
967 assert(viewportOffset == offset);
968 }
969 {
970 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
971 char *VertexPointerFunc = (char*) &table->VertexPointer;
972 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
973 assert(VertexPointerOffset == _gloffset_VertexPointer);
974 assert(VertexPointerOffset == offset);
975 }
976 {
977 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
978 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
979 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
980 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
981 assert(ResetMinMaxOffset == offset);
982 }
983 {
984 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
985 char *blendColorFunc = (char*) &table->BlendColor;
986 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
987 assert(blendColorOffset == _gloffset_BlendColor);
988 assert(blendColorOffset == offset);
989 }
990 {
991 GLuint istextureOffset = _glapi_get_proc_offset("glIsTextureEXT");
992 char *istextureFunc = (char*) &table->IsTextureEXT;
993 GLuint offset = (istextureFunc - (char *) table) / sizeof(void *);
994 assert(istextureOffset == _gloffset_IsTextureEXT);
995 assert(istextureOffset == offset);
996 }
997 {
998 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
999 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
1000 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
1001 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
1002 assert(secondaryColor3fOffset == offset);
1003 assert(_glapi_get_proc_address("glSecondaryColor3fEXT") == (_glapi_proc) &glSecondaryColor3fEXT);
1004 }
1005 {
1006 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
1007 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
1008 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
1009 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
1010 assert(pointParameterivOffset == offset);
1011 assert(_glapi_get_proc_address("glPointParameterivNV") == (_glapi_proc) &glPointParameterivNV);
1012 }
1013 {
1014 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
1015 char *setFenceFunc = (char*) &table->SetFenceNV;
1016 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
1017 assert(setFenceOffset == _gloffset_SetFenceNV);
1018 assert(setFenceOffset == offset);
1019 assert(_glapi_get_proc_address("glSetFenceNV") == (_glapi_proc) &glSetFenceNV);
1020 }
1021 #else
1022 (void) table;
1023 #endif
1024 }
1025
1026
1027 #if defined(PTHREADS) || defined(GLX_USE_TLS)
1028 /**
1029 * Perform platform-specific GL API entry-point fixups.
1030 *
1031 *
1032 */
1033 static void
1034 init_glapi_relocs( void )
1035 {
1036 #if defined( USE_X86_ASM ) && defined( GLX_USE_TLS )
1037 extern void * _x86_get_dispatch(void);
1038 const GLubyte * const get_disp = (const GLubyte *) _x86_get_dispatch;
1039 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
1040
1041
1042 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
1043 (void) memcpy( curr_func, get_disp, 6 );
1044 curr_func += X86_DISPATCH_FUNCTION_SIZE;
1045 }
1046 #endif /* defined( USE_X86_ASM ) && defined( GLX_USE_TLS ) */
1047 }
1048 #endif