Hibernate the EGL implementation when idle
If the EGL implementation supports the EGL_IMG_hibernate_process
extension, use it to hibernate (and hopefully release memory or other
resources) when the process isn't actively using EGL or OpenGL ES. The
idleness heuristic used in this change is:
(a) Wake up when entering any EGL API call, and remain awake for the
duration of the call.
(b) Do not hibernate when any window surface exists; this means the
application is very likely in the foreground.
(c) Do not hibernate while any context is made current to a thread.
The app may be using a client API without the EGL layer knowing,
so it is not safe to hibernate.
(d) Only check these conditions and attempt to hibernate after a
window surface is destroyed or a thread's context is detached. By
not attempting to hibernate at the end of every EGL call, we avoid
some transient wakeups/hibernate cycles when the app is mostly idle,
or is starting to become active but hasn't created its window
surface yet.
On a Galaxy Nexus, hibernating frees 1567 VM pages from the process.
Both hibernating and waking can take anywhere from 30ms to over 100ms
-- measurements have been very inconsistent.
Change-Id: Ib555f5d9d069aefccca06e8173a89625b5f32d7e
diff --git a/opengl/libs/EGL/egl_display.h b/opengl/libs/EGL/egl_display.h
index 8fd23eb..28607da 100644
--- a/opengl/libs/EGL/egl_display.h
+++ b/opengl/libs/EGL/egl_display.h
@@ -70,6 +70,16 @@
// add reference to this object. returns true if this is a valid object.
bool getObject(egl_object_t* object) const;
+ // These notifications allow the display to keep track of how many window
+ // surfaces exist, which it uses to decide whether to hibernate the
+ // underlying EGL implementation. They can be called by any thread without
+ // holding a lock, but must be called via egl_display_ptr to ensure
+ // proper hibernate/wakeup sequencing. If a surface destruction triggers
+ // hibernation, hibernation will be delayed at least until the calling
+ // thread's egl_display_ptr is destroyed.
+ void onWindowSurfaceCreated();
+ void onWindowSurfaceDestroyed();
+
static egl_display_t* get(EGLDisplay dpy);
static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp);
@@ -124,10 +134,16 @@
String8 mClientApiString;
String8 mExtensionString;
int32_t mWakeCount;
+ bool mHibernating;
+ bool mAttemptHibernation;
};
// ----------------------------------------------------------------------------
+// An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
+// It doesn't refcount the egl_display_t, but does ensure that the underlying
+// EGL implementation is "awake" (not hibernating) and ready for use as long
+// as the egl_display_ptr exists.
class egl_display_ptr {
public:
explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {
@@ -141,12 +157,13 @@
// We only really need a C++11 move constructor, not a copy constructor.
// A move constructor would save an enter()/leave() pair on every EGL API
// call. But enabling -std=c++0x causes lots of errors elsewhere, so I
- // can't use a move constructor yet.
+ // can't use a move constructor until those are cleaned up.
//
// egl_display_ptr(egl_display_ptr&& other) {
// mDpy = other.mDpy;
// other.mDpy = NULL;
// }
+ //
egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {
if (mDpy) {
mDpy->enter();