Refactoring: Rename SurfaceTextureClient to Surface

Change-Id: Ibed34175ae273608393aaa5f0a7df207dc40d709
diff --git a/include/gui/Surface.h b/include/gui/Surface.h
index ec75d4e..4300a0f 100644
--- a/include/gui/Surface.h
+++ b/include/gui/Surface.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007 The Android Open Source Project
+ * Copyright (C) 2010 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,155 +17,224 @@
 #ifndef ANDROID_GUI_SURFACE_H
 #define ANDROID_GUI_SURFACE_H
 
-#include <stdint.h>
-#include <sys/types.h>
+#include <gui/IGraphicBufferProducer.h>
+#include <gui/GLConsumer.h>
+#include <gui/BufferQueue.h>
 
-#include <utils/KeyedVector.h>
-#include <utils/RefBase.h>
-#include <utils/threads.h>
-
-#include <ui/PixelFormat.h>
+#include <ui/ANativeObjectBase.h>
 #include <ui/Region.h>
 
-#include <gui/SurfaceTextureClient.h>
-#include <gui/ISurface.h>
-#include <gui/ISurfaceComposerClient.h>
+#include <utils/RefBase.h>
+#include <utils/threads.h>
+#include <utils/KeyedVector.h>
+
+struct ANativeWindow_Buffer;
 
 namespace android {
 
-// ---------------------------------------------------------------------------
-
-class IGraphicBufferProducer;
-class Surface;
-class SurfaceComposerClient;
-
-// ---------------------------------------------------------------------------
-
-class SurfaceControl : public RefBase
+/*
+ * An implementation of ANativeWindow that feeds graphics buffers into a
+ * BufferQueue.
+ *
+ * This is typically used by programs that want to render frames through
+ * some means (maybe OpenGL, a software renderer, or a hardware decoder)
+ * and have the frames they create forwarded to SurfaceFlinger for
+ * compositing.  For example, a video decoder could render a frame and call
+ * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by
+ * Surface.  Surface then forwards the buffers through Binder IPC
+ * to the BufferQueue's producer interface, providing the new frame to a
+ * consumer such as GLConsumer.
+ */
+class Surface
+    : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
 {
 public:
-    static bool isValid(const sp<SurfaceControl>& surface) {
-        return (surface != 0) && surface->isValid();
-    }
-    bool isValid() {
-        return mSurface!=0 && mClient!=0;
-    }
-    static bool isSameSurface(
-            const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
-        
-    // release surface data from java
-    void        clear();
-    
-    status_t    setLayerStack(int32_t layerStack);
-    status_t    setLayer(int32_t layer);
-    status_t    setPosition(int32_t x, int32_t y);
-    status_t    setSize(uint32_t w, uint32_t h);
-    status_t    hide();
-    status_t    show();
-    status_t    setFlags(uint32_t flags, uint32_t mask);
-    status_t    setTransparentRegionHint(const Region& transparent);
-    status_t    setAlpha(float alpha=1.0f);
-    status_t    setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
-    status_t    setCrop(const Rect& crop);
 
-    static status_t writeSurfaceToParcel(
-            const sp<SurfaceControl>& control, Parcel* parcel);
+    Surface(const sp<IGraphicBufferProducer>& bufferProducer);
 
-    sp<Surface> getSurface() const;
+    sp<IGraphicBufferProducer> getIGraphicBufferProducer() const;
+
+    static status_t writeToParcel(const sp<Surface>& surface, Parcel* parcel);
+    static sp<Surface> readFromParcel(const Parcel& data);
+
+    // FIXME: temporary for source compatibility...
+    sp<IGraphicBufferProducer> getISurfaceTexture() const {
+        return getIGraphicBufferProducer();
+    }
+
+protected:
+    Surface();
+    virtual ~Surface();
+    void setIGraphicBufferProducer(const sp<IGraphicBufferProducer>& bufferProducer);
 
 private:
     // can't be copied
-    SurfaceControl& operator = (SurfaceControl& rhs);
-    SurfaceControl(const SurfaceControl& rhs);
+    Surface& operator = (const Surface& rhs);
+    Surface(const Surface& rhs);
+    void init();
 
-    friend class SurfaceComposerClient;
-    friend class Surface;
+    // ANativeWindow hooks
+    static int hook_cancelBuffer(ANativeWindow* window,
+            ANativeWindowBuffer* buffer, int fenceFd);
+    static int hook_dequeueBuffer(ANativeWindow* window,
+            ANativeWindowBuffer** buffer, int* fenceFd);
+    static int hook_perform(ANativeWindow* window, int operation, ...);
+    static int hook_query(const ANativeWindow* window, int what, int* value);
+    static int hook_queueBuffer(ANativeWindow* window,
+            ANativeWindowBuffer* buffer, int fenceFd);
+    static int hook_setSwapInterval(ANativeWindow* window, int interval);
 
-    SurfaceControl(
-            const sp<SurfaceComposerClient>& client,
-            const sp<ISurface>& surface);
+    static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
+            ANativeWindowBuffer* buffer);
+    static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
+            ANativeWindowBuffer** buffer);
+    static int hook_lockBuffer_DEPRECATED(ANativeWindow* window,
+            ANativeWindowBuffer* buffer);
+    static int hook_queueBuffer_DEPRECATED(ANativeWindow* window,
+            ANativeWindowBuffer* buffer);
 
-    ~SurfaceControl();
+    int dispatchConnect(va_list args);
+    int dispatchDisconnect(va_list args);
+    int dispatchSetBufferCount(va_list args);
+    int dispatchSetBuffersGeometry(va_list args);
+    int dispatchSetBuffersDimensions(va_list args);
+    int dispatchSetBuffersUserDimensions(va_list args);
+    int dispatchSetBuffersFormat(va_list args);
+    int dispatchSetScalingMode(va_list args);
+    int dispatchSetBuffersTransform(va_list args);
+    int dispatchSetBuffersTimestamp(va_list args);
+    int dispatchSetCrop(va_list args);
+    int dispatchSetPostTransformCrop(va_list args);
+    int dispatchSetUsage(va_list args);
+    int dispatchLock(va_list args);
+    int dispatchUnlockAndPost(va_list args);
 
-    status_t validate() const;
-    void destroy();
-    
-    sp<SurfaceComposerClient>   mClient;
-    sp<IBinder>                 mSurface;
-    mutable Mutex               mLock;
-    mutable sp<Surface>         mSurfaceData;
-};
-    
-// ---------------------------------------------------------------------------
+protected:
+    virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
+    virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
+    virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
+    virtual int perform(int operation, va_list args);
+    virtual int query(int what, int* value) const;
+    virtual int setSwapInterval(int interval);
 
