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_cache.h"
22 #include "egl_display.h"
23 #include "egl_object.h"
27 #include <cutils/properties.h>
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 static char const * const sVendorString = "Android";
34 static char const * const sVersionString = "1.4 Android META-EGL";
35 static char const * const sClientApiString = "OpenGL_ES";
37 // this is the list of EGL extensions that are exposed to applications
38 // some of them are mandatory because used by the ANDROID system.
40 // mandatory extensions are required per the CDD and not explicitly
41 // checked during EGL initialization. the system *assumes* these extensions
42 // are present. the system may not function properly if some mandatory
43 // extensions are missing.
45 // NOTE: sExtensionString MUST be have a single space as the last character.
47 static char const * const sExtensionString =
48 "EGL_KHR_image " // mandatory
49 "EGL_KHR_image_base " // mandatory
50 "EGL_KHR_image_pixmap "
51 "EGL_KHR_gl_texture_2D_image "
52 "EGL_KHR_gl_texture_cubemap_image "
53 "EGL_KHR_gl_renderbuffer_image "
55 "EGL_EXT_create_context_robustness "
57 "EGL_ANDROID_image_native_buffer " // mandatory
60 // extensions not exposed to applications but used by the ANDROID system
61 // "EGL_ANDROID_recordable " // mandatory
62 // "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
63 // "EGL_ANDROID_blob_cache " // strongly recommended
64 // "EGL_ANDROID_native_fence_sync " // strongly recommended
65 // "EGL_IMG_hibernate_process " // optional
67 extern void initEglTraceLevel();
68 extern void initEglDebugLevel();
69 extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
71 // ----------------------------------------------------------------------------
73 egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
75 egl_display_t::egl_display_t() :
76 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
79 egl_display_t::~egl_display_t() {
81 egl_cache_t::get()->terminate();
84 egl_display_t* egl_display_t::get(EGLDisplay dpy) {
85 uintptr_t index = uintptr_t(dpy)-1U;
86 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
89 void egl_display_t::addObject(egl_object_t* object) {
90 Mutex::Autolock _l(lock);
94 void egl_display_t::removeObject(egl_object_t* object) {
95 Mutex::Autolock _l(lock);
96 objects.remove(object);
99 bool egl_display_t::getObject(egl_object_t* object) const {
100 Mutex::Autolock _l(lock);
101 if (objects.indexOf(object) >= 0) {
102 if (object->getDisplay() == this) {
110 EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
111 if (uintptr_t(disp) >= NUM_DISPLAYS)
114 return sDisplay[uintptr_t(disp)].getDisplay(disp);
117 EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
119 Mutex::Autolock _l(lock);
121 // get our driver loader
122 Loader& loader(Loader::getInstance());
124 egl_connection_t* const cnx = &gEGLImpl;
125 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
126 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
128 if (dpy == EGL_NO_DISPLAY) {
129 loader.close(cnx->dso);
134 return EGLDisplay(uintptr_t(display) + 1U);
137 EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
139 Mutex::Autolock _l(lock);
143 *major = VERSION_MAJOR;
145 *minor = VERSION_MINOR;
152 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
153 // the information from that call may be stale.)
159 setGLHooksThreadSpecific(&gHooksNoContext);
161 // initialize each EGL and
162 // build our own extension string first, based on the extension we know
163 // and the extension supported by our client implementation
165 egl_connection_t* const cnx = &gEGLImpl;
170 #if defined(ADRENO130)
171 #warning "Adreno-130 eglInitialize() workaround"
173 * The ADRENO 130 driver returns a different EGLDisplay each time
174 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
175 * after eglTerminate() has been called, so that eglInitialize()
176 * cannot be called again. Therefore, we need to make sure to call
177 * eglGetDisplay() before calling eglInitialize();
179 if (i == IMPL_HARDWARE) {
180 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
184 EGLDisplay idpy = disp.dpy;
185 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
186 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
187 // idpy, cnx->major, cnx->minor, cnx);
189 // display is now initialized
190 disp.state = egl_display_t::INITIALIZED;
192 // get the query-strings for this display for each implementation
193 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
195 disp.queryString.version = cnx->egl.eglQueryString(idpy,
197 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
199 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
203 ALOGW("eglInitialize(%p) failed (%s)", idpy,
204 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
208 // the query strings are per-display
209 mVendorString.setTo(sVendorString);
210 mVersionString.setTo(sVersionString);
211 mClientApiString.setTo(sClientApiString);
213 // we only add extensions that exist in the implementation
214 char const* start = sExtensionString;
217 // find the space separating this extension for the next one
218 end = strchr(start, ' ');
220 // length of the extension string
221 const size_t len = end - start;
223 // NOTE: we could avoid the copy if we had strnstr.
224 const String8 ext(start, len);
225 // now look for this extension
226 if (disp.queryString.extensions) {
227 // if we find it, add this extension string to our list
228 // (and don't forget the space)
229 const char* match = strstr(disp.queryString.extensions, ext.string());
230 if (match && (match[len] == ' ' || match[len] == 0)) {
231 mExtensionString.append(start, len+1);
235 // process the next extension string, and skip the space.
240 egl_cache_t::get()->initialize(this);
242 char value[PROPERTY_VALUE_MAX];
243 property_get("debug.egl.finish", value, "0");
248 property_get("debug.egl.traceGpuCompletion", value, "0");
250 traceGpuCompletion = true;
255 *major = VERSION_MAJOR;
257 *minor = VERSION_MINOR;
259 mHibernation.setDisplayValid(true);
264 EGLBoolean egl_display_t::terminate() {
266 Mutex::Autolock _l(lock);
270 * From the EGL spec (3.2):
271 * "Termination of a display that has already been terminated,
272 * (...), is allowed, but the only effect of such a call is
273 * to return EGL_TRUE (...)
278 // this is specific to Android, display termination is ref-counted.
284 EGLBoolean res = EGL_FALSE;
285 egl_connection_t* const cnx = &gEGLImpl;
286 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
287 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
288 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
289 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
291 // REVISIT: it's unclear what to do if eglTerminate() fails
292 disp.state = egl_display_t::TERMINATED;
296 mHibernation.setDisplayValid(false);
298 // Reset the extension string since it will be regenerated if we get
300 mExtensionString.setTo("");
302 // Mark all objects remaining in the list as terminated, unless
303 // there are no reference to them, it which case, we're free to
305 size_t count = objects.size();
306 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
307 for (size_t i=0 ; i<count ; i++) {
308 egl_object_t* o = objects.itemAt(i);
312 // this marks all object handles are "terminated"
319 void egl_display_t::loseCurrent(egl_context_t * cur_c)
322 egl_display_t* display = cur_c->getDisplay();
324 display->loseCurrentImpl(cur_c);
329 void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
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);
339 cur_c->onLooseCurrent();
343 // This cannot be called with the lock held because it might end-up
344 // calling back into EGL (in particular when a surface is destroyed
345 // it calls ANativeWindow::disconnect
351 EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
352 EGLSurface draw, EGLSurface read, EGLContext ctx,
353 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
357 // by construction, these are either 0 or valid (possibly terminated)
358 // it should be impossible for these to be invalid
359 ContextRef _cur_c(cur_c);
360 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
361 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
363 { // scope for the lock
364 Mutex::Autolock _l(lock);
366 result = c->cnx->egl.eglMakeCurrent(
367 disp.dpy, impl_draw, impl_read, impl_ctx);
368 if (result == EGL_TRUE) {
369 c->onMakeCurrent(draw, read);
371 mHibernation.incWakeCount(HibernationMachine::STRONG);
375 result = cur_c->cnx->egl.eglMakeCurrent(
376 disp.dpy, impl_draw, impl_read, impl_ctx);
377 if (result == EGL_TRUE) {
378 cur_c->onLooseCurrent();
379 mHibernation.decWakeCount(HibernationMachine::STRONG);
384 if (result == EGL_TRUE) {
385 // This cannot be called with the lock held because it might end-up
386 // calling back into EGL (in particular when a surface is destroyed
387 // it calls ANativeWindow::disconnect
396 // ----------------------------------------------------------------------------
398 bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
399 Mutex::Autolock _l(mLock);
400 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
401 "Invalid WakeCount (%d) on enter\n", mWakeCount);
404 if (strength == STRONG)
405 mAttemptHibernation = false;
407 if (CC_UNLIKELY(mHibernating)) {
408 ALOGV("Awakening\n");
409 egl_connection_t* const cnx = &gEGLImpl;
411 // These conditions should be guaranteed before entering hibernation;
412 // we don't want to get into a state where we can't wake up.
413 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
414 "Invalid hibernation state, unable to awaken\n");
416 if (!cnx->egl.eglAwakenProcessIMG()) {
417 ALOGE("Failed to awaken EGL implementation\n");
420 mHibernating = false;
425 void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
426 Mutex::Autolock _l(mLock);
427 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
430 if (strength == STRONG)
431 mAttemptHibernation = true;
433 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
434 egl_connection_t* const cnx = &gEGLImpl;
435 mAttemptHibernation = false;
436 if (mAllowHibernation && mDpyValid &&
437 cnx->egl.eglHibernateProcessIMG &&
438 cnx->egl.eglAwakenProcessIMG) {
439 ALOGV("Hibernating\n");
440 if (!cnx->egl.eglHibernateProcessIMG()) {
441 ALOGE("Failed to hibernate EGL implementation\n");
449 void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
450 Mutex::Autolock _l(mLock);
454 // ----------------------------------------------------------------------------
455 }; // namespace android
456 // ----------------------------------------------------------------------------