1 /* 2 ** Copyright 2007, The Android Open Source Project 3 ** 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 7 ** 8 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** 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 14 ** limitations under the License. 15 */ 16 17 #include <ctype.h> 18 #include <stdlib.h> 19 #include <string.h> 20 21 #include <hardware/gralloc.h> 22 #include <system/window.h> 23 24 #include <EGL/egl.h> 25 #include <EGL/eglext.h> 26 #include <GLES/gl.h> 27 #include <GLES/glext.h> 28 29 #include <cutils/log.h> 30 #include <cutils/atomic.h> 31 #include <cutils/properties.h> 32 #include <cutils/memory.h> 33 34 #include <utils/CallStack.h> 35 #include <utils/String8.h> 36 37 #include "egldefs.h" 38 #include "egl_impl.h" 39 #include "egl_tls.h" 40 #include "glestrace.h" 41 #include "hooks.h" 42 #include "Loader.h" 43 44 #include "egl_display.h" 45 #include "egl_object.h" 46 47 // ---------------------------------------------------------------------------- 48 namespace android { 49 // ---------------------------------------------------------------------------- 50 51 egl_connection_t gEGLImpl; 52 gl_hooks_t gHooks[2]; 53 gl_hooks_t gHooksNoContext; 54 pthread_key_t gGLWrapperKey = -1; 55 56 // ---------------------------------------------------------------------------- 57 58 #if EGL_TRACE 59 60 EGLAPI pthread_key_t gGLTraceKey = -1; 61 62 // ---------------------------------------------------------------------------- 63 64 /** 65 * There are three different tracing methods: 66 * 1. libs/EGL/trace.cpp: Traces all functions to systrace. 67 * To enable: 68 * - set system property "debug.egl.trace" to "systrace" to trace all apps. 69 * 2. libs/EGL/trace.cpp: Logs a stack trace for GL errors after each function call. 70 * To enable: 71 * - set system property "debug.egl.trace" to "error" to trace all apps. 72 * 3. libs/EGL/trace.cpp: Traces all functions to logcat. 73 * To enable: 74 * - set system property "debug.egl.trace" to 1 to trace all apps. 75 * - or call setGLTraceLevel(1) from an app to enable tracing for that app. 76 * 4. libs/GLES_trace: Traces all functions via protobuf to host. 77 * To enable: 78 * - set system property "debug.egl.debug_proc" to the application name. 79 * - or call setGLDebugLevel(1) from the app. 80 */ 81 static int sEGLTraceLevel; 82 static int sEGLApplicationTraceLevel; 83 84 static bool sEGLSystraceEnabled; 85 static bool sEGLGetErrorEnabled; 86 87 int gEGLDebugLevel; 88 static int sEGLApplicationDebugLevel; 89 90 extern gl_hooks_t gHooksTrace; 91 extern gl_hooks_t gHooksSystrace; 92 extern gl_hooks_t gHooksErrorTrace; 93 94 static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) { 95 pthread_setspecific(gGLTraceKey, value); 96 } 97 98 gl_hooks_t const* getGLTraceThreadSpecific() { 99 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey)); 100 } 101 102 void initEglTraceLevel() { 103 char value[PROPERTY_VALUE_MAX]; 104 property_get("debug.egl.trace", value, "0"); 105 106 sEGLGetErrorEnabled = !strcasecmp(value, "error"); 107 if (sEGLGetErrorEnabled) { 108 sEGLSystraceEnabled = false; 109 sEGLTraceLevel = 0; 110 return; 111 } 112 113 sEGLSystraceEnabled = !strcasecmp(value, "systrace"); 114 if (sEGLSystraceEnabled) { 115 sEGLTraceLevel = 0; 116 return; 117 } 118 119 int propertyLevel = atoi(value); 120 int applicationLevel = sEGLApplicationTraceLevel; 121 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel; 122 } 123 124 void initEglDebugLevel() { 125 int propertyLevel = 0; 126 char value[PROPERTY_VALUE_MAX]; 127 property_get("debug.egl.debug_proc", value, ""); 128 if (strlen(value) > 0) { 129 long pid = getpid(); 130 char procPath[128] = {}; 131 sprintf(procPath, "/proc/%ld/cmdline", pid); 132 FILE * file = fopen(procPath, "r"); 133 if (file) { 134 char cmdline[256] = {}; 135 if (fgets(cmdline, sizeof(cmdline) - 1, file)) { 136 if (!strncmp(value, cmdline, strlen(value))) { 137 // set EGL debug if the "debug.egl.debug_proc" property 138 // matches the prefix of this application's command line 139 propertyLevel = 1; 140 } 141 } 142 fclose(file); 143 } 144 } 145 146 gEGLDebugLevel = propertyLevel || sEGLApplicationDebugLevel; 147 if (gEGLDebugLevel > 0) { 148 GLTrace_start(); 149 } 150 } 151 152 void setGLHooksThreadSpecific(gl_hooks_t const *value) { 153 if (sEGLGetErrorEnabled) { 154 setGlTraceThreadSpecific(value); 155 setGlThreadSpecific(&gHooksErrorTrace); 156 } else if (sEGLSystraceEnabled) { 157 setGlTraceThreadSpecific(value); 158 setGlThreadSpecific(&gHooksSystrace); 159 } else if (sEGLTraceLevel > 0) { 160 setGlTraceThreadSpecific(value); 161 setGlThreadSpecific(&gHooksTrace); 162 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) { 163 setGlTraceThreadSpecific(value); 164 setGlThreadSpecific(GLTrace_getGLHooks()); 165 } else { 166 setGlThreadSpecific(value); 167 } 168 } 169 170 /* 171 * Global entry point to allow applications to modify their own trace level. 172 * The effective trace level is the max of this level and the value of debug.egl.trace. 173 */ 174 extern "C" 175 void setGLTraceLevel(int level) { 176 sEGLApplicationTraceLevel = level; 177 } 178 179 /* 180 * Global entry point to allow applications to modify their own debug level. 181 * Debugging is enabled if either the application requested it, or if the system property 182 * matches the application's name. 183 */ 184 void EGLAPI setGLDebugLevel(int level) { 185 sEGLApplicationDebugLevel = level; 186 } 187 188 #else 189 190 void setGLHooksThreadSpecific(gl_hooks_t const *value) { 191 setGlThreadSpecific(value); 192 } 193 194 #endif 195 196 /*****************************************************************************/ 197 198 static int gl_no_context() { 199 if (egl_tls_t::logNoContextCall()) { 200 char const* const error = "call to OpenGL ES API with " 201 "no current context (logged once per thread)"; 202 if (LOG_NDEBUG) { 203 ALOGE(error); 204 } else { 205 LOG_ALWAYS_FATAL(error); 206 } 207 char value[PROPERTY_VALUE_MAX]; 208 property_get("debug.egl.callstack", value, "0"); 209 if (atoi(value)) { 210 CallStack stack; 211 stack.update(); 212 stack.dump(); 213 } 214 } 215 return 0; 216 } 217 218 static void early_egl_init(void) 219 { 220 #if !USE_FAST_TLS_KEY 221 pthread_key_create(&gGLWrapperKey, NULL); 222 #endif 223 #if EGL_TRACE 224 pthread_key_create(&gGLTraceKey, NULL); 225 initEglTraceLevel(); 226 initEglDebugLevel(); 227 #endif 228 uint32_t addr = (uint32_t)((void*)gl_no_context); 229 android_memset32( 230 (uint32_t*)(void*)&gHooksNoContext, 231 addr, 232 sizeof(gHooksNoContext)); 233 234 setGLHooksThreadSpecific(&gHooksNoContext); 235 } 236 237 static pthread_once_t once_control = PTHREAD_ONCE_INIT; 238 static int sEarlyInitState = pthread_once(&once_control, &early_egl_init); 239 240 // ---------------------------------------------------------------------------- 241 242 egl_display_ptr validate_display(EGLDisplay dpy) { 243 egl_display_ptr dp = get_display(dpy); 244 if (!dp) 245 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL)); 246 if (!dp->isReady()) 247 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL)); 248 249 return dp; 250 } 251 252 egl_display_ptr validate_display_connection(EGLDisplay dpy, 253 egl_connection_t*& cnx) { 254 cnx = NULL; 255 egl_display_ptr dp = validate_display(dpy); 256 if (!dp) 257 return dp; 258 cnx = &gEGLImpl; 259 if (cnx->dso == 0) { 260 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL)); 261 } 262 return dp; 263 } 264 265 // ---------------------------------------------------------------------------- 266 267 const GLubyte * egl_get_string_for_current_context(GLenum name) { 268 // NOTE: returning NULL here will fall-back to the default 269 // implementation. 270 271 EGLContext context = egl_tls_t::getContext(); 272 if (context == EGL_NO_CONTEXT) 273 return NULL; 274 275 egl_context_t const * const c = get_context(context); 276 if (c == NULL) // this should never happen, by construction 277 return NULL; 278 279 if (name != GL_EXTENSIONS) 280 return NULL; 281 282 return (const GLubyte *)c->gl_extensions.string(); 283 } 284 285 // ---------------------------------------------------------------------------- 286 287 // this mutex protects: 288 // d->disp[] 289 // egl_init_drivers_locked() 290 // 291 static EGLBoolean egl_init_drivers_locked() { 292 if (sEarlyInitState) { 293 // initialized by static ctor. should be set here. 294 return EGL_FALSE; 295 } 296 297 // get our driver loader 298 Loader& loader(Loader::getInstance()); 299 300 // dynamically load our EGL implementation 301 egl_connection_t* cnx = &gEGLImpl; 302 if (cnx->dso == 0) { 303 cnx->hooks[egl_connection_t::GLESv1_INDEX] = 304 &gHooks[egl_connection_t::GLESv1_INDEX]; 305 cnx->hooks[egl_connection_t::GLESv2_INDEX] = 306 &gHooks[egl_connection_t::GLESv2_INDEX]; 307 cnx->dso = loader.open(cnx); 308 } 309 310 return cnx->dso ? EGL_TRUE : EGL_FALSE; 311 } 312 313 static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER; 314 315 EGLBoolean egl_init_drivers() { 316 EGLBoolean res; 317 pthread_mutex_lock(&sInitDriverMutex); 318 res = egl_init_drivers_locked(); 319 pthread_mutex_unlock(&sInitDriverMutex); 320 return res; 321 } 322 323 void gl_unimplemented() { 324 ALOGE("called unimplemented OpenGL ES API"); 325 } 326 327 void gl_noop() { 328 } 329 330 // ---------------------------------------------------------------------------- 331 332 #if USE_FAST_TLS_KEY 333 334 // We have a dedicated TLS slot in bionic 335 static inline gl_hooks_t const * volatile * get_tls_hooks() { 336 volatile void *tls_base = __get_tls(); 337 gl_hooks_t const * volatile * tls_hooks = 338 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base); 339 return tls_hooks; 340 } 341 342 void setGlThreadSpecific(gl_hooks_t const *value) { 343 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); 344 tls_hooks[TLS_SLOT_OPENGL_API] = value; 345 } 346 347 gl_hooks_t const* getGlThreadSpecific() { 348 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); 349 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API]; 350 if (hooks) return hooks; 351 return &gHooksNoContext; 352 } 353 354 #else 355 356 void setGlThreadSpecific(gl_hooks_t const *value) { 357 pthread_setspecific(gGLWrapperKey, value); 358 } 359 360 gl_hooks_t const* getGlThreadSpecific() { 361 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey)); 362 if (hooks) return hooks; 363 return &gHooksNoContext; 364 } 365 366 #endif 367 368 // ---------------------------------------------------------------------------- 369 // GL / EGL hooks 370 // ---------------------------------------------------------------------------- 371 372 #undef GL_ENTRY 373 #undef EGL_ENTRY 374 #define GL_ENTRY(_r, _api, ...) #_api, 375 #define EGL_ENTRY(_r, _api, ...) #_api, 376 377 char const * const gl_names[] = { 378 #include "entries.in" 379 NULL 380 }; 381 382 char const * const egl_names[] = { 383 #include "egl_entries.in" 384 NULL 385 }; 386 387 #undef GL_ENTRY 388 #undef EGL_ENTRY 389 390 391 // ---------------------------------------------------------------------------- 392 }; // namespace android 393 // ---------------------------------------------------------------------------- 394 395