SurfaceFlinger.h revision bb5afada05e597b8a74e3888863319c319c3c944
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(int type, nsecs_t timestamp);
251    virtual void onHotplugReceived(int disp, bool connected);
252
253    /* ------------------------------------------------------------------------
254     * Message handling
255     */
256    void waitForEvent();
257    void signalTransaction();
258    void signalLayerUpdate();
259    void signalRefresh();
260
261    // called on the main thread in response to initializeDisplays()
262    void onInitializeDisplays();
263    // called on the main thread in response to setActiveConfig()
264    void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode);
265    // called on the main thread in response to setPowerMode()
266    void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode);
267
268    // Called on the main thread in response to setActiveColorMode()
269    void setActiveColorModeInternal(const sp<DisplayDevice>& hw, android_color_mode_t colorMode);
270
271    // Returns whether the transaction actually modified any state
272    bool handleMessageTransaction();
273
274    // Returns whether a new buffer has been latched (see handlePageFlip())
275    bool handleMessageInvalidate();
276
277    void handleMessageRefresh();
278
279    void handleTransaction(uint32_t transactionFlags);
280    void handleTransactionLocked(uint32_t transactionFlags);
281
282    void updateCursorAsync();
283
284    /* handlePageFlip - latch a new buffer if available and compute the dirty
285     * region. Returns whether a new buffer has been latched, i.e., whether it
286     * is necessary to perform a refresh during this vsync.
287     */
288    bool handlePageFlip();
289
290    /* ------------------------------------------------------------------------
291     * Transactions
292     */
293    uint32_t getTransactionFlags(uint32_t flags);
294    uint32_t peekTransactionFlags();
295    uint32_t setTransactionFlags(uint32_t flags);
296    void commitTransaction();
297    uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
298    uint32_t setDisplayStateLocked(const DisplayState& s);
299
300    /* ------------------------------------------------------------------------
301     * Layer management
302     */
303    status_t createLayer(const String8& name, const sp<Client>& client,
304            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
305            uint32_t windowType, uint32_t ownerUid, sp<IBinder>* handle,
306            sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent);
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 wp<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            const sp<Layer>& parent);
335
336    /* ------------------------------------------------------------------------
337     * Boot animation, on/off animations and screen capture
338     */
339
340    void startBootAnim();
341
342    void renderScreenImplLocked(
343            const sp<const DisplayDevice>& hw,
344            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
345            int32_t minLayerZ, int32_t maxLayerZ,
346            bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
347
348    status_t captureScreenImplLocked(
349            const sp<const DisplayDevice>& hw,
350            const sp<IGraphicBufferProducer>& producer,
351            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
352            int32_t minLayerZ, int32_t maxLayerZ,
353            bool useIdentityTransform, Transform::orientation_flags rotation,
354            bool isLocalScreenshot);
355
356    sp<StartBootAnimThread> mStartBootAnimThread = nullptr;
357
358    /* ------------------------------------------------------------------------
359     * EGL
360     */
361    size_t getMaxTextureSize() const;
362    size_t getMaxViewportDims() const;
363
364    /* ------------------------------------------------------------------------
365     * Display and layer stack management
366     */
367    // called when starting, or restarting after system_server death
368    void initializeDisplays();
369
370    // Create an IBinder for a builtin display and add it to current state
371    void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
372
373    // NOTE: can only be called from the main thread or with mStateLock held
374    sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
375        return mDisplays.valueFor(dpy);
376    }
377
378    // NOTE: can only be called from the main thread or with mStateLock held
379    sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
380        return mDisplays.valueFor(dpy);
381    }
382
383    int32_t getDisplayType(const sp<IBinder>& display) {
384        if (!display.get()) return NAME_NOT_FOUND;
385        for (int i = 0; i < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES; ++i) {
386            if (display == mBuiltinDisplays[i]) {
387                return i;
388            }
389        }
390        return NAME_NOT_FOUND;
391    }
392
393    // mark a region of a layer stack dirty. this updates the dirty
394    // region of all screens presenting this layer stack.
395    void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
396
397#ifndef USE_HWC2
398    int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
399#endif
400
401    /* ------------------------------------------------------------------------
402     * H/W composer
403     */
404
405    HWComposer& getHwComposer() const { return *mHwc; }
406
407    /* ------------------------------------------------------------------------
408     * Compositing
409     */
410    void invalidateHwcGeometry();
411    void computeVisibleRegions(uint32_t layerStack,
412            Region& dirtyRegion, Region& opaqueRegion);
413
414    void preComposition(nsecs_t refreshStartTime);
415    void postComposition();
416    void rebuildLayerStacks();
417    void setUpHWComposer();
418    void doComposition();
419    void doDebugFlashRegions();
420    void doDisplayComposition(const sp<const DisplayDevice>& displayDevice, const Region& dirtyRegion);
421
422    // compose surfaces for display hw. this fails if using GL and the surface
423    // has been destroyed and is no longer valid.
424    bool doComposeSurfaces(const sp<const DisplayDevice>& displayDevice, const Region& dirty);
425
426    void postFramebuffer();
427    void drawWormhole(const sp<const DisplayDevice>& displayDevice, const Region& region) const;
428
429    /* ------------------------------------------------------------------------
430     * Display management
431     */
432
433    /* ------------------------------------------------------------------------
434     * VSync
435     */
436     void enableHardwareVsync();
437     void resyncToHardwareVsync(bool makeAvailable);
438     void disableHardwareVsync(bool makeUnavailable);
439
440public:
441     void resyncWithRateLimit();
442private:
443
444    /* ------------------------------------------------------------------------
445     * Debugging & dumpsys
446     */
447    void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const;
448    void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const;
449    void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result);
450    void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const;
451    bool startDdmConnection();
452    void appendSfConfigString(String8& result) const;
453    void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
454            const sp<const DisplayDevice>& hw,
455            int32_t minLayerZ, int32_t maxLayerZ);
456
457    void logFrameStats();
458
459    void dumpStaticScreenStats(String8& result) const;
460    // Not const because each Layer needs to query Fences and cache timestamps.
461    void dumpFrameEventsLocked(String8& result);
462
463    void recordBufferingStats(const char* layerName,
464            std::vector<OccupancyTracker::Segment>&& history);
465    void dumpBufferingStats(String8& result) const;
466
467    bool isLayerTripleBufferingDisabled() const {
468        return this->mLayerTripleBufferingDisabled;
469    }
470
471#ifdef USE_HWC2
472    /* ------------------------------------------------------------------------
473     * VrFlinger
474     */
475    void clearHwcLayers(const LayerVector& layers);
476    void resetHwc();
477
478    // Check to see if we should change to or from vr mode, and if so, perform
479    // the handoff.
480    void updateVrMode();
481#endif
482
483    /* ------------------------------------------------------------------------
484     * Attributes
485     */
486
487    // access must be protected by mStateLock
488    mutable Mutex mStateLock;
489    State mCurrentState;
490    volatile int32_t mTransactionFlags;
491    Condition mTransactionCV;
492    bool mTransactionPending;
493    bool mAnimTransactionPending;
494    SortedVector< sp<Layer> > mLayersPendingRemoval;
495    SortedVector< wp<IBinder> > mGraphicBufferProducerList;
496
497    // protected by mStateLock (but we could use another lock)
498    bool mLayersRemoved;
499    bool mLayersAdded;
500
501    // access must be protected by mInvalidateLock
502    volatile int32_t mRepaintEverything;
503
504    // current, real and vr hardware composers.
505    HWComposer* mHwc;
506#ifdef USE_HWC2
507    HWComposer* mRealHwc;
508    HWComposer* mVrHwc;
509#endif
510    // constant members (no synchronization needed for access)
511    RenderEngine* mRenderEngine;
512    nsecs_t mBootTime;
513    bool mGpuToCpuSupported;
514    sp<EventThread> mEventThread;
515    sp<EventThread> mSFEventThread;
516    sp<EventThread> mInjectorEventThread;
517    sp<InjectVSyncSource> mVSyncInjector;
518    sp<EventControlThread> mEventControlThread;
519    EGLContext mEGLContext;
520    EGLDisplay mEGLDisplay;
521    sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
522
523#ifdef USE_HWC2
524    std::unique_ptr<dvr::VrFlinger> mVrFlinger;
525#endif
526
527    // Can only accessed from the main thread, these members
528    // don't need synchronization
529    State mDrawingState;
530    bool mVisibleRegionsDirty;
531#ifndef USE_HWC2
532    bool mHwWorkListDirty;
533#else
534    bool mGeometryInvalid;
535#endif
536    bool mAnimCompositionPending;
537#ifdef USE_HWC2
538    std::vector<sp<Layer>> mLayersWithQueuedFrames;
539    sp<Fence> mPreviousPresentFence = Fence::NO_FENCE;
540    bool mHadClientComposition = false;
541#endif
542    FenceTimeline mGlCompositionDoneTimeline;
543    FenceTimeline mDisplayTimeline;
544
545    // this may only be written from the main thread with mStateLock held
546    // it may be read from other threads with mStateLock held
547    DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
548
549    // don't use a lock for these, we don't care
550    int mDebugRegion;
551    int mDebugDDMS;
552    int mDebugDisableHWC;
553    int mDebugDisableTransformHint;
554    volatile nsecs_t mDebugInSwapBuffers;
555    nsecs_t mLastSwapBufferTime;
556    volatile nsecs_t mDebugInTransaction;
557    nsecs_t mLastTransactionTime;
558    bool mBootFinished;
559    bool mForceFullDamage;
560#ifdef USE_HWC2
561    bool mPropagateBackpressure = true;
562#endif
563    SurfaceInterceptor mInterceptor;
564    bool mUseHwcVirtualDisplays = false;
565
566    // Restrict layers to use two buffers in their bufferqueues.
567    bool mLayerTripleBufferingDisabled = false;
568
569    // these are thread safe
570    mutable MessageQueue mEventQueue;
571    FrameTracker mAnimFrameTracker;
572    DispSync mPrimaryDispSync;
573
574    // protected by mDestroyedLayerLock;
575    mutable Mutex mDestroyedLayerLock;
576    Vector<Layer const *> mDestroyedLayers;
577
578    // protected by mHWVsyncLock
579    Mutex mHWVsyncLock;
580    bool mPrimaryHWVsyncEnabled;
581    bool mHWVsyncAvailable;
582
583    /* ------------------------------------------------------------------------
584     * Feature prototyping
585     */
586
587    bool mInjectVSyncs;
588
589    Daltonizer mDaltonizer;
590#ifndef USE_HWC2
591    bool mDaltonize;
592#endif
593
594    mat4 mPreviousColorMatrix;
595    mat4 mColorMatrix;
596    bool mHasColorMatrix;
597
598    // Static screen stats
599    bool mHasPoweredOff;
600    static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
601    nsecs_t mFrameBuckets[NUM_BUCKETS];
602    nsecs_t mTotalTime;
603    std::atomic<nsecs_t> mLastSwapTime;
604
605    size_t mNumLayers;
606
607    // Double- vs. triple-buffering stats
608    struct BufferingStats {
609        BufferingStats()
610          : numSegments(0),
611            totalTime(0),
612            twoBufferTime(0),
613            doubleBufferedTime(0),
614            tripleBufferedTime(0) {}
615
616        size_t numSegments;
617        nsecs_t totalTime;
618
619        // "Two buffer" means that a third buffer was never used, whereas
620        // "double-buffered" means that on average the segment only used two
621        // buffers (though it may have used a third for some part of the
622        // segment)
623        nsecs_t twoBufferTime;
624        nsecs_t doubleBufferedTime;
625        nsecs_t tripleBufferedTime;
626    };
627    mutable Mutex mBufferingStatsMutex;
628    std::unordered_map<std::string, BufferingStats> mBufferingStats;
629
630    // Verify that transaction is being called by an approved process:
631    // either AID_GRAPHICS or AID_SYSTEM.
632    status_t CheckTransactCodeCredentials(uint32_t code);
633
634#ifdef USE_HWC2
635    sp<VrStateCallbacks> mVrStateCallbacks;
636
637    std::atomic<bool> mEnterVrMode;
638#endif
639    };
640
641}; // namespace android
642
643#endif // ANDROID_SURFACE_FLINGER_H
644