egl.cpp revision ada798b7ca7cabc255aa159964b64975e7fdb2df
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
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;
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->image;
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[GLESv1_INDEX] = &gHooks[GLESv1_INDEX];
273        cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX];
274        cnx->dso = loader.open(cnx);
275    }
276
277    return cnx->dso ? EGL_TRUE : EGL_FALSE;
278}
279
280static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
281
282EGLBoolean egl_init_drivers() {
283    EGLBoolean res;
284    pthread_mutex_lock(&sInitDriverMutex);
285    res = egl_init_drivers_locked();
286    pthread_mutex_unlock(&sInitDriverMutex);
287    return res;
288}
289
290void gl_unimplemented() {
291    ALOGE("called unimplemented OpenGL ES API");
292}
293
294void gl_noop() {
295}
296
297// ----------------------------------------------------------------------------
298
299#if USE_FAST_TLS_KEY
300
301// We have a dedicated TLS slot in bionic
302static inline gl_hooks_t const * volatile * get_tls_hooks() {
303    volatile void *tls_base = __get_tls();
304    gl_hooks_t const * volatile * tls_hooks =
305            reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
306    return tls_hooks;
307}
308
309void setGlThreadSpecific(gl_hooks_t const *value) {
310    gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
311    tls_hooks[TLS_SLOT_OPENGL_API] = value;
312}
313
314gl_hooks_t const* getGlThreadSpecific() {
315    gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
316    gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
317    if (hooks) return hooks;
318    return &gHooksNoContext;
319}
320
321#else
322
323void setGlThreadSpecific(gl_hooks_t const *value) {
324    pthread_setspecific(gGLWrapperKey, value);
325}
326
327gl_hooks_t const* getGlThreadSpecific() {
328    gl_hooks_t const* hooks =  static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
329    if (hooks) return hooks;
330    return &gHooksNoContext;
331}
332
333#endif
334
335// ----------------------------------------------------------------------------
336// GL / EGL hooks
337// ----------------------------------------------------------------------------
338
339#undef GL_ENTRY
340#undef EGL_ENTRY
341#define GL_ENTRY(_r, _api, ...) #_api,
342#define EGL_ENTRY(_r, _api, ...) #_api,
343
344char const * const gl_names[] = {
345    #include "entries.in"
346    NULL
347};
348
349char const * const egl_names[] = {
350    #include "egl_entries.in"
351    NULL
352};
353
354#undef GL_ENTRY
355#undef EGL_ENTRY
356
357
358// ----------------------------------------------------------------------------
359}; // namespace android
360// ----------------------------------------------------------------------------
361
362