Only destroy the GL Canvas when necessary.

Change-Id: I6caf4873b02dc4a6d7c8c91925d2447b3b0e0593
diff --git a/core/java/android/view/HardwareRenderer.java b/core/java/android/view/HardwareRenderer.java
index 32d8123..42e3641 100644
--- a/core/java/android/view/HardwareRenderer.java
+++ b/core/java/android/view/HardwareRenderer.java
@@ -53,8 +53,10 @@
 
     /**
      * Destroys the hardware rendering context.
+     * 
+     * @param full If true, destroys all associated resources.
      */
-    abstract void destroy();
+    abstract void destroy(boolean full);
 
     /**
      * Initializes the hardware renderer for the specified surface.
@@ -187,7 +189,7 @@
         }
 
         /**
-         * Checks for OpenGL errors. If an error has occured, {@link #destroy()}
+         * Checks for OpenGL errors. If an error has occured, {@link #destroy(boolean)}
          * is invoked and the requested flag is turned off. The error code is
          * also logged as a warning.
          */
@@ -197,7 +199,7 @@
                 if (error != EGL10.EGL_SUCCESS) {
                     // something bad has happened revert to
                     // normal rendering.
-                    destroy();
+                    destroy(true);
                     if (error != EGL11.EGL_CONTEXT_LOST) {
                         // we'll try again if it was context lost
                         setRequested(false);
@@ -217,13 +219,12 @@
                 if (mGl != null) {
                     int err = sEgl.eglGetError();
                     if (err != EGL10.EGL_SUCCESS) {
-                        destroy();
+                        destroy(true);
                         setRequested(false);
                     } else {
-                        if (mCanvas != null) {
-                            destroyCanvas();
+                        if (mCanvas == null) {
+                            mCanvas = createCanvas();
                         }
-                        mCanvas = createCanvas();
                         if (mCanvas != null) {
                             setEnabled(true);
                         } else {
@@ -237,11 +238,6 @@
             return false;
         }
 
-        private void destroyCanvas() {
-            mCanvas.destroy();
-            mCanvas = null;
-        }
-
         abstract GLES20Canvas createCanvas();
 
         void initializeEgl() {
@@ -341,16 +337,16 @@
         }
         
         @Override
-        void destroy() {
+        void destroy(boolean full) {
+            if (full && mCanvas != null) {
+                mCanvas.destroy();
+                mCanvas = null;
+            }
+            
             if (!isEnabled() || mDestroyed) return;
 
             mDestroyed = true;
 
-            checkCurrent();
-            // Destroy the Canvas first in case it needs to use a GL context
-            // to perform its cleanup.
-            destroyCanvas();
-
             sEgl.eglMakeCurrent(sEglDisplay, EGL10.EGL_NO_SURFACE,
                     EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
             sEgl.eglDestroySurface(sEglDisplay, mEglSurface);
@@ -358,12 +354,6 @@
             mEglSurface = null;
             mGl = null;
 
-            // mEgl.eglDestroyContext(mEglDisplay, mEglContext);
-            // mEglContext = null;            
-            // mEgl.eglTerminate(mEglDisplay);
-            // mEgl = null;
-            // mEglDisplay = null;
-
             setEnabled(false);
         }
 
diff --git a/core/java/android/view/ViewRoot.java b/core/java/android/view/ViewRoot.java
index 5999aba..a91d2bd 100644
--- a/core/java/android/view/ViewRoot.java
+++ b/core/java/android/view/ViewRoot.java
@@ -469,7 +469,9 @@
             if (attrs != null &&
                     (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
                 final boolean translucent = attrs.format != PixelFormat.OPAQUE;
-                destroyHardwareRenderer();
+                if (mHwRenderer != null) {
+                    mHwRenderer.destroy(true);
+                }                
                 mHwRenderer = HardwareRenderer.createGlRenderer(2, translucent);
             }
         }
@@ -678,7 +680,9 @@
             attachInfo.mWindowVisibility = viewVisibility;
             host.dispatchWindowVisibilityChanged(viewVisibility);
             if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
-                destroyHardwareRenderer();
+                if (mHwRenderer != null) {
+                    mHwRenderer.destroy(false);
+                }                
             }
             if (viewVisibility == View.GONE) {
                 // After making a window gone, we will count it as being
@@ -1597,9 +1601,11 @@
         mAttachInfo.mRootView = null;
         mAttachInfo.mSurface = null;
 
-        destroyHardwareRenderer();
-        mHwRenderer = null;
-        
+        if (mHwRenderer != null) {
+            mHwRenderer.destroy(true);
+            mHwRenderer = null;
+        }
+
         mSurface.release();
 
         if (mInputChannel != null) {
@@ -1624,12 +1630,6 @@
         }
     }
 
-    private void destroyHardwareRenderer() {
-        if (mHwRenderer != null) {
-            mHwRenderer.destroy();
-        }
-    }
-
     void updateConfiguration(Configuration config, boolean force) {
         if (DEBUG_CONFIGURATION) Log.v(TAG,
                 "Applying new config to window "