blob: 662acc9acf8933f9ef32610e92528c62c4be0ca0 [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>
chaviw8e3fe5d2018-02-22 10:55:42 -080022#include <unordered_map>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Mathias Agopiandd3423c2009-09-23 15:44:05 -070024#include <binder/IBinder.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <utils/RefBase.h>
Mathias Agopianb7e930d2010-06-01 15:12:58 -070027#include <utils/Singleton.h>
28#include <utils/SortedVector.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/threads.h>
30
Svetoslavd85084b2014-03-20 10:28:31 -070031#include <ui/FrameStats.h>
Peiyong Lin9f034472018-03-28 15:29:00 -070032#include <ui/GraphicTypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080034
Mathias Agopianabe815d2013-03-19 22:22:21 -070035#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036#include <gui/SurfaceControl.h>
chaviw13fdc492017-06-27 12:40:18 -070037#include <math/vec3.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070038#include <gui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40namespace android {
41
42// ---------------------------------------------------------------------------
43
Colin Crossa2362b42016-09-26 13:48:25 -070044struct DisplayInfo;
Dan Stozac4f471e2016-03-24 09:31:08 -070045class HdrCapabilities;
Mathias Agopian41f673c2011-11-17 17:48:35 -080046class ISurfaceComposerClient;
Andy McFadden2adaf042012-12-18 09:49:45 -080047class IGraphicBufferProducer;
Mathias Agopiana67932f2011-04-20 14:20:59 -070048class Region;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070049
50// ---------------------------------------------------------------------------
51
Mathias Agopiand4784a32010-05-27 19:41:15 -070052class SurfaceComposerClient : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053{
Mathias Agopian698c0872011-06-28 19:09:31 -070054 friend class Composer;
Jesse Hall6c913be2013-08-08 12:15:49 -070055public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056 SurfaceComposerClient();
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +010057 SurfaceComposerClient(const sp<ISurfaceComposerClient>& client);
Robert Carr1db73f62016-12-21 12:58:51 -080058 SurfaceComposerClient(const sp<IGraphicBufferProducer>& parent);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 virtual ~SurfaceComposerClient();
60
61 // Always make sure we could initialize
62 status_t initCheck() const;
63
64 // Return the connection of this client
65 sp<IBinder> connection() const;
Jesse Hall6c913be2013-08-08 12:15:49 -070066
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 // Forcibly remove connection before all references have gone away.
68 void dispose();
69
Mathias Agopiane57f2922012-08-09 16:29:12 -070070 // callback when the composer is dies
71 status_t linkToComposerDeath(const sp<IBinder::DeathRecipient>& recipient,
Yi Konga03e0442018-07-17 11:16:57 -070072 void* cookie = nullptr, uint32_t flags = 0);
Mathias Agopiane57f2922012-08-09 16:29:12 -070073
Dan Stoza7f7da322014-05-02 15:26:25 -070074 // Get a list of supported configurations for a given display
75 static status_t getDisplayConfigs(const sp<IBinder>& display,
76 Vector<DisplayInfo>* configs);
77
78 // Get the DisplayInfo for the currently-active configuration
79 static status_t getDisplayInfo(const sp<IBinder>& display,
80 DisplayInfo* info);
81
82 // Get the index of the current active configuration (relative to the list
83 // returned by getDisplayInfo)
84 static int getActiveConfig(const sp<IBinder>& display);
85
86 // Set a new active configuration using an index relative to the list
87 // returned by getDisplayInfo
88 static status_t setActiveConfig(const sp<IBinder>& display, int id);
Mathias Agopiane57f2922012-08-09 16:29:12 -070089
Michael Wright28f24d02016-07-12 13:30:53 -070090 // Gets the list of supported color modes for the given display
91 static status_t getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lin9f034472018-03-28 15:29:00 -070092 Vector<ui::ColorMode>* outColorModes);
Michael Wright28f24d02016-07-12 13:30:53 -070093
94 // Gets the active color mode for the given display
Peiyong Lin9f034472018-03-28 15:29:00 -070095 static ui::ColorMode getActiveColorMode(const sp<IBinder>& display);
Michael Wright28f24d02016-07-12 13:30:53 -070096
97 // Sets the active color mode for the given display
Peiyong Lin9f034472018-03-28 15:29:00 -070098 static status_t setActiveColorMode(const sp<IBinder>& display,
99 ui::ColorMode colorMode);
Michael Wright28f24d02016-07-12 13:30:53 -0700100
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700101 /* Triggers screen on/off or low power mode and waits for it to complete */
102 static void setDisplayPowerMode(const sp<IBinder>& display, int mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700103
Peiyong Lin0256f722018-08-31 15:45:10 -0700104 //
105 static status_t getCompositionPreference(ui::Dataspace* dataSpace,
106 ui::PixelFormat* pixelFormat);
107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 // ------------------------------------------------------------------------
109 // surface creation / destruction
110
111 //! Create a surface
Mathias Agopian01b76682009-04-16 20:04:08 -0700112 sp<SurfaceControl> createSurface(
Mathias Agopian285dbde2010-03-01 16:09:43 -0800113 const String8& name,// name of the surface
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700114 uint32_t w, // width in pixel
115 uint32_t h, // height in pixel
116 PixelFormat format, // pixel-format desired
Albert Chaulk479c60c2017-01-27 14:21:34 -0500117 uint32_t flags = 0, // usage flags
118 SurfaceControl* parent = nullptr, // parent
rongliucfb187b2018-03-14 12:26:23 -0700119 int32_t windowType = -1, // from WindowManager.java (STATUS_BAR, INPUT_METHOD, etc.)
120 int32_t ownerUid = -1 // UID of the task
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 );
122
Robert Carr3b382ed2018-03-14 13:49:41 -0700123 status_t createSurfaceChecked(
124 const String8& name,// name of the surface
125 uint32_t w, // width in pixel
126 uint32_t h, // height in pixel
127 PixelFormat format, // pixel-format desired
128 sp<SurfaceControl>* outSurface,
129 uint32_t flags = 0, // usage flags
130 SurfaceControl* parent = nullptr, // parent
rongliucfb187b2018-03-14 12:26:23 -0700131 int32_t windowType = -1, // from WindowManager.java (STATUS_BAR, INPUT_METHOD, etc.)
132 int32_t ownerUid = -1 // UID of the task
Robert Carr3b382ed2018-03-14 13:49:41 -0700133 );
134
Jesse Hall6c913be2013-08-08 12:15:49 -0700135 //! Create a virtual display
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700136 static sp<IBinder> createDisplay(const String8& displayName, bool secure);
Mathias Agopian285dbde2010-03-01 16:09:43 -0800137
Jesse Hall6c913be2013-08-08 12:15:49 -0700138 //! Destroy a virtual display
139 static void destroyDisplay(const sp<IBinder>& display);
140
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700141 //! Get the token for the existing default displays.
142 //! Possible values for id are eDisplayIdMain and eDisplayIdHdmi.
143 static sp<IBinder> getBuiltInDisplay(int32_t id);
144
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700145 static status_t enableVSyncInjections(bool enable);
146
147 static status_t injectVSync(nsecs_t when);
148
chaviw8e3fe5d2018-02-22 10:55:42 -0800149 struct SCHash {
150 std::size_t operator()(const sp<SurfaceControl>& sc) const {
151 return std::hash<SurfaceControl *>{}(sc.get());
152 }
153 };
154
Robert Carr4cdc58f2017-08-23 14:22:20 -0700155 class Transaction {
chaviw8e3fe5d2018-02-22 10:55:42 -0800156 std::unordered_map<sp<SurfaceControl>, ComposerState, SCHash> mComposerStates;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700157 SortedVector<DisplayState > mDisplayStates;
158 uint32_t mForceSynchronous = 0;
159 uint32_t mTransactionNestCount = 0;
160 bool mAnimation = false;
Dan Stoza84d619e2018-03-28 17:07:36 -0700161 bool mEarlyWakeup = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700162
Robert Carr4cdc58f2017-08-23 14:22:20 -0700163 int mStatus = NO_ERROR;
164
chaviw763ef572018-02-22 16:04:57 -0800165 layer_state_t* getLayerState(const sp<SurfaceControl>& sc);
166 DisplayState& getDisplayState(const sp<IBinder>& token);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700167
168 public:
169 Transaction() = default;
170 virtual ~Transaction() = default;
171 Transaction(Transaction const& other);
172
173 status_t apply(bool synchronous = false);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700174 // Merge another transaction in to this one, clearing other
175 // as if it had been applied.
176 Transaction& merge(Transaction&& other);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700177 Transaction& show(const sp<SurfaceControl>& sc);
178 Transaction& hide(const sp<SurfaceControl>& sc);
179 Transaction& setPosition(const sp<SurfaceControl>& sc,
180 float x, float y);
181 Transaction& setSize(const sp<SurfaceControl>& sc,
182 uint32_t w, uint32_t h);
183 Transaction& setLayer(const sp<SurfaceControl>& sc,
184 int32_t z);
185
186 // Sets a Z order relative to the Surface specified by "relativeTo" but
187 // without becoming a full child of the relative. Z-ordering works exactly
188 // as if it were a child however.
189 //
190 // As a nod to sanity, only non-child surfaces may have a relative Z-order.
191 //
192 // This overrides any previous call and is overriden by any future calls
193 // to setLayer.
194 //
195 // If the relative is removed, the Surface will have no layer and be
196 // invisible, until the next time set(Relative)Layer is called.
197 Transaction& setRelativeLayer(const sp<SurfaceControl>& sc,
198 const sp<IBinder>& relativeTo, int32_t z);
199 Transaction& setFlags(const sp<SurfaceControl>& sc,
200 uint32_t flags, uint32_t mask);
201 Transaction& setTransparentRegionHint(const sp<SurfaceControl>& sc,
202 const Region& transparentRegion);
203 Transaction& setAlpha(const sp<SurfaceControl>& sc,
204 float alpha);
205 Transaction& setMatrix(const sp<SurfaceControl>& sc,
206 float dsdx, float dtdx, float dtdy, float dsdy);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700207 Transaction& setCrop_legacy(const sp<SurfaceControl>& sc, const Rect& crop);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700208 Transaction& setLayerStack(const sp<SurfaceControl>& sc, uint32_t layerStack);
209 // Defers applying any changes made in this transaction until the Layer
210 // identified by handle reaches the given frameNumber. If the Layer identified
211 // by handle is removed, then we will apply this transaction regardless of
212 // what frame number has been reached.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700213 Transaction& deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
214 const sp<IBinder>& handle, uint64_t frameNumber);
215 // A variant of deferTransactionUntil_legacy which identifies the Layer we wait for by
Robert Carr4cdc58f2017-08-23 14:22:20 -0700216 // Surface instead of Handle. Useful for clients which may not have the
217 // SurfaceControl for some of their Surfaces. Otherwise behaves identically.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700218 Transaction& deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
219 const sp<Surface>& barrierSurface,
220 uint64_t frameNumber);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700221 // Reparents all children of this layer to the new parent handle.
222 Transaction& reparentChildren(const sp<SurfaceControl>& sc,
223 const sp<IBinder>& newParentHandle);
224
225 /// Reparents the current layer to the new parent handle. The new parent must not be null.
226 // This can be used instead of reparentChildren if the caller wants to
227 // only re-parent a specific child.
228 Transaction& reparent(const sp<SurfaceControl>& sc,
229 const sp<IBinder>& newParentHandle);
230
231 Transaction& setColor(const sp<SurfaceControl>& sc, const half3& color);
232
Marissa Wall61c58622018-07-18 10:12:20 -0700233 Transaction& setTransform(const sp<SurfaceControl>& sc, uint32_t transform);
234 Transaction& setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
235 bool transformToDisplayInverse);
236 Transaction& setCrop(const sp<SurfaceControl>& sc, const Rect& crop);
237 Transaction& setBuffer(const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer);
238 Transaction& setAcquireFence(const sp<SurfaceControl>& sc, const sp<Fence>& fence);
239 Transaction& setDataspace(const sp<SurfaceControl>& sc, ui::Dataspace dataspace);
240 Transaction& setHdrMetadata(const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata);
241 Transaction& setSurfaceDamageRegion(const sp<SurfaceControl>& sc,
242 const Region& surfaceDamageRegion);
243 Transaction& setApi(const sp<SurfaceControl>& sc, int32_t api);
244 Transaction& setSidebandStream(const sp<SurfaceControl>& sc,
245 const sp<NativeHandle>& sidebandStream);
246
Robert Carr4cdc58f2017-08-23 14:22:20 -0700247 // Detaches all child surfaces (and their children recursively)
248 // from their SurfaceControl.
249 // The child SurfaceControls will not throw exceptions or return errors,
250 // but transactions will have no effect.
251 // The child surfaces will continue to follow their parent surfaces,
252 // and remain eligible for rendering, but their relative state will be
253 // frozen. We use this in the WindowManager, in app shutdown/relaunch
254 // scenarios, where the app would otherwise clean up its child Surfaces.
255 // Sometimes the WindowManager needs to extend their lifetime slightly
256 // in order to perform an exit animation or prevent flicker.
257 Transaction& detachChildren(const sp<SurfaceControl>& sc);
258 // Set an override scaling mode as documented in <system/window.h>
259 // the override scaling mode will take precedence over any client
260 // specified scaling mode. -1 will clear the override scaling mode.
261 Transaction& setOverrideScalingMode(const sp<SurfaceControl>& sc,
262 int32_t overrideScalingMode);
263
264 // If the size changes in this transaction, all geometry updates specified
265 // in this transaction will not complete until a buffer of the new size
266 // arrives. As some elements normally apply immediately, this enables
267 // freezing the total geometry of a surface until a resize is completed.
268 Transaction& setGeometryAppliesWithResize(const sp<SurfaceControl>& sc);
269
chaviwca27f252018-02-06 16:46:39 -0800270 Transaction& destroySurface(const sp<SurfaceControl>& sc);
271
Robert Carr4cdc58f2017-08-23 14:22:20 -0700272 status_t setDisplaySurface(const sp<IBinder>& token,
273 const sp<IGraphicBufferProducer>& bufferProducer);
274
275 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
276
277 /* setDisplayProjection() defines the projection of layer stacks
278 * to a given display.
279 *
280 * - orientation defines the display's orientation.
281 * - layerStackRect defines which area of the window manager coordinate
282 * space will be used.
283 * - displayRect defines where on the display will layerStackRect be
284 * mapped to. displayRect is specified post-orientation, that is
285 * it uses the orientation seen by the end-user.
286 */
287 void setDisplayProjection(const sp<IBinder>& token,
288 uint32_t orientation,
289 const Rect& layerStackRect,
290 const Rect& displayRect);
291 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
292 void setAnimationTransaction();
Dan Stoza84d619e2018-03-28 17:07:36 -0700293 void setEarlyWakeup();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700294 };
Robert Carr82364e32016-05-15 11:27:47 -0700295
Mathias Agopianac9fa422013-02-11 16:40:36 -0800296 status_t destroySurface(const sp<IBinder>& id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297
Svetoslavd85084b2014-03-20 10:28:31 -0700298 status_t clearLayerFrameStats(const sp<IBinder>& token) const;
299 status_t getLayerFrameStats(const sp<IBinder>& token, FrameStats* outStats) const;
Svetoslavd85084b2014-03-20 10:28:31 -0700300 static status_t clearAnimationFrameStats();
301 static status_t getAnimationFrameStats(FrameStats* outStats);
302
Dan Stozac4f471e2016-03-24 09:31:08 -0700303 static status_t getHdrCapabilities(const sp<IBinder>& display,
304 HdrCapabilities* outCapabilities);
305
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700306 static void setDisplayProjection(const sp<IBinder>& token,
307 uint32_t orientation,
308 const Rect& layerStackRect,
309 const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700310
Robert Carr4cdc58f2017-08-23 14:22:20 -0700311 inline sp<ISurfaceComposerClient> getClient() { return mClient; }
312
Mathias Agopian631f3582010-05-25 17:51:34 -0700313private:
Mathias Agopiand4784a32010-05-27 19:41:15 -0700314 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
Mathias Agopian698c0872011-06-28 19:09:31 -0700316 mutable Mutex mLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 status_t mStatus;
Mathias Agopian7e27f052010-05-28 14:22:23 -0700318 sp<ISurfaceComposerClient> mClient;
Robert Carr1db73f62016-12-21 12:58:51 -0800319 wp<IGraphicBufferProducer> mParent;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320};
321
Mathias Agopiand4784a32010-05-27 19:41:15 -0700322// ---------------------------------------------------------------------------
Mathias Agopian74c40c02010-09-29 13:02:36 -0700323
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000324class ScreenshotClient {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800325public:
Dan Stozac1879002014-05-22 15:59:05 -0700326 // if cropping isn't required, callers may pass in a default Rect, e.g.:
327 // capture(display, producer, Rect(), reqWidth, ...);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000328 static status_t capture(const sp<IBinder>& display, Rect sourceCrop, uint32_t reqWidth,
329 uint32_t reqHeight, int32_t minLayerZ, int32_t maxLayerZ,
330 bool useIdentityTransform, uint32_t rotation,
331 sp<GraphicBuffer>* outBuffer);
Robert Carr578038f2018-03-09 12:25:24 -0800332 static status_t captureLayers(const sp<IBinder>& layerHandle, Rect sourceCrop, float frameScale,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000333 sp<GraphicBuffer>* outBuffer);
Robert Carr578038f2018-03-09 12:25:24 -0800334 static status_t captureChildLayers(const sp<IBinder>& layerHandle, Rect sourceCrop,
335 float frameScale, sp<GraphicBuffer>* outBuffer);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700336};
337
338// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339}; // namespace android
340
Mathias Agopian90ac7992012-02-25 18:48:35 -0800341#endif // ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H