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