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