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