• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 
34 #include <utils/CallStack.h>
35 #include <utils/String8.h>
36 
37 #include "egldefs.h"
38 #include "egl_impl.h"
39 #include "egl_tls.h"
40 #include "glesv2dbg.h"
41 #include "hooks.h"
42 #include "Loader.h"
43 
44 #include "egl_display.h"
45 #include "egl_object.h"
46 
47 // ----------------------------------------------------------------------------
48 namespace android {
49 // ----------------------------------------------------------------------------
50 
51 egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
52 gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
53 gl_hooks_t gHooksNoContext;
54 pthread_key_t gGLWrapperKey = -1;
55 
56 // ----------------------------------------------------------------------------
57 
58 #if EGL_TRACE
59 
60 EGLAPI pthread_key_t gGLTraceKey = -1;
61 
62 // ----------------------------------------------------------------------------
63 
64 int gEGLDebugLevel;
65 
66 static int sEGLTraceLevel;
67 static int sEGLApplicationTraceLevel;
68 
69 extern gl_hooks_t gHooksTrace;
70 extern gl_hooks_t gHooksDebug;
71 
setGlTraceThreadSpecific(gl_hooks_t const * value)72 static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
73     pthread_setspecific(gGLTraceKey, value);
74 }
75 
getGLTraceThreadSpecific()76 gl_hooks_t const* getGLTraceThreadSpecific() {
77     return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
78 }
79 
initEglTraceLevel()80 void initEglTraceLevel() {
81     char value[PROPERTY_VALUE_MAX];
82     property_get("debug.egl.trace", value, "0");
83     int propertyLevel = atoi(value);
84     int applicationLevel = sEGLApplicationTraceLevel;
85     sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
86 
87     property_get("debug.egl.debug_proc", value, "");
88     long pid = getpid();
89     char procPath[128] = {};
90     sprintf(procPath, "/proc/%ld/cmdline", pid);
91     FILE * file = fopen(procPath, "r");
92     if (file)
93     {
94         char cmdline[256] = {};
95         if (fgets(cmdline, sizeof(cmdline) - 1, file))
96         {
97             if (!strcmp(value, cmdline))
98                 gEGLDebugLevel = 1;
99         }
100         fclose(file);
101     }
102 
103     if (gEGLDebugLevel > 0)
104     {
105         property_get("debug.egl.debug_port", value, "5039");
106         const unsigned short port = (unsigned short)atoi(value);
107         property_get("debug.egl.debug_forceUseFile", value, "0");
108         const bool forceUseFile = (bool)atoi(value);
109         property_get("debug.egl.debug_maxFileSize", value, "8");
110         const unsigned int maxFileSize = atoi(value) << 20;
111         property_get("debug.egl.debug_filePath", value, "/data/local/tmp/dump.gles2dbg");
112         StartDebugServer(port, forceUseFile, maxFileSize, value);
113     }
114 }
115 
setGLHooksThreadSpecific(gl_hooks_t const * value)116 void setGLHooksThreadSpecific(gl_hooks_t const *value) {
117     if (sEGLTraceLevel > 0) {
118         setGlTraceThreadSpecific(value);
119         setGlThreadSpecific(&gHooksTrace);
120     } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) {
121         setGlTraceThreadSpecific(value);
122         setGlThreadSpecific(&gHooksDebug);
123     } else {
124         setGlThreadSpecific(value);
125     }
126 }
127 
128 /*
129  * Global entry point to allow applications to modify their own trace level.
130  * The effective trace level is the max of this level and the value of debug.egl.trace.
131  */
132 extern "C"
setGLTraceLevel(int level)133 void setGLTraceLevel(int level) {
134     sEGLApplicationTraceLevel = level;
135 }
136 
137 #else
138 
setGLHooksThreadSpecific(gl_hooks_t const * value)139 void setGLHooksThreadSpecific(gl_hooks_t const *value) {
140     setGlThreadSpecific(value);
141 }
142 
143 #endif
144 
145 /*****************************************************************************/
146 
gl_no_context()147 static int gl_no_context() {
148     if (egl_tls_t::logNoContextCall()) {
149         LOGE("call to OpenGL ES API with no current context "
150              "(logged once per thread)");
151         char value[PROPERTY_VALUE_MAX];
152         property_get("debug.egl.callstack", value, "0");
153         if (atoi(value)) {
154             CallStack stack;
155             stack.update();
156             stack.dump();
157         }
158     }
159     return 0;
160 }
161 
early_egl_init(void)162 static void early_egl_init(void)
163 {
164 #if !USE_FAST_TLS_KEY
165     pthread_key_create(&gGLWrapperKey, NULL);
166 #endif
167 #if EGL_TRACE
168     pthread_key_create(&gGLTraceKey, NULL);
169     initEglTraceLevel();
170 #endif
171     uint32_t addr = (uint32_t)((void*)gl_no_context);
172     android_memset32(
173             (uint32_t*)(void*)&gHooksNoContext,
174             addr,
175             sizeof(gHooksNoContext));
176 
177     setGLHooksThreadSpecific(&gHooksNoContext);
178 }
179 
180 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
181 static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
182 
183 // ----------------------------------------------------------------------------
184 
validate_display(EGLDisplay dpy)185 egl_display_t* validate_display(EGLDisplay dpy) {
186     egl_display_t * const dp = get_display(dpy);
187     if (!dp)
188         return setError(EGL_BAD_DISPLAY, (egl_display_t*)NULL);
189     if (!dp->isReady())
190         return setError(EGL_NOT_INITIALIZED, (egl_display_t*)NULL);
191 
192     return dp;
193 }
194 
validate_display_config(EGLDisplay dpy,EGLConfig config,egl_display_t const * & dp)195 egl_connection_t* validate_display_config(EGLDisplay dpy, EGLConfig config,
196         egl_display_t const*& dp) {
197     dp = validate_display(dpy);
198     if (!dp)
199         return (egl_connection_t*) NULL;
200 
201     if (intptr_t(config) >= dp->numTotalConfigs) {
202         return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
203     }
204     egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
205     if (cnx->dso == 0) {
206         return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
207     }
208     return cnx;
209 }
210 
211 // ----------------------------------------------------------------------------
212 
egl_get_image_for_current_context(EGLImageKHR image)213 EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
214 {
215     ImageRef _i(image);
216     if (!_i.get())
217         return EGL_NO_IMAGE_KHR;
218 
219     EGLContext context = egl_tls_t::getContext();
220     if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
221         return EGL_NO_IMAGE_KHR;
222 
223     egl_context_t const * const c = get_context(context);
224     if (c == NULL) // this should never happen
225         return EGL_NO_IMAGE_KHR;
226 
227     // here we don't validate the context because if it's been marked for
228     // termination, this call should still succeed since it's internal to
229     // EGL.
230 
231     egl_image_t const * const i = get_image(image);
232     return i->images[c->impl];
233 }
234 
235 // ----------------------------------------------------------------------------
236 
237 // this mutex protects:
238 //    d->disp[]
239 //    egl_init_drivers_locked()
240 //
egl_init_drivers_locked()241 static EGLBoolean egl_init_drivers_locked() {
242     if (sEarlyInitState) {
243         // initialized by static ctor. should be set here.
244         return EGL_FALSE;
245     }
246 
247     // get our driver loader
248     Loader& loader(Loader::getInstance());
249 
250     // dynamically load all our EGL implementations
251     egl_connection_t* cnx;
252 
253     cnx = &gEGLImpl[IMPL_SOFTWARE];
254     if (cnx->dso == 0) {
255         cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
256         cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
257         cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
258     }
259 
260     cnx = &gEGLImpl[IMPL_HARDWARE];
261     if (cnx->dso == 0) {
262         char value[PROPERTY_VALUE_MAX];
263         property_get("debug.egl.hw", value, "1");
264         if (atoi(value) != 0) {
265             cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
266             cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
267             cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
268         } else {
269             LOGD("3D hardware acceleration is disabled");
270         }
271     }
272 
273     if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
274         return EGL_FALSE;
275     }
276 
277     return EGL_TRUE;
278 }
279 
280 static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
281 
egl_init_drivers()282 EGLBoolean egl_init_drivers() {
283     EGLBoolean res;
284     pthread_mutex_lock(&sInitDriverMutex);
285     res = egl_init_drivers_locked();
286     pthread_mutex_unlock(&sInitDriverMutex);
287     return res;
288 }
289 
gl_unimplemented()290 void gl_unimplemented() {
291     LOGE("called unimplemented OpenGL ES API");
292 }
293 
294 // ----------------------------------------------------------------------------
295 
296 #if USE_FAST_TLS_KEY
297 
298 // We have a dedicated TLS slot in bionic
get_tls_hooks()299 static inline gl_hooks_t const * volatile * get_tls_hooks() {
300     volatile void *tls_base = __get_tls();
301     gl_hooks_t const * volatile * tls_hooks =
302             reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
303     return tls_hooks;
304 }
305 
setGlThreadSpecific(gl_hooks_t const * value)306 void setGlThreadSpecific(gl_hooks_t const *value) {
307     gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
308     tls_hooks[TLS_SLOT_OPENGL_API] = value;
309 }
310 
getGlThreadSpecific()311 gl_hooks_t const* getGlThreadSpecific() {
312     gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
313     gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
314     if (hooks) return hooks;
315     return &gHooksNoContext;
316 }
317 
318 #else
319 
setGlThreadSpecific(gl_hooks_t const * value)320 void setGlThreadSpecific(gl_hooks_t const *value) {
321     pthread_setspecific(gGLWrapperKey, value);
322 }
323 
getGlThreadSpecific()324 gl_hooks_t const* getGlThreadSpecific() {
325     gl_hooks_t const* hooks =  static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
326     if (hooks) return hooks;
327     return &gHooksNoContext;
328 }
329 
330 #endif
331 
332 // ----------------------------------------------------------------------------
333 // GL / EGL hooks
334 // ----------------------------------------------------------------------------
335 
336 #undef GL_ENTRY
337 #undef EGL_ENTRY
338 #define GL_ENTRY(_r, _api, ...) #_api,
339 #define EGL_ENTRY(_r, _api, ...) #_api,
340 
341 char const * const gl_names[] = {
342     #include "entries.in"
343     NULL
344 };
345 
346 char const * const egl_names[] = {
347     #include "egl_entries.in"
348     NULL
349 };
350 
351 #undef GL_ENTRY
352 #undef EGL_ENTRY
353 
354 
355 // ----------------------------------------------------------------------------
356 }; // namespace android
357 // ----------------------------------------------------------------------------
358 
359