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 class IGraphicBufferProducer;
97 class Layer;
98 class MessageBase;
99 class RefreshRateOverlay;
100 class RegionSamplingThread;
101 class RenderArea;
102 class TimeStats;
103 class FrameTracer;
104 class WindowInfosListenerInvoker;
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) const;
331 
332     // If set, disables reusing client composition buffers. This can be set by
333     // debug.sf.disable_client_composition_cache
334     bool mDisableClientCompositionCache = false;
335     void windowInfosReported();
336 
337     // Disables expensive rendering for all displays
338     // This is scheduled on the main thread
339     void disableExpensiveRendering();
340     FloatRect getMaxDisplayBounds();
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     virtual void processDisplayAdded(const wp<IBinder>& displayToken, const DisplayDeviceState&)
355             REQUIRES(mStateLock);
356 
357     // Returns true if any display matches a `bool(const DisplayDevice&)` predicate.
358     template <typename Predicate>
hasDisplay(Predicate p)359     bool hasDisplay(Predicate p) const REQUIRES(mStateLock) {
360         return static_cast<bool>(findDisplay(p));
361     }
362 
363 private:
364     friend class BufferLayer;
365     friend class BufferQueueLayer;
366     friend class BufferStateLayer;
367     friend class Client;
368     friend class FpsReporter;
369     friend class TunnelModeEnabledReporter;
370     friend class Layer;
371     friend class MonitoredProducer;
372     friend class RefreshRateOverlay;
373     friend class RegionSamplingThread;
374     friend class SurfaceTracing;
375 
376     // For unit tests
377     friend class TestableSurfaceFlinger;
378     friend class TransactionApplicationTest;
379     friend class TunnelModeEnabledReporterTest;
380 
381     using RefreshRate = scheduler::RefreshRateConfigs::RefreshRate;
382     using VsyncModulator = scheduler::VsyncModulator;
383     using TransactionSchedule = scheduler::TransactionSchedule;
384     using TraverseLayersFunction = std::function<void(const LayerVector::Visitor&)>;
385     using RenderAreaFuture = std::future<std::unique_ptr<RenderArea>>;
386     using DumpArgs = Vector<String16>;
387     using Dumper = std::function<void(const DumpArgs&, bool asProto, std::string&)>;
388 
389     // This value is specified in number of frames.  Log frame stats at most
390     // every half hour.
391     enum { LOG_FRAME_STATS_PERIOD =  30*60*60 };
392 
393     class State {
394     public:
State(LayerVector::StateSet set)395         explicit State(LayerVector::StateSet set) : stateSet(set), layersSortedByZ(set) {}
396         State& operator=(const State& other) {
397             // We explicitly don't copy stateSet so that, e.g., mDrawingState
398             // always uses the Drawing StateSet.
399             layersSortedByZ = other.layersSortedByZ;
400             displays = other.displays;
401             colorMatrixChanged = other.colorMatrixChanged;
402             if (colorMatrixChanged) {
403                 colorMatrix = other.colorMatrix;
404             }
405             globalShadowSettings = other.globalShadowSettings;
406 
407             return *this;
408         }
409 
410         const LayerVector::StateSet stateSet = LayerVector::StateSet::Invalid;
411         LayerVector layersSortedByZ;
412         DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
413 
414         bool colorMatrixChanged = true;
415         mat4 colorMatrix;
416 
417         renderengine::ShadowSettings globalShadowSettings;
418 
419         void traverse(const LayerVector::Visitor& visitor) const;
420         void traverseInZOrder(const LayerVector::Visitor& visitor) const;
421         void traverseInReverseZOrder(const LayerVector::Visitor& visitor) const;
422     };
423 
424     // Keeps track of pending buffers per layer handle in the transaction queue or current/drawing
425     // state before the buffers are latched. The layer owns the atomic counters and decrements the
426     // count in the main thread when dropping or latching a buffer.
427     //
428     // The binder threads increment the same counter when a new transaction containing a buffer is
429     // added to the transaction queue. The map is updated with the layer handle lifecycle updates.
430     // This is done to avoid lock contention with the main thread.
431     class BufferCountTracker {
432     public:
increment(BBinder * layerHandle)433         void increment(BBinder* layerHandle) {
434             std::lock_guard<std::mutex> lock(mLock);
435             auto it = mCounterByLayerHandle.find(layerHandle);
436             if (it != mCounterByLayerHandle.end()) {
437                 auto [name, pendingBuffers] = it->second;
438                 int32_t count = ++(*pendingBuffers);
439                 ATRACE_INT(name.c_str(), count);
440             } else {
441                 ALOGW("Handle not found! %p", layerHandle);
442             }
443         }
444 
add(BBinder * layerHandle,const std::string & name,std::atomic<int32_t> * counter)445         void add(BBinder* layerHandle, const std::string& name, std::atomic<int32_t>* counter) {
446             std::lock_guard<std::mutex> lock(mLock);
447             mCounterByLayerHandle[layerHandle] = std::make_pair(name, counter);
448         }
449 
remove(BBinder * layerHandle)450         void remove(BBinder* layerHandle) {
451             std::lock_guard<std::mutex> lock(mLock);
452             mCounterByLayerHandle.erase(layerHandle);
453         }
454 
455     private:
456         std::mutex mLock;
457         std::unordered_map<BBinder*, std::pair<std::string, std::atomic<int32_t>*>>
458                 mCounterByLayerHandle GUARDED_BY(mLock);
459     };
460 
461     using ActiveModeInfo = DisplayDevice::ActiveModeInfo;
462 
463     enum class BootStage {
464         BOOTLOADER,
465         BOOTANIMATION,
466         FINISHED,
467     };
468 
469     struct HotplugEvent {
470         hal::HWDisplayId hwcDisplayId;
471         hal::Connection connection = hal::Connection::INVALID;
472     };
473 
474     class CountDownLatch {
475     public:
476         enum {
477             eSyncTransaction = 1 << 0,
478             eSyncInputWindows = 1 << 1,
479         };
CountDownLatch(uint32_t flags)480         explicit CountDownLatch(uint32_t flags) : mFlags(flags) {}
481 
482         // True if there is no waiting condition after count down.
countDown(uint32_t flag)483         bool countDown(uint32_t flag) {
484             std::unique_lock<std::mutex> lock(mMutex);
485             if (mFlags == 0) {
486                 return true;
487             }
488             mFlags &= ~flag;
489             if (mFlags == 0) {
490                 mCountDownComplete.notify_all();
491                 return true;
492             }
493             return false;
494         }
495 
496         // Return true if triggered.
wait_until(const std::chrono::seconds & timeout)497         bool wait_until(const std::chrono::seconds& timeout) const {
498             std::unique_lock<std::mutex> lock(mMutex);
499             const auto untilTime = std::chrono::system_clock::now() + timeout;
500             while (mFlags != 0) {
501                 // Conditional variables can be woken up sporadically, so we check count
502                 // to verify the wakeup was triggered by |countDown|.
503                 if (std::cv_status::timeout == mCountDownComplete.wait_until(lock, untilTime)) {
504                     return false;
505                 }
506             }
507             return true;
508         }
509 
510     private:
511         uint32_t mFlags;
512         mutable std::condition_variable mCountDownComplete;
513         mutable std::mutex mMutex;
514     };
515 
516     struct TransactionState {
TransactionStateTransactionState517         TransactionState(const FrameTimelineInfo& frameTimelineInfo,
518                          const Vector<ComposerState>& composerStates,
519                          const Vector<DisplayState>& displayStates, uint32_t transactionFlags,
520                          const sp<IBinder>& applyToken,
521                          const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime,
522                          bool isAutoTimestamp, const client_cache_t& uncacheBuffer,
523                          int64_t postTime, uint32_t permissions, bool hasListenerCallbacks,
524                          std::vector<ListenerCallbacks> listenerCallbacks, int originPid,
525                          int originUid, uint64_t transactionId)
526               : frameTimelineInfo(frameTimelineInfo),
527                 states(composerStates),
528                 displays(displayStates),
529                 flags(transactionFlags),
530                 applyToken(applyToken),
531                 inputWindowCommands(inputWindowCommands),
532                 desiredPresentTime(desiredPresentTime),
533                 isAutoTimestamp(isAutoTimestamp),
534                 buffer(uncacheBuffer),
535                 postTime(postTime),
536                 permissions(permissions),
537                 hasListenerCallbacks(hasListenerCallbacks),
538                 listenerCallbacks(listenerCallbacks),
539                 originPid(originPid),
540                 originUid(originUid),
541                 id(transactionId) {}
542 
543         void traverseStatesWithBuffers(std::function<void(const layer_state_t&)> visitor);
544 
545         FrameTimelineInfo frameTimelineInfo;
546         Vector<ComposerState> states;
547         Vector<DisplayState> displays;
548         uint32_t flags;
549         sp<IBinder> applyToken;
550         InputWindowCommands inputWindowCommands;
551         const int64_t desiredPresentTime;
552         const bool isAutoTimestamp;
553         client_cache_t buffer;
554         const int64_t postTime;
555         uint32_t permissions;
556         bool hasListenerCallbacks;
557         std::vector<ListenerCallbacks> listenerCallbacks;
558         int originPid;
559         int originUid;
560         uint64_t id;
561         std::shared_ptr<CountDownLatch> transactionCommittedSignal;
562     };
563 
564     template <typename F, std::enable_if_t<!std::is_member_function_pointer_v<F>>* = nullptr>
dumper(F && dump)565     static Dumper dumper(F&& dump) {
566         using namespace std::placeholders;
567         return std::bind(std::forward<F>(dump), _3);
568     }
569 
570     template <typename F, std::enable_if_t<std::is_member_function_pointer_v<F>>* = nullptr>
dumper(F dump)571     Dumper dumper(F dump) {
572         using namespace std::placeholders;
573         return std::bind(dump, this, _3);
574     }
575 
576     template <typename F>
argsDumper(F dump)577     Dumper argsDumper(F dump) {
578         using namespace std::placeholders;
579         return std::bind(dump, this, _1, _3);
580     }
581 
582     template <typename F>
protoDumper(F dump)583     Dumper protoDumper(F dump) {
584         using namespace std::placeholders;
585         return std::bind(dump, this, _1, _2, _3);
586     }
587 
588     template <typename... Args,
589               typename Handler = VsyncModulator::VsyncConfigOpt (VsyncModulator::*)(Args...)>
modulateVsync(Handler handler,Args...args)590     void modulateVsync(Handler handler, Args... args) {
591         if (const auto config = (*mVsyncModulator.*handler)(args...)) {
592             const auto vsyncPeriod = mScheduler->getVsyncPeriodFromRefreshRateConfigs();
593             setVsyncConfig(*config, vsyncPeriod);
594         }
595     }
596 
597     static const int MAX_TRACING_MEMORY = 100 * 1024 * 1024; // 100MB
598     // Maximum allowed number of display frames that can be set through backdoor
599     static const int MAX_ALLOWED_DISPLAY_FRAMES = 2048;
600 
601     // Implements IBinder.
602     status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) override;
dump(int fd,const Vector<String16> & args)603     status_t dump(int fd, const Vector<String16>& args) override { return priorityDump(fd, args); }
604     bool callingThreadHasUnscopedSurfaceFlingerAccess(bool usePermissionCache = true)
605             EXCLUDES(mStateLock);
606 
607     // Implements ISurfaceComposer
608     sp<ISurfaceComposerClient> createConnection() override;
609     sp<IBinder> createDisplay(const String8& displayName, bool secure) override;
610     void destroyDisplay(const sp<IBinder>& displayToken) override;
getPhysicalDisplayIds()611     std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const override EXCLUDES(mStateLock) {
612         Mutex::Autolock lock(mStateLock);
613         return getPhysicalDisplayIdsLocked();
614     }
615     status_t getPrimaryPhysicalDisplayId(PhysicalDisplayId*) const override EXCLUDES(mStateLock);
616 
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     status_t addWindowInfosListener(
725             const sp<gui::IWindowInfosListener>& windowInfosListener) const override;
726     status_t removeWindowInfosListener(
727             const sp<gui::IWindowInfosListener>& windowInfosListener) const override;
728 
729     // Implements IBinder::DeathRecipient.
730     void binderDied(const wp<IBinder>& who) override;
731 
732     // Implements RefBase.
733     void onFirstRef() override;
734 
735     // HWC2::ComposerCallback overrides:
736     void onComposerHalVsync(hal::HWDisplayId, int64_t timestamp,
737                             std::optional<hal::VsyncPeriodNanos>) override;
738     void onComposerHalHotplug(hal::HWDisplayId, hal::Connection) override;
739     void onComposerHalRefresh(hal::HWDisplayId) override;
740     void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
741                                                const hal::VsyncPeriodChangeTimeline&) override;
742     void onComposerHalSeamlessPossible(hal::HWDisplayId) override;
743 
744     /*
745      * ISchedulerCallback
746      */
747 
748     // Toggles hardware VSYNC by calling into HWC.
749     void setVsyncEnabled(bool) override;
750     // Initiates a refresh rate change to be applied on invalidate.
751     void changeRefreshRate(const Scheduler::RefreshRate&, Scheduler::ModeEvent) override;
752     // Forces full composition on all displays without resetting the scheduler idle timer.
753     void repaintEverythingForHWC() override;
754     // Called when kernel idle timer has expired. Used to update the refresh rate overlay.
755     void kernelTimerChanged(bool expired) override;
756     // Called when the frame rate override list changed to trigger an event.
757     void triggerOnFrameRateOverridesChanged() override;
758     // Toggles the kernel idle timer on or off depending the policy decisions around refresh rates.
759     void toggleKernelIdleTimer() REQUIRES(mStateLock);
760     // Keeps track of whether the kernel idle timer is currently enabled, so we don't have to
761     // make calls to sys prop each time.
762     bool mKernelIdleTimerEnabled = false;
763     // Show spinner with refresh rate overlay
764     bool mRefreshRateOverlaySpinner = false;
765 
766     /*
767      * Message handling
768      */
769     // Can only be called from the main thread or with mStateLock held
770     void signalTransaction();
771     // Can only be called from the main thread or with mStateLock held
772     void signalLayerUpdate();
773     void signalRefresh();
774 
775     // Called on the main thread in response to initializeDisplays()
776     void onInitializeDisplays() REQUIRES(mStateLock);
777     // Sets the desired active mode bit. It obtains the lock, and sets mDesiredActiveMode.
778     void setDesiredActiveMode(const ActiveModeInfo& info) REQUIRES(mStateLock);
779     status_t setActiveMode(const sp<IBinder>& displayToken, int id);
780     // Once HWC has returned the present fence, this sets the active mode and a new refresh
781     // rate in SF.
782     void setActiveModeInternal() REQUIRES(mStateLock);
783     // Calls to setActiveMode on the main thread if there is a pending mode change
784     // that needs to be applied.
785     void performSetActiveMode() REQUIRES(mStateLock);
786     void clearDesiredActiveModeState(const sp<DisplayDevice>&) REQUIRES(mStateLock);
787     // Called when active mode is no longer is progress
788     void desiredActiveModeChangeDone(const sp<DisplayDevice>&) REQUIRES(mStateLock);
789     // Called on the main thread in response to setPowerMode()
790     void setPowerModeInternal(const sp<DisplayDevice>& display, hal::PowerMode mode)
791             REQUIRES(mStateLock);
792 
793     // Sets the desired display mode specs.
794     status_t setDesiredDisplayModeSpecsInternal(
795             const sp<DisplayDevice>& display,
796             const std::optional<scheduler::RefreshRateConfigs::Policy>& policy, bool overridePolicy)
797             EXCLUDES(mStateLock);
798 
799     // Handle the INVALIDATE message queue event, latching new buffers and applying
800     // incoming transactions
801     void onMessageInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTime);
802 
803     // Returns whether the transaction actually modified any state
804     bool handleMessageTransaction();
805 
806     // Handle the REFRESH message queue event, sending the current frame down to RenderEngine and
807     // the Composer HAL for presentation
808     void onMessageRefresh();
809 
810     // Returns whether a new buffer has been latched (see handlePageFlip())
811     bool handleMessageInvalidate();
812 
813     void handleTransaction(uint32_t transactionFlags);
814     void handleTransactionLocked(uint32_t transactionFlags) REQUIRES(mStateLock);
815 
816     void updateInputFlinger();
817     void notifyWindowInfos();
818     void commitInputWindowCommands() REQUIRES(mStateLock);
819     void updateCursorAsync();
820 
821     void initScheduler(const sp<DisplayDevice>& display) REQUIRES(mStateLock);
822     void updatePhaseConfiguration(const Fps&) REQUIRES(mStateLock);
823     void setVsyncConfig(const VsyncModulator::VsyncConfig&, nsecs_t vsyncPeriod);
824 
825     /* handlePageFlip - latch a new buffer if available and compute the dirty
826      * region. Returns whether a new buffer has been latched, i.e., whether it
827      * is necessary to perform a refresh during this vsync.
828      */
829     bool handlePageFlip();
830 
831     /*
832      * Transactions
833      */
834     void applyTransactionState(const FrameTimelineInfo& info, const Vector<ComposerState>& state,
835                                const Vector<DisplayState>& displays, uint32_t flags,
836                                const InputWindowCommands& inputWindowCommands,
837                                const int64_t desiredPresentTime, bool isAutoTimestamp,
838                                const client_cache_t& uncacheBuffer, const int64_t postTime,
839                                uint32_t permissions, bool hasListenerCallbacks,
840                                const std::vector<ListenerCallbacks>& listenerCallbacks,
841                                int originPid, int originUid, uint64_t transactionId)
842             REQUIRES(mStateLock);
843     // flush pending transaction that was presented after desiredPresentTime.
844     void flushTransactionQueues();
845     // Returns true if there is at least one transaction that needs to be flushed
846     bool transactionFlushNeeded();
847     uint32_t getTransactionFlags(uint32_t flags);
848     uint32_t peekTransactionFlags();
849     // Can only be called from the main thread or with mStateLock held
850     uint32_t setTransactionFlags(uint32_t flags);
851     // Indicate SF should call doTraversal on layers, but don't trigger a wakeup! We use this cases
852     // where there are still pending transactions but we know they won't be ready until a frame
853     // arrives from a different layer. So we need to ensure we performTransaction from invalidate
854     // but there is no need to try and wake up immediately to do it. Rather we rely on
855     // onFrameAvailable or another layer update to wake us up.
856     void setTraversalNeeded();
857     uint32_t setTransactionFlags(uint32_t flags, TransactionSchedule, const sp<IBinder>& = {});
858     void commitTransaction() REQUIRES(mStateLock);
859     void commitOffscreenLayers();
860     bool transactionIsReadyToBeApplied(
861             const FrameTimelineInfo& info, bool isAutoTimestamp, int64_t desiredPresentTime,
862             uid_t originUid, const Vector<ComposerState>& states,
863             const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>&
864                     bufferLayersReadyToPresent) const REQUIRES(mStateLock);
865     uint32_t setDisplayStateLocked(const DisplayState& s) REQUIRES(mStateLock);
866     uint32_t addInputWindowCommands(const InputWindowCommands& inputWindowCommands)
867             REQUIRES(mStateLock);
868     bool frameIsEarly(nsecs_t expectedPresentTime, int64_t vsyncId) const;
869     /*
870      * Layer management
871      */
872     status_t createLayer(const String8& name, const sp<Client>& client, uint32_t w, uint32_t h,
873                          PixelFormat format, uint32_t flags, LayerMetadata metadata,
874                          sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp,
875                          const sp<IBinder>& parentHandle, int32_t* outLayerId,
876                          const sp<Layer>& parentLayer = nullptr,
877                          uint32_t* outTransformHint = nullptr);
878 
879     status_t createBufferQueueLayer(const sp<Client>& client, std::string name, uint32_t w,
880                                     uint32_t h, uint32_t flags, LayerMetadata metadata,
881                                     PixelFormat& format, sp<IBinder>* outHandle,
882                                     sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
883 
884     status_t createBufferStateLayer(const sp<Client>& client, std::string name, uint32_t w,
885                                     uint32_t h, uint32_t flags, LayerMetadata metadata,
886                                     sp<IBinder>* outHandle, sp<Layer>* outLayer);
887 
888     status_t createEffectLayer(const sp<Client>& client, std::string name, uint32_t w, uint32_t h,
889                                uint32_t flags, LayerMetadata metadata, sp<IBinder>* outHandle,
890                                sp<Layer>* outLayer);
891 
892     status_t createContainerLayer(const sp<Client>& client, std::string name, uint32_t w,
893                                   uint32_t h, uint32_t flags, LayerMetadata metadata,
894                                   sp<IBinder>* outHandle, sp<Layer>* outLayer);
895 
896     status_t mirrorLayer(const sp<Client>& client, const sp<IBinder>& mirrorFromHandle,
897                          sp<IBinder>* outHandle, int32_t* outLayerId);
898 
899     std::string getUniqueLayerName(const char* name);
900 
901     // called when all clients have released all their references to
902     // this layer meaning it is entirely safe to destroy all
903     // resources associated to this layer.
904     void onHandleDestroyed(BBinder* handle, sp<Layer>& layer);
905     void markLayerPendingRemovalLocked(const sp<Layer>& layer);
906 
907     // add a layer to SurfaceFlinger
908     status_t addClientLayer(const sp<Client>& client, const sp<IBinder>& handle,
909                             const sp<IGraphicBufferProducer>& gbc, const sp<Layer>& lbc,
910                             const sp<IBinder>& parentHandle, const sp<Layer>& parentLayer,
911                             bool addToRoot, uint32_t* outTransformHint);
912 
913     // Traverse through all the layers and compute and cache its bounds.
914     void computeLayerBounds();
915 
916     // Boot animation, on/off animations and screen capture
917     void startBootAnim();
918 
919     status_t captureScreenCommon(RenderAreaFuture, TraverseLayersFunction, ui::Size bufferSize,
920                                  ui::PixelFormat, bool allowProtected, bool grayscale,
921                                  const sp<IScreenCaptureListener>&);
922     status_t captureScreenCommon(RenderAreaFuture, TraverseLayersFunction,
923                                  const std::shared_ptr<renderengine::ExternalTexture>&,
924                                  bool regionSampling, bool grayscale,
925                                  const sp<IScreenCaptureListener>&);
926     status_t renderScreenImplLocked(const RenderArea&, TraverseLayersFunction,
927                                     const std::shared_ptr<renderengine::ExternalTexture>&,
928                                     bool canCaptureBlackoutContent, bool regionSampling,
929                                     bool grayscale, ScreenCaptureResults&);
930 
931     // If the uid provided is not UNSET_UID, the traverse will skip any layers that don't have a
932     // matching ownerUid
933     void traverseLayersInLayerStack(ui::LayerStack, const int32_t uid, const LayerVector::Visitor&);
934 
935     void readPersistentProperties();
936 
exceedsMaxRenderTargetSize(uint32_t width,uint32_t height)937     bool exceedsMaxRenderTargetSize(uint32_t width, uint32_t height) const {
938         return width > mMaxRenderTargetSize || height > mMaxRenderTargetSize;
939     }
940 
941     int getMaxAcquiredBufferCountForCurrentRefreshRate(uid_t uid) const;
942 
943     /*
944      * Display and layer stack management
945      */
946     // called when starting, or restarting after system_server death
947     void initializeDisplays();
948 
getDisplayDeviceLocked(const wp<IBinder> & displayToken)949     sp<const DisplayDevice> getDisplayDeviceLocked(const wp<IBinder>& displayToken) const
950             REQUIRES(mStateLock) {
951         return const_cast<SurfaceFlinger*>(this)->getDisplayDeviceLocked(displayToken);
952     }
953 
getDisplayDeviceLocked(const wp<IBinder> & displayToken)954     sp<DisplayDevice> getDisplayDeviceLocked(const wp<IBinder>& displayToken) REQUIRES(mStateLock) {
955         const auto it = mDisplays.find(displayToken);
956         return it == mDisplays.end() ? nullptr : it->second;
957     }
958 
getDisplayDeviceLocked(PhysicalDisplayId id)959     sp<const DisplayDevice> getDisplayDeviceLocked(PhysicalDisplayId id) const
960             REQUIRES(mStateLock) {
961         return const_cast<SurfaceFlinger*>(this)->getDisplayDeviceLocked(id);
962     }
963 
getDisplayDeviceLocked(PhysicalDisplayId id)964     sp<DisplayDevice> getDisplayDeviceLocked(PhysicalDisplayId id) REQUIRES(mStateLock) {
965         if (const auto token = getPhysicalDisplayTokenLocked(id)) {
966             return getDisplayDeviceLocked(token);
967         }
968         return nullptr;
969     }
970 
getDefaultDisplayDeviceLocked()971     sp<const DisplayDevice> getDefaultDisplayDeviceLocked() const REQUIRES(mStateLock) {
972         return const_cast<SurfaceFlinger*>(this)->getDefaultDisplayDeviceLocked();
973     }
974 
getDefaultDisplayDeviceLocked()975     sp<DisplayDevice> getDefaultDisplayDeviceLocked() REQUIRES(mStateLock) {
976         if (const auto display = getDisplayDeviceLocked(mActiveDisplayToken)) {
977             return display;
978         }
979         // The active display is outdated, fall back to the internal display
980         mActiveDisplayToken.clear();
981         if (const auto token = getInternalDisplayTokenLocked()) {
982             return getDisplayDeviceLocked(token);
983         }
984         return nullptr;
985     }
986 
getDefaultDisplayDevice()987     sp<const DisplayDevice> getDefaultDisplayDevice() const EXCLUDES(mStateLock) {
988         Mutex::Autolock lock(mStateLock);
989         return getDefaultDisplayDeviceLocked();
990     }
991 
992     // Returns the first display that matches a `bool(const DisplayDevice&)` predicate.
993     template <typename Predicate>
findDisplay(Predicate p)994     sp<DisplayDevice> findDisplay(Predicate p) const REQUIRES(mStateLock) {
995         const auto it = std::find_if(mDisplays.begin(), mDisplays.end(),
996                                      [&](const auto& pair) { return p(*pair.second); });
997 
998         return it == mDisplays.end() ? nullptr : it->second;
999     }
1000 
getDisplayDeviceLocked(DisplayId id)1001     sp<const DisplayDevice> getDisplayDeviceLocked(DisplayId id) const REQUIRES(mStateLock) {
1002         // TODO(b/182939859): Replace tokens with IDs for display lookup.
1003         return findDisplay([id](const auto& display) { return display.getId() == id; });
1004     }
1005 
1006     std::vector<PhysicalDisplayId> getPhysicalDisplayIdsLocked() const REQUIRES(mStateLock);
1007 
1008     // mark a region of a layer stack dirty. this updates the dirty
1009     // region of all screens presenting this layer stack.
1010     void invalidateLayerStack(const sp<const Layer>& layer, const Region& dirty);
1011 
1012     sp<DisplayDevice> getDisplayWithInputByLayer(Layer* layer) const REQUIRES(mStateLock);
1013 
isDisplayActiveLocked(const sp<const DisplayDevice> & display)1014     bool isDisplayActiveLocked(const sp<const DisplayDevice>& display) const REQUIRES(mStateLock) {
1015         return display->getDisplayToken() == mActiveDisplayToken;
1016     }
1017 
1018     /*
1019      * H/W composer
1020      */
1021     // The following thread safety rules apply when accessing HWComposer:
1022     // 1. When reading display state from HWComposer on the main thread, it's not necessary to
1023     //    acquire mStateLock.
1024     // 2. When accessing HWComposer on a thread other than the main thread, we always
1025     //    need to acquire mStateLock. This is because the main thread could be
1026     //    in the process of writing display state, e.g. creating or destroying a display.
1027     HWComposer& getHwComposer() const;
1028 
1029     /*
1030      * Compositing
1031      */
1032     void invalidateHwcGeometry();
1033 
1034     void postComposition();
1035     void getCompositorTiming(CompositorTiming* compositorTiming);
1036     void updateCompositorTiming(const DisplayStatInfo& stats, nsecs_t compositeTime,
1037                                 std::shared_ptr<FenceTime>& presentFenceTime);
1038     void setCompositorTimingSnapped(const DisplayStatInfo& stats,
1039                                     nsecs_t compositeToPresentLatency);
1040 
1041     void postFrame();
1042 
1043     /*
1044      * Display management
1045      */
1046     void loadDisplayModes(PhysicalDisplayId displayId, DisplayModes& outModes,
1047                           DisplayModePtr& outActiveMode) const REQUIRES(mStateLock);
1048     sp<DisplayDevice> setupNewDisplayDeviceInternal(
1049             const wp<IBinder>& displayToken,
1050             std::shared_ptr<compositionengine::Display> compositionDisplay,
1051             const DisplayDeviceState& state,
1052             const sp<compositionengine::DisplaySurface>& displaySurface,
1053             const sp<IGraphicBufferProducer>& producer) REQUIRES(mStateLock);
1054     void processDisplayChangesLocked() REQUIRES(mStateLock);
1055     void processDisplayRemoved(const wp<IBinder>& displayToken) REQUIRES(mStateLock);
1056     void processDisplayChanged(const wp<IBinder>& displayToken,
1057                                const DisplayDeviceState& currentState,
1058                                const DisplayDeviceState& drawingState) REQUIRES(mStateLock);
1059     void processDisplayHotplugEventsLocked() REQUIRES(mStateLock);
1060 
1061     void dispatchDisplayHotplugEvent(PhysicalDisplayId displayId, bool connected);
1062 
1063     /*
1064      * VSYNC
1065      */
1066     nsecs_t getVsyncPeriodFromHWC() const REQUIRES(mStateLock);
1067 
setHWCVsyncEnabled(PhysicalDisplayId id,hal::Vsync enabled)1068     void setHWCVsyncEnabled(PhysicalDisplayId id, hal::Vsync enabled) {
1069         mLastHWCVsyncState = enabled;
1070         getHwComposer().setVsyncEnabled(id, enabled);
1071     }
1072 
1073     // Sets the refresh rate by switching active configs, if they are available for
1074     // the desired refresh rate.
1075     void changeRefreshRateLocked(const RefreshRate&, Scheduler::ModeEvent) REQUIRES(mStateLock);
1076 
1077     struct FenceWithFenceTime {
1078         sp<Fence> fence = Fence::NO_FENCE;
1079         std::shared_ptr<FenceTime> fenceTime = FenceTime::NO_FENCE;
1080     };
1081 
1082     // Gets the fence for the previous frame.
1083     // Must be called on the main thread.
1084     FenceWithFenceTime previousFrameFence();
1085 
1086     // Whether the previous frame has not yet been presented to the display.
1087     // If graceTimeMs is positive, this method waits for at most the provided
1088     // grace period before reporting if the frame missed.
1089     // Must be called on the main thread.
1090     bool previousFramePending(int graceTimeMs = 0);
1091 
1092     // Returns the previous time that the frame was presented. If the frame has
1093     // not been presented yet, then returns Fence::SIGNAL_TIME_PENDING. If there
1094     // is no pending frame, then returns Fence::SIGNAL_TIME_INVALID.
1095     // Must be called on the main thread.
1096     nsecs_t previousFramePresentTime();
1097 
1098     // Calculates the expected present time for this frame. For negative offsets, performs a
1099     // correction using the predicted vsync for the next frame instead.
1100 
1101     nsecs_t calculateExpectedPresentTime(DisplayStatInfo) const;
1102 
1103     /*
1104      * Display identification
1105      */
getPhysicalDisplayTokenLocked(PhysicalDisplayId displayId)1106     sp<IBinder> getPhysicalDisplayTokenLocked(PhysicalDisplayId displayId) const
1107             REQUIRES(mStateLock) {
1108         const auto it = mPhysicalDisplayTokens.find(displayId);
1109         return it != mPhysicalDisplayTokens.end() ? it->second : nullptr;
1110     }
1111 
getPhysicalDisplayIdLocked(const sp<IBinder> & displayToken)1112     std::optional<PhysicalDisplayId> getPhysicalDisplayIdLocked(
1113             const sp<IBinder>& displayToken) const REQUIRES(mStateLock) {
1114         for (const auto& [id, token] : mPhysicalDisplayTokens) {
1115             if (token == displayToken) {
1116                 return id;
1117             }
1118         }
1119         return {};
1120     }
1121 
1122     // TODO(b/74619554): Remove special cases for primary display.
getInternalDisplayTokenLocked()1123     sp<IBinder> getInternalDisplayTokenLocked() const REQUIRES(mStateLock) {
1124         const auto displayId = getInternalDisplayIdLocked();
1125         return displayId ? getPhysicalDisplayTokenLocked(*displayId) : nullptr;
1126     }
1127 
getInternalDisplayIdLocked()1128     std::optional<PhysicalDisplayId> getInternalDisplayIdLocked() const REQUIRES(mStateLock) {
1129         const auto hwcDisplayId = getHwComposer().getInternalHwcDisplayId();
1130         return hwcDisplayId ? getHwComposer().toPhysicalDisplayId(*hwcDisplayId) : std::nullopt;
1131     }
1132 
1133     // Toggles use of HAL/GPU virtual displays.
1134     void enableHalVirtualDisplays(bool);
1135 
1136     // Virtual display lifecycle for ID generation and HAL allocation.
1137     VirtualDisplayId acquireVirtualDisplay(ui::Size, ui::PixelFormat) REQUIRES(mStateLock);
1138     void releaseVirtualDisplay(VirtualDisplayId);
1139 
1140     void onActiveDisplayChangedLocked(const sp<DisplayDevice>& activeDisplay) REQUIRES(mStateLock);
1141 
1142     void onActiveDisplaySizeChanged(const sp<DisplayDevice>& activeDisplay);
1143 
1144     /*
1145      * Debugging & dumpsys
1146      */
1147     void dumpAllLocked(const DumpArgs& args, std::string& result) const REQUIRES(mStateLock);
1148 
1149     void appendSfConfigString(std::string& result) const;
1150     void listLayersLocked(std::string& result) const;
1151     void dumpStatsLocked(const DumpArgs& args, std::string& result) const REQUIRES(mStateLock);
1152     void clearStatsLocked(const DumpArgs& args, std::string& result);
1153     void dumpTimeStats(const DumpArgs& args, bool asProto, std::string& result) const;
1154     void dumpFrameTimeline(const DumpArgs& args, std::string& result) const;
1155     void logFrameStats();
1156 
1157     void dumpVSync(std::string& result) const REQUIRES(mStateLock);
1158     void dumpStaticScreenStats(std::string& result) const;
1159     // Not const because each Layer needs to query Fences and cache timestamps.
1160     void dumpFrameEventsLocked(std::string& result);
1161 
1162     void recordBufferingStats(const std::string& layerName,
1163                               std::vector<OccupancyTracker::Segment>&& history);
1164     void dumpBufferingStats(std::string& result) const;
1165     void dumpDisplayIdentificationData(std::string& result) const REQUIRES(mStateLock);
1166     void dumpRawDisplayIdentificationData(const DumpArgs&, std::string& result) const;
1167     void dumpWideColorInfo(std::string& result) const REQUIRES(mStateLock);
1168     LayersProto dumpDrawingStateProto(uint32_t traceFlags) const;
1169     void dumpOffscreenLayersProto(LayersProto& layersProto,
1170                                   uint32_t traceFlags = SurfaceTracing::TRACE_ALL) const;
1171     void dumpDisplayProto(LayersTraceProto& layersTraceProto) const;
1172 
1173     // Dumps state from HW Composer
1174     void dumpHwc(std::string& result) const;
1175     LayersProto dumpProtoFromMainThread(uint32_t traceFlags = SurfaceTracing::TRACE_ALL)
1176             EXCLUDES(mStateLock);
1177     void dumpOffscreenLayers(std::string& result) EXCLUDES(mStateLock);
1178     void dumpPlannerInfo(const DumpArgs& args, std::string& result) const REQUIRES(mStateLock);
1179 
1180     status_t doDump(int fd, const DumpArgs& args, bool asProto);
1181 
1182     status_t dumpCritical(int fd, const DumpArgs&, bool asProto);
1183 
dumpAll(int fd,const DumpArgs & args,bool asProto)1184     status_t dumpAll(int fd, const DumpArgs& args, bool asProto) override {
1185         return doDump(fd, args, asProto);
1186     }
1187 
1188     void onFrameRateFlexibilityTokenReleased();
1189 
1190     static mat4 calculateColorMatrix(float saturation);
1191 
1192     void updateColorMatrixLocked();
1193 
1194     // Verify that transaction is being called by an approved process:
1195     // either AID_GRAPHICS or AID_SYSTEM.
1196     status_t CheckTransactCodeCredentials(uint32_t code);
1197 
1198     // Add transaction to the Transaction Queue
1199     void queueTransaction(TransactionState& state) EXCLUDES(mQueueLock);
1200     void waitForSynchronousTransaction(const CountDownLatch& transactionCommittedSignal);
1201     void signalSynchronousTransactions(const uint32_t flag);
1202 
1203     /*
1204      * Generic Layer Metadata
1205      */
1206     const std::unordered_map<std::string, uint32_t>& getGenericLayerMetadataKeyMap() const;
1207 
1208     /*
1209      * Misc
1210      */
1211     std::vector<ui::ColorMode> getDisplayColorModes(PhysicalDisplayId displayId)
1212             REQUIRES(mStateLock);
1213 
1214     static int calculateMaxAcquiredBufferCount(Fps refreshRate,
1215                                                std::chrono::nanoseconds presentLatency);
1216     int getMaxAcquiredBufferCountForRefreshRate(Fps refreshRate) const;
1217 
1218     void updateInternalDisplayVsyncLocked(const sp<DisplayDevice>& activeDisplay)
1219             REQUIRES(mStateLock);
1220 
1221     sp<StartPropertySetThread> mStartPropertySetThread;
1222     surfaceflinger::Factory& mFactory;
1223 
1224     std::future<void> mRenderEnginePrimeCacheFuture;
1225 
1226     // access must be protected by mStateLock
1227     mutable Mutex mStateLock;
1228     State mCurrentState{LayerVector::StateSet::Current};
1229     std::atomic<int32_t> mTransactionFlags = 0;
1230     std::vector<std::shared_ptr<CountDownLatch>> mTransactionCommittedSignals;
1231     bool mAnimTransactionPending = false;
1232     SortedVector<sp<Layer>> mLayersPendingRemoval;
1233     bool mForceTraversal = false;
1234 
1235     // global color transform states
1236     Daltonizer mDaltonizer;
1237     float mGlobalSaturationFactor = 1.0f;
1238     mat4 mClientColorMatrix;
1239 
1240     // Can't be unordered_set because wp<> isn't hashable
1241     std::set<wp<IBinder>> mGraphicBufferProducerList;
1242     size_t mMaxGraphicBufferProducerListSize = ISurfaceComposer::MAX_LAYERS;
1243     // If there are more GraphicBufferProducers tracked by SurfaceFlinger than
1244     // this threshold, then begin logging.
1245     size_t mGraphicBufferProducerListSizeLogThreshold =
1246             static_cast<size_t>(0.95 * static_cast<double>(MAX_LAYERS));
1247 
1248     void removeGraphicBufferProducerAsync(const wp<IBinder>&);
1249 
1250     // protected by mStateLock (but we could use another lock)
1251     bool mLayersRemoved = false;
1252     bool mLayersAdded = false;
1253 
1254     std::atomic<bool> mRepaintEverything = false;
1255 
1256     // constant members (no synchronization needed for access)
1257     const nsecs_t mBootTime = systemTime();
1258     bool mGpuToCpuSupported = false;
1259     bool mIsUserBuild = true;
1260 
1261     // Can only accessed from the main thread, these members
1262     // don't need synchronization
1263     State mDrawingState{LayerVector::StateSet::Drawing};
1264     bool mVisibleRegionsDirty = false;
1265 
1266     // VisibleRegions dirty is already cleared by postComp, but we need to track it to prevent
1267     // extra work in the HDR layer info listener.
1268     bool mVisibleRegionsWereDirtyThisFrame = false;
1269     // Used to ensure we omit a callback when HDR layer info listener is newly added but the
1270     // scene hasn't changed
1271     bool mAddingHDRLayerInfoListener = false;
1272 
1273     // Set during transaction application stage to track if the input info or children
1274     // for a layer has changed.
1275     // TODO: Also move visibleRegions over to a boolean system.
1276     bool mInputInfoChanged = false;
1277     bool mSomeChildrenChanged;
1278     bool mSomeDataspaceChanged = false;
1279     bool mForceTransactionDisplayChange = false;
1280 
1281     bool mGeometryInvalid = false;
1282     bool mAnimCompositionPending = false;
1283 
1284     // Tracks layers that have pending frames which are candidates for being
1285     // latched.
1286     std::unordered_set<sp<Layer>, ISurfaceComposer::SpHash<Layer>> mLayersWithQueuedFrames;
1287     // Tracks layers that need to update a display's dirty region.
1288     std::vector<sp<Layer>> mLayersPendingRefresh;
1289     std::array<FenceWithFenceTime, 2> mPreviousPresentFences;
1290     // True if in the previous frame at least one layer was composed via the GPU.
1291     bool mHadClientComposition = false;
1292     // True if in the previous frame at least one layer was composed via HW Composer.
1293     // Note that it is possible for a frame to be composed via both client and device
1294     // composition, for example in the case of overlays.
1295     bool mHadDeviceComposition = false;
1296     // True if in the previous frame, the client composition was skipped by reusing the buffer
1297     // used in a previous composition. This can happed if the client composition requests
1298     // did not change.
1299     bool mReusedClientComposition = false;
1300 
1301     BootStage mBootStage = BootStage::BOOTLOADER;
1302 
1303     std::vector<HotplugEvent> mPendingHotplugEvents GUARDED_BY(mStateLock);
1304 
1305     // this may only be written from the main thread with mStateLock held
1306     // it may be read from other threads with mStateLock held
1307     std::map<wp<IBinder>, sp<DisplayDevice>> mDisplays GUARDED_BY(mStateLock);
1308     std::unordered_map<PhysicalDisplayId, sp<IBinder>> mPhysicalDisplayTokens
1309             GUARDED_BY(mStateLock);
1310 
1311     struct {
1312         DisplayIdGenerator<GpuVirtualDisplayId> gpu;
1313         std::optional<DisplayIdGenerator<HalVirtualDisplayId>> hal;
1314     } mVirtualDisplayIdGenerators;
1315 
1316     // don't use a lock for these, we don't care
1317     int mDebugRegion = 0;
1318     bool mDebugDisableHWC = false;
1319     bool mDebugDisableTransformHint = false;
1320     bool mLayerCachingEnabled = false;
1321     volatile nsecs_t mDebugInTransaction = 0;
1322     bool mForceFullDamage = false;
1323     bool mPropagateBackpressureClientComposition = false;
1324     sp<SurfaceInterceptor> mInterceptor;
1325 
1326     SurfaceTracing mTracing{*this};
1327     std::mutex mTracingLock;
1328     bool mTracingEnabled = false;
1329     bool mTracePostComposition = false;
1330     std::atomic<bool> mTracingEnabledChanged = false;
1331 
1332     const std::shared_ptr<TimeStats> mTimeStats;
1333     const std::unique_ptr<FrameTracer> mFrameTracer;
1334     const std::unique_ptr<frametimeline::FrameTimeline> mFrameTimeline;
1335 
1336     // If blurs should be enabled on this device.
1337     bool mSupportsBlur = false;
1338     // If blurs are considered expensive and should require high GPU frequency.
1339     bool mBlursAreExpensive = false;
1340     std::atomic<uint32_t> mFrameMissedCount = 0;
1341     std::atomic<uint32_t> mHwcFrameMissedCount = 0;
1342     std::atomic<uint32_t> mGpuFrameMissedCount = 0;
1343 
1344     TransactionCallbackInvoker mTransactionCallbackInvoker;
1345 
1346     // these are thread safe
1347     std::unique_ptr<MessageQueue> mEventQueue;
1348     FrameTracker mAnimFrameTracker;
1349 
1350     // protected by mDestroyedLayerLock;
1351     mutable Mutex mDestroyedLayerLock;
1352     Vector<Layer const *> mDestroyedLayers;
1353 
1354     nsecs_t mRefreshStartTime = 0;
1355 
1356     std::atomic<bool> mRefreshPending = false;
1357 
1358     // We maintain a pool of pre-generated texture names to hand out to avoid
1359     // layer creation needing to run on the main thread (which it would
1360     // otherwise need to do to access RenderEngine).
1361     std::mutex mTexturePoolMutex;
1362     uint32_t mTexturePoolSize = 0;
1363     std::vector<uint32_t> mTexturePool;
1364 
1365     mutable Mutex mQueueLock;
1366     Condition mTransactionQueueCV;
1367     std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash>
1368             mPendingTransactionQueues GUARDED_BY(mQueueLock);
1369     std::queue<TransactionState> mTransactionQueue GUARDED_BY(mQueueLock);
1370     /*
1371      * Feature prototyping
1372      */
1373 
1374     // Static screen stats
1375     bool mHasPoweredOff = false;
1376 
1377     std::atomic<size_t> mNumLayers = 0;
1378 
1379     // to linkToDeath
1380     sp<IBinder> mWindowManager;
1381     // We want to avoid multiple calls to BOOT_FINISHED as they come in on
1382     // different threads without a lock and could trigger unsynchronized writes to
1383     // to mWindowManager or mInputFlinger
1384     std::atomic<bool> mBootFinished = false;
1385 
1386     std::thread::id mMainThreadId = std::this_thread::get_id();
1387 
1388     DisplayColorSetting mDisplayColorSetting = DisplayColorSetting::kEnhanced;
1389 
1390     // Color mode forced by setting persist.sys.sf.color_mode, it must:
1391     //     1. not be NATIVE color mode, NATIVE color mode means no forced color mode;
1392     //     2. be one of the supported color modes returned by hardware composer, otherwise
1393     //        it will not be respected.
1394     // persist.sys.sf.color_mode will only take effect when persist.sys.sf.native_mode
1395     // is not set to 1.
1396     // This property can be used to force SurfaceFlinger to always pick a certain color mode.
1397     ui::ColorMode mForceColorMode = ui::ColorMode::NATIVE;
1398 
1399     ui::Dataspace mDefaultCompositionDataspace;
1400     ui::Dataspace mWideColorGamutCompositionDataspace;
1401     ui::Dataspace mColorSpaceAgnosticDataspace;
1402 
1403     SurfaceFlingerBE mBE;
1404     std::unique_ptr<compositionengine::CompositionEngine> mCompositionEngine;
1405     // mMaxRenderTargetSize is only set once in init() so it doesn't need to be protected by
1406     // any mutex.
1407     size_t mMaxRenderTargetSize{1};
1408 
1409     const std::string mHwcServiceName;
1410 
hasMockHwc()1411     bool hasMockHwc() const { return mHwcServiceName == "mock"; }
1412 
1413     /*
1414      * Scheduler
1415      */
1416     std::unique_ptr<Scheduler> mScheduler;
1417     scheduler::ConnectionHandle mAppConnectionHandle;
1418     scheduler::ConnectionHandle mSfConnectionHandle;
1419 
1420     // Stores phase offsets configured per refresh rate.
1421     std::unique_ptr<scheduler::VsyncConfiguration> mVsyncConfiguration;
1422 
1423     // Optional to defer construction until PhaseConfiguration is created.
1424     sp<VsyncModulator> mVsyncModulator;
1425 
1426     std::unique_ptr<scheduler::RefreshRateStats> mRefreshRateStats;
1427 
1428     std::atomic<nsecs_t> mExpectedPresentTime = 0;
1429     nsecs_t mScheduledPresentTime = 0;
1430     hal::Vsync mHWCVsyncPendingState = hal::Vsync::DISABLE;
1431     hal::Vsync mLastHWCVsyncState = hal::Vsync::DISABLE;
1432 
1433     // below flags are set by main thread only
1434     bool mSetActiveModePending = false;
1435 
1436     bool mLumaSampling = true;
1437     sp<RegionSamplingThread> mRegionSamplingThread;
1438     sp<FpsReporter> mFpsReporter;
1439     sp<TunnelModeEnabledReporter> mTunnelModeEnabledReporter;
1440     ui::DisplayPrimaries mInternalDisplayPrimaries;
1441 
1442     const float mInternalDisplayDensity;
1443     const float mEmulatedDisplayDensity;
1444 
1445     sp<os::IInputFlinger> mInputFlinger;
1446     // Should only be accessed by the main thread.
1447     InputWindowCommands mInputWindowCommands;
1448 
1449     Hwc2::impl::PowerAdvisor mPowerAdvisor;
1450 
1451     // This should only be accessed on the main thread.
1452     nsecs_t mFrameStartTime = 0;
1453 
1454     void enableRefreshRateOverlay(bool enable) REQUIRES(mStateLock);
1455 
1456     // Flag used to set override desired display mode from backdoor
1457     bool mDebugDisplayModeSetByBackdoor = false;
1458 
1459     // A set of layers that have no parent so they are not drawn on screen.
1460     // Should only be accessed by the main thread.
1461     // The Layer pointer is removed from the set when the destructor is called so there shouldn't
1462     // be any issues with a raw pointer referencing an invalid object.
1463     std::unordered_set<Layer*> mOffscreenLayers;
1464 
1465     int mFrameRateFlexibilityTokenCount = 0;
1466 
1467     sp<IBinder> mDebugFrameRateFlexibilityToken;
1468 
1469     BufferCountTracker mBufferCountTracker;
1470 
1471     std::unordered_map<DisplayId, sp<HdrLayerInfoReporter>> mHdrLayerInfoListeners
1472             GUARDED_BY(mStateLock);
1473     mutable Mutex mCreatedLayersLock;
1474     struct LayerCreatedState {
LayerCreatedStateLayerCreatedState1475         LayerCreatedState(const wp<Layer>& layer, const wp<IBinder>& parent,
1476                           const wp<Layer> parentLayer, const wp<IBinder>& producer, bool addToRoot)
1477               : layer(layer),
1478                 initialParent(parent),
1479                 initialParentLayer(parentLayer),
1480                 initialProducer(producer),
1481                 addToRoot(addToRoot) {}
1482         wp<Layer> layer;
1483         // Indicates the initial parent of the created layer, only used for creating layer in
1484         // SurfaceFlinger. If nullptr, it may add the created layer into the current root layers.
1485         wp<IBinder> initialParent;
1486         wp<Layer> initialParentLayer;
1487         // Indicates the initial graphic buffer producer of the created layer, only used for
1488         // creating layer in SurfaceFlinger.
1489         wp<IBinder> initialProducer;
1490         // Indicates whether the layer getting created should be added at root if there's no parent
1491         // and has permission ACCESS_SURFACE_FLINGER. If set to false and no parent, the layer will
1492         // be added offscreen.
1493         bool addToRoot;
1494     };
1495 
1496     // A temporay pool that store the created layers and will be added to current state in main
1497     // thread.
1498     std::unordered_map<BBinder*, std::unique_ptr<LayerCreatedState>> mCreatedLayers;
1499     void setLayerCreatedState(const sp<IBinder>& handle, const wp<Layer>& layer,
1500                               const wp<IBinder>& parent, const wp<Layer> parentLayer,
1501                               const wp<IBinder>& producer, bool addToRoot);
1502     auto getLayerCreatedState(const sp<IBinder>& handle);
1503     sp<Layer> handleLayerCreatedLocked(const sp<IBinder>& handle) REQUIRES(mStateLock);
1504 
1505     std::atomic<ui::Transform::RotationFlags> mActiveDisplayTransformHint;
1506 
1507     void scheduleRegionSamplingThread();
1508     void notifyRegionSamplingThread();
1509 
isRefreshRateOverlayEnabled()1510     bool isRefreshRateOverlayEnabled() const REQUIRES(mStateLock) {
1511         return std::any_of(mDisplays.begin(), mDisplays.end(),
1512                            [](std::pair<wp<IBinder>, sp<DisplayDevice>> display) {
1513                                return display.second->isRefreshRateOverlayEnabled();
1514                            });
1515     }
1516 
1517     wp<IBinder> mActiveDisplayToken GUARDED_BY(mStateLock);
1518 
1519     const sp<WindowInfosListenerInvoker> mWindowInfosListenerInvoker;
1520 };
1521 
1522 } // namespace android
1523