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_SURFACE_FLINGER_H
     18 #define ANDROID_SURFACE_FLINGER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <utils/SortedVector.h>
     24 #include <utils/KeyedVector.h>
     25 #include <utils/threads.h>
     26 #include <utils/Atomic.h>
     27 #include <utils/Errors.h>
     28 #include <utils/RefBase.h>
     29 
     30 #include <binder/IMemory.h>
     31 #include <binder/Permission.h>
     32 #include <binder/BinderService.h>
     33 
     34 #include <ui/PixelFormat.h>
     35 #include <surfaceflinger/ISurfaceComposer.h>
     36 #include <surfaceflinger/ISurfaceComposerClient.h>
     37 
     38 #include "Barrier.h"
     39 #include "Layer.h"
     40 
     41 #include "MessageQueue.h"
     42 
     43 namespace android {
     44 
     45 // ---------------------------------------------------------------------------
     46 
     47 class Client;
     48 class DisplayHardware;
     49 class FreezeLock;
     50 class Layer;
     51 class LayerBlur;
     52 class LayerDim;
     53 class LayerBuffer;
     54 
     55 #define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
     56 #define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
     57 
     58 // ---------------------------------------------------------------------------
     59 
     60 class Client : public BnSurfaceComposerClient
     61 {
     62 public:
     63         Client(const sp<SurfaceFlinger>& flinger);
     64         ~Client();
     65 
     66     status_t initCheck() const;
     67 
     68     // protected by SurfaceFlinger::mStateLock
     69     size_t attachLayer(const sp<LayerBaseClient>& layer);
     70     void detachLayer(const LayerBaseClient* layer);
     71     sp<LayerBaseClient> getLayerUser(int32_t i) const;
     72 
     73 private:
     74 
     75     // ISurfaceComposerClient interface
     76     virtual sp<IMemoryHeap> getControlBlock() const;
     77     virtual ssize_t getTokenForSurface(const sp<ISurface>& sur) const;
     78     virtual sp<ISurface> createSurface(
     79             surface_data_t* params, int pid, const String8& name,
     80             DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
     81             uint32_t flags);
     82     virtual status_t destroySurface(SurfaceID surfaceId);
     83     virtual status_t setState(int32_t count, const layer_state_t* states);
     84 
     85     // constant
     86     sp<SurfaceFlinger> mFlinger;
     87 
     88     // protected by mLock
     89     DefaultKeyedVector< size_t, wp<LayerBaseClient> > mLayers;
     90     size_t mNameGenerator;
     91 
     92     // thread-safe
     93     mutable Mutex mLock;
     94 };
     95 
     96 class UserClient : public BnSurfaceComposerClient
     97 {
     98 public:
     99     // pointer to this client's control block
    100     SharedClient* ctrlblk;
    101 
    102 public:
    103         UserClient(const sp<SurfaceFlinger>& flinger);
    104         ~UserClient();
    105 
    106     status_t initCheck() const;
    107 
    108     // protected by SurfaceFlinger::mStateLock
    109     void detachLayer(const Layer* layer);
    110 
    111 private:
    112 
    113     // ISurfaceComposerClient interface
    114     virtual sp<IMemoryHeap> getControlBlock() const;
    115     virtual ssize_t getTokenForSurface(const sp<ISurface>& sur) const;
    116     virtual sp<ISurface> createSurface(
    117             surface_data_t* params, int pid, const String8& name,
    118             DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
    119             uint32_t flags);
    120     virtual status_t destroySurface(SurfaceID surfaceId);
    121     virtual status_t setState(int32_t count, const layer_state_t* states);
    122 
    123     // atomic-ops
    124     mutable volatile int32_t mBitmap;
    125 
    126     sp<IMemoryHeap> mCblkHeap;
    127     sp<SurfaceFlinger> mFlinger;
    128 };
    129 
    130 // ---------------------------------------------------------------------------
    131 
    132 class GraphicPlane
    133 {
    134 public:
    135     static status_t orientationToTransfrom(int orientation, int w, int h,
    136             Transform* tr);
    137 
    138                                 GraphicPlane();
    139                                 ~GraphicPlane();
    140 
    141         bool                    initialized() const;
    142 
    143         void                    setDisplayHardware(DisplayHardware *);
    144         status_t                setOrientation(int orientation);
    145         int                     getOrientation() const { return mOrientation; }
    146         int                     getWidth() const;
    147         int                     getHeight() const;
    148 
    149         const DisplayHardware&  displayHardware() const;
    150         DisplayHardware&        editDisplayHardware();
    151         const Transform&        transform() const;
    152         EGLDisplay              getEGLDisplay() const;
    153 
    154 private:
    155                                 GraphicPlane(const GraphicPlane&);
    156         GraphicPlane            operator = (const GraphicPlane&);
    157 
    158         DisplayHardware*        mHw;
    159         Transform               mGlobalTransform;
    160         Transform               mDisplayTransform;
    161         int                     mOrientation;
    162         float                   mDisplayWidth;
    163         float                   mDisplayHeight;
    164         int                     mWidth;
    165         int                     mHeight;
    166 };
    167 
    168 // ---------------------------------------------------------------------------
    169 
    170 enum {
    171     eTransactionNeeded      = 0x01,
    172     eTraversalNeeded        = 0x02
    173 };
    174 
    175 class SurfaceFlinger :
    176         public BinderService<SurfaceFlinger>,
    177         public BnSurfaceComposer,
    178         protected Thread
    179 {
    180 public:
    181     static char const* getServiceName() { return "SurfaceFlinger"; }
    182 
    183                     SurfaceFlinger();
    184     virtual         ~SurfaceFlinger();
    185             void    init();
    186 
    187     virtual status_t onTransact(
    188         uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
    189 
    190     virtual status_t dump(int fd, const Vector<String16>& args);
    191 
    192     // ISurfaceComposer interface
    193     virtual sp<ISurfaceComposerClient>  createConnection();
    194     virtual sp<ISurfaceComposerClient>  createClientConnection();
    195     virtual sp<IMemoryHeap>             getCblk() const;
    196     virtual void                        bootFinished();
    197     virtual void                        openGlobalTransaction();
    198     virtual void                        closeGlobalTransaction();
    199     virtual status_t                    freezeDisplay(DisplayID dpy, uint32_t flags);
    200     virtual status_t                    unfreezeDisplay(DisplayID dpy, uint32_t flags);
    201     virtual int                         setOrientation(DisplayID dpy, int orientation, uint32_t flags);
    202     virtual void                        signal() const;
    203     virtual status_t                    captureScreen(DisplayID dpy,
    204                                                       sp<IMemoryHeap>* heap,
    205                                                       uint32_t* width,
    206                                                       uint32_t* height,
    207                                                       PixelFormat* format,
    208                                                       uint32_t reqWidth,
    209                                                       uint32_t reqHeight);
    210     virtual status_t                    turnElectronBeamOff(int32_t mode);
    211     virtual status_t                    turnElectronBeamOn(int32_t mode);
    212 
    213             void                        screenReleased(DisplayID dpy);
    214             void                        screenAcquired(DisplayID dpy);
    215 
    216             overlay_control_device_t* getOverlayEngine() const;
    217 
    218     status_t removeLayer(const sp<LayerBase>& layer);
    219     status_t addLayer(const sp<LayerBase>& layer);
    220     status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
    221     void destroyLayer(LayerBase const* layer);
    222 
    223     sp<Layer> getLayer(const sp<ISurface>& sur) const;
    224 
    225 private:
    226     friend class Client;
    227     friend class LayerBase;
    228     friend class LayerBuffer;
    229     friend class LayerBaseClient;
    230     friend class LayerBaseClient::Surface;
    231     friend class Layer;
    232     friend class LayerBlur;
    233     friend class LayerDim;
    234 
    235     sp<ISurface> createSurface(const sp<Client>& client,
    236             int pid, const String8& name,
    237             ISurfaceComposerClient::surface_data_t* params,
    238             DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
    239             uint32_t flags);
    240 
    241     sp<Layer> createNormalSurface(
    242             const sp<Client>& client, DisplayID display,
    243             uint32_t w, uint32_t h, uint32_t flags,
    244             PixelFormat& format);
    245 
    246     sp<LayerBlur> createBlurSurface(
    247             const sp<Client>& client, DisplayID display,
    248             uint32_t w, uint32_t h, uint32_t flags);
    249 
    250     sp<LayerDim> createDimSurface(
    251             const sp<Client>& client, DisplayID display,
    252             uint32_t w, uint32_t h, uint32_t flags);
    253 
    254     sp<LayerBuffer> createPushBuffersSurface(
    255             const sp<Client>& client, DisplayID display,
    256             uint32_t w, uint32_t h, uint32_t flags);
    257 
    258     status_t removeSurface(const sp<Client>& client, SurfaceID sid);
    259     status_t destroySurface(const wp<LayerBaseClient>& layer);
    260     status_t setClientState(const sp<Client>& client,
    261             int32_t count, const layer_state_t* states);
    262 
    263     class LayerVector : public SortedVector< sp<LayerBase> > {
    264     public:
    265         LayerVector() { }
    266         LayerVector(const LayerVector& rhs) : SortedVector< sp<LayerBase> >(rhs) { }
    267         virtual int do_compare(const void* lhs, const void* rhs) const {
    268             const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
    269             const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
    270             // sort layers by Z order
    271             uint32_t lz = l->currentState().z;
    272             uint32_t rz = r->currentState().z;
    273             // then by sequence, so we get a stable ordering
    274             return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence);
    275         }
    276     };
    277 
    278     struct State {
    279         State() {
    280             orientation = ISurfaceComposer::eOrientationDefault;
    281             freezeDisplay = 0;
    282         }
    283         LayerVector     layersSortedByZ;
    284         uint8_t         orientation;
    285         uint8_t         orientationType;
    286         uint8_t         freezeDisplay;
    287     };
    288 
    289     virtual bool        threadLoop();
    290     virtual status_t    readyToRun();
    291     virtual void        onFirstRef();
    292 
    293 public:     // hack to work around gcc 4.0.3 bug
    294     const GraphicPlane&     graphicPlane(int dpy) const;
    295           GraphicPlane&     graphicPlane(int dpy);
    296 private:
    297 
    298             void        waitForEvent();
    299 public:     // hack to work around gcc 4.0.3 bug
    300             void        signalEvent();
    301 private:
    302             void        handleConsoleEvents();
    303             void        handleTransaction(uint32_t transactionFlags);
    304             void        handleTransactionLocked(uint32_t transactionFlags);
    305             void        handleDestroyLayers();
    306 
    307             void        computeVisibleRegions(
    308                             LayerVector& currentLayers,
    309                             Region& dirtyRegion,
    310                             Region& wormholeRegion);
    311 
    312             void        handlePageFlip();
    313             bool        lockPageFlip(const LayerVector& currentLayers);
    314             void        unlockPageFlip(const LayerVector& currentLayers);
    315             void        handleRepaint();
    316             bool        handleBypassLayer();
    317             void        postFramebuffer();
    318             void        composeSurfaces(const Region& dirty);
    319 
    320 
    321             ssize_t     addClientLayer(const sp<Client>& client,
    322                     const sp<LayerBaseClient>& lbc);
    323             status_t    addLayer_l(const sp<LayerBase>& layer);
    324             status_t    removeLayer_l(const sp<LayerBase>& layer);
    325             status_t    purgatorizeLayer_l(const sp<LayerBase>& layer);
    326 
    327             uint32_t    getTransactionFlags(uint32_t flags);
    328             uint32_t    peekTransactionFlags(uint32_t flags);
    329             uint32_t    setTransactionFlags(uint32_t flags);
    330             void        commitTransaction();
    331 
    332             void        setBypassLayer(const sp<LayerBase>& layer);
    333 
    334             status_t captureScreenImplLocked(DisplayID dpy,
    335                     sp<IMemoryHeap>* heap,
    336                     uint32_t* width, uint32_t* height, PixelFormat* format,
    337                     uint32_t reqWidth = 0, uint32_t reqHeight = 0);
    338 
    339             status_t turnElectronBeamOffImplLocked(int32_t mode);
    340             status_t turnElectronBeamOnImplLocked(int32_t mode);
    341             status_t electronBeamOffAnimationImplLocked();
    342             status_t electronBeamOnAnimationImplLocked();
    343             status_t renderScreenToTextureLocked(DisplayID dpy,
    344                     GLuint* textureName, GLfloat* uOut, GLfloat* vOut);
    345 
    346             friend class FreezeLock;
    347             sp<FreezeLock> getFreezeLock() const;
    348             inline void incFreezeCount() {
    349                 if (mFreezeCount == 0)
    350                     mFreezeDisplayTime = 0;
    351                 mFreezeCount++;
    352             }
    353             inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
    354             inline bool hasFreezeRequest() const { return mFreezeDisplay; }
    355             inline bool isFrozen() const {
    356                 return (mFreezeDisplay || mFreezeCount>0) && mBootFinished;
    357             }
    358 
    359 
    360             void        debugFlashRegions();
    361             void        debugShowFPS() const;
    362             void        drawWormhole() const;
    363 
    364 
    365     mutable     MessageQueue    mEventQueue;
    366 
    367     status_t postMessageAsync(const sp<MessageBase>& msg,
    368             nsecs_t reltime=0, uint32_t flags = 0);
    369 
    370     status_t postMessageSync(const sp<MessageBase>& msg,
    371             nsecs_t reltime=0, uint32_t flags = 0);
    372 
    373                 // access must be protected by mStateLock
    374     mutable     Mutex                   mStateLock;
    375                 State                   mCurrentState;
    376                 State                   mDrawingState;
    377     volatile    int32_t                 mTransactionFlags;
    378     volatile    int32_t                 mTransactionCount;
    379                 Condition               mTransactionCV;
    380                 bool                    mResizeTransationPending;
    381 
    382                 // protected by mStateLock (but we could use another lock)
    383                 GraphicPlane                mGraphicPlanes[1];
    384                 bool                        mLayersRemoved;
    385                 DefaultKeyedVector< wp<IBinder>, wp<Layer> > mLayerMap;
    386 
    387                 // constant members (no synchronization needed for access)
    388                 sp<IMemoryHeap>             mServerHeap;
    389                 surface_flinger_cblk_t*     mServerCblk;
    390                 GLuint                      mWormholeTexName;
    391                 nsecs_t                     mBootTime;
    392                 Permission                  mHardwareTest;
    393                 Permission                  mAccessSurfaceFlinger;
    394                 Permission                  mReadFramebuffer;
    395                 Permission                  mDump;
    396 
    397                 // Can only accessed from the main thread, these members
    398                 // don't need synchronization
    399                 Region                      mDirtyRegion;
    400                 Region                      mDirtyRegionRemovedLayer;
    401                 Region                      mInvalidRegion;
    402                 Region                      mWormholeRegion;
    403                 bool                        mVisibleRegionsDirty;
    404                 bool                        mDeferReleaseConsole;
    405                 bool                        mFreezeDisplay;
    406                 int32_t                     mElectronBeamAnimationMode;
    407                 int32_t                     mFreezeCount;
    408                 nsecs_t                     mFreezeDisplayTime;
    409                 Vector< sp<LayerBase> >     mVisibleLayersSortedByZ;
    410                 wp<Layer>                   mBypassLayer;
    411 
    412 
    413                 // don't use a lock for these, we don't care
    414                 int                         mDebugRegion;
    415                 int                         mDebugBackground;
    416                 volatile nsecs_t            mDebugInSwapBuffers;
    417                 nsecs_t                     mLastSwapBufferTime;
    418                 volatile nsecs_t            mDebugInTransaction;
    419                 nsecs_t                     mLastTransactionTime;
    420                 bool                        mBootFinished;
    421 
    422                 // these are thread safe
    423     mutable     Barrier                     mReadyToRunBarrier;
    424 
    425 
    426                 // protected by mDestroyedLayerLock;
    427     mutable     Mutex                       mDestroyedLayerLock;
    428                 Vector<LayerBase const *>   mDestroyedLayers;
    429 
    430                 // atomic variables
    431                 enum {
    432                     eConsoleReleased = 1,
    433                     eConsoleAcquired = 2
    434                 };
    435    volatile     int32_t                     mConsoleSignals;
    436 
    437    // only written in the main thread, only read in other threads
    438    volatile     int32_t                     mSecureFrameBuffer;
    439 };
    440 
    441 // ---------------------------------------------------------------------------
    442 
    443 class FreezeLock : public LightRefBase<FreezeLock> {
    444     SurfaceFlinger* mFlinger;
    445 public:
    446     FreezeLock(SurfaceFlinger* flinger)
    447         : mFlinger(flinger) {
    448         mFlinger->incFreezeCount();
    449     }
    450     ~FreezeLock() {
    451         mFlinger->decFreezeCount();
    452     }
    453 };
    454 
    455 // ---------------------------------------------------------------------------
    456 }; // namespace android
    457 
    458 #endif // ANDROID_SURFACE_FLINGER_H
    459