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