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