blob: 873fac03a83e6d375563b225c040508d71026c6d [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>
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
48// ---------------------------------------------------------------------------
49
Colin Crossa2362b42016-09-26 13:48:25 -070050struct DisplayInfo;
Dan Stozac4f471e2016-03-24 09:31:08 -070051class HdrCapabilities;
Mathias Agopian41f673c2011-11-17 17:48:35 -080052class ISurfaceComposerClient;
Andy McFadden2adaf042012-12-18 09:49:45 -080053class IGraphicBufferProducer;
Kevin DuBois00c66832019-02-18 16:21:31 -080054class IRegionSamplingListener;
Mathias Agopiana67932f2011-04-20 14:20:59 -070055class Region;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070056
57// ---------------------------------------------------------------------------
58
Marissa Wall80d94ad2019-01-18 16:04:36 -080059struct SurfaceControlStats {
60 SurfaceControlStats(const sp<SurfaceControl>& sc, nsecs_t time,
Valerie Hau32cdc1f2019-10-21 14:45:54 -070061 const sp<Fence>& prevReleaseFence, uint32_t hint)
62 : surfaceControl(sc),
63 acquireTime(time),
64 previousReleaseFence(prevReleaseFence),
65 transformHint(hint) {}
Marissa Wallc837b5e2018-10-12 10:04:44 -070066
Marissa Wall80d94ad2019-01-18 16:04:36 -080067 sp<SurfaceControl> surfaceControl;
68 nsecs_t acquireTime = -1;
69 sp<Fence> previousReleaseFence;
Valerie Hau32cdc1f2019-10-21 14:45:54 -070070 uint32_t transformHint = 0;
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070071};
72
Marissa Wall80d94ad2019-01-18 16:04:36 -080073using TransactionCompletedCallbackTakesContext =
74 std::function<void(void* /*context*/, nsecs_t /*latchTime*/,
75 const sp<Fence>& /*presentFence*/,
76 const std::vector<SurfaceControlStats>& /*stats*/)>;
77using TransactionCompletedCallback =
78 std::function<void(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
79 const std::vector<SurfaceControlStats>& /*stats*/)>;
80
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070081// ---------------------------------------------------------------------------
82
Mathias Agopiand4784a32010-05-27 19:41:15 -070083class SurfaceComposerClient : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084{
Mathias Agopian698c0872011-06-28 19:09:31 -070085 friend class Composer;
Jesse Hall6c913be2013-08-08 12:15:49 -070086public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 SurfaceComposerClient();
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +010088 SurfaceComposerClient(const sp<ISurfaceComposerClient>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 virtual ~SurfaceComposerClient();
90
91 // Always make sure we could initialize
92 status_t initCheck() const;
93
94 // Return the connection of this client
95 sp<IBinder> connection() const;
Jesse Hall6c913be2013-08-08 12:15:49 -070096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 // Forcibly remove connection before all references have gone away.
98 void dispose();
99
Mathias Agopiane57f2922012-08-09 16:29:12 -0700100 // callback when the composer is dies
101 status_t linkToComposerDeath(const sp<IBinder::DeathRecipient>& recipient,
Yi Konga03e0442018-07-17 11:16:57 -0700102 void* cookie = nullptr, uint32_t flags = 0);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700103
Dan Stoza7f7da322014-05-02 15:26:25 -0700104 // Get a list of supported configurations for a given display
105 static status_t getDisplayConfigs(const sp<IBinder>& display,
106 Vector<DisplayInfo>* configs);
107
108 // Get the DisplayInfo for the currently-active configuration
109 static status_t getDisplayInfo(const sp<IBinder>& display,
110 DisplayInfo* info);
111
112 // Get the index of the current active configuration (relative to the list
113 // returned by getDisplayInfo)
114 static int getActiveConfig(const sp<IBinder>& display);
115
116 // Set a new active configuration using an index relative to the list
117 // returned by getDisplayInfo
118 static status_t setActiveConfig(const sp<IBinder>& display, int id);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700119
Ana Krulec0782b882019-10-15 17:34:54 -0700120 // Sets the refresh rate boundaries for display configuration.
121 // For all other parameters, default configuration is used. The index for the default is
122 // corresponting to the configs returned from getDisplayConfigs().
123 static status_t setDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100124 int32_t defaultConfig, float minRefreshRate,
Ana Krulec0782b882019-10-15 17:34:54 -0700125 float maxRefreshRate);
Ana Krulec234bb162019-11-10 22:55:55 +0100126 // Gets the refresh rate boundaries for display configuration.
127 // For all other parameters, default configuration is used. The index for the default is
128 // corresponting to the configs returned from getDisplayConfigs().
129 // The reason is passed in for telemetry tracking, and it corresponds to the list of all
130 // the policy rules that were used.
131 static status_t getDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100132 int32_t* outDefaultConfig,
Ana Krulec234bb162019-11-10 22:55:55 +0100133 float* outMinRefreshRate,
134 float* outMaxRefreshRate);
Ana Krulec0782b882019-10-15 17:34:54 -0700135
Michael Wright28f24d02016-07-12 13:30:53 -0700136 // Gets the list of supported color modes for the given display
137 static status_t getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lin9f034472018-03-28 15:29:00 -0700138 Vector<ui::ColorMode>* outColorModes);
Michael Wright28f24d02016-07-12 13:30:53 -0700139
Daniel Solomon42d04562019-01-20 21:03:19 -0800140 // Get the coordinates of the display's native color primaries
141 static status_t getDisplayNativePrimaries(const sp<IBinder>& display,
142 ui::DisplayPrimaries& outPrimaries);
143
Michael Wright28f24d02016-07-12 13:30:53 -0700144 // Gets the active color mode for the given display
Peiyong Lin9f034472018-03-28 15:29:00 -0700145 static ui::ColorMode getActiveColorMode(const sp<IBinder>& display);
Michael Wright28f24d02016-07-12 13:30:53 -0700146
147 // Sets the active color mode for the given display
Peiyong Lin9f034472018-03-28 15:29:00 -0700148 static status_t setActiveColorMode(const sp<IBinder>& display,
149 ui::ColorMode colorMode);
Michael Wright28f24d02016-07-12 13:30:53 -0700150
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700151 /* Triggers screen on/off or low power mode and waits for it to complete */
152 static void setDisplayPowerMode(const sp<IBinder>& display, int mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700153
Peiyong Linc6780972018-10-28 15:24:08 -0700154 /* Returns the composition preference of the default data space and default pixel format,
155 * as well as the wide color gamut data space and wide color gamut pixel format.
156 * If the wide color gamut data space is V0_SRGB, then it implies that the platform
157 * has no wide color gamut support.
158 */
159 static status_t getCompositionPreference(ui::Dataspace* defaultDataspace,
160 ui::PixelFormat* defaultPixelFormat,
161 ui::Dataspace* wideColorGamutDataspace,
162 ui::PixelFormat* wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -0700163
Peiyong Lin08d10512019-01-16 13:27:35 -0800164 /*
165 * Gets whether SurfaceFlinger can support protected content in GPU composition.
166 * Requires the ACCESS_SURFACE_FLINGER permission.
167 */
168 static bool getProtectedContentSupport();
169
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800170 /**
171 * Called from SurfaceControl d'tor to 'destroy' the surface (or rather, reparent it
172 * to null), but without needing an sp<SurfaceControl> to avoid infinite ressurection.
173 */
Vishnu Nairf03652d2019-07-16 17:56:56 -0700174 static void doDropReferenceTransaction(const sp<IBinder>& handle);
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800175
Marissa Wall78b72202019-03-15 14:58:34 -0700176 /**
177 * Uncaches a buffer in ISurfaceComposer. It must be uncached via a transaction so that it is
178 * in order with other transactions that use buffers.
179 */
180 static void doUncacheBufferTransaction(uint64_t cacheId);
181
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800182 // Queries whether a given display is wide color display.
183 static status_t isWideColorDisplay(const sp<IBinder>& display, bool* outIsWideColorDisplay);
184
Dan Gittik57e63c52019-01-18 16:37:54 +0000185 /*
186 * Returns whether brightness operations are supported on a display.
187 *
188 * displayToken
189 * The token of the display.
190 *
191 * Returns whether brightness operations are supported on a display or not.
192 */
193 static bool getDisplayBrightnessSupport(const sp<IBinder>& displayToken);
194
195 /*
196 * Sets the brightness of a display.
197 *
198 * displayToken
199 * The token of the display whose brightness is set.
200 * brightness
201 * A number between 0.0 (minimum brightness) and 1.0 (maximum brightness), or -1.0f to
202 * turn the backlight off.
203 *
204 * Returns NO_ERROR upon success. Otherwise,
205 * NAME_NOT_FOUND if the display handle is invalid, or
206 * BAD_VALUE if the brightness value is invalid, or
207 * INVALID_OPERATION if brightness operaetions are not supported.
208 */
209 static status_t setDisplayBrightness(const sp<IBinder>& displayToken, float brightness);
210
Ady Abraham8532d012019-05-08 14:50:56 -0700211 /*
212 * Sends a power hint to the composer. This function is asynchronous.
213 *
214 * hintId
215 * hint id according to android::hardware::power::V1_0::PowerHint
216 *
217 * Returns NO_ERROR upon success.
218 */
219 static status_t notifyPowerHint(int32_t hintId);
220
Vishnu Nairb13bb952019-11-15 10:24:08 -0800221 /*
222 * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows
223 * material design guidelines.
224 *
225 * ambientColor
226 * Color to the ambient shadow. The alpha is premultiplied.
227 *
228 * spotColor
229 * Color to the spot shadow. The alpha is premultiplied. The position of the spot shadow
230 * depends on the light position.
231 *
232 * lightPosY/lightPosZ
233 * Position of the light used to cast the spot shadow. The X value is always the display
234 * width / 2.
235 *
236 * lightRadius
237 * Radius of the light casting the shadow.
238 */
239 static status_t setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor,
240 float lightPosY, float lightPosZ, float lightRadius);
241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 // ------------------------------------------------------------------------
243 // surface creation / destruction
244
Robert Carrfb4d58b2019-01-15 09:21:27 -0800245 static sp<SurfaceComposerClient> getDefault();
246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 //! Create a surface
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800248 sp<SurfaceControl> createSurface(const String8& name, // name of the surface
249 uint32_t w, // width in pixel
250 uint32_t h, // height in pixel
251 PixelFormat format, // pixel-format desired
252 uint32_t flags = 0, // usage flags
253 SurfaceControl* parent = nullptr, // parent
Valerie Hau1acd6962019-10-28 16:35:48 -0700254 LayerMetadata metadata = LayerMetadata(), // metadata
255 uint32_t* outTransformHint = nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800257 status_t createSurfaceChecked(const String8& name, // name of the surface
258 uint32_t w, // width in pixel
259 uint32_t h, // height in pixel
260 PixelFormat format, // pixel-format desired
261 sp<SurfaceControl>* outSurface,
Valerie Hau1acd6962019-10-28 16:35:48 -0700262 uint32_t flags = 0, // usage flags
263 SurfaceControl* parent = nullptr, // parent
264 LayerMetadata metadata = LayerMetadata(), // metadata
265 uint32_t* outTransformHint = nullptr);
Marissa Wall35187b32019-01-08 10:08:52 -0800266
267 //! Create a surface
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800268 sp<SurfaceControl> createWithSurfaceParent(const String8& name, // name of the surface
269 uint32_t w, // width in pixel
270 uint32_t h, // height in pixel
271 PixelFormat format, // pixel-format desired
272 uint32_t flags = 0, // usage flags
273 Surface* parent = nullptr, // parent
Valerie Hau1acd6962019-10-28 16:35:48 -0700274 LayerMetadata metadata = LayerMetadata(), // metadata
275 uint32_t* outTransformHint = nullptr);
Robert Carr3b382ed2018-03-14 13:49:41 -0700276
chaviwfe94a222019-08-21 13:52:59 -0700277 // Creates a mirrored hierarchy for the mirrorFromSurface. This returns a SurfaceControl
278 // which is a parent of the root of the mirrored hierarchy.
279 //
280 // Real Hierarchy Mirror
281 // SC (value that's returned)
282 // |
283 // A A'
284 // | |
285 // B B'
286 sp<SurfaceControl> mirrorSurface(SurfaceControl* mirrorFromSurface);
287
Jesse Hall6c913be2013-08-08 12:15:49 -0700288 //! Create a virtual display
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700289 static sp<IBinder> createDisplay(const String8& displayName, bool secure);
Mathias Agopian285dbde2010-03-01 16:09:43 -0800290
Jesse Hall6c913be2013-08-08 12:15:49 -0700291 //! Destroy a virtual display
292 static void destroyDisplay(const sp<IBinder>& display);
293
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800294 //! Get stable IDs for connected physical displays
295 static std::vector<PhysicalDisplayId> getPhysicalDisplayIds();
296 static std::optional<PhysicalDisplayId> getInternalDisplayId();
297
298 //! Get token for a physical display given its stable ID
299 static sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId);
300 static sp<IBinder> getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700301
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700302 static status_t enableVSyncInjections(bool enable);
303
304 static status_t injectVSync(nsecs_t when);
305
chaviw8e3fe5d2018-02-22 10:55:42 -0800306 struct SCHash {
307 std::size_t operator()(const sp<SurfaceControl>& sc) const {
308 return std::hash<SurfaceControl *>{}(sc.get());
309 }
310 };
311
Vishnu Nairf03652d2019-07-16 17:56:56 -0700312 struct IBinderHash {
313 std::size_t operator()(const sp<IBinder>& iBinder) const {
314 return std::hash<IBinder*>{}(iBinder.get());
315 }
316 };
317
Marissa Wallc837b5e2018-10-12 10:04:44 -0700318 struct TCLHash {
319 std::size_t operator()(const sp<ITransactionCompletedListener>& tcl) const {
320 return std::hash<IBinder*>{}((tcl) ? IInterface::asBinder(tcl).get() : nullptr);
321 }
322 };
323
324 struct CallbackInfo {
325 // All the callbacks that have been requested for a TransactionCompletedListener in the
326 // Transaction
327 std::unordered_set<CallbackId> callbackIds;
328 // All the SurfaceControls that have been modified in this TransactionCompletedListener's
329 // process that require a callback if there is one or more callbackIds set.
330 std::unordered_set<sp<SurfaceControl>, SCHash> surfaceControls;
331 };
332
Valerie Hau2b0cd8d2019-08-26 10:30:52 -0700333 class Transaction : public Parcelable {
Valerie Hau9dab9732019-08-20 09:29:25 -0700334 protected:
Vishnu Nairf03652d2019-07-16 17:56:56 -0700335 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> mComposerStates;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700336 SortedVector<DisplayState > mDisplayStates;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700337 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash>
338 mListenerCallbacks;
339
Robert Carr4cdc58f2017-08-23 14:22:20 -0700340 uint32_t mForceSynchronous = 0;
341 uint32_t mTransactionNestCount = 0;
342 bool mAnimation = false;
Dan Stoza84d619e2018-03-28 17:07:36 -0700343 bool mEarlyWakeup = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700344
Marissa Wall78b72202019-03-15 14:58:34 -0700345 // Indicates that the Transaction contains a buffer that should be cached
346 bool mContainsBuffer = false;
347
Marissa Wall17b4e452018-12-26 16:32:34 -0800348 // mDesiredPresentTime is the time in nanoseconds that the client would like the transaction
349 // to be presented. When it is not possible to present at exactly that time, it will be
350 // presented after the time has passed.
351 //
352 // Desired present times that are more than 1 second in the future may be ignored.
353 // When a desired present time has already passed, the transaction will be presented as soon
354 // as possible.
355 //
356 // Transactions from the same process are presented in the same order that they are applied.
357 // The desired present time does not affect this ordering.
358 int64_t mDesiredPresentTime = -1;
359
chaviw273171b2018-12-26 11:46:30 -0800360 InputWindowCommands mInputWindowCommands;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700361 int mStatus = NO_ERROR;
362
Vishnu Nairf03652d2019-07-16 17:56:56 -0700363 layer_state_t* getLayerState(const sp<IBinder>& surfaceHandle);
364 layer_state_t* getLayerState(const sp<SurfaceControl>& sc) {
365 return getLayerState(sc->getHandle());
366 }
chaviw763ef572018-02-22 16:04:57 -0800367 DisplayState& getDisplayState(const sp<IBinder>& token);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700368
Marissa Wall78b72202019-03-15 14:58:34 -0700369 void cacheBuffers();
Marissa Wallc837b5e2018-10-12 10:04:44 -0700370 void registerSurfaceControlForCallback(const sp<SurfaceControl>& sc);
371
Robert Carr4cdc58f2017-08-23 14:22:20 -0700372 public:
373 Transaction() = default;
374 virtual ~Transaction() = default;
375 Transaction(Transaction const& other);
376
Vishnu Nair621102e2019-06-12 14:16:57 -0700377 // Factory method that creates a new Transaction instance from the parcel.
378 static std::unique_ptr<Transaction> createFromParcel(const Parcel* parcel);
379
380 status_t writeToParcel(Parcel* parcel) const override;
381 status_t readFromParcel(const Parcel* parcel) override;
382
Vishnu Nairfef244e2019-06-17 18:07:51 -0700383 // Clears the contents of the transaction without applying it.
384 void clear();
385
Robert Carr4cdc58f2017-08-23 14:22:20 -0700386 status_t apply(bool synchronous = false);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700387 // Merge another transaction in to this one, clearing other
388 // as if it had been applied.
389 Transaction& merge(Transaction&& other);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700390 Transaction& show(const sp<SurfaceControl>& sc);
391 Transaction& hide(const sp<SurfaceControl>& sc);
392 Transaction& setPosition(const sp<SurfaceControl>& sc,
393 float x, float y);
394 Transaction& setSize(const sp<SurfaceControl>& sc,
395 uint32_t w, uint32_t h);
396 Transaction& setLayer(const sp<SurfaceControl>& sc,
397 int32_t z);
398
399 // Sets a Z order relative to the Surface specified by "relativeTo" but
400 // without becoming a full child of the relative. Z-ordering works exactly
401 // as if it were a child however.
402 //
403 // As a nod to sanity, only non-child surfaces may have a relative Z-order.
404 //
405 // This overrides any previous call and is overriden by any future calls
406 // to setLayer.
407 //
408 // If the relative is removed, the Surface will have no layer and be
409 // invisible, until the next time set(Relative)Layer is called.
410 Transaction& setRelativeLayer(const sp<SurfaceControl>& sc,
411 const sp<IBinder>& relativeTo, int32_t z);
412 Transaction& setFlags(const sp<SurfaceControl>& sc,
413 uint32_t flags, uint32_t mask);
414 Transaction& setTransparentRegionHint(const sp<SurfaceControl>& sc,
415 const Region& transparentRegion);
416 Transaction& setAlpha(const sp<SurfaceControl>& sc,
417 float alpha);
418 Transaction& setMatrix(const sp<SurfaceControl>& sc,
419 float dsdx, float dtdx, float dtdy, float dsdy);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700420 Transaction& setCrop_legacy(const sp<SurfaceControl>& sc, const Rect& crop);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700421 Transaction& setCornerRadius(const sp<SurfaceControl>& sc, float cornerRadius);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700422 Transaction& setLayerStack(const sp<SurfaceControl>& sc, uint32_t layerStack);
Garfield Tan01a56132019-08-05 16:44:21 -0700423 Transaction& setMetadata(const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700424 // Defers applying any changes made in this transaction until the Layer
425 // identified by handle reaches the given frameNumber. If the Layer identified
426 // by handle is removed, then we will apply this transaction regardless of
427 // what frame number has been reached.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700428 Transaction& deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
429 const sp<IBinder>& handle, uint64_t frameNumber);
430 // A variant of deferTransactionUntil_legacy which identifies the Layer we wait for by
Robert Carr4cdc58f2017-08-23 14:22:20 -0700431 // Surface instead of Handle. Useful for clients which may not have the
432 // SurfaceControl for some of their Surfaces. Otherwise behaves identically.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700433 Transaction& deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
434 const sp<Surface>& barrierSurface,
435 uint64_t frameNumber);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700436 // Reparents all children of this layer to the new parent handle.
437 Transaction& reparentChildren(const sp<SurfaceControl>& sc,
438 const sp<IBinder>& newParentHandle);
439
440 /// Reparents the current layer to the new parent handle. The new parent must not be null.
441 // This can be used instead of reparentChildren if the caller wants to
442 // only re-parent a specific child.
443 Transaction& reparent(const sp<SurfaceControl>& sc,
444 const sp<IBinder>& newParentHandle);
445
446 Transaction& setColor(const sp<SurfaceControl>& sc, const half3& color);
447
Valerie Haudd0b7572019-01-29 14:59:27 -0800448 // Sets the background color of a layer with the specified color, alpha, and dataspace
449 Transaction& setBackgroundColor(const sp<SurfaceControl>& sc, const half3& color,
450 float alpha, ui::Dataspace dataspace);
Valerie Haued54efa2019-01-11 20:03:14 -0800451
Marissa Wall61c58622018-07-18 10:12:20 -0700452 Transaction& setTransform(const sp<SurfaceControl>& sc, uint32_t transform);
453 Transaction& setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
454 bool transformToDisplayInverse);
455 Transaction& setCrop(const sp<SurfaceControl>& sc, const Rect& crop);
Marissa Wall861616d2018-10-22 12:52:23 -0700456 Transaction& setFrame(const sp<SurfaceControl>& sc, const Rect& frame);
Marissa Wall61c58622018-07-18 10:12:20 -0700457 Transaction& setBuffer(const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer);
Marissa Wallebc2c052019-01-16 19:16:55 -0800458 Transaction& setCachedBuffer(const sp<SurfaceControl>& sc, int32_t bufferId);
Marissa Wall61c58622018-07-18 10:12:20 -0700459 Transaction& setAcquireFence(const sp<SurfaceControl>& sc, const sp<Fence>& fence);
460 Transaction& setDataspace(const sp<SurfaceControl>& sc, ui::Dataspace dataspace);
461 Transaction& setHdrMetadata(const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata);
462 Transaction& setSurfaceDamageRegion(const sp<SurfaceControl>& sc,
463 const Region& surfaceDamageRegion);
464 Transaction& setApi(const sp<SurfaceControl>& sc, int32_t api);
465 Transaction& setSidebandStream(const sp<SurfaceControl>& sc,
466 const sp<NativeHandle>& sidebandStream);
Marissa Wall17b4e452018-12-26 16:32:34 -0800467 Transaction& setDesiredPresentTime(nsecs_t desiredPresentTime);
Peiyong Linc502cb72019-03-01 15:00:23 -0800468 Transaction& setColorSpaceAgnostic(const sp<SurfaceControl>& sc, const bool agnostic);
Marissa Wall61c58622018-07-18 10:12:20 -0700469
Marissa Walle2ffb422018-10-12 11:33:52 -0700470 Transaction& addTransactionCompletedCallback(
471 TransactionCompletedCallbackTakesContext callback, void* callbackContext);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700472
Robert Carr4cdc58f2017-08-23 14:22:20 -0700473 // Detaches all child surfaces (and their children recursively)
474 // from their SurfaceControl.
475 // The child SurfaceControls will not throw exceptions or return errors,
476 // but transactions will have no effect.
477 // The child surfaces will continue to follow their parent surfaces,
478 // and remain eligible for rendering, but their relative state will be
479 // frozen. We use this in the WindowManager, in app shutdown/relaunch
480 // scenarios, where the app would otherwise clean up its child Surfaces.
481 // Sometimes the WindowManager needs to extend their lifetime slightly
482 // in order to perform an exit animation or prevent flicker.
483 Transaction& detachChildren(const sp<SurfaceControl>& sc);
484 // Set an override scaling mode as documented in <system/window.h>
485 // the override scaling mode will take precedence over any client
486 // specified scaling mode. -1 will clear the override scaling mode.
487 Transaction& setOverrideScalingMode(const sp<SurfaceControl>& sc,
488 int32_t overrideScalingMode);
489
Robert Carr2c358bf2018-08-08 15:58:15 -0700490#ifndef NO_INPUT
491 Transaction& setInputWindowInfo(const sp<SurfaceControl>& sc, const InputWindowInfo& info);
chaviw273171b2018-12-26 11:46:30 -0800492 Transaction& transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken);
chaviwa911b102019-02-14 10:18:33 -0800493 Transaction& syncInputWindows();
Robert Carr2c358bf2018-08-08 15:58:15 -0700494#endif
495
Peiyong Lind3788632018-09-18 16:01:31 -0700496 // Set a color transform matrix on the given layer on the built-in display.
497 Transaction& setColorTransform(const sp<SurfaceControl>& sc, const mat3& matrix,
498 const vec3& translation);
499
Robert Carrfb4d58b2019-01-15 09:21:27 -0800500 Transaction& setGeometry(const sp<SurfaceControl>& sc,
501 const Rect& source, const Rect& dst, int transform);
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700502 Transaction& setShadowRadius(const sp<SurfaceControl>& sc, float cornerRadius);
Robert Carrfb4d58b2019-01-15 09:21:27 -0800503
Robert Carr4cdc58f2017-08-23 14:22:20 -0700504 status_t setDisplaySurface(const sp<IBinder>& token,
505 const sp<IGraphicBufferProducer>& bufferProducer);
506
507 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
508
509 /* setDisplayProjection() defines the projection of layer stacks
510 * to a given display.
511 *
512 * - orientation defines the display's orientation.
513 * - layerStackRect defines which area of the window manager coordinate
514 * space will be used.
515 * - displayRect defines where on the display will layerStackRect be
516 * mapped to. displayRect is specified post-orientation, that is
517 * it uses the orientation seen by the end-user.
518 */
519 void setDisplayProjection(const sp<IBinder>& token,
520 uint32_t orientation,
521 const Rect& layerStackRect,
522 const Rect& displayRect);
523 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
524 void setAnimationTransaction();
Dan Stoza84d619e2018-03-28 17:07:36 -0700525 void setEarlyWakeup();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700526 };
Robert Carr82364e32016-05-15 11:27:47 -0700527
Svetoslavd85084b2014-03-20 10:28:31 -0700528 status_t clearLayerFrameStats(const sp<IBinder>& token) const;
529 status_t getLayerFrameStats(const sp<IBinder>& token, FrameStats* outStats) const;
Svetoslavd85084b2014-03-20 10:28:31 -0700530 static status_t clearAnimationFrameStats();
531 static status_t getAnimationFrameStats(FrameStats* outStats);
532
Dan Stozac4f471e2016-03-24 09:31:08 -0700533 static status_t getHdrCapabilities(const sp<IBinder>& display,
534 HdrCapabilities* outCapabilities);
535
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700536 static void setDisplayProjection(const sp<IBinder>& token,
537 uint32_t orientation,
538 const Rect& layerStackRect,
539 const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700540
Robert Carr4cdc58f2017-08-23 14:22:20 -0700541 inline sp<ISurfaceComposerClient> getClient() { return mClient; }
542
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700543 static status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
544 ui::PixelFormat* outFormat,
545 ui::Dataspace* outDataspace,
546 uint8_t* outComponentMask);
Kevin DuBois74e53772018-11-19 10:52:38 -0800547 static status_t setDisplayContentSamplingEnabled(const sp<IBinder>& display, bool enable,
548 uint8_t componentMask, uint64_t maxFrames);
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700549
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700550 static status_t getDisplayedContentSample(const sp<IBinder>& display, uint64_t maxFrames,
551 uint64_t timestamp, DisplayedFrameStats* outStats);
Kevin DuBois00c66832019-02-18 16:21:31 -0800552 static status_t addRegionSamplingListener(const Rect& samplingArea,
553 const sp<IBinder>& stopLayerHandle,
554 const sp<IRegionSamplingListener>& listener);
555 static status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener);
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700556
Mathias Agopian631f3582010-05-25 17:51:34 -0700557private:
Mathias Agopiand4784a32010-05-27 19:41:15 -0700558 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559
Mathias Agopian698c0872011-06-28 19:09:31 -0700560 mutable Mutex mLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 status_t mStatus;
Mathias Agopian7e27f052010-05-28 14:22:23 -0700562 sp<ISurfaceComposerClient> mClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563};
564
Mathias Agopiand4784a32010-05-27 19:41:15 -0700565// ---------------------------------------------------------------------------
Mathias Agopian74c40c02010-09-29 13:02:36 -0700566
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000567class ScreenshotClient {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800568public:
Dan Stozac1879002014-05-22 15:59:05 -0700569 // if cropping isn't required, callers may pass in a default Rect, e.g.:
570 // capture(display, producer, Rect(), reqWidth, ...);
Peiyong Lin0e003c92018-09-17 11:09:51 -0700571 static status_t capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
572 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
573 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Robert Carr108b2c72019-04-02 16:32:58 -0700574 uint32_t rotation, bool captureSecureLayers,
575 sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers);
Robert Carrfa8855f2019-02-19 10:05:00 -0800576 static status_t capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
577 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
578 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700579 uint32_t rotation, sp<GraphicBuffer>* outBuffer);
chaviw93df2ea2019-04-30 16:45:12 -0700580 static status_t capture(uint64_t displayOrLayerStack, ui::Dataspace* outDataspace,
581 sp<GraphicBuffer>* outBuffer);
Peiyong Lin0e003c92018-09-17 11:09:51 -0700582 static status_t captureLayers(const sp<IBinder>& layerHandle, const ui::Dataspace reqDataSpace,
583 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
584 float frameScale, sp<GraphicBuffer>* outBuffer);
Robert Carr866455f2019-04-02 16:28:26 -0700585 static status_t captureChildLayers(
586 const sp<IBinder>& layerHandle, const ui::Dataspace reqDataSpace,
587 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
588 const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>&
589 excludeHandles,
590 float frameScale, sp<GraphicBuffer>* outBuffer);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700591};
592
593// ---------------------------------------------------------------------------
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700594
Marissa Wall80d94ad2019-01-18 16:04:36 -0800595class TransactionCompletedListener : public BnTransactionCompletedListener {
596 TransactionCompletedListener();
597
598 CallbackId getNextIdLocked() REQUIRES(mMutex);
599
600 std::mutex mMutex;
601
602 bool mListening GUARDED_BY(mMutex) = false;
603
604 CallbackId mCallbackIdCounter GUARDED_BY(mMutex) = 1;
605
Marissa Wall80d94ad2019-01-18 16:04:36 -0800606 struct CallbackTranslation {
607 TransactionCompletedCallback callbackFunction;
Vishnu Nairf03652d2019-07-16 17:56:56 -0700608 std::unordered_map<sp<IBinder>, sp<SurfaceControl>, SurfaceComposerClient::IBinderHash>
609 surfaceControls;
Marissa Wall80d94ad2019-01-18 16:04:36 -0800610 };
611
612 std::unordered_map<CallbackId, CallbackTranslation> mCallbacks GUARDED_BY(mMutex);
613
614public:
615 static sp<TransactionCompletedListener> getInstance();
616 static sp<ITransactionCompletedListener> getIInstance();
617
618 void startListeningLocked() REQUIRES(mMutex);
619
620 CallbackId addCallbackFunction(
621 const TransactionCompletedCallback& callbackFunction,
622 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
623 surfaceControls);
624
625 void addSurfaceControlToCallbacks(const sp<SurfaceControl>& surfaceControl,
626 const std::unordered_set<CallbackId>& callbackIds);
627
628 // Overrides BnTransactionCompletedListener's onTransactionCompleted
629 void onTransactionCompleted(ListenerStats stats) override;
630};
631
632// ---------------------------------------------------------------------------
633
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634}; // namespace android
635
Mathias Agopian90ac7992012-02-25 18:48:35 -0800636#endif // ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H