egl.cpp revision 6482fa4db0a7ac99cd3503d6bf170f80b26fb695
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// ---------------------------------------------------------------------------- 48namespace android { 49// ---------------------------------------------------------------------------- 50 51egl_connection_t gEGLImpl; 52gl_hooks_t gHooks[2]; 53gl_hooks_t gHooksNoContext; 54pthread_key_t gGLWrapperKey = -1; 55 56// ---------------------------------------------------------------------------- 57 58#if EGL_TRACE 59 60EGLAPI 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 */ 81static int sEGLTraceLevel; 82static int sEGLApplicationTraceLevel; 83 84static bool sEGLSystraceEnabled; 85static bool sEGLGetErrorEnabled; 86 87int gEGLDebugLevel; 88static int sEGLApplicationDebugLevel; 89 90extern gl_hooks_t gHooksTrace; 91extern gl_hooks_t gHooksSystrace; 92extern gl_hooks_t gHooksErrorTrace; 93 94static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) { 95 pthread_setspecific(gGLTraceKey, value); 96} 97 98gl_hooks_t const* getGLTraceThreadSpecific() { 99 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey)); 100} 101 102void 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 124void initEglDebugLevel() { 125 int propertyLevel = 0; 126 char value[PROPERTY_VALUE_MAX]; 127 128 // check system property only on userdebug or eng builds 129 property_get("ro.debuggable", value, "0"); 130 if (value[0] == '0') 131 return; 132 133 property_get("debug.egl.debug_proc", value, ""); 134 if (strlen(value) > 0) { 135 long pid = getpid(); 136 char procPath[128] = {}; 137 sprintf(procPath, "/proc/%ld/cmdline", pid); 138 FILE * file = fopen(procPath, "r"); 139 if (file) { 140 char cmdline[256] = {}; 141 if (fgets(cmdline, sizeof(cmdline) - 1, file)) { 142 if (!strncmp(value, cmdline, strlen(value))) { 143 // set EGL debug if the "debug.egl.debug_proc" property 144 // matches the prefix of this application's command line 145 propertyLevel = 1; 146 } 147 } 148 fclose(file); 149 } 150 } 151 152 gEGLDebugLevel = propertyLevel || sEGLApplicationDebugLevel; 153 if (gEGLDebugLevel > 0) { 154 GLTrace_start(); 155 } 156} 157 158void setGLHooksThreadSpecific(gl_hooks_t const *value) { 159 if (sEGLGetErrorEnabled) { 160 setGlTraceThreadSpecific(value); 161 setGlThreadSpecific(&gHooksErrorTrace); 162 } else if (sEGLSystraceEnabled) { 163 setGlTraceThreadSpecific(value); 164 setGlThreadSpecific(&gHooksSystrace); 165 } else if (sEGLTraceLevel > 0) { 166 setGlTraceThreadSpecific(value); 167 setGlThreadSpecific(&gHooksTrace); 168 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) { 169 setGlTraceThreadSpecific(value); 170 setGlThreadSpecific(GLTrace_getGLHooks()); 171 } else { 172 setGlThreadSpecific(value); 173 } 174} 175 176/* 177 * Global entry point to allow applications to modify their own trace level. 178 * The effective trace level is the max of this level and the value of debug.egl.trace. 179 */ 180extern "C" 181void setGLTraceLevel(int level) { 182 sEGLApplicationTraceLevel = level; 183} 184 185/* 186 * Global entry point to allow applications to modify their own debug level. 187 * Debugging is enabled if either the application requested it, or if the system property 188 * matches the application's name. 189 */ 190void EGLAPI setGLDebugLevel(int level) { 191 sEGLApplicationDebugLevel = level; 192} 193 194#else 195 196void setGLHooksThreadSpecific(gl_hooks_t const *value) { 197 setGlThreadSpecific(value); 198} 199 200#endif 201 202/*****************************************************************************/ 203 204static int gl_no_context() { 205 if (egl_tls_t::logNoContextCall()) { 206 char const* const error = "call to OpenGL ES API with " 207 "no current context (logged once per thread)"; 208 if (LOG_NDEBUG) { 209 ALOGE(error); 210 } else { 211 LOG_ALWAYS_FATAL(error); 212 } 213 char value[PROPERTY_VALUE_MAX]; 214 property_get("debug.egl.callstack", value, "0"); 215 if (atoi(value)) { 216 CallStack stack; 217 stack.update(); 218 stack.dump(); 219 } 220 } 221 return 0; 222} 223 224static void early_egl_init(void) 225{ 226#if !USE_FAST_TLS_KEY 227 pthread_key_create(&gGLWrapperKey, NULL); 228#endif 229#if EGL_TRACE 230 pthread_key_create(&gGLTraceKey, NULL); 231 initEglTraceLevel(); 232#endif 233 uint32_t addr = (uint32_t)((void*)gl_no_context); 234 android_memset32( 235 (uint32_t*)(void*)&gHooksNoContext, 236 addr, 237 sizeof(gHooksNoContext)); 238 239 setGLHooksThreadSpecific(&gHooksNoContext); 240} 241 242static pthread_once_t once_control = PTHREAD_ONCE_INIT; 243static int sEarlyInitState = pthread_once(&once_control, &early_egl_init); 244 245// ---------------------------------------------------------------------------- 246 247egl_display_ptr validate_display(EGLDisplay dpy) { 248 egl_display_ptr dp = get_display(dpy); 249 if (!dp) 250 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL)); 251 if (!dp->isReady()) 252 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL)); 253 254 return dp; 255} 256 257egl_display_ptr validate_display_connection(EGLDisplay dpy, 258 egl_connection_t*& cnx) { 259 cnx = NULL; 260 egl_display_ptr dp = validate_display(dpy); 261 if (!dp) 262 return dp; 263 cnx = &gEGLImpl; 264 if (cnx->dso == 0) { 265 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL)); 266 } 267 return dp; 268} 269 270// ---------------------------------------------------------------------------- 271 272const GLubyte * egl_get_string_for_current_context(GLenum name) { 273 // NOTE: returning NULL here will fall-back to the default 274 // implementation. 275 276 EGLContext context = egl_tls_t::getContext(); 277 if (context == EGL_NO_CONTEXT) 278 return NULL; 279 280 egl_context_t const * const c = get_context(context); 281 if (c == NULL) // this should never happen, by construction 282 return NULL; 283 284 if (name != GL_EXTENSIONS) 285 return NULL; 286 287 return (const GLubyte *)c->gl_extensions.string(); 288} 289 290// ---------------------------------------------------------------------------- 291 292// this mutex protects: 293// d->disp[] 294// egl_init_drivers_locked() 295// 296static EGLBoolean egl_init_drivers_locked() { 297 if (sEarlyInitState) { 298 // initialized by static ctor. should be set here. 299 return EGL_FALSE; 300 } 301 302 // get our driver loader 303 Loader& loader(Loader::getInstance()); 304 305 // dynamically load our EGL implementation 306 egl_connection_t* cnx = &gEGLImpl; 307 if (cnx->dso == 0) { 308 cnx->hooks[egl_connection_t::GLESv1_INDEX] = 309 &gHooks[egl_connection_t::GLESv1_INDEX]; 310 cnx->hooks[egl_connection_t::GLESv2_INDEX] = 311 &gHooks[egl_connection_t::GLESv2_INDEX]; 312 cnx->dso = loader.open(cnx); 313 } 314 315 return cnx->dso ? EGL_TRUE : EGL_FALSE; 316} 317 318static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER; 319 320EGLBoolean egl_init_drivers() { 321 EGLBoolean res; 322 pthread_mutex_lock(&sInitDriverMutex); 323 res = egl_init_drivers_locked(); 324 pthread_mutex_unlock(&sInitDriverMutex); 325 return res; 326} 327 328void gl_unimplemented() { 329 ALOGE("called unimplemented OpenGL ES API"); 330} 331 332void gl_noop() { 333} 334 335// ---------------------------------------------------------------------------- 336 337#if USE_FAST_TLS_KEY 338 339// We have a dedicated TLS slot in bionic 340static inline gl_hooks_t const * volatile * get_tls_hooks() { 341 volatile void *tls_base = __get_tls(); 342 gl_hooks_t const * volatile * tls_hooks = 343 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base); 344 return tls_hooks; 345} 346 347void setGlThreadSpecific(gl_hooks_t const *value) { 348 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); 349 tls_hooks[TLS_SLOT_OPENGL_API] = value; 350} 351 352gl_hooks_t const* getGlThreadSpecific() { 353 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); 354 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API]; 355 if (hooks) return hooks; 356 return &gHooksNoContext; 357} 358 359#else 360 361void setGlThreadSpecific(gl_hooks_t const *value) { 362 pthread_setspecific(gGLWrapperKey, value); 363} 364 365gl_hooks_t const* getGlThreadSpecific() { 366 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey)); 367 if (hooks) return hooks; 368 return &gHooksNoContext; 369} 370 371#endif 372 373// ---------------------------------------------------------------------------- 374// GL / EGL hooks 375// ---------------------------------------------------------------------------- 376 377#undef GL_ENTRY 378#undef EGL_ENTRY 379#define GL_ENTRY(_r, _api, ...) #_api, 380#define EGL_ENTRY(_r, _api, ...) #_api, 381 382char const * const gl_names[] = { 383 #include "entries.in" 384 NULL 385}; 386 387char const * const egl_names[] = { 388 #include "egl_entries.in" 389 NULL 390}; 391 392#undef GL_ENTRY 393#undef EGL_ENTRY 394 395 396// ---------------------------------------------------------------------------- 397}; // namespace android 398// ---------------------------------------------------------------------------- 399 400