blob: 228bf33b9a76526ca84d7fb1df8074ed04db8a54 [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
Mathias Agopian90ac7992012-02-25 18:48:35 -080017#ifndef ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H
18#define ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019
20#include <stdint.h>
21#include <sys/types.h>
Marissa Wallc837b5e2018-10-12 10:04:44 -070022#include <set>
chaviw8e3fe5d2018-02-22 10:55:42 -080023#include <unordered_map>
Marissa Wallc837b5e2018-10-12 10:04:44 -070024#include <unordered_set>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025
Mathias Agopiandd3423c2009-09-23 15:44:05 -070026#include <binder/IBinder.h>
27
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <utils/RefBase.h>
Mathias Agopianb7e930d2010-06-01 15:12:58 -070029#include <utils/Singleton.h>
30#include <utils/SortedVector.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/threads.h>
32
Daniel Solomon42d04562019-01-20 21:03:19 -080033#include <ui/ConfigStoreTypes.h>
Kevin DuBois1d4249a2018-08-29 10:45:14 -070034#include <ui/DisplayedFrameStats.h>
Svetoslavd85084b2014-03-20 10:28:31 -070035#include <ui/FrameStats.h>
Peiyong Lin9f034472018-03-28 15:29:00 -070036#include <ui/GraphicTypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <ui/PixelFormat.h>
Dominik Laskowski718f9602019-11-09 20:01:35 -080038#include <ui/Rotation.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080039
Mathias Agopianabe815d2013-03-19 22:22:21 -070040#include <gui/CpuConsumer.h>
Robert Carr866455f2019-04-02 16:28:26 -070041#include <gui/ISurfaceComposer.h>
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070042#include <gui/ITransactionCompletedListener.h>
43#include <gui/LayerState.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/SurfaceControl.h>
chaviw13fdc492017-06-27 12:40:18 -070045#include <math/vec3.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
47namespace android {
48
49// ---------------------------------------------------------------------------
50
Colin Crossa2362b42016-09-26 13:48:25 -070051struct DisplayInfo;
Dan Stozac4f471e2016-03-24 09:31:08 -070052class HdrCapabilities;
Mathias Agopian41f673c2011-11-17 17:48:35 -080053class ISurfaceComposerClient;
Andy McFadden2adaf042012-12-18 09:49:45 -080054class IGraphicBufferProducer;
Kevin DuBois00c66832019-02-18 16:21:31 -080055class IRegionSamplingListener;
Mathias Agopiana67932f2011-04-20 14:20:59 -070056class Region;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070057
58// ---------------------------------------------------------------------------
59
Marissa Wall80d94ad2019-01-18 16:04:36 -080060struct SurfaceControlStats {
61 SurfaceControlStats(const sp<SurfaceControl>& sc, nsecs_t time,
Valerie Hau32cdc1f2019-10-21 14:45:54 -070062 const sp<Fence>& prevReleaseFence, uint32_t hint)
63 : surfaceControl(sc),
64 acquireTime(time),
65 previousReleaseFence(prevReleaseFence),
66 transformHint(hint) {}
Marissa Wallc837b5e2018-10-12 10:04:44 -070067
Marissa Wall80d94ad2019-01-18 16:04:36 -080068 sp<SurfaceControl> surfaceControl;
69 nsecs_t acquireTime = -1;
70 sp<Fence> previousReleaseFence;
Valerie Hau32cdc1f2019-10-21 14:45:54 -070071 uint32_t transformHint = 0;
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070072};
73
Marissa Wall80d94ad2019-01-18 16:04:36 -080074using TransactionCompletedCallbackTakesContext =
75 std::function<void(void* /*context*/, nsecs_t /*latchTime*/,
76 const sp<Fence>& /*presentFence*/,
77 const std::vector<SurfaceControlStats>& /*stats*/)>;
78using TransactionCompletedCallback =
79 std::function<void(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
80 const std::vector<SurfaceControlStats>& /*stats*/)>;
81
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070082// ---------------------------------------------------------------------------
83
Mathias Agopiand4784a32010-05-27 19:41:15 -070084class SurfaceComposerClient : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085{
Mathias Agopian698c0872011-06-28 19:09:31 -070086 friend class Composer;
Jesse Hall6c913be2013-08-08 12:15:49 -070087public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 SurfaceComposerClient();
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +010089 SurfaceComposerClient(const sp<ISurfaceComposerClient>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 virtual ~SurfaceComposerClient();
91
92 // Always make sure we could initialize
93 status_t initCheck() const;
94
95 // Return the connection of this client
96 sp<IBinder> connection() const;
Jesse Hall6c913be2013-08-08 12:15:49 -070097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 // Forcibly remove connection before all references have gone away.
99 void dispose();
100
Mathias Agopiane57f2922012-08-09 16:29:12 -0700101 // callback when the composer is dies
102 status_t linkToComposerDeath(const sp<IBinder::DeathRecipient>& recipient,
Yi Konga03e0442018-07-17 11:16:57 -0700103 void* cookie = nullptr, uint32_t flags = 0);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700104
Dan Stoza7f7da322014-05-02 15:26:25 -0700105 // Get a list of supported configurations for a given display
106 static status_t getDisplayConfigs(const sp<IBinder>& display,
107 Vector<DisplayInfo>* configs);
108
109 // Get the DisplayInfo for the currently-active configuration
110 static status_t getDisplayInfo(const sp<IBinder>& display,
111 DisplayInfo* info);
112
113 // Get the index of the current active configuration (relative to the list
114 // returned by getDisplayInfo)
115 static int getActiveConfig(const sp<IBinder>& display);
116
117 // Set a new active configuration using an index relative to the list
118 // returned by getDisplayInfo
119 static status_t setActiveConfig(const sp<IBinder>& display, int id);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700120
Ana Krulec0782b882019-10-15 17:34:54 -0700121 // Sets the refresh rate boundaries for display configuration.
122 // For all other parameters, default configuration is used. The index for the default is
123 // corresponting to the configs returned from getDisplayConfigs().
124 static status_t setDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100125 int32_t defaultConfig, float minRefreshRate,
Ana Krulec0782b882019-10-15 17:34:54 -0700126 float maxRefreshRate);
Ana Krulec234bb162019-11-10 22:55:55 +0100127 // Gets the refresh rate boundaries for display configuration.
128 // For all other parameters, default configuration is used. The index for the default is
129 // corresponting to the configs returned from getDisplayConfigs().
130 // The reason is passed in for telemetry tracking, and it corresponds to the list of all
131 // the policy rules that were used.
132 static status_t getDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100133 int32_t* outDefaultConfig,
Ana Krulec234bb162019-11-10 22:55:55 +0100134 float* outMinRefreshRate,
135 float* outMaxRefreshRate);
Ana Krulec0782b882019-10-15 17:34:54 -0700136
Michael Wright28f24d02016-07-12 13:30:53 -0700137 // Gets the list of supported color modes for the given display
138 static status_t getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lin9f034472018-03-28 15:29:00 -0700139 Vector<ui::ColorMode>* outColorModes);
Michael Wright28f24d02016-07-12 13:30:53 -0700140
Daniel Solomon42d04562019-01-20 21:03:19 -0800141 // Get the coordinates of the display's native color primaries
142 static status_t getDisplayNativePrimaries(const sp<IBinder>& display,
143 ui::DisplayPrimaries& outPrimaries);
144
Michael Wright28f24d02016-07-12 13:30:53 -0700145 // Gets the active color mode for the given display
Peiyong Lin9f034472018-03-28 15:29:00 -0700146 static ui::ColorMode getActiveColorMode(const sp<IBinder>& display);
Michael Wright28f24d02016-07-12 13:30:53 -0700147
148 // Sets the active color mode for the given display
Peiyong Lin9f034472018-03-28 15:29:00 -0700149 static status_t setActiveColorMode(const sp<IBinder>& display,
150 ui::ColorMode colorMode);
Michael Wright28f24d02016-07-12 13:30:53 -0700151
Galia Peycheva5492cb52019-10-30 14:13:16 +0100152 // Reports whether the connected display supports Auto Low Latency Mode
153 static bool getAutoLowLatencyModeSupport(const sp<IBinder>& display);
154
155 // Switches on/off Auto Low Latency Mode on the connected display. This should only be
156 // called if the connected display supports Auto Low Latency Mode as reported by
157 // #getAutoLowLatencyModeSupport
158 static void setAutoLowLatencyMode(const sp<IBinder>& display, bool on);
159
160 // Reports whether the connected display supports Game content type
161 static bool getGameContentTypeSupport(const sp<IBinder>& display);
162
163 // Turns Game mode on/off on the connected display. This should only be called
164 // if the display supports Game content type, as reported by #getGameContentTypeSupport
165 static void setGameContentType(const sp<IBinder>& display, bool on);
166
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700167 /* Triggers screen on/off or low power mode and waits for it to complete */
168 static void setDisplayPowerMode(const sp<IBinder>& display, int mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700169
Peiyong Linc6780972018-10-28 15:24:08 -0700170 /* Returns the composition preference of the default data space and default pixel format,
171 * as well as the wide color gamut data space and wide color gamut pixel format.
172 * If the wide color gamut data space is V0_SRGB, then it implies that the platform
173 * has no wide color gamut support.
174 */
175 static status_t getCompositionPreference(ui::Dataspace* defaultDataspace,
176 ui::PixelFormat* defaultPixelFormat,
177 ui::Dataspace* wideColorGamutDataspace,
178 ui::PixelFormat* wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -0700179
Peiyong Lin08d10512019-01-16 13:27:35 -0800180 /*
181 * Gets whether SurfaceFlinger can support protected content in GPU composition.
182 * Requires the ACCESS_SURFACE_FLINGER permission.
183 */
184 static bool getProtectedContentSupport();
185
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800186 /**
187 * Called from SurfaceControl d'tor to 'destroy' the surface (or rather, reparent it
188 * to null), but without needing an sp<SurfaceControl> to avoid infinite ressurection.
189 */
Vishnu Nairf03652d2019-07-16 17:56:56 -0700190 static void doDropReferenceTransaction(const sp<IBinder>& handle);
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800191
Marissa Wall78b72202019-03-15 14:58:34 -0700192 /**
193 * Uncaches a buffer in ISurfaceComposer. It must be uncached via a transaction so that it is
194 * in order with other transactions that use buffers.
195 */
196 static void doUncacheBufferTransaction(uint64_t cacheId);
197
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800198 // Queries whether a given display is wide color display.
199 static status_t isWideColorDisplay(const sp<IBinder>& display, bool* outIsWideColorDisplay);
200
Dan Gittik57e63c52019-01-18 16:37:54 +0000201 /*
202 * Returns whether brightness operations are supported on a display.
203 *
204 * displayToken
205 * The token of the display.
206 *
207 * Returns whether brightness operations are supported on a display or not.
208 */
209 static bool getDisplayBrightnessSupport(const sp<IBinder>& displayToken);
210
211 /*
212 * Sets the brightness of a display.
213 *
214 * displayToken
215 * The token of the display whose brightness is set.
216 * brightness
217 * A number between 0.0 (minimum brightness) and 1.0 (maximum brightness), or -1.0f to
218 * turn the backlight off.
219 *
220 * Returns NO_ERROR upon success. Otherwise,
221 * NAME_NOT_FOUND if the display handle is invalid, or
222 * BAD_VALUE if the brightness value is invalid, or
223 * INVALID_OPERATION if brightness operaetions are not supported.
224 */
225 static status_t setDisplayBrightness(const sp<IBinder>& displayToken, float brightness);
226
Ady Abraham8532d012019-05-08 14:50:56 -0700227 /*
228 * Sends a power hint to the composer. This function is asynchronous.
229 *
230 * hintId
231 * hint id according to android::hardware::power::V1_0::PowerHint
232 *
233 * Returns NO_ERROR upon success.
234 */
235 static status_t notifyPowerHint(int32_t hintId);
236
Vishnu Nairb13bb952019-11-15 10:24:08 -0800237 /*
238 * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows
239 * material design guidelines.
240 *
241 * ambientColor
242 * Color to the ambient shadow. The alpha is premultiplied.
243 *
244 * spotColor
245 * Color to the spot shadow. The alpha is premultiplied. The position of the spot shadow
246 * depends on the light position.
247 *
248 * lightPosY/lightPosZ
249 * Position of the light used to cast the spot shadow. The X value is always the display
250 * width / 2.
251 *
252 * lightRadius
253 * Radius of the light casting the shadow.
254 */
255 static status_t setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor,
256 float lightPosY, float lightPosZ, float lightRadius);
257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 // ------------------------------------------------------------------------
259 // surface creation / destruction
260
Robert Carrfb4d58b2019-01-15 09:21:27 -0800261 static sp<SurfaceComposerClient> getDefault();
262
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 //! Create a surface
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800264 sp<SurfaceControl> createSurface(const String8& name, // name of the surface
265 uint32_t w, // width in pixel
266 uint32_t h, // height in pixel
267 PixelFormat format, // pixel-format desired
268 uint32_t flags = 0, // usage flags
269 SurfaceControl* parent = nullptr, // parent
Valerie Hau1acd6962019-10-28 16:35:48 -0700270 LayerMetadata metadata = LayerMetadata(), // metadata
271 uint32_t* outTransformHint = nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800273 status_t createSurfaceChecked(const String8& name, // name of the surface
274 uint32_t w, // width in pixel
275 uint32_t h, // height in pixel
276 PixelFormat format, // pixel-format desired
277 sp<SurfaceControl>* outSurface,
Valerie Hau1acd6962019-10-28 16:35:48 -0700278 uint32_t flags = 0, // usage flags
279 SurfaceControl* parent = nullptr, // parent
280 LayerMetadata metadata = LayerMetadata(), // metadata
281 uint32_t* outTransformHint = nullptr);
Marissa Wall35187b32019-01-08 10:08:52 -0800282
283 //! Create a surface
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800284 sp<SurfaceControl> createWithSurfaceParent(const String8& name, // name of the surface
285 uint32_t w, // width in pixel
286 uint32_t h, // height in pixel
287 PixelFormat format, // pixel-format desired
288 uint32_t flags = 0, // usage flags
289 Surface* parent = nullptr, // parent
Valerie Hau1acd6962019-10-28 16:35:48 -0700290 LayerMetadata metadata = LayerMetadata(), // metadata
291 uint32_t* outTransformHint = nullptr);
Robert Carr3b382ed2018-03-14 13:49:41 -0700292
chaviwfe94a222019-08-21 13:52:59 -0700293 // Creates a mirrored hierarchy for the mirrorFromSurface. This returns a SurfaceControl
294 // which is a parent of the root of the mirrored hierarchy.
295 //
296 // Real Hierarchy Mirror
297 // SC (value that's returned)
298 // |
299 // A A'
300 // | |
301 // B B'
302 sp<SurfaceControl> mirrorSurface(SurfaceControl* mirrorFromSurface);
303
Jesse Hall6c913be2013-08-08 12:15:49 -0700304 //! Create a virtual display
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700305 static sp<IBinder> createDisplay(const String8& displayName, bool secure);
Mathias Agopian285dbde2010-03-01 16:09:43 -0800306
Jesse Hall6c913be2013-08-08 12:15:49 -0700307 //! Destroy a virtual display
308 static void destroyDisplay(const sp<IBinder>& display);
309
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800310 //! Get stable IDs for connected physical displays
311 static std::vector<PhysicalDisplayId> getPhysicalDisplayIds();
312 static std::optional<PhysicalDisplayId> getInternalDisplayId();
313
314 //! Get token for a physical display given its stable ID
315 static sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId);
316 static sp<IBinder> getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700317
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700318 static status_t enableVSyncInjections(bool enable);
319
320 static status_t injectVSync(nsecs_t when);
321
chaviw8e3fe5d2018-02-22 10:55:42 -0800322 struct SCHash {
323 std::size_t operator()(const sp<SurfaceControl>& sc) const {
324 return std::hash<SurfaceControl *>{}(sc.get());
325 }
326 };
327
Vishnu Nairf03652d2019-07-16 17:56:56 -0700328 struct IBinderHash {
329 std::size_t operator()(const sp<IBinder>& iBinder) const {
330 return std::hash<IBinder*>{}(iBinder.get());
331 }
332 };
333
Marissa Wallc837b5e2018-10-12 10:04:44 -0700334 struct TCLHash {
335 std::size_t operator()(const sp<ITransactionCompletedListener>& tcl) const {
336 return std::hash<IBinder*>{}((tcl) ? IInterface::asBinder(tcl).get() : nullptr);
337 }
338 };
339
340 struct CallbackInfo {
341 // All the callbacks that have been requested for a TransactionCompletedListener in the
342 // Transaction
343 std::unordered_set<CallbackId> callbackIds;
344 // All the SurfaceControls that have been modified in this TransactionCompletedListener's
345 // process that require a callback if there is one or more callbackIds set.
346 std::unordered_set<sp<SurfaceControl>, SCHash> surfaceControls;
347 };
348
Valerie Hau2b0cd8d2019-08-26 10:30:52 -0700349 class Transaction : public Parcelable {
Valerie Hau9dab9732019-08-20 09:29:25 -0700350 protected:
Vishnu Nairf03652d2019-07-16 17:56:56 -0700351 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> mComposerStates;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700352 SortedVector<DisplayState > mDisplayStates;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700353 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash>
354 mListenerCallbacks;
355
Robert Carr4cdc58f2017-08-23 14:22:20 -0700356 uint32_t mForceSynchronous = 0;
357 uint32_t mTransactionNestCount = 0;
358 bool mAnimation = false;
Dan Stoza84d619e2018-03-28 17:07:36 -0700359 bool mEarlyWakeup = 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
chaviw273171b2018-12-26 11:46:30 -0800376 InputWindowCommands mInputWindowCommands;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700377 int mStatus = NO_ERROR;
378
Vishnu Nairf03652d2019-07-16 17:56:56 -0700379 layer_state_t* getLayerState(const sp<IBinder>& surfaceHandle);
380 layer_state_t* getLayerState(const sp<SurfaceControl>& sc) {
381 return getLayerState(sc->getHandle());
382 }
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:
389 Transaction() = default;
390 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,
427 const sp<IBinder>& relativeTo, int32_t z);
428 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,
447 const sp<IBinder>& handle, uint64_t frameNumber);
448 // A variant of deferTransactionUntil_legacy which identifies the Layer we wait for by
Robert Carr4cdc58f2017-08-23 14:22:20 -0700449 // Surface instead of Handle. Useful for clients which may not have the
450 // SurfaceControl for some of their Surfaces. Otherwise behaves identically.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700451 Transaction& deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
452 const sp<Surface>& barrierSurface,
453 uint64_t frameNumber);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700454 // Reparents all children of this layer to the new parent handle.
455 Transaction& reparentChildren(const sp<SurfaceControl>& sc,
456 const sp<IBinder>& newParentHandle);
457
458 /// Reparents the current layer to the new parent handle. The new parent must not be null.
459 // This can be used instead of reparentChildren if the caller wants to
460 // only re-parent a specific child.
461 Transaction& reparent(const sp<SurfaceControl>& sc,
462 const sp<IBinder>& newParentHandle);
463
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
Robert Carr4cdc58f2017-08-23 14:22:20 -0700494 // Detaches all child surfaces (and their children recursively)
495 // from their SurfaceControl.
496 // The child SurfaceControls will not throw exceptions or return errors,
497 // but transactions will have no effect.
498 // The child surfaces will continue to follow their parent surfaces,
499 // and remain eligible for rendering, but their relative state will be
500 // frozen. We use this in the WindowManager, in app shutdown/relaunch
501 // scenarios, where the app would otherwise clean up its child Surfaces.
502 // Sometimes the WindowManager needs to extend their lifetime slightly
503 // in order to perform an exit animation or prevent flicker.
504 Transaction& detachChildren(const sp<SurfaceControl>& sc);
505 // Set an override scaling mode as documented in <system/window.h>
506 // the override scaling mode will take precedence over any client
507 // specified scaling mode. -1 will clear the override scaling mode.
508 Transaction& setOverrideScalingMode(const sp<SurfaceControl>& sc,
509 int32_t overrideScalingMode);
510
Robert Carr2c358bf2018-08-08 15:58:15 -0700511#ifndef NO_INPUT
512 Transaction& setInputWindowInfo(const sp<SurfaceControl>& sc, const InputWindowInfo& info);
chaviw273171b2018-12-26 11:46:30 -0800513 Transaction& transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken);
chaviwa911b102019-02-14 10:18:33 -0800514 Transaction& syncInputWindows();
Robert Carr2c358bf2018-08-08 15:58:15 -0700515#endif
516
Peiyong Lind3788632018-09-18 16:01:31 -0700517 // Set a color transform matrix on the given layer on the built-in display.
518 Transaction& setColorTransform(const sp<SurfaceControl>& sc, const mat3& matrix,
519 const vec3& translation);
520
Robert Carrfb4d58b2019-01-15 09:21:27 -0800521 Transaction& setGeometry(const sp<SurfaceControl>& sc,
522 const Rect& source, const Rect& dst, int transform);
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700523 Transaction& setShadowRadius(const sp<SurfaceControl>& sc, float cornerRadius);
Robert Carrfb4d58b2019-01-15 09:21:27 -0800524
Steven Thomas3172e202020-01-06 19:25:30 -0800525 Transaction& setFrameRate(const sp<SurfaceControl>& sc, float frameRate);
526
Robert Carr4cdc58f2017-08-23 14:22:20 -0700527 status_t setDisplaySurface(const sp<IBinder>& token,
528 const sp<IGraphicBufferProducer>& bufferProducer);
529
530 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
531
532 /* setDisplayProjection() defines the projection of layer stacks
533 * to a given display.
534 *
535 * - orientation defines the display's orientation.
536 * - layerStackRect defines which area of the window manager coordinate
537 * space will be used.
538 * - displayRect defines where on the display will layerStackRect be
539 * mapped to. displayRect is specified post-orientation, that is
540 * it uses the orientation seen by the end-user.
541 */
Dominik Laskowski718f9602019-11-09 20:01:35 -0800542 void setDisplayProjection(const sp<IBinder>& token, ui::Rotation orientation,
543 const Rect& layerStackRect, const Rect& displayRect);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700544 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
545 void setAnimationTransaction();
Dan Stoza84d619e2018-03-28 17:07:36 -0700546 void setEarlyWakeup();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700547 };
Robert Carr82364e32016-05-15 11:27:47 -0700548
Svetoslavd85084b2014-03-20 10:28:31 -0700549 status_t clearLayerFrameStats(const sp<IBinder>& token) const;
550 status_t getLayerFrameStats(const sp<IBinder>& token, FrameStats* outStats) const;
Svetoslavd85084b2014-03-20 10:28:31 -0700551 static status_t clearAnimationFrameStats();
552 static status_t getAnimationFrameStats(FrameStats* outStats);
553
Dan Stozac4f471e2016-03-24 09:31:08 -0700554 static status_t getHdrCapabilities(const sp<IBinder>& display,
555 HdrCapabilities* outCapabilities);
556
Dominik Laskowski718f9602019-11-09 20:01:35 -0800557 static void setDisplayProjection(const sp<IBinder>& token, ui::Rotation orientation,
558 const Rect& layerStackRect, const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700559
Robert Carr4cdc58f2017-08-23 14:22:20 -0700560 inline sp<ISurfaceComposerClient> getClient() { return mClient; }
561
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700562 static status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
563 ui::PixelFormat* outFormat,
564 ui::Dataspace* outDataspace,
565 uint8_t* outComponentMask);
Kevin DuBois74e53772018-11-19 10:52:38 -0800566 static status_t setDisplayContentSamplingEnabled(const sp<IBinder>& display, bool enable,
567 uint8_t componentMask, uint64_t maxFrames);
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700568
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700569 static status_t getDisplayedContentSample(const sp<IBinder>& display, uint64_t maxFrames,
570 uint64_t timestamp, DisplayedFrameStats* outStats);
Kevin DuBois00c66832019-02-18 16:21:31 -0800571 static status_t addRegionSamplingListener(const Rect& samplingArea,
572 const sp<IBinder>& stopLayerHandle,
573 const sp<IRegionSamplingListener>& listener);
574 static status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener);
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700575
Mathias Agopian631f3582010-05-25 17:51:34 -0700576private:
Mathias Agopiand4784a32010-05-27 19:41:15 -0700577 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578
Mathias Agopian698c0872011-06-28 19:09:31 -0700579 mutable Mutex mLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 status_t mStatus;
Mathias Agopian7e27f052010-05-28 14:22:23 -0700581 sp<ISurfaceComposerClient> mClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582};
583
Mathias Agopiand4784a32010-05-27 19:41:15 -0700584// ---------------------------------------------------------------------------
Mathias Agopian74c40c02010-09-29 13:02:36 -0700585
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000586class ScreenshotClient {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800587public:
Dan Stozac1879002014-05-22 15:59:05 -0700588 // if cropping isn't required, callers may pass in a default Rect, e.g.:
589 // capture(display, producer, Rect(), reqWidth, ...);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800590 static status_t capture(const sp<IBinder>& display, ui::Dataspace reqDataSpace,
591 ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700592 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Dominik Laskowski718f9602019-11-09 20:01:35 -0800593 ui::Rotation rotation, bool captureSecureLayers,
Robert Carr108b2c72019-04-02 16:32:58 -0700594 sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800595 static status_t capture(const sp<IBinder>& display, ui::Dataspace reqDataSpace,
596 ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
Robert Carrfa8855f2019-02-19 10:05:00 -0800597 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Dominik Laskowski718f9602019-11-09 20:01:35 -0800598 ui::Rotation rotation, sp<GraphicBuffer>* outBuffer);
chaviw93df2ea2019-04-30 16:45:12 -0700599 static status_t capture(uint64_t displayOrLayerStack, ui::Dataspace* outDataspace,
600 sp<GraphicBuffer>* outBuffer);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800601 static status_t captureLayers(const sp<IBinder>& layerHandle, ui::Dataspace reqDataSpace,
602 ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700603 float frameScale, sp<GraphicBuffer>* outBuffer);
Robert Carr866455f2019-04-02 16:28:26 -0700604 static status_t captureChildLayers(
Dominik Laskowski718f9602019-11-09 20:01:35 -0800605 const sp<IBinder>& layerHandle, ui::Dataspace reqDataSpace,
606 ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
Robert Carr866455f2019-04-02 16:28:26 -0700607 const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>&
608 excludeHandles,
609 float frameScale, sp<GraphicBuffer>* outBuffer);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700610};
611
612// ---------------------------------------------------------------------------
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700613
Marissa Wall80d94ad2019-01-18 16:04:36 -0800614class TransactionCompletedListener : public BnTransactionCompletedListener {
615 TransactionCompletedListener();
616
617 CallbackId getNextIdLocked() REQUIRES(mMutex);
618
619 std::mutex mMutex;
620
621 bool mListening GUARDED_BY(mMutex) = false;
622
623 CallbackId mCallbackIdCounter GUARDED_BY(mMutex) = 1;
624
Marissa Wall80d94ad2019-01-18 16:04:36 -0800625 struct CallbackTranslation {
626 TransactionCompletedCallback callbackFunction;
Vishnu Nairf03652d2019-07-16 17:56:56 -0700627 std::unordered_map<sp<IBinder>, sp<SurfaceControl>, SurfaceComposerClient::IBinderHash>
628 surfaceControls;
Marissa Wall80d94ad2019-01-18 16:04:36 -0800629 };
630
631 std::unordered_map<CallbackId, CallbackTranslation> mCallbacks GUARDED_BY(mMutex);
632
633public:
634 static sp<TransactionCompletedListener> getInstance();
635 static sp<ITransactionCompletedListener> getIInstance();
636
637 void startListeningLocked() REQUIRES(mMutex);
638
639 CallbackId addCallbackFunction(
640 const TransactionCompletedCallback& callbackFunction,
641 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
642 surfaceControls);
643
644 void addSurfaceControlToCallbacks(const sp<SurfaceControl>& surfaceControl,
645 const std::unordered_set<CallbackId>& callbackIds);
646
647 // Overrides BnTransactionCompletedListener's onTransactionCompleted
648 void onTransactionCompleted(ListenerStats stats) override;
649};
650
651// ---------------------------------------------------------------------------
652
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653}; // namespace android
654
Mathias Agopian90ac7992012-02-25 18:48:35 -0800655#endif // ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H