blob: ddad2d323fcedfe028f910955f87ec0e57616bef [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"
Mathias Agopian518ec112011-05-13 16:21:08 -070040#include "glesv2dbg.h"
41#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 Agopian518ec112011-05-13 16:21:08 -070051egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
52gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
53gl_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
Mathias Agopian518ec112011-05-13 16:21:08 -070064int gEGLDebugLevel;
65
66static int sEGLTraceLevel;
67static int sEGLApplicationTraceLevel;
68
69extern gl_hooks_t gHooksTrace;
70extern gl_hooks_t gHooksDebug;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070071
72static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
73 pthread_setspecific(gGLTraceKey, value);
74}
75
76gl_hooks_t const* getGLTraceThreadSpecific() {
77 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
78}
79
Mathias Agopian518ec112011-05-13 16:21:08 -070080void initEglTraceLevel() {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070081 char value[PROPERTY_VALUE_MAX];
82 property_get("debug.egl.trace", value, "0");
83 int propertyLevel = atoi(value);
Mathias Agopian518ec112011-05-13 16:21:08 -070084 int applicationLevel = sEGLApplicationTraceLevel;
85 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li499c6f02011-04-08 18:43:16 -070086
David Li2f5a6552011-03-01 16:08:10 -080087 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 {
David Li2f5a6552011-03-01 16:08:10 -080097 if (!strcmp(value, cmdline))
Mathias Agopian518ec112011-05-13 16:21:08 -070098 sEGLTraceLevel = 1;
David Li499c6f02011-04-08 18:43:16 -070099 }
David Li2f5a6552011-03-01 16:08:10 -0800100 fclose(file);
101 }
David Li499c6f02011-04-08 18:43:16 -0700102
Mathias Agopian518ec112011-05-13 16:21:08 -0700103 if (sEGLTraceLevel > 0)
David Li85f33a72011-03-10 19:07:42 -0800104 {
105 property_get("debug.egl.debug_port", value, "5039");
David Li499c6f02011-04-08 18:43:16 -0700106 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);
David Li85f33a72011-03-10 19:07:42 -0800113 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700114}
115
Mathias Agopian518ec112011-05-13 16:21:08 -0700116void setGLHooksThreadSpecific(gl_hooks_t const *value) {
117 if (sEGLTraceLevel > 0) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700118 setGlTraceThreadSpecific(value);
119 setGlThreadSpecific(&gHooksTrace);
Mathias Agopian518ec112011-05-13 16:21:08 -0700120 } else if (sEGLTraceLevel > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800121 setGlTraceThreadSpecific(value);
122 setGlThreadSpecific(&gHooksDebug);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700123 } 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 */
132extern "C"
133void setGLTraceLevel(int level) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700134 sEGLApplicationTraceLevel = level;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700135}
136
137#else
138
Mathias Agopian518ec112011-05-13 16:21:08 -0700139void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700140 setGlThreadSpecific(value);
141}
142
143#endif
144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145/*****************************************************************************/
146
Mathias Agopian6f087122010-09-23 16:38:38 -0700147static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -0700148 if (egl_tls_t::logNoContextCall()) {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700149 LOGE("call to OpenGL ES API with no current context "
150 "(logged once per thread)");
Romain Guye03de932011-07-11 15:33:51 -0700151 LOGE("call stack before error:");
152 CallStack stack;
153 stack.update();
154 stack.dump();
Mathias Agopiand274eae2009-07-31 16:21:17 -0700155 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700156 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700157}
158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159static void early_egl_init(void)
160{
161#if !USE_FAST_TLS_KEY
162 pthread_key_create(&gGLWrapperKey, NULL);
163#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700164#if EGL_TRACE
165 pthread_key_create(&gGLTraceKey, NULL);
166 initEglTraceLevel();
167#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 uint32_t addr = (uint32_t)((void*)gl_no_context);
169 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700170 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700172 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700173
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700174 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175}
176
177static pthread_once_t once_control = PTHREAD_ONCE_INIT;
178static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
179
Mathias Agopian518ec112011-05-13 16:21:08 -0700180// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181
Mathias Agopian5b287a62011-05-16 18:58:55 -0700182egl_display_t* validate_display(EGLDisplay dpy) {
Eric Hassold3ede7c12011-03-23 15:59:00 -0700183 egl_display_t * const dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700184 if (!dp)
185 return setError(EGL_BAD_DISPLAY, (egl_display_t*)NULL);
186 if (!dp->isReady())
187 return setError(EGL_NOT_INITIALIZED, (egl_display_t*)NULL);
Eric Hassold3ede7c12011-03-23 15:59:00 -0700188
189 return dp;
190}
191
Mathias Agopian5b287a62011-05-16 18:58:55 -0700192egl_connection_t* validate_display_config(EGLDisplay dpy, EGLConfig config,
193 egl_display_t const*& dp) {
Eric Hassold3ede7c12011-03-23 15:59:00 -0700194 dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700195 if (!dp)
196 return (egl_connection_t*) NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197
Mathias Agopiancee79392010-07-26 21:14:59 -0700198 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
200 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700201 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 if (cnx->dso == 0) {
203 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
204 }
205 return cnx;
206}
207
Mathias Agopian518ec112011-05-13 16:21:08 -0700208// ----------------------------------------------------------------------------
209
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700210EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
211{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700212 ImageRef _i(image);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700213 if (!_i.get())
214 return EGL_NO_IMAGE_KHR;
215
Mathias Agopian518ec112011-05-13 16:21:08 -0700216 EGLContext context = egl_tls_t::getContext();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
218 return EGL_NO_IMAGE_KHR;
Mathias Agopian5b287a62011-05-16 18:58:55 -0700219
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 egl_context_t const * const c = get_context(context);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700221 if (c == NULL) // this should never happen
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700222 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223
Mathias Agopian5b287a62011-05-16 18:58:55 -0700224 // here we don't validate the context because if it's been marked for
225 // termination, this call should still succeed since it's internal to
226 // EGL.
227
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700228 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229 return i->images[c->impl];
230}
231
Mathias Agopian923c6612009-08-17 18:07:06 -0700232// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700233
Mathias Agopian923c6612009-08-17 18:07:06 -0700234// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700235// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700236// egl_init_drivers_locked()
237//
Mathias Agopian518ec112011-05-13 16:21:08 -0700238static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700240 // initialized by static ctor. should be set here.
241 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 }
243
Mathias Agopiande586972009-05-28 17:39:03 -0700244 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700245 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700246
247 // dynamically load all our EGL implementations
Mathias Agopian923c6612009-08-17 18:07:06 -0700248 egl_connection_t* cnx;
Mathias Agopian923c6612009-08-17 18:07:06 -0700249
250 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700252 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
253 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
254 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 }
256
257 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700258 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 char value[PROPERTY_VALUE_MAX];
260 property_get("debug.egl.hw", value, "1");
261 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700262 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
263 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
264 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 } else {
266 LOGD("3D hardware acceleration is disabled");
267 }
268 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700269
Mathias Agopian923c6612009-08-17 18:07:06 -0700270 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
271 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 }
273
Mathias Agopian923c6612009-08-17 18:07:06 -0700274 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275}
276
Mathias Agopian518ec112011-05-13 16:21:08 -0700277static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
278
279EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700280 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700281 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700282 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700283 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700284 return res;
285}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286
Mathias Agopian1cadb252011-05-23 17:26:14 -0700287void gl_unimplemented() {
288 LOGE("called unimplemented OpenGL ES API");
289}
290
291// ----------------------------------------------------------------------------
292
293#if USE_FAST_TLS_KEY
294
295// We have a dedicated TLS slot in bionic
296static inline gl_hooks_t const * volatile * get_tls_hooks() {
297 volatile void *tls_base = __get_tls();
298 gl_hooks_t const * volatile * tls_hooks =
299 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
300 return tls_hooks;
301}
302
303void setGlThreadSpecific(gl_hooks_t const *value) {
304 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
305 tls_hooks[TLS_SLOT_OPENGL_API] = value;
306}
307
308gl_hooks_t const* getGlThreadSpecific() {
309 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
310 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
311 if (hooks) return hooks;
312 return &gHooksNoContext;
313}
314
315#else
316
317void setGlThreadSpecific(gl_hooks_t const *value) {
318 pthread_setspecific(gGLWrapperKey, value);
319}
320
321gl_hooks_t const* getGlThreadSpecific() {
322 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
323 if (hooks) return hooks;
324 return &gHooksNoContext;
325}
326
327#endif
328
329// ----------------------------------------------------------------------------
330// GL / EGL hooks
331// ----------------------------------------------------------------------------
332
333#undef GL_ENTRY
334#undef EGL_ENTRY
335#define GL_ENTRY(_r, _api, ...) #_api,
336#define EGL_ENTRY(_r, _api, ...) #_api,
337
338char const * const gl_names[] = {
339 #include "entries.in"
340 NULL
341};
342
343char const * const egl_names[] = {
344 #include "egl_entries.in"
345 NULL
346};
347
348#undef GL_ENTRY
349#undef EGL_ENTRY
350
351
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700352// ----------------------------------------------------------------------------
353}; // namespace android
354// ----------------------------------------------------------------------------
355