Fixed disconnect bug in SurfaceTexture

BufferQueue's disconnect could race with updateTexImage
where invalid buffers could be released.  Additionally
fixed similar bug with setBufferCount.  Tests were added
to stress the disconnect mechanism.

Change-Id: I9afa4c64f3e025984e8a9e8d924852a71d044716
diff --git a/include/gui/BufferQueue.h b/include/gui/BufferQueue.h
index a808216..57b9f8a 100644
--- a/include/gui/BufferQueue.h
+++ b/include/gui/BufferQueue.h
@@ -42,6 +42,7 @@
     enum { NUM_BUFFER_SLOTS = 32 };
     enum { NO_CONNECTED_API = 0 };
     enum { INVALID_BUFFER_SLOT = -1 };
+    enum { STALE_BUFFER_SLOT = 1 };
 
     // ConsumerListener is the interface through which the BufferQueue notifies
     // the consumer of events that the consumer may wish to react to.  Because
@@ -297,7 +298,8 @@
           mTimestamp(0),
           mFrameNumber(0),
           mFence(EGL_NO_SYNC_KHR),
-          mAcquireCalled(false) {
+          mAcquireCalled(false),
+          mNeedsCleanupOnRelease(false) {
             mCrop.makeInvalid();
         }
 
@@ -376,6 +378,9 @@
 
         // Indicates whether this buffer has been seen by a consumer yet
         bool mAcquireCalled;
+
+        // Indicates whether this buffer needs to be cleaned up by consumer
+        bool mNeedsCleanupOnRelease;
     };
 
     // mSlots is the array of buffer slots that must be mirrored on the client