• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <sys/types.h>
20 
21 /*
22  * NOTE: Make sure this file doesn't include  anything from <gl/ > or <gl2/ >
23  */
24 
25 #include <android-base/thread_annotations.h>
26 #include <compositionengine/OutputColorSetting.h>
27 #include <cutils/atomic.h>
28 #include <cutils/compiler.h>
29 #include <gui/BufferQueue.h>
30 #include <gui/FrameTimestamps.h>
31 #include <gui/ISurfaceComposer.h>
32 #include <gui/ISurfaceComposerClient.h>
33 #include <gui/ITransactionCompletedListener.h>
34 #include <gui/LayerState.h>
35 #include <gui/OccupancyTracker.h>
36 #include <layerproto/LayerProtoHeader.h>
37 #include <math/mat4.h>
38 #include <renderengine/LayerSettings.h>
39 #include <serviceutils/PriorityDumper.h>
40 #include <system/graphics.h>
41 #include <ui/FenceTime.h>
42 #include <ui/PixelFormat.h>
43 #include <ui/Size.h>
44 #include <utils/Errors.h>
45 #include <utils/KeyedVector.h>
46 #include <utils/RefBase.h>
47 #include <utils/SortedVector.h>
48 #include <utils/Trace.h>
49 #include <utils/threads.h>
50 
51 #include "ClientCache.h"
52 #include "DisplayDevice.h"
53 #include "DisplayHardware/HWC2.h"
54 #include "DisplayHardware/PowerAdvisor.h"
55 #include "DisplayIdGenerator.h"
56 #include "Effects/Daltonizer.h"
57 #include "Fps.h"
58 #include "FrameTracker.h"
59 #include "LayerVector.h"
60 #include "Scheduler/RefreshRateConfigs.h"
61 #include "Scheduler/RefreshRateStats.h"
62 #include "Scheduler/Scheduler.h"
63 #include "Scheduler/VsyncModulator.h"
64 #include "SurfaceFlingerFactory.h"
65 #include "SurfaceTracing.h"
66 #include "TracedOrdinal.h"
67 #include "TransactionCallbackInvoker.h"
68 
69 #include <atomic>
70 #include <cstdint>
71 #include <functional>
72 #include <future>
73 #include <map>
74 #include <memory>
75 #include <mutex>
76 #include <optional>
77 #include <queue>
78 #include <set>
79 #include <string>
80 #include <thread>
81 #include <type_traits>
82 #include <unordered_map>
83 #include <unordered_set>
84 #include <utility>
85 
86 using namespace android::surfaceflinger;
87 
88 namespace android {
89 
90 class Client;
91 class EventThread;
92 class FpsReporter;
93 class TunnelModeEnabledReporter;
94 class HdrLayerInfoReporter;
95 class HWComposer;
96 struct SetInputWindowsListener;
97 class IGraphicBufferProducer;
98 class Layer;
99 class MessageBase;
100 class RefreshRateOverlay;
101 class RegionSamplingThread;
102 class RenderArea;
103 class TimeStats;
104 class FrameTracer;
105 
106 using gui::ScreenCaptureResults;
107 
108 namespace frametimeline {
109 class FrameTimeline;
110 }
111 
112 namespace os {
113     class IInputFlinger;
114 }
115 
116 namespace compositionengine {
117 class DisplaySurface;
118 class OutputLayer;
119 
120 struct CompositionRefreshArgs;
121 } // namespace compositionengine
122 
123 namespace renderengine {
124 class RenderEngine;
125 } // namespace renderengine
126 
127 enum {
128     eTransactionNeeded = 0x01,
129     eTraversalNeeded = 0x02,
130     eDisplayTransactionNeeded = 0x04,
131     eTransformHintUpdateNeeded = 0x08,
132     eTransactionFlushNeeded = 0x10,
133     eTransactionMask = 0x1f,
134 };
135 
136 using DisplayColorSetting = compositionengine::OutputColorSetting;
137 
138 struct SurfaceFlingerBE {
139     FenceTimeline mGlCompositionDoneTimeline;
140     FenceTimeline mDisplayTimeline;
141 
142     // protected by mCompositorTimingLock;
143     mutable std::mutex mCompositorTimingLock;
144     CompositorTiming mCompositorTiming;
145 
146     // Only accessed from the main thread.
147     struct CompositePresentTime {
148         nsecs_t composite = -1;
149         std::shared_ptr<FenceTime> display = FenceTime::NO_FENCE;
150     };
151     std::queue<CompositePresentTime> mCompositePresentTimes;
152 
153     static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
154     nsecs_t mFrameBuckets[NUM_BUCKETS] = {};
155     nsecs_t mTotalTime = 0;
156     std::atomic<nsecs_t> mLastSwapTime = 0;
157 
158     // Double- vs. triple-buffering stats
159     struct BufferingStats {
160         size_t numSegments = 0;
161         nsecs_t totalTime = 0;
162 
163         // "Two buffer" means that a third buffer was never used, whereas
164         // "double-buffered" means that on average the segment only used two
165         // buffers (though it may have used a third for some part of the
166         // segment)
167         nsecs_t twoBufferTime = 0;
168         nsecs_t doubleBufferedTime = 0;
169         nsecs_t tripleBufferedTime = 0;
170     };
171     mutable Mutex mBufferingStatsMutex;
172     std::unordered_map<std::string, BufferingStats> mBufferingStats;
173 };
174 
175 class SurfaceFlinger : public BnSurfaceComposer,
176                        public PriorityDumper,
177                        private IBinder::DeathRecipient,
178                        private HWC2::ComposerCallback,
179                        private ISchedulerCallback {
180 public:
181     struct SkipInitializationTag {};
182 
183     SurfaceFlinger(surfaceflinger::Factory&, SkipInitializationTag) ANDROID_API;
184     explicit SurfaceFlinger(surfaceflinger::Factory&) ANDROID_API;
185 
186     // set main thread scheduling policy
187     static status_t setSchedFifo(bool enabled) ANDROID_API;
188 
189     // set main thread scheduling attributes
190     static status_t setSchedAttr(bool enabled);
191 
getServiceName()192     static char const* getServiceName() ANDROID_API { return "SurfaceFlinger"; }
193 
194     // This is the phase offset in nanoseconds of the software vsync event
195     // relative to the vsync event reported by HWComposer.  The software vsync
196     // event is when SurfaceFlinger and Choreographer-based applications run each
197     // frame.
198     //
199     // This phase offset allows adjustment of the minimum latency from application
200     // wake-up time (by Choreographer) to the time at which the resulting window
201     // image is displayed.  This value may be either positive (after the HW vsync)
202     // or negative (before the HW vsync). Setting it to 0 will result in a lower
203     // latency bound of two vsync periods because the app and SurfaceFlinger
204     // will run just after the HW vsync.  Setting it to a positive number will
205     // result in the minimum latency being:
206     //
207     //     (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
208     //
209     // Note that reducing this latency makes it more likely for the applications
210     // to not have their window content image ready in time.  When this happens
211     // the latency will end up being an additional vsync period, and animations
212     // will hiccup.  Therefore, this latency should be tuned somewhat
213     // conservatively (or at least with awareness of the trade-off being made).
214     static int64_t vsyncPhaseOffsetNs;
215     static int64_t sfVsyncPhaseOffsetNs;
216 
217     // If fences from sync Framework are supported.
218     static bool hasSyncFramework;
219 
220     // The offset in nanoseconds to use when VsyncController timestamps present fence
221     // signaling time.
222     static int64_t dispSyncPresentTimeOffset;
223 
224     // Some hardware can do RGB->YUV conversion more efficiently in hardware
225     // controlled by HWC than in hardware controlled by the video encoder.
226     // This instruct VirtualDisplaySurface to use HWC for such conversion on
227     // GL composition.
228     static bool useHwcForRgbToYuv;
229 
230     // Controls the number of buffers SurfaceFlinger will allocate for use in
231     // FramebufferSurface
232     static int64_t maxFrameBufferAcquiredBuffers;
233 
234     // Controls the maximum width and height in pixels that the graphics pipeline can support for
235     // GPU fallback composition. For example, 8k devices with 4k GPUs, or 4k devices with 2k GPUs.
236     static uint32_t maxGraphicsWidth;
237     static uint32_t maxGraphicsHeight;
238 
239     // Indicate if a device has wide color gamut display. This is typically
240     // found on devices with wide color gamut (e.g. Display-P3) display.
241     static bool hasWideColorDisplay;
242 
243     static ui::Rotation internalDisplayOrientation;
244 
245     // Indicate if device wants color management on its display.
246     static bool useColorManagement;
247 
248     static bool useContextPriority;
249 
250     // The data space and pixel format that SurfaceFlinger expects hardware composer
251     // to composite efficiently. Meaning under most scenarios, hardware composer
252     // will accept layers with the data space and pixel format.
253     static ui::Dataspace defaultCompositionDataspace;
254     static ui::PixelFormat defaultCompositionPixelFormat;
255 
256     // The data space and pixel format that SurfaceFlinger expects hardware composer
257     // to composite efficiently for wide color gamut surfaces. Meaning under most scenarios,
258     // hardware composer will accept layers with the data space and pixel format.
259     static ui::Dataspace wideColorGamutCompositionDataspace;
260     static ui::PixelFormat wideColorGamutCompositionPixelFormat;
261 
262     // Whether to use frame rate API when deciding about the refresh rate of the display. This
263     // variable is caches in SF, so that we can check it with each layer creation, and a void the
264     // overhead that is caused by reading from sysprop.
265     static bool useFrameRateApi;
266 
267     static constexpr SkipInitializationTag SkipInitialization;
268 
269     // Whether or not SDR layers should be dimmed to the desired SDR white point instead of
270     // being treated as native display brightness
271     static bool enableSdrDimming;
272 
273     static bool enableLatchUnsignaled;
274 
275     // must be called before clients can connect
276     void init() ANDROID_API;
277 
278     // starts SurfaceFlinger main loop in the current thread
279     void run() ANDROID_API;
280 
getBE()281     SurfaceFlingerBE& getBE() { return mBE; }
getBE()282     const SurfaceFlingerBE& getBE() const { return mBE; }
283 
284     // Schedule an asynchronous or synchronous task on the main thread.
285     template <typename F, typename T = std::invoke_result_t<F>>
286     [[nodiscard]] std::future<T> schedule(F&&);
287 
288     // force full composition on all displays
289     void repaintEverything();
290 
getFactory()291     surfaceflinger::Factory& getFactory() { return mFactory; }
292 
293     // The CompositionEngine encapsulates all composition related interfaces and actions.
294     compositionengine::CompositionEngine& getCompositionEngine() const;
295 
296     // Obtains a name from the texture pool, or, if the pool is empty, posts a
297     // synchronous message to the main thread to obtain one on the fly
298     uint32_t getNewTexture();
299 
300     // utility function to delete a texture on the main thread
301     void deleteTextureAsync(uint32_t texture);
302 
303     // called on the main thread by MessageQueue when an internal message
304     // is received
305     // TODO: this should be made accessible only to MessageQueue
306     void onMessageReceived(int32_t what, int64_t vsyncId, nsecs_t expectedVSyncTime);
307 
308     renderengine::RenderEngine& getRenderEngine() const;
309 
310     bool authenticateSurfaceTextureLocked(
311         const sp<IGraphicBufferProducer>& bufferProducer) const;
312 
313     void onLayerFirstRef(Layer*);
314     void onLayerDestroyed(Layer*);
315 
316     void removeHierarchyFromOffscreenLayers(Layer* layer);
317     void removeFromOffscreenLayers(Layer* layer);
318 
319     // TODO: Remove atomic if move dtor to main thread CL lands
320     std::atomic<uint32_t> mNumClones;
321 
getTransactionCallbackInvoker()322     TransactionCallbackInvoker& getTransactionCallbackInvoker() {
323         return mTransactionCallbackInvoker;
324     }
325 
326     // Converts from a binder handle to a Layer
327     // Returns nullptr if the handle does not point to an existing layer.
328     // Otherwise, returns a weak reference so that callers off the main-thread
329     // won't accidentally hold onto the last strong reference.
330     wp<Layer> fromHandle(const sp<IBinder>& handle);
331     wp<Layer> fromHandleLocked(const sp<IBinder>& handle) const REQUIRES(mStateLock);
332 
333     // If set, disables reusing client composition buffers. This can be set by
334     // debug.sf.disable_client_composition_cache
335     bool mDisableClientCompositionCache = false;
336     void setInputWindowsFinished();
337 
338     // Disables expensive rendering for all displays
339     // This is scheduled on the main thread
340     void disableExpensiveRendering();
341 
342 protected:
343     // We're reference counted, never destroy SurfaceFlinger directly
344     virtual ~SurfaceFlinger();
345 
346     virtual uint32_t setClientStateLocked(
347             const FrameTimelineInfo& info, const ComposerState& composerState,
348             int64_t desiredPresentTime, bool isAutoTimestamp, int64_t postTime,
349             uint32_t permissions,
350             std::unordered_set<ListenerCallbacks, ListenerCallbacksHash>& listenerCallbacks)
351             REQUIRES(mStateLock);
352     virtual void commitTransactionLocked();
353 
354     // Used internally by computeLayerBounds() to gets the clip rectangle to use for the
355     // root layers on a particular display in layer-coordinate space. The
356     // layers (and effectively their children) will be clipped against this
357     // rectangle. The base behavior is to clip to the visible region of the
358     // display.
359     virtual FloatRect getLayerClipBoundsForDisplay(const DisplayDevice&) const;
360 
361 private:
362     friend class BufferLayer;
363     friend class BufferQueueLayer;
364     friend class BufferStateLayer;
365     friend class Client;
366     friend class FpsReporter;
367     friend class TunnelModeEnabledReporter;
368     friend class Layer;
369     friend class MonitoredProducer;
370     friend class RefreshRateOverlay;
371     friend class RegionSamplingThread;
372     friend class SurfaceTracing;
373 
374     // For unit tests
375     friend class TestableSurfaceFlinger;
376     friend class TransactionApplicationTest;
377     friend class TunnelModeEnabledReporterTest;
378 
379     using RefreshRate = scheduler::RefreshRateConfigs::RefreshRate;
380     using VsyncModulator = scheduler::VsyncModulator;
381     using TransactionSchedule = scheduler::TransactionSchedule;
382     using TraverseLayersFunction = std::function<void(const LayerVector::Visitor&)>;
383     using RenderAreaFuture = std::future<std::unique_ptr<RenderArea>>;
384     using DumpArgs = Vector<String16>;
385     using Dumper = std::function<void(const DumpArgs&, bool asProto, std::string&)>;
386 
387     // This value is specified in number of frames.  Log frame stats at most
388     // every half hour.
389     enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
390 
391     class State {
392     public:
State(LayerVector::StateSet set)393         explicit State(LayerVector::StateSet set) : stateSet(set), layersSortedByZ(set) {}
394         State& operator=(const State& other) {
395             // We explicitly don't copy stateSet so that, e.g., mDrawingState
396             // always uses the Drawing StateSet.
397             layersSortedByZ = other.layersSortedByZ;
398             displays = other.displays;
399             colorMatrixChanged = other.colorMatrixChanged;
400             if (colorMatrixChanged) {
401                 colorMatrix = other.colorMatrix;
402             }
403             globalShadowSettings = other.globalShadowSettings;
404 
405             return *this;
406         }
407 
408         const LayerVector::StateSet stateSet = LayerVector::StateSet::Invalid;
409         LayerVector layersSortedByZ;
410         DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
411 
412         bool colorMatrixChanged = true;
413         mat4 colorMatrix;
414 
415         renderengine::ShadowSettings globalShadowSettings;
416 
417         void traverse(const LayerVector::Visitor& visitor) const;
418         void traverseInZOrder(const LayerVector::Visitor& visitor) const;
419         void traverseInReverseZOrder(const LayerVector::Visitor& visitor) const;
420     };
421 
422     // Keeps track of pending buffers per layer handle in the transaction queue or current/drawing
423     // state before the buffers are latched. The layer owns the atomic counters and decrements the
424     // count in the main thread when dropping or latching a buffer.
425     //
426     // The binder threads increment the same counter when a new transaction containing a buffer is
427     // added to the transaction queue. The map is updated with the layer handle lifecycle updates.
428     // This is done to avoid lock contention with the main thread.
429     class BufferCountTracker {
430     public:
increment(BBinder * layerHandle)431         void increment(BBinder* layerHandle) {
432             std::lock_guard<std::mutex> lock(mLock);
433             auto it = mCounterByLayerHandle.find(layerHandle);
434             if (it != mCounterByLayerHandle.end()) {
435                 auto [name, pendingBuffers] = it->second;
436                 int32_t count = ++(*pendingBuffers);
437                 ATRACE_INT(name.c_str(), count);
438             } else {
439                 ALOGW("Handle not found! %p", layerHandle);
440             }
441         }
442 
add(BBinder * layerHandle,const std::string & name,std::atomic<int32_t> * counter)443         void add(BBinder* layerHandle, const std::string& name, std::atomic<int32_t>* counter) {
444             std::lock_guard<std::mutex> lock(mLock);
445             mCounterByLayerHandle[layerHandle] = std::make_pair(name, counter);
446         }
447 
remove(BBinder * layerHandle)448         void remove(BBinder* layerHandle) {
449             std::lock_guard<std::mutex> lock(mLock);
450             mCounterByLayerHandle.erase(layerHandle);
451         }
452 
453     private:
454         std::mutex mLock;
455         std::unordered_map<BBinder*, std::pair<std::string, std::atomic<int32_t>*>>
456                 mCounterByLayerHandle GUARDED_BY(mLock);
457     };
458 
459     struct ActiveModeInfo {
460         DisplayModeId modeId;
461         Scheduler::ModeEvent event = Scheduler::ModeEvent::None;
462 
463         bool operator!=(const ActiveModeInfo& other) const {
464             return modeId != other.modeId || event != other.event;
465         }
466     };
467 
468     enum class BootStage {
469         BOOTLOADER,
470         BOOTANIMATION,
471         FINISHED,
472     };
473 
474     struct HotplugEvent {
475         hal::HWDisplayId hwcDisplayId;
476         hal::Connection connection = hal::Connection::INVALID;
477     };
478 
479     class CountDownLatch {
480     public:
481         enum {
482             eSyncTransaction = 1 << 0,
483             eSyncInputWindows = 1 << 1,
484         };
CountDownLatch(uint32_t flags)485         explicit CountDownLatch(uint32_t flags) : mFlags(flags) {}
486 
487         // True if there is no waiting condition after count down.
countDown(uint32_t flag)488         bool countDown(uint32_t flag) {
489             std::unique_lock<std::mutex> lock(mMutex);
490             if (mFlags == 0) {
491                 return true;
492             }
493             mFlags &= ~flag;
494             if (mFlags == 0) {
495                 mCountDownComplete.notify_all();
496                 return true;
497             }
498             return false;
499         }
500 
501         // Return true if triggered.
wait_until(const std::chrono::seconds & timeout)502         bool wait_until(const std::chrono::seconds& timeout) const {
503             std::unique_lock<std::mutex> lock(mMutex);
504             const auto untilTime = std::chrono::system_clock::now() + timeout;
505             while (mFlags != 0) {
506                 // Conditional variables can be woken up sporadically, so we check count
507                 // to verify the wakeup was triggered by |countDown|.
508                 if (std::cv_status::timeout == mCountDownComplete.wait_until(lock, untilTime)) {
509                     return false;
510                 }
511             }
512             return true;
513         }
514 
515     private:
516         uint32_t mFlags;
517         mutable std::condition_variable mCountDownComplete;
518         mutable std::mutex mMutex;
519     };
520 
521     struct TransactionState {
TransactionStateTransactionState522         TransactionState(const FrameTimelineInfo& frameTimelineInfo,
523                          const Vector<ComposerState>& composerStates,
524                          const Vector<DisplayState>& displayStates, uint32_t transactionFlags,
525                          const sp<IBinder>& applyToken,
526                          const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime,
527                          bool isAutoTimestamp, const client_cache_t& uncacheBuffer,
528                          int64_t postTime, uint32_t permissions, bool hasListenerCallbacks,
529                          std::vector<ListenerCallbacks> listenerCallbacks, int originPid,
530                          int originUid, uint64_t transactionId)
531               : frameTimelineInfo(frameTimelineInfo),
532                 states(composerStates),
533                 displays(displayStates),
534                 flags(transactionFlags),
535                 applyToken(applyToken),
536                 inputWindowCommands(inputWindowCommands),
537                 desiredPresentTime(desiredPresentTime),
538                 isAutoTimestamp(isAutoTimestamp),
539                 buffer(uncacheBuffer),
540                 postTime(postTime),
541                 permissions(permissions),
542                 hasListenerCallbacks(hasListenerCallbacks),
543                 listenerCallbacks(listenerCallbacks),
544                 originPid(originPid),
545                 originUid(originUid),
546                 id(transactionId) {}
547 
548         void traverseStatesWithBuffers(std::function<void(const layer_state_t&)> visitor);
549 
550         FrameTimelineInfo frameTimelineInfo;
551         Vector<ComposerState> states;
552         Vector<DisplayState> displays;
553         uint32_t flags;
554         sp<IBinder> applyToken;
555         InputWindowCommands inputWindowCommands;
556         const int64_t desiredPresentTime;
557         const bool isAutoTimestamp;
558         client_cache_t buffer;
559         const int64_t postTime;
560         uint32_t permissions;
561         bool hasListenerCallbacks;
562         std::vector<ListenerCallbacks> listenerCallbacks;
563         int originPid;
564         int originUid;
565         uint64_t id;
566         std::shared_ptr<CountDownLatch> transactionCommittedSignal;
567     };
568 
569     template <typename F, std::enable_if_t<!std::is_member_function_pointer_v<F>>* = nullptr>
dumper(F && dump)570     static Dumper dumper(F&& dump) {
571         using namespace std::placeholders;
572         return std::bind(std::forward<F>(dump), _3);
573     }
574 
575     template <typename F, std::enable_if_t<std::is_member_function_pointer_v<F>>* = nullptr>
dumper(F dump)576     Dumper dumper(F dump) {
577         using namespace std::placeholders;
578         return std::bind(dump, this, _3);
579     }
580 
581     template <typename F>
argsDumper(F dump)582     Dumper argsDumper(F dump) {
583         using namespace std::placeholders;
584         return std::bind(dump, this, _1, _3);
585     }
586 
587     template <typename F>
protoDumper(F dump)588     Dumper protoDumper(F dump) {
589         using namespace std::placeholders;
590         return std::bind(dump, this, _1, _2, _3);
591     }
592 
593     template <typename... Args,
594               typename Handler = VsyncModulator::VsyncConfigOpt (VsyncModulator::*)(Args...)>
modulateVsync(Handler handler,Args...args)595     void modulateVsync(Handler handler, Args... args) {
596         if (const auto config = (*mVsyncModulator.*handler)(args...)) {
597             const auto vsyncPeriod = mRefreshRateConfigs->getCurrentRefreshRate().getVsyncPeriod();
598             setVsyncConfig(*config, vsyncPeriod);
599         }
600     }
601 
602     static const int MAX_TRACING_MEMORY = 100 * 1024 * 1024; // 100MB
603     // Maximum allowed number of display frames that can be set through backdoor
604     static const int MAX_ALLOWED_DISPLAY_FRAMES = 2048;
605 
606     // Implements IBinder.
607     status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) override;
dump(int fd,const Vector<String16> & args)608     status_t dump(int fd, const Vector<String16>& args) override { return priorityDump(fd, args); }
609     bool callingThreadHasUnscopedSurfaceFlingerAccess(bool usePermissionCache = true)
610             EXCLUDES(mStateLock);
611 
612     // Implements ISurfaceComposer
613     sp<ISurfaceComposerClient> createConnection() override;
614     sp<IBinder> createDisplay(const String8& displayName, bool secure) override;
615     void destroyDisplay(const sp<IBinder>& displayToken) override;
616     std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const override;
617     sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId) const override;
618     status_t setTransactionState(const FrameTimelineInfo& frameTimelineInfo,
619                                  const Vector<ComposerState>& state,
620                                  const Vector<DisplayState>& displays, uint32_t flags,
621                                  const sp<IBinder>& applyToken,
622                                  const InputWindowCommands& inputWindowCommands,
623                                  int64_t desiredPresentTime, bool isAutoTimestamp,
624                                  const client_cache_t& uncacheBuffer, bool hasListenerCallbacks,
625                                  const std::vector<ListenerCallbacks>& listenerCallbacks,
626                                  uint64_t transactionId) override;
627     void bootFinished() override;
628     bool authenticateSurfaceTexture(
629             const sp<IGraphicBufferProducer>& bufferProducer) const override;
630     status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported) const override;
631     sp<IDisplayEventConnection> createDisplayEventConnection(
632             ISurfaceComposer::VsyncSource vsyncSource = eVsyncSourceApp,
633             ISurfaceComposer::EventRegistrationFlags eventRegistration = {}) override;
634     status_t captureDisplay(const DisplayCaptureArgs& args,
635                             const sp<IScreenCaptureListener>& captureListener) override;
636     status_t captureDisplay(uint64_t displayOrLayerStack,
637                             const sp<IScreenCaptureListener>& captureListener) override;
638     status_t captureLayers(const LayerCaptureArgs& args,
639                            const sp<IScreenCaptureListener>& captureListener) override;
640 
641     status_t getDisplayStats(const sp<IBinder>& displayToken, DisplayStatInfo* stats) override;
642     status_t getDisplayState(const sp<IBinder>& displayToken, ui::DisplayState*)
643             EXCLUDES(mStateLock) override;
644     status_t getStaticDisplayInfo(const sp<IBinder>& displayToken, ui::StaticDisplayInfo*)
645             EXCLUDES(mStateLock) override;
646     status_t getDynamicDisplayInfo(const sp<IBinder>& displayToken, ui::DynamicDisplayInfo*)
647             EXCLUDES(mStateLock) override;
648     status_t getDisplayNativePrimaries(const sp<IBinder>& displayToken,
649                                        ui::DisplayPrimaries&) override;
650     status_t setActiveColorMode(const sp<IBinder>& displayToken, ui::ColorMode colorMode) override;
651     void setAutoLowLatencyMode(const sp<IBinder>& displayToken, bool on) override;
652     void setGameContentType(const sp<IBinder>& displayToken, bool on) override;
653     void setPowerMode(const sp<IBinder>& displayToken, int mode) override;
654     status_t clearAnimationFrameStats() override;
655     status_t getAnimationFrameStats(FrameStats* outStats) const override;
656     status_t overrideHdrTypes(const sp<IBinder>& displayToken,
657                               const std::vector<ui::Hdr>& hdrTypes) override;
658     status_t onPullAtom(const int32_t atomId, std::string* pulledData, bool* success) override;
659     status_t enableVSyncInjections(bool enable) override;
660     status_t injectVSync(nsecs_t when) override;
661     status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* outLayers) override;
662     status_t getColorManagement(bool* outGetColorManagement) const override;
663     status_t getCompositionPreference(ui::Dataspace* outDataspace, ui::PixelFormat* outPixelFormat,
664                                       ui::Dataspace* outWideColorGamutDataspace,
665                                       ui::PixelFormat* outWideColorGamutPixelFormat) const override;
666     status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& displayToken,
667                                                    ui::PixelFormat* outFormat,
668                                                    ui::Dataspace* outDataspace,
669                                                    uint8_t* outComponentMask) const override;
670     status_t setDisplayContentSamplingEnabled(const sp<IBinder>& displayToken, bool enable,
671                                               uint8_t componentMask, uint64_t maxFrames) override;
672     status_t getDisplayedContentSample(const sp<IBinder>& displayToken, uint64_t maxFrames,
673                                        uint64_t timestamp,
674                                        DisplayedFrameStats* outStats) const override;
675     status_t getProtectedContentSupport(bool* outSupported) const override;
676     status_t isWideColorDisplay(const sp<IBinder>& displayToken,
677                                 bool* outIsWideColorDisplay) const override;
678     status_t addRegionSamplingListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
679                                        const sp<IRegionSamplingListener>& listener) override;
680     status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener) override;
681     status_t addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener) override;
682     status_t removeFpsListener(const sp<gui::IFpsListener>& listener) override;
683     status_t addTunnelModeEnabledListener(
684             const sp<gui::ITunnelModeEnabledListener>& listener) override;
685     status_t removeTunnelModeEnabledListener(
686             const sp<gui::ITunnelModeEnabledListener>& listener) override;
687     status_t setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
688                                         ui::DisplayModeId displayModeId, bool allowGroupSwitching,
689                                         float primaryRefreshRateMin, float primaryRefreshRateMax,
690                                         float appRequestRefreshRateMin,
691                                         float appRequestRefreshRateMax) override;
692     status_t getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
693                                         ui::DisplayModeId* outDefaultMode,
694                                         bool* outAllowGroupSwitching,
695                                         float* outPrimaryRefreshRateMin,
696                                         float* outPrimaryRefreshRateMax,
697                                         float* outAppRequestRefreshRateMin,
698                                         float* outAppRequestRefreshRateMax) override;
699     status_t getDisplayBrightnessSupport(const sp<IBinder>& displayToken,
700                                          bool* outSupport) const override;
701     status_t setDisplayBrightness(const sp<IBinder>& displayToken,
702                                   const gui::DisplayBrightness& brightness) override;
703     status_t addHdrLayerInfoListener(const sp<IBinder>& displayToken,
704                                      const sp<gui::IHdrLayerInfoListener>& listener) override;
705     status_t removeHdrLayerInfoListener(const sp<IBinder>& displayToken,
706                                         const sp<gui::IHdrLayerInfoListener>& listener) override;
707     status_t notifyPowerBoost(int32_t boostId) override;
708     status_t setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor,
709                                      float lightPosY, float lightPosZ, float lightRadius) override;
710     status_t setFrameRate(const sp<IGraphicBufferProducer>& surface, float frameRate,
711                           int8_t compatibility, int8_t changeFrameRateStrategy) override;
712     status_t acquireFrameRateFlexibilityToken(sp<IBinder>* outToken) override;
713 
714     status_t setFrameTimelineInfo(const sp<IGraphicBufferProducer>& surface,
715                                   const FrameTimelineInfo& frameTimelineInfo) override;
716 
717     status_t addTransactionTraceListener(
718             const sp<gui::ITransactionTraceListener>& listener) override;
719 
720     int getGPUContextPriority() override;
721 
722     status_t getMaxAcquiredBufferCount(int* buffers) const override;
723 
724     // Implements IBinder::DeathRecipient.
725     void binderDied(const wp<IBinder>& who) override;
726 
727     // Implements RefBase.
728     void onFirstRef() override;
729 
730     // HWC2::ComposerCallback overrides:
731     void onComposerHalVsync(hal::HWDisplayId, int64_t timestamp,
732                             std::optional<hal::VsyncPeriodNanos>) override;
733     void onComposerHalHotplug(hal::HWDisplayId, hal::Connection) override;
734     void onComposerHalRefresh(hal::HWDisplayId) override;
735     void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
736                                                const hal::VsyncPeriodChangeTimeline&) override;
737     void onComposerHalSeamlessPossible(hal::HWDisplayId) override;
738 
739     /*
740      * ISchedulerCallback
741      */
742 
743     // Toggles hardware VSYNC by calling into HWC.
744     void setVsyncEnabled(bool) override;
745     // Initiates a refresh rate change to be applied on invalidate.
746     void changeRefreshRate(const Scheduler::RefreshRate&, Scheduler::ModeEvent) override;
747     // Forces full composition on all displays without resetting the scheduler idle timer.
748     void repaintEverythingForHWC() override;
749     // Called when kernel idle timer has expired. Used to update the refresh rate overlay.
750     void kernelTimerChanged(bool expired) override;
751     // Called when the frame rate override list changed to trigger an event.
752     void triggerOnFrameRateOverridesChanged() override;
753     // Toggles the kernel idle timer on or off depending the policy decisions around refresh rates.
754     void toggleKernelIdleTimer();
755     // Keeps track of whether the kernel idle timer is currently enabled, so we don't have to
756     // make calls to sys prop each time.
757     bool mKernelIdleTimerEnabled = false;
758     // Keeps track of whether the kernel timer is supported on the SF side.
759     bool mSupportKernelIdleTimer = false;
760     // Show spinner with refresh rate overlay
761     bool mRefreshRateOverlaySpinner = false;
762 
763     /*
764      * Message handling
765      */
766     // Can only be called from the main thread or with mStateLock held
767     void signalTransaction();
768     // Can only be called from the main thread or with mStateLock held
769     void signalLayerUpdate();
770     void signalRefresh();
771 
772     // Called on the main thread in response to initializeDisplays()
773     void onInitializeDisplays() REQUIRES(mStateLock);
774     // Sets the desired active mode bit. It obtains the lock, and sets mDesiredActiveMode.
775     void setDesiredActiveMode(const ActiveModeInfo& info) REQUIRES(mStateLock);
776     status_t setActiveMode(const sp<IBinder>& displayToken, int id);
777     // Once HWC has returned the present fence, this sets the active mode and a new refresh
778     // rate in SF.
779     void setActiveModeInternal() REQUIRES(mStateLock);
780     // Calls to setActiveMode on the main thread if there is a pending mode change
781     // that needs to be applied.
782     void performSetActiveMode() REQUIRES(mStateLock);
783     void clearDesiredActiveModeState() REQUIRES(mStateLock) EXCLUDES(mActiveModeLock);
784     // Called when active mode is no longer is progress
785     void desiredActiveModeChangeDone() REQUIRES(mStateLock);
786     // Called on the main thread in response to setPowerMode()
787     void setPowerModeInternal(const sp<DisplayDevice>& display, hal::PowerMode mode)
788             REQUIRES(mStateLock);
789 
790     // Sets the desired display mode specs.
791     status_t setDesiredDisplayModeSpecsInternal(
792             const sp<DisplayDevice>& display,
793             const std::optional<scheduler::RefreshRateConfigs::Policy>& policy, bool overridePolicy)
794             EXCLUDES(mStateLock);
795 
796     // Handle the INVALIDATE message queue event, latching new buffers and applying
797     // incoming transactions
798     void onMessageInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTime);
799 
800     // Returns whether the transaction actually modified any state
801     bool handleMessageTransaction();
802 
803     // Handle the REFRESH message queue event, sending the current frame down to RenderEngine and
804     // the Composer HAL for presentation
805     void onMessageRefresh();
806 
807     // Returns whether a new buffer has been latched (see handlePageFlip())
808     bool handleMessageInvalidate();
809 
810     void handleTransaction(uint32_t transactionFlags);
811     void handleTransactionLocked(uint32_t transactionFlags) REQUIRES(mStateLock);
812 
813     void updateInputFlinger();
814     void updateInputWindowInfo();
815     void commitInputWindowCommands() REQUIRES(mStateLock);
816     void updateCursorAsync();
817 
818     void initScheduler(const DisplayDeviceState&) REQUIRES(mStateLock);
819     void updatePhaseConfiguration(const Fps&) REQUIRES(mStateLock);
820     void setVsyncConfig(const VsyncModulator::VsyncConfig&, nsecs_t vsyncPeriod);
821 
822     /* handlePageFlip - latch a new buffer if available and compute the dirty
823      * region. Returns whether a new buffer has been latched, i.e., whether it
824      * is necessary to perform a refresh during this vsync.
825      */
826     bool handlePageFlip();
827 
828     /*
829      * Transactions
830      */
831     void applyTransactionState(const FrameTimelineInfo& info, const Vector<ComposerState>& state,
832                                const Vector<DisplayState>& displays, uint32_t flags,
833                                const InputWindowCommands& inputWindowCommands,
834                                const int64_t desiredPresentTime, bool isAutoTimestamp,
835                                const client_cache_t& uncacheBuffer, const int64_t postTime,
836                                uint32_t permissions, bool hasListenerCallbacks,
837                                const std::vector<ListenerCallbacks>& listenerCallbacks,
838                                int originPid, int originUid, uint64_t transactionId)
839             REQUIRES(mStateLock);
840     // flush pending transaction that was presented after desiredPresentTime.
841     void flushTransactionQueues();
842     // Returns true if there is at least one transaction that needs to be flushed
843     bool transactionFlushNeeded();
844     uint32_t getTransactionFlags(uint32_t flags);
845     uint32_t peekTransactionFlags();
846     // Can only be called from the main thread or with mStateLock held
847     uint32_t setTransactionFlags(uint32_t flags);
848     // Indicate SF should call doTraversal on layers, but don't trigger a wakeup! We use this cases
849     // where there are still pending transactions but we know they won't be ready until a frame
850     // arrives from a different layer. So we need to ensure we performTransaction from invalidate
851     // but there is no need to try and wake up immediately to do it. Rather we rely on
852     // onFrameAvailable or another layer update to wake us up.
853     void setTraversalNeeded();
854     uint32_t setTransactionFlags(uint32_t flags, TransactionSchedule, const sp<IBinder>& = {});
855     void commitTransaction() REQUIRES(mStateLock);
856     void commitOffscreenLayers();
857     bool transactionIsReadyToBeApplied(
858             const FrameTimelineInfo& info, bool isAutoTimestamp, int64_t desiredPresentTime,
859             uid_t originUid, const Vector<ComposerState>& states,
860             const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>&
861                     bufferLayersReadyToPresent) const REQUIRES(mStateLock);
862     uint32_t setDisplayStateLocked(const DisplayState& s) REQUIRES(mStateLock);
863     uint32_t addInputWindowCommands(const InputWindowCommands& inputWindowCommands)
864             REQUIRES(mStateLock);
865     bool frameIsEarly(nsecs_t expectedPresentTime, int64_t vsyncId) const;
866     /*
867      * Layer management
868      */
869     status_t createLayer(const String8& name, const sp<Client>& client, uint32_t w, uint32_t h,
870                          PixelFormat format, uint32_t flags, LayerMetadata metadata,
871                          sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp,
872                          const sp<IBinder>& parentHandle, int32_t* outLayerId,
873                          const sp<Layer>& parentLayer = nullptr,
874                          uint32_t* outTransformHint = nullptr);
875 
876     status_t createBufferQueueLayer(const sp<Client>& client, std::string name, uint32_t w,
877                                     uint32_t h, uint32_t flags, LayerMetadata metadata,
878                                     PixelFormat& format, sp<IBinder>* outHandle,
879                                     sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
880 
881     status_t createBufferStateLayer(const sp<Client>& client, std::string name, uint32_t w,
882                                     uint32_t h, uint32_t flags, LayerMetadata metadata,
883                                     sp<IBinder>* outHandle, sp<Layer>* outLayer);
884 
885     status_t createEffectLayer(const sp<Client>& client, std::string name, uint32_t w, uint32_t h,
886                                uint32_t flags, LayerMetadata metadata, sp<IBinder>* outHandle,
887                                sp<Layer>* outLayer);
888 
889     status_t createContainerLayer(const sp<Client>& client, std::string name, uint32_t w,
890                                   uint32_t h, uint32_t flags, LayerMetadata metadata,
891                                   sp<IBinder>* outHandle, sp<Layer>* outLayer);
892 
893     status_t mirrorLayer(const sp<Client>& client, const sp<IBinder>& mirrorFromHandle,
894                          sp<IBinder>* outHandle, int32_t* outLayerId);
895 
896     std::string getUniqueLayerName(const char* name);
897 
898     // called when all clients have released all their references to
899     // this layer meaning it is entirely safe to destroy all
900     // resources associated to this layer.
901     void onHandleDestroyed(sp<Layer>& layer);
902     void markLayerPendingRemovalLocked(const sp<Layer>& layer);
903 
904     // add a layer to SurfaceFlinger
905     status_t addClientLayer(const sp<Client>& client, const sp<IBinder>& handle,
906                             const sp<IGraphicBufferProducer>& gbc, const sp<Layer>& lbc,
907                             const sp<IBinder>& parentHandle, const sp<Layer>& parentLayer,
908                             bool addToRoot, uint32_t* outTransformHint);
909 
910     // Traverse through all the layers and compute and cache its bounds.
911     void computeLayerBounds();
912 
913     // Boot animation, on/off animations and screen capture
914     void startBootAnim();
915 
916     status_t captureScreenCommon(RenderAreaFuture, TraverseLayersFunction, ui::Size bufferSize,
917                                  ui::PixelFormat, bool allowProtected, bool grayscale,
918                                  const sp<IScreenCaptureListener>&);
919     status_t captureScreenCommon(RenderAreaFuture, TraverseLayersFunction,
920                                  const std::shared_ptr<renderengine::ExternalTexture>&,
921                                  bool regionSampling, bool grayscale,
922                                  const sp<IScreenCaptureListener>&);
923     status_t renderScreenImplLocked(const RenderArea&, TraverseLayersFunction,
924                                     const std::shared_ptr<renderengine::ExternalTexture>&,
925                                     bool canCaptureBlackoutContent, bool regionSampling,
926                                     bool grayscale, ScreenCaptureResults&);
927 
928     // If the uid provided is not UNSET_UID, the traverse will skip any layers that don't have a
929     // matching ownerUid
930     void traverseLayersInLayerStack(ui::LayerStack, const int32_t uid, const LayerVector::Visitor&);
931 
932     void readPersistentProperties();
933 
934     size_t getMaxTextureSize() const;
935     size_t getMaxViewportDims() const;
936 
937     int getMaxAcquiredBufferCountForCurrentRefreshRate(uid_t uid) const;
938 
939     /*
940      * Display and layer stack management
941      */
942     // called when starting, or restarting after system_server death
943     void initializeDisplays();
944 
getDisplayDeviceLocked(const wp<IBinder> & displayToken)945     sp<const DisplayDevice> getDisplayDeviceLocked(const wp<IBinder>& displayToken) const
946             REQUIRES(mStateLock) {
947         return const_cast<SurfaceFlinger*>(this)->getDisplayDeviceLocked(displayToken);
948     }
949 
getDisplayDeviceLocked(const wp<IBinder> & displayToken)950     sp<DisplayDevice> getDisplayDeviceLocked(const wp<IBinder>& displayToken) REQUIRES(mStateLock) {
951         const auto it = mDisplays.find(displayToken);
952         return it == mDisplays.end() ? nullptr : it->second;
953     }
954 
getDisplayDeviceLocked(PhysicalDisplayId id)955     sp<const DisplayDevice> getDisplayDeviceLocked(PhysicalDisplayId id) const
956             REQUIRES(mStateLock) {
957         if (const auto token = getPhysicalDisplayTokenLocked(id)) {
958             return getDisplayDeviceLocked(token);
959         }
960         return nullptr;
961     }
962 
getDefaultDisplayDeviceLocked()963     sp<const DisplayDevice> getDefaultDisplayDeviceLocked() const REQUIRES(mStateLock) {
964         return const_cast<SurfaceFlinger*>(this)->getDefaultDisplayDeviceLocked();
965     }
966 
getDefaultDisplayDeviceLocked()967     sp<DisplayDevice> getDefaultDisplayDeviceLocked() REQUIRES(mStateLock) {
968         if (const auto token = getInternalDisplayTokenLocked()) {
969             return getDisplayDeviceLocked(token);
970         }
971         return nullptr;
972     }
973 
getDefaultDisplayDevice()974     sp<const DisplayDevice> getDefaultDisplayDevice() EXCLUDES(mStateLock) {
975         Mutex::Autolock lock(mStateLock);
976         return getDefaultDisplayDeviceLocked();
977     }
978 
979     // Returns the first display that matches a `bool(const DisplayDevice&)` predicate.
980     template <typename Predicate>
findDisplay(Predicate p)981     sp<DisplayDevice> findDisplay(Predicate p) const REQUIRES(mStateLock) {
982         const auto it = std::find_if(mDisplays.begin(), mDisplays.end(),
983                                      [&](const auto& pair) { return p(*pair.second); });
984 
985         return it == mDisplays.end() ? nullptr : it->second;
986     }
987 
getDisplayDeviceLocked(DisplayId id)988     sp<const DisplayDevice> getDisplayDeviceLocked(DisplayId id) const REQUIRES(mStateLock) {
989         // TODO(b/182939859): Replace tokens with IDs for display lookup.
990         return findDisplay([id](const auto& display) { return display.getId() == id; });
991     }
992 
993     // mark a region of a layer stack dirty. this updates the dirty
994     // region of all screens presenting this layer stack.
995     void invalidateLayerStack(const sp<const Layer>& layer, const Region& dirty);
996 
997     /*
998      * H/W composer
999      */
1000     // The following thread safety rules apply when accessing HWComposer:
1001     // 1. When reading display state from HWComposer on the main thread, it's not necessary to
1002     //    acquire mStateLock.
1003     // 2. When accessing HWComposer on a thread other than the main thread, we always
1004     //    need to acquire mStateLock. This is because the main thread could be
1005     //    in the process of writing display state, e.g. creating or destroying a display.
1006     HWComposer& getHwComposer() const;
1007 
1008     /*
1009      * Compositing
1010      */
1011     void invalidateHwcGeometry();
1012 
1013     void postComposition();
1014     void getCompositorTiming(CompositorTiming* compositorTiming);
1015     void updateCompositorTiming(const DisplayStatInfo& stats, nsecs_t compositeTime,
1016                                 std::shared_ptr<FenceTime>& presentFenceTime);
1017     void setCompositorTimingSnapped(const DisplayStatInfo& stats,
1018                                     nsecs_t compositeToPresentLatency);
1019 
1020     void postFrame();
1021 
1022     /*
1023      * Display management
1024      */
1025     void loadDisplayModes(PhysicalDisplayId displayId, DisplayModes& outModes,
1026                           DisplayModePtr& outActiveMode) const REQUIRES(mStateLock);
1027     sp<DisplayDevice> setupNewDisplayDeviceInternal(
1028             const wp<IBinder>& displayToken,
1029             std::shared_ptr<compositionengine::Display> compositionDisplay,
1030             const DisplayDeviceState& state,
1031             const sp<compositionengine::DisplaySurface>& displaySurface,
1032             const sp<IGraphicBufferProducer>& producer) REQUIRES(mStateLock);
1033     void processDisplayChangesLocked() REQUIRES(mStateLock);
1034     void processDisplayAdded(const wp<IBinder>& displayToken, const DisplayDeviceState&)
1035             REQUIRES(mStateLock);
1036     void processDisplayRemoved(const wp<IBinder>& displayToken) REQUIRES(mStateLock);
1037     void processDisplayChanged(const wp<IBinder>& displayToken,
1038                                const DisplayDeviceState& currentState,
1039                                const DisplayDeviceState& drawingState) REQUIRES(mStateLock);
1040     void processDisplayHotplugEventsLocked() REQUIRES(mStateLock);
1041 
1042     void dispatchDisplayHotplugEvent(PhysicalDisplayId displayId, bool connected);
1043 
1044     /*
1045      * VSYNC
1046      */
1047     nsecs_t getVsyncPeriodFromHWC() const REQUIRES(mStateLock);
1048 
1049     // Sets the refresh rate by switching active configs, if they are available for
1050     // the desired refresh rate.
1051     void changeRefreshRateLocked(const RefreshRate&, Scheduler::ModeEvent) REQUIRES(mStateLock);
1052 
1053     bool isDisplayModeAllowed(DisplayModeId) const REQUIRES(mStateLock);
1054 
1055     struct FenceWithFenceTime {
1056         sp<Fence> fence = Fence::NO_FENCE;
1057         std::shared_ptr<FenceTime> fenceTime = FenceTime::NO_FENCE;
1058     };
1059 
1060     // Gets the fence for the previous frame.
1061     // Must be called on the main thread.
1062     FenceWithFenceTime previousFrameFence();
1063 
1064     // Whether the previous frame has not yet been presented to the display.
1065     // If graceTimeMs is positive, this method waits for at most the provided
1066     // grace period before reporting if the frame missed.
1067     // Must be called on the main thread.
1068     bool previousFramePending(int graceTimeMs = 0);
1069 
1070     // Returns the previous time that the frame was presented. If the frame has
1071     // not been presented yet, then returns Fence::SIGNAL_TIME_PENDING. If there
1072     // is no pending frame, then returns Fence::SIGNAL_TIME_INVALID.
1073     // Must be called on the main thread.
1074     nsecs_t previousFramePresentTime();
1075 
1076     // Calculates the expected present time for this frame. For negative offsets, performs a
1077     // correction using the predicted vsync for the next frame instead.
1078 
1079     nsecs_t calculateExpectedPresentTime(DisplayStatInfo) const;
1080 
1081     /*
1082      * Display identification
1083      */
getPhysicalDisplayTokenLocked(PhysicalDisplayId displayId)1084     sp<IBinder> getPhysicalDisplayTokenLocked(PhysicalDisplayId displayId) const
1085             REQUIRES(mStateLock) {
1086         const auto it = mPhysicalDisplayTokens.find(displayId);
1087         return it != mPhysicalDisplayTokens.end() ? it->second : nullptr;
1088     }
1089 
getPhysicalDisplayIdLocked(const sp<IBinder> & displayToken)1090     std::optional<PhysicalDisplayId> getPhysicalDisplayIdLocked(
1091             const sp<IBinder>& displayToken) const REQUIRES(mStateLock) {
1092         for (const auto& [id, token] : mPhysicalDisplayTokens) {
1093             if (token == displayToken) {
1094                 return id;
1095             }
1096         }
1097         return {};
1098     }
1099 
1100     // TODO(b/74619554): Remove special cases for primary display.
getInternalDisplayTokenLocked()1101     sp<IBinder> getInternalDisplayTokenLocked() const REQUIRES(mStateLock) {
1102         const auto displayId = getInternalDisplayIdLocked();
1103         return displayId ? getPhysicalDisplayTokenLocked(*displayId) : nullptr;
1104     }
1105 
getInternalDisplayIdLocked()1106     std::optional<PhysicalDisplayId> getInternalDisplayIdLocked() const REQUIRES(mStateLock) {
1107         const auto hwcDisplayId = getHwComposer().getInternalHwcDisplayId();
1108         return hwcDisplayId ? getHwComposer().toPhysicalDisplayId(*hwcDisplayId) : std::nullopt;
1109     }
1110 
1111     // Toggles use of HAL/GPU virtual displays.
1112     void enableHalVirtualDisplays(bool);
1113 
1114     // Virtual display lifecycle for ID generation and HAL allocation.
1115     VirtualDisplayId acquireVirtualDisplay(ui::Size, ui::PixelFormat, ui::LayerStack)
1116             REQUIRES(mStateLock);
1117     void releaseVirtualDisplay(VirtualDisplayId);
1118 
1119     /*
1120      * Debugging & dumpsys
1121      */
1122     void dumpAllLocked(const DumpArgs& args, std::string& result) const REQUIRES(mStateLock);
1123 
1124     void appendSfConfigString(std::string& result) const;
1125     void listLayersLocked(std::string& result) const;
1126     void dumpStatsLocked(const DumpArgs& args, std::string& result) const REQUIRES(mStateLock);
1127     void clearStatsLocked(const DumpArgs& args, std::string& result);
1128     void dumpTimeStats(const DumpArgs& args, bool asProto, std::string& result) const;
1129     void dumpFrameTimeline(const DumpArgs& args, std::string& result) const;
1130     void logFrameStats();
1131 
1132     void dumpVSync(std::string& result) const REQUIRES(mStateLock);
1133     void dumpStaticScreenStats(std::string& result) const;
1134     // Not const because each Layer needs to query Fences and cache timestamps.
1135     void dumpFrameEventsLocked(std::string& result);
1136 
1137     void recordBufferingStats(const std::string& layerName,
1138                               std::vector<OccupancyTracker::Segment>&& history);
1139     void dumpBufferingStats(std::string& result) const;
1140     void dumpDisplayIdentificationData(std::string& result) const REQUIRES(mStateLock);
1141     void dumpRawDisplayIdentificationData(const DumpArgs&, std::string& result) const;
1142     void dumpWideColorInfo(std::string& result) const REQUIRES(mStateLock);
1143     LayersProto dumpDrawingStateProto(uint32_t traceFlags) const;
1144     void dumpOffscreenLayersProto(LayersProto& layersProto,
1145                                   uint32_t traceFlags = SurfaceTracing::TRACE_ALL) const;
1146     // Dumps state from HW Composer
1147     void dumpHwc(std::string& result) const;
1148     LayersProto dumpProtoFromMainThread(uint32_t traceFlags = SurfaceTracing::TRACE_ALL)
1149             EXCLUDES(mStateLock);
1150     void dumpOffscreenLayers(std::string& result) EXCLUDES(mStateLock);
1151     void dumpPlannerInfo(const DumpArgs& args, std::string& result) const REQUIRES(mStateLock);
1152 
1153     status_t doDump(int fd, const DumpArgs& args, bool asProto);
1154 
1155     status_t dumpCritical(int fd, const DumpArgs&, bool asProto);
1156 
dumpAll(int fd,const DumpArgs & args,bool asProto)1157     status_t dumpAll(int fd, const DumpArgs& args, bool asProto) override {
1158         return doDump(fd, args, asProto);
1159     }
1160 
1161     void onFrameRateFlexibilityTokenReleased();
1162 
1163     static mat4 calculateColorMatrix(float saturation);
1164 
1165     void updateColorMatrixLocked();
1166 
1167     // Verify that transaction is being called by an approved process:
1168     // either AID_GRAPHICS or AID_SYSTEM.
1169     status_t CheckTransactCodeCredentials(uint32_t code);
1170 
1171     // Add transaction to the Transaction Queue
1172     void queueTransaction(TransactionState& state) EXCLUDES(mQueueLock);
1173     void waitForSynchronousTransaction(const CountDownLatch& transactionCommittedSignal);
1174     void signalSynchronousTransactions(const uint32_t flag);
1175 
1176     /*
1177      * Generic Layer Metadata
1178      */
1179     const std::unordered_map<std::string, uint32_t>& getGenericLayerMetadataKeyMap() const;
1180 
1181     /*
1182      * Misc
1183      */
1184 
getDesiredActiveMode()1185     std::optional<ActiveModeInfo> getDesiredActiveMode() EXCLUDES(mActiveModeLock) {
1186         std::lock_guard<std::mutex> lock(mActiveModeLock);
1187         if (mDesiredActiveModeChanged) return mDesiredActiveMode;
1188         return std::nullopt;
1189     }
1190 
1191     std::vector<ui::ColorMode> getDisplayColorModes(PhysicalDisplayId displayId)
1192             REQUIRES(mStateLock);
1193 
1194     static int calculateMaxAcquiredBufferCount(Fps refreshRate,
1195                                                std::chrono::nanoseconds presentLatency);
1196     int getMaxAcquiredBufferCountForRefreshRate(Fps refreshRate) const;
1197 
1198     sp<StartPropertySetThread> mStartPropertySetThread;
1199     surfaceflinger::Factory& mFactory;
1200 
1201     std::future<void> mRenderEnginePrimeCacheFuture;
1202 
1203     // access must be protected by mStateLock
1204     mutable Mutex mStateLock;
1205     State mCurrentState{LayerVector::StateSet::Current};
1206     std::atomic<int32_t> mTransactionFlags = 0;
1207     std::vector<std::shared_ptr<CountDownLatch>> mTransactionCommittedSignals;
1208     bool mAnimTransactionPending = false;
1209     SortedVector<sp<Layer>> mLayersPendingRemoval;
1210     bool mForceTraversal = false;
1211 
1212     // global color transform states
1213     Daltonizer mDaltonizer;
1214     float mGlobalSaturationFactor = 1.0f;
1215     mat4 mClientColorMatrix;
1216 
1217     // Can't be unordered_set because wp<> isn't hashable
1218     std::set<wp<IBinder>> mGraphicBufferProducerList;
1219     size_t mMaxGraphicBufferProducerListSize = ISurfaceComposer::MAX_LAYERS;
1220     // If there are more GraphicBufferProducers tracked by SurfaceFlinger than
1221     // this threshold, then begin logging.
1222     size_t mGraphicBufferProducerListSizeLogThreshold =
1223             static_cast<size_t>(0.95 * static_cast<double>(MAX_LAYERS));
1224 
1225     void removeGraphicBufferProducerAsync(const wp<IBinder>&);
1226 
1227     // protected by mStateLock (but we could use another lock)
1228     bool mLayersRemoved = false;
1229     bool mLayersAdded = false;
1230 
1231     std::atomic<bool> mRepaintEverything = false;
1232 
1233     // constant members (no synchronization needed for access)
1234     const nsecs_t mBootTime = systemTime();
1235     bool mGpuToCpuSupported = false;
1236     bool mIsUserBuild = true;
1237 
1238     // Can only accessed from the main thread, these members
1239     // don't need synchronization
1240     State mDrawingState{LayerVector::StateSet::Drawing};
1241     bool mVisibleRegionsDirty = false;
1242 
1243     // VisibleRegions dirty is already cleared by postComp, but we need to track it to prevent
1244     // extra work in the HDR layer info listener.
1245     bool mVisibleRegionsWereDirtyThisFrame = false;
1246     // Used to ensure we omit a callback when HDR layer info listener is newly added but the
1247     // scene hasn't changed
1248     bool mAddingHDRLayerInfoListener = false;
1249 
1250     // Set during transaction application stage to track if the input info or children
1251     // for a layer has changed.
1252     // TODO: Also move visibleRegions over to a boolean system.
1253     bool mInputInfoChanged = false;
1254     bool mSomeChildrenChanged;
1255     bool mSomeDataspaceChanged = false;
1256     bool mForceTransactionDisplayChange = false;
1257 
1258     bool mGeometryInvalid = false;
1259     bool mAnimCompositionPending = false;
1260 
1261     // Tracks layers that have pending frames which are candidates for being
1262     // latched.
1263     std::unordered_set<sp<Layer>, ISurfaceComposer::SpHash<Layer>> mLayersWithQueuedFrames;
1264     // Tracks layers that need to update a display's dirty region.
1265     std::vector<sp<Layer>> mLayersPendingRefresh;
1266     std::array<FenceWithFenceTime, 2> mPreviousPresentFences;
1267     // True if in the previous frame at least one layer was composed via the GPU.
1268     bool mHadClientComposition = false;
1269     // True if in the previous frame at least one layer was composed via HW Composer.
1270     // Note that it is possible for a frame to be composed via both client and device
1271     // composition, for example in the case of overlays.
1272     bool mHadDeviceComposition = false;
1273     // True if in the previous frame, the client composition was skipped by reusing the buffer
1274     // used in a previous composition. This can happed if the client composition requests
1275     // did not change.
1276     bool mReusedClientComposition = false;
1277 
1278     BootStage mBootStage = BootStage::BOOTLOADER;
1279 
1280     std::vector<HotplugEvent> mPendingHotplugEvents GUARDED_BY(mStateLock);
1281 
1282     // this may only be written from the main thread with mStateLock held
1283     // it may be read from other threads with mStateLock held
1284     std::map<wp<IBinder>, sp<DisplayDevice>> mDisplays GUARDED_BY(mStateLock);
1285     std::unordered_map<PhysicalDisplayId, sp<IBinder>> mPhysicalDisplayTokens
1286             GUARDED_BY(mStateLock);
1287 
1288     struct {
1289         DisplayIdGenerator<GpuVirtualDisplayId> gpu;
1290         std::optional<DisplayIdGenerator<HalVirtualDisplayId>> hal;
1291     } mVirtualDisplayIdGenerators;
1292 
1293     std::unordered_map<BBinder*, wp<Layer>> mLayersByLocalBinderToken GUARDED_BY(mStateLock);
1294 
1295     // don't use a lock for these, we don't care
1296     int mDebugRegion = 0;
1297     bool mDebugDisableHWC = false;
1298     bool mDebugDisableTransformHint = false;
1299     bool mLayerCachingEnabled = false;
1300     volatile nsecs_t mDebugInTransaction = 0;
1301     bool mForceFullDamage = false;
1302     bool mPropagateBackpressureClientComposition = false;
1303     sp<SurfaceInterceptor> mInterceptor;
1304 
1305     SurfaceTracing mTracing{*this};
1306     std::mutex mTracingLock;
1307     bool mTracingEnabled = false;
1308     bool mTracePostComposition = false;
1309     std::atomic<bool> mTracingEnabledChanged = false;
1310 
1311     const std::shared_ptr<TimeStats> mTimeStats;
1312     const std::unique_ptr<FrameTracer> mFrameTracer;
1313     const std::unique_ptr<frametimeline::FrameTimeline> mFrameTimeline;
1314 
1315     // If blurs should be enabled on this device.
1316     bool mSupportsBlur = false;
1317     // If blurs are considered expensive and should require high GPU frequency.
1318     bool mBlursAreExpensive = false;
1319     std::atomic<uint32_t> mFrameMissedCount = 0;
1320     std::atomic<uint32_t> mHwcFrameMissedCount = 0;
1321     std::atomic<uint32_t> mGpuFrameMissedCount = 0;
1322 
1323     TransactionCallbackInvoker mTransactionCallbackInvoker;
1324 
1325     // these are thread safe
1326     std::unique_ptr<MessageQueue> mEventQueue;
1327     FrameTracker mAnimFrameTracker;
1328 
1329     // protected by mDestroyedLayerLock;
1330     mutable Mutex mDestroyedLayerLock;
1331     Vector<Layer const *> mDestroyedLayers;
1332 
1333     nsecs_t mRefreshStartTime = 0;
1334 
1335     std::atomic<bool> mRefreshPending = false;
1336 
1337     // We maintain a pool of pre-generated texture names to hand out to avoid
1338     // layer creation needing to run on the main thread (which it would
1339     // otherwise need to do to access RenderEngine).
1340     std::mutex mTexturePoolMutex;
1341     uint32_t mTexturePoolSize = 0;
1342     std::vector<uint32_t> mTexturePool;
1343 
1344     mutable Mutex mQueueLock;
1345     Condition mTransactionQueueCV;
1346     std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash>
1347             mPendingTransactionQueues GUARDED_BY(mQueueLock);
1348     std::queue<TransactionState> mTransactionQueue GUARDED_BY(mQueueLock);
1349     /*
1350      * Feature prototyping
1351      */
1352 
1353     // Static screen stats
1354     bool mHasPoweredOff = false;
1355 
1356     std::atomic<size_t> mNumLayers = 0;
1357 
1358     // to linkToDeath
1359     sp<IBinder> mWindowManager;
1360     // We want to avoid multiple calls to BOOT_FINISHED as they come in on
1361     // different threads without a lock and could trigger unsynchronized writes to
1362     // to mWindowManager or mInputFlinger
1363     std::atomic<bool> mBootFinished = false;
1364 
1365     std::thread::id mMainThreadId = std::this_thread::get_id();
1366 
1367     DisplayColorSetting mDisplayColorSetting = DisplayColorSetting::kEnhanced;
1368 
1369     // Color mode forced by setting persist.sys.sf.color_mode, it must:
1370     //     1. not be NATIVE color mode, NATIVE color mode means no forced color mode;
1371     //     2. be one of the supported color modes returned by hardware composer, otherwise
1372     //        it will not be respected.
1373     // persist.sys.sf.color_mode will only take effect when persist.sys.sf.native_mode
1374     // is not set to 1.
1375     // This property can be used to force SurfaceFlinger to always pick a certain color mode.
1376     ui::ColorMode mForceColorMode = ui::ColorMode::NATIVE;
1377 
1378     ui::Dataspace mDefaultCompositionDataspace;
1379     ui::Dataspace mWideColorGamutCompositionDataspace;
1380     ui::Dataspace mColorSpaceAgnosticDataspace;
1381 
1382     SurfaceFlingerBE mBE;
1383     std::unique_ptr<compositionengine::CompositionEngine> mCompositionEngine;
1384 
1385     const std::string mHwcServiceName;
1386 
hasMockHwc()1387     bool hasMockHwc() const { return mHwcServiceName == "mock"; }
1388 
1389     /*
1390      * Scheduler
1391      */
1392     std::unique_ptr<Scheduler> mScheduler;
1393     scheduler::ConnectionHandle mAppConnectionHandle;
1394     scheduler::ConnectionHandle mSfConnectionHandle;
1395 
1396     // Stores phase offsets configured per refresh rate.
1397     std::unique_ptr<scheduler::VsyncConfiguration> mVsyncConfiguration;
1398 
1399     // Optional to defer construction until PhaseConfiguration is created.
1400     sp<VsyncModulator> mVsyncModulator;
1401 
1402     std::unique_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs;
1403     std::unique_ptr<scheduler::RefreshRateStats> mRefreshRateStats;
1404 
1405     std::atomic<nsecs_t> mExpectedPresentTime = 0;
1406     nsecs_t mScheduledPresentTime = 0;
1407     hal::Vsync mHWCVsyncPendingState = hal::Vsync::DISABLE;
1408 
1409     std::mutex mActiveModeLock;
1410     // This bit is set once we start setting the mode. We read from this bit during the
1411     // process. If at the end, this bit is different than mDesiredActiveMode, we restart
1412     // the process.
1413     ActiveModeInfo mUpcomingActiveMode; // Always read and written on the main thread.
1414     // This bit can be set at any point in time when the system wants the new mode.
1415     ActiveModeInfo mDesiredActiveMode GUARDED_BY(mActiveModeLock);
1416 
1417     // below flags are set by main thread only
1418     TracedOrdinal<bool> mDesiredActiveModeChanged
1419             GUARDED_BY(mActiveModeLock) = {"DesiredActiveModeChanged", false};
1420     bool mSetActiveModePending = false;
1421 
1422     bool mLumaSampling = true;
1423     sp<RegionSamplingThread> mRegionSamplingThread;
1424     sp<FpsReporter> mFpsReporter;
1425     sp<TunnelModeEnabledReporter> mTunnelModeEnabledReporter;
1426     ui::DisplayPrimaries mInternalDisplayPrimaries;
1427 
1428     const float mInternalDisplayDensity;
1429     const float mEmulatedDisplayDensity;
1430 
1431     sp<os::IInputFlinger> mInputFlinger;
1432     // Should only be accessed by the main thread.
1433     InputWindowCommands mInputWindowCommands;
1434 
1435     sp<SetInputWindowsListener> mSetInputWindowsListener;
1436 
1437     Hwc2::impl::PowerAdvisor mPowerAdvisor;
1438 
1439     // This should only be accessed on the main thread.
1440     nsecs_t mFrameStartTime = 0;
1441 
1442     void enableRefreshRateOverlay(bool enable);
1443     std::unique_ptr<RefreshRateOverlay> mRefreshRateOverlay GUARDED_BY(mStateLock);
1444 
1445     // Flag used to set override desired display mode from backdoor
1446     bool mDebugDisplayModeSetByBackdoor = false;
1447 
1448     // A set of layers that have no parent so they are not drawn on screen.
1449     // Should only be accessed by the main thread.
1450     // The Layer pointer is removed from the set when the destructor is called so there shouldn't
1451     // be any issues with a raw pointer referencing an invalid object.
1452     std::unordered_set<Layer*> mOffscreenLayers;
1453 
1454     int mFrameRateFlexibilityTokenCount = 0;
1455 
1456     sp<IBinder> mDebugFrameRateFlexibilityToken;
1457 
1458     BufferCountTracker mBufferCountTracker;
1459 
1460     std::unordered_map<DisplayId, sp<HdrLayerInfoReporter>> mHdrLayerInfoListeners
1461             GUARDED_BY(mStateLock);
1462     mutable Mutex mCreatedLayersLock;
1463     struct LayerCreatedState {
LayerCreatedStateLayerCreatedState1464         LayerCreatedState(const wp<Layer>& layer, const wp<IBinder>& parent,
1465                           const wp<Layer> parentLayer, const wp<IBinder>& producer, bool addToRoot)
1466               : layer(layer),
1467                 initialParent(parent),
1468                 initialParentLayer(parentLayer),
1469                 initialProducer(producer),
1470                 addToRoot(addToRoot) {}
1471         wp<Layer> layer;
1472         // Indicates the initial parent of the created layer, only used for creating layer in
1473         // SurfaceFlinger. If nullptr, it may add the created layer into the current root layers.
1474         wp<IBinder> initialParent;
1475         wp<Layer> initialParentLayer;
1476         // Indicates the initial graphic buffer producer of the created layer, only used for
1477         // creating layer in SurfaceFlinger.
1478         wp<IBinder> initialProducer;
1479         // Indicates whether the layer getting created should be added at root if there's no parent
1480         // and has permission ACCESS_SURFACE_FLINGER. If set to false and no parent, the layer will
1481         // be added offscreen.
1482         bool addToRoot;
1483     };
1484 
1485     // A temporay pool that store the created layers and will be added to current state in main
1486     // thread.
1487     std::unordered_map<BBinder*, std::unique_ptr<LayerCreatedState>> mCreatedLayers;
1488     void setLayerCreatedState(const sp<IBinder>& handle, const wp<Layer>& layer,
1489                               const wp<IBinder>& parent, const wp<Layer> parentLayer,
1490                               const wp<IBinder>& producer, bool addToRoot);
1491     auto getLayerCreatedState(const sp<IBinder>& handle);
1492     sp<Layer> handleLayerCreatedLocked(const sp<IBinder>& handle) REQUIRES(mStateLock);
1493 
1494     std::atomic<ui::Transform::RotationFlags> mDefaultDisplayTransformHint;
1495 
1496     void scheduleRegionSamplingThread();
1497     void notifyRegionSamplingThread();
1498 };
1499 
1500 } // namespace android
1501