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