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