SurfaceTexture: Fully refactored from BufferQueue

SurfaceTexture and BufferQueue are separate objects.

Change-Id: I230bc0ae6f78d0f9b2b5df902f40ab443ed5a055
diff --git a/include/gui/BufferQueue.h b/include/gui/BufferQueue.h
index 8c21a28..46d1854 100644
--- a/include/gui/BufferQueue.h
+++ b/include/gui/BufferQueue.h
@@ -203,6 +203,16 @@
     // when a new frame becomes available.
     void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
 
+    // setDefaultBufferFormat allows the BufferQueue to create
+    // GraphicBuffers of a defaultFormat if no format is specified
+    // in dequeueBuffer
+    status_t setDefaultBufferFormat(uint32_t defaultFormat);
+
+    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer
+    status_t setConsumerUsageBits(uint32_t usage);
+
+    // setTransformHint bakes in rotation to buffers so overlays can be used
+    status_t setTransformHint(uint32_t hint);
 
 private:
     // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
@@ -417,7 +427,19 @@
     // with the surface Texture.
     uint64_t mFrameCounter;
 
+    // mBufferHasBeenQueued is true once a buffer has been queued.  It is reset
+    // by changing the buffer count.
     bool mBufferHasBeenQueued;
+
+    // mDefaultBufferFormat can be set so it will override
+    // the buffer format when it isn't specified in dequeueBuffer
+    uint32_t mDefaultBufferFormat;
+
+    // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
+    uint32_t mConsumerUsageBits;
+
+    // mTransformHint is used to optimize for screen rotations
+    uint32_t mTransformHint;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/include/gui/SurfaceTexture.h b/include/gui/SurfaceTexture.h
index 5531e53..2ab2ab7 100644
--- a/include/gui/SurfaceTexture.h
+++ b/include/gui/SurfaceTexture.h
@@ -39,8 +39,12 @@
 
 class String8;
 
-class SurfaceTexture : public BufferQueue {
+class SurfaceTexture : public virtual RefBase {
 public:
+    // This typedef allows external code to continue referencing
+    // SurfaceTexture::FrameAvailableListener during refactoring
+    typedef  BufferQueue::FrameAvailableListener FrameAvailableListener;
+
 
     // SurfaceTexture constructs a new SurfaceTexture object. tex indicates the
     // name of the OpenGL ES texture to which images are to be streamed. This
@@ -49,14 +53,15 @@
     // enabled. texTarget specifies the OpenGL ES texture target to which the
     // texture will be bound in updateTexImage. useFenceSync specifies whether
     // fences should be used to synchronize access to buffers if that behavior
-    // is enabled at compile-time.
+    // is enabled at compile-time. A custom bufferQueue can be specified
+    // if behavior for queue/dequeue/connect etc needs to be customized.
+    // Otherwise a default BufferQueue will be created and used.
     SurfaceTexture(GLuint tex, bool allowSynchronousMode = true,
-            GLenum texTarget = GL_TEXTURE_EXTERNAL_OES, bool useFenceSync = true);
+            GLenum texTarget = GL_TEXTURE_EXTERNAL_OES, bool useFenceSync = true,
+            const sp<BufferQueue> &bufferQueue = 0);
 
     virtual ~SurfaceTexture();
 
-
-
     // updateTexImage sets the image contents of the target texture to that of
     // the most recently queued buffer.
     //
@@ -152,6 +157,18 @@
     // log messages.
     void setName(const String8& name);
 
+    // These functions call the corresponding BufferQueue implementation
+    // so the refactoring can proceed smoothly
+    status_t setDefaultBufferFormat(uint32_t defaultFormat);
+    status_t setConsumerUsageBits(uint32_t usage);
+    status_t setTransformHint(uint32_t hint);
+    virtual status_t setSynchronousMode(bool enabled);
+    virtual status_t setBufferCount(int bufferCount);
+    virtual status_t connect(int api,
+                uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
+
+    sp<BufferQueue> getBufferQueue() const;
+
     // dump our state in a String
     virtual void dump(String8& result) const;
     virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
@@ -241,7 +258,7 @@
         EGLSyncKHR mFence;
     };
 
-    EGLSlot mEGLSlots[NUM_BUFFER_SLOTS];
+    EGLSlot mEGLSlots[BufferQueue::NUM_BUFFER_SLOTS];
 
     // mAbandoned indicates that the BufferQueue will no longer be used to
     // consume images buffers pushed to it using the ISurfaceTexture interface.
@@ -267,6 +284,10 @@
     // reset mCurrentTexture to INVALID_BUFFER_SLOT.
     int mCurrentTexture;
 
+    // The SurfaceTexture has-a BufferQueue and is responsible for creating this object
+    // if none is supplied
+    sp<BufferQueue> mBufferQueue;
+
 };
 
 // ----------------------------------------------------------------------------
diff --git a/include/gui/SurfaceTextureClient.h b/include/gui/SurfaceTextureClient.h
index aa7fe48..1051bd2 100644
--- a/include/gui/SurfaceTextureClient.h
+++ b/include/gui/SurfaceTextureClient.h
@@ -19,6 +19,7 @@
 
 #include <gui/ISurfaceTexture.h>
 #include <gui/SurfaceTexture.h>
+#include <gui/BufferQueue.h>
 
 #include <ui/ANativeObjectBase.h>
 #include <ui/Region.h>
@@ -34,8 +35,15 @@
     : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase>
 {
 public:
+
     SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture);
 
+    // SurfaceTextureClient is overloaded to assist in refactoring ST and BQ.
+    // SurfaceTexture is no longer an ISurfaceTexture, so client code
+    // calling the original constructor will fail. Thus this convenience method
+    // passes in the surfaceTexture's bufferQueue to the init method.
+    SurfaceTextureClient(const sp<SurfaceTexture>& surfaceTexture);
+
     sp<ISurfaceTexture> getISurfaceTexture() const;
 
 protected:
@@ -94,8 +102,8 @@
     virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
     virtual int unlockAndPost();
 
-    enum { MIN_UNDEQUEUED_BUFFERS = SurfaceTexture::MIN_UNDEQUEUED_BUFFERS };
-    enum { NUM_BUFFER_SLOTS = SurfaceTexture::NUM_BUFFER_SLOTS };
+    enum { MIN_UNDEQUEUED_BUFFERS = BufferQueue::MIN_UNDEQUEUED_BUFFERS };
+    enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
     enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
 
 private: