SurfaceFlinger.h revision 3cfac28462910d3f976aebac54ac7301aca7e434
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 <memory>
21#include <stdint.h>
22#include <sys/types.h>
23
24#include <EGL/egl.h>
25
26/*
27 * NOTE: Make sure this file doesn't include  anything from <gl/ > or <gl2/ >
28 */
29
30#include <cutils/compiler.h>
31
32#include <utils/Atomic.h>
33#include <utils/Errors.h>
34#include <utils/KeyedVector.h>
35#include <utils/RefBase.h>
36#include <utils/SortedVector.h>
37#include <utils/threads.h>
38
39#include <binder/IMemory.h>
40
41#include <ui/FenceTime.h>
42#include <ui/PixelFormat.h>
43#include <ui/mat4.h>
44
45#include <gui/ISurfaceComposer.h>
46#include <gui/ISurfaceComposerClient.h>
47#include <gui/OccupancyTracker.h>
48
49#include <hardware/hwcomposer_defs.h>
50
51#include <system/graphics.h>
52
53#include <private/gui/LayerState.h>
54
55#include "Barrier.h"
56#include "DisplayDevice.h"
57#include "DispSync.h"
58#include "FrameTracker.h"
59#include "LayerVector.h"
60#include "MessageQueue.h"
61#include "SurfaceInterceptor.h"
62#include "StartBootAnimThread.h"
63
64#include "DisplayHardware/HWComposer.h"
65#include "Effects/Daltonizer.h"
66
67#include <map>
68#include <string>
69
70namespace android {
71
72// ---------------------------------------------------------------------------
73
74class Client;
75class DisplayEventConnection;
76class EventThread;
77class IGraphicBufferAlloc;
78class Layer;
79class LayerDim;
80class Surface;
81class RenderEngine;
82class EventControlThread;
83class VSyncSource;
84class InjectVSyncSource;
85class VrStateCallbacks;
86
87namespace dvr {
88class VrFlinger;
89} // namespace dvr
90
91// ---------------------------------------------------------------------------
92
93enum {
94    eTransactionNeeded        = 0x01,
95    eTraversalNeeded          = 0x02,
96    eDisplayTransactionNeeded = 0x04,
97    eTransactionMask          = 0x07
98};
99
100class SurfaceFlinger : public BnSurfaceComposer,
101                       private IBinder::DeathRecipient,
102                       private HWComposer::EventHandler
103{
104public:
105    static char const* getServiceName() ANDROID_API {
106        return "SurfaceFlinger";
107    }
108
109    SurfaceFlinger() ANDROID_API;
110
111    // must be called before clients can connect
112    void init() ANDROID_API;
113
114    // starts SurfaceFlinger main loop in the current thread
115    void run() ANDROID_API;
116
117    enum {
118        EVENT_VSYNC = HWC_EVENT_VSYNC
119    };
120
121    // post an asynchronous message to the main thread
122    status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
123
124    // post a synchronous message to the main thread
125    status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
126
127    // force full composition on all displays
128    void repaintEverything();
129
130    // returns the default Display
131    sp<const DisplayDevice> getDefaultDisplayDevice() const {
132        return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
133    }
134
135    // utility function to delete a texture on the main thread
136    void deleteTextureAsync(uint32_t texture);
137
138    // enable/disable h/w composer event
139    // TODO: this should be made accessible only to EventThread
140#ifdef USE_HWC2
141    void setVsyncEnabled(int disp, int enabled);
142#else
143    void eventControl(int disp, int event, int enabled);
144#endif
145
146    // called on the main thread by MessageQueue when an internal message
147    // is received
148    // TODO: this should be made accessible only to MessageQueue
149    void onMessageReceived(int32_t what);
150
151    // for debugging only
152    // TODO: this should be made accessible only to HWComposer
153    const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
154
155    RenderEngine& getRenderEngine() const {
156        return *mRenderEngine;
157    }
158
159private:
160    friend class Client;
161    friend class DisplayEventConnection;
162    friend class EventThread;
163    friend class Layer;
164    friend class MonitoredProducer;
165    friend class VrStateCallbacks;
166
167    // This value is specified in number of frames.  Log frame stats at most
168    // every half hour.
169    enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
170
171    static const size_t MAX_LAYERS = 4096;
172
173    // We're reference counted, never destroy SurfaceFlinger directly
174    virtual ~SurfaceFlinger();
175
176    /* ------------------------------------------------------------------------
177     * Internal data structures
178     */
179
180    class State {
181    public:
182        LayerVector layersSortedByZ;
183        DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
184
185        void traverseInZOrder(const std::function<void(Layer*)>& consume) const;
186        void traverseInReverseZOrder(const std::function<void(Layer*)>& consume) const;
187    };
188
189    /* ------------------------------------------------------------------------
190     * IBinder interface
191     */
192    virtual status_t onTransact(uint32_t code, const Parcel& data,
193        Parcel* reply, uint32_t flags);
194    virtual status_t dump(int fd, const Vector<String16>& args);
195
196    /* ------------------------------------------------------------------------
197     * ISurfaceComposer interface
198     */
199    virtual sp<ISurfaceComposerClient> createConnection();
200    virtual sp<ISurfaceComposerClient> createScopedConnection(const sp<IGraphicBufferProducer>& gbp);
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 status_t getSupportedFrameTimestamps(
211            std::vector<FrameEvent>* outSupported) const;
212    virtual sp<IDisplayEventConnection> createDisplayEventConnection();
213    virtual status_t captureScreen(const sp<IBinder>& display,
214            const sp<IGraphicBufferProducer>& producer,
215            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
216            int32_t minLayerZ, int32_t maxLayerZ,
217            bool useIdentityTransform, ISurfaceComposer::Rotation rotation);
218    virtual status_t getDisplayStats(const sp<IBinder>& display,
219            DisplayStatInfo* stats);
220    virtual status_t getDisplayConfigs(const sp<IBinder>& display,
221            Vector<DisplayInfo>* configs);
222    virtual int getActiveConfig(const sp<IBinder>& display);
223    virtual status_t getDisplayColorModes(const sp<IBinder>& display,
224            Vector<android_color_mode_t>* configs);
225    virtual android_color_mode_t getActiveColorMode(const sp<IBinder>& display);
226    virtual status_t setActiveColorMode(const sp<IBinder>& display, android_color_mode_t colorMode);
227    virtual void setPowerMode(const sp<IBinder>& display, int mode);
228    virtual status_t setActiveConfig(const sp<IBinder>& display, int id);
229    virtual status_t clearAnimationFrameStats();
230    virtual status_t getAnimationFrameStats(FrameStats* outStats) const;
231    virtual status_t getHdrCapabilities(const sp<IBinder>& display,
232            HdrCapabilities* outCapabilities) const;
233    virtual status_t enableVSyncInjections(bool enable);
234    virtual status_t injectVSync(nsecs_t when);
235
236
237    /* ------------------------------------------------------------------------
238     * DeathRecipient interface
239     */
240    virtual void binderDied(const wp<IBinder>& who);
241
242    /* ------------------------------------------------------------------------
243     * RefBase interface
244     */
245    virtual void onFirstRef();
246
247    /* ------------------------------------------------------------------------
248     * HWComposer::EventHandler interface
249     */
250    virtual void onVSyncReceived(HWComposer* composer, int type, nsecs_t timestamp);
251    virtual void onHotplugReceived(int disp, bool connected);
252    virtual void onInvalidateReceived(HWComposer* composer);
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();
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            uint32_t windowType, uint32_t ownerUid, sp<IBinder>* handle,
307            sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent);
308
309    status_t createNormalLayer(const sp<Client>& client, const String8& name,
310            uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
311            sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
312            sp<Layer>* outLayer);
313
314    status_t createDimLayer(const sp<Client>& client, const String8& name,
315            uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
316            sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
317
318    // called in response to the window-manager calling
319    // ISurfaceComposerClient::destroySurface()
320    status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
321
322    // called when all clients have released all their references to
323    // this layer meaning it is entirely safe to destroy all
324    // resources associated to this layer.
325    status_t onLayerDestroyed(const wp<Layer>& layer);
326
327    // remove a layer from SurfaceFlinger immediately
328    status_t removeLayer(const wp<Layer>& layer);
329
330    // add a layer to SurfaceFlinger
331    status_t addClientLayer(const sp<Client>& client,
332            const sp<IBinder>& handle,
333            const sp<IGraphicBufferProducer>& gbc,
334            const sp<Layer>& lbc,
335            const sp<Layer>& parent);
336
337    /* ------------------------------------------------------------------------
338     * Boot animation, on/off animations and screen capture
339     */
340
341    void startBootAnim();
342
343    void renderScreenImplLocked(
344            const sp<const DisplayDevice>& hw,
345            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
346            int32_t minLayerZ, int32_t maxLayerZ,
347            bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
348
349    status_t captureScreenImplLocked(
350            const sp<const DisplayDevice>& hw,
351            const sp<IGraphicBufferProducer>& producer,
352            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
353            int32_t minLayerZ, int32_t maxLayerZ,
354            bool useIdentityTransform, Transform::orientation_flags rotation,
355            bool isLocalScreenshot);
356
357    sp<StartBootAnimThread> mStartBootAnimThread = nullptr;
358
359    /* ------------------------------------------------------------------------
360     * EGL
361     */
362    size_t getMaxTextureSize() const;
363    size_t getMaxViewportDims() const;
364
365    /* ------------------------------------------------------------------------
366     * Display and layer stack management
367     */
368    // called when starting, or restarting after system_server death
369    void initializeDisplays();
370
371    // Create an IBinder for a builtin display and add it to current state
372    void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
373
374    // NOTE: can only be called from the main thread or with mStateLock held
375    sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
376        return mDisplays.valueFor(dpy);
377    }
378
379    // NOTE: can only be called from the main thread or with mStateLock held
380    sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
381        return mDisplays.valueFor(dpy);
382    }
383
384    int32_t getDisplayType(const sp<IBinder>& display) {
385        if (!display.get()) return NAME_NOT_FOUND;
386        for (int i = 0; i < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES; ++i) {
387            if (display == mBuiltinDisplays[i]) {
388                return i;
389            }
390        }
391        return NAME_NOT_FOUND;
392    }
393
394    // mark a region of a layer stack dirty. this updates the dirty
395    // region of all screens presenting this layer stack.
396    void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
397
398#ifndef USE_HWC2
399    int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
400#endif
401
402    /* ------------------------------------------------------------------------
403     * H/W composer
404     */
405
406    HWComposer& getHwComposer() const { return *mHwc; }
407
408    /* ------------------------------------------------------------------------
409     * Compositing
410     */
411    void invalidateHwcGeometry();
412    void computeVisibleRegions(uint32_t layerStack,
413            Region& dirtyRegion, Region& opaqueRegion);
414
415    void preComposition(nsecs_t refreshStartTime);
416    void postComposition();
417    void rebuildLayerStacks();
418    void setUpHWComposer();
419    void doComposition();
420    void doDebugFlashRegions();
421    void doDisplayComposition(const sp<const DisplayDevice>& displayDevice, const Region& dirtyRegion);
422
423    // compose surfaces for display hw. this fails if using GL and the surface
424    // has been destroyed and is no longer valid.
425    bool doComposeSurfaces(const sp<const DisplayDevice>& displayDevice, const Region& dirty);
426
427    void postFramebuffer();
428    void drawWormhole(const sp<const DisplayDevice>& displayDevice, const Region& region) const;
429
430    /* ------------------------------------------------------------------------
431     * Display management
432     */
433
434    /* ------------------------------------------------------------------------
435     * VSync
436     */
437     void enableHardwareVsync();
438     void resyncToHardwareVsync(bool makeAvailable);
439     void disableHardwareVsync(bool makeUnavailable);
440
441public:
442     void resyncWithRateLimit();
443private:
444
445    /* ------------------------------------------------------------------------
446     * Debugging & dumpsys
447     */
448    void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const;
449    void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const;
450    void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result);
451    void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const;
452    bool startDdmConnection();
453    void appendSfConfigString(String8& result) const;
454    void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
455            const sp<const DisplayDevice>& hw,
456            int32_t minLayerZ, int32_t maxLayerZ);
457
458    void logFrameStats();
459
460    void dumpStaticScreenStats(String8& result) const;
461    // Not const because each Layer needs to query Fences and cache timestamps.
462    void dumpFrameEventsLocked(String8& result);
463
464    void recordBufferingStats(const char* layerName,
465            std::vector<OccupancyTracker::Segment>&& history);
466    void dumpBufferingStats(String8& result) const;
467
468    bool isLayerTripleBufferingDisabled() const {
469        return this->mLayerTripleBufferingDisabled;
470    }
471
472#ifdef USE_HWC2
473    /* ------------------------------------------------------------------------
474     * VrFlinger
475     */
476    void clearHwcLayers(const LayerVector& layers);
477    void resetHwc();
478
479    // Check to see if we should change to or from vr mode, and if so, perform
480    // the handoff.
481    void updateVrMode();
482#endif
483
484    /* ------------------------------------------------------------------------
485     * Attributes
486     */
487
488    // access must be protected by mStateLock
489    mutable Mutex mStateLock;
490    State mCurrentState;
491    volatile int32_t mTransactionFlags;
492    Condition mTransactionCV;
493    bool mTransactionPending;
494    bool mAnimTransactionPending;
495    SortedVector< sp<Layer> > mLayersPendingRemoval;
496    SortedVector< wp<IBinder> > mGraphicBufferProducerList;
497
498    // protected by mStateLock (but we could use another lock)
499    bool mLayersRemoved;
500    bool mLayersAdded;
501
502    // access must be protected by mInvalidateLock
503    volatile int32_t mRepaintEverything;
504
505    // current, real and vr hardware composers.
506    HWComposer* mHwc;
507#ifdef USE_HWC2
508    HWComposer* mRealHwc;
509    HWComposer* mVrHwc;
510#endif
511    // constant members (no synchronization needed for access)
512    RenderEngine* mRenderEngine;
513    nsecs_t mBootTime;
514    bool mGpuToCpuSupported;
515    sp<EventThread> mEventThread;
516    sp<EventThread> mSFEventThread;
517    sp<EventThread> mInjectorEventThread;
518    sp<InjectVSyncSource> mVSyncInjector;
519    sp<EventControlThread> mEventControlThread;
520    EGLContext mEGLContext;
521    EGLDisplay mEGLDisplay;
522    sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
523
524#ifdef USE_HWC2
525    std::unique_ptr<dvr::VrFlinger> mVrFlinger;
526#endif
527
528    // Can only accessed from the main thread, these members
529    // don't need synchronization
530    State mDrawingState;
531    bool mVisibleRegionsDirty;
532#ifndef USE_HWC2
533    bool mHwWorkListDirty;
534#else
535    bool mGeometryInvalid;
536#endif
537    bool mAnimCompositionPending;
538#ifdef USE_HWC2
539    std::vector<sp<Layer>> mLayersWithQueuedFrames;
540    sp<Fence> mPreviousPresentFence = Fence::NO_FENCE;
541    bool mHadClientComposition = false;
542#endif
543    FenceTimeline mGlCompositionDoneTimeline;
544    FenceTimeline mDisplayTimeline;
545
546    // this may only be written from the main thread with mStateLock held
547    // it may be read from other threads with mStateLock held
548    DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
549
550    // don't use a lock for these, we don't care
551    int mDebugRegion;
552    int mDebugDDMS;
553    int mDebugDisableHWC;
554    int mDebugDisableTransformHint;
555    volatile nsecs_t mDebugInSwapBuffers;
556    nsecs_t mLastSwapBufferTime;
557    volatile nsecs_t mDebugInTransaction;
558    nsecs_t mLastTransactionTime;
559    bool mBootFinished;
560    bool mForceFullDamage;
561#ifdef USE_HWC2
562    bool mPropagateBackpressure = true;
563#endif
564    SurfaceInterceptor mInterceptor;
565    bool mUseHwcVirtualDisplays = false;
566
567    // Restrict layers to use two buffers in their bufferqueues.
568    bool mLayerTripleBufferingDisabled = false;
569
570    // these are thread safe
571    mutable MessageQueue mEventQueue;
572    FrameTracker mAnimFrameTracker;
573    DispSync mPrimaryDispSync;
574
575    // protected by mDestroyedLayerLock;
576    mutable Mutex mDestroyedLayerLock;
577    Vector<Layer const *> mDestroyedLayers;
578
579    // protected by mHWVsyncLock
580    Mutex mHWVsyncLock;
581    bool mPrimaryHWVsyncEnabled;
582    bool mHWVsyncAvailable;
583
584    /* ------------------------------------------------------------------------
585     * Feature prototyping
586     */
587
588    bool mInjectVSyncs;
589
590    Daltonizer mDaltonizer;
591#ifndef USE_HWC2
592    bool mDaltonize;
593#endif
594
595    mat4 mPreviousColorMatrix;
596    mat4 mColorMatrix;
597    bool mHasColorMatrix;
598
599    // Static screen stats
600    bool mHasPoweredOff;
601    static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
602    nsecs_t mFrameBuckets[NUM_BUCKETS];
603    nsecs_t mTotalTime;
604    std::atomic<nsecs_t> mLastSwapTime;
605
606    size_t mNumLayers;
607
608    // Double- vs. triple-buffering stats
609    struct BufferingStats {
610        BufferingStats()
611          : numSegments(0),
612            totalTime(0),
613            twoBufferTime(0),
614            doubleBufferedTime(0),
615            tripleBufferedTime(0) {}
616
617        size_t numSegments;
618        nsecs_t totalTime;
619
620        // "Two buffer" means that a third buffer was never used, whereas
621        // "double-buffered" means that on average the segment only used two
622        // buffers (though it may have used a third for some part of the
623        // segment)
624        nsecs_t twoBufferTime;
625        nsecs_t doubleBufferedTime;
626        nsecs_t tripleBufferedTime;
627    };
628    mutable Mutex mBufferingStatsMutex;
629    std::unordered_map<std::string, BufferingStats> mBufferingStats;
630
631    // Verify that transaction is being called by an approved process:
632    // either AID_GRAPHICS or AID_SYSTEM.
633    status_t CheckTransactCodeCredentials(uint32_t code);
634
635#ifdef USE_HWC2
636    sp<VrStateCallbacks> mVrStateCallbacks;
637
638    std::atomic<bool> mEnterVrMode;
639#endif
640    };
641
642}; // namespace android
643
644#endif // ANDROID_SURFACE_FLINGER_H
645