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