blob: da1b39772b989cd98e0c1a1dba45efba4a4a3042 [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, "");
Siva Velusamy1e81e712011-12-14 12:19:56 -080087 if (strlen(value) == 0)
88 return;
89
David Li28ca2ab2011-03-01 16:08:10 -080090 long pid = getpid();
91 char procPath[128] = {};
92 sprintf(procPath, "/proc/%ld/cmdline", pid);
93 FILE * file = fopen(procPath, "r");
Siva Velusamydb974682011-11-30 15:05:37 -080094 if (file) {
David Li28ca2ab2011-03-01 16:08:10 -080095 char cmdline[256] = {};
Siva Velusamydb974682011-11-30 15:05:37 -080096 if (fgets(cmdline, sizeof(cmdline) - 1, file)) {
Siva Velusamy1e81e712011-12-14 12:19:56 -080097 if (!strncmp(value, cmdline, strlen(value))) {
98 // set EGL debug if the "debug.egl.debug_proc" property
99 // matches the prefix of this application's command line
Mathias Agopian3b583222011-09-01 14:55:00 -0700100 gEGLDebugLevel = 1;
Siva Velusamy1e81e712011-12-14 12:19:56 -0800101 }
David Li27f130a2011-04-08 18:43:16 -0700102 }
David Li28ca2ab2011-03-01 16:08:10 -0800103 fclose(file);
104 }
David Li27f130a2011-04-08 18:43:16 -0700105
Siva Velusamydb974682011-11-30 15:05:37 -0800106 if (gEGLDebugLevel > 0) {
107 GLTrace_start();
David Li940c3f82011-03-10 19:07:42 -0800108 }
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700109}
110
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700111void setGLHooksThreadSpecific(gl_hooks_t const *value) {
112 if (sEGLTraceLevel > 0) {
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700113 setGlTraceThreadSpecific(value);
114 setGlThreadSpecific(&gHooksTrace);
Mathias Agopian3b583222011-09-01 14:55:00 -0700115 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) {
David Li28ca2ab2011-03-01 16:08:10 -0800116 setGlTraceThreadSpecific(value);
Siva Velusamydb974682011-11-30 15:05:37 -0800117 setGlThreadSpecific(GLTrace_getGLHooks());
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700118 } else {
119 setGlThreadSpecific(value);
120 }
121}
122
123/*
124 * Global entry point to allow applications to modify their own trace level.
125 * The effective trace level is the max of this level and the value of debug.egl.trace.
126 */
127extern "C"
128void setGLTraceLevel(int level) {
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700129 sEGLApplicationTraceLevel = level;
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700130}
131
132#else
133
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700134void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700135 setGlThreadSpecific(value);
136}
137
138#endif
139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140/*****************************************************************************/
141
Mathias Agopian25b388c2010-09-23 16:38:38 -0700142static int gl_no_context() {
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700143 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian997d1072009-07-31 16:21:17 -0700144 LOGE("call to OpenGL ES API with no current context "
145 "(logged once per thread)");
Mathias Agopian8ec2ff62011-09-06 17:24:05 -0700146 char value[PROPERTY_VALUE_MAX];
147 property_get("debug.egl.callstack", value, "0");
148 if (atoi(value)) {
149 CallStack stack;
150 stack.update();
151 stack.dump();
152 }
Mathias Agopian997d1072009-07-31 16:21:17 -0700153 }
Mathias Agopian25b388c2010-09-23 16:38:38 -0700154 return 0;
Mathias Agopian5c6c5c72010-09-23 11:32:52 -0700155}
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157static void early_egl_init(void)
158{
159#if !USE_FAST_TLS_KEY
160 pthread_key_create(&gGLWrapperKey, NULL);
161#endif
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700162#if EGL_TRACE
163 pthread_key_create(&gGLTraceKey, NULL);
164 initEglTraceLevel();
165#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 uint32_t addr = (uint32_t)((void*)gl_no_context);
167 android_memset32(
Mathias Agopian6fc56992009-10-14 02:06:37 -0700168 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 addr,
Mathias Agopian6fc56992009-10-14 02:06:37 -0700170 sizeof(gHooksNoContext));
Mathias Agopian5c6c5c72010-09-23 11:32:52 -0700171
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700172 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173}
174
175static pthread_once_t once_control = PTHREAD_ONCE_INIT;
176static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
177
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700178// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700180egl_display_t* validate_display(EGLDisplay dpy) {
Eric Hassold2a790ac2011-03-23 15:59:00 -0700181 egl_display_t * const dp = get_display(dpy);
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700182 if (!dp)
183 return setError(EGL_BAD_DISPLAY, (egl_display_t*)NULL);
184 if (!dp->isReady())
185 return setError(EGL_NOT_INITIALIZED, (egl_display_t*)NULL);
Eric Hassold2a790ac2011-03-23 15:59:00 -0700186
187 return dp;
188}
189
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700190egl_connection_t* validate_display_config(EGLDisplay dpy, EGLConfig config,
191 egl_display_t const*& dp) {
Eric Hassold2a790ac2011-03-23 15:59:00 -0700192 dp = validate_display(dpy);
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700193 if (!dp)
194 return (egl_connection_t*) NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700196 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
198 }
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700199 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 if (cnx->dso == 0) {
201 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
202 }
203 return cnx;
204}
205
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700206// ----------------------------------------------------------------------------
207
Mathias Agopian1473f462009-04-10 14:24:30 -0700208EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
209{
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700210 EGLContext context = egl_tls_t::getContext();
Mathias Agopian1473f462009-04-10 14:24:30 -0700211 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
212 return EGL_NO_IMAGE_KHR;
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700213
Mathias Agopian1473f462009-04-10 14:24:30 -0700214 egl_context_t const * const c = get_context(context);
Mathias Agopian274e03c2011-11-13 20:50:07 -0800215 if (c == NULL) // this should never happen, by construction
216 return EGL_NO_IMAGE_KHR;
217
218 egl_display_t* display = egl_display_t::get(c->dpy);
219 if (display == NULL) // this should never happen, by construction
220 return EGL_NO_IMAGE_KHR;
221
222 ImageRef _i(display, image);
223 if (!_i.get())
Mathias Agopian1473f462009-04-10 14:24:30 -0700224 return EGL_NO_IMAGE_KHR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700226 // here we don't validate the context because if it's been marked for
227 // termination, this call should still succeed since it's internal to
228 // EGL.
229
Mathias Agopian1473f462009-04-10 14:24:30 -0700230 egl_image_t const * const i = get_image(image);
Mathias Agopian1473f462009-04-10 14:24:30 -0700231 return i->images[c->impl];
232}
233
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700234// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -0700235
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700236// this mutex protects:
Mathias Agopian94263d72009-08-24 21:47:13 -0700237// d->disp[]
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700238// egl_init_drivers_locked()
239//
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700240static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 if (sEarlyInitState) {
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700242 // initialized by static ctor. should be set here.
243 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
245
Mathias Agopian9d17c052009-05-28 17:39:03 -0700246 // get our driver loader
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700247 Loader& loader(Loader::getInstance());
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700248
249 // dynamically load all our EGL implementations
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700250 egl_connection_t* cnx;
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700251
252 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 if (cnx->dso == 0) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700254 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
255 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
256 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
258
259 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700260 if (cnx->dso == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 char value[PROPERTY_VALUE_MAX];
262 property_get("debug.egl.hw", value, "1");
263 if (atoi(value) != 0) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700264 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
265 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
266 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 } else {
Steve Block5baa3a62011-12-20 16:23:08 +0000268 ALOGD("3D hardware acceleration is disabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 }
270 }
Mathias Agopian94263d72009-08-24 21:47:13 -0700271
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700272 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
273 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700276 return EGL_TRUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277}
278
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700279static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
280
281EGLBoolean egl_init_drivers() {
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700282 EGLBoolean res;
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700283 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700284 res = egl_init_drivers_locked();
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700285 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700286 return res;
287}
Mathias Agopian1473f462009-04-10 14:24:30 -0700288
Mathias Agopianf56a9602011-05-23 17:26:14 -0700289void gl_unimplemented() {
290 LOGE("called unimplemented OpenGL ES API");
291}
292
293// ----------------------------------------------------------------------------
294
295#if USE_FAST_TLS_KEY
296
297// We have a dedicated TLS slot in bionic
298static inline gl_hooks_t const * volatile * get_tls_hooks() {
299 volatile void *tls_base = __get_tls();
300 gl_hooks_t const * volatile * tls_hooks =
301 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
302 return tls_hooks;
303}
304
305void setGlThreadSpecific(gl_hooks_t const *value) {
306 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
307 tls_hooks[TLS_SLOT_OPENGL_API] = value;
308}
309
310gl_hooks_t const* getGlThreadSpecific() {
311 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
312 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
313 if (hooks) return hooks;
314 return &gHooksNoContext;
315}
316
317#else
318
319void setGlThreadSpecific(gl_hooks_t const *value) {
320 pthread_setspecific(gGLWrapperKey, value);
321}
322
323gl_hooks_t const* getGlThreadSpecific() {
324 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
325 if (hooks) return hooks;
326 return &gHooksNoContext;
327}
328
329#endif
330
331// ----------------------------------------------------------------------------
332// GL / EGL hooks
333// ----------------------------------------------------------------------------
334
335#undef GL_ENTRY
336#undef EGL_ENTRY
337#define GL_ENTRY(_r, _api, ...) #_api,
338#define EGL_ENTRY(_r, _api, ...) #_api,
339
340char const * const gl_names[] = {
341 #include "entries.in"
342 NULL
343};
344
345char const * const egl_names[] = {
346 #include "egl_entries.in"
347 NULL
348};
349
350#undef GL_ENTRY
351#undef EGL_ENTRY
352
353
Mathias Agopian1473f462009-04-10 14:24:30 -0700354// ----------------------------------------------------------------------------
355}; // namespace android
356// ----------------------------------------------------------------------------
357