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