blob: 377fe68c4154de4af693680affff5981903385ca [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 Linfd997e02018-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,
72 void* cookie = NULL, uint32_t flags = 0);
73
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 Linfd997e02018-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 Linfd997e02018-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 Linfd997e02018-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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 // ------------------------------------------------------------------------
105 // surface creation / destruction
106
107 //! Create a surface
Mathias Agopian01b76682009-04-16 20:04:08 -0700108 sp<SurfaceControl> createSurface(
Mathias Agopian285dbde2010-03-01 16:09:43 -0800109 const String8& name,// name of the surface
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700110 uint32_t w, // width in pixel
111 uint32_t h, // height in pixel
112 PixelFormat format, // pixel-format desired
Albert Chaulk479c60c2017-01-27 14:21:34 -0500113 uint32_t flags = 0, // usage flags
114 SurfaceControl* parent = nullptr, // parent
rongliuccd34842018-03-14 12:26:23 -0700115 int32_t windowType = -1, // from WindowManager.java (STATUS_BAR, INPUT_METHOD, etc.)
116 int32_t ownerUid = -1 // UID of the task
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 );
118
Robert Carr3b382ed2018-03-14 13:49:41 -0700119 status_t createSurfaceChecked(
120 const String8& name,// name of the surface
121 uint32_t w, // width in pixel
122 uint32_t h, // height in pixel
123 PixelFormat format, // pixel-format desired
124 sp<SurfaceControl>* outSurface,
125 uint32_t flags = 0, // usage flags
126 SurfaceControl* parent = nullptr, // parent
rongliuccd34842018-03-14 12:26:23 -0700127 int32_t windowType = -1, // from WindowManager.java (STATUS_BAR, INPUT_METHOD, etc.)
128 int32_t ownerUid = -1 // UID of the task
Robert Carr3b382ed2018-03-14 13:49:41 -0700129 );
130
Jesse Hall6c913be2013-08-08 12:15:49 -0700131 //! Create a virtual display
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700132 static sp<IBinder> createDisplay(const String8& displayName, bool secure);
Mathias Agopian285dbde2010-03-01 16:09:43 -0800133
Jesse Hall6c913be2013-08-08 12:15:49 -0700134 //! Destroy a virtual display
135 static void destroyDisplay(const sp<IBinder>& display);
136
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700137 //! Get the token for the existing default displays.
138 //! Possible values for id are eDisplayIdMain and eDisplayIdHdmi.
139 static sp<IBinder> getBuiltInDisplay(int32_t id);
140
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700141 static status_t enableVSyncInjections(bool enable);
142
143 static status_t injectVSync(nsecs_t when);
144
chaviw8e3fe5d2018-02-22 10:55:42 -0800145 struct SCHash {
146 std::size_t operator()(const sp<SurfaceControl>& sc) const {
147 return std::hash<SurfaceControl *>{}(sc.get());
148 }
149 };
150
Robert Carr4cdc58f2017-08-23 14:22:20 -0700151 class Transaction {
chaviw8e3fe5d2018-02-22 10:55:42 -0800152 std::unordered_map<sp<SurfaceControl>, ComposerState, SCHash> mComposerStates;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700153 SortedVector<DisplayState > mDisplayStates;
154 uint32_t mForceSynchronous = 0;
155 uint32_t mTransactionNestCount = 0;
156 bool mAnimation = false;
Dan Stoza2713c302018-03-28 17:07:36 -0700157 bool mEarlyWakeup = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700158
Robert Carr4cdc58f2017-08-23 14:22:20 -0700159 int mStatus = NO_ERROR;
160
chaviw763ef572018-02-22 16:04:57 -0800161 layer_state_t* getLayerState(const sp<SurfaceControl>& sc);
162 DisplayState& getDisplayState(const sp<IBinder>& token);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700163
164 public:
165 Transaction() = default;
166 virtual ~Transaction() = default;
167 Transaction(Transaction const& other);
168
169 status_t apply(bool synchronous = false);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700170 // Merge another transaction in to this one, clearing other
171 // as if it had been applied.
172 Transaction& merge(Transaction&& other);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700173 Transaction& show(const sp<SurfaceControl>& sc);
174 Transaction& hide(const sp<SurfaceControl>& sc);
175 Transaction& setPosition(const sp<SurfaceControl>& sc,
176 float x, float y);
177 Transaction& setSize(const sp<SurfaceControl>& sc,
178 uint32_t w, uint32_t h);
179 Transaction& setLayer(const sp<SurfaceControl>& sc,
180 int32_t z);
181
182 // Sets a Z order relative to the Surface specified by "relativeTo" but
183 // without becoming a full child of the relative. Z-ordering works exactly
184 // as if it were a child however.
185 //
186 // As a nod to sanity, only non-child surfaces may have a relative Z-order.
187 //
188 // This overrides any previous call and is overriden by any future calls
189 // to setLayer.
190 //
191 // If the relative is removed, the Surface will have no layer and be
192 // invisible, until the next time set(Relative)Layer is called.
193 Transaction& setRelativeLayer(const sp<SurfaceControl>& sc,
194 const sp<IBinder>& relativeTo, int32_t z);
195 Transaction& setFlags(const sp<SurfaceControl>& sc,
196 uint32_t flags, uint32_t mask);
197 Transaction& setTransparentRegionHint(const sp<SurfaceControl>& sc,
198 const Region& transparentRegion);
199 Transaction& setAlpha(const sp<SurfaceControl>& sc,
200 float alpha);
201 Transaction& setMatrix(const sp<SurfaceControl>& sc,
202 float dsdx, float dtdx, float dtdy, float dsdy);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700203 Transaction& setCrop(const sp<SurfaceControl>& sc, const Rect& crop);
204 Transaction& setFinalCrop(const sp<SurfaceControl>& sc, const Rect& crop);
205 Transaction& setLayerStack(const sp<SurfaceControl>& sc, uint32_t layerStack);
206 // Defers applying any changes made in this transaction until the Layer
207 // identified by handle reaches the given frameNumber. If the Layer identified
208 // by handle is removed, then we will apply this transaction regardless of
209 // what frame number has been reached.
210 Transaction& deferTransactionUntil(const sp<SurfaceControl>& sc,
211 const sp<IBinder>& handle,
212 uint64_t frameNumber);
213 // A variant of deferTransactionUntil which identifies the Layer we wait for by
214 // Surface instead of Handle. Useful for clients which may not have the
215 // SurfaceControl for some of their Surfaces. Otherwise behaves identically.
216 Transaction& deferTransactionUntil(const sp<SurfaceControl>& sc,
217 const sp<Surface>& barrierSurface,
218 uint64_t frameNumber);
219 // Reparents all children of this layer to the new parent handle.
220 Transaction& reparentChildren(const sp<SurfaceControl>& sc,
221 const sp<IBinder>& newParentHandle);
222
223 /// Reparents the current layer to the new parent handle. The new parent must not be null.
224 // This can be used instead of reparentChildren if the caller wants to
225 // only re-parent a specific child.
226 Transaction& reparent(const sp<SurfaceControl>& sc,
227 const sp<IBinder>& newParentHandle);
228
229 Transaction& setColor(const sp<SurfaceControl>& sc, const half3& color);
230
231 // Detaches all child surfaces (and their children recursively)
232 // from their SurfaceControl.
233 // The child SurfaceControls will not throw exceptions or return errors,
234 // but transactions will have no effect.
235 // The child surfaces will continue to follow their parent surfaces,
236 // and remain eligible for rendering, but their relative state will be
237 // frozen. We use this in the WindowManager, in app shutdown/relaunch
238 // scenarios, where the app would otherwise clean up its child Surfaces.
239 // Sometimes the WindowManager needs to extend their lifetime slightly
240 // in order to perform an exit animation or prevent flicker.
241 Transaction& detachChildren(const sp<SurfaceControl>& sc);
242 // Set an override scaling mode as documented in <system/window.h>
243 // the override scaling mode will take precedence over any client
244 // specified scaling mode. -1 will clear the override scaling mode.
245 Transaction& setOverrideScalingMode(const sp<SurfaceControl>& sc,
246 int32_t overrideScalingMode);
247
248 // If the size changes in this transaction, all geometry updates specified
249 // in this transaction will not complete until a buffer of the new size
250 // arrives. As some elements normally apply immediately, this enables
251 // freezing the total geometry of a surface until a resize is completed.
252 Transaction& setGeometryAppliesWithResize(const sp<SurfaceControl>& sc);
253
chaviwca27f252018-02-06 16:46:39 -0800254 Transaction& destroySurface(const sp<SurfaceControl>& sc);
255
Robert Carr4cdc58f2017-08-23 14:22:20 -0700256 status_t setDisplaySurface(const sp<IBinder>& token,
257 const sp<IGraphicBufferProducer>& bufferProducer);
258
259 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
260
261 /* setDisplayProjection() defines the projection of layer stacks
262 * to a given display.
263 *
264 * - orientation defines the display's orientation.
265 * - layerStackRect defines which area of the window manager coordinate
266 * space will be used.
267 * - displayRect defines where on the display will layerStackRect be
268 * mapped to. displayRect is specified post-orientation, that is
269 * it uses the orientation seen by the end-user.
270 */
271 void setDisplayProjection(const sp<IBinder>& token,
272 uint32_t orientation,
273 const Rect& layerStackRect,
274 const Rect& displayRect);
275 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
276 void setAnimationTransaction();
Dan Stoza2713c302018-03-28 17:07:36 -0700277 void setEarlyWakeup();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700278 };
Robert Carr82364e32016-05-15 11:27:47 -0700279
Mathias Agopianac9fa422013-02-11 16:40:36 -0800280 status_t destroySurface(const sp<IBinder>& id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281
Svetoslavd85084b2014-03-20 10:28:31 -0700282 status_t clearLayerFrameStats(const sp<IBinder>& token) const;
283 status_t getLayerFrameStats(const sp<IBinder>& token, FrameStats* outStats) const;
Svetoslavd85084b2014-03-20 10:28:31 -0700284 static status_t clearAnimationFrameStats();
285 static status_t getAnimationFrameStats(FrameStats* outStats);
286
Dan Stozac4f471e2016-03-24 09:31:08 -0700287 static status_t getHdrCapabilities(const sp<IBinder>& display,
288 HdrCapabilities* outCapabilities);
289
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700290 static void setDisplayProjection(const sp<IBinder>& token,
291 uint32_t orientation,
292 const Rect& layerStackRect,
293 const Rect& displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700294
Robert Carr4cdc58f2017-08-23 14:22:20 -0700295 inline sp<ISurfaceComposerClient> getClient() { return mClient; }
296
Mathias Agopian631f3582010-05-25 17:51:34 -0700297private:
Mathias Agopiand4784a32010-05-27 19:41:15 -0700298 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
Mathias Agopian698c0872011-06-28 19:09:31 -0700300 mutable Mutex mLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 status_t mStatus;
Mathias Agopian7e27f052010-05-28 14:22:23 -0700302 sp<ISurfaceComposerClient> mClient;
Robert Carr1db73f62016-12-21 12:58:51 -0800303 wp<IGraphicBufferProducer> mParent;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304};
305
Mathias Agopiand4784a32010-05-27 19:41:15 -0700306// ---------------------------------------------------------------------------
Mathias Agopian74c40c02010-09-29 13:02:36 -0700307
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000308class ScreenshotClient {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800309public:
Dan Stozac1879002014-05-22 15:59:05 -0700310 // if cropping isn't required, callers may pass in a default Rect, e.g.:
311 // capture(display, producer, Rect(), reqWidth, ...);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000312 static status_t capture(const sp<IBinder>& display, Rect sourceCrop, uint32_t reqWidth,
313 uint32_t reqHeight, int32_t minLayerZ, int32_t maxLayerZ,
314 bool useIdentityTransform, uint32_t rotation,
315 sp<GraphicBuffer>* outBuffer);
Robert Carr578038f2018-03-09 12:25:24 -0800316 static status_t captureLayers(const sp<IBinder>& layerHandle, Rect sourceCrop, float frameScale,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000317 sp<GraphicBuffer>* outBuffer);
Robert Carr578038f2018-03-09 12:25:24 -0800318 static status_t captureChildLayers(const sp<IBinder>& layerHandle, Rect sourceCrop,
319 float frameScale, sp<GraphicBuffer>* outBuffer);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700320};
321
322// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323}; // namespace android
324
Mathias Agopian90ac7992012-02-25 18:48:35 -0800325#endif // ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H