2 ** Copyright 2007, The Android Open Source Project
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
8 ** http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define __STDC_LIMIT_MACROS 1
21 #include "../egl_impl.h"
23 #include "egl_cache.h"
24 #include "egl_display.h"
25 #include "egl_object.h"
28 #include <cutils/properties.h>
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 static char const * const sVendorString = "Android";
35 static char const * const sVersionString = "1.4 Android META-EGL";
36 static char const * const sClientApiString = "OpenGL_ES";
38 extern char const * const gBuiltinExtensionString;
39 extern char const * const gExtensionString;
41 extern void initEglTraceLevel();
42 extern void initEglDebugLevel();
43 extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
45 // ----------------------------------------------------------------------------
47 egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
49 egl_display_t::egl_display_t() :
50 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
53 egl_display_t::~egl_display_t() {
55 egl_cache_t::get()->terminate();
58 egl_display_t* egl_display_t::get(EGLDisplay dpy) {
59 uintptr_t index = uintptr_t(dpy)-1U;
60 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
63 void egl_display_t::addObject(egl_object_t* object) {
64 Mutex::Autolock _l(lock);
68 void egl_display_t::removeObject(egl_object_t* object) {
69 Mutex::Autolock _l(lock);
70 objects.remove(object);
73 bool egl_display_t::getObject(egl_object_t* object) const {
74 Mutex::Autolock _l(lock);
75 if (objects.indexOf(object) >= 0) {
76 if (object->getDisplay() == this) {
84 EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
85 if (uintptr_t(disp) >= NUM_DISPLAYS)
88 return sDisplay[uintptr_t(disp)].getDisplay(disp);
91 EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
93 Mutex::Autolock _l(lock);
95 // get our driver loader
96 Loader& loader(Loader::getInstance());
98 egl_connection_t* const cnx = &gEGLImpl;
99 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
100 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
102 if (dpy == EGL_NO_DISPLAY) {
103 loader.close(cnx->dso);
108 return EGLDisplay(uintptr_t(display) + 1U);
111 EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
113 Mutex::Autolock _l(lock);
117 *major = VERSION_MAJOR;
119 *minor = VERSION_MINOR;
126 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
127 // the information from that call may be stale.)
133 setGLHooksThreadSpecific(&gHooksNoContext);
135 // initialize each EGL and
136 // build our own extension string first, based on the extension we know
137 // and the extension supported by our client implementation
139 egl_connection_t* const cnx = &gEGLImpl;
144 #if defined(ADRENO130)
145 #warning "Adreno-130 eglInitialize() workaround"
147 * The ADRENO 130 driver returns a different EGLDisplay each time
148 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
149 * after eglTerminate() has been called, so that eglInitialize()
150 * cannot be called again. Therefore, we need to make sure to call
151 * eglGetDisplay() before calling eglInitialize();
153 if (i == IMPL_HARDWARE) {
154 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
158 EGLDisplay idpy = disp.dpy;
159 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
160 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
161 // idpy, cnx->major, cnx->minor, cnx);
163 // display is now initialized
164 disp.state = egl_display_t::INITIALIZED;
166 // get the query-strings for this display for each implementation
167 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
169 disp.queryString.version = cnx->egl.eglQueryString(idpy,
171 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
173 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
177 ALOGW("eglInitialize(%p) failed (%s)", idpy,
178 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
182 // the query strings are per-display
183 mVendorString.setTo(sVendorString);
184 mVersionString.setTo(sVersionString);
185 mClientApiString.setTo(sClientApiString);
187 mExtensionString.setTo(gBuiltinExtensionString);
188 char const* start = gExtensionString;
191 // find the space separating this extension for the next one
192 end = strchr(start, ' ');
194 // length of the extension string
195 const size_t len = end - start;
197 // NOTE: we could avoid the copy if we had strnstr.
198 const String8 ext(start, len);
199 // now look for this extension
200 if (disp.queryString.extensions) {
201 // if we find it, add this extension string to our list
202 // (and don't forget the space)
203 const char* match = strstr(disp.queryString.extensions, ext.string());
204 if (match && (match[len] == ' ' || match[len] == 0)) {
205 mExtensionString.append(start, len+1);
209 // process the next extension string, and skip the space.
214 egl_cache_t::get()->initialize(this);
216 char value[PROPERTY_VALUE_MAX];
217 property_get("debug.egl.finish", value, "0");
222 property_get("debug.egl.traceGpuCompletion", value, "0");
224 traceGpuCompletion = true;
229 *major = VERSION_MAJOR;
231 *minor = VERSION_MINOR;
233 mHibernation.setDisplayValid(true);
238 EGLBoolean egl_display_t::terminate() {
240 Mutex::Autolock _l(lock);
244 * From the EGL spec (3.2):
245 * "Termination of a display that has already been terminated,
246 * (...), is allowed, but the only effect of such a call is
247 * to return EGL_TRUE (...)
252 // this is specific to Android, display termination is ref-counted.
258 EGLBoolean res = EGL_FALSE;
259 egl_connection_t* const cnx = &gEGLImpl;
260 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
261 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
262 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
263 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
265 // REVISIT: it's unclear what to do if eglTerminate() fails
266 disp.state = egl_display_t::TERMINATED;
270 mHibernation.setDisplayValid(false);
272 // Reset the extension string since it will be regenerated if we get
274 mExtensionString.setTo("");
276 // Mark all objects remaining in the list as terminated, unless
277 // there are no reference to them, it which case, we're free to
279 size_t count = objects.size();
280 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
281 for (size_t i=0 ; i<count ; i++) {
282 egl_object_t* o = objects.itemAt(i);
286 // this marks all object handles are "terminated"
293 void egl_display_t::loseCurrent(egl_context_t * cur_c)
296 egl_display_t* display = cur_c->getDisplay();
298 display->loseCurrentImpl(cur_c);
303 void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
305 // by construction, these are either 0 or valid (possibly terminated)
306 // it should be impossible for these to be invalid
307 ContextRef _cur_c(cur_c);
308 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
309 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
311 { // scope for the lock
312 Mutex::Autolock _l(lock);
313 cur_c->onLooseCurrent();
317 // This cannot be called with the lock held because it might end-up
318 // calling back into EGL (in particular when a surface is destroyed
319 // it calls ANativeWindow::disconnect
325 EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
326 EGLSurface draw, EGLSurface read, EGLContext ctx,
327 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
331 // by construction, these are either 0 or valid (possibly terminated)
332 // it should be impossible for these to be invalid
333 ContextRef _cur_c(cur_c);
334 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
335 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
337 { // scope for the lock
338 Mutex::Autolock _l(lock);
340 result = c->cnx->egl.eglMakeCurrent(
341 disp.dpy, impl_draw, impl_read, impl_ctx);
342 if (result == EGL_TRUE) {
343 c->onMakeCurrent(draw, read);
345 mHibernation.incWakeCount(HibernationMachine::STRONG);
349 result = cur_c->cnx->egl.eglMakeCurrent(
350 disp.dpy, impl_draw, impl_read, impl_ctx);
351 if (result == EGL_TRUE) {
352 cur_c->onLooseCurrent();
353 mHibernation.decWakeCount(HibernationMachine::STRONG);
358 if (result == EGL_TRUE) {
359 // This cannot be called with the lock held because it might end-up
360 // calling back into EGL (in particular when a surface is destroyed
361 // it calls ANativeWindow::disconnect
370 // ----------------------------------------------------------------------------
372 bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
373 Mutex::Autolock _l(mLock);
374 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
375 "Invalid WakeCount (%d) on enter\n", mWakeCount);
378 if (strength == STRONG)
379 mAttemptHibernation = false;
381 if (CC_UNLIKELY(mHibernating)) {
382 ALOGV("Awakening\n");
383 egl_connection_t* const cnx = &gEGLImpl;
385 // These conditions should be guaranteed before entering hibernation;
386 // we don't want to get into a state where we can't wake up.
387 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
388 "Invalid hibernation state, unable to awaken\n");
390 if (!cnx->egl.eglAwakenProcessIMG()) {
391 ALOGE("Failed to awaken EGL implementation\n");
394 mHibernating = false;
399 void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
400 Mutex::Autolock _l(mLock);
401 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
404 if (strength == STRONG)
405 mAttemptHibernation = true;
407 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
408 egl_connection_t* const cnx = &gEGLImpl;
409 mAttemptHibernation = false;
410 if (mAllowHibernation && mDpyValid &&
411 cnx->egl.eglHibernateProcessIMG &&
412 cnx->egl.eglAwakenProcessIMG) {
413 ALOGV("Hibernating\n");
414 if (!cnx->egl.eglHibernateProcessIMG()) {
415 ALOGE("Failed to hibernate EGL implementation\n");
423 void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
424 Mutex::Autolock _l(mLock);
428 // ----------------------------------------------------------------------------
429 }; // namespace android
430 // ----------------------------------------------------------------------------