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