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