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