egl.cpp revision 455e3601498096d1daa0cf0ec7c23abb28b39af3
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    initEglDebugLevel();
196#endif
197    uint32_t addr = (uint32_t)((void*)gl_no_context);
198    android_memset32(
199            (uint32_t*)(void*)&gHooksNoContext,
200            addr,
201            sizeof(gHooksNoContext));
202
203    setGLHooksThreadSpecific(&gHooksNoContext);
204}
205
206static pthread_once_t once_control = PTHREAD_ONCE_INIT;
207static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
208
209// ----------------------------------------------------------------------------
210
211egl_display_ptr validate_display(EGLDisplay dpy) {
212    egl_display_ptr dp = get_display(dpy);
213    if (!dp)
214        return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
215    if (!dp->isReady())
216        return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
217
218    return dp;
219}
220
221egl_display_ptr validate_display_connection(EGLDisplay dpy,
222        egl_connection_t*& cnx) {
223    cnx = NULL;
224    egl_display_ptr dp = validate_display(dpy);
225    if (!dp)
226        return dp;
227    cnx = &gEGLImpl;
228    if (cnx->dso == 0) {
229        return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
230    }
231    return dp;
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 our EGL implementation
270    egl_connection_t* cnx = &gEGLImpl;
271    if (cnx->dso == 0) {
272        cnx->hooks[egl_connection_t::GLESv1_INDEX] =
273                &gHooks[egl_connection_t::GLESv1_INDEX];
274        cnx->hooks[egl_connection_t::GLESv2_INDEX] =
275                &gHooks[egl_connection_t::GLESv2_INDEX];
276        cnx->dso = loader.open(cnx);
277    }
278
279    return cnx->dso ? EGL_TRUE : EGL_FALSE;
280}
281
282static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
283
284EGLBoolean egl_init_drivers() {
285    EGLBoolean res;
286    pthread_mutex_lock(&sInitDriverMutex);
287    res = egl_init_drivers_locked();
288    pthread_mutex_unlock(&sInitDriverMutex);
289    return res;
290}
291
292void gl_unimplemented() {
293    ALOGE("called unimplemented OpenGL ES API");
294}
295
296void gl_noop() {
297}
298
299// ----------------------------------------------------------------------------
300
301#if USE_FAST_TLS_KEY
302
303// We have a dedicated TLS slot in bionic
304static inline gl_hooks_t const * volatile * get_tls_hooks() {
305    volatile void *tls_base = __get_tls();
306    gl_hooks_t const * volatile * tls_hooks =
307            reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
308    return tls_hooks;
309}
310
311void setGlThreadSpecific(gl_hooks_t const *value) {
312    gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
313    tls_hooks[TLS_SLOT_OPENGL_API] = value;
314}
315
316gl_hooks_t const* getGlThreadSpecific() {
317    gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
318    gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
319    if (hooks) return hooks;
320    return &gHooksNoContext;
321}
322
323#else
324
325void setGlThreadSpecific(gl_hooks_t const *value) {
326    pthread_setspecific(gGLWrapperKey, value);
327}
328
329gl_hooks_t const* getGlThreadSpecific() {
330    gl_hooks_t const* hooks =  static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
331    if (hooks) return hooks;
332    return &gHooksNoContext;
333}
334
335#endif
336
337// ----------------------------------------------------------------------------
338// GL / EGL hooks
339// ----------------------------------------------------------------------------
340
341#undef GL_ENTRY
342#undef EGL_ENTRY
343#define GL_ENTRY(_r, _api, ...) #_api,
344#define EGL_ENTRY(_r, _api, ...) #_api,
345
346char const * const gl_names[] = {
347    #include "entries.in"
348    NULL
349};
350
351char const * const egl_names[] = {
352    #include "egl_entries.in"
353    NULL
354};
355
356#undef GL_ENTRY
357#undef EGL_ENTRY
358
359
360// ----------------------------------------------------------------------------
361}; // namespace android
362// ----------------------------------------------------------------------------
363
364