blob: 67fbae5dd0267306b193ee64449fbacb5520f840 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Romain Guye03de932011-07-11 15:33:51 -070031#include <utils/CallStack.h>
Mathias Agopian24035332010-08-02 17:34:32 -070032#include <utils/String8.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070033
Mathias Agopian39c24a22013-04-04 23:17:56 -070034#include "../egl_impl.h"
35#include "../glestrace.h"
36
David Li864f8392011-03-28 10:39:28 -070037#include "egl_tls.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070038#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070039#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040
Mathias Agopian518ec112011-05-13 16:21:08 -070041#include "egl_display.h"
42#include "egl_object.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Dan Stozac3289c42014-01-17 11:38:34 -080044typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer;
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046// ----------------------------------------------------------------------------
47namespace android {
48// ----------------------------------------------------------------------------
49
Mathias Agopianada798b2012-02-13 17:09:30 -080050egl_connection_t gEGLImpl;
51gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070052gl_hooks_t gHooksNoContext;
53pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054
55// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070057#if EGL_TRACE
58
59EGLAPI pthread_key_t gGLTraceKey = -1;
60
61// ----------------------------------------------------------------------------
62
Siva Velusamyb13c78f2012-03-09 14:51:28 -080063/**
Romain Guy16928bf2012-10-18 16:16:10 -070064 * There are three different tracing methods:
65 * 1. libs/EGL/trace.cpp: Traces all functions to systrace.
66 * To enable:
67 * - set system property "debug.egl.trace" to "systrace" to trace all apps.
68 * 2. libs/EGL/trace.cpp: Logs a stack trace for GL errors after each function call.
69 * To enable:
70 * - set system property "debug.egl.trace" to "error" to trace all apps.
71 * 3. libs/EGL/trace.cpp: Traces all functions to logcat.
Siva Velusamyb13c78f2012-03-09 14:51:28 -080072 * To enable:
73 * - set system property "debug.egl.trace" to 1 to trace all apps.
74 * - or call setGLTraceLevel(1) from an app to enable tracing for that app.
Romain Guy16928bf2012-10-18 16:16:10 -070075 * 4. libs/GLES_trace: Traces all functions via protobuf to host.
Siva Velusamyb13c78f2012-03-09 14:51:28 -080076 * To enable:
77 * - set system property "debug.egl.debug_proc" to the application name.
78 * - or call setGLDebugLevel(1) from the app.
79 */
Mathias Agopian518ec112011-05-13 16:21:08 -070080static int sEGLTraceLevel;
81static int sEGLApplicationTraceLevel;
82
Romain Guy16928bf2012-10-18 16:16:10 -070083static bool sEGLSystraceEnabled;
84static bool sEGLGetErrorEnabled;
85
Siva Velusamya73a9772012-12-18 14:56:55 -080086static volatile int sEGLDebugLevel;
Siva Velusamyb13c78f2012-03-09 14:51:28 -080087
Mathias Agopian518ec112011-05-13 16:21:08 -070088extern gl_hooks_t gHooksTrace;
Romain Guy16928bf2012-10-18 16:16:10 -070089extern gl_hooks_t gHooksSystrace;
90extern gl_hooks_t gHooksErrorTrace;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070091
Siva Velusamya73a9772012-12-18 14:56:55 -080092int getEGLDebugLevel() {
93 return sEGLDebugLevel;
94}
95
96void setEGLDebugLevel(int level) {
97 sEGLDebugLevel = level;
98}
99
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700100static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
101 pthread_setspecific(gGLTraceKey, value);
102}
103
104gl_hooks_t const* getGLTraceThreadSpecific() {
105 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
106}
107
Mathias Agopian518ec112011-05-13 16:21:08 -0700108void initEglTraceLevel() {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700109 char value[PROPERTY_VALUE_MAX];
110 property_get("debug.egl.trace", value, "0");
Romain Guy16928bf2012-10-18 16:16:10 -0700111
112 sEGLGetErrorEnabled = !strcasecmp(value, "error");
113 if (sEGLGetErrorEnabled) {
114 sEGLSystraceEnabled = false;
115 sEGLTraceLevel = 0;
116 return;
117 }
118
119 sEGLSystraceEnabled = !strcasecmp(value, "systrace");
120 if (sEGLSystraceEnabled) {
121 sEGLTraceLevel = 0;
122 return;
123 }
124
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700125 int propertyLevel = atoi(value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700126 int applicationLevel = sEGLApplicationTraceLevel;
127 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800128}
David Li499c6f02011-04-08 18:43:16 -0700129
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800130void initEglDebugLevel() {
Siva Velusamya73a9772012-12-18 14:56:55 -0800131 if (getEGLDebugLevel() == 0) {
132 char value[PROPERTY_VALUE_MAX];
Siva Velusamy6482fa42012-11-20 13:39:57 -0800133
Siva Velusamya73a9772012-12-18 14:56:55 -0800134 // check system property only on userdebug or eng builds
135 property_get("ro.debuggable", value, "0");
136 if (value[0] == '0')
137 return;
Siva Velusamy6482fa42012-11-20 13:39:57 -0800138
Siva Velusamya73a9772012-12-18 14:56:55 -0800139 property_get("debug.egl.debug_proc", value, "");
140 if (strlen(value) > 0) {
141 FILE * file = fopen("/proc/self/cmdline", "r");
142 if (file) {
143 char cmdline[256];
144 if (fgets(cmdline, sizeof(cmdline), file)) {
145 if (!strncmp(value, cmdline, strlen(value))) {
146 // set EGL debug if the "debug.egl.debug_proc" property
147 // matches the prefix of this application's command line
148 setEGLDebugLevel(1);
149 }
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800150 }
Siva Velusamya73a9772012-12-18 14:56:55 -0800151 fclose(file);
Siva Velusamy93a826f2011-12-14 12:19:56 -0800152 }
David Li499c6f02011-04-08 18:43:16 -0700153 }
David Li2f5a6552011-03-01 16:08:10 -0800154 }
David Li499c6f02011-04-08 18:43:16 -0700155
Siva Velusamya73a9772012-12-18 14:56:55 -0800156 if (getEGLDebugLevel() > 0) {
157 if (GLTrace_start() < 0) {
158 ALOGE("Error starting Tracer for OpenGL ES. Disabling..");
159 setEGLDebugLevel(0);
160 }
David Li85f33a72011-03-10 19:07:42 -0800161 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700162}
163
Mathias Agopian518ec112011-05-13 16:21:08 -0700164void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Romain Guy16928bf2012-10-18 16:16:10 -0700165 if (sEGLGetErrorEnabled) {
166 setGlTraceThreadSpecific(value);
167 setGlThreadSpecific(&gHooksErrorTrace);
168 } else if (sEGLSystraceEnabled) {
169 setGlTraceThreadSpecific(value);
170 setGlThreadSpecific(&gHooksSystrace);
171 } else if (sEGLTraceLevel > 0) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700172 setGlTraceThreadSpecific(value);
173 setGlThreadSpecific(&gHooksTrace);
Siva Velusamya73a9772012-12-18 14:56:55 -0800174 } else if (getEGLDebugLevel() > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800175 setGlTraceThreadSpecific(value);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800176 setGlThreadSpecific(GLTrace_getGLHooks());
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700177 } else {
Siva Velusamya73a9772012-12-18 14:56:55 -0800178 setGlTraceThreadSpecific(NULL);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700179 setGlThreadSpecific(value);
180 }
181}
182
183/*
184 * Global entry point to allow applications to modify their own trace level.
185 * The effective trace level is the max of this level and the value of debug.egl.trace.
186 */
187extern "C"
188void setGLTraceLevel(int level) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700189 sEGLApplicationTraceLevel = level;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700190}
191
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800192/*
193 * Global entry point to allow applications to modify their own debug level.
194 * Debugging is enabled if either the application requested it, or if the system property
195 * matches the application's name.
Siva Velusamya73a9772012-12-18 14:56:55 -0800196 * Note that this only sets the debug level. The value is read and used either in
197 * initEglDebugLevel() if the application hasn't initialized its display yet, or when
198 * eglSwapBuffers() is called next.
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800199 */
200void EGLAPI setGLDebugLevel(int level) {
Siva Velusamya73a9772012-12-18 14:56:55 -0800201 setEGLDebugLevel(level);
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800202}
203
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700204#else
205
Mathias Agopian518ec112011-05-13 16:21:08 -0700206void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700207 setGlThreadSpecific(value);
208}
209
210#endif
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212/*****************************************************************************/
213
Mathias Agopian6f087122010-09-23 16:38:38 -0700214static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -0700215 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -0700216 char const* const error = "call to OpenGL ES API with "
217 "no current context (logged once per thread)";
218 if (LOG_NDEBUG) {
219 ALOGE(error);
220 } else {
221 LOG_ALWAYS_FATAL(error);
222 }
Mathias Agopianecfe0912011-09-06 17:24:05 -0700223 char value[PROPERTY_VALUE_MAX];
224 property_get("debug.egl.callstack", value, "0");
225 if (atoi(value)) {
Mathias Agopiancab25d62013-03-21 17:12:40 -0700226 CallStack stack(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -0700227 }
Mathias Agopiand274eae2009-07-31 16:21:17 -0700228 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700229 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700230}
231
Jesse Hall47743382013-02-08 11:13:46 -0800232static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233{
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700234#if EGL_TRACE
235 pthread_key_create(&gGLTraceKey, NULL);
236 initEglTraceLevel();
237#endif
Dan Stozac3289c42014-01-17 11:38:34 -0800238 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
239 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
240 for (int hook = 0; hook < numHooks; ++hook) {
241 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
242 }
Mathias Agopian05c53112010-09-23 11:32:52 -0700243
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700244 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245}
246
247static pthread_once_t once_control = PTHREAD_ONCE_INIT;
248static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
249
Mathias Agopian518ec112011-05-13 16:21:08 -0700250// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
Jesse Hallb29e5e82012-04-04 16:53:42 -0700252egl_display_ptr validate_display(EGLDisplay dpy) {
253 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700254 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700255 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -0700256 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -0700257 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -0700258
259 return dp;
260}
261
Jesse Hallb29e5e82012-04-04 16:53:42 -0700262egl_display_ptr validate_display_connection(EGLDisplay dpy,
263 egl_connection_t*& cnx) {
264 cnx = NULL;
265 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700266 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700267 return dp;
268 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700270 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700272 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273}
274
Mathias Agopian518ec112011-05-13 16:21:08 -0700275// ----------------------------------------------------------------------------
276
Mathias Agopian48d438d2012-01-28 21:44:00 -0800277const GLubyte * egl_get_string_for_current_context(GLenum name) {
278 // NOTE: returning NULL here will fall-back to the default
279 // implementation.
280
281 EGLContext context = egl_tls_t::getContext();
282 if (context == EGL_NO_CONTEXT)
283 return NULL;
284
285 egl_context_t const * const c = get_context(context);
286 if (c == NULL) // this should never happen, by construction
287 return NULL;
288
289 if (name != GL_EXTENSIONS)
290 return NULL;
291
292 return (const GLubyte *)c->gl_extensions.string();
293}
294
295// ----------------------------------------------------------------------------
296
Mathias Agopian923c6612009-08-17 18:07:06 -0700297// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700298// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700299// egl_init_drivers_locked()
300//
Mathias Agopian518ec112011-05-13 16:21:08 -0700301static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700303 // initialized by static ctor. should be set here.
304 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 }
306
Mathias Agopiande586972009-05-28 17:39:03 -0700307 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700308 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700309
Mathias Agopianada798b2012-02-13 17:09:30 -0800310 // dynamically load our EGL implementation
311 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800313 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
314 &gHooks[egl_connection_t::GLESv1_INDEX];
315 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
316 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800317 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
319
Mathias Agopianada798b2012-02-13 17:09:30 -0800320 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321}
322
Mathias Agopian518ec112011-05-13 16:21:08 -0700323static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
324
325EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700326 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700327 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700328 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700329 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700330 return res;
331}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332
Mathias Agopian1cadb252011-05-23 17:26:14 -0700333void gl_unimplemented() {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000334 ALOGE("called unimplemented OpenGL ES API");
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700335 char value[PROPERTY_VALUE_MAX];
336 property_get("debug.egl.callstack", value, "0");
337 if (atoi(value)) {
338 CallStack stack(LOG_TAG);
339 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700340}
341
Mathias Agopian48d438d2012-01-28 21:44:00 -0800342void gl_noop() {
343}
344
Mathias Agopian1cadb252011-05-23 17:26:14 -0700345// ----------------------------------------------------------------------------
346
Mathias Agopian1cadb252011-05-23 17:26:14 -0700347void setGlThreadSpecific(gl_hooks_t const *value) {
348 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
349 tls_hooks[TLS_SLOT_OPENGL_API] = value;
350}
351
Mathias Agopian1cadb252011-05-23 17:26:14 -0700352// ----------------------------------------------------------------------------
353// GL / EGL hooks
354// ----------------------------------------------------------------------------
355
356#undef GL_ENTRY
357#undef EGL_ENTRY
358#define GL_ENTRY(_r, _api, ...) #_api,
359#define EGL_ENTRY(_r, _api, ...) #_api,
360
361char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700362 #include "../entries.in"
Mathias Agopian1cadb252011-05-23 17:26:14 -0700363 NULL
364};
365
366char const * const egl_names[] = {
367 #include "egl_entries.in"
368 NULL
369};
370
371#undef GL_ENTRY
372#undef EGL_ENTRY
373
374
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700375// ----------------------------------------------------------------------------
376}; // namespace android
377// ----------------------------------------------------------------------------
378