Transfer HWC release fences to BufferQueue

After a HWC set, each SurfaceFlinger Layer retrieves the release fence
HWC returned and gives it to the layer's SurfaceTexture. The
SurfaceTexture accumulates the fences into a merged fence until the
next updateTexImage, then passes the merged fence to the BufferQueue
in releaseBuffer.

In a follow-on change, BufferQueue will return the fence along with
the buffer slot in dequeueBuffer. For now, dequeueBuffer waits for the
fence to signal before returning.

The releaseFence default value for BufferQueue::releaseBuffer() is
temporary to avoid transient build breaks with a multi-project
checkin. It'll disappear in the next change.

Change-Id: Iaa9a0d5775235585d9cbf453d3a64623d08013d9
diff --git a/include/gui/BufferQueue.h b/include/gui/BufferQueue.h
index 04a2f52..6db2c87 100644
--- a/include/gui/BufferQueue.h
+++ b/include/gui/BufferQueue.h
@@ -23,6 +23,7 @@
 #include <gui/IGraphicBufferAlloc.h>
 #include <gui/ISurfaceTexture.h>
 
+#include <ui/Fence.h>
 #include <ui/GraphicBuffer.h>
 
 #include <utils/String8.h>
@@ -218,7 +219,8 @@
     //
     // Note that the dependencies on EGL will be removed once we switch to using
     // the Android HW Sync HAL.
-    status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence);
+    status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence,
+            const sp<Fence>& releaseFence = Fence::NO_FENCE);
 
     // consumerConnect connects a consumer to the BufferQueue.  Only one
     // consumer may be connected, and when that consumer disconnects the
@@ -378,6 +380,10 @@
         // on a compile-time option) set to a new sync object in updateTexImage.
         EGLSyncKHR mFence;
 
+        // mReleaseFence is a fence which must signal before the contents of
+        // the buffer associated with this buffer slot may be overwritten.
+        sp<Fence> mReleaseFence;
+
         // Indicates whether this buffer has been seen by a consumer yet
         bool mAcquireCalled;
 
diff --git a/include/gui/SurfaceTexture.h b/include/gui/SurfaceTexture.h
index 2635e2f..622724e 100644
--- a/include/gui/SurfaceTexture.h
+++ b/include/gui/SurfaceTexture.h
@@ -91,6 +91,14 @@
     // target texture belongs is bound to the calling thread.
     status_t updateTexImage();
 
+    // setReleaseFence stores a fence file descriptor that will signal when the
+    // current buffer is no longer being read. This fence will be returned to
+    // the producer when the current buffer is released by updateTexImage().
+    // Multiple fences can be set for a given buffer; they will be merged into
+    // a single union fence. The SurfaceTexture will close the file descriptor
+    // when finished with it.
+    void setReleaseFence(int fenceFd);
+
     // setBufferCountServer set the buffer count. If the client has requested
     // a buffer count using setBufferCount, the server-buffer count will
     // take effect once the client sets the count back to zero.
@@ -349,6 +357,13 @@
         // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
         // on a compile-time option) set to a new sync object in updateTexImage.
         EGLSyncKHR mFence;
+
+        // mReleaseFence is a fence which will signal when the buffer
+        // associated with this buffer slot is no longer being used by the
+        // consumer and can be overwritten. The buffer can be dequeued before
+        // the fence signals; the producer is responsible for delaying writes
+        // until it signals.
+        sp<Fence> mReleaseFence;
     };
 
     // mEglDisplay is the EGLDisplay with which this SurfaceTexture is currently