Terminate EGL when an app goes in the background

This does not happen on high end gfx devices. This happens
only if only one EGL context is initialized in the current
process.

Change-Id: Ibd1737efdf84eef8a84108b05795440d1ae9964e
diff --git a/libs/hwui/Caches.cpp b/libs/hwui/Caches.cpp
index 75b07de..f293cba 100644
--- a/libs/hwui/Caches.cpp
+++ b/libs/hwui/Caches.cpp
@@ -46,22 +46,16 @@
 // Constructors/destructor
 ///////////////////////////////////////////////////////////////////////////////
 
-Caches::Caches(): Singleton<Caches>(), blend(false), lastSrcMode(GL_ZERO),
-        lastDstMode(GL_ZERO), currentProgram(NULL) {
+Caches::Caches(): Singleton<Caches>(), mInitialized(false) {
     GLint maxTextureUnits;
     glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
     if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
         LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
     }
 
-    glGenBuffers(1, &meshBuffer);
-    glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
-    glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
-
     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
 
-    mCurrentBuffer = meshBuffer;
-    mRegionMesh = NULL;
+    init();
 
     mDebugLevel = readDebugLevel();
     LOGD("Enabling debug mode %d", mDebugLevel);
@@ -71,8 +65,40 @@
 #endif
 }
 
-Caches::~Caches() {
+void Caches::init() {
+    if (mInitialized) return;
+
+    glGenBuffers(1, &meshBuffer);
+    glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
+    glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
+
+    mCurrentBuffer = meshBuffer;
+    mRegionMesh = NULL;
+
+    blend = false;
+    lastSrcMode = GL_ZERO;
+    lastDstMode = GL_ZERO;
+    currentProgram = NULL;
+
+    mInitialized = true;
+}
+
+void Caches::terminate() {
+    if (!mInitialized) return;
+
+    glDeleteBuffers(1, &meshBuffer);
+    mCurrentBuffer = 0;
+
+    glDeleteBuffers(1, &mRegionMeshIndices);
     delete[] mRegionMesh;
+    mRegionMesh = NULL;
+
+    fboCache.clear();
+
+    programCache.clear();
+    currentProgram = NULL;
+
+    mInitialized = false;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/hwui/Caches.h b/libs/hwui/Caches.h
index 9b0d7c6e..5e58a9e 100644
--- a/libs/hwui/Caches.h
+++ b/libs/hwui/Caches.h
@@ -86,7 +86,6 @@
 
 class ANDROID_API Caches: public Singleton<Caches> {
     Caches();
-    ~Caches();
 
     friend class Singleton<Caches>;
 
@@ -109,6 +108,11 @@
     };
 
     /**
+     * Initializes the cache.
+     */
+    void init();
+
+    /**
      * Flush the cache.
      *
      * @param mode Indicates how much of the cache should be flushed
@@ -116,6 +120,12 @@
     void flush(FlushMode mode);
 
     /**
+     * Destroys all resources associated with this cache. This should
+     * be called after a flush(kFlushMode_Full).
+     */
+    void terminate();
+
+    /**
      * Indicates whether the renderer is in debug mode.
      * This debug mode provides limited information to app developers.
      */
@@ -194,6 +204,7 @@
 
 private:
     DebugLevel mDebugLevel;
+    bool mInitialized;
 }; // class Caches
 
 }; // namespace uirenderer