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 setGLHooksThreadSpecific(gl_hooks_t const *value);
43 // ----------------------------------------------------------------------------
45 static bool findExtension(const char* exts, const char* name, size_t nameLen) {
47 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
48 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
56 egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
58 egl_display_t::egl_display_t() :
59 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
62 egl_display_t::~egl_display_t() {
64 egl_cache_t::get()->terminate();
67 egl_display_t* egl_display_t::get(EGLDisplay dpy) {
68 uintptr_t index = uintptr_t(dpy)-1U;
69 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
72 return &sDisplay[index];
75 void egl_display_t::addObject(egl_object_t* object) {
76 Mutex::Autolock _l(lock);
80 void egl_display_t::removeObject(egl_object_t* object) {
81 Mutex::Autolock _l(lock);
82 objects.remove(object);
85 bool egl_display_t::getObject(egl_object_t* object) const {
86 Mutex::Autolock _l(lock);
87 if (objects.indexOf(object) >= 0) {
88 if (object->getDisplay() == this) {
96 EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
97 if (uintptr_t(disp) >= NUM_DISPLAYS)
100 return sDisplay[uintptr_t(disp)].getDisplay(disp);
103 EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
105 Mutex::Autolock _l(lock);
107 // get our driver loader
108 Loader& loader(Loader::getInstance());
110 egl_connection_t* const cnx = &gEGLImpl;
111 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
112 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
114 if (dpy == EGL_NO_DISPLAY) {
115 loader.close(cnx->dso);
120 return EGLDisplay(uintptr_t(display) + 1U);
123 EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
126 Mutex::Autolock _rf(refLock);
131 *major = VERSION_MAJOR;
133 *minor = VERSION_MINOR;
134 while(!eglIsInitialized) refCond.wait(refLock);
138 while(eglIsInitialized) refCond.wait(refLock);
142 Mutex::Autolock _l(lock);
144 setGLHooksThreadSpecific(&gHooksNoContext);
146 // initialize each EGL and
147 // build our own extension string first, based on the extension we know
148 // and the extension supported by our client implementation
150 egl_connection_t* const cnx = &gEGLImpl;
154 EGLDisplay idpy = disp.dpy;
155 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
156 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
157 // idpy, cnx->major, cnx->minor, cnx);
159 // display is now initialized
160 disp.state = egl_display_t::INITIALIZED;
162 // get the query-strings for this display for each implementation
163 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
165 disp.queryString.version = cnx->egl.eglQueryString(idpy,
167 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
169 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
173 ALOGW("eglInitialize(%p) failed (%s)", idpy,
174 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
178 // the query strings are per-display
179 mVendorString.setTo(sVendorString);
180 mVersionString.setTo(sVersionString);
181 mClientApiString.setTo(sClientApiString);
183 mExtensionString.setTo(gBuiltinExtensionString);
184 char const* start = gExtensionString;
186 // length of the extension name
187 size_t len = strcspn(start, " ");
189 // NOTE: we could avoid the copy if we had strnstr.
190 const String8 ext(start, len);
191 if (findExtension(disp.queryString.extensions, ext.string(),
193 mExtensionString.append(ext + " ");
195 // advance to the next extension name, skipping the space.
197 start += (*start == ' ') ? 1 : 0;
199 } while (*start != '\0');
201 egl_cache_t::get()->initialize(this);
203 char value[PROPERTY_VALUE_MAX];
204 property_get("debug.egl.finish", value, "0");
209 property_get("debug.egl.traceGpuCompletion", value, "0");
211 traceGpuCompletion = true;
215 *major = VERSION_MAJOR;
217 *minor = VERSION_MINOR;
219 mHibernation.setDisplayValid(true);
223 Mutex::Autolock _rf(refLock);
224 eglIsInitialized = true;
231 EGLBoolean egl_display_t::terminate() {
234 Mutex::Autolock _rl(refLock);
237 * From the EGL spec (3.2):
238 * "Termination of a display that has already been terminated,
239 * (...), is allowed, but the only effect of such a call is
240 * to return EGL_TRUE (...)
245 // this is specific to Android, display termination is ref-counted.
252 EGLBoolean res = EGL_FALSE;
255 Mutex::Autolock _l(lock);
257 egl_connection_t* const cnx = &gEGLImpl;
258 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
259 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
260 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
261 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
263 // REVISIT: it's unclear what to do if eglTerminate() fails
264 disp.state = egl_display_t::TERMINATED;
268 mHibernation.setDisplayValid(false);
270 // Reset the extension string since it will be regenerated if we get
272 mExtensionString.setTo("");
274 // Mark all objects remaining in the list as terminated, unless
275 // there are no reference to them, it which case, we're free to
277 size_t count = objects.size();
278 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
279 for (size_t i=0 ; i<count ; i++) {
280 egl_object_t* o = objects.itemAt(i);
284 // this marks all object handles are "terminated"
289 Mutex::Autolock _rl(refLock);
290 eglIsInitialized = false;
297 void egl_display_t::loseCurrent(egl_context_t * cur_c)
300 egl_display_t* display = cur_c->getDisplay();
302 display->loseCurrentImpl(cur_c);
307 void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
309 // by construction, these are either 0 or valid (possibly terminated)
310 // it should be impossible for these to be invalid
311 ContextRef _cur_c(cur_c);
312 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
313 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
315 { // scope for the lock
316 Mutex::Autolock _l(lock);
317 cur_c->onLooseCurrent();
321 // This cannot be called with the lock held because it might end-up
322 // calling back into EGL (in particular when a surface is destroyed
323 // it calls ANativeWindow::disconnect
329 EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
330 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
331 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
335 // by construction, these are either 0 or valid (possibly terminated)
336 // it should be impossible for these to be invalid
337 ContextRef _cur_c(cur_c);
338 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
339 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
341 { // scope for the lock
342 Mutex::Autolock _l(lock);
344 result = c->cnx->egl.eglMakeCurrent(
345 disp.dpy, impl_draw, impl_read, impl_ctx);
346 if (result == EGL_TRUE) {
347 c->onMakeCurrent(draw, read);
349 mHibernation.incWakeCount(HibernationMachine::STRONG);
353 result = cur_c->cnx->egl.eglMakeCurrent(
354 disp.dpy, impl_draw, impl_read, impl_ctx);
355 if (result == EGL_TRUE) {
356 cur_c->onLooseCurrent();
357 mHibernation.decWakeCount(HibernationMachine::STRONG);
362 if (result == EGL_TRUE) {
363 // This cannot be called with the lock held because it might end-up
364 // calling back into EGL (in particular when a surface is destroyed
365 // it calls ANativeWindow::disconnect
374 bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
376 nameLen = strlen(name);
378 return findExtension(mExtensionString.string(), name, nameLen);
381 // ----------------------------------------------------------------------------
383 bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
384 Mutex::Autolock _l(mLock);
385 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
386 "Invalid WakeCount (%d) on enter\n", mWakeCount);
389 if (strength == STRONG)
390 mAttemptHibernation = false;
392 if (CC_UNLIKELY(mHibernating)) {
393 ALOGV("Awakening\n");
394 egl_connection_t* const cnx = &gEGLImpl;
396 // These conditions should be guaranteed before entering hibernation;
397 // we don't want to get into a state where we can't wake up.
398 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
399 "Invalid hibernation state, unable to awaken\n");
401 if (!cnx->egl.eglAwakenProcessIMG()) {
402 ALOGE("Failed to awaken EGL implementation\n");
405 mHibernating = false;
410 void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
411 Mutex::Autolock _l(mLock);
412 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
415 if (strength == STRONG)
416 mAttemptHibernation = true;
418 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
419 egl_connection_t* const cnx = &gEGLImpl;
420 mAttemptHibernation = false;
421 if (mAllowHibernation && mDpyValid &&
422 cnx->egl.eglHibernateProcessIMG &&
423 cnx->egl.eglAwakenProcessIMG) {
424 ALOGV("Hibernating\n");
425 if (!cnx->egl.eglHibernateProcessIMG()) {
426 ALOGE("Failed to hibernate EGL implementation\n");
434 void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
435 Mutex::Autolock _l(mLock);
439 // ----------------------------------------------------------------------------
440 }; // namespace android
441 // ----------------------------------------------------------------------------