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