Home | History | Annotate | Download | only in EGL
      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 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  */
     75 static int sEGLTraceLevel;
     76 static int sEGLApplicationTraceLevel;
     77 
     78 int gEGLDebugLevel;
     79 static int sEGLApplicationDebugLevel;
     80 
     81 extern gl_hooks_t gHooksTrace;
     82 
     83 static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
     84     pthread_setspecific(gGLTraceKey, value);
     85 }
     86 
     87 gl_hooks_t const* getGLTraceThreadSpecific() {
     88     return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
     89 }
     90 
     91 void 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 
     99 void 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 
    127 void 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  */
    143 extern "C"
    144 void 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  */
    153 void EGLAPI setGLDebugLevel(int level) {
    154     sEGLApplicationDebugLevel = level;
    155 }
    156 
    157 #else
    158 
    159 void setGLHooksThreadSpecific(gl_hooks_t const *value) {
    160     setGlThreadSpecific(value);
    161 }
    162 
    163 #endif
    164 
    165 /*****************************************************************************/
    166 
    167 static int gl_no_context() {
    168     if (egl_tls_t::logNoContextCall()) {
    169         ALOGE("call to OpenGL ES API with no current context "
    170              "(logged once per thread)");
    171         char value[PROPERTY_VALUE_MAX];
    172         property_get("debug.egl.callstack", value, "0");
    173         if (atoi(value)) {
    174             CallStack stack;
    175             stack.update();
    176             stack.dump();
    177         }
    178     }
    179     return 0;
    180 }
    181 
    182 static void early_egl_init(void)
    183 {
    184 #if !USE_FAST_TLS_KEY
    185     pthread_key_create(&gGLWrapperKey, NULL);
    186 #endif
    187 #if EGL_TRACE
    188     pthread_key_create(&gGLTraceKey, NULL);
    189     initEglTraceLevel();
    190     initEglDebugLevel();
    191 #endif
    192     uint32_t addr = (uint32_t)((void*)gl_no_context);
    193     android_memset32(
    194             (uint32_t*)(void*)&gHooksNoContext,
    195             addr,
    196             sizeof(gHooksNoContext));
    197 
    198     setGLHooksThreadSpecific(&gHooksNoContext);
    199 }
    200 
    201 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
    202 static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
    203 
    204 // ----------------------------------------------------------------------------
    205 
    206 egl_display_ptr validate_display(EGLDisplay dpy) {
    207     egl_display_ptr dp = get_display(dpy);
    208     if (!dp)
    209         return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
    210     if (!dp->isReady())
    211         return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
    212 
    213     return dp;
    214 }
    215 
    216 egl_display_ptr validate_display_connection(EGLDisplay dpy,
    217         egl_connection_t*& cnx) {
    218     cnx = NULL;
    219     egl_display_ptr dp = validate_display(dpy);
    220     if (!dp)
    221         return dp;
    222     cnx = &gEGLImpl;
    223     if (cnx->dso == 0) {
    224         return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
    225     }
    226     return dp;
    227 }
    228 
    229 // ----------------------------------------------------------------------------
    230 
    231 const GLubyte * egl_get_string_for_current_context(GLenum name) {
    232     // NOTE: returning NULL here will fall-back to the default
    233     // implementation.
    234 
    235     EGLContext context = egl_tls_t::getContext();
    236     if (context == EGL_NO_CONTEXT)
    237         return NULL;
    238 
    239     egl_context_t const * const c = get_context(context);
    240     if (c == NULL) // this should never happen, by construction
    241         return NULL;
    242 
    243     if (name != GL_EXTENSIONS)
    244         return NULL;
    245 
    246     return (const GLubyte *)c->gl_extensions.string();
    247 }
    248 
    249 // ----------------------------------------------------------------------------
    250 
    251 // this mutex protects:
    252 //    d->disp[]
    253 //    egl_init_drivers_locked()
    254 //
    255 static EGLBoolean egl_init_drivers_locked() {
    256     if (sEarlyInitState) {
    257         // initialized by static ctor. should be set here.
    258         return EGL_FALSE;
    259     }
    260 
    261     // get our driver loader
    262     Loader& loader(Loader::getInstance());
    263 
    264     // dynamically load our EGL implementation
    265     egl_connection_t* cnx = &gEGLImpl;
    266     if (cnx->dso == 0) {
    267         cnx->hooks[egl_connection_t::GLESv1_INDEX] =
    268                 &gHooks[egl_connection_t::GLESv1_INDEX];
    269         cnx->hooks[egl_connection_t::GLESv2_INDEX] =
    270                 &gHooks[egl_connection_t::GLESv2_INDEX];
    271         cnx->dso = loader.open(cnx);
    272     }
    273 
    274     return cnx->dso ? EGL_TRUE : EGL_FALSE;
    275 }
    276 
    277 static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
    278 
    279 EGLBoolean egl_init_drivers() {
    280     EGLBoolean res;
    281     pthread_mutex_lock(&sInitDriverMutex);
    282     res = egl_init_drivers_locked();
    283     pthread_mutex_unlock(&sInitDriverMutex);
    284     return res;
    285 }
    286 
    287 void gl_unimplemented() {
    288     ALOGE("called unimplemented OpenGL ES API");
    289 }
    290 
    291 void gl_noop() {
    292 }
    293 
    294 // ----------------------------------------------------------------------------
    295 
    296 #if USE_FAST_TLS_KEY
    297 
    298 // We have a dedicated TLS slot in bionic
    299 static inline gl_hooks_t const * volatile * get_tls_hooks() {
    300     volatile void *tls_base = __get_tls();
    301     gl_hooks_t const * volatile * tls_hooks =
    302             reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
    303     return tls_hooks;
    304 }
    305 
    306 void setGlThreadSpecific(gl_hooks_t const *value) {
    307     gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
    308     tls_hooks[TLS_SLOT_OPENGL_API] = value;
    309 }
    310 
    311 gl_hooks_t const* getGlThreadSpecific() {
    312     gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
    313     gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
    314     if (hooks) return hooks;
    315     return &gHooksNoContext;
    316 }
    317 
    318 #else
    319 
    320 void setGlThreadSpecific(gl_hooks_t const *value) {
    321     pthread_setspecific(gGLWrapperKey, value);
    322 }
    323 
    324 gl_hooks_t const* getGlThreadSpecific() {
    325     gl_hooks_t const* hooks =  static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
    326     if (hooks) return hooks;
    327     return &gHooksNoContext;
    328 }
    329 
    330 #endif
    331 
    332 // ----------------------------------------------------------------------------
    333 // GL / EGL hooks
    334 // ----------------------------------------------------------------------------
    335 
    336 #undef GL_ENTRY
    337 #undef EGL_ENTRY
    338 #define GL_ENTRY(_r, _api, ...) #_api,
    339 #define EGL_ENTRY(_r, _api, ...) #_api,
    340 
    341 char const * const gl_names[] = {
    342     #include "entries.in"
    343     NULL
    344 };
    345 
    346 char const * const egl_names[] = {
    347     #include "egl_entries.in"
    348     NULL
    349 };
    350 
    351 #undef GL_ENTRY
    352 #undef EGL_ENTRY
    353 
    354 
    355 // ----------------------------------------------------------------------------
    356 }; // namespace android
    357 // ----------------------------------------------------------------------------
    358 
    359