blob: 14745b36d7cd8c789b01da934a8ba9bb69dad250 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080017#include <ctype.h>
Mathias Agopian11be99d2009-05-17 18:50:16 -070018#include <stdlib.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <string.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Mathias Agopian7adf4ef2011-05-13 16:21:08 -070021#include <hardware/gralloc.h>
22#include <system/window.h>
The Android Open Source Project9066cfe2009-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 Guy03985752011-07-11 15:33:51 -070034#include <utils/CallStack.h>
Mathias Agopian3944eab2010-08-02 17:34:32 -070035#include <utils/String8.h>
Mathias Agopian4a34e882009-08-21 02:18:25 -070036
Mathias Agopianf56a9602011-05-23 17:26:14 -070037#include "egldefs.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include "egl_impl.h"
David Lice30eb82011-03-28 10:39:28 -070039#include "egl_tls.h"
Siva Velusamydb974682011-11-30 15:05:37 -080040#include "glestrace.h"
Mathias Agopian7adf4ef2011-05-13 16:21:08 -070041#include "hooks.h"
42#include "Loader.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Mathias Agopian7adf4ef2011-05-13 16:21:08 -070044#include "egl_display.h"
45#include "egl_object.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47// ----------------------------------------------------------------------------
48namespace android {
49// ----------------------------------------------------------------------------
50
Mathias Agopian7adf4ef2011-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 Agopian1473f462009-04-10 14:24:30 -070055
56// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Jack Palevichd4d0fb92010-10-26 15:21:24 -070058#if EGL_TRACE
59
60EGLAPI pthread_key_t gGLTraceKey = -1;
61
62// ----------------------------------------------------------------------------
63
Mathias Agopian7adf4ef2011-05-13 16:21:08 -070064int gEGLDebugLevel;
65
66static int sEGLTraceLevel;
67static int sEGLApplicationTraceLevel;
68
69extern gl_hooks_t gHooksTrace;
Jack Palevichd4d0fb92010-10-26 15:21:24 -070070
71static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
72 pthread_setspecific(gGLTraceKey, value);
73}
74
75gl_hooks_t const* getGLTraceThreadSpecific() {
76 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
77}
78
Mathias Agopian7adf4ef2011-05-13 16:21:08 -070079void initEglTraceLevel() {
Jack Palevichd4d0fb92010-10-26 15:21:24 -070080 char value[PROPERTY_VALUE_MAX];
81 property_get("debug.egl.trace", value, "0");
82 int propertyLevel = atoi(value);
Mathias Agopian7adf4ef2011-05-13 16:21:08 -070083 int applicationLevel = sEGLApplicationTraceLevel;
84 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li27f130a2011-04-08 18:43:16 -070085
David Li28ca2ab2011-03-01 16:08:10 -080086 property_get("debug.egl.debug_proc", value, "");
87 long pid = getpid();
88 char procPath[128] = {};
89 sprintf(procPath, "/proc/%ld/cmdline", pid);
90 FILE * file = fopen(procPath, "r");
Siva Velusamydb974682011-11-30 15:05:37 -080091 if (file) {
David Li28ca2ab2011-03-01 16:08:10 -080092 char cmdline[256] = {};
Siva Velusamydb974682011-11-30 15:05:37 -080093 if (fgets(cmdline, sizeof(cmdline) - 1, file)) {
David Li28ca2ab2011-03-01 16:08:10 -080094 if (!strcmp(value, cmdline))
Mathias Agopian3b583222011-09-01 14:55:00 -070095 gEGLDebugLevel = 1;
David Li27f130a2011-04-08 18:43:16 -070096 }
David Li28ca2ab2011-03-01 16:08:10 -080097 fclose(file);
98 }
David Li27f130a2011-04-08 18:43:16 -070099
Siva Velusamydb974682011-11-30 15:05:37 -0800100 if (gEGLDebugLevel > 0) {
101 GLTrace_start();
David Li940c3f82011-03-10 19:07:42 -0800102 }
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700103}
104
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700105void setGLHooksThreadSpecific(gl_hooks_t const *value) {
106 if (sEGLTraceLevel > 0) {
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700107 setGlTraceThreadSpecific(value);
108 setGlThreadSpecific(&gHooksTrace);
Mathias Agopian3b583222011-09-01 14:55:00 -0700109 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) {
David Li28ca2ab2011-03-01 16:08:10 -0800110 setGlTraceThreadSpecific(value);
Siva Velusamydb974682011-11-30 15:05:37 -0800111 setGlThreadSpecific(GLTrace_getGLHooks());
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700112 } else {
113 setGlThreadSpecific(value);
114 }
115}
116
117/*
118 * Global entry point to allow applications to modify their own trace level.
119 * The effective trace level is the max of this level and the value of debug.egl.trace.
120 */
121extern "C"
122void setGLTraceLevel(int level) {
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700123 sEGLApplicationTraceLevel = level;
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700124}
125
126#else
127
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700128void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700129 setGlThreadSpecific(value);
130}
131
132#endif
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134/*****************************************************************************/
135
Mathias Agopian25b388c2010-09-23 16:38:38 -0700136static int gl_no_context() {
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700137 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian997d1072009-07-31 16:21:17 -0700138 LOGE("call to OpenGL ES API with no current context "
139 "(logged once per thread)");
Mathias Agopian8ec2ff62011-09-06 17:24:05 -0700140 char value[PROPERTY_VALUE_MAX];
141 property_get("debug.egl.callstack", value, "0");
142 if (atoi(value)) {
143 CallStack stack;
144 stack.update();
145 stack.dump();
146 }
Mathias Agopian997d1072009-07-31 16:21:17 -0700147 }
Mathias Agopian25b388c2010-09-23 16:38:38 -0700148 return 0;
Mathias Agopian5c6c5c72010-09-23 11:32:52 -0700149}
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151static void early_egl_init(void)
152{
153#if !USE_FAST_TLS_KEY
154 pthread_key_create(&gGLWrapperKey, NULL);
155#endif
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700156#if EGL_TRACE
157 pthread_key_create(&gGLTraceKey, NULL);
158 initEglTraceLevel();
159#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 uint32_t addr = (uint32_t)((void*)gl_no_context);
161 android_memset32(
Mathias Agopian6fc56992009-10-14 02:06:37 -0700162 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 addr,
Mathias Agopian6fc56992009-10-14 02:06:37 -0700164 sizeof(gHooksNoContext));
Mathias Agopian5c6c5c72010-09-23 11:32:52 -0700165
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700166 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167}
168
169static pthread_once_t once_control = PTHREAD_ONCE_INIT;
170static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
171
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700172// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700174egl_display_t* validate_display(EGLDisplay dpy) {
Eric Hassold2a790ac2011-03-23 15:59:00 -0700175 egl_display_t * const dp = get_display(dpy);
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700176 if (!dp)
177 return setError(EGL_BAD_DISPLAY, (egl_display_t*)NULL);
178 if (!dp->isReady())
179 return setError(EGL_NOT_INITIALIZED, (egl_display_t*)NULL);
Eric Hassold2a790ac2011-03-23 15:59:00 -0700180
181 return dp;
182}
183
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700184egl_connection_t* validate_display_config(EGLDisplay dpy, EGLConfig config,
185 egl_display_t const*& dp) {
Eric Hassold2a790ac2011-03-23 15:59:00 -0700186 dp = validate_display(dpy);
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700187 if (!dp)
188 return (egl_connection_t*) NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700190 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
192 }
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700193 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 if (cnx->dso == 0) {
195 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
196 }
197 return cnx;
198}
199
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700200// ----------------------------------------------------------------------------
201
Mathias Agopian1473f462009-04-10 14:24:30 -0700202EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
203{
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700204 EGLContext context = egl_tls_t::getContext();
Mathias Agopian1473f462009-04-10 14:24:30 -0700205 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
206 return EGL_NO_IMAGE_KHR;
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700207
Mathias Agopian1473f462009-04-10 14:24:30 -0700208 egl_context_t const * const c = get_context(context);
Mathias Agopian274e03c2011-11-13 20:50:07 -0800209 if (c == NULL) // this should never happen, by construction
210 return EGL_NO_IMAGE_KHR;
211
212 egl_display_t* display = egl_display_t::get(c->dpy);
213 if (display == NULL) // this should never happen, by construction
214 return EGL_NO_IMAGE_KHR;
215
216 ImageRef _i(display, image);
217 if (!_i.get())
Mathias Agopian1473f462009-04-10 14:24:30 -0700218 return EGL_NO_IMAGE_KHR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700220 // here we don't validate the context because if it's been marked for
221 // termination, this call should still succeed since it's internal to
222 // EGL.
223
Mathias Agopian1473f462009-04-10 14:24:30 -0700224 egl_image_t const * const i = get_image(image);
Mathias Agopian1473f462009-04-10 14:24:30 -0700225 return i->images[c->impl];
226}
227
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700228// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -0700229
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700230// this mutex protects:
Mathias Agopian94263d72009-08-24 21:47:13 -0700231// d->disp[]
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700232// egl_init_drivers_locked()
233//
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700234static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 if (sEarlyInitState) {
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700236 // initialized by static ctor. should be set here.
237 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
239
Mathias Agopian9d17c052009-05-28 17:39:03 -0700240 // get our driver loader
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700241 Loader& loader(Loader::getInstance());
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700242
243 // dynamically load all our EGL implementations
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700244 egl_connection_t* cnx;
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700245
246 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 if (cnx->dso == 0) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700248 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
249 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
250 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 }
252
253 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700254 if (cnx->dso == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 char value[PROPERTY_VALUE_MAX];
256 property_get("debug.egl.hw", value, "1");
257 if (atoi(value) != 0) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700258 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
259 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
260 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 } else {
262 LOGD("3D hardware acceleration is disabled");
263 }
264 }
Mathias Agopian94263d72009-08-24 21:47:13 -0700265
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700266 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
267 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 }
269
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700270 return EGL_TRUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271}
272
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700273static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
274
275EGLBoolean egl_init_drivers() {
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700276 EGLBoolean res;
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700277 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700278 res = egl_init_drivers_locked();
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700279 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700280 return res;
281}
Mathias Agopian1473f462009-04-10 14:24:30 -0700282
Mathias Agopianf56a9602011-05-23 17:26:14 -0700283void gl_unimplemented() {
284 LOGE("called unimplemented OpenGL ES API");
285}
286
287// ----------------------------------------------------------------------------
288
289#if USE_FAST_TLS_KEY
290
291// We have a dedicated TLS slot in bionic
292static inline gl_hooks_t const * volatile * get_tls_hooks() {
293 volatile void *tls_base = __get_tls();
294 gl_hooks_t const * volatile * tls_hooks =
295 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
296 return tls_hooks;
297}
298
299void setGlThreadSpecific(gl_hooks_t const *value) {
300 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
301 tls_hooks[TLS_SLOT_OPENGL_API] = value;
302}
303
304gl_hooks_t const* getGlThreadSpecific() {
305 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
306 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
307 if (hooks) return hooks;
308 return &gHooksNoContext;
309}
310
311#else
312
313void setGlThreadSpecific(gl_hooks_t const *value) {
314 pthread_setspecific(gGLWrapperKey, value);
315}
316
317gl_hooks_t const* getGlThreadSpecific() {
318 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
319 if (hooks) return hooks;
320 return &gHooksNoContext;
321}
322
323#endif
324
325// ----------------------------------------------------------------------------
326// GL / EGL hooks
327// ----------------------------------------------------------------------------
328
329#undef GL_ENTRY
330#undef EGL_ENTRY
331#define GL_ENTRY(_r, _api, ...) #_api,
332#define EGL_ENTRY(_r, _api, ...) #_api,
333
334char const * const gl_names[] = {
335 #include "entries.in"
336 NULL
337};
338
339char const * const egl_names[] = {
340 #include "egl_entries.in"
341 NULL
342};
343
344#undef GL_ENTRY
345#undef EGL_ENTRY
346
347
Mathias Agopian1473f462009-04-10 14:24:30 -0700348// ----------------------------------------------------------------------------
349}; // namespace android
350// ----------------------------------------------------------------------------
351