1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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 #include "c11/threads.h"
52 #include "util/u_thread.h"
53 #include "u_current.h"
54
55 #ifndef MAPI_MODE_UTIL
56
57 #include "table.h"
58 #include "stub.h"
59
60 #else
61
62 extern void init_glapi_relocs_once(void);
63 extern void (*__glapi_noop_table[])(void);
64
65 #define table_noop_array __glapi_noop_table
66 #define stub_init_once() init_glapi_relocs_once()
67
68 #endif
69
70 /**
71 * \name Current dispatch and current context control variables
72 *
73 * Depending on whether or not multithreading is support, and the type of
74 * support available, several variables are used to store the current context
75 * pointer and the current dispatch table pointer. In the non-threaded case,
76 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
77 * purpose.
78 *
79 * In multi threaded case, The TLS variables \c _glapi_tls_Dispatch and
80 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and \c _glapi_Context
81 * be hardcoded to \c NULL maintains binary compatability between TLS enabled
82 * loaders and non-TLS DRI drivers. When \c _glapi_Dispatch and \c _glapi_Context
83 * are \c NULL, the thread state data \c ContextTSD are used. Drivers and the
84 * static dispatch functions access these variables via \c _glapi_get_dispatch
85 * and \c _glapi_get_context.
86 */
87 /*@{*/
88
89 __THREAD_INITIAL_EXEC struct _glapi_table *_glapi_tls_Dispatch
90 = (struct _glapi_table *) table_noop_array;
91
92 __THREAD_INITIAL_EXEC void *_glapi_tls_Context;
93
94 /* not used, but defined for compatibility */
95 const struct _glapi_table *_glapi_Dispatch;
96 const void *_glapi_Context;
97
98 /*@}*/
99
100 /* not used, but defined for compatibility */
101 void
_glapi_destroy_multithread(void)102 _glapi_destroy_multithread(void)
103 {
104 }
105
106 /* not used, but defined for compatibility */
107 void
_glapi_check_multithread(void)108 _glapi_check_multithread(void)
109 {
110 }
111
112 /**
113 * Set the current context pointer for this thread.
114 * The context pointer is an opaque type which should be cast to
115 * void from the real context pointer type.
116 */
117 void
_glapi_set_context(void * ptr)118 _glapi_set_context(void *ptr)
119 {
120 _glapi_tls_Context = ptr;
121 }
122
123 /**
124 * Get the current context pointer for this thread.
125 * The context pointer is an opaque type which should be cast from
126 * void to the real context pointer type.
127 */
128 void *
_glapi_get_context(void)129 _glapi_get_context(void)
130 {
131 return _glapi_tls_Context;
132 }
133
134 /**
135 * Set the global or per-thread dispatch table pointer.
136 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
137 * table (__glapi_noop_table).
138 */
139 void
_glapi_set_dispatch(struct _glapi_table * tbl)140 _glapi_set_dispatch(struct _glapi_table *tbl)
141 {
142 stub_init_once();
143
144 if (!tbl)
145 tbl = (struct _glapi_table *) table_noop_array;
146
147 _glapi_tls_Dispatch = tbl;
148 }
149
150 /**
151 * Return pointer to current dispatch table for calling thread.
152 */
153 struct _glapi_table *
_glapi_get_dispatch(void)154 _glapi_get_dispatch(void)
155 {
156 return _glapi_tls_Dispatch;
157 }
158