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 static bool findExtension(const char* exts, const char* name, size_t nameLen) {
49 const char* match = strstr(exts, name);
50 if (match && (match[nameLen] == '\0' || match[nameLen] == ' ')) {
57 egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
59 egl_display_t::egl_display_t() :
60 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
63 egl_display_t::~egl_display_t() {
65 egl_cache_t::get()->terminate();
68 egl_display_t* egl_display_t::get(EGLDisplay dpy) {
69 uintptr_t index = uintptr_t(dpy)-1U;
70 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
73 void egl_display_t::addObject(egl_object_t* object) {
74 Mutex::Autolock _l(lock);
78 void egl_display_t::removeObject(egl_object_t* object) {
79 Mutex::Autolock _l(lock);
80 objects.remove(object);
83 bool egl_display_t::getObject(egl_object_t* object) const {
84 Mutex::Autolock _l(lock);
85 if (objects.indexOf(object) >= 0) {
86 if (object->getDisplay() == this) {
94 EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
95 if (uintptr_t(disp) >= NUM_DISPLAYS)
98 return sDisplay[uintptr_t(disp)].getDisplay(disp);
101 EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
103 Mutex::Autolock _l(lock);
105 // get our driver loader
106 Loader& loader(Loader::getInstance());
108 egl_connection_t* const cnx = &gEGLImpl;
109 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
110 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
112 if (dpy == EGL_NO_DISPLAY) {
113 loader.close(cnx->dso);
118 return EGLDisplay(uintptr_t(display) + 1U);
121 EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
123 Mutex::Autolock _l(lock);
127 *major = VERSION_MAJOR;
129 *minor = VERSION_MINOR;
136 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
137 // the information from that call may be stale.)
143 setGLHooksThreadSpecific(&gHooksNoContext);
145 // initialize each EGL and
146 // build our own extension string first, based on the extension we know
147 // and the extension supported by our client implementation
149 egl_connection_t* const cnx = &gEGLImpl;
153 EGLDisplay idpy = disp.dpy;
154 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
155 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
156 // idpy, cnx->major, cnx->minor, cnx);
158 // display is now initialized
159 disp.state = egl_display_t::INITIALIZED;
161 // get the query-strings for this display for each implementation
162 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
164 disp.queryString.version = cnx->egl.eglQueryString(idpy,
166 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
168 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
172 ALOGW("eglInitialize(%p) failed (%s)", idpy,
173 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
177 // the query strings are per-display
178 mVendorString.setTo(sVendorString);
179 mVersionString.setTo(sVersionString);
180 mClientApiString.setTo(sClientApiString);
182 mExtensionString.setTo(gBuiltinExtensionString);
183 char const* start = gExtensionString;
186 // find the space separating this extension for the next one
187 end = strchr(start, ' ');
189 // length of the extension string
190 const size_t len = end - start;
192 // NOTE: we could avoid the copy if we had strnstr.
193 const String8 ext(start, len);
194 if (findExtension(disp.queryString.extensions, ext.string(),
196 mExtensionString.append(start, len+1);
199 // process the next extension string, and skip the space.
204 egl_cache_t::get()->initialize(this);
206 char value[PROPERTY_VALUE_MAX];
207 property_get("debug.egl.finish", value, "0");
212 property_get("debug.egl.traceGpuCompletion", value, "0");
214 traceGpuCompletion = true;
219 *major = VERSION_MAJOR;
221 *minor = VERSION_MINOR;
223 mHibernation.setDisplayValid(true);
228 EGLBoolean egl_display_t::terminate() {
230 Mutex::Autolock _l(lock);
234 * From the EGL spec (3.2):
235 * "Termination of a display that has already been terminated,
236 * (...), is allowed, but the only effect of such a call is
237 * to return EGL_TRUE (...)
242 // this is specific to Android, display termination is ref-counted.
248 EGLBoolean res = EGL_FALSE;
249 egl_connection_t* const cnx = &gEGLImpl;
250 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
251 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
252 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
253 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
255 // REVISIT: it's unclear what to do if eglTerminate() fails
256 disp.state = egl_display_t::TERMINATED;
260 mHibernation.setDisplayValid(false);
262 // Reset the extension string since it will be regenerated if we get
264 mExtensionString.setTo("");
266 // Mark all objects remaining in the list as terminated, unless
267 // there are no reference to them, it which case, we're free to
269 size_t count = objects.size();
270 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
271 for (size_t i=0 ; i<count ; i++) {
272 egl_object_t* o = objects.itemAt(i);
276 // this marks all object handles are "terminated"
283 void egl_display_t::loseCurrent(egl_context_t * cur_c)
286 egl_display_t* display = cur_c->getDisplay();
288 display->loseCurrentImpl(cur_c);
293 void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
295 // by construction, these are either 0 or valid (possibly terminated)
296 // it should be impossible for these to be invalid
297 ContextRef _cur_c(cur_c);
298 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
299 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
301 { // scope for the lock
302 Mutex::Autolock _l(lock);
303 cur_c->onLooseCurrent();
307 // This cannot be called with the lock held because it might end-up
308 // calling back into EGL (in particular when a surface is destroyed
309 // it calls ANativeWindow::disconnect
315 EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
316 EGLSurface draw, EGLSurface read, EGLContext ctx,
317 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
321 // by construction, these are either 0 or valid (possibly terminated)
322 // it should be impossible for these to be invalid
323 ContextRef _cur_c(cur_c);
324 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
325 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
327 { // scope for the lock
328 Mutex::Autolock _l(lock);
330 result = c->cnx->egl.eglMakeCurrent(
331 disp.dpy, impl_draw, impl_read, impl_ctx);
332 if (result == EGL_TRUE) {
333 c->onMakeCurrent(draw, read);
335 mHibernation.incWakeCount(HibernationMachine::STRONG);
339 result = cur_c->cnx->egl.eglMakeCurrent(
340 disp.dpy, impl_draw, impl_read, impl_ctx);
341 if (result == EGL_TRUE) {
342 cur_c->onLooseCurrent();
343 mHibernation.decWakeCount(HibernationMachine::STRONG);
348 if (result == EGL_TRUE) {
349 // This cannot be called with the lock held because it might end-up
350 // calling back into EGL (in particular when a surface is destroyed
351 // it calls ANativeWindow::disconnect
360 bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
362 nameLen = strlen(name);
364 return findExtension(mExtensionString.string(), name, nameLen);
367 // ----------------------------------------------------------------------------
369 bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
370 Mutex::Autolock _l(mLock);
371 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
372 "Invalid WakeCount (%d) on enter\n", mWakeCount);
375 if (strength == STRONG)
376 mAttemptHibernation = false;
378 if (CC_UNLIKELY(mHibernating)) {
379 ALOGV("Awakening\n");
380 egl_connection_t* const cnx = &gEGLImpl;
382 // These conditions should be guaranteed before entering hibernation;
383 // we don't want to get into a state where we can't wake up.
384 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
385 "Invalid hibernation state, unable to awaken\n");
387 if (!cnx->egl.eglAwakenProcessIMG()) {
388 ALOGE("Failed to awaken EGL implementation\n");
391 mHibernating = false;
396 void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
397 Mutex::Autolock _l(mLock);
398 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
401 if (strength == STRONG)
402 mAttemptHibernation = true;
404 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
405 egl_connection_t* const cnx = &gEGLImpl;
406 mAttemptHibernation = false;
407 if (mAllowHibernation && mDpyValid &&
408 cnx->egl.eglHibernateProcessIMG &&
409 cnx->egl.eglAwakenProcessIMG) {
410 ALOGV("Hibernating\n");
411 if (!cnx->egl.eglHibernateProcessIMG()) {
412 ALOGE("Failed to hibernate EGL implementation\n");
420 void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
421 Mutex::Autolock _l(mLock);
425 // ----------------------------------------------------------------------------
426 }; // namespace android
427 // ----------------------------------------------------------------------------