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