blob: 1e4319510e66d86c5f7ae91989f21d2a30bcb452 [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"
Mathias Agopian7adf4ef2011-05-13 16:21:08 -070040#include "glesv2dbg.h"
41#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;
70extern gl_hooks_t gHooksDebug;
Jack Palevichd4d0fb92010-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 Agopian7adf4ef2011-05-13 16:21:08 -070080void initEglTraceLevel() {
Jack Palevichd4d0fb92010-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 Agopian7adf4ef2011-05-13 16:21:08 -070084 int applicationLevel = sEGLApplicationTraceLevel;
85 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li27f130a2011-04-08 18:43:16 -070086
David Li28ca2ab2011-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 Li28ca2ab2011-03-01 16:08:10 -080097 if (!strcmp(value, cmdline))
Mathias Agopian3b583222011-09-01 14:55:00 -070098 gEGLDebugLevel = 1;
David Li27f130a2011-04-08 18:43:16 -070099 }
David Li28ca2ab2011-03-01 16:08:10 -0800100 fclose(file);
101 }
David Li27f130a2011-04-08 18:43:16 -0700102
Mathias Agopian3b583222011-09-01 14:55:00 -0700103 if (gEGLDebugLevel > 0)
David Li940c3f82011-03-10 19:07:42 -0800104 {
105 property_get("debug.egl.debug_port", value, "5039");
David Li27f130a2011-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 Li940c3f82011-03-10 19:07:42 -0800113 }
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700114}
115
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700116void setGLHooksThreadSpecific(gl_hooks_t const *value) {
117 if (sEGLTraceLevel > 0) {
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700118 setGlTraceThreadSpecific(value);
119 setGlThreadSpecific(&gHooksTrace);
Mathias Agopian3b583222011-09-01 14:55:00 -0700120 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) {
David Li28ca2ab2011-03-01 16:08:10 -0800121 setGlTraceThreadSpecific(value);
122 setGlThreadSpecific(&gHooksDebug);
Jack Palevichd4d0fb92010-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 Agopian7adf4ef2011-05-13 16:21:08 -0700134 sEGLApplicationTraceLevel = level;
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700135}
136
137#else
138
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700139void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700140 setGlThreadSpecific(value);
141}
142
143#endif
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145/*****************************************************************************/
146
Mathias Agopian25b388c2010-09-23 16:38:38 -0700147static int gl_no_context() {
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700148 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian997d1072009-07-31 16:21:17 -0700149 LOGE("call to OpenGL ES API with no current context "
150 "(logged once per thread)");
Mathias Agopian8ec2ff62011-09-06 17:24:05 -0700151 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 }
Mathias Agopian997d1072009-07-31 16:21:17 -0700158 }
Mathias Agopian25b388c2010-09-23 16:38:38 -0700159 return 0;
Mathias Agopian5c6c5c72010-09-23 11:32:52 -0700160}
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162static void early_egl_init(void)
163{
164#if !USE_FAST_TLS_KEY
165 pthread_key_create(&gGLWrapperKey, NULL);
166#endif
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700167#if EGL_TRACE
168 pthread_key_create(&gGLTraceKey, NULL);
169 initEglTraceLevel();
170#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 uint32_t addr = (uint32_t)((void*)gl_no_context);
172 android_memset32(
Mathias Agopian6fc56992009-10-14 02:06:37 -0700173 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 addr,
Mathias Agopian6fc56992009-10-14 02:06:37 -0700175 sizeof(gHooksNoContext));
Mathias Agopian5c6c5c72010-09-23 11:32:52 -0700176
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700177 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178}
179
180static pthread_once_t once_control = PTHREAD_ONCE_INIT;
181static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
182
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700183// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700185egl_display_t* validate_display(EGLDisplay dpy) {
Eric Hassold2a790ac2011-03-23 15:59:00 -0700186 egl_display_t * const dp = get_display(dpy);
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700187 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);
Eric Hassold2a790ac2011-03-23 15:59:00 -0700191
192 return dp;
193}
194
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700195egl_connection_t* validate_display_config(EGLDisplay dpy, EGLConfig config,
196 egl_display_t const*& dp) {
Eric Hassold2a790ac2011-03-23 15:59:00 -0700197 dp = validate_display(dpy);
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700198 if (!dp)
199 return (egl_connection_t*) NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700201 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
203 }
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700204 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 if (cnx->dso == 0) {
206 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
207 }
208 return cnx;
209}
210
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700211// ----------------------------------------------------------------------------
212
Mathias Agopian1473f462009-04-10 14:24:30 -0700213EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
214{
Mathias Agopian4a34e882009-08-21 02:18:25 -0700215 ImageRef _i(image);
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700216 if (!_i.get())
217 return EGL_NO_IMAGE_KHR;
218
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700219 EGLContext context = egl_tls_t::getContext();
Mathias Agopian1473f462009-04-10 14:24:30 -0700220 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
221 return EGL_NO_IMAGE_KHR;
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700222
Mathias Agopian1473f462009-04-10 14:24:30 -0700223 egl_context_t const * const c = get_context(context);
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700224 if (c == NULL) // this should never happen
Mathias Agopian1473f462009-04-10 14:24:30 -0700225 return EGL_NO_IMAGE_KHR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
Mathias Agopianf1e4e062011-05-16 18:58:55 -0700227 // 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
Mathias Agopian1473f462009-04-10 14:24:30 -0700231 egl_image_t const * const i = get_image(image);
Mathias Agopian1473f462009-04-10 14:24:30 -0700232 return i->images[c->impl];
233}
234
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700235// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -0700236
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700237// this mutex protects:
Mathias Agopian94263d72009-08-24 21:47:13 -0700238// d->disp[]
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700239// egl_init_drivers_locked()
240//
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700241static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 if (sEarlyInitState) {
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700243 // initialized by static ctor. should be set here.
244 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246
Mathias Agopian9d17c052009-05-28 17:39:03 -0700247 // get our driver loader
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700248 Loader& loader(Loader::getInstance());
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700249
250 // dynamically load all our EGL implementations
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700251 egl_connection_t* cnx;
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700252
253 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 if (cnx->dso == 0) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700255 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
259
260 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700261 if (cnx->dso == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 char value[PROPERTY_VALUE_MAX];
263 property_get("debug.egl.hw", value, "1");
264 if (atoi(value) != 0) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700265 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 } else {
269 LOGD("3D hardware acceleration is disabled");
270 }
271 }
Mathias Agopian94263d72009-08-24 21:47:13 -0700272
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700273 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
274 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700277 return EGL_TRUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278}
279
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700280static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
281
282EGLBoolean egl_init_drivers() {
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700283 EGLBoolean res;
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700284 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700285 res = egl_init_drivers_locked();
Mathias Agopian7adf4ef2011-05-13 16:21:08 -0700286 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700287 return res;
288}
Mathias Agopian1473f462009-04-10 14:24:30 -0700289
Mathias Agopianf56a9602011-05-23 17:26:14 -0700290void 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
299static 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
306void 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
311gl_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
320void setGlThreadSpecific(gl_hooks_t const *value) {
321 pthread_setspecific(gGLWrapperKey, value);
322}
323
324gl_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
341char const * const gl_names[] = {
342 #include "entries.in"
343 NULL
344};
345
346char const * const egl_names[] = {
347 #include "egl_entries.in"
348 NULL
349};
350
351#undef GL_ENTRY
352#undef EGL_ENTRY
353
354
Mathias Agopian1473f462009-04-10 14:24:30 -0700355// ----------------------------------------------------------------------------
356}; // namespace android
357// ----------------------------------------------------------------------------
358