blob: 1aca4c38d06c26a09e9742605ee38349b6026301 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "egl_impl.h"
David Li864f8392011-03-28 10:39:28 -070037#include "egl_tls.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070038#include "glesv2dbg.h"
39#include "hooks.h"
40#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Mathias Agopian518ec112011-05-13 16:21:08 -070042#include "egl_display.h"
43#include "egl_object.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45// ----------------------------------------------------------------------------
46namespace android {
47// ----------------------------------------------------------------------------
48
Mathias Agopian518ec112011-05-13 16:21:08 -070049egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
50gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
51gl_hooks_t gHooksNoContext;
52pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053
54// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070056#if EGL_TRACE
57
58EGLAPI pthread_key_t gGLTraceKey = -1;
59
60// ----------------------------------------------------------------------------
61
Mathias Agopian518ec112011-05-13 16:21:08 -070062int gEGLDebugLevel;
63
64static int sEGLTraceLevel;
65static int sEGLApplicationTraceLevel;
66
67extern gl_hooks_t gHooksTrace;
68extern gl_hooks_t gHooksDebug;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070069
70static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
71 pthread_setspecific(gGLTraceKey, value);
72}
73
74gl_hooks_t const* getGLTraceThreadSpecific() {
75 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
76}
77
Mathias Agopian518ec112011-05-13 16:21:08 -070078void initEglTraceLevel() {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070079 char value[PROPERTY_VALUE_MAX];
80 property_get("debug.egl.trace", value, "0");
81 int propertyLevel = atoi(value);
Mathias Agopian518ec112011-05-13 16:21:08 -070082 int applicationLevel = sEGLApplicationTraceLevel;
83 sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li499c6f02011-04-08 18:43:16 -070084
David Li2f5a6552011-03-01 16:08:10 -080085 property_get("debug.egl.debug_proc", value, "");
86 long pid = getpid();
87 char procPath[128] = {};
88 sprintf(procPath, "/proc/%ld/cmdline", pid);
89 FILE * file = fopen(procPath, "r");
90 if (file)
91 {
92 char cmdline[256] = {};
93 if (fgets(cmdline, sizeof(cmdline) - 1, file))
94 {
David Li2f5a6552011-03-01 16:08:10 -080095 if (!strcmp(value, cmdline))
Mathias Agopian518ec112011-05-13 16:21:08 -070096 sEGLTraceLevel = 1;
David Li499c6f02011-04-08 18:43:16 -070097 }
David Li2f5a6552011-03-01 16:08:10 -080098 fclose(file);
99 }
David Li499c6f02011-04-08 18:43:16 -0700100
Mathias Agopian518ec112011-05-13 16:21:08 -0700101 if (sEGLTraceLevel > 0)
David Li85f33a72011-03-10 19:07:42 -0800102 {
103 property_get("debug.egl.debug_port", value, "5039");
David Li499c6f02011-04-08 18:43:16 -0700104 const unsigned short port = (unsigned short)atoi(value);
105 property_get("debug.egl.debug_forceUseFile", value, "0");
106 const bool forceUseFile = (bool)atoi(value);
107 property_get("debug.egl.debug_maxFileSize", value, "8");
108 const unsigned int maxFileSize = atoi(value) << 20;
109 property_get("debug.egl.debug_filePath", value, "/data/local/tmp/dump.gles2dbg");
110 StartDebugServer(port, forceUseFile, maxFileSize, value);
David Li85f33a72011-03-10 19:07:42 -0800111 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700112}
113
Mathias Agopian518ec112011-05-13 16:21:08 -0700114void setGLHooksThreadSpecific(gl_hooks_t const *value) {
115 if (sEGLTraceLevel > 0) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700116 setGlTraceThreadSpecific(value);
117 setGlThreadSpecific(&gHooksTrace);
Mathias Agopian518ec112011-05-13 16:21:08 -0700118 } else if (sEGLTraceLevel > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800119 setGlTraceThreadSpecific(value);
120 setGlThreadSpecific(&gHooksDebug);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700121 } else {
122 setGlThreadSpecific(value);
123 }
124}
125
126/*
127 * Global entry point to allow applications to modify their own trace level.
128 * The effective trace level is the max of this level and the value of debug.egl.trace.
129 */
130extern "C"
131void setGLTraceLevel(int level) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700132 sEGLApplicationTraceLevel = level;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700133}
134
135#else
136
Mathias Agopian518ec112011-05-13 16:21:08 -0700137void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700138 setGlThreadSpecific(value);
139}
140
141#endif
142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143/*****************************************************************************/
144
Mathias Agopian6f087122010-09-23 16:38:38 -0700145static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -0700146 if (egl_tls_t::logNoContextCall()) {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700147 LOGE("call to OpenGL ES API with no current context "
148 "(logged once per thread)");
149 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700150 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700151}
152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153static void early_egl_init(void)
154{
155#if !USE_FAST_TLS_KEY
156 pthread_key_create(&gGLWrapperKey, NULL);
157#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700158#if EGL_TRACE
159 pthread_key_create(&gGLTraceKey, NULL);
160 initEglTraceLevel();
161#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 uint32_t addr = (uint32_t)((void*)gl_no_context);
163 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700164 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700166 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700167
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700168 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169}
170
171static pthread_once_t once_control = PTHREAD_ONCE_INIT;
172static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
173
Mathias Agopian518ec112011-05-13 16:21:08 -0700174// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175
Eric Hassold3ede7c12011-03-23 15:59:00 -0700176egl_display_t* validate_display(EGLDisplay dpy)
177{
178 egl_display_t * const dp = get_display(dpy);
179 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_display_t*)NULL);
180 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (egl_display_t*)NULL);
181
182 return dp;
183}
184
Mathias Agopian518ec112011-05-13 16:21:08 -0700185egl_connection_t* validate_display_config(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700187 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700189 dp = validate_display(dpy);
190 if (!dp) return (egl_connection_t*) NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191
Mathias Agopiancee79392010-07-26 21:14:59 -0700192 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
194 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700195 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 if (cnx->dso == 0) {
197 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
198 }
199 return cnx;
200}
201
Mathias Agopian518ec112011-05-13 16:21:08 -0700202EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700204 egl_display_t const * const dp = validate_display(dpy);
205 if (!dp) return EGL_FALSE;
206 if (!dp->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700208 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
210 return EGL_TRUE;
211}
212
Mathias Agopian518ec112011-05-13 16:21:08 -0700213EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700215 egl_display_t const * const dp = validate_display(dpy);
216 if (!dp) return EGL_FALSE;
217 if (!dp->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700219 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 return setError(EGL_BAD_SURFACE, EGL_FALSE);
221 return EGL_TRUE;
222}
223
Mathias Agopian518ec112011-05-13 16:21:08 -0700224// ----------------------------------------------------------------------------
225
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700226EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
227{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700228 ImageRef _i(image);
229 if (!_i.get()) return EGL_NO_IMAGE_KHR;
230
Mathias Agopian518ec112011-05-13 16:21:08 -0700231 EGLContext context = egl_tls_t::getContext();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
233 return EGL_NO_IMAGE_KHR;
234
235 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700236 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700237 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700239 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 return i->images[c->impl];
241}
242
Mathias Agopian923c6612009-08-17 18:07:06 -0700243// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700244
Mathias Agopian923c6612009-08-17 18:07:06 -0700245// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700246// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700247// egl_init_drivers_locked()
248//
Mathias Agopian518ec112011-05-13 16:21:08 -0700249static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700251 // initialized by static ctor. should be set here.
252 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 }
254
Mathias Agopiande586972009-05-28 17:39:03 -0700255 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700256 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700257
258 // dynamically load all our EGL implementations
Mathias Agopian923c6612009-08-17 18:07:06 -0700259 egl_connection_t* cnx;
Mathias Agopian923c6612009-08-17 18:07:06 -0700260
261 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700263 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
264 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
265 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 }
267
268 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700269 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 char value[PROPERTY_VALUE_MAX];
271 property_get("debug.egl.hw", value, "1");
272 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700273 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
274 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
275 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 } else {
277 LOGD("3D hardware acceleration is disabled");
278 }
279 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700280
Mathias Agopian923c6612009-08-17 18:07:06 -0700281 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
282 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 }
284
Mathias Agopian923c6612009-08-17 18:07:06 -0700285 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286}
287
Mathias Agopian518ec112011-05-13 16:21:08 -0700288static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
289
290EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700291 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700292 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700293 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700295 return res;
296}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297
298// ----------------------------------------------------------------------------
299}; // namespace android
300// ----------------------------------------------------------------------------
301