Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
Mathias Agopian | d8fb7b5 | 2009-05-17 18:50:16 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <string.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 21 | #include <hardware/gralloc.h> |
| 22 | #include <system/window.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | #include <EGL/egl.h> |
| 25 | #include <EGL/eglext.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include <cutils/log.h> |
| 28 | #include <cutils/atomic.h> |
| 29 | #include <cutils/properties.h> |
| 30 | #include <cutils/memory.h> |
| 31 | |
Romain Guy | e03de93 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 32 | #include <utils/CallStack.h> |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 33 | #include <utils/String8.h> |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 34 | |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame^] | 35 | #include "../egl_impl.h" |
| 36 | #include "../glestrace.h" |
| 37 | |
David Li | 864f839 | 2011-03-28 10:39:28 -0700 | [diff] [blame] | 38 | #include "egl_tls.h" |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame^] | 39 | #include "egldefs.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 40 | #include "Loader.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 42 | #include "egl_display.h" |
| 43 | #include "egl_object.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | namespace android { |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 49 | egl_connection_t gEGLImpl; |
| 50 | gl_hooks_t gHooks[2]; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 51 | gl_hooks_t gHooksNoContext; |
| 52 | pthread_key_t gGLWrapperKey = -1; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 53 | |
| 54 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 56 | #if EGL_TRACE |
| 57 | |
| 58 | EGLAPI pthread_key_t gGLTraceKey = -1; |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 62 | /** |
Romain Guy | 16928bf | 2012-10-18 16:16:10 -0700 | [diff] [blame] | 63 | * There are three different tracing methods: |
| 64 | * 1. libs/EGL/trace.cpp: Traces all functions to systrace. |
| 65 | * To enable: |
| 66 | * - set system property "debug.egl.trace" to "systrace" to trace all apps. |
| 67 | * 2. libs/EGL/trace.cpp: Logs a stack trace for GL errors after each function call. |
| 68 | * To enable: |
| 69 | * - set system property "debug.egl.trace" to "error" to trace all apps. |
| 70 | * 3. libs/EGL/trace.cpp: Traces all functions to logcat. |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 71 | * To enable: |
| 72 | * - set system property "debug.egl.trace" to 1 to trace all apps. |
| 73 | * - or call setGLTraceLevel(1) from an app to enable tracing for that app. |
Romain Guy | 16928bf | 2012-10-18 16:16:10 -0700 | [diff] [blame] | 74 | * 4. libs/GLES_trace: Traces all functions via protobuf to host. |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 75 | * To enable: |
| 76 | * - set system property "debug.egl.debug_proc" to the application name. |
| 77 | * - or call setGLDebugLevel(1) from the app. |
| 78 | */ |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 79 | static int sEGLTraceLevel; |
| 80 | static int sEGLApplicationTraceLevel; |
| 81 | |
Romain Guy | 16928bf | 2012-10-18 16:16:10 -0700 | [diff] [blame] | 82 | static bool sEGLSystraceEnabled; |
| 83 | static bool sEGLGetErrorEnabled; |
| 84 | |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 85 | static volatile int sEGLDebugLevel; |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 86 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 87 | extern gl_hooks_t gHooksTrace; |
Romain Guy | 16928bf | 2012-10-18 16:16:10 -0700 | [diff] [blame] | 88 | extern gl_hooks_t gHooksSystrace; |
| 89 | extern gl_hooks_t gHooksErrorTrace; |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 90 | |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 91 | int getEGLDebugLevel() { |
| 92 | return sEGLDebugLevel; |
| 93 | } |
| 94 | |
| 95 | void setEGLDebugLevel(int level) { |
| 96 | sEGLDebugLevel = level; |
| 97 | } |
| 98 | |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 99 | static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) { |
| 100 | pthread_setspecific(gGLTraceKey, value); |
| 101 | } |
| 102 | |
| 103 | gl_hooks_t const* getGLTraceThreadSpecific() { |
| 104 | return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey)); |
| 105 | } |
| 106 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 107 | void initEglTraceLevel() { |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 108 | char value[PROPERTY_VALUE_MAX]; |
| 109 | property_get("debug.egl.trace", value, "0"); |
Romain Guy | 16928bf | 2012-10-18 16:16:10 -0700 | [diff] [blame] | 110 | |
| 111 | sEGLGetErrorEnabled = !strcasecmp(value, "error"); |
| 112 | if (sEGLGetErrorEnabled) { |
| 113 | sEGLSystraceEnabled = false; |
| 114 | sEGLTraceLevel = 0; |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | sEGLSystraceEnabled = !strcasecmp(value, "systrace"); |
| 119 | if (sEGLSystraceEnabled) { |
| 120 | sEGLTraceLevel = 0; |
| 121 | return; |
| 122 | } |
| 123 | |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 124 | int propertyLevel = atoi(value); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 125 | int applicationLevel = sEGLApplicationTraceLevel; |
| 126 | sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel; |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 127 | } |
David Li | 499c6f0 | 2011-04-08 18:43:16 -0700 | [diff] [blame] | 128 | |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 129 | void initEglDebugLevel() { |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 130 | if (getEGLDebugLevel() == 0) { |
| 131 | char value[PROPERTY_VALUE_MAX]; |
Siva Velusamy | 6482fa4 | 2012-11-20 13:39:57 -0800 | [diff] [blame] | 132 | |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 133 | // check system property only on userdebug or eng builds |
| 134 | property_get("ro.debuggable", value, "0"); |
| 135 | if (value[0] == '0') |
| 136 | return; |
Siva Velusamy | 6482fa4 | 2012-11-20 13:39:57 -0800 | [diff] [blame] | 137 | |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 138 | property_get("debug.egl.debug_proc", value, ""); |
| 139 | if (strlen(value) > 0) { |
| 140 | FILE * file = fopen("/proc/self/cmdline", "r"); |
| 141 | if (file) { |
| 142 | char cmdline[256]; |
| 143 | if (fgets(cmdline, sizeof(cmdline), file)) { |
| 144 | if (!strncmp(value, cmdline, strlen(value))) { |
| 145 | // set EGL debug if the "debug.egl.debug_proc" property |
| 146 | // matches the prefix of this application's command line |
| 147 | setEGLDebugLevel(1); |
| 148 | } |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 149 | } |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 150 | fclose(file); |
Siva Velusamy | 93a826f | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 151 | } |
David Li | 499c6f0 | 2011-04-08 18:43:16 -0700 | [diff] [blame] | 152 | } |
David Li | 2f5a655 | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 153 | } |
David Li | 499c6f0 | 2011-04-08 18:43:16 -0700 | [diff] [blame] | 154 | |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 155 | if (getEGLDebugLevel() > 0) { |
| 156 | if (GLTrace_start() < 0) { |
| 157 | ALOGE("Error starting Tracer for OpenGL ES. Disabling.."); |
| 158 | setEGLDebugLevel(0); |
| 159 | } |
David Li | 85f33a7 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 160 | } |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 163 | void setGLHooksThreadSpecific(gl_hooks_t const *value) { |
Romain Guy | 16928bf | 2012-10-18 16:16:10 -0700 | [diff] [blame] | 164 | if (sEGLGetErrorEnabled) { |
| 165 | setGlTraceThreadSpecific(value); |
| 166 | setGlThreadSpecific(&gHooksErrorTrace); |
| 167 | } else if (sEGLSystraceEnabled) { |
| 168 | setGlTraceThreadSpecific(value); |
| 169 | setGlThreadSpecific(&gHooksSystrace); |
| 170 | } else if (sEGLTraceLevel > 0) { |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 171 | setGlTraceThreadSpecific(value); |
| 172 | setGlThreadSpecific(&gHooksTrace); |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 173 | } else if (getEGLDebugLevel() > 0 && value != &gHooksNoContext) { |
David Li | 2f5a655 | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 174 | setGlTraceThreadSpecific(value); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 175 | setGlThreadSpecific(GLTrace_getGLHooks()); |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 176 | } else { |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 177 | setGlTraceThreadSpecific(NULL); |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 178 | setGlThreadSpecific(value); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Global entry point to allow applications to modify their own trace level. |
| 184 | * The effective trace level is the max of this level and the value of debug.egl.trace. |
| 185 | */ |
| 186 | extern "C" |
| 187 | void setGLTraceLevel(int level) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 188 | sEGLApplicationTraceLevel = level; |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 191 | /* |
| 192 | * Global entry point to allow applications to modify their own debug level. |
| 193 | * Debugging is enabled if either the application requested it, or if the system property |
| 194 | * matches the application's name. |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 195 | * Note that this only sets the debug level. The value is read and used either in |
| 196 | * initEglDebugLevel() if the application hasn't initialized its display yet, or when |
| 197 | * eglSwapBuffers() is called next. |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 198 | */ |
| 199 | void EGLAPI setGLDebugLevel(int level) { |
Siva Velusamy | a73a977 | 2012-12-18 14:56:55 -0800 | [diff] [blame] | 200 | setEGLDebugLevel(level); |
Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 203 | #else |
| 204 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 205 | void setGLHooksThreadSpecific(gl_hooks_t const *value) { |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 206 | setGlThreadSpecific(value); |
| 207 | } |
| 208 | |
| 209 | #endif |
| 210 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | /*****************************************************************************/ |
| 212 | |
Mathias Agopian | 6f08712 | 2010-09-23 16:38:38 -0700 | [diff] [blame] | 213 | static int gl_no_context() { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 214 | if (egl_tls_t::logNoContextCall()) { |
Mathias Agopian | 455e360 | 2012-09-26 17:19:48 -0700 | [diff] [blame] | 215 | char const* const error = "call to OpenGL ES API with " |
| 216 | "no current context (logged once per thread)"; |
| 217 | if (LOG_NDEBUG) { |
| 218 | ALOGE(error); |
| 219 | } else { |
| 220 | LOG_ALWAYS_FATAL(error); |
| 221 | } |
Mathias Agopian | ecfe091 | 2011-09-06 17:24:05 -0700 | [diff] [blame] | 222 | char value[PROPERTY_VALUE_MAX]; |
| 223 | property_get("debug.egl.callstack", value, "0"); |
| 224 | if (atoi(value)) { |
Mathias Agopian | cab25d6 | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 225 | CallStack stack(LOG_TAG); |
Mathias Agopian | ecfe091 | 2011-09-06 17:24:05 -0700 | [diff] [blame] | 226 | } |
Mathias Agopian | d274eae | 2009-07-31 16:21:17 -0700 | [diff] [blame] | 227 | } |
Mathias Agopian | 6f08712 | 2010-09-23 16:38:38 -0700 | [diff] [blame] | 228 | return 0; |
Mathias Agopian | 05c5311 | 2010-09-23 11:32:52 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 231 | static void early_egl_init(void) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | { |
| 233 | #if !USE_FAST_TLS_KEY |
| 234 | pthread_key_create(&gGLWrapperKey, NULL); |
| 235 | #endif |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 236 | #if EGL_TRACE |
| 237 | pthread_key_create(&gGLTraceKey, NULL); |
| 238 | initEglTraceLevel(); |
| 239 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | uint32_t addr = (uint32_t)((void*)gl_no_context); |
| 241 | android_memset32( |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 242 | (uint32_t*)(void*)&gHooksNoContext, |
| 243 | addr, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 244 | sizeof(gHooksNoContext)); |
Mathias Agopian | 05c5311 | 2010-09-23 11:32:52 -0700 | [diff] [blame] | 245 | |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 246 | setGLHooksThreadSpecific(&gHooksNoContext); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 250 | static int sEarlyInitState = pthread_once(&once_control, &early_egl_init); |
| 251 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 252 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 254 | egl_display_ptr validate_display(EGLDisplay dpy) { |
| 255 | egl_display_ptr dp = get_display(dpy); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 256 | if (!dp) |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 257 | return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL)); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 258 | if (!dp->isReady()) |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 259 | return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL)); |
Eric Hassold | 3ede7c1 | 2011-03-23 15:59:00 -0700 | [diff] [blame] | 260 | |
| 261 | return dp; |
| 262 | } |
| 263 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 264 | egl_display_ptr validate_display_connection(EGLDisplay dpy, |
| 265 | egl_connection_t*& cnx) { |
| 266 | cnx = NULL; |
| 267 | egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 268 | if (!dp) |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 269 | return dp; |
| 270 | cnx = &gEGLImpl; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | if (cnx->dso == 0) { |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 272 | return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | } |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 274 | return dp; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 277 | // ---------------------------------------------------------------------------- |
| 278 | |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 279 | const GLubyte * egl_get_string_for_current_context(GLenum name) { |
| 280 | // NOTE: returning NULL here will fall-back to the default |
| 281 | // implementation. |
| 282 | |
| 283 | EGLContext context = egl_tls_t::getContext(); |
| 284 | if (context == EGL_NO_CONTEXT) |
| 285 | return NULL; |
| 286 | |
| 287 | egl_context_t const * const c = get_context(context); |
| 288 | if (c == NULL) // this should never happen, by construction |
| 289 | return NULL; |
| 290 | |
| 291 | if (name != GL_EXTENSIONS) |
| 292 | return NULL; |
| 293 | |
| 294 | return (const GLubyte *)c->gl_extensions.string(); |
| 295 | } |
| 296 | |
| 297 | // ---------------------------------------------------------------------------- |
| 298 | |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 299 | // this mutex protects: |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 300 | // d->disp[] |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 301 | // egl_init_drivers_locked() |
| 302 | // |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 303 | static EGLBoolean egl_init_drivers_locked() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | if (sEarlyInitState) { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 305 | // initialized by static ctor. should be set here. |
| 306 | return EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 309 | // get our driver loader |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 310 | Loader& loader(Loader::getInstance()); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 311 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 312 | // dynamically load our EGL implementation |
| 313 | egl_connection_t* cnx = &gEGLImpl; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | if (cnx->dso == 0) { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 315 | cnx->hooks[egl_connection_t::GLESv1_INDEX] = |
| 316 | &gHooks[egl_connection_t::GLESv1_INDEX]; |
| 317 | cnx->hooks[egl_connection_t::GLESv2_INDEX] = |
| 318 | &gHooks[egl_connection_t::GLESv2_INDEX]; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 319 | cnx->dso = loader.open(cnx); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 322 | return cnx->dso ? EGL_TRUE : EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 325 | static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER; |
| 326 | |
| 327 | EGLBoolean egl_init_drivers() { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 328 | EGLBoolean res; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 329 | pthread_mutex_lock(&sInitDriverMutex); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 330 | res = egl_init_drivers_locked(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 331 | pthread_mutex_unlock(&sInitDriverMutex); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 332 | return res; |
| 333 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 334 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 335 | void gl_unimplemented() { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 336 | ALOGE("called unimplemented OpenGL ES API"); |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 339 | void gl_noop() { |
| 340 | } |
| 341 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 342 | // ---------------------------------------------------------------------------- |
| 343 | |
| 344 | #if USE_FAST_TLS_KEY |
| 345 | |
| 346 | // We have a dedicated TLS slot in bionic |
| 347 | static inline gl_hooks_t const * volatile * get_tls_hooks() { |
| 348 | volatile void *tls_base = __get_tls(); |
| 349 | gl_hooks_t const * volatile * tls_hooks = |
| 350 | reinterpret_cast<gl_hooks_t const * volatile *>(tls_base); |
| 351 | return tls_hooks; |
| 352 | } |
| 353 | |
| 354 | void setGlThreadSpecific(gl_hooks_t const *value) { |
| 355 | gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); |
| 356 | tls_hooks[TLS_SLOT_OPENGL_API] = value; |
| 357 | } |
| 358 | |
| 359 | gl_hooks_t const* getGlThreadSpecific() { |
| 360 | gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); |
| 361 | gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API]; |
| 362 | if (hooks) return hooks; |
| 363 | return &gHooksNoContext; |
| 364 | } |
| 365 | |
| 366 | #else |
| 367 | |
| 368 | void setGlThreadSpecific(gl_hooks_t const *value) { |
| 369 | pthread_setspecific(gGLWrapperKey, value); |
| 370 | } |
| 371 | |
| 372 | gl_hooks_t const* getGlThreadSpecific() { |
| 373 | gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey)); |
| 374 | if (hooks) return hooks; |
| 375 | return &gHooksNoContext; |
| 376 | } |
| 377 | |
| 378 | #endif |
| 379 | |
| 380 | // ---------------------------------------------------------------------------- |
| 381 | // GL / EGL hooks |
| 382 | // ---------------------------------------------------------------------------- |
| 383 | |
| 384 | #undef GL_ENTRY |
| 385 | #undef EGL_ENTRY |
| 386 | #define GL_ENTRY(_r, _api, ...) #_api, |
| 387 | #define EGL_ENTRY(_r, _api, ...) #_api, |
| 388 | |
| 389 | char const * const gl_names[] = { |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame^] | 390 | #include "../entries.in" |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 391 | NULL |
| 392 | }; |
| 393 | |
| 394 | char const * const egl_names[] = { |
| 395 | #include "egl_entries.in" |
| 396 | NULL |
| 397 | }; |
| 398 | |
| 399 | #undef GL_ENTRY |
| 400 | #undef EGL_ENTRY |
| 401 | |
| 402 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 403 | // ---------------------------------------------------------------------------- |
| 404 | }; // namespace android |
| 405 | // ---------------------------------------------------------------------------- |
| 406 | |