Home | History | Annotate | Download | only in gui
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_GUI_SURFACE_H
     18 #define ANDROID_GUI_SURFACE_H
     19 
     20 #include <gui/IGraphicBufferProducer.h>
     21 #include <gui/GLConsumer.h>
     22 #include <gui/BufferQueue.h>
     23 
     24 #include <ui/ANativeObjectBase.h>
     25 #include <ui/Region.h>
     26 
     27 #include <utils/RefBase.h>
     28 #include <utils/threads.h>
     29 #include <utils/KeyedVector.h>
     30 
     31 struct ANativeWindow_Buffer;
     32 
     33 namespace android {
     34 
     35 /*
     36  * An implementation of ANativeWindow that feeds graphics buffers into a
     37  * BufferQueue.
     38  *
     39  * This is typically used by programs that want to render frames through
     40  * some means (maybe OpenGL, a software renderer, or a hardware decoder)
     41  * and have the frames they create forwarded to SurfaceFlinger for
     42  * compositing.  For example, a video decoder could render a frame and call
     43  * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by
     44  * Surface.  Surface then forwards the buffers through Binder IPC
     45  * to the BufferQueue's producer interface, providing the new frame to a
     46  * consumer such as GLConsumer.
     47  */
     48 class Surface
     49     : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
     50 {
     51 public:
     52 
     53     /*
     54      * creates a Surface from the given IGraphicBufferProducer (which concrete
     55      * implementation is a BufferQueue).
     56      *
     57      * Surface is mainly state-less while it's disconnected, it can be
     58      * viewed as a glorified IGraphicBufferProducer holder. It's therefore
     59      * safe to create other Surfaces from the same IGraphicBufferProducer.
     60      *
     61      * However, once a Surface is connected, it'll prevent other Surfaces
     62      * referring to the same IGraphicBufferProducer to become connected and
     63      * therefore prevent them to be used as actual producers of buffers.
     64      */
     65     Surface(const sp<IGraphicBufferProducer>& bufferProducer);
     66 
     67     /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this
     68      * Surface was created with. Usually it's an error to use the
     69      * IGraphicBufferProducer while the Surface is connected.
     70      */
     71     sp<IGraphicBufferProducer> getIGraphicBufferProducer() const;
     72 
     73     /* convenience function to check that the given surface is non NULL as
     74      * well as its IGraphicBufferProducer */
     75     static bool isValid(const sp<Surface>& surface) {
     76         return surface != NULL && surface->getIGraphicBufferProducer() != NULL;
     77     }
     78 
     79 protected:
     80     virtual ~Surface();
     81 
     82 private:
     83     // can't be copied
     84     Surface& operator = (const Surface& rhs);
     85     Surface(const Surface& rhs);
     86 
     87     // ANativeWindow hooks
     88     static int hook_cancelBuffer(ANativeWindow* window,
     89             ANativeWindowBuffer* buffer, int fenceFd);
     90     static int hook_dequeueBuffer(ANativeWindow* window,
     91             ANativeWindowBuffer** buffer, int* fenceFd);
     92     static int hook_perform(ANativeWindow* window, int operation, ...);
     93     static int hook_query(const ANativeWindow* window, int what, int* value);
     94     static int hook_queueBuffer(ANativeWindow* window,
     95             ANativeWindowBuffer* buffer, int fenceFd);
     96     static int hook_setSwapInterval(ANativeWindow* window, int interval);
     97 
     98     static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
     99             ANativeWindowBuffer* buffer);
    100     static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
    101             ANativeWindowBuffer** buffer);
    102     static int hook_lockBuffer_DEPRECATED(ANativeWindow* window,
    103             ANativeWindowBuffer* buffer);
    104     static int hook_queueBuffer_DEPRECATED(ANativeWindow* window,
    105             ANativeWindowBuffer* buffer);
    106 
    107     int dispatchConnect(va_list args);
    108     int dispatchDisconnect(va_list args);
    109     int dispatchSetBufferCount(va_list args);
    110     int dispatchSetBuffersGeometry(va_list args);
    111     int dispatchSetBuffersDimensions(va_list args);
    112     int dispatchSetBuffersUserDimensions(va_list args);
    113     int dispatchSetBuffersFormat(va_list args);
    114     int dispatchSetScalingMode(va_list args);
    115     int dispatchSetBuffersTransform(va_list args);
    116     int dispatchSetBuffersTimestamp(va_list args);
    117     int dispatchSetCrop(va_list args);
    118     int dispatchSetPostTransformCrop(va_list args);
    119     int dispatchSetUsage(va_list args);
    120     int dispatchLock(va_list args);
    121     int dispatchUnlockAndPost(va_list args);
    122 
    123 protected:
    124     virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
    125     virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
    126     virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
    127     virtual int perform(int operation, va_list args);
    128     virtual int query(int what, int* value) const;
    129     virtual int setSwapInterval(int interval);
    130 
    131     virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer);
    132 
    133     virtual int connect(int api);
    134     virtual int disconnect(int api);
    135     virtual int setBufferCount(int bufferCount);
    136     virtual int setBuffersDimensions(int w, int h);
    137     virtual int setBuffersUserDimensions(int w, int h);
    138     virtual int setBuffersFormat(int format);
    139     virtual int setScalingMode(int mode);
    140     virtual int setBuffersTransform(int transform);
    141     virtual int setBuffersTimestamp(int64_t timestamp);
    142     virtual int setCrop(Rect const* rect);
    143     virtual int setUsage(uint32_t reqUsage);
    144 
    145 public:
    146     virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
    147     virtual int unlockAndPost();
    148 
    149 protected:
    150     enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
    151     enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
    152 
    153 private:
    154     void freeAllBuffers();
    155     int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
    156 
    157     struct BufferSlot {
    158         sp<GraphicBuffer> buffer;
    159         Region dirtyRegion;
    160     };
    161 
    162     // mSurfaceTexture is the interface to the surface texture server. All
    163     // operations on the surface texture client ultimately translate into
    164     // interactions with the server using this interface.
    165     // TODO: rename to mBufferProducer
    166     sp<IGraphicBufferProducer> mGraphicBufferProducer;
    167 
    168     // mSlots stores the buffers that have been allocated for each buffer slot.
    169     // It is initialized to null pointers, and gets filled in with the result of
    170     // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a
    171     // slot that has not yet been used. The buffer allocated to a slot will also
    172     // be replaced if the requested buffer usage or geometry differs from that
    173     // of the buffer allocated to a slot.
    174     BufferSlot mSlots[NUM_BUFFER_SLOTS];
    175 
    176     // mReqWidth is the buffer width that will be requested at the next dequeue
    177     // operation. It is initialized to 1.
    178     uint32_t mReqWidth;
    179 
    180     // mReqHeight is the buffer height that will be requested at the next
    181     // dequeue operation. It is initialized to 1.
    182     uint32_t mReqHeight;
    183 
    184     // mReqFormat is the buffer pixel format that will be requested at the next
    185     // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
    186     uint32_t mReqFormat;
    187 
    188     // mReqUsage is the set of buffer usage flags that will be requested
    189     // at the next deuque operation. It is initialized to 0.
    190     uint32_t mReqUsage;
    191 
    192     // mTimestamp is the timestamp that will be used for the next buffer queue
    193     // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
    194     // a timestamp is auto-generated when queueBuffer is called.
    195     int64_t mTimestamp;
    196 
    197     // mCrop is the crop rectangle that will be used for the next buffer
    198     // that gets queued. It is set by calling setCrop.
    199     Rect mCrop;
    200 
    201     // mScalingMode is the scaling mode that will be used for the next
    202     // buffers that get queued. It is set by calling setScalingMode.
    203     int mScalingMode;
    204 
    205     // mTransform is the transform identifier that will be used for the next
    206     // buffer that gets queued. It is set by calling setTransform.
    207     uint32_t mTransform;
    208 
    209      // mDefaultWidth is default width of the buffers, regardless of the
    210      // native_window_set_buffers_dimensions call.
    211      uint32_t mDefaultWidth;
    212 
    213      // mDefaultHeight is default height of the buffers, regardless of the
    214      // native_window_set_buffers_dimensions call.
    215      uint32_t mDefaultHeight;
    216 
    217      // mUserWidth, if non-zero, is an application-specified override
    218      // of mDefaultWidth.  This is lower priority than the width set by
    219      // native_window_set_buffers_dimensions.
    220      uint32_t mUserWidth;
    221 
    222      // mUserHeight, if non-zero, is an application-specified override
    223      // of mDefaultHeight.  This is lower priority than the height set
    224      // by native_window_set_buffers_dimensions.
    225      uint32_t mUserHeight;
    226 
    227     // mTransformHint is the transform probably applied to buffers of this
    228     // window. this is only a hint, actual transform may differ.
    229     uint32_t mTransformHint;
    230 
    231     // mConsumerRunningBehind whether the consumer is running more than
    232     // one buffer behind the producer.
    233     mutable bool mConsumerRunningBehind;
    234 
    235     // mMutex is the mutex used to prevent concurrent access to the member
    236     // variables of Surface objects. It must be locked whenever the
    237     // member variables are accessed.
    238     mutable Mutex mMutex;
    239 
    240     // must be used from the lock/unlock thread
    241     sp<GraphicBuffer>           mLockedBuffer;
    242     sp<GraphicBuffer>           mPostedBuffer;
    243     bool                        mConnectedToCpu;
    244 
    245     // must be accessed from lock/unlock thread only
    246     Region mDirtyRegion;
    247 };
    248 
    249 }; // namespace android
    250 
    251 #endif  // ANDROID_GUI_SURFACE_H
    252