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