blob: 0d4bed522df78d56032f28919d3fef5f3ec9a64f [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 Velusamyb13c78f2012-03-09 14:51:28 -080087int gEGLDebugLevel;
88static int sEGLApplicationDebugLevel;
89
Mathias Agopian518ec112011-05-13 16:21:08 -070090extern gl_hooks_t gHooksTrace;
Romain Guy16928bf2012-10-18 16:16:10 -070091extern gl_hooks_t gHooksSystrace;
92extern gl_hooks_t gHooksErrorTrace;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070093
94static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
95 pthread_setspecific(gGLTraceKey, value);
96}
97
98gl_hooks_t const* getGLTraceThreadSpecific() {
99 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
100}
101
Mathias Agopian518ec112011-05-13 16:21:08 -0700102void initEglTraceLevel() {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700103 char value[PROPERTY_VALUE_MAX];
104 property_get("debug.egl.trace", value, "0");
Romain Guy16928bf2012-10-18 16:16:10 -0700105
106 sEGLGetErrorEnabled = !strcasecmp(value, "error");
107 if (sEGLGetErrorEnabled) {
108 sEGLSystraceEnabled = false;
109 sEGLTraceLevel = 0;
110 return;
111 }
112
113 sEGLSystraceEnabled = !strcasecmp(value, "systrace");
114 if (sEGLSystraceEnabled) {
115 sEGLTraceLevel = 0;
116 return;
117 }
118
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700119 int propertyLevel = atoi(value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700120 int applicationLevel = sEGLApplicationTraceLevel;
121 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800122}
David Li499c6f02011-04-08 18:43:16 -0700123
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800124void initEglDebugLevel() {
125 int propertyLevel = 0;
126 char value[PROPERTY_VALUE_MAX];
Siva Velusamya9a4cd42012-11-20 13:39:57 -0800127
128 // check system property only on userdebug or eng builds
129 property_get("ro.debuggable", value, "0");
130 if (value[0] == '0')
131 return;
132
David Li2f5a6552011-03-01 16:08:10 -0800133 property_get("debug.egl.debug_proc", value, "");
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800134 if (strlen(value) > 0) {
135 long pid = getpid();
136 char procPath[128] = {};
137 sprintf(procPath, "/proc/%ld/cmdline", pid);
138 FILE * file = fopen(procPath, "r");
139 if (file) {
140 char cmdline[256] = {};
141 if (fgets(cmdline, sizeof(cmdline) - 1, file)) {
142 if (!strncmp(value, cmdline, strlen(value))) {
143 // set EGL debug if the "debug.egl.debug_proc" property
144 // matches the prefix of this application's command line
145 propertyLevel = 1;
146 }
Siva Velusamy93a826f2011-12-14 12:19:56 -0800147 }
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800148 fclose(file);
David Li499c6f02011-04-08 18:43:16 -0700149 }
David Li2f5a6552011-03-01 16:08:10 -0800150 }
David Li499c6f02011-04-08 18:43:16 -0700151
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800152 gEGLDebugLevel = propertyLevel || sEGLApplicationDebugLevel;
Siva Velusamy0469dd62011-11-30 15:05:37 -0800153 if (gEGLDebugLevel > 0) {
154 GLTrace_start();
David Li85f33a72011-03-10 19:07:42 -0800155 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700156}
157
Mathias Agopian518ec112011-05-13 16:21:08 -0700158void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Romain Guy16928bf2012-10-18 16:16:10 -0700159 if (sEGLGetErrorEnabled) {
160 setGlTraceThreadSpecific(value);
161 setGlThreadSpecific(&gHooksErrorTrace);
162 } else if (sEGLSystraceEnabled) {
163 setGlTraceThreadSpecific(value);
164 setGlThreadSpecific(&gHooksSystrace);
165 } else if (sEGLTraceLevel > 0) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700166 setGlTraceThreadSpecific(value);
167 setGlThreadSpecific(&gHooksTrace);
Mathias Agopianccfa5c32011-09-01 14:55:00 -0700168 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800169 setGlTraceThreadSpecific(value);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800170 setGlThreadSpecific(GLTrace_getGLHooks());
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700171 } else {
172 setGlThreadSpecific(value);
173 }
174}
175
176/*
177 * Global entry point to allow applications to modify their own trace level.
178 * The effective trace level is the max of this level and the value of debug.egl.trace.
179 */
180extern "C"
181void setGLTraceLevel(int level) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700182 sEGLApplicationTraceLevel = level;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700183}
184
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800185/*
186 * Global entry point to allow applications to modify their own debug level.
187 * Debugging is enabled if either the application requested it, or if the system property
188 * matches the application's name.
189 */
190void EGLAPI setGLDebugLevel(int level) {
191 sEGLApplicationDebugLevel = level;
192}
193
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700194#else
195
Mathias Agopian518ec112011-05-13 16:21:08 -0700196void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700197 setGlThreadSpecific(value);
198}
199
200#endif
201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202/*****************************************************************************/
203
Mathias Agopian6f087122010-09-23 16:38:38 -0700204static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -0700205 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -0700206 char const* const error = "call to OpenGL ES API with "
207 "no current context (logged once per thread)";
208 if (LOG_NDEBUG) {
209 ALOGE(error);
210 } else {
211 LOG_ALWAYS_FATAL(error);
212 }
Mathias Agopianecfe0912011-09-06 17:24:05 -0700213 char value[PROPERTY_VALUE_MAX];
214 property_get("debug.egl.callstack", value, "0");
215 if (atoi(value)) {
216 CallStack stack;
217 stack.update();
218 stack.dump();
219 }
Mathias Agopiand274eae2009-07-31 16:21:17 -0700220 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700221 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700222}
223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224static void early_egl_init(void)
225{
226#if !USE_FAST_TLS_KEY
227 pthread_key_create(&gGLWrapperKey, NULL);
228#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700229#if EGL_TRACE
230 pthread_key_create(&gGLTraceKey, NULL);
231 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800232 initEglDebugLevel();
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700233#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 uint32_t addr = (uint32_t)((void*)gl_no_context);
235 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700236 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700238 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700239
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700240 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241}
242
243static pthread_once_t once_control = PTHREAD_ONCE_INIT;
244static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
245
Mathias Agopian518ec112011-05-13 16:21:08 -0700246// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247
Jesse Hallb29e5e82012-04-04 16:53:42 -0700248egl_display_ptr validate_display(EGLDisplay dpy) {
249 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700250 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700251 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -0700252 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -0700253 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -0700254
255 return dp;
256}
257
Jesse Hallb29e5e82012-04-04 16:53:42 -0700258egl_display_ptr validate_display_connection(EGLDisplay dpy,
259 egl_connection_t*& cnx) {
260 cnx = NULL;
261 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700262 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700263 return dp;
264 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700266 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700268 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269}
270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271// ----------------------------------------------------------------------------
272
Mathias Agopian48d438d2012-01-28 21:44:00 -0800273const GLubyte * egl_get_string_for_current_context(GLenum name) {
274 // NOTE: returning NULL here will fall-back to the default
275 // implementation.
276
277 EGLContext context = egl_tls_t::getContext();
278 if (context == EGL_NO_CONTEXT)
279 return NULL;
280
281 egl_context_t const * const c = get_context(context);
282 if (c == NULL) // this should never happen, by construction
283 return NULL;
284
285 if (name != GL_EXTENSIONS)
286 return NULL;
287
288 return (const GLubyte *)c->gl_extensions.string();
289}
290
291// ----------------------------------------------------------------------------
292
Mathias Agopian923c6612009-08-17 18:07:06 -0700293// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700294// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700295// egl_init_drivers_locked()
296//
Mathias Agopian518ec112011-05-13 16:21:08 -0700297static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700299 // initialized by static ctor. should be set here.
300 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 }
302
Mathias Agopiande586972009-05-28 17:39:03 -0700303 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700304 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700305
Mathias Agopianada798b2012-02-13 17:09:30 -0800306 // dynamically load our EGL implementation
307 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800309 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
310 &gHooks[egl_connection_t::GLESv1_INDEX];
311 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
312 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800313 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 }
315
Mathias Agopianada798b2012-02-13 17:09:30 -0800316 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317}
318
Mathias Agopian518ec112011-05-13 16:21:08 -0700319static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
320
321EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700322 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700323 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700324 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700325 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700326 return res;
327}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328
Mathias Agopian1cadb252011-05-23 17:26:14 -0700329void gl_unimplemented() {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000330 ALOGE("called unimplemented OpenGL ES API");
Mathias Agopian1cadb252011-05-23 17:26:14 -0700331}
332
Mathias Agopian48d438d2012-01-28 21:44:00 -0800333void gl_noop() {
334}
335
Mathias Agopian1cadb252011-05-23 17:26:14 -0700336// ----------------------------------------------------------------------------
337
338#if USE_FAST_TLS_KEY
339
340// We have a dedicated TLS slot in bionic
341static inline gl_hooks_t const * volatile * get_tls_hooks() {
342 volatile void *tls_base = __get_tls();
343 gl_hooks_t const * volatile * tls_hooks =
344 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
345 return tls_hooks;
346}
347
348void setGlThreadSpecific(gl_hooks_t const *value) {
349 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
350 tls_hooks[TLS_SLOT_OPENGL_API] = value;
351}
352
353gl_hooks_t const* getGlThreadSpecific() {
354 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
355 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
356 if (hooks) return hooks;
357 return &gHooksNoContext;
358}
359
360#else
361
362void setGlThreadSpecific(gl_hooks_t const *value) {
363 pthread_setspecific(gGLWrapperKey, value);
364}
365
366gl_hooks_t const* getGlThreadSpecific() {
367 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
368 if (hooks) return hooks;
369 return &gHooksNoContext;
370}
371
372#endif
373
374// ----------------------------------------------------------------------------
375// GL / EGL hooks
376// ----------------------------------------------------------------------------
377
378#undef GL_ENTRY
379#undef EGL_ENTRY
380#define GL_ENTRY(_r, _api, ...) #_api,
381#define EGL_ENTRY(_r, _api, ...) #_api,
382
383char const * const gl_names[] = {
384 #include "entries.in"
385 NULL
386};
387
388char const * const egl_names[] = {
389 #include "egl_entries.in"
390 NULL
391};
392
393#undef GL_ENTRY
394#undef EGL_ENTRY
395
396
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700397// ----------------------------------------------------------------------------
398}; // namespace android
399// ----------------------------------------------------------------------------
400