Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / mapi / glapi / glapi_getproc.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 * \file glapi_getproc.c
27 *
28 * Code for implementing glXGetProcAddress(), etc.
29 * This was originally in glapi.c but refactored out.
30 */
31
32
33 #include "glapi/glapi_priv.h"
34 #include "glapi/glapitable.h"
35
36
37 #define FIRST_DYNAMIC_OFFSET (sizeof(struct _glapi_table) / sizeof(void *))
38
39
40 /**********************************************************************
41 * Static function management.
42 */
43
44
45 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server)
46 # define NEED_FUNCTION_POINTER
47 #endif
48 #include "glapi/glprocs.h"
49
50
51 /**
52 * Search the table of static entrypoint functions for the named function
53 * and return the corresponding glprocs_table_t entry.
54 */
55 static const glprocs_table_t *
56 get_static_proc( const char * n )
57 {
58 GLuint i;
59 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
60 const char *testName = gl_string_table + static_functions[i].Name_offset;
61 #ifdef MANGLE
62 /* skip the prefix on the name */
63 if (strcmp(testName, n + 1) == 0)
64 #else
65 if (strcmp(testName, n) == 0)
66 #endif
67 {
68 return &static_functions[i];
69 }
70 }
71 return NULL;
72 }
73
74
75 /**
76 * Return dispatch table offset of the named static (built-in) function.
77 * Return -1 if function not found.
78 */
79 static GLint
80 get_static_proc_offset(const char *funcName)
81 {
82 const glprocs_table_t * const f = get_static_proc( funcName );
83 if (f == NULL) {
84 return -1;
85 }
86
87 return f->Offset;
88 }
89
90
91 #if !defined(XFree86Server)
92
93 /**
94 * Return dispatch function address for the named static (built-in) function.
95 * Return NULL if function not found.
96 */
97 static _glapi_proc
98 get_static_proc_address(const char *funcName)
99 {
100 const glprocs_table_t * const f = get_static_proc( funcName );
101 if (f == NULL) {
102 return NULL;
103 }
104
105 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
106 return (f->Address == NULL)
107 ? get_entrypoint_address(f->Offset)
108 : f->Address;
109 #elif defined(DISPATCH_FUNCTION_SIZE)
110 return get_entrypoint_address(f->Offset);
111 #else
112 return f->Address;
113 #endif
114 }
115
116 #else
117
118 static _glapi_proc
119 get_static_proc_address(const char *funcName)
120 {
121 (void) funcName;
122 return NULL;
123 }
124
125 #endif /* !defined(XFree86Server) */
126
127
128 /**
129 * Return the name of the function at the given offset in the dispatch
130 * table. For debugging only.
131 */
132 static const char *
133 get_static_proc_name( GLuint offset )
134 {
135 GLuint i;
136 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
137 if (static_functions[i].Offset == offset) {
138 return gl_string_table + static_functions[i].Name_offset;
139 }
140 }
141 return NULL;
142 }
143
144
145
146 /**********************************************************************
147 * Extension function management.
148 */
149
150
151 /**
152 * Track information about a function added to the GL API.
153 */
154 struct _glapi_function {
155 /**
156 * Name of the function.
157 */
158 const char * name;
159
160
161 /**
162 * Text string that describes the types of the parameters passed to the
163 * named function. Parameter types are converted to characters using the
164 * following rules:
165 * - 'i' for \c GLint, \c GLuint, and \c GLenum
166 * - 'p' for any pointer type
167 * - 'f' for \c GLfloat and \c GLclampf
168 * - 'd' for \c GLdouble and \c GLclampd
169 */
170 const char * parameter_signature;
171
172
173 /**
174 * Offset in the dispatch table where the pointer to the real function is
175 * located. If the driver has not requested that the named function be
176 * added to the dispatch table, this will have the value ~0.
177 */
178 unsigned dispatch_offset;
179
180
181 /**
182 * Pointer to the dispatch stub for the named function.
183 *
184 * \todo
185 * The semantic of this field should be changed slightly. Currently, it
186 * is always expected to be non-\c NULL. However, it would be better to
187 * only allocate the entry-point stub when the application requests the
188 * function via \c glXGetProcAddress. This would save memory for all the
189 * functions that the driver exports but that the application never wants
190 * to call.
191 */
192 _glapi_proc dispatch_stub;
193 };
194
195
196 static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
197 static GLuint NumExtEntryPoints = 0;
198
199
200 static struct _glapi_function *
201 get_extension_proc(const char *funcName)
202 {
203 GLuint i;
204 for (i = 0; i < NumExtEntryPoints; i++) {
205 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
206 return & ExtEntryTable[i];
207 }
208 }
209 return NULL;
210 }
211
212
213 static GLint
214 get_extension_proc_offset(const char *funcName)
215 {
216 const struct _glapi_function * const f = get_extension_proc( funcName );
217 if (f == NULL) {
218 return -1;
219 }
220
221 return f->dispatch_offset;
222 }
223
224
225 static _glapi_proc
226 get_extension_proc_address(const char *funcName)
227 {
228 const struct _glapi_function * const f = get_extension_proc( funcName );
229 if (f == NULL) {
230 return NULL;
231 }
232
233 return f->dispatch_stub;
234 }
235
236
237 static const char *
238 get_extension_proc_name(GLuint offset)
239 {
240 GLuint i;
241 for (i = 0; i < NumExtEntryPoints; i++) {
242 if (ExtEntryTable[i].dispatch_offset == offset) {
243 return ExtEntryTable[i].name;
244 }
245 }
246 return NULL;
247 }
248
249
250 /**
251 * strdup() is actually not a standard ANSI C or POSIX routine.
252 * Irix will not define it if ANSI mode is in effect.
253 */
254 static char *
255 str_dup(const char *str)
256 {
257 char *copy;
258 copy = (char*) malloc(strlen(str) + 1);
259 if (!copy)
260 return NULL;
261 strcpy(copy, str);
262 return copy;
263 }
264
265
266 /**
267 * Generate new entrypoint
268 *
269 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
270 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
271 * never happens, and the user calls this function, he'll segfault. That's
272 * what you get when you try calling a GL function that doesn't really exist.
273 *
274 * \param funcName Name of the function to create an entry-point for.
275 *
276 * \sa _glapi_add_entrypoint
277 */
278
279 static struct _glapi_function *
280 add_function_name( const char * funcName )
281 {
282 struct _glapi_function * entry = NULL;
283 _glapi_proc entrypoint = NULL;
284 char * name_dup = NULL;
285
286 if (NumExtEntryPoints >= MAX_EXTENSION_FUNCS)
287 return NULL;
288
289 if (funcName == NULL)
290 return NULL;
291
292 name_dup = str_dup(funcName);
293 if (name_dup == NULL)
294 return NULL;
295
296 entrypoint = generate_entrypoint(~0);
297
298 if (entrypoint == NULL) {
299 free(name_dup);
300 return NULL;
301 }
302
303 entry = & ExtEntryTable[NumExtEntryPoints];
304 NumExtEntryPoints++;
305
306 entry->name = name_dup;
307 entry->parameter_signature = NULL;
308 entry->dispatch_offset = ~0;
309 entry->dispatch_stub = entrypoint;
310
311 return entry;
312 }
313
314
315 static struct _glapi_function *
316 set_entry_info( struct _glapi_function * entry, const char * signature, unsigned offset )
317 {
318 char * sig_dup = NULL;
319
320 if (signature == NULL)
321 return NULL;
322
323 sig_dup = str_dup(signature);
324 if (sig_dup == NULL)
325 return NULL;
326
327 fill_in_entrypoint_offset(entry->dispatch_stub, offset);
328
329 entry->parameter_signature = sig_dup;
330 entry->dispatch_offset = offset;
331
332 return entry;
333 }
334
335
336 /**
337 * Fill-in the dispatch stub for the named function.
338 *
339 * This function is intended to be called by a hardware driver. When called,
340 * a dispatch stub may be created created for the function. A pointer to this
341 * dispatch function will be returned by glXGetProcAddress.
342 *
343 * \param function_names Array of pointers to function names that should
344 * share a common dispatch offset.
345 * \param parameter_signature String representing the types of the parameters
346 * passed to the named function. Parameter types
347 * are converted to characters using the following
348 * rules:
349 * - 'i' for \c GLint, \c GLuint, and \c GLenum
350 * - 'p' for any pointer type
351 * - 'f' for \c GLfloat and \c GLclampf
352 * - 'd' for \c GLdouble and \c GLclampd
353 *
354 * \returns
355 * The offset in the dispatch table of the named function. A pointer to the
356 * driver's implementation of the named function should be stored at
357 * \c dispatch_table[\c offset]. Return -1 if error/problem.
358 *
359 * \sa glXGetProcAddress
360 *
361 * \warning
362 * This function can only handle up to 8 names at a time. As far as I know,
363 * the maximum number of names ever associated with an existing GL function is
364 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
365 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
366 * too painful of a limitation.
367 *
368 * \todo
369 * Determine whether or not \c parameter_signature should be allowed to be
370 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
371 * pass in an empty string.
372 *
373 * \todo
374 * Determine if code should be added to reject function names that start with
375 * 'glX'.
376 *
377 * \bug
378 * Add code to compare \c parameter_signature with the parameter signature of
379 * a static function. In order to do that, we need to find a way to \b get
380 * the parameter signature of a static function.
381 */
382
383 int
384 _glapi_add_dispatch( const char * const * function_names,
385 const char * parameter_signature )
386 {
387 static int next_dynamic_offset = FIRST_DYNAMIC_OFFSET;
388 const char * const real_sig = (parameter_signature != NULL)
389 ? parameter_signature : "";
390 struct _glapi_function * entry[8];
391 GLboolean is_static[8];
392 unsigned i;
393 int offset = ~0;
394
395 init_glapi_relocs_once();
396
397 (void) memset( is_static, 0, sizeof( is_static ) );
398 (void) memset( entry, 0, sizeof( entry ) );
399
400 /* Find the _single_ dispatch offset for all function names that already
401 * exist (and have a dispatch offset).
402 */
403
404 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
405 const char * funcName = function_names[i];
406 int static_offset;
407 int extension_offset;
408
409 if (funcName[0] != 'g' || funcName[1] != 'l')
410 return -1;
411
412 /* search built-in functions */
413 static_offset = get_static_proc_offset(funcName);
414
415 if (static_offset >= 0) {
416
417 is_static[i] = GL_TRUE;
418
419 /* FIXME: Make sure the parameter signatures match! How do we get
420 * FIXME: the parameter signature for static functions?
421 */
422
423 if ( (offset != ~0) && (static_offset != offset) ) {
424 return -1;
425 }
426
427 offset = static_offset;
428
429 continue;
430 }
431
432 /* search added extension functions */
433 entry[i] = get_extension_proc(funcName);
434
435 if (entry[i] != NULL) {
436 extension_offset = entry[i]->dispatch_offset;
437
438 /* The offset may be ~0 if the function name was added by
439 * glXGetProcAddress but never filled in by the driver.
440 */
441
442 if (extension_offset == ~0) {
443 continue;
444 }
445
446 if (strcmp(real_sig, entry[i]->parameter_signature) != 0) {
447 return -1;
448 }
449
450 if ( (offset != ~0) && (extension_offset != offset) ) {
451 return -1;
452 }
453
454 offset = extension_offset;
455 }
456 }
457
458 /* If all function names are either new (or with no dispatch offset),
459 * allocate a new dispatch offset.
460 */
461
462 if (offset == ~0) {
463 offset = next_dynamic_offset;
464 next_dynamic_offset++;
465 }
466
467 /* Fill in the dispatch offset for the new function names (and those with
468 * no dispatch offset).
469 */
470
471 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
472 if (is_static[i]) {
473 continue;
474 }
475
476 /* generate entrypoints for new function names */
477 if (entry[i] == NULL) {
478 entry[i] = add_function_name( function_names[i] );
479 if (entry[i] == NULL) {
480 /* FIXME: Possible memory leak here. */
481 return -1;
482 }
483 }
484
485 if (entry[i]->dispatch_offset == ~0) {
486 set_entry_info( entry[i], real_sig, offset );
487 }
488 }
489
490 return offset;
491 }
492
493
494 /**
495 * Return offset of entrypoint for named function within dispatch table.
496 */
497 GLint
498 _glapi_get_proc_offset(const char *funcName)
499 {
500 GLint offset;
501
502 /* search extension functions first */
503 offset = get_extension_proc_offset(funcName);
504 if (offset >= 0)
505 return offset;
506
507 /* search static functions */
508 return get_static_proc_offset(funcName);
509 }
510
511
512
513 /**
514 * Return pointer to the named function. If the function name isn't found
515 * in the name of static functions, try generating a new API entrypoint on
516 * the fly with assembly language.
517 */
518 _glapi_proc
519 _glapi_get_proc_address(const char *funcName)
520 {
521 _glapi_proc func;
522 struct _glapi_function * entry;
523
524 init_glapi_relocs_once();
525
526 #ifdef MANGLE
527 /* skip the prefix on the name */
528 if (funcName[1] != 'g' || funcName[2] != 'l')
529 return NULL;
530 #else
531 if (funcName[0] != 'g' || funcName[1] != 'l')
532 return NULL;
533 #endif
534
535 /* search extension functions first */
536 func = get_extension_proc_address(funcName);
537 if (func)
538 return func;
539
540 /* search static functions */
541 func = get_static_proc_address(funcName);
542 if (func)
543 return func;
544
545 /* generate entrypoint, dispatch offset must be filled in by the driver */
546 entry = add_function_name(funcName);
547 if (entry == NULL)
548 return NULL;
549
550 return entry->dispatch_stub;
551 }
552
553
554
555 /**
556 * Return the name of the function at the given dispatch offset.
557 * This is only intended for debugging.
558 */
559 const char *
560 _glapi_get_proc_name(GLuint offset)
561 {
562 const char * n;
563
564 /* search built-in functions */
565 n = get_static_proc_name(offset);
566 if ( n != NULL ) {
567 return n;
568 }
569
570 /* search added extension functions */
571 return get_extension_proc_name(offset);
572 }
573
574
575
576 /**********************************************************************
577 * GL API table functions.
578 */
579
580
581 /**
582 * Return size of dispatch table struct as number of functions (or
583 * slots).
584 */
585 GLuint
586 _glapi_get_dispatch_table_size(void)
587 {
588 /*
589 * The dispatch table size (number of entries) is the size of the
590 * _glapi_table struct plus the number of dynamic entries we can add.
591 * The extra slots can be filled in by DRI drivers that register new
592 * extension functions.
593 */
594 return FIRST_DYNAMIC_OFFSET + MAX_EXTENSION_FUNCS;
595 }
596
597
598 /**
599 * Make sure there are no NULL pointers in the given dispatch table.
600 * Intended for debugging purposes.
601 */
602 void
603 _glapi_check_table_not_null(const struct _glapi_table *table)
604 {
605 #ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
606 const GLuint entries = _glapi_get_dispatch_table_size();
607 const void **tab = (const void **) table;
608 GLuint i;
609 for (i = 1; i < entries; i++) {
610 assert(tab[i]);
611 }
612 #else
613 (void) table;
614 #endif
615 }
616
617
618 /**
619 * Do some spot checks to be sure that the dispatch table
620 * slots are assigned correctly. For debugging only.
621 */
622 void
623 _glapi_check_table(const struct _glapi_table *table)
624 {
625 #ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
626 {
627 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
628 char *BeginFunc = (char*) &table->Begin;
629 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
630 assert(BeginOffset == offset);
631 }
632 {
633 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
634 char *viewportFunc = (char*) &table->Viewport;
635 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
636 assert(viewportOffset == offset);
637 }
638 {
639 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
640 char *VertexPointerFunc = (char*) &table->VertexPointer;
641 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
642 assert(VertexPointerOffset == offset);
643 }
644 {
645 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
646 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
647 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
648 assert(ResetMinMaxOffset == offset);
649 }
650 {
651 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
652 char *blendColorFunc = (char*) &table->BlendColor;
653 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
654 assert(blendColorOffset == offset);
655 }
656 {
657 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
658 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
659 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
660 assert(secondaryColor3fOffset == offset);
661 }
662 {
663 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
664 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
665 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
666 assert(pointParameterivOffset == offset);
667 }
668 {
669 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
670 char *setFenceFunc = (char*) &table->SetFenceNV;
671 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
672 assert(setFenceOffset == offset);
673 }
674 #else
675 (void) table;
676 #endif
677 }