blob: bf2477a64809104a1e303febdd2c74437829630b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
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>
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
Romain Guye03de932011-07-11 15:33:51 -070034#include <utils/CallStack.h>
Mathias Agopian24035332010-08-02 17:34:32 -070035#include <utils/String8.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070036
Mathias Agopian1cadb252011-05-23 17:26:14 -070037#include "egldefs.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include "egl_impl.h"
David Li864f8392011-03-28 10:39:28 -070039#include "egl_tls.h"
Siva Velusamy0469dd62011-11-30 15:05:37 -080040#include "glestrace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070041#include "hooks.h"
42#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Mathias Agopian518ec112011-05-13 16:21:08 -070044#include "egl_display.h"
45#include "egl_object.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
47// ----------------------------------------------------------------------------
48namespace android {
49// ----------------------------------------------------------------------------
50
Mathias Agopianada798b2012-02-13 17:09:30 -080051egl_connection_t gEGLImpl;
52gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070053gl_hooks_t gHooksNoContext;
54pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055
56// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070058#if EGL_TRACE
59
60EGLAPI pthread_key_t gGLTraceKey = -1;
61
62// ----------------------------------------------------------------------------
63
Siva Velusamyb13c78f2012-03-09 14:51:28 -080064/**
Romain Guy16928bf2012-10-18 16:16:10 -070065 * There are three different tracing methods:
66 * 1. libs/EGL/trace.cpp: Traces all functions to systrace.
67 * To enable:
68 * - set system property "debug.egl.trace" to "systrace" to trace all apps.
69 * 2. libs/EGL/trace.cpp: Logs a stack trace for GL errors after each function call.
70 * To enable:
71 * - set system property "debug.egl.trace" to "error" to trace all apps.
72 * 3. libs/EGL/trace.cpp: Traces all functions to logcat.
Siva Velusamyb13c78f2012-03-09 14:51:28 -080073 * To enable:
74 * - set system property "debug.egl.trace" to 1 to trace all apps.
75 * - or call setGLTraceLevel(1) from an app to enable tracing for that app.
Romain Guy16928bf2012-10-18 16:16:10 -070076 * 4. libs/GLES_trace: Traces all functions via protobuf to host.
Siva Velusamyb13c78f2012-03-09 14:51:28 -080077 * To enable:
78 * - set system property "debug.egl.debug_proc" to the application name.
79 * - or call setGLDebugLevel(1) from the app.
80 */
Mathias Agopian518ec112011-05-13 16:21:08 -070081static int sEGLTraceLevel;
82static int sEGLApplicationTraceLevel;
83
Romain Guy16928bf2012-10-18 16:16:10 -070084static bool sEGLSystraceEnabled;
85static bool sEGLGetErrorEnabled;
86
Siva Velusamya73a9772012-12-18 14:56:55 -080087static volatile int sEGLDebugLevel;
Siva Velusamyb13c78f2012-03-09 14:51:28 -080088
Mathias Agopian518ec112011-05-13 16:21:08 -070089extern gl_hooks_t gHooksTrace;
Romain Guy16928bf2012-10-18 16:16:10 -070090extern gl_hooks_t gHooksSystrace;
91extern gl_hooks_t gHooksErrorTrace;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070092
Siva Velusamya73a9772012-12-18 14:56:55 -080093int getEGLDebugLevel() {
94 return sEGLDebugLevel;
95}
96
97void setEGLDebugLevel(int level) {
98 sEGLDebugLevel = level;
99}
100
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700101static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
102 pthread_setspecific(gGLTraceKey, value);
103}
104
105gl_hooks_t const* getGLTraceThreadSpecific() {
106 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
107}
108
Mathias Agopian518ec112011-05-13 16:21:08 -0700109void initEglTraceLevel() {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700110 char value[PROPERTY_VALUE_MAX];
111 property_get("debug.egl.trace", value, "0");
Romain Guy16928bf2012-10-18 16:16:10 -0700112
113 sEGLGetErrorEnabled = !strcasecmp(value, "error");
114 if (sEGLGetErrorEnabled) {
115 sEGLSystraceEnabled = false;
116 sEGLTraceLevel = 0;
117 return;
118 }
119
120 sEGLSystraceEnabled = !strcasecmp(value, "systrace");
121 if (sEGLSystraceEnabled) {
122 sEGLTraceLevel = 0;
123 return;
124 }
125
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700126 int propertyLevel = atoi(value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700127 int applicationLevel = sEGLApplicationTraceLevel;
128 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800129}
David Li499c6f02011-04-08 18:43:16 -0700130
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800131void initEglDebugLevel() {
Siva Velusamya73a9772012-12-18 14:56:55 -0800132 if (getEGLDebugLevel() == 0) {
133 char value[PROPERTY_VALUE_MAX];
Siva Velusamy6482fa42012-11-20 13:39:57 -0800134
Siva Velusamya73a9772012-12-18 14:56:55 -0800135 // check system property only on userdebug or eng builds
136 property_get("ro.debuggable", value, "0");
137 if (value[0] == '0')
138 return;
Siva Velusamy6482fa42012-11-20 13:39:57 -0800139
Siva Velusamya73a9772012-12-18 14:56:55 -0800140 property_get("debug.egl.debug_proc", value, "");
141 if (strlen(value) > 0) {
142 FILE * file = fopen("/proc/self/cmdline", "r");
143 if (file) {
144 char cmdline[256];
145 if (fgets(cmdline, sizeof(cmdline), file)) {
146 if (!strncmp(value, cmdline, strlen(value))) {
147 // set EGL debug if the "debug.egl.debug_proc" property
148 // matches the prefix of this application's command line
149 setEGLDebugLevel(1);
150 }
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800151 }
Siva Velusamya73a9772012-12-18 14:56:55 -0800152 fclose(file);
Siva Velusamy93a826f2011-12-14 12:19:56 -0800153 }
David Li499c6f02011-04-08 18:43:16 -0700154 }
David Li2f5a6552011-03-01 16:08:10 -0800155 }
David Li499c6f02011-04-08 18:43:16 -0700156
Siva Velusamya73a9772012-12-18 14:56:55 -0800157 if (getEGLDebugLevel() > 0) {
158 if (GLTrace_start() < 0) {
159 ALOGE("Error starting Tracer for OpenGL ES. Disabling..");
160 setEGLDebugLevel(0);
161 }
David Li85f33a72011-03-10 19:07:42 -0800162 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700163}
164
Mathias Agopian518ec112011-05-13 16:21:08 -0700165void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Romain Guy16928bf2012-10-18 16:16:10 -0700166 if (sEGLGetErrorEnabled) {
167 setGlTraceThreadSpecific(value);
168 setGlThreadSpecific(&gHooksErrorTrace);
169 } else if (sEGLSystraceEnabled) {
170 setGlTraceThreadSpecific(value);
171 setGlThreadSpecific(&gHooksSystrace);
172 } else if (sEGLTraceLevel > 0) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700173 setGlTraceThreadSpecific(value);
174 setGlThreadSpecific(&gHooksTrace);
Siva Velusamya73a9772012-12-18 14:56:55 -0800175 } else if (getEGLDebugLevel() > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800176 setGlTraceThreadSpecific(value);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800177 setGlThreadSpecific(GLTrace_getGLHooks());
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700178 } else {
Siva Velusamya73a9772012-12-18 14:56:55 -0800179 setGlTraceThreadSpecific(NULL);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700180 setGlThreadSpecific(value);
181 }
182}
183
184/*
185 * Global entry point to allow applications to modify their own trace level.
186 * The effective trace level is the max of this level and the value of debug.egl.trace.
187 */
188extern "C"
189void setGLTraceLevel(int level) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700190 sEGLApplicationTraceLevel = level;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700191}
192
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800193/*
194 * Global entry point to allow applications to modify their own debug level.
195 * Debugging is enabled if either the application requested it, or if the system property
196 * matches the application's name.
Siva Velusamya73a9772012-12-18 14:56:55 -0800197 * Note that this only sets the debug level. The value is read and used either in
198 * initEglDebugLevel() if the application hasn't initialized its display yet, or when
199 * eglSwapBuffers() is called next.
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800200 */
201void EGLAPI setGLDebugLevel(int level) {
Siva Velusamya73a9772012-12-18 14:56:55 -0800202 setEGLDebugLevel(level);
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800203}
204
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700205#else
206
Mathias Agopian518ec112011-05-13 16:21:08 -0700207void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700208 setGlThreadSpecific(value);
209}
210
211#endif
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213/*****************************************************************************/
214
Mathias Agopian6f087122010-09-23 16:38:38 -0700215static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -0700216 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -0700217 char const* const error = "call to OpenGL ES API with "
218 "no current context (logged once per thread)";
219 if (LOG_NDEBUG) {
220 ALOGE(error);
221 } else {
222 LOG_ALWAYS_FATAL(error);
223 }
Mathias Agopianecfe0912011-09-06 17:24:05 -0700224 char value[PROPERTY_VALUE_MAX];
225 property_get("debug.egl.callstack", value, "0");
226 if (atoi(value)) {
227 CallStack stack;
228 stack.update();
229 stack.dump();
230 }
Mathias Agopiand274eae2009-07-31 16:21:17 -0700231 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700232 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700233}
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235static void early_egl_init(void)
236{
237#if !USE_FAST_TLS_KEY
238 pthread_key_create(&gGLWrapperKey, NULL);
239#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700240#if EGL_TRACE
241 pthread_key_create(&gGLTraceKey, NULL);
242 initEglTraceLevel();
243#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 uint32_t addr = (uint32_t)((void*)gl_no_context);
245 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700246 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700248 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700249
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700250 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251}
252
253static pthread_once_t once_control = PTHREAD_ONCE_INIT;
254static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
255
Mathias Agopian518ec112011-05-13 16:21:08 -0700256// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
Jesse Hallb29e5e82012-04-04 16:53:42 -0700258egl_display_ptr validate_display(EGLDisplay dpy) {
259 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700260 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700261 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -0700262 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -0700263 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -0700264
265 return dp;
266}
267
Jesse Hallb29e5e82012-04-04 16:53:42 -0700268egl_display_ptr validate_display_connection(EGLDisplay dpy,
269 egl_connection_t*& cnx) {
270 cnx = NULL;
271 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700272 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700273 return dp;
274 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700276 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700278 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279}
280
Mathias Agopian518ec112011-05-13 16:21:08 -0700281// ----------------------------------------------------------------------------
282
Mathias Agopian48d438d2012-01-28 21:44:00 -0800283const GLubyte * egl_get_string_for_current_context(GLenum name) {
284 // NOTE: returning NULL here will fall-back to the default
285 // implementation.
286
287 EGLContext context = egl_tls_t::getContext();
288 if (context == EGL_NO_CONTEXT)
289 return NULL;
290
291 egl_context_t const * const c = get_context(context);
292 if (c == NULL) // this should never happen, by construction
293 return NULL;
294
295 if (name != GL_EXTENSIONS)
296 return NULL;
297
298 return (const GLubyte *)c->gl_extensions.string();
299}
300
301// ----------------------------------------------------------------------------
302
Mathias Agopian923c6612009-08-17 18:07:06 -0700303// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700304// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700305// egl_init_drivers_locked()
306//
Mathias Agopian518ec112011-05-13 16:21:08 -0700307static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700309 // initialized by static ctor. should be set here.
310 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 }
312
Mathias Agopiande586972009-05-28 17:39:03 -0700313 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700314 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700315
Mathias Agopianada798b2012-02-13 17:09:30 -0800316 // dynamically load our EGL implementation
317 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800319 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
320 &gHooks[egl_connection_t::GLESv1_INDEX];
321 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
322 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800323 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 }
325
Mathias Agopianada798b2012-02-13 17:09:30 -0800326 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327}
328
Mathias Agopian518ec112011-05-13 16:21:08 -0700329static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
330
331EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700332 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700333 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700334 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700335 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700336 return res;
337}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700338
Mathias Agopian1cadb252011-05-23 17:26:14 -0700339void gl_unimplemented() {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000340 ALOGE("called unimplemented OpenGL ES API");
Mathias Agopian1cadb252011-05-23 17:26:14 -0700341}
342
Mathias Agopian48d438d2012-01-28 21:44:00 -0800343void gl_noop() {
344}
345
Mathias Agopian1cadb252011-05-23 17:26:14 -0700346// ----------------------------------------------------------------------------
347
348#if USE_FAST_TLS_KEY
349
350// We have a dedicated TLS slot in bionic
351static inline gl_hooks_t const * volatile * get_tls_hooks() {
352 volatile void *tls_base = __get_tls();
353 gl_hooks_t const * volatile * tls_hooks =
354 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
355 return tls_hooks;
356}
357
358void setGlThreadSpecific(gl_hooks_t const *value) {
359 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
360 tls_hooks[TLS_SLOT_OPENGL_API] = value;
361}
362
363gl_hooks_t const* getGlThreadSpecific() {
364 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
365 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
366 if (hooks) return hooks;
367 return &gHooksNoContext;
368}
369
370#else
371
372void setGlThreadSpecific(gl_hooks_t const *value) {
373 pthread_setspecific(gGLWrapperKey, value);
374}
375
376gl_hooks_t const* getGlThreadSpecific() {
377 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
378 if (hooks) return hooks;
379 return &gHooksNoContext;
380}
381
382#endif
383
384// ----------------------------------------------------------------------------
385// GL / EGL hooks
386// ----------------------------------------------------------------------------
387
388#undef GL_ENTRY
389#undef EGL_ENTRY
390#define GL_ENTRY(_r, _api, ...) #_api,
391#define EGL_ENTRY(_r, _api, ...) #_api,
392
393char const * const gl_names[] = {
394 #include "entries.in"
395 NULL
396};
397
398char const * const egl_names[] = {
399 #include "egl_entries.in"
400 NULL
401};
402
403#undef GL_ENTRY
404#undef EGL_ENTRY
405
406
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700407// ----------------------------------------------------------------------------
408}; // namespace android
409// ----------------------------------------------------------------------------
410