-/*
- * This is a small wrapper around SurfaceTextureClient.
- *
- * TODO: rename and/or merge with STC.
- */
-class Surface : public SurfaceTextureClient
-{
+    virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer);
+
+    virtual int connect(int api);
+    virtual int disconnect(int api);
+    virtual int setBufferCount(int bufferCount);
+    virtual int setBuffersDimensions(int w, int h);
+    virtual int setBuffersUserDimensions(int w, int h);
+    virtual int setBuffersFormat(int format);
+    virtual int setScalingMode(int mode);
+    virtual int setBuffersTransform(int transform);
+    virtual int setBuffersTimestamp(int64_t timestamp);
+    virtual int setCrop(Rect const* rect);
+    virtual int setUsage(uint32_t reqUsage);
+
 public:
-    struct SurfaceInfo {
-        uint32_t    w;
-        uint32_t    h;
-        uint32_t    s;
-        uint32_t    usage;
-        PixelFormat format;
-        void*       bits;
-        uint32_t    reserved[2];
+    virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
+    virtual int unlockAndPost();
+
+protected:
+    enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
+    enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
+
+private:
+    void freeAllBuffers();
+    int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
+
+    struct BufferSlot {
+        sp<GraphicBuffer> buffer;
+        Region dirtyRegion;
     };
 
-    explicit Surface(const sp<IGraphicBufferProducer>& bp);
+    // mSurfaceTexture is the interface to the surface texture server. All
+    // operations on the surface texture client ultimately translate into
+    // interactions with the server using this interface.
+    // TODO: rename to mBufferProducer
+    sp<IGraphicBufferProducer> mGraphicBufferProducer;
 
-    static status_t writeToParcel(const sp<Surface>& control, Parcel* parcel);
+    // mSlots stores the buffers that have been allocated for each buffer slot.
+    // It is initialized to null pointers, and gets filled in with the result of
+    // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a
+    // slot that has not yet been used. The buffer allocated to a slot will also
+    // be replaced if the requested buffer usage or geometry differs from that
+    // of the buffer allocated to a slot.
+    BufferSlot mSlots[NUM_BUFFER_SLOTS];
 
-    static sp<Surface> readFromParcel(const Parcel& data);
-    static bool isValid(const sp<Surface>& surface) {
-        return (surface != 0) && surface->isValid();
-    }
+    // mReqWidth is the buffer width that will be requested at the next dequeue
+    // operation. It is initialized to 1.
+    uint32_t mReqWidth;
 
-    bool        isValid();
-    uint32_t    getIdentity() const { return mIdentity; }
-    sp<IGraphicBufferProducer> getSurfaceTexture();     // TODO: rename this
+    // mReqHeight is the buffer height that will be requested at the next
+    // dequeue operation. It is initialized to 1.
+    uint32_t mReqHeight;
 
-    // the lock/unlock APIs must be used from the same thread
-    status_t    lock(SurfaceInfo* info, Region* dirty = NULL);
-    status_t    unlockAndPost();
+    // mReqFormat is the buffer pixel format that will be requested at the next
+    // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
+    uint32_t mReqFormat;
 
-    sp<IBinder> asBinder() const;
+    // mReqUsage is the set of buffer usage flags that will be requested
+    // at the next deuque operation. It is initialized to 0.
+    uint32_t mReqUsage;
 
-private:
-    // this is just to be able to write some unit tests
-    friend class Test;
-    friend class SurfaceControl;
+    // mTimestamp is the timestamp that will be used for the next buffer queue
+    // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
+    // a timestamp is auto-generated when queueBuffer is called.
+    int64_t mTimestamp;
 
-    // can't be copied
-    Surface& operator = (Surface& rhs);
-    Surface(const Surface& rhs);
+    // mCrop is the crop rectangle that will be used for the next buffer
+    // that gets queued. It is set by calling setCrop.
+    Rect mCrop;
 
-    explicit Surface(const sp<SurfaceControl>& control);
-    Surface(const Parcel& data, const sp<IBinder>& ref);
-    ~Surface();
+    // mScalingMode is the scaling mode that will be used for the next
+    // buffers that get queued. It is set by calling setScalingMode.
+    int mScalingMode;
 
-    /*
-     *  private stuff...
-     */
-    void init(const sp<IGraphicBufferProducer>& bufferProducer);
+    // mTransform is the transform identifier that will be used for the next
+    // buffer that gets queued. It is set by calling setTransform.
+    uint32_t mTransform;
 
-    static void cleanCachedSurfacesLocked();
+     // mDefaultWidth is default width of the buffers, regardless of the
+     // native_window_set_buffers_dimensions call.
+     uint32_t mDefaultWidth;
 
-    virtual int query(int what, int* value) const;
+     // mDefaultHeight is default height of the buffers, regardless of the
+     // native_window_set_buffers_dimensions call.
+     uint32_t mDefaultHeight;
 
-    // constants
-    sp<ISurface>                mSurface;
-    uint32_t                    mIdentity;
+     // mUserWidth, if non-zero, is an application-specified override
+     // of mDefaultWidth.  This is lower priority than the width set by
+     // native_window_set_buffers_dimensions.
+     uint32_t mUserWidth;
 
-    // A cache of Surface objects that have been deserialized into this process.
-    static Mutex sCachedSurfacesLock;
-    static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
+     // mUserHeight, if non-zero, is an application-specified override
+     // of mDefaultHeight.  This is lower priority than the height set
+     // by native_window_set_buffers_dimensions.
+     uint32_t mUserHeight;
+
+    // mTransformHint is the transform probably applied to buffers of this
+    // window. this is only a hint, actual transform may differ.
+    uint32_t mTransformHint;
+
+    // mConsumerRunningBehind whether the consumer is running more than
+    // one buffer behind the producer.
+    mutable bool mConsumerRunningBehind;
+
+    // mMutex is the mutex used to prevent concurrent access to the member
+    // variables of Surface objects. It must be locked whenever the
+    // member variables are accessed.
+    mutable Mutex mMutex;
+
+    // must be used from the lock/unlock thread
+    sp<GraphicBuffer>           mLockedBuffer;
+    sp<GraphicBuffer>           mPostedBuffer;
+    bool                        mConnectedToCpu;
+
+    // must be accessed from lock/unlock thread only
+    Region mDirtyRegion;
 };
 
 }; // namespace android
 
-#endif // ANDROID_GUI_SURFACE_H
+#endif  // ANDROID_GUI_SURFACE_H