blob: a8691b5c90dbccaea09a82473951c3695365998e [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080014 ** limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <ctype.h>
Mathias Agopiand8fb7b52009-05-17 18:50:16 -070018#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <string.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <hardware/gralloc.h>
22#include <system/window.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/atomic.h>
29#include <cutils/properties.h>
30#include <cutils/memory.h>
31
Romain Guye03de932011-07-11 15:33:51 -070032#include <utils/CallStack.h>
Mathias Agopian24035332010-08-02 17:34:32 -070033#include <utils/String8.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070034
Mathias Agopian1cadb252011-05-23 17:26:14 -070035#include "egldefs.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "egl_impl.h"
David Li864f8392011-03-28 10:39:28 -070037#include "egl_tls.h"
Siva Velusamy0469dd62011-11-30 15:05:37 -080038#include "glestrace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070039#include "hooks.h"
40#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Mathias Agopian518ec112011-05-13 16:21:08 -070042#include "egl_display.h"
43#include "egl_object.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45// ----------------------------------------------------------------------------
46namespace android {
47// ----------------------------------------------------------------------------
48
Mathias Agopianada798b2012-02-13 17:09:30 -080049egl_connection_t gEGLImpl;
50gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070051gl_hooks_t gHooksNoContext;
52pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053
54// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070056#if EGL_TRACE
57
58EGLAPI pthread_key_t gGLTraceKey = -1;
59
60// ----------------------------------------------------------------------------
61
Siva Velusamyb13c78f2012-03-09 14:51:28 -080062/**
Romain Guy16928bf2012-10-18 16:16:10 -070063 * There are three different tracing methods:
64 * 1. libs/EGL/trace.cpp: Traces all functions to systrace.
65 * To enable:
66 * - set system property "debug.egl.trace" to "systrace" to trace all apps.
67 * 2. libs/EGL/trace.cpp: Logs a stack trace for GL errors after each function call.
68 * To enable:
69 * - set system property "debug.egl.trace" to "error" to trace all apps.
70 * 3. libs/EGL/trace.cpp: Traces all functions to logcat.
Siva Velusamyb13c78f2012-03-09 14:51:28 -080071 * To enable:
72 * - set system property "debug.egl.trace" to 1 to trace all apps.
73 * - or call setGLTraceLevel(1) from an app to enable tracing for that app.
Romain Guy16928bf2012-10-18 16:16:10 -070074 * 4. libs/GLES_trace: Traces all functions via protobuf to host.
Siva Velusamyb13c78f2012-03-09 14:51:28 -080075 * To enable:
76 * - set system property "debug.egl.debug_proc" to the application name.
77 * - or call setGLDebugLevel(1) from the app.
78 */
Mathias Agopian518ec112011-05-13 16:21:08 -070079static int sEGLTraceLevel;
80static int sEGLApplicationTraceLevel;
81
Romain Guy16928bf2012-10-18 16:16:10 -070082static bool sEGLSystraceEnabled;
83static bool sEGLGetErrorEnabled;
84
Siva Velusamya73a9772012-12-18 14:56:55 -080085static volatile int sEGLDebugLevel;
Siva Velusamyb13c78f2012-03-09 14:51:28 -080086
Mathias Agopian518ec112011-05-13 16:21:08 -070087extern gl_hooks_t gHooksTrace;
Romain Guy16928bf2012-10-18 16:16:10 -070088extern gl_hooks_t gHooksSystrace;
89extern gl_hooks_t gHooksErrorTrace;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070090
Siva Velusamya73a9772012-12-18 14:56:55 -080091int getEGLDebugLevel() {
92 return sEGLDebugLevel;
93}
94
95void setEGLDebugLevel(int level) {
96 sEGLDebugLevel = level;
97}
98
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070099static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
100 pthread_setspecific(gGLTraceKey, value);
101}
102
103gl_hooks_t const* getGLTraceThreadSpecific() {
104 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
105}
106
Mathias Agopian518ec112011-05-13 16:21:08 -0700107void initEglTraceLevel() {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700108 char value[PROPERTY_VALUE_MAX];
109 property_get("debug.egl.trace", value, "0");
Romain Guy16928bf2012-10-18 16:16:10 -0700110
111 sEGLGetErrorEnabled = !strcasecmp(value, "error");
112 if (sEGLGetErrorEnabled) {
113 sEGLSystraceEnabled = false;
114 sEGLTraceLevel = 0;
115 return;
116 }
117
118 sEGLSystraceEnabled = !strcasecmp(value, "systrace");
119 if (sEGLSystraceEnabled) {
120 sEGLTraceLevel = 0;
121 return;
122 }
123
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700124 int propertyLevel = atoi(value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700125 int applicationLevel = sEGLApplicationTraceLevel;
126 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800127}
David Li499c6f02011-04-08 18:43:16 -0700128
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800129void initEglDebugLevel() {
Siva Velusamya73a9772012-12-18 14:56:55 -0800130 if (getEGLDebugLevel() == 0) {
131 char value[PROPERTY_VALUE_MAX];
Siva Velusamy6482fa42012-11-20 13:39:57 -0800132
Siva Velusamya73a9772012-12-18 14:56:55 -0800133 // check system property only on userdebug or eng builds
134 property_get("ro.debuggable", value, "0");
135 if (value[0] == '0')
136 return;
Siva Velusamy6482fa42012-11-20 13:39:57 -0800137
Siva Velusamya73a9772012-12-18 14:56:55 -0800138 property_get("debug.egl.debug_proc", value, "");
139 if (strlen(value) > 0) {
140 FILE * file = fopen("/proc/self/cmdline", "r");
141 if (file) {
142 char cmdline[256];
143 if (fgets(cmdline, sizeof(cmdline), file)) {
144 if (!strncmp(value, cmdline, strlen(value))) {
145 // set EGL debug if the "debug.egl.debug_proc" property
146 // matches the prefix of this application's command line
147 setEGLDebugLevel(1);
148 }
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800149 }
Siva Velusamya73a9772012-12-18 14:56:55 -0800150 fclose(file);
Siva Velusamy93a826f2011-12-14 12:19:56 -0800151 }
David Li499c6f02011-04-08 18:43:16 -0700152 }
David Li2f5a6552011-03-01 16:08:10 -0800153 }
David Li499c6f02011-04-08 18:43:16 -0700154
Siva Velusamya73a9772012-12-18 14:56:55 -0800155 if (getEGLDebugLevel() > 0) {
156 if (GLTrace_start() < 0) {
157 ALOGE("Error starting Tracer for OpenGL ES. Disabling..");
158 setEGLDebugLevel(0);
159 }
David Li85f33a72011-03-10 19:07:42 -0800160 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700161}
162
Mathias Agopian518ec112011-05-13 16:21:08 -0700163void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Romain Guy16928bf2012-10-18 16:16:10 -0700164 if (sEGLGetErrorEnabled) {
165 setGlTraceThreadSpecific(value);
166 setGlThreadSpecific(&gHooksErrorTrace);
167 } else if (sEGLSystraceEnabled) {
168 setGlTraceThreadSpecific(value);
169 setGlThreadSpecific(&gHooksSystrace);
170 } else if (sEGLTraceLevel > 0) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700171 setGlTraceThreadSpecific(value);
172 setGlThreadSpecific(&gHooksTrace);
Siva Velusamya73a9772012-12-18 14:56:55 -0800173 } else if (getEGLDebugLevel() > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800174 setGlTraceThreadSpecific(value);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800175 setGlThreadSpecific(GLTrace_getGLHooks());
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700176 } else {
Siva Velusamya73a9772012-12-18 14:56:55 -0800177 setGlTraceThreadSpecific(NULL);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700178 setGlThreadSpecific(value);
179 }
180}
181
182/*
183 * Global entry point to allow applications to modify their own trace level.
184 * The effective trace level is the max of this level and the value of debug.egl.trace.
185 */
186extern "C"
187void setGLTraceLevel(int level) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700188 sEGLApplicationTraceLevel = level;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700189}
190
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800191/*
192 * Global entry point to allow applications to modify their own debug level.
193 * Debugging is enabled if either the application requested it, or if the system property
194 * matches the application's name.
Siva Velusamya73a9772012-12-18 14:56:55 -0800195 * Note that this only sets the debug level. The value is read and used either in
196 * initEglDebugLevel() if the application hasn't initialized its display yet, or when
197 * eglSwapBuffers() is called next.
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800198 */
199void EGLAPI setGLDebugLevel(int level) {
Siva Velusamya73a9772012-12-18 14:56:55 -0800200 setEGLDebugLevel(level);
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800201}
202
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700203#else
204
Mathias Agopian518ec112011-05-13 16:21:08 -0700205void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700206 setGlThreadSpecific(value);
207}
208
209#endif
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211/*****************************************************************************/
212
Mathias Agopian6f087122010-09-23 16:38:38 -0700213static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -0700214 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -0700215 char const* const error = "call to OpenGL ES API with "
216 "no current context (logged once per thread)";
217 if (LOG_NDEBUG) {
218 ALOGE(error);
219 } else {
220 LOG_ALWAYS_FATAL(error);
221 }
Mathias Agopianecfe0912011-09-06 17:24:05 -0700222 char value[PROPERTY_VALUE_MAX];
223 property_get("debug.egl.callstack", value, "0");
224 if (atoi(value)) {
225 CallStack stack;
226 stack.update();
227 stack.dump();
228 }
Mathias Agopiand274eae2009-07-31 16:21:17 -0700229 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700230 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700231}
232
Jesse Hall47743382013-02-08 11:13:46 -0800233static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234{
235#if !USE_FAST_TLS_KEY
236 pthread_key_create(&gGLWrapperKey, NULL);
237#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700238#if EGL_TRACE
239 pthread_key_create(&gGLTraceKey, NULL);
240 initEglTraceLevel();
241#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 uint32_t addr = (uint32_t)((void*)gl_no_context);
243 android_memset32(
Jesse Hall47743382013-02-08 11:13:46 -0800244 (uint32_t*)(void*)&gHooksNoContext,
245 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700246 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700247
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700248 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249}
250
251static pthread_once_t once_control = PTHREAD_ONCE_INIT;
252static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
253
Mathias Agopian518ec112011-05-13 16:21:08 -0700254// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255
Jesse Hallb29e5e82012-04-04 16:53:42 -0700256egl_display_ptr validate_display(EGLDisplay dpy) {
257 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700258 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700259 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -0700260 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -0700261 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -0700262
263 return dp;
264}
265
Jesse Hallb29e5e82012-04-04 16:53:42 -0700266egl_display_ptr validate_display_connection(EGLDisplay dpy,
267 egl_connection_t*& cnx) {
268 cnx = NULL;
269 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700270 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700271 return dp;
272 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700274 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700276 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277}
278
Mathias Agopian518ec112011-05-13 16:21:08 -0700279// ----------------------------------------------------------------------------
280
Mathias Agopian48d438d2012-01-28 21:44:00 -0800281const GLubyte * egl_get_string_for_current_context(GLenum name) {
282 // NOTE: returning NULL here will fall-back to the default
283 // implementation.
284
285 EGLContext context = egl_tls_t::getContext();
286 if (context == EGL_NO_CONTEXT)
287 return NULL;
288
289 egl_context_t const * const c = get_context(context);
290 if (c == NULL) // this should never happen, by construction
291 return NULL;
292
293 if (name != GL_EXTENSIONS)
294 return NULL;
295
296 return (const GLubyte *)c->gl_extensions.string();
297}
298
299// ----------------------------------------------------------------------------
300
Mathias Agopian923c6612009-08-17 18:07:06 -0700301// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700302// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700303// egl_init_drivers_locked()
304//
Mathias Agopian518ec112011-05-13 16:21:08 -0700305static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700307 // initialized by static ctor. should be set here.
308 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 }
310
Mathias Agopiande586972009-05-28 17:39:03 -0700311 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700312 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700313
Mathias Agopianada798b2012-02-13 17:09:30 -0800314 // dynamically load our EGL implementation
315 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800317 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
318 &gHooks[egl_connection_t::GLESv1_INDEX];
319 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
320 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800321 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 }
323
Mathias Agopianada798b2012-02-13 17:09:30 -0800324 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
326
Mathias Agopian518ec112011-05-13 16:21:08 -0700327static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
328
329EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700330 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700331 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700332 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700333 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700334 return res;
335}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700336
Mathias Agopian1cadb252011-05-23 17:26:14 -0700337void gl_unimplemented() {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000338 ALOGE("called unimplemented OpenGL ES API");
Mathias Agopian1cadb252011-05-23 17:26:14 -0700339}
340
Mathias Agopian48d438d2012-01-28 21:44:00 -0800341void gl_noop() {
342}
343
Mathias Agopian1cadb252011-05-23 17:26:14 -0700344// ----------------------------------------------------------------------------
345
346#if USE_FAST_TLS_KEY
347
348// We have a dedicated TLS slot in bionic
349static inline gl_hooks_t const * volatile * get_tls_hooks() {
350 volatile void *tls_base = __get_tls();
351 gl_hooks_t const * volatile * tls_hooks =
352 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
353 return tls_hooks;
354}
355
356void setGlThreadSpecific(gl_hooks_t const *value) {
357 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
358 tls_hooks[TLS_SLOT_OPENGL_API] = value;
359}
360
361gl_hooks_t const* getGlThreadSpecific() {
362 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
363 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
364 if (hooks) return hooks;
365 return &gHooksNoContext;
366}
367
368#else
369
370void setGlThreadSpecific(gl_hooks_t const *value) {
371 pthread_setspecific(gGLWrapperKey, value);
372}
373
374gl_hooks_t const* getGlThreadSpecific() {
375 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
376 if (hooks) return hooks;
377 return &gHooksNoContext;
378}
379
380#endif
381
382// ----------------------------------------------------------------------------
383// GL / EGL hooks
384// ----------------------------------------------------------------------------
385
386#undef GL_ENTRY
387#undef EGL_ENTRY
388#define GL_ENTRY(_r, _api, ...) #_api,
389#define EGL_ENTRY(_r, _api, ...) #_api,
390
391char const * const gl_names[] = {
392 #include "entries.in"
393 NULL
394};
395
396char const * const egl_names[] = {
397 #include "egl_entries.in"
398 NULL
399};
400
401#undef GL_ENTRY
402#undef EGL_ENTRY
403
404
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700405// ----------------------------------------------------------------------------
406}; // namespace android
407// ----------------------------------------------------------------------------
408