1/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "config.h"
25
26#ifdef _WIN32
27#define PLATFORM_HAS_EGL ENABLE_EGL
28#define PLATFORM_HAS_GLX ENABLE_GLX
29#define PLATFORM_HAS_WGL 1
30#elif defined(__APPLE__)
31#define PLATFORM_HAS_EGL 0
32#define PLATFORM_HAS_GLX ENABLE_GLX
33#define PLATFORM_HAS_WGL 0
34#elif defined(ANDROID)
35#define PLATFORM_HAS_EGL ENABLE_EGL
36#define PLATFORM_HAS_GLX 0
37#define PLATFORM_HAS_WGL 0
38#elif defined(__NetBSD__)
39# if !defined(PLATFORM_HAS_EGL)
40#  if defined(__amd64__) || defined(__i386__) || defined(__aarch64__) // XXX evbarm32
41#   define PLATFORM_HAS_EGL 1
42#  else
43#   define PLATFORM_HAS_EGL ENABLE_EGL
44#  endif
45# endif
46# define PLATFORM_HAS_GLX 1
47# define PLATFORM_HAS_WGL 0
48#else
49#define PLATFORM_HAS_EGL ENABLE_EGL
50#define PLATFORM_HAS_GLX ENABLE_GLX
51#define PLATFORM_HAS_WGL 0
52#endif
53
54#include "epoxy/gl.h"
55#if PLATFORM_HAS_GLX
56#include "epoxy/glx.h"
57#endif
58#if PLATFORM_HAS_EGL
59# if !ENABLE_X11
60/* Mesa uses this symbol to avoid including X11 headers when including
61 * EGL.h; since X11 was explicitly disabled at configuration time, we
62 * should do the same
63 */
64#  define MESA_EGL_NO_X11_HEADERS 1
65# endif
66#include "epoxy/egl.h"
67#endif
68#if PLATFORM_HAS_WGL
69#include "epoxy/wgl.h"
70#endif
71
72#if defined(__GNUC__)
73#define PACKED __attribute__((__packed__))
74#define ENDPACKED
75#elif defined (_MSC_VER)
76#define PACKED __pragma(pack(push,1))
77#define ENDPACKED __pragma(pack(pop))
78#else
79#define PACKED
80#define ENDPACKED
81#endif
82
83/* On win32, we're going to need to keep a per-thread dispatch table,
84 * since the function pointers depend on the device and pixel format
85 * of the current context.
86 */
87#if defined(_WIN32)
88#define USING_DISPATCH_TABLE 1
89#else
90#define USING_DISPATCH_TABLE 0
91#endif
92
93#define UNWRAPPED_PROTO(x) (GLAPIENTRY *x)
94#define WRAPPER_VISIBILITY(type) static type GLAPIENTRY
95#define WRAPPER(x) x ## _wrapped
96
97#define GEN_GLOBAL_REWRITE_PTR(name, args, passthrough)          \
98    static void EPOXY_CALLSPEC                                   \
99    name##_global_rewrite_ptr args                               \
100    {                                                            \
101        if (name == (void *)name##_global_rewrite_ptr)           \
102            name = (void *)name##_resolver();                    \
103        name passthrough;                                        \
104    }
105
106#define GEN_GLOBAL_REWRITE_PTR_RET(ret, name, args, passthrough) \
107    static ret EPOXY_CALLSPEC                                    \
108    name##_global_rewrite_ptr args                               \
109    {                                                            \
110        if (name == (void *)name##_global_rewrite_ptr)           \
111            name = (void *)name##_resolver();                    \
112        return name passthrough;                                 \
113    }
114
115#if USING_DISPATCH_TABLE
116#define GEN_DISPATCH_TABLE_REWRITE_PTR(name, args, passthrough)            \
117    static void EPOXY_CALLSPEC                                             \
118    name##_dispatch_table_rewrite_ptr args                                 \
119    {                                                                      \
120        struct dispatch_table *dispatch_table = get_dispatch_table();      \
121                                                                           \
122        dispatch_table->name = (void *)name##_resolver();                  \
123        dispatch_table->name passthrough;                                  \
124    }
125
126#define GEN_DISPATCH_TABLE_REWRITE_PTR_RET(ret, name, args, passthrough)   \
127    static ret EPOXY_CALLSPEC                                              \
128    name##_dispatch_table_rewrite_ptr args                                 \
129    {                                                                      \
130        struct dispatch_table *dispatch_table = get_dispatch_table();      \
131                                                                           \
132        dispatch_table->name = (void *)name##_resolver();                  \
133        return dispatch_table->name passthrough;                           \
134    }
135
136#define GEN_DISPATCH_TABLE_THUNK(name, args, passthrough)                  \
137    static void EPOXY_CALLSPEC                                             \
138    name##_dispatch_table_thunk args                                       \
139    {                                                                      \
140        get_dispatch_table()->name passthrough;                            \
141    }
142
143#define GEN_DISPATCH_TABLE_THUNK_RET(ret, name, args, passthrough)         \
144    static ret EPOXY_CALLSPEC                                              \
145    name##_dispatch_table_thunk args                                       \
146    {                                                                      \
147        return get_dispatch_table()->name passthrough;                     \
148    }
149
150#else
151#define GEN_DISPATCH_TABLE_REWRITE_PTR(name, args, passthrough)
152#define GEN_DISPATCH_TABLE_REWRITE_PTR_RET(ret, name, args, passthrough)
153#define GEN_DISPATCH_TABLE_THUNK(name, args, passthrough)
154#define GEN_DISPATCH_TABLE_THUNK_RET(ret, name, args, passthrough)
155#endif
156
157#define GEN_THUNKS(name, args, passthrough)                          \
158    GEN_GLOBAL_REWRITE_PTR(name, args, passthrough)                  \
159    GEN_DISPATCH_TABLE_REWRITE_PTR(name, args, passthrough)          \
160    GEN_DISPATCH_TABLE_THUNK(name, args, passthrough)
161
162#define GEN_THUNKS_RET(ret, name, args, passthrough)                 \
163    GEN_GLOBAL_REWRITE_PTR_RET(ret, name, args, passthrough)         \
164    GEN_DISPATCH_TABLE_REWRITE_PTR_RET(ret, name, args, passthrough) \
165    GEN_DISPATCH_TABLE_THUNK_RET(ret, name, args, passthrough)
166
167void *epoxy_egl_dlsym(const char *name);
168void *epoxy_glx_dlsym(const char *name);
169void *epoxy_gl_dlsym(const char *name);
170void *epoxy_gles1_dlsym(const char *name);
171void *epoxy_gles2_dlsym(const char *name);
172void *epoxy_gles3_dlsym(const char *name);
173void *epoxy_get_proc_address(const char *name);
174void *epoxy_get_core_proc_address(const char *name, int core_version);
175void *epoxy_get_bootstrap_proc_address(const char *name);
176
177int epoxy_conservative_gl_version(void);
178bool epoxy_conservative_has_gl_extension(const char *name);
179int epoxy_conservative_glx_version(void);
180bool epoxy_conservative_has_glx_extension(const char *name);
181int epoxy_conservative_egl_version(void);
182bool epoxy_conservative_has_egl_extension(const char *name);
183bool epoxy_conservative_has_wgl_extension(const char *name);
184void *epoxy_conservative_egl_dlsym(const char *name, bool exit_if_fails);
185void *epoxy_conservative_glx_dlsym(const char *name, bool exit_if_fails);
186
187bool epoxy_load_glx(bool exit_if_fails, bool load);
188bool epoxy_load_egl(bool exit_if_fails, bool load);
189
190#define glBegin_unwrapped epoxy_glBegin_unwrapped
191#define glEnd_unwrapped epoxy_glEnd_unwrapped
192extern void UNWRAPPED_PROTO(glBegin_unwrapped)(GLenum primtype);
193extern void UNWRAPPED_PROTO(glEnd_unwrapped)(void);
194
195extern epoxy_resolver_failure_handler_t epoxy_resolver_failure_handler;
196
197#if USING_DISPATCH_TABLE
198void gl_init_dispatch_table(void);
199void gl_switch_to_dispatch_table(void);
200void wgl_init_dispatch_table(void);
201void wgl_switch_to_dispatch_table(void);
202extern uint32_t gl_tls_index, gl_tls_size;
203extern uint32_t wgl_tls_index, wgl_tls_size;
204
205#define wglMakeCurrent_unwrapped epoxy_wglMakeCurrent_unwrapped
206#define wglMakeContextCurrentARB_unwrapped epoxy_wglMakeContextCurrentARB_unwrapped
207#define wglMakeContextCurrentEXT_unwrapped epoxy_wglMakeContextCurrentEXT_unwrapped
208#define wglMakeAssociatedContextCurrentAMD_unwrapped epoxy_wglMakeAssociatedContextCurrentAMD_unwrapped
209extern BOOL UNWRAPPED_PROTO(wglMakeCurrent_unwrapped)(HDC hdc, HGLRC hglrc);
210extern BOOL UNWRAPPED_PROTO(wglMakeContextCurrentARB_unwrapped)(HDC hDrawDC, HDC hReadDC, HGLRC hglrc);
211extern BOOL UNWRAPPED_PROTO(wglMakeContextCurrentEXT_unwrapped)(HDC hDrawDC, HDC hReadDC, HGLRC hglrc);
212extern BOOL UNWRAPPED_PROTO(wglMakeAssociatedContextCurrentAMD_unwrapped)(HGLRC hglrc);
213#endif /* _WIN32_ */
214