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 <EGL/egl.h>
     24 
     25 /*
     26  * NOTE: Make sure this file doesn't include  anything from <gl/ > or <gl2/ >
     27  */
     28 
     29 #include <cutils/compiler.h>
     30 
     31 #include <utils/Atomic.h>
     32 #include <utils/Errors.h>
     33 #include <utils/KeyedVector.h>
     34 #include <utils/RefBase.h>
     35 #include <utils/SortedVector.h>
     36 #include <utils/threads.h>
     37 
     38 #include <binder/IMemory.h>
     39 
     40 #include <ui/PixelFormat.h>
     41 #include <ui/mat4.h>
     42 
     43 #include <gui/ISurfaceComposer.h>
     44 #include <gui/ISurfaceComposerClient.h>
     45 
     46 #include <hardware/hwcomposer_defs.h>
     47 
     48 #include <private/gui/LayerState.h>
     49 
     50 #include "Barrier.h"
     51 #include "DisplayDevice.h"
     52 #include "DispSync.h"
     53 #include "FenceTracker.h"
     54 #include "FrameTracker.h"
     55 #include "MessageQueue.h"
     56 
     57 #include "DisplayHardware/HWComposer.h"
     58 #include "Effects/Daltonizer.h"
     59 
     60 namespace android {
     61 
     62 // ---------------------------------------------------------------------------
     63 
     64 class Client;
     65 class DisplayEventConnection;
     66 class EventThread;
     67 class IGraphicBufferAlloc;
     68 class Layer;
     69 class LayerDim;
     70 class Surface;
     71 class RenderEngine;
     72 class EventControlThread;
     73 
     74 // ---------------------------------------------------------------------------
     75 
     76 enum {
     77     eTransactionNeeded        = 0x01,
     78     eTraversalNeeded          = 0x02,
     79     eDisplayTransactionNeeded = 0x04,
     80     eTransactionMask          = 0x07
     81 };
     82 
     83 class SurfaceFlinger : public BnSurfaceComposer,
     84                        private IBinder::DeathRecipient,
     85                        private HWComposer::EventHandler
     86 {
     87 public:
     88     static char const* getServiceName() ANDROID_API {
     89         return "SurfaceFlinger";
     90     }
     91 
     92     SurfaceFlinger() ANDROID_API;
     93 
     94     // must be called before clients can connect
     95     void init() ANDROID_API;
     96 
     97     // starts SurfaceFlinger main loop in the current thread
     98     void run() ANDROID_API;
     99 
    100     enum {
    101         EVENT_VSYNC = HWC_EVENT_VSYNC
    102     };
    103 
    104     // post an asynchronous message to the main thread
    105     status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
    106 
    107     // post a synchronous message to the main thread
    108     status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
    109 
    110     // force full composition on all displays
    111     void repaintEverything();
    112 
    113     // returns the default Display
    114     sp<const DisplayDevice> getDefaultDisplayDevice() const {
    115         return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
    116     }
    117 
    118     // utility function to delete a texture on the main thread
    119     void deleteTextureAsync(uint32_t texture);
    120 
    121     // enable/disable h/w composer event
    122     // TODO: this should be made accessible only to EventThread
    123 #ifdef USE_HWC2
    124     void setVsyncEnabled(int disp, int enabled);
    125 #else
    126     void eventControl(int disp, int event, int enabled);
    127 #endif
    128 
    129     // called on the main thread by MessageQueue when an internal message
    130     // is received
    131     // TODO: this should be made accessible only to MessageQueue
    132     void onMessageReceived(int32_t what);
    133 
    134     // for debugging only
    135     // TODO: this should be made accessible only to HWComposer
    136     const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
    137 
    138     RenderEngine& getRenderEngine() const {
    139         return *mRenderEngine;
    140     }
    141 
    142 private:
    143     friend class Client;
    144     friend class DisplayEventConnection;
    145     friend class Layer;
    146     friend class MonitoredProducer;
    147 
    148     // This value is specified in number of frames.  Log frame stats at most
    149     // every half hour.
    150     enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
    151 
    152     static const size_t MAX_LAYERS = 4096;
    153 
    154     // We're reference counted, never destroy SurfaceFlinger directly
    155     virtual ~SurfaceFlinger();
    156 
    157     /* ------------------------------------------------------------------------
    158      * Internal data structures
    159      */
    160 
    161     class LayerVector : public SortedVector< sp<Layer> > {
    162     public:
    163         LayerVector();
    164         LayerVector(const LayerVector& rhs);
    165         virtual int do_compare(const void* lhs, const void* rhs) const;
    166     };
    167 
    168     struct DisplayDeviceState {
    169         DisplayDeviceState();
    170         DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
    171         bool isValid() const { return type >= 0; }
    172         bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
    173         bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
    174         DisplayDevice::DisplayType type;
    175         sp<IGraphicBufferProducer> surface;
    176         uint32_t layerStack;
    177         Rect viewport;
    178         Rect frame;
    179         uint8_t orientation;
    180         uint32_t width, height;
    181         String8 displayName;
    182         bool isSecure;
    183     };
    184 
    185     struct State {
    186         LayerVector layersSortedByZ;
    187         DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
    188     };
    189 
    190     /* ------------------------------------------------------------------------
    191      * IBinder interface
    192      */
    193     virtual status_t onTransact(uint32_t code, const Parcel& data,
    194         Parcel* reply, uint32_t flags);
    195     virtual status_t dump(int fd, const Vector<String16>& args);
    196 
    197     /* ------------------------------------------------------------------------
    198      * ISurfaceComposer interface
    199      */
    200     virtual sp<ISurfaceComposerClient> createConnection();
    201     virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
    202     virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
    203     virtual void destroyDisplay(const sp<IBinder>& display);
    204     virtual sp<IBinder> getBuiltInDisplay(int32_t id);
    205     virtual void setTransactionState(const Vector<ComposerState>& state,
    206             const Vector<DisplayState>& displays, uint32_t flags);
    207     virtual void bootFinished();
    208     virtual bool authenticateSurfaceTexture(
    209         const sp<IGraphicBufferProducer>& bufferProducer) const;
    210     virtual sp<IDisplayEventConnection> createDisplayEventConnection();
    211     virtual status_t captureScreen(const sp<IBinder>& display,
    212             const sp<IGraphicBufferProducer>& producer,
    213             Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
    214             uint32_t minLayerZ, uint32_t maxLayerZ,
    215             bool useIdentityTransform, ISurfaceComposer::Rotation rotation);
    216     virtual status_t getDisplayStats(const sp<IBinder>& display,
    217             DisplayStatInfo* stats);
    218     virtual status_t getDisplayConfigs(const sp<IBinder>& display,
    219             Vector<DisplayInfo>* configs);
    220     virtual int getActiveConfig(const sp<IBinder>& display);
    221     virtual void setPowerMode(const sp<IBinder>& display, int mode);
    222     virtual status_t setActiveConfig(const sp<IBinder>& display, int id);
    223     virtual status_t clearAnimationFrameStats();
    224     virtual status_t getAnimationFrameStats(FrameStats* outStats) const;
    225     virtual status_t getHdrCapabilities(const sp<IBinder>& display,
    226             HdrCapabilities* outCapabilities) const;
    227 
    228     /* ------------------------------------------------------------------------
    229      * DeathRecipient interface
    230      */
    231     virtual void binderDied(const wp<IBinder>& who);
    232 
    233     /* ------------------------------------------------------------------------
    234      * RefBase interface
    235      */
    236     virtual void onFirstRef();
    237 
    238     /* ------------------------------------------------------------------------
    239      * HWComposer::EventHandler interface
    240      */
    241     virtual void onVSyncReceived(int type, nsecs_t timestamp);
    242     virtual void onHotplugReceived(int disp, bool connected);
    243 
    244     /* ------------------------------------------------------------------------
    245      * Message handling
    246      */
    247     void waitForEvent();
    248     void signalTransaction();
    249     void signalLayerUpdate();
    250     void signalRefresh();
    251 
    252     // called on the main thread in response to initializeDisplays()
    253     void onInitializeDisplays();
    254     // called on the main thread in response to setActiveConfig()
    255     void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode);
    256     // called on the main thread in response to setPowerMode()
    257     void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode);
    258 
    259     // Returns whether the transaction actually modified any state
    260     bool handleMessageTransaction();
    261 
    262     // Returns whether a new buffer has been latched (see handlePageFlip())
    263     bool handleMessageInvalidate();
    264 
    265     void handleMessageRefresh();
    266 
    267     void handleTransaction(uint32_t transactionFlags);
    268     void handleTransactionLocked(uint32_t transactionFlags);
    269 
    270     void updateCursorAsync();
    271 
    272     /* handlePageFlip - latch a new buffer if available and compute the dirty
    273      * region. Returns whether a new buffer has been latched, i.e., whether it
    274      * is necessary to perform a refresh during this vsync.
    275      */
    276     bool handlePageFlip();
    277 
    278     /* ------------------------------------------------------------------------
    279      * Transactions
    280      */
    281     uint32_t getTransactionFlags(uint32_t flags);
    282     uint32_t peekTransactionFlags(uint32_t flags);
    283     uint32_t setTransactionFlags(uint32_t flags);
    284     void commitTransaction();
    285     uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
    286     uint32_t setDisplayStateLocked(const DisplayState& s);
    287 
    288     /* ------------------------------------------------------------------------
    289      * Layer management
    290      */
    291     status_t createLayer(const String8& name, const sp<Client>& client,
    292             uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
    293             sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
    294 
    295     status_t createNormalLayer(const sp<Client>& client, const String8& name,
    296             uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
    297             sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
    298             sp<Layer>* outLayer);
    299 
    300     status_t createDimLayer(const sp<Client>& client, const String8& name,
    301             uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
    302             sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
    303 
    304     // called in response to the window-manager calling
    305     // ISurfaceComposerClient::destroySurface()
    306     status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
    307 
    308     // called when all clients have released all their references to
    309     // this layer meaning it is entirely safe to destroy all
    310     // resources associated to this layer.
    311     status_t onLayerDestroyed(const wp<Layer>& layer);
    312 
    313     // remove a layer from SurfaceFlinger immediately
    314     status_t removeLayer(const sp<Layer>& layer);
    315 
    316     // add a layer to SurfaceFlinger
    317     status_t addClientLayer(const sp<Client>& client,
    318             const sp<IBinder>& handle,
    319             const sp<IGraphicBufferProducer>& gbc,
    320             const sp<Layer>& lbc);
    321 
    322     /* ------------------------------------------------------------------------
    323      * Boot animation, on/off animations and screen capture
    324      */
    325 
    326     void startBootAnim();
    327 
    328     void renderScreenImplLocked(
    329             const sp<const DisplayDevice>& hw,
    330             Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
    331             uint32_t minLayerZ, uint32_t maxLayerZ,
    332             bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
    333 
    334     status_t captureScreenImplLocked(
    335             const sp<const DisplayDevice>& hw,
    336             const sp<IGraphicBufferProducer>& producer,
    337             Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
    338             uint32_t minLayerZ, uint32_t maxLayerZ,
    339             bool useIdentityTransform, Transform::orientation_flags rotation,
    340             bool isLocalScreenshot);
    341 
    342     /* ------------------------------------------------------------------------
    343      * EGL
    344      */
    345     size_t getMaxTextureSize() const;
    346     size_t getMaxViewportDims() const;
    347 
    348     /* ------------------------------------------------------------------------
    349      * Display and layer stack management
    350      */
    351     // called when starting, or restarting after system_server death
    352     void initializeDisplays();
    353 
    354     // Create an IBinder for a builtin display and add it to current state
    355     void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
    356 
    357     // NOTE: can only be called from the main thread or with mStateLock held
    358     sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
    359         return mDisplays.valueFor(dpy);
    360     }
    361 
    362     // NOTE: can only be called from the main thread or with mStateLock held
    363     sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
    364         return mDisplays.valueFor(dpy);
    365     }
    366 
    367     // mark a region of a layer stack dirty. this updates the dirty
    368     // region of all screens presenting this layer stack.
    369     void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
    370 
    371 #ifndef USE_HWC2
    372     int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
    373 #endif
    374 
    375     /* ------------------------------------------------------------------------
    376      * H/W composer
    377      */
    378 
    379     HWComposer& getHwComposer() const { return *mHwc; }
    380 
    381     /* ------------------------------------------------------------------------
    382      * Compositing
    383      */
    384     void invalidateHwcGeometry();
    385     static void computeVisibleRegions(
    386             const LayerVector& currentLayers, uint32_t layerStack,
    387             Region& dirtyRegion, Region& opaqueRegion);
    388 
    389     void preComposition();
    390     void postComposition(nsecs_t refreshStartTime);
    391     void rebuildLayerStacks();
    392     void setUpHWComposer();
    393     void doComposition();
    394     void doDebugFlashRegions();
    395     void doDisplayComposition(const sp<const DisplayDevice>& hw, const Region& dirtyRegion);
    396 
    397     // compose surfaces for display hw. this fails if using GL and the surface
    398     // has been destroyed and is no longer valid.
    399     bool doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty);
    400 
    401     void postFramebuffer();
    402     void drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const;
    403 
    404     /* ------------------------------------------------------------------------
    405      * Display management
    406      */
    407 
    408     /* ------------------------------------------------------------------------
    409      * VSync
    410      */
    411      void enableHardwareVsync();
    412      void resyncToHardwareVsync(bool makeAvailable);
    413      void disableHardwareVsync(bool makeUnavailable);
    414 public:
    415      void resyncWithRateLimit();
    416 private:
    417 
    418     /* ------------------------------------------------------------------------
    419      * Debugging & dumpsys
    420      */
    421     void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const;
    422     void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const;
    423     void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result);
    424     void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const;
    425     bool startDdmConnection();
    426     static void appendSfConfigString(String8& result);
    427     void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
    428             const sp<const DisplayDevice>& hw,
    429             uint32_t minLayerZ, uint32_t maxLayerZ);
    430 
    431     void logFrameStats();
    432 
    433     void dumpStaticScreenStats(String8& result) const;
    434 
    435     /* ------------------------------------------------------------------------
    436      * Attributes
    437      */
    438 
    439     // access must be protected by mStateLock
    440     mutable Mutex mStateLock;
    441     State mCurrentState;
    442     volatile int32_t mTransactionFlags;
    443     Condition mTransactionCV;
    444     bool mTransactionPending;
    445     bool mAnimTransactionPending;
    446     Vector< sp<Layer> > mLayersPendingRemoval;
    447     SortedVector< wp<IBinder> > mGraphicBufferProducerList;
    448 
    449     // protected by mStateLock (but we could use another lock)
    450     bool mLayersRemoved;
    451 
    452     // access must be protected by mInvalidateLock
    453     volatile int32_t mRepaintEverything;
    454 
    455     // constant members (no synchronization needed for access)
    456     HWComposer* mHwc;
    457     RenderEngine* mRenderEngine;
    458     nsecs_t mBootTime;
    459     bool mGpuToCpuSupported;
    460     bool mDropMissedFrames;
    461     sp<EventThread> mEventThread;
    462     sp<EventThread> mSFEventThread;
    463     sp<EventControlThread> mEventControlThread;
    464     EGLContext mEGLContext;
    465     EGLDisplay mEGLDisplay;
    466     sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
    467 
    468     // Can only accessed from the main thread, these members
    469     // don't need synchronization
    470     State mDrawingState;
    471     bool mVisibleRegionsDirty;
    472 #ifndef USE_HWC2
    473     bool mHwWorkListDirty;
    474 #else
    475     bool mGeometryInvalid;
    476 #endif
    477     bool mAnimCompositionPending;
    478 #ifdef USE_HWC2
    479     std::vector<sp<Layer>> mLayersWithQueuedFrames;
    480 #endif
    481 
    482     // this may only be written from the main thread with mStateLock held
    483     // it may be read from other threads with mStateLock held
    484     DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
    485 
    486     // don't use a lock for these, we don't care
    487     int mDebugRegion;
    488     int mDebugDDMS;
    489     int mDebugDisableHWC;
    490     int mDebugDisableTransformHint;
    491     volatile nsecs_t mDebugInSwapBuffers;
    492     nsecs_t mLastSwapBufferTime;
    493     volatile nsecs_t mDebugInTransaction;
    494     nsecs_t mLastTransactionTime;
    495     bool mBootFinished;
    496     bool mForceFullDamage;
    497     FenceTracker mFenceTracker;
    498 
    499     // these are thread safe
    500     mutable MessageQueue mEventQueue;
    501     FrameTracker mAnimFrameTracker;
    502     DispSync mPrimaryDispSync;
    503 
    504     // protected by mDestroyedLayerLock;
    505     mutable Mutex mDestroyedLayerLock;
    506     Vector<Layer const *> mDestroyedLayers;
    507 
    508     // protected by mHWVsyncLock
    509     Mutex mHWVsyncLock;
    510     bool mPrimaryHWVsyncEnabled;
    511     bool mHWVsyncAvailable;
    512 
    513     /* ------------------------------------------------------------------------
    514      * Feature prototyping
    515      */
    516 
    517     Daltonizer mDaltonizer;
    518     bool mDaltonize;
    519 
    520     mat4 mColorMatrix;
    521     bool mHasColorMatrix;
    522 
    523     // Static screen stats
    524     bool mHasPoweredOff;
    525     static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
    526     nsecs_t mFrameBuckets[NUM_BUCKETS];
    527     nsecs_t mTotalTime;
    528     std::atomic<nsecs_t> mLastSwapTime;
    529 };
    530 
    531 }; // namespace android
    532 
    533 #endif // ANDROID_SURFACE_FLINGER_H
    534