blob: f366c2935d4308cc3818fec4088943ddf78ea0e7 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080017#pragma once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
19#include <stdint.h>
20#include <sys/types.h>
Marissa Wallc837b5e2018-10-12 10:04:44 -070021#include <set>
chaviw8e3fe5d2018-02-22 10:55:42 -080022#include <unordered_map>
Marissa Wallc837b5e2018-10-12 10:04:44 -070023#include <unordered_set>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
Mathias Agopiandd3423c2009-09-23 15:44:05 -070025#include <binder/IBinder.h>
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/RefBase.h>
Mathias Agopianb7e930d2010-06-01 15:12:58 -070028#include <utils/Singleton.h>
29#include <utils/SortedVector.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <utils/threads.h>
31
Daniel Solomon42d04562019-01-20 21:03:19 -080032#include <ui/ConfigStoreTypes.h>
Kevin DuBois1d4249a2018-08-29 10:45:14 -070033#include <ui/DisplayedFrameStats.h>
Svetoslavd85084b2014-03-20 10:28:31 -070034#include <ui/FrameStats.h>
Peiyong Lin9f034472018-03-28 15:29:00 -070035#include <ui/GraphicTypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <ui/PixelFormat.h>
Dominik Laskowski718f9602019-11-09 20:01:35 -080037#include <ui/Rotation.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080038
Mathias Agopianabe815d2013-03-19 22:22:21 -070039#include <gui/CpuConsumer.h>
Robert Carr866455f2019-04-02 16:28:26 -070040#include <gui/ISurfaceComposer.h>
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070041#include <gui/ITransactionCompletedListener.h>
42#include <gui/LayerState.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043#include <gui/SurfaceControl.h>
chaviw13fdc492017-06-27 12:40:18 -070044#include <math/vec3.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46namespace android {
47
Dan Stozac4f471e2016-03-24 09:31:08 -070048class HdrCapabilities;
Mathias Agopian41f673c2011-11-17 17:48:35 -080049class ISurfaceComposerClient;
Andy McFadden2adaf042012-12-18 09:49:45 -080050class IGraphicBufferProducer;
Kevin DuBois00c66832019-02-18 16:21:31 -080051class IRegionSamplingListener;
Mathias Agopiana67932f2011-04-20 14:20:59 -070052class Region;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070053
Marissa Wall80d94ad2019-01-18 16:04:36 -080054struct SurfaceControlStats {
Valerie Hau871d6352020-01-29 08:44:02 -080055 SurfaceControlStats(const sp<SurfaceControl>& sc, nsecs_t latchTime, nsecs_t acquireTime,
56 const sp<Fence>& presentFence, const sp<Fence>& prevReleaseFence,
57 uint32_t hint, FrameEventHistoryStats eventStats)
Valerie Hau32cdc1f2019-10-21 14:45:54 -070058 : surfaceControl(sc),
Valerie Hau871d6352020-01-29 08:44:02 -080059 latchTime(latchTime),
60 acquireTime(acquireTime),
61 presentFence(presentFence),
Valerie Hau32cdc1f2019-10-21 14:45:54 -070062 previousReleaseFence(prevReleaseFence),
Valerie Hau871d6352020-01-29 08:44:02 -080063 transformHint(hint),
64 frameEventStats(eventStats) {}
Marissa Wallc837b5e2018-10-12 10:04:44 -070065
Marissa Wall80d94ad2019-01-18 16:04:36 -080066 sp<SurfaceControl> surfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -080067 nsecs_t latchTime = -1;
Marissa Wall80d94ad2019-01-18 16:04:36 -080068 nsecs_t acquireTime = -1;
Valerie Hau871d6352020-01-29 08:44:02 -080069 sp<Fence> presentFence;
Marissa Wall80d94ad2019-01-18 16:04:36 -080070 sp<Fence> previousReleaseFence;
Valerie Hau32cdc1f2019-10-21 14:45:54 -070071 uint32_t transformHint = 0;
Valerie Hau871d6352020-01-29 08:44:02 -080072 FrameEventHistoryStats frameEventStats;
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070073};
74
Marissa Wall80d94ad2019-01-18 16:04:36 -080075using TransactionCompletedCallbackTakesContext =
76 std::function<void(void* /*context*/, nsecs_t /*latchTime*/,
77 const sp<Fence>& /*presentFence*/,
78 const std::vector<SurfaceControlStats>& /*stats*/)>;
79using TransactionCompletedCallback =
80 std::function<void(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
81 const std::vector<SurfaceControlStats>& /*stats*/)>;
82
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070083// ---------------------------------------------------------------------------
84
Mathias Agopiand4784a32010-05-27 19:41:15 -070085class SurfaceComposerClient : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086{
Mathias Agopian698c0872011-06-28 19:09:31 -070087 friend class Composer;
Jesse Hall6c913be2013-08-08 12:15:49 -070088public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 SurfaceComposerClient();
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +010090 SurfaceComposerClient(const sp<ISurfaceComposerClient>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 virtual ~SurfaceComposerClient();
92
93 // Always make sure we could initialize
94 status_t initCheck() const;
95
96 // Return the connection of this client
97 sp<IBinder> connection() const;
Jesse Hall6c913be2013-08-08 12:15:49 -070098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 // Forcibly remove connection before all references have gone away.
100 void dispose();
101
Mathias Agopiane57f2922012-08-09 16:29:12 -0700102 // callback when the composer is dies
103 status_t linkToComposerDeath(const sp<IBinder::DeathRecipient>& recipient,
Yi Konga03e0442018-07-17 11:16:57 -0700104 void* cookie = nullptr, uint32_t flags = 0);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700105
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800106 // Get transactional state of given display.
107 static status_t getDisplayState(const sp<IBinder>& display, ui::DisplayState*);
Dan Stoza7f7da322014-05-02 15:26:25 -0700108
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800109 // Get immutable information about given physical display.
110 static status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo*);
Dan Stoza7f7da322014-05-02 15:26:25 -0700111
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800112 // Get configurations supported by given physical display.
113 static status_t getDisplayConfigs(const sp<IBinder>& display, Vector<DisplayConfig>*);
114
115 // Get the ID of the active DisplayConfig, as getDisplayConfigs index.
Dan Stoza7f7da322014-05-02 15:26:25 -0700116 static int getActiveConfig(const sp<IBinder>& display);
117
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800118 // Shorthand for getDisplayConfigs element at getActiveConfig index.
119 static status_t getActiveDisplayConfig(const sp<IBinder>& display, DisplayConfig*);
120
Steven Thomasf734df42020-04-13 21:09:28 -0700121 // Sets the refresh rate boundaries for the display.
Ana Krulec0782b882019-10-15 17:34:54 -0700122 static status_t setDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Steven Thomasf734df42020-04-13 21:09:28 -0700123 int32_t defaultConfig, float primaryRefreshRateMin,
124 float primaryRefreshRateMax,
125 float appRequestRefreshRateMin,
126 float appRequestRefreshRateMax);
127 // Gets the refresh rate boundaries for the display.
Ana Krulec234bb162019-11-10 22:55:55 +0100128 static status_t getDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100129 int32_t* outDefaultConfig,
Steven Thomasf734df42020-04-13 21:09:28 -0700130 float* outPrimaryRefreshRateMin,
131 float* outPrimaryRefreshRateMax,
132 float* outAppRequestRefreshRateMin,
133 float* outAppRequestRefreshRateMax);
Ana Krulec0782b882019-10-15 17:34:54 -0700134
Michael Wright28f24d02016-07-12 13:30:53 -0700135 // Gets the list of supported color modes for the given display
136 static status_t getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lin9f034472018-03-28 15:29:00 -0700137 Vector<ui::ColorMode>* outColorModes);
Michael Wright28f24d02016-07-12 13:30:53 -0700138
Daniel Solomon42d04562019-01-20 21:03:19 -0800139 // Get the coordinates of the display's native color primaries
140 static status_t getDisplayNativePrimaries(const sp<IBinder>& display,
141 ui::DisplayPrimaries& outPrimaries);
142
Michael Wright28f24d02016-07-12 13:30:53 -0700143 // Gets the active color mode for the given display
Peiyong Lin9f034472018-03-28 15:29:00 -0700144 static ui::ColorMode getActiveColorMode(const sp<IBinder>& display);
Michael Wright28f24d02016-07-12 13:30:53 -0700145
146 // Sets the active color mode for the given display
Peiyong Lin9f034472018-03-28 15:29:00 -0700147 static status_t setActiveColorMode(const sp<IBinder>& display,
148 ui::ColorMode colorMode);
Michael Wright28f24d02016-07-12 13:30:53 -0700149
Galia Peycheva5492cb52019-10-30 14:13:16 +0100150 // Reports whether the connected display supports Auto Low Latency Mode
151 static bool getAutoLowLatencyModeSupport(const sp<IBinder>& display);
152
153 // Switches on/off Auto Low Latency Mode on the connected display. This should only be
154 // called if the connected display supports Auto Low Latency Mode as reported by
155 // #getAutoLowLatencyModeSupport
156 static void setAutoLowLatencyMode(const sp<IBinder>& display, bool on);
157
158 // Reports whether the connected display supports Game content type
159 static bool getGameContentTypeSupport(const sp<IBinder>& display);
160
161 // Turns Game mode on/off on the connected display. This should only be called
162 // if the display supports Game content type, as reported by #getGameContentTypeSupport
163 static void setGameContentType(const sp<IBinder>& display, bool on);
164
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700165 /* Triggers screen on/off or low power mode and waits for it to complete */
166 static void setDisplayPowerMode(const sp<IBinder>& display, int mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700167
Peiyong Linc6780972018-10-28 15:24:08 -0700168 /* Returns the composition preference of the default data space and default pixel format,
169 * as well as the wide color gamut data space and wide color gamut pixel format.
170 * If the wide color gamut data space is V0_SRGB, then it implies that the platform
171 * has no wide color gamut support.
172 */
173 static status_t getCompositionPreference(ui::Dataspace* defaultDataspace,
174 ui::PixelFormat* defaultPixelFormat,
175 ui::Dataspace* wideColorGamutDataspace,
176 ui::PixelFormat* wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -0700177
Peiyong Lin08d10512019-01-16 13:27:35 -0800178 /*
179 * Gets whether SurfaceFlinger can support protected content in GPU composition.
180 * Requires the ACCESS_SURFACE_FLINGER permission.
181 */
182 static bool getProtectedContentSupport();
183
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800184 /**
Marissa Wall78b72202019-03-15 14:58:34 -0700185 * Uncaches a buffer in ISurfaceComposer. It must be uncached via a transaction so that it is
186 * in order with other transactions that use buffers.
187 */
188 static void doUncacheBufferTransaction(uint64_t cacheId);
189
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800190 // Queries whether a given display is wide color display.
191 static status_t isWideColorDisplay(const sp<IBinder>& display, bool* outIsWideColorDisplay);
192
Dan Gittik57e63c52019-01-18 16:37:54 +0000193 /*
194 * Returns whether brightness operations are supported on a display.
195 *
196 * displayToken
197 * The token of the display.
198 *
199 * Returns whether brightness operations are supported on a display or not.
200 */
201 static bool getDisplayBrightnessSupport(const sp<IBinder>& displayToken);
202
203 /*
204 * Sets the brightness of a display.
205 *
206 * displayToken
207 * The token of the display whose brightness is set.
208 * brightness
209 * A number between 0.0 (minimum brightness) and 1.0 (maximum brightness), or -1.0f to
210 * turn the backlight off.
211 *
212 * Returns NO_ERROR upon success. Otherwise,
213 * NAME_NOT_FOUND if the display handle is invalid, or
214 * BAD_VALUE if the brightness value is invalid, or
215 * INVALID_OPERATION if brightness operaetions are not supported.
216 */
217 static status_t setDisplayBrightness(const sp<IBinder>& displayToken, float brightness);
218
Ady Abraham8532d012019-05-08 14:50:56 -0700219 /*
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100220 * Sends a power boost to the composer. This function is asynchronous.
Ady Abraham8532d012019-05-08 14:50:56 -0700221 *
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100222 * boostId
223 * boost id according to android::hardware::power::Boost
Ady Abraham8532d012019-05-08 14:50:56 -0700224 *
225 * Returns NO_ERROR upon success.
226 */
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100227 static status_t notifyPowerBoost(int32_t boostId);
Ady Abraham8532d012019-05-08 14:50:56 -0700228
Vishnu Nairb13bb952019-11-15 10:24:08 -0800229 /*
230 * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows
231 * material design guidelines.
232 *
233 * ambientColor
234 * Color to the ambient shadow. The alpha is premultiplied.
235 *
236 * spotColor
237 * Color to the spot shadow. The alpha is premultiplied. The position of the spot shadow
238 * depends on the light position.
239 *
240 * lightPosY/lightPosZ
241 * Position of the light used to cast the spot shadow. The X value is always the display
242 * width / 2.
243 *
244 * lightRadius
245 * Radius of the light casting the shadow.
246 */
247 static status_t setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor,
248 float lightPosY, float lightPosZ, float lightRadius);
249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 // ------------------------------------------------------------------------
251 // surface creation / destruction
252
Robert Carrfb4d58b2019-01-15 09:21:27 -0800253 static sp<SurfaceComposerClient> getDefault();
254
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 //! Create a surface
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800256 sp<SurfaceControl> createSurface(const String8& name, // name of the surface
257 uint32_t w, // width in pixel
258 uint32_t h, // height in pixel
259 PixelFormat format, // pixel-format desired
260 uint32_t flags = 0, // usage flags
261 SurfaceControl* parent = nullptr, // parent
Valerie Hau1acd6962019-10-28 16:35:48 -0700262 LayerMetadata metadata = LayerMetadata(), // metadata
263 uint32_t* outTransformHint = nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800265 status_t createSurfaceChecked(const String8& name, // name of the surface
266 uint32_t w, // width in pixel
267 uint32_t h, // height in pixel
268 PixelFormat format, // pixel-format desired
269 sp<SurfaceControl>* outSurface,
Valerie Hau1acd6962019-10-28 16:35:48 -0700270 uint32_t flags = 0, // usage flags
271 SurfaceControl* parent = nullptr, // parent
272 LayerMetadata metadata = LayerMetadata(), // metadata
273 uint32_t* outTransformHint = nullptr);
Marissa Wall35187b32019-01-08 10:08:52 -0800274
275 //! Create a surface
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800276 sp<SurfaceControl> createWithSurfaceParent(const String8& name, // name of the surface
277 uint32_t w, // width in pixel
278 uint32_t h, // height in pixel
279 PixelFormat format, // pixel-format desired
280 uint32_t flags = 0, // usage flags
281 Surface* parent = nullptr, // parent
Valerie Hau1acd6962019-10-28 16:35:48 -0700282 LayerMetadata metadata = LayerMetadata(), // metadata
283 uint32_t* outTransformHint = nullptr);
Robert Carr3b382ed2018-03-14 13:49:41 -0700284
chaviwfe94a222019-08-21 13:52:59 -0700285 // Creates a mirrored hierarchy for the mirrorFromSurface. This returns a SurfaceControl
286 // which is a parent of the root of the mirrored hierarchy.
287 //
288 // Real Hierarchy Mirror
289 // SC (value that's returned)
290 // |
291 // A A'
292 // | |
293 // B B'
294 sp<SurfaceControl> mirrorSurface(SurfaceControl* mirrorFromSurface);
295
Jesse Hall6c913be2013-08-08 12:15:49 -0700296 //! Create a virtual display
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700297 static sp<IBinder> createDisplay(const String8& displayName, bool secure);
Mathias Agopian285dbde2010-03-01 16:09:43 -0800298
Jesse Hall6c913be2013-08-08 12:15:49 -0700299 //! Destroy a virtual display
300 static void destroyDisplay(const sp<IBinder>& display);
301
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800302 //! Get stable IDs for connected physical displays
303 static std::vector<PhysicalDisplayId> getPhysicalDisplayIds();
304 static std::optional<PhysicalDisplayId> getInternalDisplayId();
305
306 //! Get token for a physical display given its stable ID
307 static sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId);
308 static sp<IBinder> getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700309
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700310 static status_t enableVSyncInjections(bool enable);
311
312 static status_t injectVSync(nsecs_t when);
313
chaviw8e3fe5d2018-02-22 10:55:42 -0800314 struct SCHash {
315 std::size_t operator()(const sp<SurfaceControl>& sc) const {
316 return std::hash<SurfaceControl *>{}(sc.get());
317 }
318 };
319
Vishnu Nairf03652d2019-07-16 17:56:56 -0700320 struct IBinderHash {
321 std::size_t operator()(const sp<IBinder>& iBinder) const {
322 return std::hash<IBinder*>{}(iBinder.get());
323 }
324 };
325
Marissa Wallc837b5e2018-10-12 10:04:44 -0700326 struct TCLHash {
327 std::size_t operator()(const sp<ITransactionCompletedListener>& tcl) const {
328 return std::hash<IBinder*>{}((tcl) ? IInterface::asBinder(tcl).get() : nullptr);
329 }
330 };
331
332 struct CallbackInfo {
333 // All the callbacks that have been requested for a TransactionCompletedListener in the
334 // Transaction
335 std::unordered_set<CallbackId> callbackIds;
336 // All the SurfaceControls that have been modified in this TransactionCompletedListener's
337 // process that require a callback if there is one or more callbackIds set.
338 std::unordered_set<sp<SurfaceControl>, SCHash> surfaceControls;
339 };
340
Valerie Hau2b0cd8d2019-08-26 10:30:52 -0700341 class Transaction : public Parcelable {
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000342 private:
343 static std::atomic<uint32_t> idCounter;
344 int64_t generateId();
345
Valerie Hau9dab9732019-08-20 09:29:25 -0700346 protected:
Vishnu Nairf03652d2019-07-16 17:56:56 -0700347 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> mComposerStates;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000348 SortedVector<DisplayState> mDisplayStates;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700349 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash>
350 mListenerCallbacks;
351
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000352 uint64_t mId;
353
Ady Abrahambf1349c2020-06-12 14:26:18 -0700354 uint32_t mForceSynchronous = 0;
355 uint32_t mTransactionNestCount = 0;
356 bool mAnimation = false;
357 bool mEarlyWakeup = false;
358 bool mExplicitEarlyWakeupStart = false;
359 bool mExplicitEarlyWakeupEnd = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700360
Marissa Wall78b72202019-03-15 14:58:34 -0700361 // Indicates that the Transaction contains a buffer that should be cached
362 bool mContainsBuffer = false;
363
Marissa Wall17b4e452018-12-26 16:32:34 -0800364 // mDesiredPresentTime is the time in nanoseconds that the client would like the transaction
365 // to be presented. When it is not possible to present at exactly that time, it will be
366 // presented after the time has passed.
367 //
368 // Desired present times that are more than 1 second in the future may be ignored.
369 // When a desired present time has already passed, the transaction will be presented as soon
370 // as possible.
371 //
372 // Transactions from the same process are presented in the same order that they are applied.
373 // The desired present time does not affect this ordering.
374 int64_t mDesiredPresentTime = -1;
375
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700376 // The vsync Id provided by Choreographer.getVsyncId
377 int64_t mFrameTimelineVsyncId = ISurfaceComposer::INVALID_VSYNC_ID;
378
chaviw273171b2018-12-26 11:46:30 -0800379 InputWindowCommands mInputWindowCommands;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700380 int mStatus = NO_ERROR;
381
Pablo Gamitodbc31672020-09-01 18:28:58 +0000382 layer_state_t* getLayerState(const sp<SurfaceControl>& sc);
chaviw763ef572018-02-22 16:04:57 -0800383 DisplayState& getDisplayState(const sp<IBinder>& token);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700384
Marissa Wall78b72202019-03-15 14:58:34 -0700385 void cacheBuffers();
Marissa Wallc837b5e2018-10-12 10:04:44 -0700386 void registerSurfaceControlForCallback(const sp<SurfaceControl>& sc);
387
Robert Carr4cdc58f2017-08-23 14:22:20 -0700388 public:
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000389 Transaction();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700390 virtual ~Transaction() = default;
391 Transaction(Transaction const& other);
392
Vishnu Nair621102e2019-06-12 14:16:57 -0700393 // Factory method that creates a new Transaction instance from the parcel.
394 static std::unique_ptr<Transaction> createFromParcel(const Parcel* parcel);
395
396 status_t writeToParcel(Parcel* parcel) const override;
397 status_t readFromParcel(const Parcel* parcel) override;
398
Vishnu Nairfef244e2019-06-17 18:07:51 -0700399 // Clears the contents of the transaction without applying it.
400 void clear();
401
Robert Carr4cdc58f2017-08-23 14:22:20 -0700402 status_t apply(bool synchronous = false);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700403 // Merge another transaction in to this one, clearing other
404 // as if it had been applied.
405 Transaction& merge(Transaction&& other);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700406 Transaction& show(const sp<SurfaceControl>& sc);
407 Transaction& hide(const sp<SurfaceControl>& sc);
408 Transaction& setPosition(const sp<SurfaceControl>& sc,
409 float x, float y);
410 Transaction& setSize(const sp<SurfaceControl>& sc,
411 uint32_t w, uint32_t h);
412 Transaction& setLayer(const sp<SurfaceControl>& sc,
413 int32_t z);
414
415 // Sets a Z order relative to the Surface specified by "relativeTo" but
416 // without becoming a full child of the relative. Z-ordering works exactly
417 // as if it were a child however.
418 //
419 // As a nod to sanity, only non-child surfaces may have a relative Z-order.
420 //
421 // This overrides any previous call and is overriden by any future calls
422 // to setLayer.
423 //
424 // If the relative is removed, the Surface will have no layer and be
425 // invisible, until the next time set(Relative)Layer is called.
426 Transaction& setRelativeLayer(const sp<SurfaceControl>& sc,
Pablo Gamito11dcc222020-09-12 15:49:39 +0000427 const sp<SurfaceControl>& relativeTo, int32_t z);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700428 Transaction& setFlags(const sp<SurfaceControl>& sc,
429 uint32_t flags, uint32_t mask);
430 Transaction& setTransparentRegionHint(const sp<SurfaceControl>& sc,
431 const Region& transparentRegion);
432 Transaction& setAlpha(const sp<SurfaceControl>& sc,
433 float alpha);
434 Transaction& setMatrix(const sp<SurfaceControl>& sc,
435 float dsdx, float dtdx, float dtdy, float dsdy);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700436 Transaction& setCrop_legacy(const sp<SurfaceControl>& sc, const Rect& crop);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700437 Transaction& setCornerRadius(const sp<SurfaceControl>& sc, float cornerRadius);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800438 Transaction& setBackgroundBlurRadius(const sp<SurfaceControl>& sc,
439 int backgroundBlurRadius);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700440 Transaction& setLayerStack(const sp<SurfaceControl>& sc, uint32_t layerStack);
Garfield Tan01a56132019-08-05 16:44:21 -0700441 Transaction& setMetadata(const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700442 // Defers applying any changes made in this transaction until the Layer
443 // identified by handle reaches the given frameNumber. If the Layer identified
444 // by handle is removed, then we will apply this transaction regardless of
445 // what frame number has been reached.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700446 Transaction& deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
Pablo Gamito11dcc222020-09-12 15:49:39 +0000447 const sp<SurfaceControl>& barrierSurfaceControl,
448 uint64_t frameNumber);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700449 // A variant of deferTransactionUntil_legacy which identifies the Layer we wait for by
Robert Carr4cdc58f2017-08-23 14:22:20 -0700450 // Surface instead of Handle. Useful for clients which may not have the
451 // SurfaceControl for some of their Surfaces. Otherwise behaves identically.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700452 Transaction& deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
453 const sp<Surface>& barrierSurface,
454 uint64_t frameNumber);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700455 // Reparents all children of this layer to the new parent handle.
456 Transaction& reparentChildren(const sp<SurfaceControl>& sc,
Pablo Gamito11dcc222020-09-12 15:49:39 +0000457 const sp<SurfaceControl>& newParent);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700458
459 /// Reparents the current layer to the new parent handle. The new parent must not be null.
460 // This can be used instead of reparentChildren if the caller wants to
461 // only re-parent a specific child.
Pablo Gamito11dcc222020-09-12 15:49:39 +0000462 Transaction& reparent(const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700463
464 Transaction& setColor(const sp<SurfaceControl>& sc, const half3& color);
465
Valerie Haudd0b7572019-01-29 14:59:27 -0800466 // Sets the background color of a layer with the specified color, alpha, and dataspace
467 Transaction& setBackgroundColor(const sp<SurfaceControl>& sc, const half3& color,
468 float alpha, ui::Dataspace dataspace);
Valerie Haued54efa2019-01-11 20:03:14 -0800469
Marissa Wall61c58622018-07-18 10:12:20 -0700470 Transaction& setTransform(const sp<SurfaceControl>& sc, uint32_t transform);
471 Transaction& setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
472 bool transformToDisplayInverse);
473 Transaction& setCrop(const sp<SurfaceControl>& sc, const Rect& crop);
Marissa Wall861616d2018-10-22 12:52:23 -0700474 Transaction& setFrame(const sp<SurfaceControl>& sc, const Rect& frame);
Marissa Wall61c58622018-07-18 10:12:20 -0700475 Transaction& setBuffer(const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer);
Marissa Wallebc2c052019-01-16 19:16:55 -0800476 Transaction& setCachedBuffer(const sp<SurfaceControl>& sc, int32_t bufferId);
Marissa Wall61c58622018-07-18 10:12:20 -0700477 Transaction& setAcquireFence(const sp<SurfaceControl>& sc, const sp<Fence>& fence);
478 Transaction& setDataspace(const sp<SurfaceControl>& sc, ui::Dataspace dataspace);
479 Transaction& setHdrMetadata(const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata);
480 Transaction& setSurfaceDamageRegion(const sp<SurfaceControl>& sc,
481 const Region& surfaceDamageRegion);
482 Transaction& setApi(const sp<SurfaceControl>& sc, int32_t api);
483 Transaction& setSidebandStream(const sp<SurfaceControl>& sc,
484 const sp<NativeHandle>& sidebandStream);
Marissa Wall17b4e452018-12-26 16:32:34 -0800485 Transaction& setDesiredPresentTime(nsecs_t desiredPresentTime);
Peiyong Linc502cb72019-03-01 15:00:23 -0800486 Transaction& setColorSpaceAgnostic(const sp<SurfaceControl>& sc, const bool agnostic);
Marissa Wall61c58622018-07-18 10:12:20 -0700487
Ana Krulecc84d09b2019-11-02 23:10:29 +0100488 // Sets information about the priority of the frame.
489 Transaction& setFrameRateSelectionPriority(const sp<SurfaceControl>& sc, int32_t priority);
490
Marissa Walle2ffb422018-10-12 11:33:52 -0700491 Transaction& addTransactionCompletedCallback(
492 TransactionCompletedCallbackTakesContext callback, void* callbackContext);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700493
Valerie Hau871d6352020-01-29 08:44:02 -0800494 // ONLY FOR BLAST ADAPTER
495 Transaction& notifyProducerDisconnect(const sp<SurfaceControl>& sc);
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700496 // Set the framenumber generated by the graphics producer to mimic BufferQueue behaviour.
497 Transaction& setFrameNumber(const sp<SurfaceControl>& sc, uint64_t frameNumber);
Valerie Hau871d6352020-01-29 08:44:02 -0800498
Robert Carr4cdc58f2017-08-23 14:22:20 -0700499 // Detaches all child surfaces (and their children recursively)
500 // from their SurfaceControl.
501 // The child SurfaceControls will not throw exceptions or return errors,
502 // but transactions will have no effect.
503 // The child surfaces will continue to follow their parent surfaces,
504 // and remain eligible for rendering, but their relative state will be
505 // frozen. We use this in the WindowManager, in app shutdown/relaunch
506 // scenarios, where the app would otherwise clean up its child Surfaces.
507 // Sometimes the WindowManager needs to extend their lifetime slightly
508 // in order to perform an exit animation or prevent flicker.
509 Transaction& detachChildren(const sp<SurfaceControl>& sc);
510 // Set an override scaling mode as documented in <system/window.h>
511 // the override scaling mode will take precedence over any client
512 // specified scaling mode. -1 will clear the override scaling mode.
513 Transaction& setOverrideScalingMode(const sp<SurfaceControl>& sc,
514 int32_t overrideScalingMode);
515
Robert Carr2c358bf2018-08-08 15:58:15 -0700516#ifndef NO_INPUT
517 Transaction& setInputWindowInfo(const sp<SurfaceControl>& sc, const InputWindowInfo& info);
Vishnu Naire798b472020-07-23 13:52:21 -0700518 Transaction& setFocusedWindow(const sp<IBinder>& token, const sp<IBinder>& focusedToken,
Vishnu Nair958da932020-08-21 17:12:37 -0700519 nsecs_t timestampNanos, int32_t displayId);
Vishnu Naire798b472020-07-23 13:52:21 -0700520 Transaction& setFocusedWindow(const FocusRequest& request);
chaviwa911b102019-02-14 10:18:33 -0800521 Transaction& syncInputWindows();
Robert Carr2c358bf2018-08-08 15:58:15 -0700522#endif
523
Peiyong Lind3788632018-09-18 16:01:31 -0700524 // Set a color transform matrix on the given layer on the built-in display.
525 Transaction& setColorTransform(const sp<SurfaceControl>& sc, const mat3& matrix,
526 const vec3& translation);
527
Robert Carrfb4d58b2019-01-15 09:21:27 -0800528 Transaction& setGeometry(const sp<SurfaceControl>& sc,
529 const Rect& source, const Rect& dst, int transform);
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700530 Transaction& setShadowRadius(const sp<SurfaceControl>& sc, float cornerRadius);
Robert Carrfb4d58b2019-01-15 09:21:27 -0800531
Steven Thomas62a4cf82020-01-31 12:04:03 -0800532 Transaction& setFrameRate(const sp<SurfaceControl>& sc, float frameRate,
533 int8_t compatibility);
Steven Thomas3172e202020-01-06 19:25:30 -0800534
Vishnu Nair6213bd92020-05-08 17:42:25 -0700535 // Set by window manager indicating the layer and all its children are
536 // in a different orientation than the display. The hint suggests that
537 // the graphic producers should receive a transform hint as if the
538 // display was in this orientation. When the display changes to match
539 // the layer orientation, the graphic producer may not need to allocate
540 // a buffer of a different size.
541 Transaction& setFixedTransformHint(const sp<SurfaceControl>& sc, int32_t transformHint);
542
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700543 // Sets the frame timeline vsync id received from choreographer that corresponds
544 // to the transaction.
545 Transaction& setFrameTimelineVsync(int64_t frameTimelineVsyncId);
546
Robert Carr4cdc58f2017-08-23 14:22:20 -0700547 status_t setDisplaySurface(const sp<IBinder>& token,
548 const sp<IGraphicBufferProducer>& bufferProducer);
549
550 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
551
552 /* setDisplayProjection() defines the projection of layer stacks
553 * to a given display.
554 *
555 * - orientation defines the display's orientation.
556 * - layerStackRect defines which area of the window manager coordinate
557 * space will be used.
558 * - displayRect defines where on the display will layerStackRect be
559 * mapped to. displayRect is specified post-orientation, that is
560 * it uses the orientation seen by the end-user.
561 */
Dominik Laskowski718f9602019-11-09 20:01:35 -0800562 void setDisplayProjection(const sp<IBinder>& token, ui::Rotation orientation,
563 const Rect& layerStackRect, const Rect& displayRect);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700564 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
565 void setAnimationTransaction();
Dan Stoza84d619e2018-03-28 17:07:36 -0700566 void setEarlyWakeup();
Ady Abrahambf1349c2020-06-12 14:26:18 -0700567 void setExplicitEarlyWakeupStart();
568 void setExplicitEarlyWakeupEnd();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700569 };
Robert Carr82364e32016-05-15 11:27:47 -0700570
Svetoslavd85084b2014-03-20 10:28:31 -0700571 status_t clearLayerFrameStats(const sp<IBinder>& token) const;
572 status_t getLayerFrameStats(const sp<IBinder>& token, FrameStats* outStats) const;
Svetoslavd85084b2014-03-20 10:28:31 -0700573 static status_t clearAnimationFrameStats();
574 static status_t getAnimationFrameStats(FrameStats* outStats);
575
Dan Stozac4f471e2016-03-24 09:31:08 -0700576 static status_t getHdrCapabilities(const sp<IBinder>& display,
577 HdrCapabilities* outCapabilities);
578
Dominik Laskowski718f9602019-11-09 20:01:35 -0800579 static void setDisplayProjection(const sp<IBinder>& token, ui::Rotation orientation,
580 const Rect& layerStackRect, const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700581
Robert Carr4cdc58f2017-08-23 14:22:20 -0700582 inline sp<ISurfaceComposerClient> getClient() { return mClient; }
583
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700584 static status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
585 ui::PixelFormat* outFormat,
586 ui::Dataspace* outDataspace,
587 uint8_t* outComponentMask);
Kevin DuBois74e53772018-11-19 10:52:38 -0800588 static status_t setDisplayContentSamplingEnabled(const sp<IBinder>& display, bool enable,
589 uint8_t componentMask, uint64_t maxFrames);
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700590
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700591 static status_t getDisplayedContentSample(const sp<IBinder>& display, uint64_t maxFrames,
592 uint64_t timestamp, DisplayedFrameStats* outStats);
Kevin DuBois00c66832019-02-18 16:21:31 -0800593 static status_t addRegionSamplingListener(const Rect& samplingArea,
594 const sp<IBinder>& stopLayerHandle,
595 const sp<IRegionSamplingListener>& listener);
596 static status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener);
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700597
Mathias Agopian631f3582010-05-25 17:51:34 -0700598private:
Mathias Agopiand4784a32010-05-27 19:41:15 -0700599 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600
Mathias Agopian698c0872011-06-28 19:09:31 -0700601 mutable Mutex mLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 status_t mStatus;
Mathias Agopian7e27f052010-05-28 14:22:23 -0700603 sp<ISurfaceComposerClient> mClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604};
605
Mathias Agopiand4784a32010-05-27 19:41:15 -0700606// ---------------------------------------------------------------------------
Mathias Agopian74c40c02010-09-29 13:02:36 -0700607
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000608class ScreenshotClient {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800609public:
chaviw690db382020-07-27 16:46:46 -0700610 static status_t captureDisplay(const DisplayCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -0700611 const sp<IScreenCaptureListener>& captureListener);
chaviw690db382020-07-27 16:46:46 -0700612 static status_t captureDisplay(uint64_t displayOrLayerStack,
chaviwe7b9f272020-08-18 16:08:59 -0700613 const sp<IScreenCaptureListener>& captureListener);
chaviw26c52482020-07-28 16:25:52 -0700614 static status_t captureLayers(const LayerCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -0700615 const sp<IScreenCaptureListener>& captureListener);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700616};
617
618// ---------------------------------------------------------------------------
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700619
Marissa Wall80d94ad2019-01-18 16:04:36 -0800620class TransactionCompletedListener : public BnTransactionCompletedListener {
621 TransactionCompletedListener();
622
623 CallbackId getNextIdLocked() REQUIRES(mMutex);
624
625 std::mutex mMutex;
626
627 bool mListening GUARDED_BY(mMutex) = false;
628
629 CallbackId mCallbackIdCounter GUARDED_BY(mMutex) = 1;
630
Marissa Wall80d94ad2019-01-18 16:04:36 -0800631 struct CallbackTranslation {
632 TransactionCompletedCallback callbackFunction;
Vishnu Nairf03652d2019-07-16 17:56:56 -0700633 std::unordered_map<sp<IBinder>, sp<SurfaceControl>, SurfaceComposerClient::IBinderHash>
634 surfaceControls;
Marissa Wall80d94ad2019-01-18 16:04:36 -0800635 };
636
637 std::unordered_map<CallbackId, CallbackTranslation> mCallbacks GUARDED_BY(mMutex);
638
639public:
640 static sp<TransactionCompletedListener> getInstance();
641 static sp<ITransactionCompletedListener> getIInstance();
642
643 void startListeningLocked() REQUIRES(mMutex);
644
645 CallbackId addCallbackFunction(
646 const TransactionCompletedCallback& callbackFunction,
647 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
648 surfaceControls);
649
650 void addSurfaceControlToCallbacks(const sp<SurfaceControl>& surfaceControl,
651 const std::unordered_set<CallbackId>& callbackIds);
652
653 // Overrides BnTransactionCompletedListener's onTransactionCompleted
654 void onTransactionCompleted(ListenerStats stats) override;
655};
656
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800657} // namespace android