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