blob: b11db324ddb3c6993093cd181834ee6adb664a73 [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
Mathias Agopian24035332010-08-02 17:34:32 -070034#include <utils/String8.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070035
Mathias Agopian1cadb252011-05-23 17:26:14 -070036#include "egldefs.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include "egl_impl.h"
David Li864f8392011-03-28 10:39:28 -070038#include "egl_tls.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070039#include "glesv2dbg.h"
40#include "hooks.h"
41#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
Mathias Agopian518ec112011-05-13 16:21:08 -070043#include "egl_display.h"
44#include "egl_object.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46// ----------------------------------------------------------------------------
47namespace android {
48// ----------------------------------------------------------------------------
49
Mathias Agopian518ec112011-05-13 16:21:08 -070050egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
51gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
52gl_hooks_t gHooksNoContext;
53pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054
55// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070057#if EGL_TRACE
58
59EGLAPI pthread_key_t gGLTraceKey = -1;
60
61// ----------------------------------------------------------------------------
62
Mathias Agopian518ec112011-05-13 16:21:08 -070063int gEGLDebugLevel;
64
65static int sEGLTraceLevel;
66static int sEGLApplicationTraceLevel;
67
68extern gl_hooks_t gHooksTrace;
69extern gl_hooks_t gHooksDebug;
Jack Palevicha2dd6cf2010-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 Agopian518ec112011-05-13 16:21:08 -070079void initEglTraceLevel() {
Jack Palevicha2dd6cf2010-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 Agopian518ec112011-05-13 16:21:08 -070083 int applicationLevel = sEGLApplicationTraceLevel;
84 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li499c6f02011-04-08 18:43:16 -070085
David Li2f5a6552011-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");
91 if (file)
92 {
93 char cmdline[256] = {};
94 if (fgets(cmdline, sizeof(cmdline) - 1, file))
95 {
David Li2f5a6552011-03-01 16:08:10 -080096 if (!strcmp(value, cmdline))
Mathias Agopian518ec112011-05-13 16:21:08 -070097 sEGLTraceLevel = 1;
David Li499c6f02011-04-08 18:43:16 -070098 }
David Li2f5a6552011-03-01 16:08:10 -080099 fclose(file);
100 }
David Li499c6f02011-04-08 18:43:16 -0700101
Mathias Agopian518ec112011-05-13 16:21:08 -0700102 if (sEGLTraceLevel > 0)
David Li85f33a72011-03-10 19:07:42 -0800103 {
104 property_get("debug.egl.debug_port", value, "5039");
David Li499c6f02011-04-08 18:43:16 -0700105 const unsigned short port = (unsigned short)atoi(value);
106 property_get("debug.egl.debug_forceUseFile", value, "0");
107 const bool forceUseFile = (bool)atoi(value);
108 property_get("debug.egl.debug_maxFileSize", value, "8");
109 const unsigned int maxFileSize = atoi(value) << 20;
110 property_get("debug.egl.debug_filePath", value, "/data/local/tmp/dump.gles2dbg");
111 StartDebugServer(port, forceUseFile, maxFileSize, value);
David Li85f33a72011-03-10 19:07:42 -0800112 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700113}
114
Mathias Agopian518ec112011-05-13 16:21:08 -0700115void setGLHooksThreadSpecific(gl_hooks_t const *value) {
116 if (sEGLTraceLevel > 0) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700117 setGlTraceThreadSpecific(value);
118 setGlThreadSpecific(&gHooksTrace);
Mathias Agopian518ec112011-05-13 16:21:08 -0700119 } else if (sEGLTraceLevel > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800120 setGlTraceThreadSpecific(value);
121 setGlThreadSpecific(&gHooksDebug);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700122 } else {
123 setGlThreadSpecific(value);
124 }
125}
126
127/*
128 * Global entry point to allow applications to modify their own trace level.
129 * The effective trace level is the max of this level and the value of debug.egl.trace.
130 */
131extern "C"
132void setGLTraceLevel(int level) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700133 sEGLApplicationTraceLevel = level;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700134}
135
136#else
137
Mathias Agopian518ec112011-05-13 16:21:08 -0700138void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700139 setGlThreadSpecific(value);
140}
141
142#endif
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144/*****************************************************************************/
145
Mathias Agopian6f087122010-09-23 16:38:38 -0700146static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -0700147 if (egl_tls_t::logNoContextCall()) {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700148 LOGE("call to OpenGL ES API with no current context "
149 "(logged once per thread)");
150 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700151 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700152}
153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154static void early_egl_init(void)
155{
156#if !USE_FAST_TLS_KEY
157 pthread_key_create(&gGLWrapperKey, NULL);
158#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700159#if EGL_TRACE
160 pthread_key_create(&gGLTraceKey, NULL);
161 initEglTraceLevel();
162#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 uint32_t addr = (uint32_t)((void*)gl_no_context);
164 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700165 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700167 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700168
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700169 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170}
171
172static pthread_once_t once_control = PTHREAD_ONCE_INIT;
173static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
174
Mathias Agopian518ec112011-05-13 16:21:08 -0700175// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
Mathias Agopian5b287a62011-05-16 18:58:55 -0700177egl_display_t* validate_display(EGLDisplay dpy) {
Eric Hassold3ede7c12011-03-23 15:59:00 -0700178 egl_display_t * const dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700179 if (!dp)
180 return setError(EGL_BAD_DISPLAY, (egl_display_t*)NULL);
181 if (!dp->isReady())
182 return setError(EGL_NOT_INITIALIZED, (egl_display_t*)NULL);
Eric Hassold3ede7c12011-03-23 15:59:00 -0700183
184 return dp;
185}
186
Mathias Agopian5b287a62011-05-16 18:58:55 -0700187egl_connection_t* validate_display_config(EGLDisplay dpy, EGLConfig config,
188 egl_display_t const*& dp) {
Eric Hassold3ede7c12011-03-23 15:59:00 -0700189 dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700190 if (!dp)
191 return (egl_connection_t*) NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192
Mathias Agopiancee79392010-07-26 21:14:59 -0700193 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
195 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700196 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 if (cnx->dso == 0) {
198 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
199 }
200 return cnx;
201}
202
Mathias Agopian518ec112011-05-13 16:21:08 -0700203// ----------------------------------------------------------------------------
204
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
206{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700207 ImageRef _i(image);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700208 if (!_i.get())
209 return EGL_NO_IMAGE_KHR;
210
Mathias Agopian518ec112011-05-13 16:21:08 -0700211 EGLContext context = egl_tls_t::getContext();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700212 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
213 return EGL_NO_IMAGE_KHR;
Mathias Agopian5b287a62011-05-16 18:58:55 -0700214
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215 egl_context_t const * const c = get_context(context);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700216 if (c == NULL) // this should never happen
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218
Mathias Agopian5b287a62011-05-16 18:58:55 -0700219 // here we don't validate the context because if it's been marked for
220 // termination, this call should still succeed since it's internal to
221 // EGL.
222
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700223 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700224 return i->images[c->impl];
225}
226
Mathias Agopian923c6612009-08-17 18:07:06 -0700227// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700228
Mathias Agopian923c6612009-08-17 18:07:06 -0700229// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700230// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700231// egl_init_drivers_locked()
232//
Mathias Agopian518ec112011-05-13 16:21:08 -0700233static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700235 // initialized by static ctor. should be set here.
236 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 }
238
Mathias Agopiande586972009-05-28 17:39:03 -0700239 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700240 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700241
242 // dynamically load all our EGL implementations
Mathias Agopian923c6612009-08-17 18:07:06 -0700243 egl_connection_t* cnx;
Mathias Agopian923c6612009-08-17 18:07:06 -0700244
245 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700247 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
248 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
249 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 }
251
252 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700253 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 char value[PROPERTY_VALUE_MAX];
255 property_get("debug.egl.hw", value, "1");
256 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700257 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
258 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
259 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 } else {
261 LOGD("3D hardware acceleration is disabled");
262 }
263 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700264
Mathias Agopian923c6612009-08-17 18:07:06 -0700265 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
266 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 }
268
Mathias Agopian923c6612009-08-17 18:07:06 -0700269 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270}
271
Mathias Agopian518ec112011-05-13 16:21:08 -0700272static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
273
274EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700275 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700276 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700277 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700278 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700279 return res;
280}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700281
Mathias Agopian1cadb252011-05-23 17:26:14 -0700282void gl_unimplemented() {
283 LOGE("called unimplemented OpenGL ES API");
284}
285
286// ----------------------------------------------------------------------------
287
288#if USE_FAST_TLS_KEY
289
290// We have a dedicated TLS slot in bionic
291static inline gl_hooks_t const * volatile * get_tls_hooks() {
292 volatile void *tls_base = __get_tls();
293 gl_hooks_t const * volatile * tls_hooks =
294 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base);
295 return tls_hooks;
296}
297
298void setGlThreadSpecific(gl_hooks_t const *value) {
299 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
300 tls_hooks[TLS_SLOT_OPENGL_API] = value;
301}
302
303gl_hooks_t const* getGlThreadSpecific() {
304 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
305 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API];
306 if (hooks) return hooks;
307 return &gHooksNoContext;
308}
309
310#else
311
312void setGlThreadSpecific(gl_hooks_t const *value) {
313 pthread_setspecific(gGLWrapperKey, value);
314}
315
316gl_hooks_t const* getGlThreadSpecific() {
317 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
318 if (hooks) return hooks;
319 return &gHooksNoContext;
320}
321
322#endif
323
324// ----------------------------------------------------------------------------
325// GL / EGL hooks
326// ----------------------------------------------------------------------------
327
328#undef GL_ENTRY
329#undef EGL_ENTRY
330#define GL_ENTRY(_r, _api, ...) #_api,
331#define EGL_ENTRY(_r, _api, ...) #_api,
332
333char const * const gl_names[] = {
334 #include "entries.in"
335 NULL
336};
337
338char const * const egl_names[] = {
339 #include "egl_entries.in"
340 NULL
341};
342
343#undef GL_ENTRY
344#undef EGL_ENTRY
345
346
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700347// ----------------------------------------------------------------------------
348}; // namespace android
349// ----------------------------------------------------------------------------
350