SurfaceFlinger.h revision 0a61b0c813f5991bf462e36a2314dda062727a10
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/FenceTime.h>
41#include <ui/PixelFormat.h>
42#include <ui/mat4.h>
43
44#include <gui/FrameTimestamps.h>
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 <mutex>
69#include <queue>
70#include <string>
71#include <utility>
72
73namespace android {
74
75// ---------------------------------------------------------------------------
76
77class Client;
78class DisplayEventConnection;
79class EventThread;
80class IGraphicBufferAlloc;
81class Layer;
82class LayerDim;
83class Surface;
84class RenderEngine;
85class EventControlThread;
86class VSyncSource;
87class InjectVSyncSource;
88
89// ---------------------------------------------------------------------------
90
91enum {
92    eTransactionNeeded        = 0x01,
93    eTraversalNeeded          = 0x02,
94    eDisplayTransactionNeeded = 0x04,
95    eTransactionMask          = 0x07
96};
97
98class SurfaceFlinger : public BnSurfaceComposer,
99                       private IBinder::DeathRecipient,
100                       private HWComposer::EventHandler
101{
102public:
103    static char const* getServiceName() ANDROID_API {
104        return "SurfaceFlinger";
105    }
106
107    SurfaceFlinger() ANDROID_API;
108
109    // must be called before clients can connect
110    void init() ANDROID_API;
111
112    // starts SurfaceFlinger main loop in the current thread
113    void run() ANDROID_API;
114
115    enum {
116        EVENT_VSYNC = HWC_EVENT_VSYNC
117    };
118
119    // post an asynchronous message to the main thread
120    status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
121
122    // post a synchronous message to the main thread
123    status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
124
125    // force full composition on all displays
126    void repaintEverything();
127
128    // returns the default Display
129    sp<const DisplayDevice> getDefaultDisplayDevice() const {
130        return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
131    }
132
133    // utility function to delete a texture on the main thread
134    void deleteTextureAsync(uint32_t texture);
135
136    // enable/disable h/w composer event
137    // TODO: this should be made accessible only to EventThread
138#ifdef USE_HWC2
139    void setVsyncEnabled(int disp, int enabled);
140#else
141    void eventControl(int disp, int event, int enabled);
142#endif
143
144    // called on the main thread by MessageQueue when an internal message
145    // is received
146    // TODO: this should be made accessible only to MessageQueue
147    void onMessageReceived(int32_t what);
148
149    // for debugging only
150    // TODO: this should be made accessible only to HWComposer
151    const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
152
153    RenderEngine& getRenderEngine() const {
154        return *mRenderEngine;
155    }
156
157private:
158    friend class Client;
159    friend class DisplayEventConnection;
160    friend class EventThread;
161    friend class Layer;
162    friend class MonitoredProducer;
163
164    // This value is specified in number of frames.  Log frame stats at most
165    // every half hour.
166    enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
167
168    static const size_t MAX_LAYERS = 4096;
169
170    // We're reference counted, never destroy SurfaceFlinger directly
171    virtual ~SurfaceFlinger();
172
173    /* ------------------------------------------------------------------------
174     * Internal data structures
175     */
176
177    class State {
178    public:
179        LayerVector layersSortedByZ;
180        DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
181
182        void traverseInZOrder(const std::function<void(Layer*)>& consume) const;
183        void traverseInReverseZOrder(const std::function<void(Layer*)>& consume) const;
184    };
185
186    /* ------------------------------------------------------------------------
187     * IBinder interface
188     */
189    virtual status_t onTransact(uint32_t code, const Parcel& data,
190        Parcel* reply, uint32_t flags);
191    virtual status_t dump(int fd, const Vector<String16>& args);
192
193    /* ------------------------------------------------------------------------
194     * ISurfaceComposer interface
195     */
196    virtual sp<ISurfaceComposerClient> createConnection();
197    virtual sp<ISurfaceComposerClient> createScopedConnection(const sp<IGraphicBufferProducer>& gbp);
198    virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
199    virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
200    virtual void destroyDisplay(const sp<IBinder>& display);
201    virtual sp<IBinder> getBuiltInDisplay(int32_t id);
202    virtual void setTransactionState(const Vector<ComposerState>& state,
203            const Vector<DisplayState>& displays, uint32_t flags);
204    virtual void bootFinished();
205    virtual bool authenticateSurfaceTexture(
206        const sp<IGraphicBufferProducer>& bufferProducer) const;
207    virtual status_t getSupportedFrameTimestamps(
208            std::vector<FrameEvent>* outSupported) const;
209    virtual sp<IDisplayEventConnection> createDisplayEventConnection();
210    virtual status_t captureScreen(const sp<IBinder>& display,
211            const sp<IGraphicBufferProducer>& producer,
212            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
213            int32_t minLayerZ, int32_t maxLayerZ,
214            bool useIdentityTransform, ISurfaceComposer::Rotation rotation);
215    virtual status_t getDisplayStats(const sp<IBinder>& display,
216            DisplayStatInfo* stats);
217    virtual status_t getDisplayConfigs(const sp<IBinder>& display,
218            Vector<DisplayInfo>* configs);
219    virtual int getActiveConfig(const sp<IBinder>& display);
220    virtual status_t getDisplayColorModes(const sp<IBinder>& display,
221            Vector<android_color_mode_t>* configs);
222    virtual android_color_mode_t getActiveColorMode(const sp<IBinder>& display);
223    virtual status_t setActiveColorMode(const sp<IBinder>& display, android_color_mode_t colorMode);
224    virtual void setPowerMode(const sp<IBinder>& display, int mode);
225    virtual status_t setActiveConfig(const sp<IBinder>& display, int id);
226    virtual status_t clearAnimationFrameStats();
227    virtual status_t getAnimationFrameStats(FrameStats* outStats) const;
228    virtual status_t getHdrCapabilities(const sp<IBinder>& display,
229            HdrCapabilities* outCapabilities) const;
230    virtual status_t enableVSyncInjections(bool enable);
231    virtual status_t injectVSync(nsecs_t when);
232
233
234    /* ------------------------------------------------------------------------
235     * DeathRecipient interface
236     */
237    virtual void binderDied(const wp<IBinder>& who);
238
239    /* ------------------------------------------------------------------------
240     * RefBase interface
241     */
242    virtual void onFirstRef();
243
244    /* ------------------------------------------------------------------------
245     * HWComposer::EventHandler interface
246     */
247    virtual void onVSyncReceived(int type, nsecs_t timestamp);
248    virtual void onHotplugReceived(int disp, bool connected);
249
250    /* ------------------------------------------------------------------------
251     * Message handling
252     */
253    void waitForEvent();
254    void signalTransaction();
255    void signalLayerUpdate();
256    void signalRefresh();
257
258    // called on the main thread in response to initializeDisplays()
259    void onInitializeDisplays();
260    // called on the main thread in response to setActiveConfig()
261    void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode);
262    // called on the main thread in response to setPowerMode()
263    void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode);
264
265    // Called on the main thread in response to setActiveColorMode()
266    void setActiveColorModeInternal(const sp<DisplayDevice>& hw, android_color_mode_t colorMode);
267
268    // Returns whether the transaction actually modified any state
269    bool handleMessageTransaction();
270
271    // Returns whether a new buffer has been latched (see handlePageFlip())
272    bool handleMessageInvalidate();
273
274    void handleMessageRefresh();
275
276    void handleTransaction(uint32_t transactionFlags);
277    void handleTransactionLocked(uint32_t transactionFlags);
278
279    void updateCursorAsync();
280
281    /* handlePageFlip - latch a new buffer if available and compute the dirty
282     * region. Returns whether a new buffer has been latched, i.e., whether it
283     * is necessary to perform a refresh during this vsync.
284     */
285    bool handlePageFlip();
286
287    /* ------------------------------------------------------------------------
288     * Transactions
289     */
290    uint32_t getTransactionFlags(uint32_t flags);
291    uint32_t peekTransactionFlags();
292    uint32_t setTransactionFlags(uint32_t flags);
293    void commitTransaction();
294    uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
295    uint32_t setDisplayStateLocked(const DisplayState& s);
296
297    /* ------------------------------------------------------------------------
298     * Layer management
299     */
300    status_t createLayer(const String8& name, const sp<Client>& client,
301            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
302            uint32_t windowType, uint32_t ownerUid, sp<IBinder>* handle,
303            sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent);
304
305    status_t createNormalLayer(const sp<Client>& client, const String8& name,
306            uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
307            sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
308            sp<Layer>* outLayer);
309
310    status_t createDimLayer(const sp<Client>& client, const String8& name,
311            uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
312            sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
313
314    // called in response to the window-manager calling
315    // ISurfaceComposerClient::destroySurface()
316    status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
317
318    // called when all clients have released all their references to
319    // this layer meaning it is entirely safe to destroy all
320    // resources associated to this layer.
321    status_t onLayerDestroyed(const wp<Layer>& layer);
322
323    // remove a layer from SurfaceFlinger immediately
324    status_t removeLayer(const wp<Layer>& layer);
325
326    // add a layer to SurfaceFlinger
327    status_t addClientLayer(const sp<Client>& client,
328            const sp<IBinder>& handle,
329            const sp<IGraphicBufferProducer>& gbc,
330            const sp<Layer>& lbc,
331            const sp<Layer>& parent);
332
333    /* ------------------------------------------------------------------------
334     * Boot animation, on/off animations and screen capture
335     */
336
337    void startBootAnim();
338
339    void renderScreenImplLocked(
340            const sp<const DisplayDevice>& hw,
341            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
342            int32_t minLayerZ, int32_t maxLayerZ,
343            bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
344
345    status_t captureScreenImplLocked(
346            const sp<const DisplayDevice>& hw,
347            const sp<IGraphicBufferProducer>& producer,
348            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
349            int32_t minLayerZ, int32_t maxLayerZ,
350            bool useIdentityTransform, Transform::orientation_flags rotation,
351            bool isLocalScreenshot);
352
353    sp<StartBootAnimThread> mStartBootAnimThread = nullptr;
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    void computeVisibleRegions(uint32_t layerStack,
409            Region& dirtyRegion, Region& opaqueRegion);
410
411    void preComposition(nsecs_t refreshStartTime);
412    void postComposition(nsecs_t refreshStartTime);
413    void updateCompositorTiming(
414            nsecs_t vsyncPhase, nsecs_t vsyncInterval, nsecs_t compositeTime,
415            std::shared_ptr<FenceTime>& presentFenceTime);
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();
442    void getCompositorTiming(CompositorTiming* compositorTiming);
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     * Attributes
473     */
474
475    // access must be protected by mStateLock
476    mutable Mutex mStateLock;
477    State mCurrentState;
478    volatile int32_t mTransactionFlags;
479    Condition mTransactionCV;
480    bool mTransactionPending;
481    bool mAnimTransactionPending;
482    SortedVector< sp<Layer> > mLayersPendingRemoval;
483    SortedVector< wp<IBinder> > mGraphicBufferProducerList;
484
485    // protected by mStateLock (but we could use another lock)
486    bool mLayersRemoved;
487    bool mLayersAdded;
488
489    // access must be protected by mInvalidateLock
490    volatile int32_t mRepaintEverything;
491
492    // constant members (no synchronization needed for access)
493    HWComposer* mHwc;
494    RenderEngine* mRenderEngine;
495    nsecs_t mBootTime;
496    bool mGpuToCpuSupported;
497    sp<EventThread> mEventThread;
498    sp<EventThread> mSFEventThread;
499    sp<EventThread> mInjectorEventThread;
500    sp<InjectVSyncSource> mVSyncInjector;
501    sp<EventControlThread> mEventControlThread;
502    EGLContext mEGLContext;
503    EGLDisplay mEGLDisplay;
504    sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
505
506    // Can only accessed from the main thread, these members
507    // don't need synchronization
508    State mDrawingState;
509    bool mVisibleRegionsDirty;
510#ifndef USE_HWC2
511    bool mHwWorkListDirty;
512#else
513    bool mGeometryInvalid;
514#endif
515    bool mAnimCompositionPending;
516#ifdef USE_HWC2
517    std::vector<sp<Layer>> mLayersWithQueuedFrames;
518    sp<Fence> mPreviousPresentFence = Fence::NO_FENCE;
519    bool mHadClientComposition = false;
520#endif
521    FenceTimeline mGlCompositionDoneTimeline;
522    FenceTimeline mDisplayTimeline;
523
524    // this may only be written from the main thread with mStateLock held
525    // it may be read from other threads with mStateLock held
526    DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
527
528    // don't use a lock for these, we don't care
529    int mDebugRegion;
530    int mDebugDDMS;
531    int mDebugDisableHWC;
532    int mDebugDisableTransformHint;
533    volatile nsecs_t mDebugInSwapBuffers;
534    nsecs_t mLastSwapBufferTime;
535    volatile nsecs_t mDebugInTransaction;
536    nsecs_t mLastTransactionTime;
537    bool mBootFinished;
538    bool mForceFullDamage;
539#ifdef USE_HWC2
540    bool mPropagateBackpressure = true;
541#endif
542    SurfaceInterceptor mInterceptor;
543    bool mUseHwcVirtualDisplays = true;
544
545    // Restrict layers to use two buffers in their bufferqueues.
546    bool mLayerTripleBufferingDisabled = false;
547
548    // these are thread safe
549    mutable MessageQueue mEventQueue;
550    FrameTracker mAnimFrameTracker;
551    DispSync mPrimaryDispSync;
552
553    // protected by mDestroyedLayerLock;
554    mutable Mutex mDestroyedLayerLock;
555    Vector<Layer const *> mDestroyedLayers;
556
557    // protected by mHWVsyncLock
558    Mutex mHWVsyncLock;
559    bool mPrimaryHWVsyncEnabled;
560    bool mHWVsyncAvailable;
561
562    // protected by mCompositorTimingLock;
563    mutable std::mutex mCompositeTimingLock;
564    CompositorTiming mCompositorTiming;
565
566    // Only accessed from the main thread.
567    struct CompositePresentTime {
568        nsecs_t composite { -1 };
569        std::shared_ptr<FenceTime> display { FenceTime::NO_FENCE };
570    };
571    std::queue<CompositePresentTime> mCompositePresentTimes;
572
573    /* ------------------------------------------------------------------------
574     * Feature prototyping
575     */
576
577    bool mInjectVSyncs;
578
579    Daltonizer mDaltonizer;
580#ifndef USE_HWC2
581    bool mDaltonize;
582#endif
583
584    mat4 mPreviousColorMatrix;
585    mat4 mColorMatrix;
586    bool mHasColorMatrix;
587
588    // Static screen stats
589    bool mHasPoweredOff;
590    static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
591    nsecs_t mFrameBuckets[NUM_BUCKETS];
592    nsecs_t mTotalTime;
593    std::atomic<nsecs_t> mLastSwapTime;
594
595    size_t mNumLayers;
596
597    // Double- vs. triple-buffering stats
598    struct BufferingStats {
599        BufferingStats()
600          : numSegments(0),
601            totalTime(0),
602            twoBufferTime(0),
603            doubleBufferedTime(0),
604            tripleBufferedTime(0) {}
605
606        size_t numSegments;
607        nsecs_t totalTime;
608
609        // "Two buffer" means that a third buffer was never used, whereas
610        // "double-buffered" means that on average the segment only used two
611        // buffers (though it may have used a third for some part of the
612        // segment)
613        nsecs_t twoBufferTime;
614        nsecs_t doubleBufferedTime;
615        nsecs_t tripleBufferedTime;
616    };
617    mutable Mutex mBufferingStatsMutex;
618    std::unordered_map<std::string, BufferingStats> mBufferingStats;
619
620    // Verify that transaction is being called by an approved process:
621    // either AID_GRAPHICS or AID_SYSTEM.
622    status_t CheckTransactCodeCredentials(uint32_t code);
623    };
624
625}; // namespace android
626
627#endif // ANDROID_SURFACE_FLINGER_H
628