Home | History | Annotate | Download | only in surfaceflinger
      1 /*
      2  * Copyright (C) 2007 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_SF_SURFACE_H
     18 #define ANDROID_SF_SURFACE_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <utils/KeyedVector.h>
     24 #include <utils/RefBase.h>
     25 #include <utils/threads.h>
     26 
     27 #include <ui/PixelFormat.h>
     28 #include <ui/Region.h>
     29 #include <ui/egl/android_natives.h>
     30 
     31 #include <surfaceflinger/ISurface.h>
     32 #include <surfaceflinger/ISurfaceComposerClient.h>
     33 
     34 #define ANDROID_VIEW_SURFACE_JNI_ID    "mNativeSurface"
     35 
     36 namespace android {
     37 
     38 // ---------------------------------------------------------------------------
     39 
     40 class GraphicBuffer;
     41 class GraphicBufferMapper;
     42 class IOMX;
     43 class Rect;
     44 class Surface;
     45 class SurfaceComposerClient;
     46 class SharedClient;
     47 class SharedBufferClient;
     48 class SurfaceClient;
     49 
     50 // ---------------------------------------------------------------------------
     51 
     52 class SurfaceControl : public RefBase
     53 {
     54 public:
     55     static bool isValid(const sp<SurfaceControl>& surface) {
     56         return (surface != 0) && surface->isValid();
     57     }
     58     bool isValid() {
     59         return mToken>=0 && mClient!=0;
     60     }
     61     static bool isSameSurface(
     62             const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
     63 
     64     uint32_t    getFlags() const { return mFlags; }
     65     uint32_t    getIdentity() const { return mIdentity; }
     66 
     67     // release surface data from java
     68     void        clear();
     69 
     70     status_t    setLayer(int32_t layer);
     71     status_t    setPosition(int32_t x, int32_t y);
     72     status_t    setSize(uint32_t w, uint32_t h);
     73     status_t    hide();
     74     status_t    show(int32_t layer = -1);
     75     status_t    freeze();
     76     status_t    unfreeze();
     77     status_t    setFlags(uint32_t flags, uint32_t mask);
     78     status_t    setTransparentRegionHint(const Region& transparent);
     79     status_t    setAlpha(float alpha=1.0f);
     80     status_t    setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
     81     status_t    setFreezeTint(uint32_t tint);
     82 
     83     static status_t writeSurfaceToParcel(
     84             const sp<SurfaceControl>& control, Parcel* parcel);
     85 
     86     sp<Surface> getSurface() const;
     87 
     88 private:
     89     // can't be copied
     90     SurfaceControl& operator = (SurfaceControl& rhs);
     91     SurfaceControl(const SurfaceControl& rhs);
     92 
     93 
     94     friend class SurfaceComposerClient;
     95 
     96     // camera and camcorder need access to the ISurface binder interface for preview
     97     friend class Camera;
     98     friend class MediaRecorder;
     99     // mediaplayer needs access to ISurface for display
    100     friend class MediaPlayer;
    101     // for testing
    102     friend class Test;
    103     const sp<ISurface>& getISurface() const { return mSurface; }
    104 
    105 
    106     friend class Surface;
    107 
    108     SurfaceControl(
    109             const sp<SurfaceComposerClient>& client,
    110             const sp<ISurface>& surface,
    111             const ISurfaceComposerClient::surface_data_t& data,
    112             uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
    113 
    114     ~SurfaceControl();
    115 
    116     status_t validate() const;
    117     void destroy();
    118 
    119     sp<SurfaceComposerClient>   mClient;
    120     sp<ISurface>                mSurface;
    121     SurfaceID                   mToken;
    122     uint32_t                    mIdentity;
    123     uint32_t                    mWidth;
    124     uint32_t                    mHeight;
    125     PixelFormat                 mFormat;
    126     uint32_t                    mFlags;
    127     mutable Mutex               mLock;
    128 
    129     mutable sp<Surface>         mSurfaceData;
    130 };
    131 
    132 // ---------------------------------------------------------------------------
    133 
    134 class Surface
    135     : public EGLNativeBase<ANativeWindow, Surface, RefBase>
    136 {
    137 public:
    138     struct SurfaceInfo {
    139         uint32_t    w;
    140         uint32_t    h;
    141         uint32_t    s;
    142         uint32_t    usage;
    143         PixelFormat format;
    144         void*       bits;
    145         uint32_t    reserved[2];
    146     };
    147 
    148     static status_t writeToParcel(
    149             const sp<Surface>& control, Parcel* parcel);
    150 
    151     static sp<Surface> readFromParcel(const Parcel& data);
    152 
    153     static bool isValid(const sp<Surface>& surface) {
    154         return (surface != 0) && surface->isValid();
    155     }
    156 
    157     bool        isValid();
    158     uint32_t    getFlags() const    { return mFlags; }
    159     uint32_t    getIdentity() const { return mIdentity; }
    160 
    161     // the lock/unlock APIs must be used from the same thread
    162     status_t    lock(SurfaceInfo* info, bool blocking = true);
    163     status_t    lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
    164     status_t    unlockAndPost();
    165 
    166     // setSwapRectangle() is intended to be used by GL ES clients
    167     void        setSwapRectangle(const Rect& r);
    168 
    169 
    170 private:
    171     /*
    172      * Android frameworks friends
    173      * (eventually this should go away and be replaced by proper APIs)
    174      */
    175     // camera and camcorder need access to the ISurface binder interface for preview
    176     friend class Camera;
    177     friend class MediaRecorder;
    178     // MediaPlayer needs access to ISurface for display
    179     friend class MediaPlayer;
    180     friend class IOMX;
    181     // this is just to be able to write some unit tests
    182     friend class Test;
    183 
    184 private:
    185     friend class SurfaceComposerClient;
    186     friend class SurfaceControl;
    187 
    188     // can't be copied
    189     Surface& operator = (Surface& rhs);
    190     Surface(const Surface& rhs);
    191 
    192     Surface(const sp<SurfaceControl>& control);
    193     Surface(const Parcel& data, const sp<IBinder>& ref);
    194     ~Surface();
    195 
    196 
    197     /*
    198      *  ANativeWindow hooks
    199      */
    200     static int setSwapInterval(ANativeWindow* window, int interval);
    201     static int dequeueBuffer(ANativeWindow* window, android_native_buffer_t** buffer);
    202     static int cancelBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
    203     static int lockBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
    204     static int queueBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
    205     static int query(ANativeWindow* window, int what, int* value);
    206     static int perform(ANativeWindow* window, int operation, ...);
    207 
    208     int dequeueBuffer(android_native_buffer_t** buffer);
    209     int lockBuffer(android_native_buffer_t* buffer);
    210     int queueBuffer(android_native_buffer_t* buffer);
    211     int cancelBuffer(android_native_buffer_t* buffer);
    212     int query(int what, int* value);
    213     int perform(int operation, va_list args);
    214 
    215     void dispatch_setUsage(va_list args);
    216     int  dispatch_connect(va_list args);
    217     int  dispatch_disconnect(va_list args);
    218     int  dispatch_crop(va_list args);
    219     int  dispatch_set_buffer_count(va_list args);
    220     int  dispatch_set_buffers_geometry(va_list args);
    221     int  dispatch_set_buffers_transform(va_list args);
    222 
    223     void setUsage(uint32_t reqUsage);
    224     int  connect(int api);
    225     int  disconnect(int api);
    226     int  crop(Rect const* rect);
    227     int  setBufferCount(int bufferCount);
    228     int  setBuffersGeometry(int w, int h, int format);
    229     int  setBuffersTransform(int transform);
    230 
    231     /*
    232      *  private stuff...
    233      */
    234     void init();
    235     status_t validate() const;
    236     sp<ISurface> getISurface() const;
    237 
    238     inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
    239     inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
    240 
    241     status_t getBufferLocked(int index,
    242             uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
    243     int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
    244 
    245     int getConnectedApi() const;
    246 
    247     bool needNewBuffer(int bufIdx,
    248             uint32_t *pWidth, uint32_t *pHeight,
    249             uint32_t *pFormat, uint32_t *pUsage) const;
    250 
    251     static void cleanCachedSurfacesLocked();
    252 
    253     class BufferInfo {
    254         uint32_t mWidth;
    255         uint32_t mHeight;
    256         uint32_t mFormat;
    257         uint32_t mUsage;
    258         mutable uint32_t mDirty;
    259         enum {
    260             GEOMETRY = 0x01
    261         };
    262     public:
    263         BufferInfo();
    264         void set(uint32_t w, uint32_t h, uint32_t format);
    265         void set(uint32_t usage);
    266         void get(uint32_t *pWidth, uint32_t *pHeight,
    267                 uint32_t *pFormat, uint32_t *pUsage) const;
    268         bool validateBuffer(const sp<GraphicBuffer>& buffer) const;
    269     };
    270 
    271     // constants
    272     GraphicBufferMapper&        mBufferMapper;
    273     SurfaceClient&              mClient;
    274     SharedBufferClient*         mSharedBufferClient;
    275     status_t                    mInitCheck;
    276     sp<ISurface>                mSurface;
    277     uint32_t                    mIdentity;
    278     PixelFormat                 mFormat;
    279     uint32_t                    mFlags;
    280 
    281     // protected by mSurfaceLock
    282     Rect                        mSwapRectangle;
    283     int                         mConnected;
    284     Rect                        mNextBufferCrop;
    285     uint32_t                    mNextBufferTransform;
    286     BufferInfo                  mBufferInfo;
    287 
    288     // protected by mSurfaceLock. These are also used from lock/unlock
    289     // but in that case, they must be called form the same thread.
    290     mutable Region              mDirtyRegion;
    291 
    292     // must be used from the lock/unlock thread
    293     sp<GraphicBuffer>           mLockedBuffer;
    294     sp<GraphicBuffer>           mPostedBuffer;
    295     mutable Region              mOldDirtyRegion;
    296     bool                        mReserved;
    297 
    298     // only used from dequeueBuffer()
    299     Vector< sp<GraphicBuffer> > mBuffers;
    300 
    301     // query() must be called from dequeueBuffer() thread
    302     uint32_t                    mWidth;
    303     uint32_t                    mHeight;
    304 
    305     // Inherently thread-safe
    306     mutable Mutex               mSurfaceLock;
    307     mutable Mutex               mApiLock;
    308 
    309     // A cache of Surface objects that have been deserialized into this process.
    310     static Mutex sCachedSurfacesLock;
    311     static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
    312 };
    313 
    314 }; // namespace android
    315 
    316 #endif // ANDROID_SF_SURFACE_H
    317 
    318