SurfaceFlinger.h revision d8ab4396a858a9c929a1bb3cadf7705fb8061574
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_SURFACE_FLINGER_H
18#define ANDROID_SURFACE_FLINGER_H
19
20#include <memory>
21#include <stdint.h>
22#include <sys/types.h>
23
24#include <EGL/egl.h>
25
26/*
27 * NOTE: Make sure this file doesn't include  anything from <gl/ > or <gl2/ >
28 */
29
30#include <cutils/compiler.h>
31
32#include <utils/Atomic.h>
33#include <utils/Errors.h>
34#include <utils/KeyedVector.h>
35#include <utils/RefBase.h>
36#include <utils/SortedVector.h>
37#include <utils/threads.h>
38
39#include <ui/FenceTime.h>
40#include <ui/PixelFormat.h>
41#include <math/mat4.h>
42
43#include <gui/FrameTimestamps.h>
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 <mutex>
68#include <queue>
69#include <string>
70#include <utility>
71
72namespace android {
73
74// ---------------------------------------------------------------------------
75
76class Client;
77class DisplayEventConnection;
78class EventThread;
79class IGraphicBufferAlloc;
80class Layer;
81class LayerDim;
82class Surface;
83class RenderEngine;
84class EventControlThread;
85class VSyncSource;
86class InjectVSyncSource;
87class VrStateCallbacks;
88
89namespace dvr {
90class VrFlinger;
91} // namespace dvr
92
93// ---------------------------------------------------------------------------
94
95enum {
96    eTransactionNeeded        = 0x01,
97    eTraversalNeeded          = 0x02,
98    eDisplayTransactionNeeded = 0x04,
99    eTransactionMask          = 0x07
100};
101
102class SurfaceFlinger : public BnSurfaceComposer,
103                       private IBinder::DeathRecipient,
104                       private HWComposer::EventHandler
105{
106public:
107
108    // This is the phase offset in nanoseconds of the software vsync event
109    // relative to the vsync event reported by HWComposer.  The software vsync
110    // event is when SurfaceFlinger and Choreographer-based applications run each
111    // frame.
112    //
113    // This phase offset allows adjustment of the minimum latency from application
114    // wake-up time (by Choreographer) to the time at which the resulting window
115    // image is displayed.  This value may be either positive (after the HW vsync)
116    // or negative (before the HW vsync). Setting it to 0 will result in a lower
117    // latency bound of two vsync periods because the app and SurfaceFlinger
118    // will run just after the HW vsync.  Setting it to a positive number will
119    // result in the minimum latency being:
120    //
121    //     (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
122    //
123    // Note that reducing this latency makes it more likely for the applications
124    // to not have their window content image ready in time.  When this happens
125    // the latency will end up being an additional vsync period, and animations
126    // will hiccup.  Therefore, this latency should be tuned somewhat
127    // conservatively (or at least with awareness of the trade-off being made).
128    static int64_t vsyncPhaseOffsetNs;
129    static int64_t sfVsyncPhaseOffsetNs;
130
131    // If fences from sync Framework are supported.
132    static bool hasSyncFramework;
133
134    // Instruct the Render Engine to use EGL_IMG_context_priority is available.
135    static bool useContextPriority;
136
137    // The offset in nanoseconds to use when DispSync timestamps present fence
138    // signaling time.
139    static int64_t dispSyncPresentTimeOffset;
140
141    // Some hardware can do RGB->YUV conversion more efficiently in hardware
142    // controlled by HWC than in hardware controlled by the video encoder.
143    // This instruct VirtualDisplaySurface to use HWC for such conversion on
144    // GL composition.
145    static bool useHwcForRgbToYuv;
146
147    // Maximum dimension supported by HWC for virtual display.
148    // Equal to min(max_height, max_width).
149    static uint64_t maxVirtualDisplaySize;
150
151    static char const* getServiceName() ANDROID_API {
152        return "SurfaceFlinger";
153    }
154
155    SurfaceFlinger() ANDROID_API;
156
157    // must be called before clients can connect
158    void init() ANDROID_API;
159
160    // starts SurfaceFlinger main loop in the current thread
161    void run() ANDROID_API;
162
163    enum {
164        EVENT_VSYNC = HWC_EVENT_VSYNC
165    };
166
167    // post an asynchronous message to the main thread
168    status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
169
170    // post a synchronous message to the main thread
171    status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
172
173    // force full composition on all displays
174    void repaintEverything();
175
176    // returns the default Display
177    sp<const DisplayDevice> getDefaultDisplayDevice() const {
178        return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
179    }
180
181    // utility function to delete a texture on the main thread
182    void deleteTextureAsync(uint32_t texture);
183
184    // enable/disable h/w composer event
185    // TODO: this should be made accessible only to EventThread
186#ifdef USE_HWC2
187    void setVsyncEnabled(int disp, int enabled);
188#else
189    void eventControl(int disp, int event, int enabled);
190#endif
191
192    // called on the main thread by MessageQueue when an internal message
193    // is received
194    // TODO: this should be made accessible only to MessageQueue
195    void onMessageReceived(int32_t what);
196
197    // for debugging only
198    // TODO: this should be made accessible only to HWComposer
199    const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
200
201    RenderEngine& getRenderEngine() const {
202        return *mRenderEngine;
203    }
204
205    bool authenticateSurfaceTextureLocked(
206        const sp<IGraphicBufferProducer>& bufferProducer) const;
207
208private:
209    friend class Client;
210    friend class DisplayEventConnection;
211    friend class EventThread;
212    friend class Layer;
213    friend class MonitoredProducer;
214    friend class VrStateCallbacks;
215
216    // This value is specified in number of frames.  Log frame stats at most
217    // every half hour.
218    enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
219
220    static const size_t MAX_LAYERS = 4096;
221
222    // We're reference counted, never destroy SurfaceFlinger directly
223    virtual ~SurfaceFlinger();
224
225    /* ------------------------------------------------------------------------
226     * Internal data structures
227     */
228
229    class State {
230    public:
231        LayerVector layersSortedByZ;
232        DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
233
234        void traverseInZOrder(const std::function<void(Layer*)>& consume) const;
235        void traverseInReverseZOrder(const std::function<void(Layer*)>& consume) const;
236    };
237
238    /* ------------------------------------------------------------------------
239     * IBinder interface
240     */
241    virtual status_t onTransact(uint32_t code, const Parcel& data,
242        Parcel* reply, uint32_t flags);
243    virtual status_t dump(int fd, const Vector<String16>& args);
244
245    /* ------------------------------------------------------------------------
246     * ISurfaceComposer interface
247     */
248    virtual sp<ISurfaceComposerClient> createConnection();
249    virtual sp<ISurfaceComposerClient> createScopedConnection(const sp<IGraphicBufferProducer>& gbp);
250    virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
251    virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
252    virtual void destroyDisplay(const sp<IBinder>& display);
253    virtual sp<IBinder> getBuiltInDisplay(int32_t id);
254    virtual void setTransactionState(const Vector<ComposerState>& state,
255            const Vector<DisplayState>& displays, uint32_t flags);
256    virtual void bootFinished();
257    virtual bool authenticateSurfaceTexture(
258        const sp<IGraphicBufferProducer>& bufferProducer) const;
259    virtual sp<IDisplayEventConnection> createDisplayEventConnection();
260    virtual status_t captureScreen(const sp<IBinder>& display,
261            const sp<IGraphicBufferProducer>& producer,
262            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
263            int32_t minLayerZ, int32_t maxLayerZ,
264            bool useIdentityTransform, ISurfaceComposer::Rotation rotation);
265    virtual status_t getDisplayStats(const sp<IBinder>& display,
266            DisplayStatInfo* stats);
267    virtual status_t getDisplayConfigs(const sp<IBinder>& display,
268            Vector<DisplayInfo>* configs);
269    virtual int getActiveConfig(const sp<IBinder>& display);
270    virtual status_t getDisplayColorModes(const sp<IBinder>& display,
271            Vector<android_color_mode_t>* configs);
272    virtual android_color_mode_t getActiveColorMode(const sp<IBinder>& display);
273    virtual status_t setActiveColorMode(const sp<IBinder>& display, android_color_mode_t colorMode);
274    virtual void setPowerMode(const sp<IBinder>& display, int mode);
275    virtual status_t setActiveConfig(const sp<IBinder>& display, int id);
276    virtual status_t clearAnimationFrameStats();
277    virtual status_t getAnimationFrameStats(FrameStats* outStats) const;
278    virtual status_t getHdrCapabilities(const sp<IBinder>& display,
279            HdrCapabilities* outCapabilities) const;
280    virtual status_t enableVSyncInjections(bool enable);
281    virtual status_t injectVSync(nsecs_t when);
282
283
284    /* ------------------------------------------------------------------------
285     * DeathRecipient interface
286     */
287    virtual void binderDied(const wp<IBinder>& who);
288
289    /* ------------------------------------------------------------------------
290     * RefBase interface
291     */
292    virtual void onFirstRef();
293
294    /* ------------------------------------------------------------------------
295     * HWComposer::EventHandler interface
296     */
297    virtual void onVSyncReceived(HWComposer* composer, int type, nsecs_t timestamp);
298    virtual void onHotplugReceived(int disp, bool connected);
299    virtual void onInvalidateReceived(HWComposer* composer);
300
301    /* ------------------------------------------------------------------------
302     * Message handling
303     */
304    void waitForEvent();
305    void signalTransaction();
306    void signalLayerUpdate();
307    void signalRefresh();
308
309    // called on the main thread in response to initializeDisplays()
310    void onInitializeDisplays();
311    // called on the main thread in response to setActiveConfig()
312    void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode);
313    // called on the main thread in response to setPowerMode()
314    void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode);
315
316    // Called on the main thread in response to setActiveColorMode()
317    void setActiveColorModeInternal(const sp<DisplayDevice>& hw, android_color_mode_t colorMode);
318
319    // Returns whether the transaction actually modified any state
320    bool handleMessageTransaction();
321
322    // Returns whether a new buffer has been latched (see handlePageFlip())
323    bool handleMessageInvalidate();
324
325    void handleMessageRefresh();
326
327    void handleTransaction(uint32_t transactionFlags);
328    void handleTransactionLocked(uint32_t transactionFlags);
329
330    void updateCursorAsync();
331
332    /* handlePageFlip - latch a new buffer if available and compute the dirty
333     * region. Returns whether a new buffer has been latched, i.e., whether it
334     * is necessary to perform a refresh during this vsync.
335     */
336    bool handlePageFlip();
337
338    /* ------------------------------------------------------------------------
339     * Transactions
340     */
341    uint32_t getTransactionFlags(uint32_t flags);
342    uint32_t peekTransactionFlags();
343    uint32_t setTransactionFlags(uint32_t flags);
344    void commitTransaction();
345    uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
346    uint32_t setDisplayStateLocked(const DisplayState& s);
347
348    /* ------------------------------------------------------------------------
349     * Layer management
350     */
351    status_t createLayer(const String8& name, const sp<Client>& client,
352            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
353            uint32_t windowType, uint32_t ownerUid, sp<IBinder>* handle,
354            sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent);
355
356    status_t createNormalLayer(const sp<Client>& client, const String8& name,
357            uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
358            sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
359            sp<Layer>* outLayer);
360
361    status_t createDimLayer(const sp<Client>& client, const String8& name,
362            uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
363            sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
364
365    // called in response to the window-manager calling
366    // ISurfaceComposerClient::destroySurface()
367    status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
368
369    // called when all clients have released all their references to
370    // this layer meaning it is entirely safe to destroy all
371    // resources associated to this layer.
372    status_t onLayerDestroyed(const wp<Layer>& layer);
373
374    // remove a layer from SurfaceFlinger immediately
375    status_t removeLayer(const sp<Layer>& layer);
376
377    // add a layer to SurfaceFlinger
378    status_t addClientLayer(const sp<Client>& client,
379            const sp<IBinder>& handle,
380            const sp<IGraphicBufferProducer>& gbc,
381            const sp<Layer>& lbc,
382            const sp<Layer>& parent);
383
384    /* ------------------------------------------------------------------------
385     * Boot animation, on/off animations and screen capture
386     */
387
388    void startBootAnim();
389
390    void renderScreenImplLocked(
391            const sp<const DisplayDevice>& hw,
392            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
393            int32_t minLayerZ, int32_t maxLayerZ,
394            bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
395
396    status_t captureScreenImplLocked(
397            const sp<const DisplayDevice>& hw,
398            const sp<IGraphicBufferProducer>& producer,
399            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
400            int32_t minLayerZ, int32_t maxLayerZ,
401            bool useIdentityTransform, Transform::orientation_flags rotation,
402            bool isLocalScreenshot);
403
404    sp<StartBootAnimThread> mStartBootAnimThread = nullptr;
405
406    /* ------------------------------------------------------------------------
407     * EGL
408     */
409    size_t getMaxTextureSize() const;
410    size_t getMaxViewportDims() const;
411
412    /* ------------------------------------------------------------------------
413     * Display and layer stack management
414     */
415    // called when starting, or restarting after system_server death
416    void initializeDisplays();
417
418    // Create an IBinder for a builtin display and add it to current state
419    void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
420
421    // NOTE: can only be called from the main thread or with mStateLock held
422    sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
423        return mDisplays.valueFor(dpy);
424    }
425
426    // NOTE: can only be called from the main thread or with mStateLock held
427    sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
428        return mDisplays.valueFor(dpy);
429    }
430
431    int32_t getDisplayType(const sp<IBinder>& display) {
432        if (!display.get()) return NAME_NOT_FOUND;
433        for (int i = 0; i < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES; ++i) {
434            if (display == mBuiltinDisplays[i]) {
435                return i;
436            }
437        }
438        return NAME_NOT_FOUND;
439    }
440
441    // mark a region of a layer stack dirty. this updates the dirty
442    // region of all screens presenting this layer stack.
443    void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
444
445#ifndef USE_HWC2
446    int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
447#endif
448
449    /* ------------------------------------------------------------------------
450     * H/W composer
451     */
452
453    HWComposer& getHwComposer() const { return *mHwc; }
454
455    /* ------------------------------------------------------------------------
456     * Compositing
457     */
458    void invalidateHwcGeometry();
459    void computeVisibleRegions(uint32_t layerStack,
460            Region& dirtyRegion, Region& opaqueRegion);
461
462    void preComposition(nsecs_t refreshStartTime);
463    void postComposition(nsecs_t refreshStartTime);
464    void updateCompositorTiming(
465            nsecs_t vsyncPhase, nsecs_t vsyncInterval, nsecs_t compositeTime,
466            std::shared_ptr<FenceTime>& presentFenceTime);
467    void setCompositorTimingSnapped(
468            nsecs_t vsyncPhase, nsecs_t vsyncInterval,
469            nsecs_t compositeToPresentLatency);
470    void rebuildLayerStacks();
471    void setUpHWComposer();
472    void doComposition();
473    void doDebugFlashRegions();
474    void doDisplayComposition(const sp<const DisplayDevice>& displayDevice, const Region& dirtyRegion);
475
476    // compose surfaces for display hw. this fails if using GL and the surface
477    // has been destroyed and is no longer valid.
478    bool doComposeSurfaces(const sp<const DisplayDevice>& displayDevice, const Region& dirty);
479
480    void postFramebuffer();
481    void drawWormhole(const sp<const DisplayDevice>& displayDevice, const Region& region) const;
482
483    /* ------------------------------------------------------------------------
484     * Display management
485     */
486
487    /* ------------------------------------------------------------------------
488     * VSync
489     */
490    void enableHardwareVsync();
491    void resyncToHardwareVsync(bool makeAvailable);
492    void disableHardwareVsync(bool makeUnavailable);
493
494public:
495    void resyncWithRateLimit();
496    void getCompositorTiming(CompositorTiming* compositorTiming);
497private:
498
499    /* ------------------------------------------------------------------------
500     * Debugging & dumpsys
501     */
502    void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const;
503    void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const;
504    void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result);
505    void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const;
506    bool startDdmConnection();
507    void appendSfConfigString(String8& result) const;
508    void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
509            const sp<const DisplayDevice>& hw,
510            int32_t minLayerZ, int32_t maxLayerZ);
511
512    void logFrameStats();
513
514    void dumpStaticScreenStats(String8& result) const;
515    // Not const because each Layer needs to query Fences and cache timestamps.
516    void dumpFrameEventsLocked(String8& result);
517
518    void recordBufferingStats(const char* layerName,
519            std::vector<OccupancyTracker::Segment>&& history);
520    void dumpBufferingStats(String8& result) const;
521
522    bool isLayerTripleBufferingDisabled() const {
523        return this->mLayerTripleBufferingDisabled;
524    }
525
526#ifdef USE_HWC2
527    /* ------------------------------------------------------------------------
528     * VrFlinger
529     */
530    void clearHwcLayers(const LayerVector& layers);
531    void resetHwc();
532
533    // Check to see if we should change to or from vr mode, and if so, perform
534    // the handoff.
535    void updateVrMode();
536#endif
537
538    /* ------------------------------------------------------------------------
539     * Attributes
540     */
541
542    // access must be protected by mStateLock
543    mutable Mutex mStateLock;
544    State mCurrentState;
545    volatile int32_t mTransactionFlags;
546    Condition mTransactionCV;
547    bool mTransactionPending;
548    bool mAnimTransactionPending;
549    SortedVector< sp<Layer> > mLayersPendingRemoval;
550    SortedVector< wp<IBinder> > mGraphicBufferProducerList;
551
552    // protected by mStateLock (but we could use another lock)
553    bool mLayersRemoved;
554    bool mLayersAdded;
555
556    // access must be protected by mInvalidateLock
557    volatile int32_t mRepaintEverything;
558
559    // current, real and vr hardware composers.
560    HWComposer* mHwc;
561#ifdef USE_HWC2
562    HWComposer* mRealHwc;
563    HWComposer* mVrHwc;
564#endif
565    // constant members (no synchronization needed for access)
566    RenderEngine* mRenderEngine;
567    nsecs_t mBootTime;
568    bool mGpuToCpuSupported;
569    sp<EventThread> mEventThread;
570    sp<EventThread> mSFEventThread;
571    sp<EventThread> mInjectorEventThread;
572    sp<InjectVSyncSource> mVSyncInjector;
573    sp<EventControlThread> mEventControlThread;
574    EGLContext mEGLContext;
575    EGLDisplay mEGLDisplay;
576    sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
577
578#ifdef USE_HWC2
579    std::unique_ptr<dvr::VrFlinger> mVrFlinger;
580#endif
581
582    // Can only accessed from the main thread, these members
583    // don't need synchronization
584    State mDrawingState;
585    bool mVisibleRegionsDirty;
586#ifndef USE_HWC2
587    bool mHwWorkListDirty;
588#else
589    bool mGeometryInvalid;
590#endif
591    bool mAnimCompositionPending;
592#ifdef USE_HWC2
593    std::vector<sp<Layer>> mLayersWithQueuedFrames;
594    sp<Fence> mPreviousPresentFence = Fence::NO_FENCE;
595    bool mHadClientComposition = false;
596#endif
597    FenceTimeline mGlCompositionDoneTimeline;
598    FenceTimeline mDisplayTimeline;
599
600    // this may only be written from the main thread with mStateLock held
601    // it may be read from other threads with mStateLock held
602    DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
603
604    // don't use a lock for these, we don't care
605    int mVrModeSupported;
606    int mDebugRegion;
607    int mDebugDDMS;
608    int mDebugDisableHWC;
609    int mDebugDisableTransformHint;
610    volatile nsecs_t mDebugInSwapBuffers;
611    nsecs_t mLastSwapBufferTime;
612    volatile nsecs_t mDebugInTransaction;
613    nsecs_t mLastTransactionTime;
614    bool mBootFinished;
615    bool mForceFullDamage;
616#ifdef USE_HWC2
617    bool mPropagateBackpressure = true;
618#endif
619    SurfaceInterceptor mInterceptor;
620    bool mUseHwcVirtualDisplays = false;
621
622    // Restrict layers to use two buffers in their bufferqueues.
623    bool mLayerTripleBufferingDisabled = false;
624
625    // these are thread safe
626    mutable MessageQueue mEventQueue;
627    FrameTracker mAnimFrameTracker;
628    DispSync mPrimaryDispSync;
629
630    // protected by mDestroyedLayerLock;
631    mutable Mutex mDestroyedLayerLock;
632    Vector<Layer const *> mDestroyedLayers;
633
634    // protected by mHWVsyncLock
635    Mutex mHWVsyncLock;
636    bool mPrimaryHWVsyncEnabled;
637    bool mHWVsyncAvailable;
638
639    // protected by mCompositorTimingLock;
640    mutable std::mutex mCompositorTimingLock;
641    CompositorTiming mCompositorTiming;
642
643    // Only accessed from the main thread.
644    struct CompositePresentTime {
645        nsecs_t composite { -1 };
646        std::shared_ptr<FenceTime> display { FenceTime::NO_FENCE };
647    };
648    std::queue<CompositePresentTime> mCompositePresentTimes;
649
650    /* ------------------------------------------------------------------------
651     * Feature prototyping
652     */
653
654    bool mInjectVSyncs;
655
656    Daltonizer mDaltonizer;
657#ifndef USE_HWC2
658    bool mDaltonize;
659#endif
660
661    mat4 mPreviousColorMatrix;
662    mat4 mColorMatrix;
663    bool mHasColorMatrix;
664
665    // Static screen stats
666    bool mHasPoweredOff;
667    static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
668    nsecs_t mFrameBuckets[NUM_BUCKETS];
669    nsecs_t mTotalTime;
670    std::atomic<nsecs_t> mLastSwapTime;
671
672    size_t mNumLayers;
673
674    // Double- vs. triple-buffering stats
675    struct BufferingStats {
676        BufferingStats()
677          : numSegments(0),
678            totalTime(0),
679            twoBufferTime(0),
680            doubleBufferedTime(0),
681            tripleBufferedTime(0) {}
682
683        size_t numSegments;
684        nsecs_t totalTime;
685
686        // "Two buffer" means that a third buffer was never used, whereas
687        // "double-buffered" means that on average the segment only used two
688        // buffers (though it may have used a third for some part of the
689        // segment)
690        nsecs_t twoBufferTime;
691        nsecs_t doubleBufferedTime;
692        nsecs_t tripleBufferedTime;
693    };
694    mutable Mutex mBufferingStatsMutex;
695    std::unordered_map<std::string, BufferingStats> mBufferingStats;
696
697    // Verify that transaction is being called by an approved process:
698    // either AID_GRAPHICS or AID_SYSTEM.
699    status_t CheckTransactCodeCredentials(uint32_t code);
700
701#ifdef USE_HWC2
702    sp<VrStateCallbacks> mVrStateCallbacks;
703
704    std::atomic<bool> mEnterVrMode;
705#endif
706    };
707}; // namespace android
708
709#endif // ANDROID_SURFACE_FLINGER_H
710