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 <stdint.h> 20 #include <sys/types.h> 21 22 #include <set> 23 #include <thread> 24 #include <unordered_map> 25 #include <unordered_set> 26 27 #include <binder/IBinder.h> 28 29 #include <utils/Errors.h> 30 #include <utils/RefBase.h> 31 #include <utils/Singleton.h> 32 #include <utils/SortedVector.h> 33 #include <utils/threads.h> 34 35 #include <ui/BlurRegion.h> 36 #include <ui/ConfigStoreTypes.h> 37 #include <ui/DisplayedFrameStats.h> 38 #include <ui/EdgeExtensionEffect.h> 39 #include <ui/FrameStats.h> 40 #include <ui/GraphicTypes.h> 41 #include <ui/PictureProfileHandle.h> 42 #include <ui/PixelFormat.h> 43 #include <ui/Rotation.h> 44 #include <ui/StaticDisplayInfo.h> 45 46 #include <android/gui/BnJankListener.h> 47 #include <android/gui/ISurfaceComposerClient.h> 48 49 #include <gui/BufferReleaseChannel.h> 50 #include <gui/CpuConsumer.h> 51 #include <gui/ISurfaceComposer.h> 52 #include <gui/ITransactionCompletedListener.h> 53 #include <gui/LayerState.h> 54 #include <gui/SurfaceControl.h> 55 #include <gui/WindowInfosListenerReporter.h> 56 #include <math/vec3.h> 57 58 #include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h> 59 60 namespace android { 61 62 class HdrCapabilities; 63 class IGraphicBufferProducer; 64 class ITunnelModeEnabledListener; 65 class Region; 66 class TransactionCompletedListener; 67 68 using gui::DisplayCaptureArgs; 69 using gui::IRegionSamplingListener; 70 using gui::ISurfaceComposerClient; 71 using gui::LayerCaptureArgs; 72 using gui::LayerMetadata; 73 74 struct SurfaceControlStats { SurfaceControlStatsSurfaceControlStats75 SurfaceControlStats(const sp<SurfaceControl>& sc, nsecs_t latchTime, 76 std::variant<nsecs_t, sp<Fence>> acquireTimeOrFence, 77 const sp<Fence>& presentFence, const sp<Fence>& prevReleaseFence, 78 std::optional<uint32_t> hint, FrameEventHistoryStats eventStats, 79 uint32_t currentMaxAcquiredBufferCount) 80 : surfaceControl(sc), 81 latchTime(latchTime), 82 acquireTimeOrFence(std::move(acquireTimeOrFence)), 83 presentFence(presentFence), 84 previousReleaseFence(prevReleaseFence), 85 transformHint(hint), 86 frameEventStats(eventStats), 87 currentMaxAcquiredBufferCount(currentMaxAcquiredBufferCount) {} 88 89 sp<SurfaceControl> surfaceControl; 90 nsecs_t latchTime = -1; 91 std::variant<nsecs_t, sp<Fence>> acquireTimeOrFence = -1; 92 sp<Fence> presentFence; 93 sp<Fence> previousReleaseFence; 94 std::optional<uint32_t> transformHint = 0; 95 FrameEventHistoryStats frameEventStats; 96 uint32_t currentMaxAcquiredBufferCount = 0; 97 }; 98 99 using TransactionCompletedCallbackTakesContext = 100 std::function<void(void* /*context*/, nsecs_t /*latchTime*/, 101 const sp<Fence>& /*presentFence*/, 102 const std::vector<SurfaceControlStats>& /*stats*/)>; 103 using TransactionCompletedCallback = 104 std::function<void(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/, 105 const std::vector<SurfaceControlStats>& /*stats*/)>; 106 using ReleaseBufferCallback = 107 std::function<void(const ReleaseCallbackId&, const sp<Fence>& /*releaseFence*/, 108 std::optional<uint32_t> currentMaxAcquiredBufferCount)>; 109 110 using SurfaceStatsCallback = 111 std::function<void(void* /*context*/, nsecs_t /*latchTime*/, 112 const sp<Fence>& /*presentFence*/, 113 const SurfaceStats& /*stats*/)>; 114 115 using TrustedPresentationCallback = std::function<void(void*, bool)>; 116 117 // --------------------------------------------------------------------------- 118 119 class ReleaseCallbackThread { 120 public: 121 void addReleaseCallback(const ReleaseCallbackId, sp<Fence>); 122 void threadMain(); 123 124 private: 125 std::thread mThread; 126 std::mutex mMutex; 127 bool mStarted GUARDED_BY(mMutex) = false; 128 std::condition_variable mReleaseCallbackPending; 129 std::queue<std::tuple<const ReleaseCallbackId, const sp<Fence>>> mCallbackInfos 130 GUARDED_BY(mMutex); 131 }; 132 133 // --------------------------------------------------------------------------- 134 135 class SurfaceComposerClient : public RefBase 136 { 137 friend class Composer; 138 public: 139 SurfaceComposerClient(); 140 SurfaceComposerClient(const sp<ISurfaceComposerClient>& client); 141 virtual ~SurfaceComposerClient(); 142 143 // Always make sure we could initialize 144 status_t initCheck() const; 145 146 // Return the connection of this client 147 sp<IBinder> connection() const; 148 149 // Forcibly remove connection before all references have gone away. 150 void dispose(); 151 152 // callback when the composer is dies 153 status_t linkToComposerDeath(const sp<IBinder::DeathRecipient>& recipient, 154 void* cookie = nullptr, uint32_t flags = 0); 155 156 // Notify the SurfaceComposerClient that the boot procedure has completed 157 static status_t bootFinished(); 158 159 // Get transactional state of given display. 160 static status_t getDisplayState(const sp<IBinder>& display, ui::DisplayState*); 161 162 // Get immutable information about given physical display. 163 static status_t getStaticDisplayInfo(int64_t, ui::StaticDisplayInfo*); 164 165 // Get dynamic information about given physical display from display id 166 static status_t getDynamicDisplayInfoFromId(int64_t, ui::DynamicDisplayInfo*); 167 168 // Shorthand for the active display mode from getDynamicDisplayInfo(). 169 // TODO(b/180391891): Update clients to use getDynamicDisplayInfo and remove this function. 170 static status_t getActiveDisplayMode(const sp<IBinder>& display, ui::DisplayMode*); 171 172 // Sets the refresh rate boundaries for the display. 173 static status_t setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, 174 const gui::DisplayModeSpecs&); 175 // Gets the refresh rate boundaries for the display. 176 static status_t getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, 177 gui::DisplayModeSpecs*); 178 179 // Get the coordinates of the display's native color primaries 180 static status_t getDisplayNativePrimaries(const sp<IBinder>& display, 181 ui::DisplayPrimaries& outPrimaries); 182 183 // Sets the active color mode for the given display 184 static status_t setActiveColorMode(const sp<IBinder>& display, 185 ui::ColorMode colorMode); 186 187 // Gets if boot display mode operations are supported on a device 188 static status_t getBootDisplayModeSupport(bool* support); 189 190 // Gets the overlay properties of the device 191 static status_t getOverlaySupport(gui::OverlayProperties* outProperties); 192 193 // Sets the user-preferred display mode that a device should boot in 194 static status_t setBootDisplayMode(const sp<IBinder>& display, ui::DisplayModeId); 195 // Clears the user-preferred display mode 196 static status_t clearBootDisplayMode(const sp<IBinder>& display); 197 198 // Gets the HDR conversion capabilities of the device 199 static status_t getHdrConversionCapabilities(std::vector<gui::HdrConversionCapability>*); 200 // Sets the HDR conversion strategy for the device. in case when HdrConversionStrategy has 201 // autoAllowedHdrTypes set. Returns Hdr::INVALID in other cases. 202 static status_t setHdrConversionStrategy(gui::HdrConversionStrategy hdrConversionStrategy, 203 ui::Hdr* outPreferredHdrOutputType); 204 // Returns whether HDR conversion is supported by the device. 205 static status_t getHdrOutputConversionSupport(bool* isSupported); 206 207 // Sets the frame rate of a particular app (uid). This is currently called 208 // by GameManager. 209 static status_t setGameModeFrameRateOverride(uid_t uid, float frameRate); 210 211 // Sets the frame rate of a particular app (uid). This is currently called 212 // by GameManager and controlled by two sysprops: 213 // "ro.surface_flinger.game_default_frame_rate_override" holding the override value, 214 // "persisit.graphics.game_default_frame_rate.enabled" to determine if it's enabled. 215 static status_t setGameDefaultFrameRateOverride(uid_t uid, float frameRate); 216 217 // Update the small area detection whole appId-threshold mappings by same size appId and 218 // threshold vector. 219 // Ref:setSmallAreaDetectionThreshold. 220 static status_t updateSmallAreaDetection(std::vector<int32_t>& appIds, 221 std::vector<float>& thresholds); 222 223 // Sets the small area detection threshold to particular apps (appId). Passing value 0 means 224 // to disable small area detection to the app. 225 static status_t setSmallAreaDetectionThreshold(int32_t appId, float threshold); 226 227 // Switches on/off Auto Low Latency Mode on the connected display. This should only be 228 // called if the connected display supports Auto Low Latency Mode as reported by 229 // #getAutoLowLatencyModeSupport 230 static void setAutoLowLatencyMode(const sp<IBinder>& display, bool on); 231 232 // Turns Game mode on/off on the connected display. This should only be called 233 // if the display supports Game content type, as reported by #getGameContentTypeSupport 234 static void setGameContentType(const sp<IBinder>& display, bool on); 235 236 /* Triggers screen on/off or low power mode and waits for it to complete */ 237 static void setDisplayPowerMode(const sp<IBinder>& display, int mode); 238 239 /* Returns the composition preference of the default data space and default pixel format, 240 * as well as the wide color gamut data space and wide color gamut pixel format. 241 * If the wide color gamut data space is V0_SRGB, then it implies that the platform 242 * has no wide color gamut support. 243 */ 244 static status_t getCompositionPreference(ui::Dataspace* defaultDataspace, 245 ui::PixelFormat* defaultPixelFormat, 246 ui::Dataspace* wideColorGamutDataspace, 247 ui::PixelFormat* wideColorGamutPixelFormat); 248 249 /* 250 * Gets whether SurfaceFlinger can support protected content in GPU composition. 251 * Requires the ACCESS_SURFACE_FLINGER permission. 252 */ 253 static bool getProtectedContentSupport(); 254 255 /** 256 * Gets the context priority of surface flinger's render engine. 257 */ 258 static int getGpuContextPriority(); 259 260 /** 261 * Uncaches a buffer in ISurfaceComposer. It must be uncached via a transaction so that it is 262 * in order with other transactions that use buffers. 263 */ 264 static void doUncacheBufferTransaction(uint64_t cacheId); 265 266 // Queries whether a given display is wide color display. 267 static status_t isWideColorDisplay(const sp<IBinder>& display, bool* outIsWideColorDisplay); 268 269 /* 270 * Returns whether brightness operations are supported on a display. 271 * 272 * displayToken 273 * The token of the display. 274 * 275 * Returns whether brightness operations are supported on a display or not. 276 */ 277 static bool getDisplayBrightnessSupport(const sp<IBinder>& displayToken); 278 279 /* 280 * Sets the brightness of a display. 281 * 282 * displayToken 283 * The token of the display whose brightness is set. 284 * brightness 285 * A number between 0.0 (minimum brightness) and 1.0 (maximum brightness), or -1.0f to 286 * turn the backlight off. 287 * 288 * Returns NO_ERROR upon success. Otherwise, 289 * NAME_NOT_FOUND if the display handle is invalid, or 290 * BAD_VALUE if the brightness value is invalid, or 291 * INVALID_OPERATION if brightness operaetions are not supported. 292 */ 293 static status_t setDisplayBrightness(const sp<IBinder>& displayToken, 294 const gui::DisplayBrightness& brightness); 295 296 static status_t addHdrLayerInfoListener(const sp<IBinder>& displayToken, 297 const sp<gui::IHdrLayerInfoListener>& listener); 298 static status_t removeHdrLayerInfoListener(const sp<IBinder>& displayToken, 299 const sp<gui::IHdrLayerInfoListener>& listener); 300 301 static status_t setActivePictureListener(const sp<gui::IActivePictureListener>& listener); 302 303 /* 304 * Sends a power boost to the composer. This function is asynchronous. 305 * 306 * boostId 307 * boost id according to android::hardware::power::Boost 308 * 309 * Returns NO_ERROR upon success. 310 */ 311 static status_t notifyPowerBoost(int32_t boostId); 312 313 /* 314 * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows 315 * material design guidelines. 316 * 317 * ambientColor 318 * Color to the ambient shadow. The alpha is premultiplied. 319 * 320 * spotColor 321 * Color to the spot shadow. The alpha is premultiplied. The position of the spot shadow 322 * depends on the light position. 323 * 324 * lightPosY/lightPosZ 325 * Position of the light used to cast the spot shadow. The X value is always the display 326 * width / 2. 327 * 328 * lightRadius 329 * Radius of the light casting the shadow. 330 */ 331 static status_t setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor, 332 float lightPosY, float lightPosZ, float lightRadius); 333 334 /* 335 * Returns whether and how a display supports DISPLAY_DECORATION layers. 336 * 337 * displayToken 338 * The token of the display. 339 * 340 * Returns how a display supports DISPLAY_DECORATION layers, or nullopt if 341 * it does not. 342 */ 343 static std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport> 344 getDisplayDecorationSupport(const sp<IBinder>& displayToken); 345 346 static bool flagEdgeExtensionEffectUseShader(); 347 348 /** 349 * Returns how many picture profiles are supported by the display. 350 * 351 * displayToken 352 * The token of the display. 353 */ 354 static status_t getMaxLayerPictureProfiles(const sp<IBinder>& displayToken, 355 int32_t* outMaxProfiles); 356 357 // ------------------------------------------------------------------------ 358 // surface creation / destruction 359 360 static sp<SurfaceComposerClient> getDefault(); 361 362 //! Create a surface 363 sp<SurfaceControl> createSurface(const String8& name, // name of the surface 364 uint32_t w, // width in pixel 365 uint32_t h, // height in pixel 366 PixelFormat format, // pixel-format desired 367 int32_t flags = 0, // usage flags 368 const sp<IBinder>& parentHandle = nullptr, // parentHandle 369 LayerMetadata metadata = LayerMetadata(), // metadata 370 uint32_t* outTransformHint = nullptr); 371 372 status_t createSurfaceChecked(const String8& name, // name of the surface 373 uint32_t w, // width in pixel 374 uint32_t h, // height in pixel 375 PixelFormat format, // pixel-format desired 376 sp<SurfaceControl>* outSurface, 377 int32_t flags = 0, // usage flags 378 const sp<IBinder>& parentHandle = nullptr, // parentHandle 379 LayerMetadata metadata = LayerMetadata(), // metadata 380 uint32_t* outTransformHint = nullptr); 381 382 // Creates a mirrored hierarchy for the mirrorFromSurface. This returns a SurfaceControl 383 // which is a parent of the root of the mirrored hierarchy. 384 // 385 // Real Hierarchy Mirror 386 // SC (value that's returned) 387 // | 388 // A A' 389 // | | 390 // B B' 391 sp<SurfaceControl> mirrorSurface(SurfaceControl* mirrorFromSurface); 392 393 sp<SurfaceControl> mirrorDisplay(DisplayId displayId); 394 395 static const std::string kEmpty; 396 static sp<IBinder> createVirtualDisplay(const std::string& displayName, bool isSecure, 397 const std::string& uniqueId = kEmpty, 398 float requestedRefreshRate = 0); 399 400 static status_t destroyVirtualDisplay(const sp<IBinder>& displayToken); 401 402 static std::vector<PhysicalDisplayId> getPhysicalDisplayIds(); 403 404 static sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId); 405 406 // Returns StalledTransactionInfo if a transaction from the provided pid has not been applied 407 // due to an unsignaled fence. 408 static std::optional<gui::StalledTransactionInfo> getStalledTransactionInfo(pid_t pid); 409 410 struct SCHash { operatorSCHash411 std::size_t operator()(const sp<SurfaceControl>& sc) const { 412 return std::hash<SurfaceControl *>{}(sc.get()); 413 } 414 }; 415 416 struct IBinderHash { operatorIBinderHash417 std::size_t operator()(const sp<IBinder>& iBinder) const { 418 return std::hash<IBinder*>{}(iBinder.get()); 419 } 420 }; 421 422 struct TCLHash { operatorTCLHash423 std::size_t operator()(const sp<ITransactionCompletedListener>& tcl) const { 424 return std::hash<IBinder*>{}((tcl) ? IInterface::asBinder(tcl).get() : nullptr); 425 } 426 }; 427 428 struct CallbackInfo { 429 // All the callbacks that have been requested for a TransactionCompletedListener in the 430 // Transaction 431 std::unordered_set<CallbackId, CallbackIdHash> callbackIds; 432 // All the SurfaceControls that have been modified in this TransactionCompletedListener's 433 // process that require a callback if there is one or more callbackIds set. 434 std::unordered_set<sp<SurfaceControl>, SCHash> surfaceControls; 435 }; 436 437 struct PresentationCallbackRAII : public RefBase { 438 sp<TransactionCompletedListener> mTcl; 439 int mId; 440 PresentationCallbackRAII(TransactionCompletedListener* tcl, int id); 441 virtual ~PresentationCallbackRAII(); 442 }; 443 444 class Transaction : public Parcelable { 445 private: 446 static sp<IBinder> sApplyToken; 447 static std::mutex sApplyTokenMutex; 448 void releaseBufferIfOverwriting(const layer_state_t& state); 449 static void mergeFrameTimelineInfo(FrameTimelineInfo& t, const FrameTimelineInfo& other); 450 // Tracks registered callbacks 451 sp<TransactionCompletedListener> mTransactionCompletedListener = nullptr; 452 // Prints debug logs when enabled. 453 bool mLogCallPoints = false; 454 455 protected: 456 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> mComposerStates; 457 SortedVector<DisplayState> mDisplayStates; 458 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> 459 mListenerCallbacks; 460 std::vector<client_cache_t> mUncacheBuffers; 461 462 // We keep track of the last MAX_MERGE_HISTORY_LENGTH merged transaction ids. 463 // Ordered most recently merged to least recently merged. 464 static const size_t MAX_MERGE_HISTORY_LENGTH = 10u; 465 std::vector<uint64_t> mMergedTransactionIds; 466 467 uint64_t mId; 468 469 bool mAnimation = false; 470 bool mEarlyWakeupStart = false; 471 bool mEarlyWakeupEnd = false; 472 473 // Indicates that the Transaction may contain buffers that should be cached. The reason this 474 // is only a guess is that buffers can be removed before cache is called. This is only a 475 // hint that at some point a buffer was added to this transaction before apply was called. 476 bool mMayContainBuffer = false; 477 478 // mDesiredPresentTime is the time in nanoseconds that the client would like the transaction 479 // to be presented. When it is not possible to present at exactly that time, it will be 480 // presented after the time has passed. 481 // 482 // If the client didn't pass a desired presentation time, mDesiredPresentTime will be 483 // populated to the time setBuffer was called, and mIsAutoTimestamp will be set to true. 484 // 485 // Desired present times that are more than 1 second in the future may be ignored. 486 // When a desired present time has already passed, the transaction will be presented as soon 487 // as possible. 488 // 489 // Transactions from the same process are presented in the same order that they are applied. 490 // The desired present time does not affect this ordering. 491 int64_t mDesiredPresentTime = 0; 492 bool mIsAutoTimestamp = true; 493 494 // The vsync id provided by Choreographer.getVsyncId and the input event id 495 FrameTimelineInfo mFrameTimelineInfo; 496 497 // If not null, transactions will be queued up using this token otherwise a common token 498 // per process will be used. 499 sp<IBinder> mApplyToken = nullptr; 500 501 InputWindowCommands mInputWindowCommands; 502 int mStatus = NO_ERROR; 503 504 layer_state_t* getLayerState(const sp<SurfaceControl>& sc); 505 DisplayState& getDisplayState(const sp<IBinder>& token); 506 507 void cacheBuffers(); 508 void registerSurfaceControlForCallback(const sp<SurfaceControl>& sc); 509 void setReleaseBufferCallback(BufferData*, ReleaseBufferCallback); 510 511 public: 512 Transaction(); 513 virtual ~Transaction() = default; 514 Transaction(Transaction const& other); 515 516 // Factory method that creates a new Transaction instance from the parcel. 517 static std::unique_ptr<Transaction> createFromParcel(const Parcel* parcel); 518 519 status_t writeToParcel(Parcel* parcel) const override; 520 status_t readFromParcel(const Parcel* parcel) override; 521 522 // Clears the contents of the transaction without applying it. 523 void clear(); 524 525 // Returns the current id of the transaction. 526 // The id is updated every time the transaction is applied. 527 uint64_t getId(); 528 529 std::vector<uint64_t> getMergedTransactionIds(); 530 531 status_t apply(bool synchronous = false, bool oneWay = false); 532 // Merge another transaction in to this one, clearing other 533 // as if it had been applied. 534 Transaction& merge(Transaction&& other); 535 Transaction& show(const sp<SurfaceControl>& sc); 536 Transaction& hide(const sp<SurfaceControl>& sc); 537 Transaction& setPosition(const sp<SurfaceControl>& sc, float x, float y); 538 // b/243180033 remove once functions are not called from vendor code setSize(const sp<SurfaceControl> &,uint32_t,uint32_t)539 Transaction& setSize(const sp<SurfaceControl>&, uint32_t, uint32_t) { return *this; } 540 Transaction& setLayer(const sp<SurfaceControl>& sc, 541 int32_t z); 542 543 // Sets a Z order relative to the Surface specified by "relativeTo" but 544 // without becoming a full child of the relative. Z-ordering works exactly 545 // as if it were a child however. 546 // 547 // As a nod to sanity, only non-child surfaces may have a relative Z-order. 548 // 549 // This overrides any previous call and is overriden by any future calls 550 // to setLayer. 551 // 552 // If the relative is removed, the Surface will have no layer and be 553 // invisible, until the next time set(Relative)Layer is called. 554 Transaction& setRelativeLayer(const sp<SurfaceControl>& sc, 555 const sp<SurfaceControl>& relativeTo, int32_t z); 556 Transaction& setFlags(const sp<SurfaceControl>& sc, 557 uint32_t flags, uint32_t mask); 558 Transaction& setTransparentRegionHint(const sp<SurfaceControl>& sc, 559 const Region& transparentRegion); 560 Transaction& setDimmingEnabled(const sp<SurfaceControl>& sc, bool dimmingEnabled); 561 Transaction& setAlpha(const sp<SurfaceControl>& sc, 562 float alpha); 563 Transaction& setMatrix(const sp<SurfaceControl>& sc, 564 float dsdx, float dtdx, float dtdy, float dsdy); 565 Transaction& setCrop(const sp<SurfaceControl>& sc, const Rect& crop); 566 Transaction& setCrop(const sp<SurfaceControl>& sc, const FloatRect& crop); 567 Transaction& setCornerRadius(const sp<SurfaceControl>& sc, float cornerRadius); 568 Transaction& setBackgroundBlurRadius(const sp<SurfaceControl>& sc, 569 int backgroundBlurRadius); 570 Transaction& setBlurRegions(const sp<SurfaceControl>& sc, 571 const std::vector<BlurRegion>& regions); 572 Transaction& setLayerStack(const sp<SurfaceControl>&, ui::LayerStack); 573 Transaction& setMetadata(const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p); 574 575 /// Reparents the current layer to the new parent handle. The new parent must not be null. 576 Transaction& reparent(const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent); 577 578 Transaction& setColor(const sp<SurfaceControl>& sc, const half3& color); 579 580 // Sets the background color of a layer with the specified color, alpha, and dataspace 581 Transaction& setBackgroundColor(const sp<SurfaceControl>& sc, const half3& color, 582 float alpha, ui::Dataspace dataspace); 583 584 Transaction& setTransform(const sp<SurfaceControl>& sc, uint32_t transform); 585 Transaction& setTransformToDisplayInverse(const sp<SurfaceControl>& sc, 586 bool transformToDisplayInverse); 587 Transaction& setBuffer(const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer, 588 const std::optional<sp<Fence>>& fence = std::nullopt, 589 const std::optional<uint64_t>& frameNumber = std::nullopt, 590 uint32_t producerId = 0, ReleaseBufferCallback callback = nullptr, 591 nsecs_t dequeueTime = -1); 592 Transaction& unsetBuffer(const sp<SurfaceControl>& sc); 593 std::shared_ptr<BufferData> getAndClearBuffer(const sp<SurfaceControl>& sc); 594 595 /** 596 * If this transaction, has a a buffer set for the given SurfaceControl 597 * mark that buffer as ordered after a given barrierFrameNumber. 598 * 599 * SurfaceFlinger will refuse to apply this transaction until after 600 * the frame in barrierFrameNumber has been applied. This transaction may 601 * be applied in the same frame as the barrier buffer or after. 602 * 603 * This is only designed to be used to handle switches between multiple 604 * apply tokens, as explained in the comment for BLASTBufferQueue::mAppliedLastTransaction. 605 * 606 * Has to be called after setBuffer. 607 * 608 * WARNING: 609 * This API is very dangerous to the caller, as if you invoke it without 610 * a frameNumber you have not yet submitted, you can dead-lock your 611 * SurfaceControl's transaction queue. 612 */ 613 Transaction& setBufferHasBarrier(const sp<SurfaceControl>& sc, 614 uint64_t barrierFrameNumber); 615 Transaction& setDataspace(const sp<SurfaceControl>& sc, ui::Dataspace dataspace); 616 Transaction& setExtendedRangeBrightness(const sp<SurfaceControl>& sc, 617 float currentBufferRatio, float desiredRatio); 618 Transaction& setDesiredHdrHeadroom(const sp<SurfaceControl>& sc, float desiredRatio); 619 Transaction& setLuts(const sp<SurfaceControl>& sc, const base::unique_fd& lutFd, 620 const std::vector<int32_t>& offsets, 621 const std::vector<int32_t>& dimensions, 622 const std::vector<int32_t>& sizes, 623 const std::vector<int32_t>& samplingKeys); 624 Transaction& setCachingHint(const sp<SurfaceControl>& sc, gui::CachingHint cachingHint); 625 Transaction& setHdrMetadata(const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata); 626 Transaction& setSurfaceDamageRegion(const sp<SurfaceControl>& sc, 627 const Region& surfaceDamageRegion); 628 Transaction& setApi(const sp<SurfaceControl>& sc, int32_t api); 629 Transaction& setSidebandStream(const sp<SurfaceControl>& sc, 630 const sp<NativeHandle>& sidebandStream); 631 Transaction& setDesiredPresentTime(nsecs_t desiredPresentTime); 632 Transaction& setColorSpaceAgnostic(const sp<SurfaceControl>& sc, const bool agnostic); 633 634 // Sets information about the priority of the frame. 635 Transaction& setFrameRateSelectionPriority(const sp<SurfaceControl>& sc, int32_t priority); 636 637 Transaction& addTransactionCallback(TransactionCompletedCallbackTakesContext callback, 638 void* callbackContext, CallbackId::Type callbackType); 639 640 Transaction& addTransactionCompletedCallback( 641 TransactionCompletedCallbackTakesContext callback, void* callbackContext); 642 643 Transaction& addTransactionCommittedCallback( 644 TransactionCompletedCallbackTakesContext callback, void* callbackContext); 645 646 /** 647 * Set a callback to receive feedback about the presentation of a layer. 648 * When the layer is presented according to the passed in Thresholds, 649 * it is said to "enter the state", and receives the callback with true. 650 * When the conditions fall out of thresholds, it is then said to leave the 651 * state. 652 * 653 * There are a few simple thresholds: 654 * minAlpha: Lower bound on computed alpha 655 * minFractionRendered: Lower bounds on fraction of pixels that 656 * were rendered. 657 * stabilityThresholdMs: A time that alpha and fraction rendered 658 * must remain within bounds before we can "enter the state" 659 * 660 * The fraction of pixels rendered is a computation based on scale, crop 661 * and occlusion. The calculation may be somewhat counterintuitive, so we 662 * can work through an example. Imagine we have a layer with a 100x100 buffer 663 * which is occluded by (10x100) pixels on the left, and cropped by (100x10) pixels 664 * on the top. Furthermore imagine this layer is scaled by 0.9 in both dimensions. 665 * (c=crop,o=occluded,b=both,x=none 666 * b c c c 667 * o x x x 668 * o x x x 669 * o x x x 670 * 671 * We first start by computing fr=xscale*yscale=0.9*0.9=0.81, indicating 672 * that "81%" of the pixels were rendered. This corresponds to what was 100 673 * pixels being displayed in 81 pixels. This is somewhat of an abuse of 674 * language, as the information of merged pixels isn't totally lost, but 675 * we err on the conservative side. 676 * 677 * We then repeat a similar process for the crop and covered regions and 678 * accumulate the results: fr = fr * (fractionNotCropped) * (fractionNotCovered) 679 * So for this example we would get 0.9*0.9*0.9*0.9=0.65... 680 * 681 * Notice that this is not completely accurate, as we have double counted 682 * the region marked as b. However we only wanted a "lower bound" and so it 683 * is ok to err in this direction. Selection of the threshold will ultimately 684 * be somewhat arbitrary, and so there are some somewhat arbitrary decisions in 685 * this API as well. 686 * 687 * The caller must keep "PresentationCallbackRAII" alive, or the callback 688 * in SurfaceComposerClient will be unregistered. 689 */ 690 Transaction& setTrustedPresentationCallback(const sp<SurfaceControl>& sc, 691 TrustedPresentationCallback callback, 692 const TrustedPresentationThresholds& thresholds, 693 void* context, 694 sp<PresentationCallbackRAII>& outCallbackOwner); 695 696 // Clear local memory in SCC 697 Transaction& clearTrustedPresentationCallback(const sp<SurfaceControl>& sc); 698 699 // ONLY FOR BLAST ADAPTER 700 Transaction& notifyProducerDisconnect(const sp<SurfaceControl>& sc); 701 702 Transaction& setInputWindowInfo(const sp<SurfaceControl>& sc, 703 sp<gui::WindowInfoHandle> info); 704 Transaction& setFocusedWindow(const gui::FocusRequest& request); 705 706 Transaction& addWindowInfosReportedListener( 707 sp<gui::IWindowInfosReportedListener> windowInfosReportedListener); 708 709 // Set a color transform matrix on the given layer on the built-in display. 710 Transaction& setColorTransform(const sp<SurfaceControl>& sc, const mat3& matrix, 711 const vec3& translation); 712 713 Transaction& setGeometry(const sp<SurfaceControl>& sc, 714 const Rect& source, const Rect& dst, int transform); 715 Transaction& setShadowRadius(const sp<SurfaceControl>& sc, float cornerRadius); 716 717 Transaction& setFrameRate(const sp<SurfaceControl>& sc, float frameRate, 718 int8_t compatibility, int8_t changeFrameRateStrategy); 719 720 Transaction& setDefaultFrameRateCompatibility(const sp<SurfaceControl>& sc, 721 int8_t compatibility); 722 723 Transaction& setFrameRateCategory(const sp<SurfaceControl>& sc, int8_t category, 724 bool smoothSwitchOnly); 725 726 Transaction& setFrameRateSelectionStrategy(const sp<SurfaceControl>& sc, int8_t strategy); 727 728 // Set by window manager indicating the layer and all its children are 729 // in a different orientation than the display. The hint suggests that 730 // the graphic producers should receive a transform hint as if the 731 // display was in this orientation. When the display changes to match 732 // the layer orientation, the graphic producer may not need to allocate 733 // a buffer of a different size. 734 Transaction& setFixedTransformHint(const sp<SurfaceControl>& sc, int32_t transformHint); 735 736 // Sets the frame timeline vsync id received from choreographer that corresponds 737 // to the transaction, and the input event id that identifies the input event that caused 738 // the current frame. 739 Transaction& setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo); 740 741 // Indicates that the consumer should acquire the next frame as soon as it 742 // can and not wait for a frame to become available. This is only relevant 743 // in shared buffer mode. 744 Transaction& setAutoRefresh(const sp<SurfaceControl>& sc, bool autoRefresh); 745 746 // Sets that this surface control and its children are trusted overlays for input 747 Transaction& setTrustedOverlay(const sp<SurfaceControl>& sc, bool isTrustedOverlay); 748 Transaction& setTrustedOverlay(const sp<SurfaceControl>& sc, 749 gui::TrustedOverlay trustedOverlay); 750 751 // Queues up transactions using this token in SurfaceFlinger. By default, all transactions 752 // from a client are placed on the same queue. This can be used to prevent multiple 753 // transactions from blocking each other. 754 Transaction& setApplyToken(const sp<IBinder>& token); 755 756 /** 757 * Provides the stretch effect configured on a container that the 758 * surface is rendered within. 759 * @param sc target surface the stretch should be applied to 760 * @param stretchEffect the corresponding stretch effect to be applied 761 * to the surface. This can be directly on the surface itself or 762 * configured from a parent of the surface in which case the 763 * StretchEffect provided has parameters mapping the position of 764 * the surface within the container that has the stretch configured 765 * on it 766 * @return The transaction being constructed 767 */ 768 Transaction& setStretchEffect(const sp<SurfaceControl>& sc, 769 const StretchEffect& stretchEffect); 770 771 /** 772 * Provides the edge extension effect configured on a container that the 773 * surface is rendered within. 774 * @param sc target surface the edge extension should be applied to 775 * @param effect the corresponding EdgeExtensionParameters to be applied 776 * to the surface. 777 * @return The transaction being constructed 778 */ 779 Transaction& setEdgeExtensionEffect(const sp<SurfaceControl>& sc, 780 const gui::EdgeExtensionParameters& effect); 781 782 Transaction& setBufferCrop(const sp<SurfaceControl>& sc, const Rect& bufferCrop); 783 Transaction& setDestinationFrame(const sp<SurfaceControl>& sc, 784 const Rect& destinationFrame); 785 Transaction& setDropInputMode(const sp<SurfaceControl>& sc, gui::DropInputMode mode); 786 787 Transaction& setBufferReleaseChannel( 788 const sp<SurfaceControl>& sc, 789 const std::shared_ptr<gui::BufferReleaseChannel::ProducerEndpoint>& channel); 790 791 /** 792 * Configures a surface control to use picture processing hardware, configured as specified 793 * by the picture profile, to enhance the quality of all subsequent buffer contents. 794 */ 795 Transaction& setPictureProfileHandle(const sp<SurfaceControl>& sc, 796 const PictureProfileHandle& pictureProfileHandle); 797 798 /** 799 * Configures the relative importance of the contents of the layer with respect to the app's 800 * user experience. A lower priority value will give the layer preferred access to limited 801 * resources, such as picture processing, over a layer with a higher priority value. 802 */ 803 Transaction& setContentPriority(const sp<SurfaceControl>& sc, int32_t contentPriority); 804 805 status_t setDisplaySurface(const sp<IBinder>& token, 806 const sp<IGraphicBufferProducer>& bufferProducer); 807 808 void setDisplayLayerStack(const sp<IBinder>& token, ui::LayerStack); 809 810 void setDisplayFlags(const sp<IBinder>& token, uint32_t flags); 811 812 /* setDisplayProjection() defines the projection of layer stacks 813 * to a given display. 814 * 815 * - orientation defines the display's orientation. 816 * - layerStackRect defines which area of the window manager coordinate 817 * space will be used. 818 * - displayRect defines where on the display will layerStackRect be 819 * mapped to. displayRect is specified post-orientation, that is 820 * it uses the orientation seen by the end-user. 821 */ 822 void setDisplayProjection(const sp<IBinder>& token, ui::Rotation orientation, 823 const Rect& layerStackRect, const Rect& displayRect); 824 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height); 825 void setAnimationTransaction(); 826 void setEarlyWakeupStart(); 827 void setEarlyWakeupEnd(); 828 829 /** 830 * Strip the transaction of all permissioned requests, required when 831 * accepting transactions across process boundaries. 832 * 833 * TODO (b/213644870): Remove all permissioned things from Transaction 834 */ 835 void sanitize(int pid, int uid); 836 837 static sp<IBinder> getDefaultApplyToken(); 838 static void setDefaultApplyToken(sp<IBinder> applyToken); 839 840 static status_t sendSurfaceFlushJankDataTransaction(const sp<SurfaceControl>& sc); 841 void enableDebugLogCallPoints(); 842 }; 843 844 status_t clearLayerFrameStats(const sp<IBinder>& token) const; 845 status_t getLayerFrameStats(const sp<IBinder>& token, FrameStats* outStats) const; 846 static status_t clearAnimationFrameStats(); 847 static status_t getAnimationFrameStats(FrameStats* outStats); 848 849 static status_t overrideHdrTypes(const sp<IBinder>& display, 850 const std::vector<ui::Hdr>& hdrTypes); 851 852 static status_t onPullAtom(const int32_t atomId, std::string* outData, bool* success); 853 854 static void setDisplayProjection(const sp<IBinder>& token, ui::Rotation orientation, 855 const Rect& layerStackRect, const Rect& displayRect); 856 getClient()857 inline sp<ISurfaceComposerClient> getClient() { return mClient; } 858 859 static status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& display, 860 ui::PixelFormat* outFormat, 861 ui::Dataspace* outDataspace, 862 uint8_t* outComponentMask); 863 static status_t setDisplayContentSamplingEnabled(const sp<IBinder>& display, bool enable, 864 uint8_t componentMask, uint64_t maxFrames); 865 866 static status_t getDisplayedContentSample(const sp<IBinder>& display, uint64_t maxFrames, 867 uint64_t timestamp, DisplayedFrameStats* outStats); 868 static status_t addRegionSamplingListener(const Rect& samplingArea, 869 const sp<IBinder>& stopLayerHandle, 870 const sp<IRegionSamplingListener>& listener); 871 static status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener); 872 static status_t addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener); 873 static status_t removeFpsListener(const sp<gui::IFpsListener>& listener); 874 static status_t addTunnelModeEnabledListener( 875 const sp<gui::ITunnelModeEnabledListener>& listener); 876 static status_t removeTunnelModeEnabledListener( 877 const sp<gui::ITunnelModeEnabledListener>& listener); 878 879 status_t addWindowInfosListener( 880 const sp<gui::WindowInfosListener>& windowInfosListener, 881 std::pair<std::vector<gui::WindowInfo>, std::vector<gui::DisplayInfo>>* outInitialInfo = 882 nullptr); 883 status_t removeWindowInfosListener(const sp<gui::WindowInfosListener>& windowInfosListener); 884 885 static void notifyShutdown(); 886 887 protected: 888 ReleaseCallbackThread mReleaseCallbackThread; 889 890 private: 891 // Get dynamic information about given physical display from token 892 static status_t getDynamicDisplayInfoFromToken(const sp<IBinder>& display, 893 ui::DynamicDisplayInfo*); 894 895 static void getDynamicDisplayInfoInternal(gui::DynamicDisplayInfo& ginfo, 896 ui::DynamicDisplayInfo*& outInfo); 897 virtual void onFirstRef(); 898 899 mutable Mutex mLock; 900 status_t mStatus; 901 sp<ISurfaceComposerClient> mClient; 902 }; 903 904 // --------------------------------------------------------------------------- 905 906 class ScreenshotClient { 907 public: 908 static status_t captureDisplay(const DisplayCaptureArgs&, const sp<IScreenCaptureListener>&); 909 static status_t captureDisplay(DisplayId, const gui::CaptureArgs&, 910 const sp<IScreenCaptureListener>&); 911 static status_t captureLayers(const LayerCaptureArgs&, const sp<IScreenCaptureListener>&, 912 bool sync); 913 captureDisplay(DisplayId id,const sp<IScreenCaptureListener> & listener)914 [[deprecated]] static status_t captureDisplay(DisplayId id, 915 const sp<IScreenCaptureListener>& listener) { 916 return captureDisplay(id, gui::CaptureArgs(), listener); 917 } 918 }; 919 920 // --------------------------------------------------------------------------- 921 922 class JankDataListener; 923 924 // Acts as a representative listener to the composer for a single layer and 925 // forwards any received jank data to multiple listeners. Will remove itself 926 // from the composer only once the last listener is removed. 927 class JankDataListenerFanOut : public gui::BnJankListener { 928 public: JankDataListenerFanOut(int32_t layerId)929 JankDataListenerFanOut(int32_t layerId) : mLayerId(layerId) {} 930 931 binder::Status onJankData(const std::vector<gui::JankData>& jankData) override; 932 933 static status_t addListener(sp<SurfaceControl> sc, sp<JankDataListener> listener); 934 static status_t removeListener(sp<JankDataListener> listener); 935 936 private: 937 std::vector<sp<JankDataListener>> getActiveListeners(); 938 bool removeListeners(const std::vector<wp<JankDataListener>>& listeners); 939 int64_t updateAndGetRemovalVSync(); 940 941 struct WpJDLHash { operatorWpJDLHash942 std::size_t operator()(const wp<JankDataListener>& listener) const { 943 return std::hash<JankDataListener*>{}(listener.unsafe_get()); 944 } 945 }; 946 947 std::mutex mMutex; 948 std::unordered_set<wp<JankDataListener>, WpJDLHash> mListeners GUARDED_BY(mMutex); 949 int32_t mLayerId; 950 int64_t mRemoveAfter = -1; 951 952 static std::mutex sFanoutInstanceMutex; 953 static std::unordered_map<int32_t, sp<JankDataListenerFanOut>> sFanoutInstances; 954 }; 955 956 // Base class for client listeners interested in jank classification data from 957 // the composer. Subclasses should override onJankDataAvailable and call the add 958 // and removal methods to receive jank data. 959 class JankDataListener : public virtual RefBase { 960 public: JankDataListener()961 JankDataListener() {} 962 virtual ~JankDataListener(); 963 964 virtual bool onJankDataAvailable(const std::vector<gui::JankData>& jankData) = 0; 965 addListener(sp<SurfaceControl> sc)966 status_t addListener(sp<SurfaceControl> sc) { 967 if (mLayerId != -1) { 968 removeListener(0); 969 mLayerId = -1; 970 } 971 972 int32_t layerId = sc->getLayerId(); 973 status_t status = 974 JankDataListenerFanOut::addListener(std::move(sc), 975 sp<JankDataListener>::fromExisting(this)); 976 if (status == OK) { 977 mLayerId = layerId; 978 } 979 return status; 980 } 981 removeListener(int64_t afterVsync)982 status_t removeListener(int64_t afterVsync) { 983 mRemoveAfter = std::max(static_cast<int64_t>(0), afterVsync); 984 return JankDataListenerFanOut::removeListener(sp<JankDataListener>::fromExisting(this)); 985 } 986 987 status_t flushJankData(); 988 989 friend class JankDataListenerFanOut; 990 991 private: 992 int32_t mLayerId = -1; 993 int64_t mRemoveAfter = -1; 994 }; 995 996 // --------------------------------------------------------------------------- 997 998 class TransactionCompletedListener : public BnTransactionCompletedListener { 999 public: 1000 TransactionCompletedListener(); 1001 1002 protected: 1003 int64_t getNextIdLocked() REQUIRES(mMutex); 1004 1005 std::mutex mMutex; 1006 1007 // This lock needs to be recursive so we can unregister a callback from within that callback. 1008 std::recursive_mutex mSurfaceStatsListenerMutex; 1009 1010 bool mListening GUARDED_BY(mMutex) = false; 1011 1012 int64_t mCallbackIdCounter GUARDED_BY(mMutex) = 1; 1013 struct CallbackTranslation { 1014 TransactionCompletedCallback callbackFunction; 1015 std::unordered_map<sp<IBinder>, sp<SurfaceControl>, SurfaceComposerClient::IBinderHash> 1016 surfaceControls; 1017 }; 1018 1019 struct SurfaceStatsCallbackEntry { SurfaceStatsCallbackEntrySurfaceStatsCallbackEntry1020 SurfaceStatsCallbackEntry(void* context, void* cookie, SurfaceStatsCallback callback) 1021 : context(context), 1022 cookie(cookie), 1023 callback(callback) {} 1024 1025 void* context; 1026 void* cookie; 1027 SurfaceStatsCallback callback; 1028 }; 1029 1030 std::unordered_map<CallbackId, CallbackTranslation, CallbackIdHash> mCallbacks 1031 GUARDED_BY(mMutex); 1032 std::unordered_map<ReleaseCallbackId, ReleaseBufferCallback, ReleaseBufferCallbackIdHash> 1033 mReleaseBufferCallbacks GUARDED_BY(mMutex); 1034 1035 // This is protected by mSurfaceStatsListenerMutex, but GUARDED_BY isn't supported for 1036 // std::recursive_mutex 1037 std::multimap<int32_t, SurfaceStatsCallbackEntry> mSurfaceStatsListeners; 1038 std::unordered_map<void*, std::function<void(const std::string&)>> mQueueStallListeners; 1039 1040 std::unordered_map<int, std::tuple<TrustedPresentationCallback, void*>> 1041 mTrustedPresentationCallbacks; 1042 1043 public: 1044 static sp<TransactionCompletedListener> getInstance(); 1045 static sp<ITransactionCompletedListener> getIInstance(); 1046 1047 void startListeningLocked() REQUIRES(mMutex); 1048 1049 CallbackId addCallbackFunction( 1050 const TransactionCompletedCallback& callbackFunction, 1051 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>& 1052 surfaceControls, 1053 CallbackId::Type callbackType); 1054 1055 void addSurfaceControlToCallbacks( 1056 const sp<SurfaceControl>& surfaceControl, 1057 const std::unordered_set<CallbackId, CallbackIdHash>& callbackIds); 1058 1059 void addQueueStallListener(std::function<void(const std::string&)> stallListener, void* id); 1060 void removeQueueStallListener(void *id); 1061 1062 sp<SurfaceComposerClient::PresentationCallbackRAII> addTrustedPresentationCallback( 1063 TrustedPresentationCallback tpc, int id, void* context); 1064 void clearTrustedPresentationCallback(int id); 1065 1066 void addSurfaceStatsListener(void* context, void* cookie, sp<SurfaceControl> surfaceControl, 1067 SurfaceStatsCallback listener); 1068 void removeSurfaceStatsListener(void* context, void* cookie); 1069 1070 void setReleaseBufferCallback(const ReleaseCallbackId&, ReleaseBufferCallback); 1071 1072 // BnTransactionCompletedListener overrides 1073 void onTransactionCompleted(ListenerStats stats) override; 1074 void onReleaseBuffer(ReleaseCallbackId, sp<Fence> releaseFence, 1075 uint32_t currentMaxAcquiredBufferCount) override; 1076 1077 void removeReleaseBufferCallback(const ReleaseCallbackId& callbackId); 1078 1079 // For Testing Only 1080 static void setInstance(const sp<TransactionCompletedListener>&); 1081 1082 void onTransactionQueueStalled(const String8& reason) override; 1083 1084 void onTrustedPresentationChanged(int id, bool presentedWithinThresholds) override; 1085 1086 private: 1087 ReleaseBufferCallback popReleaseBufferCallbackLocked(const ReleaseCallbackId&) REQUIRES(mMutex); 1088 static sp<TransactionCompletedListener> sInstance; 1089 }; 1090 1091 } // namespace android 1092