Surfaces are now destroyed properly in SurfaceFlinger.

First, the window manager tells us when a surface is no longer needed. At this point, several things happen:
- the surface is removed from the active/visible list
- it is added to a purgatory list, where it waits for all clients to release their reference
- it destroys all data/state that can be spared

Later, when all clients are done, the remains of the Surface are disposed off: it is removed from the purgatory and destroyed.
In particular its gralloc buffers are destroyed at that point (when we're sure nobody is using them anymore).
diff --git a/libs/surfaceflinger/Layer.cpp b/libs/surfaceflinger/Layer.cpp
index 5fdec3f..4e3f3a9 100644
--- a/libs/surfaceflinger/Layer.cpp
+++ b/libs/surfaceflinger/Layer.cpp
@@ -61,15 +61,23 @@
 
 Layer::~Layer()
 {
+    destroy();
+    // the actual buffers will be destroyed here
+}
+
+void Layer::destroy()
+{
     for (int i=0 ; i<NUM_BUFFERS ; i++) {
         if (mTextures[i].name != -1U) {
             // FIXME: this was originally to work-around a bug in the
             // adreno driver. this should be fixed now.
             deletedTextures.add(mTextures[i].name);
+            mTextures[i].name = -1U;
         }
         if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
             EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
             eglDestroyImageKHR(dpy, mTextures[i].image);
+            mTextures[i].image = EGL_NO_IMAGE_KHR;
         }
     }
 }
@@ -89,7 +97,9 @@
 
 status_t Layer::ditch()
 {
+    // the layer is not on screen anymore. free as much resources as possible
     mSurface.clear();
+    destroy();
     return NO_ERROR;
 }