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
27 #include <cutils/log.h>
28 #include <cutils/atomic.h>
29 #include <cutils/properties.h>
30 #include <cutils/memory.h>
31
32 #include <utils/CallStack.h>
33 #include <utils/String8.h>
34
35 #include "../egl_impl.h"
36 #include "../glestrace.h"
37
38 #include "egl_tls.h"
39 #include "egldefs.h"
40 #include "Loader.h"
41
42 #include "egl_display.h"
43 #include "egl_object.h"
44
45 // ----------------------------------------------------------------------------
46 namespace android {
47 // ----------------------------------------------------------------------------
48
49 egl_connection_t gEGLImpl;
50 gl_hooks_t gHooks[2];
51 gl_hooks_t gHooksNoContext;
52 pthread_key_t gGLWrapperKey = -1;
53
54 // ----------------------------------------------------------------------------
55
56 #if EGL_TRACE
57
58 EGLAPI pthread_key_t gGLTraceKey = -1;
59
60 // ----------------------------------------------------------------------------
61
62 /**
63 * 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.
71 * 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.
74 * 4. libs/GLES_trace: Traces all functions via protobuf to host.
75 * To enable:
76 * - set system property "debug.egl.debug_proc" to the application name.
77 * - or call setGLDebugLevel(1) from the app.
78 */
79 static int sEGLTraceLevel;
80 static int sEGLApplicationTraceLevel;
81
82 static bool sEGLSystraceEnabled;
83 static bool sEGLGetErrorEnabled;
84
85 static volatile int sEGLDebugLevel;
86
87 extern gl_hooks_t gHooksTrace;
88 extern gl_hooks_t gHooksSystrace;
89 extern gl_hooks_t gHooksErrorTrace;
90
getEGLDebugLevel()91 int getEGLDebugLevel() {
92 return sEGLDebugLevel;
93 }
94
setEGLDebugLevel(int level)95 void setEGLDebugLevel(int level) {
96 sEGLDebugLevel = level;
97 }
98
setGlTraceThreadSpecific(gl_hooks_t const * value)99 static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
100 pthread_setspecific(gGLTraceKey, value);
101 }
102
getGLTraceThreadSpecific()103 gl_hooks_t const* getGLTraceThreadSpecific() {
104 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
105 }
106
initEglTraceLevel()107 void initEglTraceLevel() {
108 char value[PROPERTY_VALUE_MAX];
109 property_get("debug.egl.trace", value, "0");
110
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
124 int propertyLevel = atoi(value);
125 int applicationLevel = sEGLApplicationTraceLevel;
126 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
127 }
128
initEglDebugLevel()129 void initEglDebugLevel() {
130 if (getEGLDebugLevel() == 0) {
131 char value[PROPERTY_VALUE_MAX];
132
133 // check system property only on userdebug or eng builds
134 property_get("ro.debuggable", value, "0");
135 if (value[0] == '0')
136 return;
137
138 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 }
149 }
150 fclose(file);
151 }
152 }
153 }
154
155 if (getEGLDebugLevel() > 0) {
156 if (GLTrace_start() < 0) {
157 ALOGE("Error starting Tracer for OpenGL ES. Disabling..");
158 setEGLDebugLevel(0);
159 }
160 }
161 }
162
setGLHooksThreadSpecific(gl_hooks_t const * value)163 void setGLHooksThreadSpecific(gl_hooks_t const *value) {
164 if (sEGLGetErrorEnabled) {
165 setGlTraceThreadSpecific(value);
166 setGlThreadSpecific(&gHooksErrorTrace);
167 } else if (sEGLSystraceEnabled) {
168 setGlTraceThreadSpecific(value);
169 setGlThreadSpecific(&gHooksSystrace);
170 } else if (sEGLTraceLevel > 0) {
171 setGlTraceThreadSpecific(value);
172 setGlThreadSpecific(&gHooksTrace);
173 } else if (getEGLDebugLevel() > 0 && value != &gHooksNoContext) {
174 setGlTraceThreadSpecific(value);
175 setGlThreadSpecific(GLTrace_getGLHooks());
176 } else {
177 setGlTraceThreadSpecific(NULL);
178 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 */
186 extern "C"
setGLTraceLevel(int level)187 void setGLTraceLevel(int level) {
188 sEGLApplicationTraceLevel = level;
189 }
190
191 /*
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.
195 * 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.
198 */
setGLDebugLevel(int level)199 void EGLAPI setGLDebugLevel(int level) {
200 setEGLDebugLevel(level);
201 }
202
203 #else
204
setGLHooksThreadSpecific(gl_hooks_t const * value)205 void setGLHooksThreadSpecific(gl_hooks_t const *value) {
206 setGlThreadSpecific(value);
207 }
208
209 #endif
210
211 /*****************************************************************************/
212
gl_no_context()213 static int gl_no_context() {
214 if (egl_tls_t::logNoContextCall()) {
215 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 }
222 char value[PROPERTY_VALUE_MAX];
223 property_get("debug.egl.callstack", value, "0");
224 if (atoi(value)) {
225 CallStack stack(LOG_TAG);
226 }
227 }
228 return 0;
229 }
230
early_egl_init(void)231 static void early_egl_init(void)
232 {
233 #if EGL_TRACE
234 pthread_key_create(&gGLTraceKey, NULL);
235 initEglTraceLevel();
236 #endif
237 uint32_t addr = (uint32_t)((void*)gl_no_context);
238 android_memset32(
239 (uint32_t*)(void*)&gHooksNoContext,
240 addr,
241 sizeof(gHooksNoContext));
242
243 setGLHooksThreadSpecific(&gHooksNoContext);
244 }
245
246 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
247 static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
248
249 // ----------------------------------------------------------------------------
250
validate_display(EGLDisplay dpy)251 egl_display_ptr validate_display(EGLDisplay dpy) {
252 egl_display_ptr dp = get_display(dpy);
253 if (!dp)
254 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
255 if (!dp->isReady())
256 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
257
258 return dp;
259 }
260
validate_display_connection(EGLDisplay dpy,egl_connection_t * & cnx)261 egl_display_ptr validate_display_connection(EGLDisplay dpy,
262 egl_connection_t*& cnx) {
263 cnx = NULL;
264 egl_display_ptr dp = validate_display(dpy);
265 if (!dp)
266 return dp;
267 cnx = &gEGLImpl;
268 if (cnx->dso == 0) {
269 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
270 }
271 return dp;
272 }
273
274 // ----------------------------------------------------------------------------
275
egl_get_string_for_current_context(GLenum name)276 const GLubyte * egl_get_string_for_current_context(GLenum name) {
277 // NOTE: returning NULL here will fall-back to the default
278 // implementation.
279
280 EGLContext context = egl_tls_t::getContext();
281 if (context == EGL_NO_CONTEXT)
282 return NULL;
283
284 egl_context_t const * const c = get_context(context);
285 if (c == NULL) // this should never happen, by construction
286 return NULL;
287
288 if (name != GL_EXTENSIONS)
289 return NULL;
290
291 return (const GLubyte *)c->gl_extensions.string();
292 }
293
294 // ----------------------------------------------------------------------------
295
296 // this mutex protects:
297 // d->disp[]
298 // egl_init_drivers_locked()
299 //
egl_init_drivers_locked()300 static EGLBoolean egl_init_drivers_locked() {
301 if (sEarlyInitState) {
302 // initialized by static ctor. should be set here.
303 return EGL_FALSE;
304 }
305
306 // get our driver loader
307 Loader& loader(Loader::getInstance());
308
309 // dynamically load our EGL implementation
310 egl_connection_t* cnx = &gEGLImpl;
311 if (cnx->dso == 0) {
312 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
313 &gHooks[egl_connection_t::GLESv1_INDEX];
314 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
315 &gHooks[egl_connection_t::GLESv2_INDEX];
316 cnx->dso = loader.open(cnx);
317 }
318
319 return cnx->dso ? EGL_TRUE : EGL_FALSE;
320 }
321
322 static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
323
egl_init_drivers()324 EGLBoolean egl_init_drivers() {
325 EGLBoolean res;
326 pthread_mutex_lock(&sInitDriverMutex);
327 res = egl_init_drivers_locked();
328 pthread_mutex_unlock(&sInitDriverMutex);
329 return res;
330 }
331
gl_unimplemented()332 void gl_unimplemented() {
333 ALOGE("called unimplemented OpenGL ES API");
334 char value[PROPERTY_VALUE_MAX];
335 property_get("debug.egl.callstack", value, "0");
336 if (atoi(value)) {
337 CallStack stack(LOG_TAG);
338 }
339 }
340
gl_noop()341 void gl_noop() {
342 }
343
344 // ----------------------------------------------------------------------------
345
setGlThreadSpecific(gl_hooks_t const * value)346 void setGlThreadSpecific(gl_hooks_t const *value) {
347 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
348 tls_hooks[TLS_SLOT_OPENGL_API] = value;
349 }
350
351 // ----------------------------------------------------------------------------
352 // GL / EGL hooks
353 // ----------------------------------------------------------------------------
354
355 #undef GL_ENTRY
356 #undef EGL_ENTRY
357 #define GL_ENTRY(_r, _api, ...) #_api,
358 #define EGL_ENTRY(_r, _api, ...) #_api,
359
360 char const * const gl_names[] = {
361 #include "../entries.in"
362 NULL
363 };
364
365 char const * const egl_names[] = {
366 #include "egl_entries.in"
367 NULL
368 };
369
370 #undef GL_ENTRY
371 #undef EGL_ENTRY
372
373
374 // ----------------------------------------------------------------------------
375 }; // namespace android
376 // ----------------------------------------------------------------------------
377
378