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 <input/ISetInputWindowsListener.h> 37 #include <layerproto/LayerProtoHeader.h> 38 #include <math/mat4.h> 39 #include <renderengine/LayerSettings.h> 40 #include <serviceutils/PriorityDumper.h> 41 #include <system/graphics.h> 42 #include <ui/FenceTime.h> 43 #include <ui/PixelFormat.h> 44 #include <ui/Size.h> 45 #include <utils/Errors.h> 46 #include <utils/KeyedVector.h> 47 #include <utils/RefBase.h> 48 #include <utils/SortedVector.h> 49 #include <utils/Trace.h> 50 #include <utils/threads.h> 51 52 #include "ClientCache.h" 53 #include "DisplayDevice.h" 54 #include "DisplayHardware/HWC2.h" 55 #include "DisplayHardware/PowerAdvisor.h" 56 #include "Effects/Daltonizer.h" 57 #include "FrameTracker.h" 58 #include "LayerVector.h" 59 #include "Scheduler/RefreshRateConfigs.h" 60 #include "Scheduler/RefreshRateStats.h" 61 #include "Scheduler/Scheduler.h" 62 #include "Scheduler/VSyncModulator.h" 63 #include "SurfaceFlingerFactory.h" 64 #include "SurfaceTracing.h" 65 #include "TracedOrdinal.h" 66 #include "TransactionCompletedThread.h" 67 68 #include <atomic> 69 #include <cstdint> 70 #include <functional> 71 #include <future> 72 #include <map> 73 #include <memory> 74 #include <mutex> 75 #include <optional> 76 #include <queue> 77 #include <set> 78 #include <string> 79 #include <thread> 80 #include <type_traits> 81 #include <unordered_map> 82 #include <unordered_set> 83 #include <utility> 84 85 using namespace android::surfaceflinger; 86 87 namespace android { 88 89 class Client; 90 class EventThread; 91 class HWComposer; 92 class IGraphicBufferProducer; 93 class IInputFlinger; 94 class Layer; 95 class MessageBase; 96 class RefreshRateOverlay; 97 class RegionSamplingThread; 98 class TimeStats; 99 class FrameTracer; 100 101 namespace compositionengine { 102 class DisplaySurface; 103 class OutputLayer; 104 105 struct CompositionRefreshArgs; 106 } // namespace compositionengine 107 108 namespace renderengine { 109 class RenderEngine; 110 } // namespace renderengine 111 112 namespace dvr { 113 class VrFlinger; 114 } // namespace dvr 115 116 enum { 117 eTransactionNeeded = 0x01, 118 eTraversalNeeded = 0x02, 119 eDisplayTransactionNeeded = 0x04, 120 eTransformHintUpdateNeeded = 0x08, 121 eTransactionFlushNeeded = 0x10, 122 eTransactionMask = 0x1f, 123 }; 124 125 using DisplayColorSetting = compositionengine::OutputColorSetting; 126 127 class SurfaceFlingerBE 128 { 129 public: 130 SurfaceFlingerBE(); 131 132 const std::string mHwcServiceName; // "default" for real use, something else for testing. 133 134 FenceTimeline mGlCompositionDoneTimeline; 135 FenceTimeline mDisplayTimeline; 136 137 // protected by mCompositorTimingLock; 138 mutable std::mutex mCompositorTimingLock; 139 CompositorTiming mCompositorTiming; 140 141 // Only accessed from the main thread. 142 struct CompositePresentTime { 143 nsecs_t composite = -1; 144 std::shared_ptr<FenceTime> display = FenceTime::NO_FENCE; 145 }; 146 std::queue<CompositePresentTime> mCompositePresentTimes; 147 148 static const size_t NUM_BUCKETS = 8; // < 1-7, 7+ 149 nsecs_t mFrameBuckets[NUM_BUCKETS] = {}; 150 nsecs_t mTotalTime = 0; 151 std::atomic<nsecs_t> mLastSwapTime = 0; 152 153 // Double- vs. triple-buffering stats 154 struct BufferingStats { 155 size_t numSegments = 0; 156 nsecs_t totalTime = 0; 157 158 // "Two buffer" means that a third buffer was never used, whereas 159 // "double-buffered" means that on average the segment only used two 160 // buffers (though it may have used a third for some part of the 161 // segment) 162 nsecs_t twoBufferTime = 0; 163 nsecs_t doubleBufferedTime = 0; 164 nsecs_t tripleBufferedTime = 0; 165 }; 166 mutable Mutex mBufferingStatsMutex; 167 std::unordered_map<std::string, BufferingStats> mBufferingStats; 168 169 // The composer sequence id is a monotonically increasing integer that we 170 // use to differentiate callbacks from different hardware composer 171 // instances. Each hardware composer instance gets a different sequence id. 172 int32_t mComposerSequenceId = 0; 173 }; 174 175 class SurfaceFlinger : public BnSurfaceComposer, 176 public PriorityDumper, 177 public ClientCache::ErasedRecipient, 178 private IBinder::DeathRecipient, 179 private HWC2::ComposerCallback, 180 private ISchedulerCallback { 181 public: getBE()182 SurfaceFlingerBE& getBE() { return mBE; } getBE()183 const SurfaceFlingerBE& getBE() const { return mBE; } 184 185 // This is the phase offset in nanoseconds of the software vsync event 186 // relative to the vsync event reported by HWComposer. The software vsync 187 // event is when SurfaceFlinger and Choreographer-based applications run each 188 // frame. 189 // 190 // This phase offset allows adjustment of the minimum latency from application 191 // wake-up time (by Choreographer) to the time at which the resulting window 192 // image is displayed. This value may be either positive (after the HW vsync) 193 // or negative (before the HW vsync). Setting it to 0 will result in a lower 194 // latency bound of two vsync periods because the app and SurfaceFlinger 195 // will run just after the HW vsync. Setting it to a positive number will 196 // result in the minimum latency being: 197 // 198 // (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD)) 199 // 200 // Note that reducing this latency makes it more likely for the applications 201 // to not have their window content image ready in time. When this happens 202 // the latency will end up being an additional vsync period, and animations 203 // will hiccup. Therefore, this latency should be tuned somewhat 204 // conservatively (or at least with awareness of the trade-off being made). 205 static int64_t vsyncPhaseOffsetNs; 206 static int64_t sfVsyncPhaseOffsetNs; 207 208 // If fences from sync Framework are supported. 209 static bool hasSyncFramework; 210 211 // The offset in nanoseconds to use when DispSync timestamps present fence 212 // signaling time. 213 static int64_t dispSyncPresentTimeOffset; 214 215 // Some hardware can do RGB->YUV conversion more efficiently in hardware 216 // controlled by HWC than in hardware controlled by the video encoder. 217 // This instruct VirtualDisplaySurface to use HWC for such conversion on 218 // GL composition. 219 static bool useHwcForRgbToYuv; 220 221 // Maximum dimension supported by HWC for virtual display. 222 // Equal to min(max_height, max_width). 223 static uint64_t maxVirtualDisplaySize; 224 225 // Controls the number of buffers SurfaceFlinger will allocate for use in 226 // FramebufferSurface 227 static int64_t maxFrameBufferAcquiredBuffers; 228 229 // Controls the maximum width and height in pixels that the graphics pipeline can support for 230 // GPU fallback composition. For example, 8k devices with 4k GPUs, or 4k devices with 2k GPUs. 231 static uint32_t maxGraphicsWidth; 232 static uint32_t maxGraphicsHeight; 233 234 // Indicate if a device has wide color gamut display. This is typically 235 // found on devices with wide color gamut (e.g. Display-P3) display. 236 static bool hasWideColorDisplay; 237 238 static ui::Rotation internalDisplayOrientation; 239 240 // Indicate if device wants color management on its display. 241 static bool useColorManagement; 242 243 static bool useContextPriority; 244 245 // The data space and pixel format that SurfaceFlinger expects hardware composer 246 // to composite efficiently. Meaning under most scenarios, hardware composer 247 // will accept layers with the data space and pixel format. 248 static ui::Dataspace defaultCompositionDataspace; 249 static ui::PixelFormat defaultCompositionPixelFormat; 250 251 // The data space and pixel format that SurfaceFlinger expects hardware composer 252 // to composite efficiently for wide color gamut surfaces. Meaning under most scenarios, 253 // hardware composer will accept layers with the data space and pixel format. 254 static ui::Dataspace wideColorGamutCompositionDataspace; 255 static ui::PixelFormat wideColorGamutCompositionPixelFormat; 256 257 // Whether to use frame rate API when deciding about the refresh rate of the display. This 258 // variable is caches in SF, so that we can check it with each layer creation, and a void the 259 // overhead that is caused by reading from sysprop. 260 static bool useFrameRateApi; 261 262 // set main thread scheduling policy 263 static status_t setSchedFifo(bool enabled) ANDROID_API; 264 getServiceName()265 static char const* getServiceName() ANDROID_API { 266 return "SurfaceFlinger"; 267 } 268 269 struct SkipInitializationTag {}; 270 static constexpr SkipInitializationTag SkipInitialization; 271 SurfaceFlinger(surfaceflinger::Factory&, SkipInitializationTag) ANDROID_API; 272 explicit SurfaceFlinger(surfaceflinger::Factory&) ANDROID_API; 273 274 // must be called before clients can connect 275 void init() ANDROID_API; 276 277 // starts SurfaceFlinger main loop in the current thread 278 void run() ANDROID_API; 279 280 // Schedule an asynchronous or synchronous task on the main thread. 281 template <typename F, typename T = std::invoke_result_t<F>> 282 [[nodiscard]] std::future<T> schedule(F&&); 283 284 // force full composition on all displays 285 void repaintEverything(); 286 getFactory()287 surfaceflinger::Factory& getFactory() { return mFactory; } 288 289 // The CompositionEngine encapsulates all composition related interfaces and actions. 290 compositionengine::CompositionEngine& getCompositionEngine() const; 291 292 // Obtains a name from the texture pool, or, if the pool is empty, posts a 293 // synchronous message to the main thread to obtain one on the fly 294 uint32_t getNewTexture(); 295 296 // utility function to delete a texture on the main thread 297 void deleteTextureAsync(uint32_t texture); 298 299 // enable/disable h/w composer event 300 // TODO: this should be made accessible only to EventThread 301 void setPrimaryVsyncEnabled(bool enabled); 302 303 // main thread function to enable/disable h/w composer event 304 void setPrimaryVsyncEnabledInternal(bool enabled) REQUIRES(mStateLock); 305 306 // called on the main thread by MessageQueue when an internal message 307 // is received 308 // TODO: this should be made accessible only to MessageQueue 309 void onMessageReceived(int32_t what, nsecs_t expectedVSyncTime); 310 311 renderengine::RenderEngine& getRenderEngine() const; 312 313 bool authenticateSurfaceTextureLocked( 314 const sp<IGraphicBufferProducer>& bufferProducer) const; 315 316 void onLayerFirstRef(Layer*); 317 void onLayerDestroyed(Layer*); 318 319 void removeFromOffscreenLayers(Layer* layer); 320 getTransactionCompletedThread()321 TransactionCompletedThread& getTransactionCompletedThread() { 322 return mTransactionCompletedThread; 323 } 324 325 // Converts from a binder handle to a Layer 326 // Returns nullptr if the handle does not point to an existing layer. 327 // Otherwise, returns a weak reference so that callers off the main-thread 328 // won't accidentally hold onto the last strong reference. 329 wp<Layer> fromHandle(const sp<IBinder>& handle); 330 wp<Layer> fromHandleLocked(const sp<IBinder>& handle) REQUIRES(mStateLock); 331 332 // Inherit from ClientCache::ErasedRecipient 333 void bufferErased(const client_cache_t& clientCacheId) override; 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 339 private: 340 friend class BufferLayer; 341 friend class BufferQueueLayer; 342 friend class BufferStateLayer; 343 friend class Client; 344 friend class Layer; 345 friend class MonitoredProducer; 346 friend class RefreshRateOverlay; 347 friend class RegionSamplingThread; 348 friend class SurfaceTracing; 349 350 // For unit tests 351 friend class TestableSurfaceFlinger; 352 friend class TransactionApplicationTest; 353 354 // This value is specified in number of frames. Log frame stats at most 355 // every half hour. 356 enum { LOG_FRAME_STATS_PERIOD = 30*60*60 }; 357 358 static const int MAX_TRACING_MEMORY = 100 * 1024 * 1024; // 100MB 359 360 protected: 361 // We're reference counted, never destroy SurfaceFlinger directly 362 virtual ~SurfaceFlinger(); 363 364 private: 365 /* ------------------------------------------------------------------------ 366 * Internal data structures 367 */ 368 369 class State { 370 public: State(LayerVector::StateSet set)371 explicit State(LayerVector::StateSet set) : stateSet(set), layersSortedByZ(set) {} 372 State& operator=(const State& other) { 373 // We explicitly don't copy stateSet so that, e.g., mDrawingState 374 // always uses the Drawing StateSet. 375 layersSortedByZ = other.layersSortedByZ; 376 displays = other.displays; 377 colorMatrixChanged = other.colorMatrixChanged; 378 if (colorMatrixChanged) { 379 colorMatrix = other.colorMatrix; 380 } 381 globalShadowSettings = other.globalShadowSettings; 382 383 return *this; 384 } 385 386 const LayerVector::StateSet stateSet = LayerVector::StateSet::Invalid; 387 LayerVector layersSortedByZ; 388 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 389 390 bool colorMatrixChanged = true; 391 mat4 colorMatrix; 392 393 renderengine::ShadowSettings globalShadowSettings; 394 395 void traverse(const LayerVector::Visitor& visitor) const; 396 void traverseInZOrder(const LayerVector::Visitor& visitor) const; 397 void traverseInReverseZOrder(const LayerVector::Visitor& visitor) const; 398 }; 399 400 /* ------------------------------------------------------------------------ 401 * IBinder interface 402 */ 403 status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) override; dump(int fd,const Vector<String16> & args)404 status_t dump(int fd, const Vector<String16>& args) override { return priorityDump(fd, args); } 405 bool callingThreadHasUnscopedSurfaceFlingerAccess(bool usePermissionCache = true) 406 EXCLUDES(mStateLock); 407 408 /* ------------------------------------------------------------------------ 409 * ISurfaceComposer interface 410 */ 411 sp<ISurfaceComposerClient> createConnection() override; 412 sp<IBinder> createDisplay(const String8& displayName, bool secure) override; 413 void destroyDisplay(const sp<IBinder>& displayToken) override; 414 std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const override; 415 sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId) const override; 416 void setTransactionState(const Vector<ComposerState>& state, 417 const Vector<DisplayState>& displays, uint32_t flags, 418 const sp<IBinder>& applyToken, 419 const InputWindowCommands& inputWindowCommands, 420 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer, 421 bool hasListenerCallbacks, 422 const std::vector<ListenerCallbacks>& listenerCallbacks) override; 423 void bootFinished() override; 424 bool authenticateSurfaceTexture( 425 const sp<IGraphicBufferProducer>& bufferProducer) const override; 426 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported) const override; 427 sp<IDisplayEventConnection> createDisplayEventConnection( 428 ISurfaceComposer::VsyncSource vsyncSource = eVsyncSourceApp, 429 ISurfaceComposer::ConfigChanged configChanged = 430 ISurfaceComposer::eConfigChangedSuppress) override; 431 status_t captureScreen(const sp<IBinder>& displayToken, sp<GraphicBuffer>* outBuffer, 432 bool& outCapturedSecureLayers, ui::Dataspace reqDataspace, 433 ui::PixelFormat reqPixelFormat, const Rect& sourceCrop, 434 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform, 435 ui::Rotation rotation, bool captureSecureLayers) override; 436 status_t captureScreen(uint64_t displayOrLayerStack, ui::Dataspace* outDataspace, 437 sp<GraphicBuffer>* outBuffer) override; 438 status_t captureLayers( 439 const sp<IBinder>& parentHandle, sp<GraphicBuffer>* outBuffer, 440 const ui::Dataspace reqDataspace, const ui::PixelFormat reqPixelFormat, 441 const Rect& sourceCrop, 442 const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>& exclude, 443 float frameScale, bool childrenOnly) override; 444 445 status_t getDisplayStats(const sp<IBinder>& displayToken, DisplayStatInfo* stats) override; 446 status_t getDisplayState(const sp<IBinder>& displayToken, ui::DisplayState*) override; 447 status_t getDisplayInfo(const sp<IBinder>& displayToken, DisplayInfo*) override; 448 status_t getDisplayConfigs(const sp<IBinder>& displayToken, Vector<DisplayConfig>*) override; 449 int getActiveConfig(const sp<IBinder>& displayToken) override; 450 status_t getDisplayColorModes(const sp<IBinder>& displayToken, Vector<ui::ColorMode>*) override; 451 status_t getDisplayNativePrimaries(const sp<IBinder>& displayToken, 452 ui::DisplayPrimaries&) override; 453 ui::ColorMode getActiveColorMode(const sp<IBinder>& displayToken) override; 454 status_t setActiveColorMode(const sp<IBinder>& displayToken, ui::ColorMode colorMode) override; 455 status_t getAutoLowLatencyModeSupport(const sp<IBinder>& displayToken, 456 bool* outSupported) const override; 457 void setAutoLowLatencyMode(const sp<IBinder>& displayToken, bool on) override; 458 status_t getGameContentTypeSupport(const sp<IBinder>& displayToken, 459 bool* outSupported) const override; 460 void setGameContentType(const sp<IBinder>& displayToken, bool on) override; 461 void setPowerMode(const sp<IBinder>& displayToken, int mode) override; 462 status_t clearAnimationFrameStats() override; 463 status_t getAnimationFrameStats(FrameStats* outStats) const override; 464 status_t getHdrCapabilities(const sp<IBinder>& displayToken, 465 HdrCapabilities* outCapabilities) const override; 466 status_t enableVSyncInjections(bool enable) override; 467 status_t injectVSync(nsecs_t when) override; 468 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* outLayers) override; 469 status_t getColorManagement(bool* outGetColorManagement) const override; 470 status_t getCompositionPreference(ui::Dataspace* outDataspace, ui::PixelFormat* outPixelFormat, 471 ui::Dataspace* outWideColorGamutDataspace, 472 ui::PixelFormat* outWideColorGamutPixelFormat) const override; 473 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& displayToken, 474 ui::PixelFormat* outFormat, 475 ui::Dataspace* outDataspace, 476 uint8_t* outComponentMask) const override; 477 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& displayToken, bool enable, 478 uint8_t componentMask, uint64_t maxFrames) override; 479 status_t getDisplayedContentSample(const sp<IBinder>& displayToken, uint64_t maxFrames, 480 uint64_t timestamp, 481 DisplayedFrameStats* outStats) const override; 482 status_t getProtectedContentSupport(bool* outSupported) const override; 483 status_t isWideColorDisplay(const sp<IBinder>& displayToken, 484 bool* outIsWideColorDisplay) const override; 485 status_t addRegionSamplingListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle, 486 const sp<IRegionSamplingListener>& listener) override; 487 status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener) override; 488 status_t setDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken, int32_t displayModeId, 489 float primaryRefreshRateMin, float primaryRefreshRateMax, 490 float appRequestRefreshRateMin, 491 float appRequestRefreshRateMax) override; 492 status_t getDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken, 493 int32_t* outDefaultConfig, 494 float* outPrimaryRefreshRateMin, 495 float* outPrimaryRefreshRateMax, 496 float* outAppRequestRefreshRateMin, 497 float* outAppRequestRefreshRateMax) override; 498 status_t getDisplayBrightnessSupport(const sp<IBinder>& displayToken, 499 bool* outSupport) const override; 500 status_t setDisplayBrightness(const sp<IBinder>& displayToken, float brightness) override; 501 status_t notifyPowerHint(int32_t hintId) override; 502 status_t setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor, 503 float lightPosY, float lightPosZ, float lightRadius) override; 504 status_t setFrameRate(const sp<IGraphicBufferProducer>& surface, float frameRate, 505 int8_t compatibility) override; 506 status_t acquireFrameRateFlexibilityToken(sp<IBinder>* outToken) override; 507 /* ------------------------------------------------------------------------ 508 * DeathRecipient interface 509 */ 510 void binderDied(const wp<IBinder>& who) override; 511 512 /* ------------------------------------------------------------------------ 513 * RefBase interface 514 */ 515 void onFirstRef() override; 516 517 /* ------------------------------------------------------------------------ 518 * HWC2::ComposerCallback / HWComposer::EventHandler interface 519 */ 520 void onVsyncReceived(int32_t sequenceId, hal::HWDisplayId hwcDisplayId, int64_t timestamp, 521 std::optional<hal::VsyncPeriodNanos> vsyncPeriod) override; 522 void onHotplugReceived(int32_t sequenceId, hal::HWDisplayId hwcDisplayId, 523 hal::Connection connection) override; 524 void onRefreshReceived(int32_t sequenceId, hal::HWDisplayId hwcDisplayId) override; 525 void onVsyncPeriodTimingChangedReceived( 526 int32_t sequenceId, hal::HWDisplayId display, 527 const hal::VsyncPeriodChangeTimeline& updatedTimeline) override; 528 void onSeamlessPossible(int32_t sequenceId, hal::HWDisplayId display) override; 529 530 /* ------------------------------------------------------------------------ 531 * ISchedulerCallback 532 */ 533 void changeRefreshRate(const Scheduler::RefreshRate&, Scheduler::ConfigEvent) override; 534 // force full composition on all displays without resetting the scheduler idle timer. 535 void repaintEverythingForHWC() override; 536 // Called when kernel idle timer has expired. Used to update the refresh rate overlay. 537 void kernelTimerChanged(bool expired) override; 538 // Toggles the kernel idle timer on or off depending the policy decisions around refresh rates. 539 void toggleKernelIdleTimer(); 540 // Keeps track of whether the kernel idle timer is currently enabled, so we don't have to 541 // make calls to sys prop each time. 542 bool mKernelIdleTimerEnabled = false; 543 // Keeps track of whether the kernel timer is supported on the SF side. 544 bool mSupportKernelIdleTimer = false; 545 /* ------------------------------------------------------------------------ 546 * Message handling 547 */ 548 // Can only be called from the main thread or with mStateLock held 549 void signalTransaction(); 550 // Can only be called from the main thread or with mStateLock held 551 void signalLayerUpdate(); 552 void signalRefresh(); 553 554 using RefreshRate = scheduler::RefreshRateConfigs::RefreshRate; 555 556 struct ActiveConfigInfo { 557 HwcConfigIndexType configId; 558 Scheduler::ConfigEvent event = Scheduler::ConfigEvent::None; 559 560 bool operator!=(const ActiveConfigInfo& other) const { 561 return configId != other.configId || event != other.event; 562 } 563 }; 564 565 // called on the main thread in response to initializeDisplays() 566 void onInitializeDisplays() REQUIRES(mStateLock); 567 // Sets the desired active config bit. It obtains the lock, and sets mDesiredActiveConfig. 568 void setDesiredActiveConfig(const ActiveConfigInfo& info) REQUIRES(mStateLock); 569 status_t setActiveConfig(const sp<IBinder>& displayToken, int id); 570 // Once HWC has returned the present fence, this sets the active config and a new refresh 571 // rate in SF. 572 void setActiveConfigInternal() REQUIRES(mStateLock); 573 // Calls to setActiveConfig on the main thread if there is a pending config 574 // that needs to be applied. 575 void performSetActiveConfig() REQUIRES(mStateLock); 576 // Called when active config is no longer is progress 577 void desiredActiveConfigChangeDone() REQUIRES(mStateLock); 578 // called on the main thread in response to setPowerMode() 579 void setPowerModeInternal(const sp<DisplayDevice>& display, hal::PowerMode mode) 580 REQUIRES(mStateLock); 581 582 // Sets the desired display configs. 583 status_t setDesiredDisplayConfigSpecsInternal( 584 const sp<DisplayDevice>& display, 585 const std::optional<scheduler::RefreshRateConfigs::Policy>& policy, bool overridePolicy) 586 EXCLUDES(mStateLock); 587 588 // Handle the INVALIDATE message queue event, latching new buffers and applying 589 // incoming transactions 590 void onMessageInvalidate(nsecs_t expectedVSyncTime); 591 592 // Returns whether the transaction actually modified any state 593 bool handleMessageTransaction(); 594 595 // Handle the REFRESH message queue event, sending the current frame down to RenderEngine and 596 // the Composer HAL for presentation 597 void onMessageRefresh(); 598 599 // Returns whether a new buffer has been latched (see handlePageFlip()) 600 bool handleMessageInvalidate(); 601 602 void handleTransaction(uint32_t transactionFlags); 603 void handleTransactionLocked(uint32_t transactionFlags) REQUIRES(mStateLock); 604 605 void updateInputFlinger(); 606 void updateInputWindowInfo(); 607 void commitInputWindowCommands() REQUIRES(mStateLock); 608 void setInputWindowsFinished(); 609 void updateCursorAsync(); 610 void initScheduler(DisplayId primaryDisplayId); 611 612 /* handlePageFlip - latch a new buffer if available and compute the dirty 613 * region. Returns whether a new buffer has been latched, i.e., whether it 614 * is necessary to perform a refresh during this vsync. 615 */ 616 bool handlePageFlip(); 617 618 /* ------------------------------------------------------------------------ 619 * Transactions 620 */ 621 void applyTransactionState(const Vector<ComposerState>& state, 622 const Vector<DisplayState>& displays, uint32_t flags, 623 const InputWindowCommands& inputWindowCommands, 624 const int64_t desiredPresentTime, 625 const client_cache_t& uncacheBuffer, const int64_t postTime, 626 bool privileged, bool hasListenerCallbacks, 627 const std::vector<ListenerCallbacks>& listenerCallbacks, 628 bool isMainThread = false) REQUIRES(mStateLock); 629 // Returns true if at least one transaction was flushed 630 bool flushTransactionQueues(); 631 // Returns true if there is at least one transaction that needs to be flushed 632 bool transactionFlushNeeded(); 633 uint32_t getTransactionFlags(uint32_t flags); 634 uint32_t peekTransactionFlags(); 635 // Can only be called from the main thread or with mStateLock held 636 uint32_t setTransactionFlags(uint32_t flags); 637 // Indicate SF should call doTraversal on layers, but don't trigger a wakeup! We use this cases 638 // where there are still pending transactions but we know they won't be ready until a frame 639 // arrives from a different layer. So we need to ensure we performTransaction from invalidate 640 // but there is no need to try and wake up immediately to do it. Rather we rely on 641 // onFrameAvailable or another layer update to wake us up. 642 void setTraversalNeeded(); 643 uint32_t setTransactionFlags(uint32_t flags, Scheduler::TransactionStart transactionStart); 644 void commitTransaction() REQUIRES(mStateLock); 645 void commitOffscreenLayers(); 646 bool transactionIsReadyToBeApplied(int64_t desiredPresentTime, 647 const Vector<ComposerState>& states); 648 uint32_t setDisplayStateLocked(const DisplayState& s) REQUIRES(mStateLock); 649 uint32_t addInputWindowCommands(const InputWindowCommands& inputWindowCommands) 650 REQUIRES(mStateLock); 651 652 protected: 653 virtual uint32_t setClientStateLocked( 654 const ComposerState& composerState, int64_t desiredPresentTime, int64_t postTime, 655 bool privileged, 656 std::unordered_set<ListenerCallbacks, ListenerCallbacksHash>& listenerCallbacks) 657 REQUIRES(mStateLock); 658 virtual void commitTransactionLocked(); 659 660 // Used internally by computeLayerBounds() to gets the clip rectangle to use for the 661 // root layers on a particular display in layer-coordinate space. The 662 // layers (and effectively their children) will be clipped against this 663 // rectangle. The base behavior is to clip to the visible region of the 664 // display. 665 virtual FloatRect getLayerClipBoundsForDisplay(const DisplayDevice&) const; 666 667 private: 668 /* ------------------------------------------------------------------------ 669 * Layer management 670 */ 671 status_t createLayer(const String8& name, const sp<Client>& client, uint32_t w, uint32_t h, 672 PixelFormat format, uint32_t flags, LayerMetadata metadata, 673 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, 674 const sp<IBinder>& parentHandle, const sp<Layer>& parentLayer = nullptr, 675 uint32_t* outTransformHint = nullptr); 676 677 status_t createBufferQueueLayer(const sp<Client>& client, std::string name, uint32_t w, 678 uint32_t h, uint32_t flags, LayerMetadata metadata, 679 PixelFormat& format, sp<IBinder>* outHandle, 680 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer); 681 682 status_t createBufferStateLayer(const sp<Client>& client, std::string name, uint32_t w, 683 uint32_t h, uint32_t flags, LayerMetadata metadata, 684 sp<IBinder>* outHandle, sp<Layer>* outLayer); 685 686 status_t createEffectLayer(const sp<Client>& client, std::string name, uint32_t w, uint32_t h, 687 uint32_t flags, LayerMetadata metadata, sp<IBinder>* outHandle, 688 sp<Layer>* outLayer); 689 690 status_t createContainerLayer(const sp<Client>& client, std::string name, uint32_t w, 691 uint32_t h, uint32_t flags, LayerMetadata metadata, 692 sp<IBinder>* outHandle, sp<Layer>* outLayer); 693 694 status_t mirrorLayer(const sp<Client>& client, const sp<IBinder>& mirrorFromHandle, 695 sp<IBinder>* outHandle); 696 697 std::string getUniqueLayerName(const char* name); 698 699 // called when all clients have released all their references to 700 // this layer meaning it is entirely safe to destroy all 701 // resources associated to this layer. 702 void onHandleDestroyed(sp<Layer>& layer); 703 void markLayerPendingRemovalLocked(const sp<Layer>& layer); 704 705 // add a layer to SurfaceFlinger 706 status_t addClientLayer(const sp<Client>& client, const sp<IBinder>& handle, 707 const sp<IGraphicBufferProducer>& gbc, const sp<Layer>& lbc, 708 const sp<IBinder>& parentHandle, const sp<Layer>& parentLayer, 709 bool addToCurrentState, uint32_t* outTransformHint); 710 711 // Traverse through all the layers and compute and cache its bounds. 712 void computeLayerBounds(); 713 714 /* ------------------------------------------------------------------------ 715 * Boot animation, on/off animations and screen capture 716 */ 717 718 void startBootAnim(); 719 720 using TraverseLayersFunction = std::function<void(const LayerVector::Visitor&)>; 721 722 void renderScreenImplLocked(const RenderArea& renderArea, TraverseLayersFunction traverseLayers, 723 ANativeWindowBuffer* buffer, bool useIdentityTransform, 724 bool regionSampling, int* outSyncFd); 725 status_t captureScreenCommon(RenderArea& renderArea, TraverseLayersFunction traverseLayers, 726 sp<GraphicBuffer>* outBuffer, const ui::PixelFormat reqPixelFormat, 727 bool useIdentityTransform, bool& outCapturedSecureLayers); 728 status_t captureScreenCommon(RenderArea& renderArea, TraverseLayersFunction traverseLayers, 729 const sp<GraphicBuffer>& buffer, bool useIdentityTransform, 730 bool regionSampling, bool& outCapturedSecureLayers); 731 sp<DisplayDevice> getDisplayByIdOrLayerStack(uint64_t displayOrLayerStack) REQUIRES(mStateLock); 732 sp<DisplayDevice> getDisplayByLayerStack(uint64_t layerStack) REQUIRES(mStateLock); 733 status_t captureScreenImplLocked(const RenderArea& renderArea, 734 TraverseLayersFunction traverseLayers, 735 ANativeWindowBuffer* buffer, bool useIdentityTransform, 736 bool forSystem, int* outSyncFd, bool regionSampling, 737 bool& outCapturedSecureLayers); 738 void traverseLayersInDisplay(const sp<const DisplayDevice>& display, 739 const LayerVector::Visitor& visitor); 740 741 sp<StartPropertySetThread> mStartPropertySetThread; 742 743 /* ------------------------------------------------------------------------ 744 * Properties 745 */ 746 void readPersistentProperties(); 747 748 /* ------------------------------------------------------------------------ 749 * EGL 750 */ 751 size_t getMaxTextureSize() const; 752 size_t getMaxViewportDims() const; 753 754 /* ------------------------------------------------------------------------ 755 * Display and layer stack management 756 */ 757 // called when starting, or restarting after system_server death 758 void initializeDisplays(); 759 getDisplayDeviceLocked(const wp<IBinder> & displayToken)760 sp<const DisplayDevice> getDisplayDeviceLocked(const wp<IBinder>& displayToken) const 761 REQUIRES(mStateLock) { 762 return const_cast<SurfaceFlinger*>(this)->getDisplayDeviceLocked(displayToken); 763 } 764 getDisplayDeviceLocked(const wp<IBinder> & displayToken)765 sp<DisplayDevice> getDisplayDeviceLocked(const wp<IBinder>& displayToken) REQUIRES(mStateLock) { 766 const auto it = mDisplays.find(displayToken); 767 return it == mDisplays.end() ? nullptr : it->second; 768 } 769 getDefaultDisplayDeviceLocked()770 sp<const DisplayDevice> getDefaultDisplayDeviceLocked() const REQUIRES(mStateLock) { 771 return const_cast<SurfaceFlinger*>(this)->getDefaultDisplayDeviceLocked(); 772 } 773 getDefaultDisplayDeviceLocked()774 sp<DisplayDevice> getDefaultDisplayDeviceLocked() REQUIRES(mStateLock) { 775 if (const auto token = getInternalDisplayTokenLocked()) { 776 return getDisplayDeviceLocked(token); 777 } 778 return nullptr; 779 } 780 getDefaultDisplayDevice()781 sp<const DisplayDevice> getDefaultDisplayDevice() EXCLUDES(mStateLock) { 782 Mutex::Autolock lock(mStateLock); 783 return getDefaultDisplayDeviceLocked(); 784 } 785 786 std::optional<DeviceProductInfo> getDeviceProductInfoLocked(const DisplayDevice&) const; 787 788 // mark a region of a layer stack dirty. this updates the dirty 789 // region of all screens presenting this layer stack. 790 void invalidateLayerStack(const sp<const Layer>& layer, const Region& dirty); 791 792 /* ------------------------------------------------------------------------ 793 * H/W composer 794 */ 795 796 // The current hardware composer interface. 797 // 798 // The following thread safety rules apply when accessing mHwc, either 799 // directly or via getHwComposer(): 800 // 801 // 1. When recreating mHwc, acquire mStateLock. We currently recreate mHwc 802 // only when switching into and out of vr. Recreating mHwc must only be 803 // done on the main thread. 804 // 805 // 2. When accessing mHwc on the main thread, it's not necessary to acquire 806 // mStateLock. 807 // 808 // 3. When accessing mHwc on a thread other than the main thread, we always 809 // need to acquire mStateLock. This is because the main thread could be 810 // in the process of destroying the current mHwc instance. 811 // 812 // The above thread safety rules only apply to SurfaceFlinger.cpp. In 813 // SurfaceFlinger_hwc1.cpp we create mHwc at surface flinger init and never 814 // destroy it, so it's always safe to access mHwc from any thread without 815 // acquiring mStateLock. 816 HWComposer& getHwComposer() const; 817 818 /* ------------------------------------------------------------------------ 819 * Compositing 820 */ 821 void invalidateHwcGeometry(); 822 823 void postComposition(); 824 void getCompositorTiming(CompositorTiming* compositorTiming); 825 void updateCompositorTiming(const DisplayStatInfo& stats, nsecs_t compositeTime, 826 std::shared_ptr<FenceTime>& presentFenceTime); 827 void setCompositorTimingSnapped(const DisplayStatInfo& stats, 828 nsecs_t compositeToPresentLatency); 829 830 void postFrame(); 831 832 /* ------------------------------------------------------------------------ 833 * Display management 834 */ 835 sp<DisplayDevice> setupNewDisplayDeviceInternal( 836 const wp<IBinder>& displayToken, 837 std::shared_ptr<compositionengine::Display> compositionDisplay, 838 const DisplayDeviceState& state, 839 const sp<compositionengine::DisplaySurface>& displaySurface, 840 const sp<IGraphicBufferProducer>& producer) REQUIRES(mStateLock); 841 void processDisplayChangesLocked() REQUIRES(mStateLock); 842 void processDisplayAdded(const wp<IBinder>& displayToken, const DisplayDeviceState&) 843 REQUIRES(mStateLock); 844 void processDisplayRemoved(const wp<IBinder>& displayToken) REQUIRES(mStateLock); 845 void processDisplayChanged(const wp<IBinder>& displayToken, 846 const DisplayDeviceState& currentState, 847 const DisplayDeviceState& drawingState) REQUIRES(mStateLock); 848 void processDisplayHotplugEventsLocked() REQUIRES(mStateLock); 849 850 void dispatchDisplayHotplugEvent(PhysicalDisplayId displayId, bool connected); 851 852 /* ------------------------------------------------------------------------ 853 * VSync 854 */ 855 nsecs_t getVsyncPeriod() const REQUIRES(mStateLock); 856 857 // Sets the refresh rate by switching active configs, if they are available for 858 // the desired refresh rate. 859 void changeRefreshRateLocked(const RefreshRate&, Scheduler::ConfigEvent event) 860 REQUIRES(mStateLock); 861 862 bool isDisplayConfigAllowed(HwcConfigIndexType configId) const REQUIRES(mStateLock); 863 864 // Gets the fence for the previous frame. 865 // Must be called on the main thread. 866 sp<Fence> previousFrameFence(); 867 868 // Whether the previous frame has not yet been presented to the display. 869 // If graceTimeMs is positive, this method waits for at most the provided 870 // grace period before reporting if the frame missed. 871 // Must be called on the main thread. 872 bool previousFramePending(int graceTimeMs = 0); 873 874 // Returns the previous time that the frame was presented. If the frame has 875 // not been presented yet, then returns Fence::SIGNAL_TIME_PENDING. If there 876 // is no pending frame, then returns Fence::SIGNAL_TIME_INVALID. 877 // Must be called on the main thread. 878 nsecs_t previousFramePresentTime(); 879 880 // Calculates the expected present time for this frame. For negative offsets, performs a 881 // correction using the predicted vsync for the next frame instead. 882 nsecs_t calculateExpectedPresentTime(nsecs_t now) const; 883 884 /* 885 * Display identification 886 */ getPhysicalDisplayTokenLocked(DisplayId displayId)887 sp<IBinder> getPhysicalDisplayTokenLocked(DisplayId displayId) const REQUIRES(mStateLock) { 888 const auto it = mPhysicalDisplayTokens.find(displayId); 889 return it != mPhysicalDisplayTokens.end() ? it->second : nullptr; 890 } 891 getPhysicalDisplayIdLocked(const sp<IBinder> & displayToken)892 std::optional<DisplayId> getPhysicalDisplayIdLocked(const sp<IBinder>& displayToken) const 893 REQUIRES(mStateLock) { 894 for (const auto& [id, token] : mPhysicalDisplayTokens) { 895 if (token == displayToken) { 896 return id; 897 } 898 } 899 return {}; 900 } 901 902 // TODO(b/74619554): Remove special cases for primary display. getInternalDisplayTokenLocked()903 sp<IBinder> getInternalDisplayTokenLocked() const REQUIRES(mStateLock) { 904 const auto displayId = getInternalDisplayIdLocked(); 905 return displayId ? getPhysicalDisplayTokenLocked(*displayId) : nullptr; 906 } 907 getInternalDisplayIdLocked()908 std::optional<DisplayId> getInternalDisplayIdLocked() const REQUIRES(mStateLock) { 909 const auto hwcDisplayId = getHwComposer().getInternalHwcDisplayId(); 910 return hwcDisplayId ? getHwComposer().toPhysicalDisplayId(*hwcDisplayId) : std::nullopt; 911 } 912 913 /* 914 * Debugging & dumpsys 915 */ 916 using DumpArgs = Vector<String16>; 917 using Dumper = std::function<void(const DumpArgs&, bool asProto, std::string&)>; 918 919 template <typename F, std::enable_if_t<!std::is_member_function_pointer_v<F>>* = nullptr> dumper(F && dump)920 static Dumper dumper(F&& dump) { 921 using namespace std::placeholders; 922 return std::bind(std::forward<F>(dump), _3); 923 } 924 925 template <typename F, std::enable_if_t<std::is_member_function_pointer_v<F>>* = nullptr> dumper(F dump)926 Dumper dumper(F dump) { 927 using namespace std::placeholders; 928 return std::bind(dump, this, _3); 929 } 930 931 template <typename F> argsDumper(F dump)932 Dumper argsDumper(F dump) { 933 using namespace std::placeholders; 934 return std::bind(dump, this, _1, _3); 935 } 936 937 template <typename F> protoDumper(F dump)938 Dumper protoDumper(F dump) { 939 using namespace std::placeholders; 940 return std::bind(dump, this, _1, _2, _3); 941 } 942 943 void dumpAllLocked(const DumpArgs& args, std::string& result) const REQUIRES(mStateLock); 944 945 void appendSfConfigString(std::string& result) const; 946 void listLayersLocked(std::string& result) const; 947 void dumpStatsLocked(const DumpArgs& args, std::string& result) const REQUIRES(mStateLock); 948 void clearStatsLocked(const DumpArgs& args, std::string& result); 949 void dumpTimeStats(const DumpArgs& args, bool asProto, std::string& result) const; 950 void logFrameStats(); 951 952 void dumpVSync(std::string& result) const REQUIRES(mStateLock); 953 void dumpStaticScreenStats(std::string& result) const; 954 // Not const because each Layer needs to query Fences and cache timestamps. 955 void dumpFrameEventsLocked(std::string& result); 956 957 void recordBufferingStats(const std::string& layerName, 958 std::vector<OccupancyTracker::Segment>&& history); 959 void dumpBufferingStats(std::string& result) const; 960 void dumpDisplayIdentificationData(std::string& result) const REQUIRES(mStateLock); 961 void dumpRawDisplayIdentificationData(const DumpArgs&, std::string& result) const; 962 void dumpWideColorInfo(std::string& result) const REQUIRES(mStateLock); 963 LayersProto dumpDrawingStateProto(uint32_t traceFlags) const; 964 void dumpOffscreenLayersProto(LayersProto& layersProto, 965 uint32_t traceFlags = SurfaceTracing::TRACE_ALL) const; 966 // Dumps state from HW Composer 967 void dumpHwc(std::string& result) const; 968 LayersProto dumpProtoFromMainThread(uint32_t traceFlags = SurfaceTracing::TRACE_ALL) 969 EXCLUDES(mStateLock); 970 void dumpOffscreenLayers(std::string& result) EXCLUDES(mStateLock); 971 isLayerTripleBufferingDisabled()972 bool isLayerTripleBufferingDisabled() const { 973 return this->mLayerTripleBufferingDisabled; 974 } 975 976 status_t doDump(int fd, const DumpArgs& args, bool asProto); 977 978 status_t dumpCritical(int fd, const DumpArgs&, bool asProto); 979 dumpAll(int fd,const DumpArgs & args,bool asProto)980 status_t dumpAll(int fd, const DumpArgs& args, bool asProto) override { 981 return doDump(fd, args, asProto); 982 } 983 984 void onFrameRateFlexibilityTokenReleased(); 985 986 /* ------------------------------------------------------------------------ 987 * VrFlinger 988 */ 989 void resetDisplayState() REQUIRES(mStateLock); 990 991 // Check to see if we should handoff to vr flinger. 992 void updateVrFlinger(); 993 994 void updateColorMatrixLocked(); 995 996 /* ------------------------------------------------------------------------ 997 * Attributes 998 */ 999 1000 surfaceflinger::Factory& mFactory; 1001 1002 // access must be protected by mStateLock 1003 mutable Mutex mStateLock; 1004 State mCurrentState{LayerVector::StateSet::Current}; 1005 std::atomic<int32_t> mTransactionFlags = 0; 1006 Condition mTransactionCV; 1007 bool mTransactionPending = false; 1008 bool mAnimTransactionPending = false; 1009 SortedVector<sp<Layer>> mLayersPendingRemoval; 1010 bool mForceTraversal = false; 1011 1012 // global color transform states 1013 Daltonizer mDaltonizer; 1014 float mGlobalSaturationFactor = 1.0f; 1015 mat4 mClientColorMatrix; 1016 1017 // Can't be unordered_set because wp<> isn't hashable 1018 std::set<wp<IBinder>> mGraphicBufferProducerList; 1019 size_t mMaxGraphicBufferProducerListSize = ISurfaceComposer::MAX_LAYERS; 1020 1021 void removeGraphicBufferProducerAsync(const wp<IBinder>&); 1022 1023 // protected by mStateLock (but we could use another lock) 1024 bool mLayersRemoved = false; 1025 bool mLayersAdded = false; 1026 1027 std::atomic<bool> mRepaintEverything = false; 1028 1029 // constant members (no synchronization needed for access) 1030 const nsecs_t mBootTime = systemTime(); 1031 bool mGpuToCpuSupported = false; 1032 bool mIsUserBuild = true; 1033 1034 // Can only accessed from the main thread, these members 1035 // don't need synchronization 1036 State mDrawingState{LayerVector::StateSet::Drawing}; 1037 bool mVisibleRegionsDirty = false; 1038 // Set during transaction commit stage to track if the input info for a layer has changed. 1039 bool mInputInfoChanged = false; 1040 bool mGeometryInvalid = false; 1041 bool mAnimCompositionPending = false; 1042 std::vector<sp<Layer>> mLayersWithQueuedFrames; 1043 // Tracks layers that need to update a display's dirty region. 1044 std::vector<sp<Layer>> mLayersPendingRefresh; 1045 std::array<sp<Fence>, 2> mPreviousPresentFences = {Fence::NO_FENCE, Fence::NO_FENCE}; 1046 // True if in the previous frame at least one layer was composed via the GPU. 1047 bool mHadClientComposition = false; 1048 // True if in the previous frame at least one layer was composed via HW Composer. 1049 // Note that it is possible for a frame to be composed via both client and device 1050 // composition, for example in the case of overlays. 1051 bool mHadDeviceComposition = false; 1052 // True if in the previous frame, the client composition was skipped by reusing the buffer 1053 // used in a previous composition. This can happed if the client composition requests 1054 // did not change. 1055 bool mReusedClientComposition = false; 1056 1057 enum class BootStage { 1058 BOOTLOADER, 1059 BOOTANIMATION, 1060 FINISHED, 1061 }; 1062 BootStage mBootStage = BootStage::BOOTLOADER; 1063 1064 struct HotplugEvent { 1065 hal::HWDisplayId hwcDisplayId; 1066 hal::Connection connection = hal::Connection::INVALID; 1067 }; 1068 std::vector<HotplugEvent> mPendingHotplugEvents GUARDED_BY(mStateLock); 1069 1070 // this may only be written from the main thread with mStateLock held 1071 // it may be read from other threads with mStateLock held 1072 std::map<wp<IBinder>, sp<DisplayDevice>> mDisplays GUARDED_BY(mStateLock); 1073 std::unordered_map<DisplayId, sp<IBinder>> mPhysicalDisplayTokens GUARDED_BY(mStateLock); 1074 1075 std::unordered_map<BBinder*, wp<Layer>> mLayersByLocalBinderToken GUARDED_BY(mStateLock); 1076 1077 // don't use a lock for these, we don't care 1078 int mDebugRegion = 0; 1079 bool mDebugDisableHWC = false; 1080 bool mDebugDisableTransformHint = false; 1081 volatile nsecs_t mDebugInTransaction = 0; 1082 bool mForceFullDamage = false; 1083 bool mPropagateBackpressure = true; 1084 bool mPropagateBackpressureClientComposition = false; 1085 std::unique_ptr<SurfaceInterceptor> mInterceptor; 1086 1087 SurfaceTracing mTracing{*this}; 1088 std::mutex mTracingLock; 1089 bool mTracingEnabled = false; 1090 bool mAddCompositionStateToTrace = false; 1091 std::atomic<bool> mTracingEnabledChanged = false; 1092 1093 const std::shared_ptr<TimeStats> mTimeStats; 1094 const std::unique_ptr<FrameTracer> mFrameTracer; 1095 bool mUseHwcVirtualDisplays = false; 1096 // If blurs should be enabled on this device. 1097 bool mSupportsBlur = false; 1098 // Disable blurs, for debugging 1099 std::atomic<bool> mDisableBlurs = false; 1100 // If blurs are considered expensive and should require high GPU frequency. 1101 bool mBlursAreExpensive = false; 1102 std::atomic<uint32_t> mFrameMissedCount = 0; 1103 std::atomic<uint32_t> mHwcFrameMissedCount = 0; 1104 std::atomic<uint32_t> mGpuFrameMissedCount = 0; 1105 1106 TransactionCompletedThread mTransactionCompletedThread; 1107 1108 // Restrict layers to use two buffers in their bufferqueues. 1109 bool mLayerTripleBufferingDisabled = false; 1110 1111 // these are thread safe 1112 std::unique_ptr<MessageQueue> mEventQueue; 1113 FrameTracker mAnimFrameTracker; 1114 1115 // protected by mDestroyedLayerLock; 1116 mutable Mutex mDestroyedLayerLock; 1117 Vector<Layer const *> mDestroyedLayers; 1118 1119 nsecs_t mRefreshStartTime = 0; 1120 1121 std::atomic<bool> mRefreshPending = false; 1122 1123 // We maintain a pool of pre-generated texture names to hand out to avoid 1124 // layer creation needing to run on the main thread (which it would 1125 // otherwise need to do to access RenderEngine). 1126 std::mutex mTexturePoolMutex; 1127 uint32_t mTexturePoolSize = 0; 1128 std::vector<uint32_t> mTexturePool; 1129 1130 struct TransactionState { TransactionStateTransactionState1131 TransactionState(const Vector<ComposerState>& composerStates, 1132 const Vector<DisplayState>& displayStates, uint32_t transactionFlags, 1133 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer, 1134 int64_t postTime, bool privileged, bool hasListenerCallbacks, 1135 std::vector<ListenerCallbacks> listenerCallbacks) 1136 : states(composerStates), 1137 displays(displayStates), 1138 flags(transactionFlags), 1139 desiredPresentTime(desiredPresentTime), 1140 buffer(uncacheBuffer), 1141 postTime(postTime), 1142 privileged(privileged), 1143 hasListenerCallbacks(hasListenerCallbacks), 1144 listenerCallbacks(listenerCallbacks) {} 1145 1146 Vector<ComposerState> states; 1147 Vector<DisplayState> displays; 1148 uint32_t flags; 1149 const int64_t desiredPresentTime; 1150 client_cache_t buffer; 1151 const int64_t postTime; 1152 bool privileged; 1153 bool hasListenerCallbacks; 1154 std::vector<ListenerCallbacks> listenerCallbacks; 1155 }; 1156 std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash> mTransactionQueues; 1157 1158 /* ------------------------------------------------------------------------ 1159 * Feature prototyping 1160 */ 1161 1162 // Static screen stats 1163 bool mHasPoweredOff = false; 1164 1165 std::atomic<size_t> mNumLayers = 0; 1166 1167 // Verify that transaction is being called by an approved process: 1168 // either AID_GRAPHICS or AID_SYSTEM. 1169 status_t CheckTransactCodeCredentials(uint32_t code); 1170 1171 // to linkToDeath 1172 sp<IBinder> mWindowManager; 1173 // We want to avoid multiple calls to BOOT_FINISHED as they come in on 1174 // different threads without a lock and could trigger unsynchronized writes to 1175 // to mWindowManager or mInputFlinger 1176 std::atomic<bool> mBootFinished = false; 1177 1178 std::unique_ptr<dvr::VrFlinger> mVrFlinger; 1179 std::atomic<bool> mVrFlingerRequestsDisplay = false; 1180 static bool useVrFlinger; 1181 std::thread::id mMainThreadId = std::this_thread::get_id(); 1182 1183 DisplayColorSetting mDisplayColorSetting = DisplayColorSetting::kEnhanced; 1184 1185 // Color mode forced by setting persist.sys.sf.color_mode, it must: 1186 // 1. not be NATIVE color mode, NATIVE color mode means no forced color mode; 1187 // 2. be one of the supported color modes returned by hardware composer, otherwise 1188 // it will not be respected. 1189 // persist.sys.sf.color_mode will only take effect when persist.sys.sf.native_mode 1190 // is not set to 1. 1191 // This property can be used to force SurfaceFlinger to always pick a certain color mode. 1192 ui::ColorMode mForceColorMode = ui::ColorMode::NATIVE; 1193 1194 ui::Dataspace mDefaultCompositionDataspace; 1195 ui::Dataspace mWideColorGamutCompositionDataspace; 1196 ui::Dataspace mColorSpaceAgnosticDataspace; 1197 1198 SurfaceFlingerBE mBE; 1199 std::unique_ptr<compositionengine::CompositionEngine> mCompositionEngine; 1200 1201 /* ------------------------------------------------------------------------ 1202 * Scheduler 1203 */ 1204 std::unique_ptr<Scheduler> mScheduler; 1205 scheduler::ConnectionHandle mAppConnectionHandle; 1206 scheduler::ConnectionHandle mSfConnectionHandle; 1207 1208 // Stores phase offsets configured per refresh rate. 1209 std::unique_ptr<scheduler::PhaseConfiguration> mPhaseConfiguration; 1210 1211 // Optional to defer construction until scheduler connections are created. 1212 std::optional<scheduler::VSyncModulator> mVSyncModulator; 1213 1214 std::unique_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs; 1215 std::unique_ptr<scheduler::RefreshRateStats> mRefreshRateStats; 1216 1217 std::atomic<nsecs_t> mExpectedPresentTime = 0; 1218 hal::Vsync mHWCVsyncPendingState = hal::Vsync::DISABLE; 1219 1220 /* ------------------------------------------------------------------------ 1221 * Generic Layer Metadata 1222 */ 1223 const std::unordered_map<std::string, uint32_t>& getGenericLayerMetadataKeyMap() const; 1224 1225 /* ------------------------------------------------------------------------ 1226 * Misc 1227 */ 1228 getDesiredActiveConfig()1229 std::optional<ActiveConfigInfo> getDesiredActiveConfig() EXCLUDES(mActiveConfigLock) { 1230 std::lock_guard<std::mutex> lock(mActiveConfigLock); 1231 if (mDesiredActiveConfigChanged) return mDesiredActiveConfig; 1232 return std::nullopt; 1233 } 1234 1235 std::mutex mActiveConfigLock; 1236 // This bit is set once we start setting the config. We read from this bit during the 1237 // process. If at the end, this bit is different than mDesiredActiveConfig, we restart 1238 // the process. 1239 ActiveConfigInfo mUpcomingActiveConfig; // Always read and written on the main thread. 1240 // This bit can be set at any point in time when the system wants the new config. 1241 ActiveConfigInfo mDesiredActiveConfig GUARDED_BY(mActiveConfigLock); 1242 1243 // below flags are set by main thread only 1244 TracedOrdinal<bool> mDesiredActiveConfigChanged 1245 GUARDED_BY(mActiveConfigLock) = {"DesiredActiveConfigChanged", false}; 1246 bool mSetActiveConfigPending = false; 1247 1248 bool mLumaSampling = true; 1249 sp<RegionSamplingThread> mRegionSamplingThread; 1250 ui::DisplayPrimaries mInternalDisplayPrimaries; 1251 1252 const float mInternalDisplayDensity; 1253 const float mEmulatedDisplayDensity; 1254 1255 sp<IInputFlinger> mInputFlinger; 1256 InputWindowCommands mPendingInputWindowCommands GUARDED_BY(mStateLock); 1257 // Should only be accessed by the main thread. 1258 InputWindowCommands mInputWindowCommands; 1259 1260 struct SetInputWindowsListener : BnSetInputWindowsListener { SetInputWindowsListenerSetInputWindowsListener1261 explicit SetInputWindowsListener(sp<SurfaceFlinger> flinger) 1262 : mFlinger(std::move(flinger)) {} 1263 1264 void onSetInputWindowsFinished() override; 1265 1266 const sp<SurfaceFlinger> mFlinger; 1267 }; 1268 1269 const sp<SetInputWindowsListener> mSetInputWindowsListener = new SetInputWindowsListener(this); 1270 1271 bool mPendingSyncInputWindows GUARDED_BY(mStateLock) = false; 1272 Hwc2::impl::PowerAdvisor mPowerAdvisor; 1273 1274 // This should only be accessed on the main thread. 1275 nsecs_t mFrameStartTime = 0; 1276 1277 void enableRefreshRateOverlay(bool enable); 1278 std::unique_ptr<RefreshRateOverlay> mRefreshRateOverlay GUARDED_BY(mStateLock); 1279 1280 // Flag used to set override allowed display configs from backdoor 1281 bool mDebugDisplayConfigSetByBackdoor = false; 1282 1283 // A set of layers that have no parent so they are not drawn on screen. 1284 // Should only be accessed by the main thread. 1285 // The Layer pointer is removed from the set when the destructor is called so there shouldn't 1286 // be any issues with a raw pointer referencing an invalid object. 1287 std::unordered_set<Layer*> mOffscreenLayers; 1288 1289 // Fields tracking the current jank event: when it started and how many 1290 // janky frames there are. 1291 nsecs_t mMissedFrameJankStart = 0; 1292 int32_t mMissedFrameJankCount = 0; 1293 // Positive if jank should be uploaded in postComposition 1294 nsecs_t mLastJankDuration = -1; 1295 1296 int mFrameRateFlexibilityTokenCount = 0; 1297 1298 sp<IBinder> mDebugFrameRateFlexibilityToken; 1299 }; 1300 1301 } // namespace android 1302