Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 1 | /* |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 2 | * Copyright © 2013-2014 Intel Corporation |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 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 | /** |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 25 | * \mainpage Epoxy |
| 26 | * |
| 27 | * \section intro_sec Introduction |
| 28 | * |
| 29 | * Epoxy is a library for handling OpenGL function pointer management for |
| 30 | * you. |
| 31 | * |
| 32 | * It hides the complexity of `dlopen()`, `dlsym()`, `glXGetProcAddress()`, |
| 33 | * `eglGetProcAddress()`, etc. from the app developer, with very little |
| 34 | * knowledge needed on their part. They get to read GL specs and write |
| 35 | * code using undecorated function names like `glCompileShader()`. |
| 36 | * |
| 37 | * Don't forget to check for your extensions or versions being present |
| 38 | * before you use them, just like before! We'll tell you what you forgot |
| 39 | * to check for instead of just segfaulting, though. |
| 40 | * |
| 41 | * \section features_sec Features |
| 42 | * |
| 43 | * - Automatically initializes as new GL functions are used. |
Emmanuele Bassi | c794dce | 2018-02-23 15:48:44 +0000 | [diff] [blame] | 44 | * - GL 4.6 core and compatibility context support. |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 45 | * - GLES 1/2/3 context support. |
| 46 | * - Knows about function aliases so (e.g.) `glBufferData()` can be |
| 47 | * used with `GL_ARB_vertex_buffer_object` implementations, along |
| 48 | * with GL 1.5+ implementations. |
| 49 | * - EGL, GLX, and WGL support. |
| 50 | * - Can be mixed with non-epoxy GL usage. |
| 51 | * |
| 52 | * \section using_sec Using Epoxy |
| 53 | * |
| 54 | * Using Epoxy should be as easy as replacing: |
| 55 | * |
| 56 | * ```cpp |
| 57 | * #include <GL/gl.h> |
| 58 | * #include <GL/glx.h> |
| 59 | * #include <GL/glext.h> |
| 60 | * ``` |
| 61 | * |
| 62 | * with: |
| 63 | * |
| 64 | * ```cpp |
| 65 | * #include <epoxy/gl.h> |
| 66 | * #include <epoxy/glx.h> |
| 67 | * ``` |
| 68 | * |
| 69 | * \subsection using_include_sec Headers |
| 70 | * |
| 71 | * Epoxy comes with the following public headers: |
| 72 | * |
| 73 | * - `epoxy/gl.h` - For GL API |
| 74 | * - `epoxy/egl.h` - For EGL API |
| 75 | * - `epoxy/glx.h` - For GLX API |
| 76 | * - `epoxy/wgl.h` - For WGL API |
| 77 | * |
| 78 | * \section links_sec Additional links |
| 79 | * |
| 80 | * The latest version of the Epoxy code is available on [GitHub](https://github.com/anholt/libepoxy). |
| 81 | * |
| 82 | * For bug reports and enhancements, please use the [Issues](https://github.com/anholt/libepoxy/issues) |
| 83 | * link. |
| 84 | * |
| 85 | * The scope of this API reference does not include the documentation for |
| 86 | * OpenGL and OpenGL ES. For more information on those programming interfaces |
| 87 | * please visit: |
| 88 | * |
| 89 | * - [Khronos](https://www.khronos.org/) |
| 90 | * - [OpenGL page on Khronos.org](https://www.khronos.org/opengl/) |
| 91 | * - [OpenGL ES page on Khronos.org](https://www.khronos.org/opengles/) |
| 92 | * - [docs.GL](http://docs.gl/) |
| 93 | */ |
| 94 | |
| 95 | /** |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 96 | * @file dispatch_common.c |
| 97 | * |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 98 | * @brief Implements common code shared by the generated GL/EGL/GLX dispatch code. |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 99 | * |
| 100 | * A collection of some important specs on getting GL function pointers. |
| 101 | * |
| 102 | * From the linux GL ABI (http://www.opengl.org/registry/ABI/): |
| 103 | * |
| 104 | * "3.4. The libraries must export all OpenGL 1.2, GLU 1.3, GLX 1.3, and |
| 105 | * ARB_multitexture entry points statically. |
| 106 | * |
| 107 | * 3.5. Because non-ARB extensions vary so widely and are constantly |
| 108 | * increasing in number, it's infeasible to require that they all be |
| 109 | * supported, and extensions can always be added to hardware drivers |
| 110 | * after the base link libraries are released. These drivers are |
| 111 | * dynamically loaded by libGL, so extensions not in the base |
| 112 | * library must also be obtained dynamically. |
| 113 | * |
| 114 | * 3.6. To perform the dynamic query, libGL also must export an entry |
| 115 | * point called |
| 116 | * |
| 117 | * void (*glXGetProcAddressARB(const GLubyte *))(); |
| 118 | * |
| 119 | * The full specification of this function is available separately. It |
| 120 | * takes the string name of a GL or GLX entry point and returns a pointer |
| 121 | * to a function implementing that entry point. It is functionally |
| 122 | * identical to the wglGetProcAddress query defined by the Windows OpenGL |
| 123 | * library, except that the function pointers returned are context |
| 124 | * independent, unlike the WGL query." |
| 125 | * |
| 126 | * From the EGL 1.4 spec: |
| 127 | * |
| 128 | * "Client API function pointers returned by eglGetProcAddress are |
| 129 | * independent of the display and the currently bound client API context, |
| 130 | * and may be used by any client API context which supports the extension. |
| 131 | * |
| 132 | * eglGetProcAddress may be queried for all of the following functions: |
| 133 | * |
| 134 | * • All EGL and client API extension functions supported by the |
| 135 | * implementation (whether those extensions are supported by the current |
| 136 | * client API context or not). This includes any mandatory OpenGL ES |
| 137 | * extensions. |
| 138 | * |
| 139 | * eglGetProcAddress may not be queried for core (non-extension) functions |
| 140 | * in EGL or client APIs 20 . |
| 141 | * |
| 142 | * For functions that are queryable with eglGetProcAddress, |
| 143 | * implementations may choose to also export those functions statically |
| 144 | * from the object libraries im- plementing those functions. However, |
| 145 | * portable clients cannot rely on this behavior. |
| 146 | * |
| 147 | * From the GLX 1.4 spec: |
| 148 | * |
| 149 | * "glXGetProcAddress may be queried for all of the following functions: |
| 150 | * |
| 151 | * • All GL and GLX extension functions supported by the implementation |
| 152 | * (whether those extensions are supported by the current context or |
| 153 | * not). |
| 154 | * |
| 155 | * • All core (non-extension) functions in GL and GLX from version 1.0 up |
| 156 | * to and including the versions of those specifications supported by |
| 157 | * the implementation, as determined by glGetString(GL VERSION) and |
| 158 | * glXQueryVersion queries." |
| 159 | */ |
| 160 | |
| 161 | #include <assert.h> |
| 162 | #include <stdlib.h> |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 163 | #ifdef _WIN32 |
| 164 | #include <windows.h> |
| 165 | #else |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 166 | #include <dlfcn.h> |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 167 | #include <err.h> |
| 168 | #include <pthread.h> |
| 169 | #endif |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 170 | #include <string.h> |
Eric Anholt | 66d7b9f | 2013-11-11 09:27:07 -0800 | [diff] [blame] | 171 | #include <ctype.h> |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 172 | #include <stdio.h> |
Eric Anholt | 906d5b6 | 2013-12-06 12:21:54 -0800 | [diff] [blame] | 173 | |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 174 | #include "dispatch_common.h" |
| 175 | |
Eric Anholt | 8d8334c | 2013-12-12 10:55:32 -0800 | [diff] [blame] | 176 | #ifdef __APPLE__ |
| 177 | #define GLX_LIB "/opt/X11/lib/libGL.1.dylib" |
Robert Bragg | 4e5e17e | 2018-01-18 21:21:15 +0000 | [diff] [blame] | 178 | #elif defined(__ANDROID__) |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 179 | #define GLX_LIB "libGLESv2.so" |
Eric Anholt | 8d8334c | 2013-12-12 10:55:32 -0800 | [diff] [blame] | 180 | #else |
Adam Jackson | 759de64 | 2017-06-16 14:37:06 -0400 | [diff] [blame] | 181 | #define GLVND_GLX_LIB "libGLX.so.1" |
Eric Anholt | 8d8334c | 2013-12-12 10:55:32 -0800 | [diff] [blame] | 182 | #define GLX_LIB "libGL.so.1" |
| 183 | #endif |
| 184 | |
Robert Bragg | 4e5e17e | 2018-01-18 21:21:15 +0000 | [diff] [blame] | 185 | #ifdef __ANDROID__ |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 186 | #define EGL_LIB "libEGL.so" |
| 187 | #define GLES1_LIB "libGLESv1_CM.so" |
| 188 | #define GLES2_LIB "libGLESv2.so" |
Yaron Cohen-Tal | 9e46b8e | 2016-12-13 12:26:41 +0000 | [diff] [blame] | 189 | #elif defined _WIN32 |
| 190 | #define EGL_LIB "libEGL.dll" |
| 191 | #define GLES1_LIB "libGLES_CM.dll" |
| 192 | #define GLES2_LIB "libGLESv2.dll" |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 193 | #else |
| 194 | #define EGL_LIB "libEGL.so.1" |
| 195 | #define GLES1_LIB "libGLESv1_CM.so.1" |
| 196 | #define GLES2_LIB "libGLESv2.so.2" |
Adam Jackson | e5372a2 | 2017-09-07 17:02:22 -0400 | [diff] [blame] | 197 | #define OPENGL_LIB "libOpenGL.so.0" |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 198 | #endif |
| 199 | |
Chun-wei Fan | 95ecc2d | 2015-06-05 14:03:51 +0800 | [diff] [blame] | 200 | #ifdef __GNUC__ |
| 201 | #define CONSTRUCT(_func) static void _func (void) __attribute__((constructor)); |
| 202 | #define DESTRUCT(_func) static void _func (void) __attribute__((destructor)); |
| 203 | #elif defined (_MSC_VER) && (_MSC_VER >= 1500) |
| 204 | #define CONSTRUCT(_func) \ |
| 205 | static void _func(void); \ |
| 206 | static int _func ## _wrapper(void) { _func(); return 0; } \ |
| 207 | __pragma(section(".CRT$XCU",read)) \ |
| 208 | __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper; |
| 209 | |
| 210 | #define DESTRUCT(_func) \ |
| 211 | static void _func(void); \ |
| 212 | static int _func ## _constructor(void) { atexit (_func); return 0; } \ |
| 213 | __pragma(section(".CRT$XCU",read)) \ |
| 214 | __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor; |
| 215 | |
| 216 | #else |
| 217 | #error "You will need constructor support for your compiler" |
| 218 | #endif |
| 219 | |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 220 | struct api { |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 221 | #ifndef _WIN32 |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 222 | /* |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 223 | * Locking for making sure we don't double-dlopen(). |
| 224 | */ |
| 225 | pthread_mutex_t mutex; |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 226 | #endif |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 227 | |
Adam Jackson | 759de64 | 2017-06-16 14:37:06 -0400 | [diff] [blame] | 228 | /* |
| 229 | * dlopen() return value for the GLX API. This is libGLX.so.1 if the |
| 230 | * runtime is glvnd-enabled, else libGL.so.1 |
| 231 | */ |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 232 | void *glx_handle; |
| 233 | |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 234 | /* |
Adam Jackson | 759de64 | 2017-06-16 14:37:06 -0400 | [diff] [blame] | 235 | * dlopen() return value for the desktop GL library. |
Eric Anholt | d82c5c3 | 2013-12-11 16:49:08 -0800 | [diff] [blame] | 236 | * |
Adam Jackson | 759de64 | 2017-06-16 14:37:06 -0400 | [diff] [blame] | 237 | * On Windows this is OPENGL32. On OSX this is classic libGL. On Linux |
| 238 | * this is either libOpenGL (if the runtime is glvnd-enabled) or |
| 239 | * classic libGL.so.1 |
Eric Anholt | d82c5c3 | 2013-12-11 16:49:08 -0800 | [diff] [blame] | 240 | */ |
| 241 | void *gl_handle; |
| 242 | |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 243 | /* dlopen() return value for libEGL.so.1 */ |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 244 | void *egl_handle; |
| 245 | |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 246 | /* dlopen() return value for libGLESv1_CM.so.1 */ |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 247 | void *gles1_handle; |
| 248 | |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 249 | /* dlopen() return value for libGLESv2.so.2 */ |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 250 | void *gles2_handle; |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 251 | |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 252 | /* |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 253 | * This value gets incremented when any thread is in |
| 254 | * glBegin()/glEnd() called through epoxy. |
| 255 | * |
| 256 | * We're not guaranteed to be called through our wrapper, so the |
| 257 | * conservative paths also try to handle the failure cases they'll |
| 258 | * see if begin_count didn't reflect reality. It's also a bit of |
| 259 | * a bug that the conservative paths might return success because |
| 260 | * some other thread was in epoxy glBegin/glEnd while our thread |
| 261 | * is trying to resolve, but given that it's basically just for |
| 262 | * informative error messages, we shouldn't need to care. |
| 263 | */ |
Eric Anholt | 4eaf4bf | 2013-12-10 14:09:37 -0800 | [diff] [blame] | 264 | long begin_count; |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | static struct api api = { |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 268 | #ifndef _WIN32 |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 269 | .mutex = PTHREAD_MUTEX_INITIALIZER, |
Chun-wei Fan | e242497 | 2015-06-05 17:37:01 -0700 | [diff] [blame] | 270 | #else |
| 271 | 0, |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 272 | #endif |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
Eric Anholt | 10c611a | 2013-12-13 12:22:36 -0800 | [diff] [blame] | 275 | static bool library_initialized; |
| 276 | |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 277 | static bool epoxy_current_context_is_glx(void); |
| 278 | |
| 279 | #if PLATFORM_HAS_EGL |
Eric Anholt | b251e3a | 2014-03-27 19:04:27 -0700 | [diff] [blame] | 280 | static EGLenum |
| 281 | epoxy_egl_get_current_gl_context_api(void); |
| 282 | #endif |
| 283 | |
Chun-wei Fan | 95ecc2d | 2015-06-05 14:03:51 +0800 | [diff] [blame] | 284 | CONSTRUCT (library_init) |
Eric Anholt | 10c611a | 2013-12-13 12:22:36 -0800 | [diff] [blame] | 285 | |
| 286 | static void |
| 287 | library_init(void) |
| 288 | { |
| 289 | library_initialized = true; |
| 290 | } |
| 291 | |
Eric Anholt | 72187a2 | 2014-03-27 18:53:10 -0700 | [diff] [blame] | 292 | static bool |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 293 | get_dlopen_handle(void **handle, const char *lib_name, bool exit_on_fail) |
| 294 | { |
| 295 | if (*handle) |
Eric Anholt | 72187a2 | 2014-03-27 18:53:10 -0700 | [diff] [blame] | 296 | return true; |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 297 | |
Eric Anholt | 10c611a | 2013-12-13 12:22:36 -0800 | [diff] [blame] | 298 | if (!library_initialized) { |
Ikey Doherty | c8d7ae6 | 2017-10-19 16:02:57 +0100 | [diff] [blame] | 299 | fputs("Attempting to dlopen() while in the dynamic linker.\n", stderr); |
Eric Anholt | 10c611a | 2013-12-13 12:22:36 -0800 | [diff] [blame] | 300 | abort(); |
| 301 | } |
| 302 | |
Eric Anholt | 5112b47 | 2013-12-17 10:38:29 -0800 | [diff] [blame] | 303 | #ifdef _WIN32 |
| 304 | *handle = LoadLibraryA(lib_name); |
| 305 | #else |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 306 | pthread_mutex_lock(&api.mutex); |
| 307 | if (!*handle) { |
| 308 | *handle = dlopen(lib_name, RTLD_LAZY | RTLD_LOCAL); |
Eric Anholt | d48978c | 2014-03-27 18:54:12 -0700 | [diff] [blame] | 309 | if (!*handle) { |
| 310 | if (exit_on_fail) { |
| 311 | fprintf(stderr, "Couldn't open %s: %s\n", lib_name, dlerror()); |
| 312 | exit(1); |
| 313 | } else { |
| 314 | (void)dlerror(); |
| 315 | } |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | pthread_mutex_unlock(&api.mutex); |
Eric Anholt | 5112b47 | 2013-12-17 10:38:29 -0800 | [diff] [blame] | 319 | #endif |
Eric Anholt | 72187a2 | 2014-03-27 18:53:10 -0700 | [diff] [blame] | 320 | |
| 321 | return *handle != NULL; |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static void * |
| 325 | do_dlsym(void **handle, const char *lib_name, const char *name, |
| 326 | bool exit_on_fail) |
| 327 | { |
| 328 | void *result; |
Eric Anholt | d48978c | 2014-03-27 18:54:12 -0700 | [diff] [blame] | 329 | const char *error = ""; |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 330 | |
Eric Anholt | 72187a2 | 2014-03-27 18:53:10 -0700 | [diff] [blame] | 331 | if (!get_dlopen_handle(handle, lib_name, exit_on_fail)) |
| 332 | return NULL; |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 333 | |
Eric Anholt | 5112b47 | 2013-12-17 10:38:29 -0800 | [diff] [blame] | 334 | #ifdef _WIN32 |
| 335 | result = GetProcAddress(*handle, name); |
| 336 | #else |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 337 | result = dlsym(*handle, name); |
Eric Anholt | d48978c | 2014-03-27 18:54:12 -0700 | [diff] [blame] | 338 | if (!result) |
| 339 | error = dlerror(); |
Eric Anholt | 5112b47 | 2013-12-17 10:38:29 -0800 | [diff] [blame] | 340 | #endif |
Eric Anholt | 14f2448 | 2014-03-17 11:17:19 -0700 | [diff] [blame] | 341 | if (!result && exit_on_fail) { |
Eric Anholt | d48978c | 2014-03-27 18:54:12 -0700 | [diff] [blame] | 342 | fprintf(stderr,"%s() not found in %s: %s\n", name, lib_name, error); |
Eric Anholt | 5112b47 | 2013-12-17 10:38:29 -0800 | [diff] [blame] | 343 | exit(1); |
| 344 | } |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 345 | |
| 346 | return result; |
| 347 | } |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 348 | |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 349 | /** |
| 350 | * @brief Checks whether we're using OpenGL or OpenGL ES |
| 351 | * |
| 352 | * @return `true` if we're using OpenGL |
| 353 | */ |
Emmanuele Bassi | 7a06803 | 2017-01-24 17:43:59 +0000 | [diff] [blame] | 354 | bool |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 355 | epoxy_is_desktop_gl(void) |
| 356 | { |
Eric Anholt | aa778ef | 2014-01-22 00:57:35 -0800 | [diff] [blame] | 357 | const char *es_prefix = "OpenGL ES"; |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 358 | const char *version; |
| 359 | |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 360 | #if PLATFORM_HAS_EGL |
Yaron Cohen-Tal | a7c270f | 2015-07-09 13:44:52 +0300 | [diff] [blame] | 361 | /* PowerVR's OpenGL ES implementation (and perhaps other) don't |
| 362 | * comply with the standard, which states that |
| 363 | * "glGetString(GL_VERSION)" should return a string starting with |
| 364 | * "OpenGL ES". Therefore, to distinguish desktop OpenGL from |
| 365 | * OpenGL ES, we must also check the context type through EGL (we |
| 366 | * can do that as PowerVR is only usable through EGL). |
| 367 | */ |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 368 | if (!epoxy_current_context_is_glx()) { |
Yaron Cohen-Tal | a7c270f | 2015-07-09 13:44:52 +0300 | [diff] [blame] | 369 | switch (epoxy_egl_get_current_gl_context_api()) { |
| 370 | case EGL_OPENGL_API: return true; |
| 371 | case EGL_OPENGL_ES_API: return false; |
| 372 | case EGL_NONE: |
| 373 | default: break; |
| 374 | } |
| 375 | } |
| 376 | #endif |
| 377 | |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 378 | if (api.begin_count) |
| 379 | return true; |
| 380 | |
| 381 | version = (const char *)glGetString(GL_VERSION); |
| 382 | |
| 383 | /* If we didn't get a version back, there are only two things that |
| 384 | * could have happened: either malloc failure (which basically |
| 385 | * doesn't exist), or we were called within a glBegin()/glEnd(). |
| 386 | * Assume the second, which only exists for desktop GL. |
| 387 | */ |
| 388 | if (!version) |
| 389 | return true; |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 390 | |
Eric Anholt | 6924148 | 2013-11-11 09:19:36 -0800 | [diff] [blame] | 391 | return strncmp(es_prefix, version, strlen(es_prefix)); |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 394 | static int |
Emmanuele Bassi | 60bb767 | 2018-02-24 13:39:16 +0000 | [diff] [blame] | 395 | epoxy_internal_gl_version(GLenum version_string, int error_version) |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 396 | { |
Emmanuele Bassi | d8726f2 | 2018-02-23 16:46:55 +0000 | [diff] [blame] | 397 | const char *version = (const char *)glGetString(version_string); |
Emmanuele Bassi | e1ffd32 | 2018-02-24 14:03:06 +0000 | [diff] [blame] | 398 | GLint major, minor, factor; |
Eric Anholt | 66d7b9f | 2013-11-11 09:27:07 -0800 | [diff] [blame] | 399 | int scanf_count; |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 400 | |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 401 | if (!version) |
| 402 | return error_version; |
| 403 | |
Eric Anholt | 66d7b9f | 2013-11-11 09:27:07 -0800 | [diff] [blame] | 404 | /* skip to version number */ |
| 405 | while (!isdigit(*version) && *version != '\0') |
| 406 | version++; |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 407 | |
Eric Anholt | 66d7b9f | 2013-11-11 09:27:07 -0800 | [diff] [blame] | 408 | /* Interpret version number */ |
| 409 | scanf_count = sscanf(version, "%i.%i", &major, &minor); |
| 410 | if (scanf_count != 2) { |
| 411 | fprintf(stderr, "Unable to interpret GL_VERSION string: %s\n", |
| 412 | version); |
| 413 | exit(1); |
| 414 | } |
Emmanuele Bassi | e1ffd32 | 2018-02-24 14:03:06 +0000 | [diff] [blame] | 415 | |
| 416 | if (minor >= 10) |
| 417 | factor = 100; |
| 418 | else |
| 419 | factor = 10; |
| 420 | |
| 421 | return factor * major + minor; |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 424 | /** |
| 425 | * @brief Returns the version of OpenGL we are using |
| 426 | * |
| 427 | * The version is encoded as: |
| 428 | * |
| 429 | * ``` |
| 430 | * |
| 431 | * version = major * 10 + minor |
| 432 | * |
| 433 | * ``` |
| 434 | * |
| 435 | * So it can be easily used for version comparisons. |
| 436 | * |
| 437 | * @return The encoded version of OpenGL we are using |
| 438 | */ |
Emmanuele Bassi | 7a06803 | 2017-01-24 17:43:59 +0000 | [diff] [blame] | 439 | int |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 440 | epoxy_gl_version(void) |
| 441 | { |
Emmanuele Bassi | d8726f2 | 2018-02-23 16:46:55 +0000 | [diff] [blame] | 442 | return epoxy_internal_gl_version(GL_VERSION, 0); |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Chun-wei Fan | d9df59d | 2015-06-17 09:38:53 +0800 | [diff] [blame] | 445 | int |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 446 | epoxy_conservative_gl_version(void) |
| 447 | { |
| 448 | if (api.begin_count) |
| 449 | return 100; |
| 450 | |
Emmanuele Bassi | d8726f2 | 2018-02-23 16:46:55 +0000 | [diff] [blame] | 451 | return epoxy_internal_gl_version(GL_VERSION, 100); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * @brief Returns the version of the GL Shading Language we are using |
| 456 | * |
| 457 | * The version is encoded as: |
| 458 | * |
| 459 | * ``` |
| 460 | * |
Emmanuele Bassi | e1ffd32 | 2018-02-24 14:03:06 +0000 | [diff] [blame] | 461 | * version = major * 100 + minor |
Emmanuele Bassi | d8726f2 | 2018-02-23 16:46:55 +0000 | [diff] [blame] | 462 | * |
| 463 | * ``` |
| 464 | * |
| 465 | * So it can be easily used for version comparisons. |
| 466 | * |
| 467 | * @return The encoded version of the GL Shading Language we are using |
| 468 | */ |
| 469 | int |
| 470 | epoxy_glsl_version(void) |
| 471 | { |
| 472 | if (epoxy_gl_version() >= 20 || |
| 473 | epoxy_has_gl_extension ("GL_ARB_shading_language_100")) |
| 474 | return epoxy_internal_gl_version(GL_SHADING_LANGUAGE_VERSION, 0); |
| 475 | |
| 476 | return 0; |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Lyude Paul | 4bde48e | 2017-08-24 14:50:19 -0400 | [diff] [blame] | 479 | /** |
| 480 | * @brief Checks for the presence of an extension in an OpenGL extension string |
| 481 | * |
| 482 | * @param extension_list The string containing the list of extensions to check |
| 483 | * @param ext The name of the GL extension |
| 484 | * @return `true` if the extension is available' |
| 485 | * |
| 486 | * @note If you are looking to check whether a normal GL, EGL or GLX extension |
| 487 | * is supported by the client, this probably isn't the function you want. |
| 488 | * |
| 489 | * Some parts of the spec for OpenGL and friends will return an OpenGL formatted |
luz.paz | d60b923 | 2018-03-05 06:08:53 -0500 | [diff] [blame^] | 490 | * extension string that is separate from the usual extension strings for the |
Lyude Paul | 4bde48e | 2017-08-24 14:50:19 -0400 | [diff] [blame] | 491 | * spec. This function provides easy parsing of those strings. |
| 492 | * |
| 493 | * @see epoxy_has_gl_extension() |
| 494 | * @see epoxy_has_egl_extension() |
| 495 | * @see epoxy_has_glx_extension() |
| 496 | */ |
Eric Anholt | 60ea7c3 | 2013-12-06 12:53:00 -0800 | [diff] [blame] | 497 | bool |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 498 | epoxy_extension_in_string(const char *extension_list, const char *ext) |
| 499 | { |
| 500 | const char *ptr = extension_list; |
Gianfranco Costamagna | 106eae7 | 2017-04-24 16:10:28 +0200 | [diff] [blame] | 501 | int len; |
| 502 | |
| 503 | if (!ext) |
| 504 | return false; |
| 505 | |
| 506 | len = strlen(ext); |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 507 | |
Emmanuele Bassi | ad1f920 | 2016-12-07 15:12:15 +0000 | [diff] [blame] | 508 | if (extension_list == NULL || *extension_list == '\0') |
| 509 | return false; |
| 510 | |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 511 | /* Make sure that don't just find an extension with our name as a prefix. */ |
Eric Anholt | 03e9537 | 2013-12-12 13:44:06 -0800 | [diff] [blame] | 512 | while (true) { |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 513 | ptr = strstr(ptr, ext); |
Eric Anholt | 03e9537 | 2013-12-12 13:44:06 -0800 | [diff] [blame] | 514 | if (!ptr) |
| 515 | return false; |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 516 | |
Eric Anholt | 03e9537 | 2013-12-12 13:44:06 -0800 | [diff] [blame] | 517 | if (ptr[len] == ' ' || ptr[len] == 0) |
| 518 | return true; |
| 519 | ptr += len; |
| 520 | } |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 523 | static bool |
| 524 | epoxy_internal_has_gl_extension(const char *ext, bool invalid_op_mode) |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 525 | { |
Eric Anholt | 8bb5a25 | 2013-12-05 13:02:51 -0800 | [diff] [blame] | 526 | if (epoxy_gl_version() < 30) { |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 527 | const char *exts = (const char *)glGetString(GL_EXTENSIONS); |
| 528 | if (!exts) |
| 529 | return invalid_op_mode; |
| 530 | return epoxy_extension_in_string(exts, ext); |
Eric Anholt | 8bb5a25 | 2013-12-05 13:02:51 -0800 | [diff] [blame] | 531 | } else { |
| 532 | int num_extensions; |
Eric Anholt | 28c9c47 | 2014-06-10 14:16:06 -0700 | [diff] [blame] | 533 | int i; |
Eric Anholt | 8bb5a25 | 2013-12-05 13:02:51 -0800 | [diff] [blame] | 534 | |
| 535 | glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions); |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 536 | if (num_extensions == 0) |
| 537 | return invalid_op_mode; |
| 538 | |
Eric Anholt | 28c9c47 | 2014-06-10 14:16:06 -0700 | [diff] [blame] | 539 | for (i = 0; i < num_extensions; i++) { |
Eric Anholt | 1cb041f | 2013-12-05 13:07:09 -0800 | [diff] [blame] | 540 | const char *gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i); |
Gianfranco Costamagna | 106eae7 | 2017-04-24 16:10:28 +0200 | [diff] [blame] | 541 | if (!gl_ext) |
| 542 | return false; |
Eric Anholt | 8bb5a25 | 2013-12-05 13:02:51 -0800 | [diff] [blame] | 543 | if (strcmp(ext, gl_ext) == 0) |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | return false; |
| 548 | } |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Adam Jackson | f81274b | 2017-07-12 14:03:35 -0400 | [diff] [blame] | 551 | void * |
| 552 | epoxy_conservative_glx_dlsym(const char *name, bool exit_if_fails) |
| 553 | { |
| 554 | #ifdef GLVND_GLX_LIB |
| 555 | /* prefer the glvnd library if it exists */ |
| 556 | if (!api.glx_handle) |
| 557 | get_dlopen_handle(&api.glx_handle, GLVND_GLX_LIB, false); |
| 558 | #endif |
| 559 | |
| 560 | return do_dlsym(&api.glx_handle, GLX_LIB, name, exit_if_fails); |
| 561 | } |
| 562 | |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 563 | /** |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 564 | * Tests whether the currently bound context is EGL or GLX, trying to |
| 565 | * avoid loading libraries unless necessary. |
Eric Anholt | b251e3a | 2014-03-27 19:04:27 -0700 | [diff] [blame] | 566 | */ |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 567 | static bool |
| 568 | epoxy_current_context_is_glx(void) |
Eric Anholt | b251e3a | 2014-03-27 19:04:27 -0700 | [diff] [blame] | 569 | { |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 570 | #if !PLATFORM_HAS_GLX |
| 571 | return false; |
| 572 | #else |
Adam Jackson | 7c4817f | 2017-07-12 13:26:05 -0400 | [diff] [blame] | 573 | void *sym; |
| 574 | |
| 575 | /* If we've been called already, don't load more */ |
| 576 | if (!api.egl_handle != !api.glx_handle) { |
| 577 | if (api.glx_handle) |
| 578 | return true; |
| 579 | else if (api.egl_handle) |
| 580 | return false; |
| 581 | } |
| 582 | |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 583 | /* If the application hasn't explicitly called some of our GLX |
| 584 | * or EGL code but has presumably set up a context on its own, |
| 585 | * then we need to figure out how to getprocaddress anyway. |
| 586 | * |
| 587 | * If there's a public GetProcAddress loaded in the |
| 588 | * application's namespace, then use that. |
| 589 | */ |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 590 | |
| 591 | sym = dlsym(NULL, "glXGetCurrentContext"); |
| 592 | if (sym) { |
| 593 | if (glXGetCurrentContext()) |
| 594 | return true; |
| 595 | } else { |
| 596 | (void)dlerror(); |
| 597 | } |
| 598 | |
| 599 | #if PLATFORM_HAS_EGL |
| 600 | sym = dlsym(NULL, "eglGetCurrentContext"); |
| 601 | if (sym) { |
| 602 | if (epoxy_egl_get_current_gl_context_api() != EGL_NONE) |
| 603 | return false; |
| 604 | } else { |
| 605 | (void)dlerror(); |
| 606 | } |
| 607 | #endif /* PLATFORM_HAS_EGL */ |
| 608 | |
| 609 | /* OK, couldn't find anything in the app's address space. |
| 610 | * Presumably they dlopened with RTLD_LOCAL, which hides it |
| 611 | * from us. Just go dlopen()ing likely libraries and try them. |
| 612 | */ |
Adam Jackson | f81274b | 2017-07-12 14:03:35 -0400 | [diff] [blame] | 613 | sym = epoxy_conservative_glx_dlsym("glXGetCurrentContext", false); |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 614 | if (sym && glXGetCurrentContext()) |
Eric Anholt | b251e3a | 2014-03-27 19:04:27 -0700 | [diff] [blame] | 615 | return true; |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 616 | |
| 617 | #if PLATFORM_HAS_EGL |
| 618 | sym = do_dlsym(&api.egl_handle, EGL_LIB, "eglGetCurrentContext", |
| 619 | false); |
| 620 | if (sym && epoxy_egl_get_current_gl_context_api() != EGL_NONE) |
| 621 | return false; |
| 622 | #endif /* PLATFORM_HAS_EGL */ |
Eric Anholt | b251e3a | 2014-03-27 19:04:27 -0700 | [diff] [blame] | 623 | |
| 624 | return false; |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 625 | #endif /* PLATFORM_HAS_GLX */ |
Eric Anholt | b251e3a | 2014-03-27 19:04:27 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 629 | * @brief Returns true if the given GL extension is supported in the current context. |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 630 | * |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 631 | * @param ext The name of the GL extension |
| 632 | * @return `true` if the extension is available |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 633 | * |
Emmanuele Bassi | 6af57b0 | 2017-01-27 18:12:05 +0000 | [diff] [blame] | 634 | * @note that this function can't be called from within `glBegin()` and `glEnd()`. |
| 635 | * |
| 636 | * @see epoxy_has_egl_extension() |
| 637 | * @see epoxy_has_glx_extension() |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 638 | */ |
Emmanuele Bassi | 7a06803 | 2017-01-24 17:43:59 +0000 | [diff] [blame] | 639 | bool |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 640 | epoxy_has_gl_extension(const char *ext) |
| 641 | { |
| 642 | return epoxy_internal_has_gl_extension(ext, false); |
| 643 | } |
| 644 | |
| 645 | bool |
| 646 | epoxy_conservative_has_gl_extension(const char *ext) |
| 647 | { |
| 648 | if (api.begin_count) |
| 649 | return true; |
| 650 | |
| 651 | return epoxy_internal_has_gl_extension(ext, true); |
| 652 | } |
| 653 | |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 654 | void * |
Emmanuele Bassi | 30b8a4c | 2017-02-03 16:13:45 +0000 | [diff] [blame] | 655 | epoxy_conservative_egl_dlsym(const char *name, bool exit_if_fails) |
| 656 | { |
| 657 | return do_dlsym(&api.egl_handle, EGL_LIB, name, exit_if_fails); |
| 658 | } |
| 659 | |
| 660 | void * |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 661 | epoxy_egl_dlsym(const char *name) |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 662 | { |
Emmanuele Bassi | 30b8a4c | 2017-02-03 16:13:45 +0000 | [diff] [blame] | 663 | return epoxy_conservative_egl_dlsym(name, true); |
| 664 | } |
| 665 | |
| 666 | void * |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 667 | epoxy_glx_dlsym(const char *name) |
| 668 | { |
Emmanuele Bassi | 30b8a4c | 2017-02-03 16:13:45 +0000 | [diff] [blame] | 669 | return epoxy_conservative_glx_dlsym(name, true); |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void * |
| 673 | epoxy_gl_dlsym(const char *name) |
| 674 | { |
Eric Anholt | 1d746bf | 2013-12-09 14:52:19 -0800 | [diff] [blame] | 675 | #ifdef _WIN32 |
Eric Anholt | 5112b47 | 2013-12-17 10:38:29 -0800 | [diff] [blame] | 676 | return do_dlsym(&api.gl_handle, "OPENGL32", name, true); |
Eric Anholt | d82c5c3 | 2013-12-11 16:49:08 -0800 | [diff] [blame] | 677 | #elif defined(__APPLE__) |
| 678 | return do_dlsym(&api.gl_handle, |
| 679 | "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", |
| 680 | name, true); |
Eric Anholt | 1d746bf | 2013-12-09 14:52:19 -0800 | [diff] [blame] | 681 | #else |
Adam Jackson | 759de64 | 2017-06-16 14:37:06 -0400 | [diff] [blame] | 682 | void *sym; |
| 683 | |
Emmanuele Bassi | f6d2b1f | 2018-02-23 19:59:48 +0000 | [diff] [blame] | 684 | # if defined(OPENGL_LIB) |
Adam Jackson | 759de64 | 2017-06-16 14:37:06 -0400 | [diff] [blame] | 685 | if (!api.gl_handle) |
| 686 | get_dlopen_handle(&api.gl_handle, OPENGL_LIB, false); |
Emmanuele Bassi | f6d2b1f | 2018-02-23 19:59:48 +0000 | [diff] [blame] | 687 | # endif |
Adam Jackson | 759de64 | 2017-06-16 14:37:06 -0400 | [diff] [blame] | 688 | |
| 689 | if (api.gl_handle) |
| 690 | return do_dlsym(&api.gl_handle, NULL, name, true); |
| 691 | |
| 692 | sym = do_dlsym(&api.glx_handle, GLX_LIB, name, true); |
| 693 | api.gl_handle = api.glx_handle; /* skip the dlopen next time */ |
| 694 | |
| 695 | return sym; |
Eric Anholt | 1d746bf | 2013-12-09 14:52:19 -0800 | [diff] [blame] | 696 | #endif |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void * |
| 700 | epoxy_gles1_dlsym(const char *name) |
| 701 | { |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 702 | if (epoxy_current_context_is_glx()) { |
Eric Anholt | db5b939 | 2014-03-27 19:06:21 -0700 | [diff] [blame] | 703 | return epoxy_get_proc_address(name); |
| 704 | } else { |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 705 | return do_dlsym(&api.gles1_handle, GLES1_LIB, name, true); |
Eric Anholt | db5b939 | 2014-03-27 19:06:21 -0700 | [diff] [blame] | 706 | } |
Eric Anholt | 0270c80 | 2013-12-02 16:25:02 -0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void * |
| 710 | epoxy_gles2_dlsym(const char *name) |
| 711 | { |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 712 | if (epoxy_current_context_is_glx()) { |
Eric Anholt | db5b939 | 2014-03-27 19:06:21 -0700 | [diff] [blame] | 713 | return epoxy_get_proc_address(name); |
| 714 | } else { |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 715 | return do_dlsym(&api.gles2_handle, GLES2_LIB, name, true); |
Eric Anholt | db5b939 | 2014-03-27 19:06:21 -0700 | [diff] [blame] | 716 | } |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 719 | /** |
Eric Anholt | 14f2448 | 2014-03-17 11:17:19 -0700 | [diff] [blame] | 720 | * Does the appropriate dlsym() or eglGetProcAddress() for GLES3 |
| 721 | * functions. |
| 722 | * |
| 723 | * Mesa interpreted GLES as intending that the GLES3 functions were |
| 724 | * available only through eglGetProcAddress() and not dlsym(), while |
| 725 | * ARM's Mali drivers interpreted GLES as intending that GLES3 |
| 726 | * functions were available only through dlsym() and not |
| 727 | * eglGetProcAddress(). Thanks, Khronos. |
| 728 | */ |
| 729 | void * |
| 730 | epoxy_gles3_dlsym(const char *name) |
| 731 | { |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 732 | if (epoxy_current_context_is_glx()) { |
Eric Anholt | db5b939 | 2014-03-27 19:06:21 -0700 | [diff] [blame] | 733 | return epoxy_get_proc_address(name); |
| 734 | } else { |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 735 | void *func = do_dlsym(&api.gles2_handle, GLES2_LIB, name, false); |
Eric Anholt | 14f2448 | 2014-03-17 11:17:19 -0700 | [diff] [blame] | 736 | |
Eric Anholt | db5b939 | 2014-03-27 19:06:21 -0700 | [diff] [blame] | 737 | if (func) |
| 738 | return func; |
Eric Anholt | 14f2448 | 2014-03-17 11:17:19 -0700 | [diff] [blame] | 739 | |
Eric Anholt | db5b939 | 2014-03-27 19:06:21 -0700 | [diff] [blame] | 740 | return epoxy_get_proc_address(name); |
| 741 | } |
Eric Anholt | 14f2448 | 2014-03-17 11:17:19 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /** |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 745 | * Performs either the dlsym or glXGetProcAddress()-equivalent for |
| 746 | * core functions in desktop GL. |
| 747 | */ |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 748 | void * |
Eric Anholt | b128dd9 | 2013-12-09 22:14:09 -0800 | [diff] [blame] | 749 | epoxy_get_core_proc_address(const char *name, int core_version) |
| 750 | { |
Eric Anholt | 1d746bf | 2013-12-09 14:52:19 -0800 | [diff] [blame] | 751 | #ifdef _WIN32 |
Eric Anholt | 227d131 | 2015-07-15 13:52:56 -0700 | [diff] [blame] | 752 | int core_symbol_support = 11; |
Robert Bragg | 4e5e17e | 2018-01-18 21:21:15 +0000 | [diff] [blame] | 753 | #elif defined(__ANDROID__) |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 754 | /** |
| 755 | * All symbols must be resolved through eglGetProcAddress |
| 756 | * on Android |
| 757 | */ |
| 758 | int core_symbol_support = 0; |
Eric Anholt | 1d746bf | 2013-12-09 14:52:19 -0800 | [diff] [blame] | 759 | #else |
| 760 | int core_symbol_support = 12; |
| 761 | #endif |
| 762 | |
| 763 | if (core_version <= core_symbol_support) { |
Eric Anholt | b128dd9 | 2013-12-09 22:14:09 -0800 | [diff] [blame] | 764 | return epoxy_gl_dlsym(name); |
| 765 | } else { |
| 766 | return epoxy_get_proc_address(name); |
| 767 | } |
| 768 | } |
| 769 | |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 770 | #if PLATFORM_HAS_EGL |
Eric Anholt | b670c84 | 2014-03-19 18:24:29 -0700 | [diff] [blame] | 771 | static EGLenum |
| 772 | epoxy_egl_get_current_gl_context_api(void) |
| 773 | { |
Adam Jackson | 9191626 | 2016-10-18 11:08:12 -0400 | [diff] [blame] | 774 | EGLint curapi; |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 775 | |
Adam Jackson | 9191626 | 2016-10-18 11:08:12 -0400 | [diff] [blame] | 776 | if (eglQueryContext(eglGetCurrentDisplay(), eglGetCurrentContext(), |
| 777 | EGL_CONTEXT_CLIENT_TYPE, &curapi) == EGL_FALSE) { |
| 778 | (void)eglGetError(); |
| 779 | return EGL_NONE; |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Adam Jackson | 9191626 | 2016-10-18 11:08:12 -0400 | [diff] [blame] | 782 | return (EGLenum) curapi; |
Eric Anholt | b670c84 | 2014-03-19 18:24:29 -0700 | [diff] [blame] | 783 | } |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 784 | #endif /* PLATFORM_HAS_EGL */ |
Eric Anholt | b670c84 | 2014-03-19 18:24:29 -0700 | [diff] [blame] | 785 | |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 786 | /** |
| 787 | * Performs the dlsym() for the core GL 1.0 functions that we use for |
| 788 | * determining version and extension support for deciding on dlsym |
| 789 | * versus glXGetProcAddress() for all other functions. |
| 790 | * |
| 791 | * This needs to succeed on implementations without GLX (since |
| 792 | * glGetString() and glGetIntegerv() are both in GLES1/2 as well, and |
| 793 | * at call time we don't know for sure what API they're trying to use |
| 794 | * without inspecting contexts ourselves). |
| 795 | */ |
| 796 | void * |
| 797 | epoxy_get_bootstrap_proc_address(const char *name) |
| 798 | { |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 799 | /* If we already have a library that links to libglapi loaded, |
| 800 | * use that. |
| 801 | */ |
| 802 | #if PLATFORM_HAS_GLX |
| 803 | if (api.glx_handle && glXGetCurrentContext()) |
| 804 | return epoxy_gl_dlsym(name); |
| 805 | #endif |
| 806 | |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 807 | /* If epoxy hasn't loaded any API-specific library yet, try to |
| 808 | * figure out what API the context is using and use that library, |
| 809 | * since future calls will also use that API (this prevents a |
| 810 | * non-X11 ES2 context from loading a bunch of X11 junk). |
| 811 | */ |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 812 | #if PLATFORM_HAS_EGL |
Ryan Houdek | c791612 | 2014-05-04 19:05:34 -0500 | [diff] [blame] | 813 | get_dlopen_handle(&api.egl_handle, EGL_LIB, false); |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 814 | if (api.egl_handle) { |
Eric Anholt | b670c84 | 2014-03-19 18:24:29 -0700 | [diff] [blame] | 815 | switch (epoxy_egl_get_current_gl_context_api()) { |
| 816 | case EGL_OPENGL_API: |
| 817 | return epoxy_gl_dlsym(name); |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 818 | case EGL_OPENGL_ES_API: |
| 819 | /* We can't resolve the GL version, because |
| 820 | * epoxy_glGetString() is one of the two things calling |
| 821 | * us. Try the GLES2 implementation first, and fall back |
| 822 | * to GLES1 otherwise. |
| 823 | */ |
| 824 | get_dlopen_handle(&api.gles2_handle, GLES2_LIB, false); |
| 825 | if (api.gles2_handle) |
| 826 | return epoxy_gles2_dlsym(name); |
Eric Anholt | b670c84 | 2014-03-19 18:24:29 -0700 | [diff] [blame] | 827 | else |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 828 | return epoxy_gles1_dlsym(name); |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 829 | } |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 830 | } |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 831 | #endif /* PLATFORM_HAS_EGL */ |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 832 | |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 833 | /* Fall back to GLX */ |
Eric Anholt | 1064406 | 2014-01-31 14:49:00 -0800 | [diff] [blame] | 834 | return epoxy_gl_dlsym(name); |
| 835 | } |
| 836 | |
Eric Anholt | b128dd9 | 2013-12-09 22:14:09 -0800 | [diff] [blame] | 837 | void * |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 838 | epoxy_get_proc_address(const char *name) |
| 839 | { |
Emmanuele Bassi | bac9400 | 2017-01-18 15:36:54 +0000 | [diff] [blame] | 840 | #if PLATFORM_HAS_EGL |
| 841 | GLenum egl_api = EGL_NONE; |
| 842 | |
| 843 | if (!epoxy_current_context_is_glx()) |
| 844 | egl_api = epoxy_egl_get_current_gl_context_api(); |
| 845 | |
| 846 | switch (egl_api) { |
| 847 | case EGL_OPENGL_API: |
| 848 | case EGL_OPENGL_ES_API: |
| 849 | return eglGetProcAddress(name); |
| 850 | case EGL_NONE: |
| 851 | break; |
| 852 | } |
| 853 | #endif |
| 854 | |
| 855 | #if defined(_WIN32) |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 856 | return wglGetProcAddress(name); |
| 857 | #elif defined(__APPLE__) |
Eric Anholt | 2688054 | 2013-12-15 20:21:02 -0800 | [diff] [blame] | 858 | return epoxy_gl_dlsym(name); |
Emmanuele Bassi | bac9400 | 2017-01-18 15:36:54 +0000 | [diff] [blame] | 859 | #elif PLATFORM_HAS_GLX |
| 860 | if (epoxy_current_context_is_glx()) |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 861 | return glXGetProcAddressARB((const GLubyte *)name); |
Emmanuele Bassi | 8dead45 | 2016-12-07 14:45:14 +0000 | [diff] [blame] | 862 | errx(1, "Couldn't find current GLX or EGL context.\n"); |
Emmanuele Bassi | 5df022e | 2017-01-19 17:58:59 +0000 | [diff] [blame] | 863 | #endif |
Emmanuele Bassi | bac9400 | 2017-01-18 15:36:54 +0000 | [diff] [blame] | 864 | |
| 865 | return NULL; |
Eric Anholt | a909eb4 | 2013-09-19 09:50:49 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Chun-wei Fan | 7d5e8e9 | 2015-06-05 17:38:26 -0700 | [diff] [blame] | 868 | WRAPPER_VISIBILITY (void) |
Eric Anholt | 3d2a2b3 | 2013-12-13 14:41:12 -0800 | [diff] [blame] | 869 | WRAPPER(epoxy_glBegin)(GLenum primtype) |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 870 | { |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 871 | #ifdef _WIN32 |
Eric Anholt | 4eaf4bf | 2013-12-10 14:09:37 -0800 | [diff] [blame] | 872 | InterlockedIncrement(&api.begin_count); |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 873 | #else |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 874 | pthread_mutex_lock(&api.mutex); |
| 875 | api.begin_count++; |
| 876 | pthread_mutex_unlock(&api.mutex); |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 877 | #endif |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 878 | |
| 879 | epoxy_glBegin_unwrapped(primtype); |
| 880 | } |
| 881 | |
Chun-wei Fan | 7d5e8e9 | 2015-06-05 17:38:26 -0700 | [diff] [blame] | 882 | WRAPPER_VISIBILITY (void) |
Eric Anholt | 3d2a2b3 | 2013-12-13 14:41:12 -0800 | [diff] [blame] | 883 | WRAPPER(epoxy_glEnd)(void) |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 884 | { |
| 885 | epoxy_glEnd_unwrapped(); |
| 886 | |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 887 | #ifdef _WIN32 |
Eric Anholt | 4eaf4bf | 2013-12-10 14:09:37 -0800 | [diff] [blame] | 888 | InterlockedDecrement(&api.begin_count); |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 889 | #else |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 890 | pthread_mutex_lock(&api.mutex); |
| 891 | api.begin_count--; |
| 892 | pthread_mutex_unlock(&api.mutex); |
Eric Anholt | 2591368 | 2013-12-06 13:40:30 -0800 | [diff] [blame] | 893 | #endif |
Eric Anholt | 0983996 | 2013-12-05 13:59:34 -0800 | [diff] [blame] | 894 | } |
Eric Anholt | 3d2a2b3 | 2013-12-13 14:41:12 -0800 | [diff] [blame] | 895 | |
Emmanuele Bassi | 7a06803 | 2017-01-24 17:43:59 +0000 | [diff] [blame] | 896 | PFNGLBEGINPROC epoxy_glBegin = epoxy_glBegin_wrapped; |
| 897 | PFNGLENDPROC epoxy_glEnd = epoxy_glEnd_wrapped; |
Adam Jackson | 7ff061a | 2017-07-05 16:54:15 -0400 | [diff] [blame] | 898 | |
| 899 | epoxy_resolver_failure_handler_t epoxy_resolver_failure_handler; |
| 900 | |
Emmanuele Bassi | 158ce2b | 2018-02-23 16:56:52 +0000 | [diff] [blame] | 901 | /** |
| 902 | * Sets the function that will be called every time Epoxy fails to |
| 903 | * resolve a symbol. |
| 904 | * |
| 905 | * @param handler The new handler function |
| 906 | * @return The previous handler function |
| 907 | */ |
Adam Jackson | 7ff061a | 2017-07-05 16:54:15 -0400 | [diff] [blame] | 908 | epoxy_resolver_failure_handler_t |
| 909 | epoxy_set_resolver_failure_handler(epoxy_resolver_failure_handler_t handler) |
| 910 | { |
| 911 | #ifdef _WIN32 |
danem | 60271ab | 2017-10-13 16:20:11 -0700 | [diff] [blame] | 912 | return InterlockedExchangePointer((void**)&epoxy_resolver_failure_handler, |
Adam Jackson | 7ff061a | 2017-07-05 16:54:15 -0400 | [diff] [blame] | 913 | handler); |
| 914 | #else |
| 915 | epoxy_resolver_failure_handler_t old; |
| 916 | pthread_mutex_lock(&api.mutex); |
| 917 | old = epoxy_resolver_failure_handler; |
| 918 | epoxy_resolver_failure_handler = handler; |
| 919 | pthread_mutex_unlock(&api.mutex); |
| 920 | return old; |
| 921 | #endif |
| 922 | } |