intel: Remove the remaining cliprects code from DRI1.
[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 "main/compiler.h"
65
66 #include <stdlib.h>
67 #include <string.h>
68 #ifdef DEBUG
69 #include <assert.h>
70 #endif
71
72 #include "glapi/glapi.h"
73 #include "glapi/glapioffsets.h"
74 #include "glapi/glapitable.h"
75
76 extern _glapi_proc __glapi_noop_table[];
77
78
79 /**
80 * \name Current dispatch and current context control variables
81 *
82 * Depending on whether or not multithreading is support, and the type of
83 * support available, several variables are used to store the current context
84 * pointer and the current dispatch table pointer. In the non-threaded case,
85 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
86 * purpose.
87 *
88 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
89 * \c _glapi_Context will be \c NULL if an application is detected as being
90 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
91 * and \c _glapi_Context just like the case without any threading support.
92 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
93 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
94 * static dispatch functions access these variables via \c _glapi_get_dispatch
95 * and \c _glapi_get_context.
96 *
97 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
98 * possible for the original thread to be setting it at the same instant a new
99 * thread, perhaps running on a different processor, is clearing it. Because
100 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
101 * used to determine whether or not the application is multithreaded.
102 *
103 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
104 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
105 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
106 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
107 * between TLS enabled loaders and non-TLS DRI drivers.
108 */
109 /*@{*/
110 #if defined(GLX_USE_TLS)
111
112 PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
113 __attribute__((tls_model("initial-exec")))
114 = (struct _glapi_table *) __glapi_noop_table;
115
116 PUBLIC __thread void * _glapi_tls_Context
117 __attribute__((tls_model("initial-exec")));
118
119 PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
120 PUBLIC const void *_glapi_Context = NULL;
121
122 #else
123
124 #if defined(THREADS)
125
126 #ifdef WIN32_THREADS
127 /* _glthread_DECLARE_STATIC_MUTEX is broken on windows. There will be race! */
128 #define CHECK_MULTITHREAD_LOCK()
129 #define CHECK_MULTITHREAD_UNLOCK()
130 #else
131 _glthread_DECLARE_STATIC_MUTEX(ThreadCheckMutex);
132 #define CHECK_MULTITHREAD_LOCK() _glthread_LOCK_MUTEX(ThreadCheckMutex)
133 #define CHECK_MULTITHREAD_UNLOCK() _glthread_UNLOCK_MUTEX(ThreadCheckMutex)
134 #endif
135
136 static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
137 _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
138 static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
139
140 #if defined(WIN32_THREADS)
141 void FreeTSD(_glthread_TSD *p);
142 void FreeAllTSD(void)
143 {
144 FreeTSD(&_gl_DispatchTSD);
145 FreeTSD(&ContextTSD);
146 }
147 #endif /* defined(WIN32_THREADS) */
148
149 #endif /* defined(THREADS) */
150
151 PUBLIC struct _glapi_table *_glapi_Dispatch =
152 (struct _glapi_table *) __glapi_noop_table;
153 PUBLIC void *_glapi_Context = NULL;
154
155 #endif /* defined(GLX_USE_TLS) */
156 /*@}*/
157
158
159
160
161 /**
162 * We should call this periodically from a function such as glXMakeCurrent
163 * in order to test if multiple threads are being used.
164 */
165 PUBLIC void
166 _glapi_check_multithread(void)
167 {
168 #if defined(THREADS) && !defined(GLX_USE_TLS)
169 static unsigned long knownID;
170 static GLboolean firstCall = GL_TRUE;
171
172 if (ThreadSafe)
173 return;
174
175 CHECK_MULTITHREAD_LOCK();
176 if (firstCall) {
177 /* initialize TSDs */
178 (void) _glthread_GetTSD(&ContextTSD);
179 (void) _glthread_GetTSD(&_gl_DispatchTSD);
180
181 knownID = _glthread_GetID();
182 firstCall = GL_FALSE;
183 }
184 else if (knownID != _glthread_GetID()) {
185 ThreadSafe = GL_TRUE;
186 _glapi_set_dispatch(NULL);
187 _glapi_set_context(NULL);
188 }
189 CHECK_MULTITHREAD_UNLOCK();
190 #endif
191 }
192
193
194
195 /**
196 * Set the current context pointer for this thread.
197 * The context pointer is an opaque type which should be cast to
198 * void from the real context pointer type.
199 */
200 PUBLIC void
201 _glapi_set_context(void *context)
202 {
203 #if defined(GLX_USE_TLS)
204 _glapi_tls_Context = context;
205 #elif defined(THREADS)
206 _glthread_SetTSD(&ContextTSD, context);
207 _glapi_Context = (ThreadSafe) ? NULL : context;
208 #else
209 _glapi_Context = context;
210 #endif
211 }
212
213
214
215 /**
216 * Get the current context pointer for this thread.
217 * The context pointer is an opaque type which should be cast from
218 * void to the real context pointer type.
219 */
220 PUBLIC void *
221 _glapi_get_context(void)
222 {
223 #if defined(GLX_USE_TLS)
224 return _glapi_tls_Context;
225 #elif defined(THREADS)
226 if (ThreadSafe) {
227 return _glthread_GetTSD(&ContextTSD);
228 }
229 else {
230 return _glapi_Context;
231 }
232 #else
233 return _glapi_Context;
234 #endif
235 }
236
237 #ifdef USE_X86_ASM
238
239 #if defined( GLX_USE_TLS )
240 extern GLubyte gl_dispatch_functions_start[];
241 extern GLubyte gl_dispatch_functions_end[];
242 #else
243 extern const GLubyte gl_dispatch_functions_start[];
244 #endif
245
246 #endif /* USE_X86_ASM */
247
248
249 #if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
250 # define DISPATCH_FUNCTION_SIZE 16
251 #elif defined(USE_X86_ASM)
252 # if defined(THREADS) && !defined(GLX_USE_TLS)
253 # define DISPATCH_FUNCTION_SIZE 32
254 # else
255 # define DISPATCH_FUNCTION_SIZE 16
256 # endif
257 #endif
258
259 #ifdef USE_SPARC_ASM
260 #ifdef GLX_USE_TLS
261 extern unsigned int __glapi_sparc_tls_stub;
262 #else
263 extern unsigned int __glapi_sparc_pthread_stub;
264 #endif
265 #endif
266
267 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
268 # define NEED_FUNCTION_POINTER
269 #endif
270
271 #if defined(PTHREADS) || defined(GLX_USE_TLS)
272 /**
273 * Perform platform-specific GL API entry-point fixups.
274 */
275 static void
276 init_glapi_relocs( void )
277 {
278 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
279 extern unsigned long _x86_get_dispatch(void);
280 char run_time_patch[] = {
281 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
282 };
283 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
284 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
285 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
286
287 *offset = _x86_get_dispatch();
288 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
289 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
290 curr_func += DISPATCH_FUNCTION_SIZE;
291 }
292 #endif
293 #ifdef USE_SPARC_ASM
294 extern void __glapi_sparc_icache_flush(unsigned int *);
295 static const unsigned int template[] = {
296 #ifdef GLX_USE_TLS
297 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
298 0x8730e00a, /* srl %g3, 10, %g3 */
299 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
300 #ifdef __arch64__
301 0xc259c002, /* ldx [%g7 + %g2], %g1 */
302 0xc2584003, /* ldx [%g1 + %g3], %g1 */
303 #else
304 0xc201c002, /* ld [%g7 + %g2], %g1 */
305 0xc2004003, /* ld [%g1 + %g3], %g1 */
306 #endif
307 0x81c04000, /* jmp %g1 */
308 0x01000000, /* nop */
309 #else
310 #ifdef __arch64__
311 0x03000000, /* 64-bit 0x00 --> sethi %hh(_glapi_Dispatch), %g1 */
312 0x05000000, /* 64-bit 0x04 --> sethi %lm(_glapi_Dispatch), %g2 */
313 0x82106000, /* 64-bit 0x08 --> or %g1, %hm(_glapi_Dispatch), %g1 */
314 0x8730e00a, /* 64-bit 0x0c --> srl %g3, 10, %g3 */
315 0x83287020, /* 64-bit 0x10 --> sllx %g1, 32, %g1 */
316 0x82004002, /* 64-bit 0x14 --> add %g1, %g2, %g1 */
317 0xc2586000, /* 64-bit 0x18 --> ldx [%g1 + %lo(_glapi_Dispatch)], %g1 */
318 #else
319 0x03000000, /* 32-bit 0x00 --> sethi %hi(_glapi_Dispatch), %g1 */
320 0x8730e00a, /* 32-bit 0x04 --> srl %g3, 10, %g3 */
321 0xc2006000, /* 32-bit 0x08 --> ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
322 #endif
323 0x80a06000, /* --> cmp %g1, 0 */
324 0x02800005, /* --> be +4*5 */
325 0x01000000, /* --> nop */
326 #ifdef __arch64__
327 0xc2584003, /* 64-bit --> ldx [%g1 + %g3], %g1 */
328 #else
329 0xc2004003, /* 32-bit --> ld [%g1 + %g3], %g1 */
330 #endif
331 0x81c04000, /* --> jmp %g1 */
332 0x01000000, /* --> nop */
333 #ifdef __arch64__
334 0x9de3bf80, /* 64-bit --> save %sp, -128, %sp */
335 #else
336 0x9de3bfc0, /* 32-bit --> save %sp, -64, %sp */
337 #endif
338 0xa0100003, /* --> mov %g3, %l0 */
339 0x40000000, /* --> call _glapi_get_dispatch */
340 0x01000000, /* --> nop */
341 0x82100008, /* --> mov %o0, %g1 */
342 0x86100010, /* --> mov %l0, %g3 */
343 0x10bffff7, /* --> ba -4*9 */
344 0x81e80000, /* --> restore */
345 #endif
346 };
347 #ifdef GLX_USE_TLS
348 extern unsigned long __glapi_sparc_get_dispatch(void);
349 unsigned int *code = &__glapi_sparc_tls_stub;
350 unsigned long dispatch = __glapi_sparc_get_dispatch();
351 #else
352 unsigned int *code = &__glapi_sparc_pthread_stub;
353 unsigned long dispatch = (unsigned long) &_glapi_Dispatch;
354 unsigned long call_dest = (unsigned long ) &_glapi_get_dispatch;
355 int idx;
356 #endif
357
358 #if defined(GLX_USE_TLS)
359 code[0] = template[0] | (dispatch >> 10);
360 code[1] = template[1];
361 __glapi_sparc_icache_flush(&code[0]);
362 code[2] = template[2] | (dispatch & 0x3ff);
363 code[3] = template[3];
364 __glapi_sparc_icache_flush(&code[2]);
365 code[4] = template[4];
366 code[5] = template[5];
367 __glapi_sparc_icache_flush(&code[4]);
368 code[6] = template[6];
369 __glapi_sparc_icache_flush(&code[6]);
370 #else
371 #if defined(__arch64__)
372 code[0] = template[0] | (dispatch >> (32 + 10));
373 code[1] = template[1] | ((dispatch & 0xffffffff) >> 10);
374 __glapi_sparc_icache_flush(&code[0]);
375 code[2] = template[2] | ((dispatch >> 32) & 0x3ff);
376 code[3] = template[3];
377 __glapi_sparc_icache_flush(&code[2]);
378 code[4] = template[4];
379 code[5] = template[5];
380 __glapi_sparc_icache_flush(&code[4]);
381 code[6] = template[6] | (dispatch & 0x3ff);
382 idx = 7;
383 #else
384 code[0] = template[0] | (dispatch >> 10);
385 code[1] = template[1];
386 __glapi_sparc_icache_flush(&code[0]);
387 code[2] = template[2] | (dispatch & 0x3ff);
388 idx = 3;
389 #endif
390 code[idx + 0] = template[idx + 0];
391 __glapi_sparc_icache_flush(&code[idx - 1]);
392 code[idx + 1] = template[idx + 1];
393 code[idx + 2] = template[idx + 2];
394 __glapi_sparc_icache_flush(&code[idx + 1]);
395 code[idx + 3] = template[idx + 3];
396 code[idx + 4] = template[idx + 4];
397 __glapi_sparc_icache_flush(&code[idx + 3]);
398 code[idx + 5] = template[idx + 5];
399 code[idx + 6] = template[idx + 6];
400 __glapi_sparc_icache_flush(&code[idx + 5]);
401 code[idx + 7] = template[idx + 7];
402 code[idx + 8] = template[idx + 8] |
403 (((call_dest - ((unsigned long) &code[idx + 8]))
404 >> 2) & 0x3fffffff);
405 __glapi_sparc_icache_flush(&code[idx + 7]);
406 code[idx + 9] = template[idx + 9];
407 code[idx + 10] = template[idx + 10];
408 __glapi_sparc_icache_flush(&code[idx + 9]);
409 code[idx + 11] = template[idx + 11];
410 code[idx + 12] = template[idx + 12];
411 __glapi_sparc_icache_flush(&code[idx + 11]);
412 code[idx + 13] = template[idx + 13];
413 __glapi_sparc_icache_flush(&code[idx + 13]);
414 #endif
415 #endif
416 }
417 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
418
419
420 /**
421 * Set the global or per-thread dispatch table pointer.
422 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
423 * table (__glapi_noop_table).
424 */
425 PUBLIC void
426 _glapi_set_dispatch(struct _glapi_table *dispatch)
427 {
428 #if defined(PTHREADS) || defined(GLX_USE_TLS)
429 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
430 pthread_once( & once_control, init_glapi_relocs );
431 #endif
432
433 if (!dispatch) {
434 /* use the no-op functions */
435 dispatch = (struct _glapi_table *) __glapi_noop_table;
436 }
437 #ifdef DEBUG
438 else {
439 _glapi_check_table(dispatch);
440 }
441 #endif
442
443 #if defined(GLX_USE_TLS)
444 _glapi_tls_Dispatch = dispatch;
445 #elif defined(THREADS)
446 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
447 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
448 #else /*THREADS*/
449 _glapi_Dispatch = dispatch;
450 #endif /*THREADS*/
451 }
452
453
454
455 /**
456 * Return pointer to current dispatch table for calling thread.
457 */
458 PUBLIC struct _glapi_table *
459 _glapi_get_dispatch(void)
460 {
461 struct _glapi_table * api;
462 #if defined(GLX_USE_TLS)
463 api = _glapi_tls_Dispatch;
464 #elif defined(THREADS)
465 api = (ThreadSafe)
466 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
467 : _glapi_Dispatch;
468 #else
469 api = _glapi_Dispatch;
470 #endif
471 return api;
472 }
473
474
475
476
477 /*
478 * The dispatch table size (number of entries) is the size of the
479 * _glapi_table struct plus the number of dynamic entries we can add.
480 * The extra slots can be filled in by DRI drivers that register new extension
481 * functions.
482 */
483 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
484
485
486 /**
487 * Return size of dispatch table struct as number of functions (or
488 * slots).
489 */
490 PUBLIC GLuint
491 _glapi_get_dispatch_table_size(void)
492 {
493 return DISPATCH_TABLE_SIZE;
494 }
495
496
497 /**
498 * Make sure there are no NULL pointers in the given dispatch table.
499 * Intended for debugging purposes.
500 */
501 void
502 _glapi_check_table(const struct _glapi_table *table)
503 {
504 #ifdef EXTRA_DEBUG
505 const GLuint entries = _glapi_get_dispatch_table_size();
506 const void **tab = (const void **) table;
507 GLuint i;
508 for (i = 1; i < entries; i++) {
509 assert(tab[i]);
510 }
511
512 /* Do some spot checks to be sure that the dispatch table
513 * slots are assigned correctly.
514 */
515 {
516 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
517 char *BeginFunc = (char*) &table->Begin;
518 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
519 assert(BeginOffset == _gloffset_Begin);
520 assert(BeginOffset == offset);
521 }
522 {
523 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
524 char *viewportFunc = (char*) &table->Viewport;
525 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
526 assert(viewportOffset == _gloffset_Viewport);
527 assert(viewportOffset == offset);
528 }
529 {
530 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
531 char *VertexPointerFunc = (char*) &table->VertexPointer;
532 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
533 assert(VertexPointerOffset == _gloffset_VertexPointer);
534 assert(VertexPointerOffset == offset);
535 }
536 {
537 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
538 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
539 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
540 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
541 assert(ResetMinMaxOffset == offset);
542 }
543 {
544 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
545 char *blendColorFunc = (char*) &table->BlendColor;
546 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
547 assert(blendColorOffset == _gloffset_BlendColor);
548 assert(blendColorOffset == offset);
549 }
550 {
551 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
552 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
553 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
554 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
555 assert(secondaryColor3fOffset == offset);
556 }
557 {
558 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
559 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
560 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
561 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
562 assert(pointParameterivOffset == offset);
563 }
564 {
565 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
566 char *setFenceFunc = (char*) &table->SetFenceNV;
567 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
568 assert(setFenceOffset == _gloffset_SetFenceNV);
569 assert(setFenceOffset == offset);
570 }
571 #else
572 (void) table;
573 #endif
574 }