blob: 0efdf5441848041c3f77cfcd99a5c7e7e688e625 [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
17#ifndef ANDROID_LAYER_H
18#define ANDROID_LAYER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian076b1cc2009-04-10 14:24:30 -070023#include <EGL/egl.h>
24#include <EGL/eglext.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070025
Mathias Agopian13127d82013-03-05 17:47:11 -080026#include <utils/RefBase.h>
27#include <utils/String8.h>
28#include <utils/Timers.h>
29
Svetoslavd85084b2014-03-20 10:28:31 -070030#include <ui/FrameStats.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080031#include <ui/GraphicBuffer.h>
32#include <ui/PixelFormat.h>
33#include <ui/Region.h>
34
35#include <gui/ISurfaceComposerClient.h>
36
37#include <private/gui/LayerState.h>
38
Dan Stoza7dde5992015-05-22 09:51:44 -070039#include <list>
40
Jamie Gennis82dbc742012-11-08 19:23:28 -080041#include "FrameTracker.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080042#include "Client.h"
Robert Carr1f0a16a2016-10-24 16:27:39 -070043#include "LayerVector.h"
Dan Stozab9b08832014-03-13 11:55:57 -070044#include "MonitoredProducer.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080045#include "SurfaceFlinger.h"
46#include "SurfaceFlingerConsumer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include "Transform.h"
48
Mathias Agopian13127d82013-03-05 17:47:11 -080049#include "DisplayHardware/HWComposer.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070050#include "RenderEngine/Mesh.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070051#include "RenderEngine/Texture.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080052
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053namespace android {
54
55// ---------------------------------------------------------------------------
56
Mathias Agopian1f7bec62010-06-25 18:02:21 -070057class Client;
Mathias Agopian3e25fd82013-04-22 17:52:16 +020058class Colorizer;
Mathias Agopian13127d82013-03-05 17:47:11 -080059class DisplayDevice;
60class GraphicBuffer;
61class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
63// ---------------------------------------------------------------------------
64
Andy McFadden882e3a32013-01-08 16:06:15 -080065/*
Andy McFadden882e3a32013-01-08 16:06:15 -080066 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
67 * Layer is first referenced.
68 *
69 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
70 * that new data has arrived.
71 */
Jesse Hall399184a2014-03-03 15:42:54 -080072class Layer : public SurfaceFlingerConsumer::ContentsChangedListener {
Mathias Agopian13127d82013-03-05 17:47:11 -080073 static int32_t sSequence;
74
Mathias Agopiand606de62010-05-10 20:06:11 -070075public:
Mathias Agopian13127d82013-03-05 17:47:11 -080076 mutable bool contentDirty;
77 // regions below are in window-manager space
78 Region visibleRegion;
79 Region coveredRegion;
80 Region visibleNonTransparentRegion;
Dan Stozaee44edd2015-03-23 15:50:23 -070081 Region surfaceDamageRegion;
Andy McFadden4df87bd2014-04-21 18:08:54 -070082
83 // Layer serial number. This gives layers an explicit ordering, so we
84 // have a stable sort order when their layer stack and Z-order are
85 // the same.
Mathias Agopian13127d82013-03-05 17:47:11 -080086 int32_t sequence;
87
88 enum { // flags for doTransaction()
89 eDontUpdateGeometryState = 0x00000001,
90 eVisibleRegion = 0x00000002,
91 };
92
93 struct Geometry {
94 uint32_t w;
95 uint32_t h;
Robert Carr3dcabfa2016-03-01 18:36:58 -080096 Transform transform;
97
Mathias Agopian13127d82013-03-05 17:47:11 -080098 inline bool operator ==(const Geometry& rhs) const {
Robert Carr99e27f02016-06-16 15:18:02 -070099 return (w == rhs.w && h == rhs.h) &&
100 (transform.tx() == rhs.transform.tx()) &&
101 (transform.ty() == rhs.transform.ty());
Mathias Agopian13127d82013-03-05 17:47:11 -0800102 }
103 inline bool operator !=(const Geometry& rhs) const {
104 return !operator ==(rhs);
105 }
106 };
107
108 struct State {
109 Geometry active;
110 Geometry requested;
Robert Carrae060832016-11-28 10:51:00 -0800111 int32_t z;
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800112
113 // The identifier of the layer stack this layer belongs to. A layer can
114 // only be associated to a single layer stack. A layer stack is a
115 // z-ordered group of layers which can be associated to one or more
116 // displays. Using the same layer stack on different displays is a way
117 // to achieve mirroring.
Mathias Agopian13127d82013-03-05 17:47:11 -0800118 uint32_t layerStack;
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800119
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000120#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800121 float alpha;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000122#else
123 uint8_t alpha;
124#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800125 uint8_t flags;
Dan Stoza7dde5992015-05-22 09:51:44 -0700126 uint8_t mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800127 uint8_t reserved[2];
128 int32_t sequence; // changes when visible regions can change
Dan Stoza7dde5992015-05-22 09:51:44 -0700129 bool modified;
130
Fabien Sanglard4ed383c2016-12-13 14:02:41 -0800131 // Crop is expressed in layer space coordinate.
Robert Carrb5d3d262016-03-25 15:08:13 -0700132 Rect crop;
Robert Carr99e27f02016-06-16 15:18:02 -0700133 Rect requestedCrop;
134
Fabien Sanglard4ed383c2016-12-13 14:02:41 -0800135 // finalCrop is expressed in display space coordinate.
Robert Carrb5d3d262016-03-25 15:08:13 -0700136 Rect finalCrop;
137
Robert Carr0d480722017-01-10 16:42:54 -0800138 // If set, defers this state update until the identified Layer
Dan Stoza7dde5992015-05-22 09:51:44 -0700139 // receives a frame with the given frameNumber
Robert Carr0d480722017-01-10 16:42:54 -0800140 wp<Layer> barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -0700141 uint64_t frameNumber;
142
Mathias Agopian2ca79392013-04-02 18:30:32 -0700143 // the transparentRegion hint is a bit special, it's latched only
144 // when we receive a buffer -- this is because it's "content"
145 // dependent.
146 Region activeTransparentRegion;
147 Region requestedTransparentRegion;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700148 android_dataspace dataSpace;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500149
150 uint32_t appId;
151 uint32_t type;
Mathias Agopian13127d82013-03-05 17:47:11 -0800152 };
153
Mathias Agopian13127d82013-03-05 17:47:11 -0800154 // -----------------------------------------------------------------------
155
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700156 Layer(SurfaceFlinger* flinger, const sp<Client>& client,
157 const String8& name, uint32_t w, uint32_t h, uint32_t flags);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700158
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700159 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700161 // the this layer's size and format
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700162 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163
Mathias Agopian13127d82013-03-05 17:47:11 -0800164 // modify current state
Robert Carr82364e32016-05-15 11:27:47 -0700165 bool setPosition(float x, float y, bool immediate);
Robert Carrae060832016-11-28 10:51:00 -0800166 bool setLayer(int32_t z);
Mathias Agopian13127d82013-03-05 17:47:11 -0800167 bool setSize(uint32_t w, uint32_t h);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000168#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800169 bool setAlpha(float alpha);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000170#else
171 bool setAlpha(uint8_t alpha);
172#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800173 bool setMatrix(const layer_state_t::matrix22_t& matrix);
174 bool setTransparentRegionHint(const Region& transparent);
175 bool setFlags(uint8_t flags, uint8_t mask);
Robert Carr99e27f02016-06-16 15:18:02 -0700176 bool setCrop(const Rect& crop, bool immediate);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000177 bool setFinalCrop(const Rect& crop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800178 bool setLayerStack(uint32_t layerStack);
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700179 bool setDataSpace(android_dataspace dataSpace);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700180 uint32_t getLayerStack() const;
Robert Carr0d480722017-01-10 16:42:54 -0800181 void deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber);
182 void deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber);
Robert Carrc3574f72016-03-24 12:19:32 -0700183 bool setOverrideScalingMode(int32_t overrideScalingMode);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500184 void setInfo(uint32_t type, uint32_t appId);
Robert Carr1db73f62016-12-21 12:58:51 -0800185 bool reparentChildren(const sp<IBinder>& layer);
Robert Carr9524cb32017-02-13 11:32:32 -0800186 bool detachChildren();
Mathias Agopian13127d82013-03-05 17:47:11 -0800187
Dan Stozaee44edd2015-03-23 15:50:23 -0700188 // If we have received a new buffer this frame, we will pass its surface
189 // damage down to hardware composer. Otherwise, we must send a region with
190 // one empty rect.
191 void useSurfaceDamage();
192 void useEmptyDamage();
193
Mathias Agopian13127d82013-03-05 17:47:11 -0800194 uint32_t getTransactionFlags(uint32_t flags);
195 uint32_t setTransactionFlags(uint32_t flags);
196
Dan Stozac7014012014-02-14 15:03:43 -0800197 void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
198 bool useIdentityTransform) const;
Michael Lentine6c925ed2014-09-26 17:55:01 -0700199 Rect computeBounds(const Region& activeTransparentRegion) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800200 Rect computeBounds() const;
201
Pablo Ceballos40845df2016-01-25 17:41:15 -0800202 int32_t getSequence() const { return sequence; }
203
Mathias Agopian13127d82013-03-05 17:47:11 -0800204 // -----------------------------------------------------------------------
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700205 // Virtuals
Mathias Agopian13127d82013-03-05 17:47:11 -0800206
Mathias Agopian13127d82013-03-05 17:47:11 -0800207 virtual const char* getTypeId() const { return "Layer"; }
208
Mathias Agopian13127d82013-03-05 17:47:11 -0800209 /*
210 * isOpaque - true if this surface is opaque
Andy McFadden4125a4f2014-01-29 17:17:11 -0800211 *
212 * This takes into account the buffer format (i.e. whether or not the
213 * pixel format includes an alpha channel) and the "opaque" flag set
214 * on the layer. It does not examine the current plane alpha value.
Mathias Agopian13127d82013-03-05 17:47:11 -0800215 */
Andy McFadden4125a4f2014-01-29 17:17:11 -0800216 virtual bool isOpaque(const Layer::State& s) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800217
218 /*
219 * isSecure - true if this surface is secure, that is if it prevents
220 * screenshots or VNC servers.
221 */
Dan Stoza23116082015-06-18 14:58:39 -0700222 virtual bool isSecure() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800223
224 /*
225 * isProtected - true if the layer may contain protected content in the
226 * GRALLOC_USAGE_PROTECTED sense.
227 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800228 virtual bool isProtected() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800229
230 /*
231 * isVisible - true if this layer is visible, false otherwise
232 */
Mathias Agopianda27af92012-09-13 18:17:13 -0700233 virtual bool isVisible() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
Mathias Agopian13127d82013-03-05 17:47:11 -0800235 /*
Robert Carr1f0a16a2016-10-24 16:27:39 -0700236 * isHiddenByPolicy - true if this layer has been forced invisible.
237 * just because this is false, doesn't mean isVisible() is true.
238 * For example if this layer has no active buffer, it may not be hidden by
239 * policy, but it still can not be visible.
240 */
241 virtual bool isHiddenByPolicy() const;
242
243 /*
Mathias Agopian13127d82013-03-05 17:47:11 -0800244 * isFixedSize - true if content has a fixed size
245 */
246 virtual bool isFixedSize() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700247
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700248protected:
249 /*
250 * onDraw - draws the surface.
251 */
Dan Stozac7014012014-02-14 15:03:43 -0800252 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
253 bool useIdentityTransform) const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700254
255public:
256 // -----------------------------------------------------------------------
257
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000258#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800259 void setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800260 void forceClientComposition(int32_t hwcId);
261 void setPerFrameData(const sp<const DisplayDevice>& displayDevice);
262
263 // callIntoHwc exists so we can update our local state and call
264 // acceptDisplayChanges without unnecessarily updating the device's state
265 void setCompositionType(int32_t hwcId, HWC2::Composition type,
266 bool callIntoHwc = true);
267 HWC2::Composition getCompositionType(int32_t hwcId) const;
268
269 void setClearClientTarget(int32_t hwcId, bool clear);
270 bool getClearClientTarget(int32_t hwcId) const;
271
272 void updateCursorPosition(const sp<const DisplayDevice>& hw);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000273#else
274 void setGeometry(const sp<const DisplayDevice>& hw,
275 HWComposer::HWCLayerInterface& layer);
276 void setPerFrameData(const sp<const DisplayDevice>& hw,
277 HWComposer::HWCLayerInterface& layer);
278 void setAcquireFence(const sp<const DisplayDevice>& hw,
279 HWComposer::HWCLayerInterface& layer);
280
281 Rect getPosition(const sp<const DisplayDevice>& hw);
282#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700283
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700284 /*
285 * called after page-flip
286 */
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000287#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800288 void onLayerDisplayed(const sp<Fence>& releaseFence);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000289#else
290 void onLayerDisplayed(const sp<const DisplayDevice>& hw,
291 HWComposer::HWCLayerInterface* layer);
292#endif
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700293
Dan Stoza6b9454d2014-11-07 16:00:59 -0800294 bool shouldPresentNow(const DispSync& dispSync) const;
295
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700296 /*
297 * called before composition.
298 * returns true if the layer has pending updates.
299 */
Brian Andersond6927fb2016-07-23 23:37:30 -0700300 bool onPreComposition(nsecs_t refreshStartTime);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700301
302 /*
Dan Stozae77c7662016-05-13 11:37:28 -0700303 * called after composition.
304 * returns true if the layer latched a new buffer this frame.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700305 */
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800306 bool onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700307 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800308 const std::shared_ptr<FenceTime>& retireFence,
309 const CompositorTiming& compositorTiming);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700310
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000311#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800312 // If a buffer was replaced this frame, release the former buffer
Brian Andersonf6386862016-10-31 16:34:13 -0700313 void releasePendingBuffer(nsecs_t dequeueReadyTime);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000314#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800315
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700316 /*
317 * draw - performs some global clipping optimizations
318 * and calls onDraw().
319 */
320 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
Dan Stozac7014012014-02-14 15:03:43 -0800321 void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const;
322 void draw(const sp<const DisplayDevice>& hw) const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700323
324 /*
325 * doTransaction - process the transaction. This is a good place to figure
326 * out which attributes of the surface have changed.
327 */
328 uint32_t doTransaction(uint32_t transactionFlags);
329
330 /*
331 * setVisibleRegion - called to set the new visible region. This gives
332 * a chance to update the new visible region or record the fact it changed.
333 */
334 void setVisibleRegion(const Region& visibleRegion);
335
336 /*
337 * setCoveredRegion - called when the covered region changes. The covered
338 * region corresponds to any area of the surface that is covered
339 * (transparently or not) by another surface.
340 */
341 void setCoveredRegion(const Region& coveredRegion);
342
343 /*
344 * setVisibleNonTransparentRegion - called when the visible and
345 * non-transparent region changes.
346 */
347 void setVisibleNonTransparentRegion(const Region&
348 visibleNonTransparentRegion);
349
350 /*
351 * latchBuffer - called each time the screen is redrawn and returns whether
352 * the visible regions need to be recomputed (this is a fairly heavy
353 * operation, so this should be set only if needed). Typically this is used
354 * to figure out if the content or size of a surface has changed.
355 */
Brian Andersond6927fb2016-07-23 23:37:30 -0700356 Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700357
Riley Andrews03414a12014-07-01 14:22:59 -0700358 bool isPotentialCursor() const { return mPotentialCursor;}
359
Mathias Agopian13127d82013-03-05 17:47:11 -0800360 /*
361 * called with the state lock when the surface is removed from the
362 * current list
363 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700364 void onRemoved();
Mathias Agopian13127d82013-03-05 17:47:11 -0800365
366
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800367 // Updates the transform hint in our SurfaceFlingerConsumer to match
Mathias Agopian84300952012-11-21 16:02:13 -0800368 // the current orientation of the display device.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700369 void updateTransformHint(const sp<const DisplayDevice>& hw) const;
Andy McFadden69052052012-09-14 16:10:11 -0700370
Mathias Agopian13127d82013-03-05 17:47:11 -0800371 /*
372 * returns the rectangle that crops the content of the layer and scales it
373 * to the layer's size.
374 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700375 Rect getContentCrop() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800376
Eric Penner51c59cd2014-07-28 19:51:58 -0700377 /*
378 * Returns if a frame is queued.
379 */
Pablo Ceballos06312182015-10-07 16:32:12 -0700380 bool hasQueuedFrame() const { return mQueuedFrames > 0 ||
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800381 mSidebandStreamChanged || mAutoRefresh; }
Eric Penner51c59cd2014-07-28 19:51:58 -0700382
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000383#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800384 // -----------------------------------------------------------------------
385
Chia-I Wu06d63de2017-01-04 14:58:51 +0800386 void eraseHwcLayer(int32_t hwcId) {
387 mHwcLayers.erase(hwcId);
388
389 Mutex::Autolock lock(mHwcBufferCacheMutex);
390 mHwcBufferCaches.erase(hwcId);
391 }
392
Dan Stoza9e56aa02015-11-02 13:00:03 -0800393 bool hasHwcLayer(int32_t hwcId) {
394 if (mHwcLayers.count(hwcId) == 0) {
395 return false;
396 }
397 if (mHwcLayers[hwcId].layer->isAbandoned()) {
398 ALOGI("Erasing abandoned layer %s on %d", mName.string(), hwcId);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800399 eraseHwcLayer(hwcId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800400 return false;
401 }
402 return true;
403 }
404
405 std::shared_ptr<HWC2::Layer> getHwcLayer(int32_t hwcId) {
406 if (mHwcLayers.count(hwcId) == 0) {
407 return nullptr;
408 }
409 return mHwcLayers[hwcId].layer;
410 }
411
412 void setHwcLayer(int32_t hwcId, std::shared_ptr<HWC2::Layer>&& layer) {
413 if (layer) {
414 mHwcLayers[hwcId].layer = layer;
Chia-I Wu06d63de2017-01-04 14:58:51 +0800415
416 Mutex::Autolock lock(mHwcBufferCacheMutex);
417 mHwcBufferCaches[hwcId] = HWComposerBufferCache();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800418 } else {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800419 eraseHwcLayer(hwcId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800420 }
421 }
422
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800423 void clearHwcLayers() {
424 mHwcLayers.clear();
425 }
426
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000427#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800428 // -----------------------------------------------------------------------
429
Fabien Sanglard17487192016-12-07 13:03:32 -0800430 void clearWithOpenGL(const sp<const DisplayDevice>& hw) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800431 void setFiltering(bool filtering);
432 bool getFiltering() const;
433
434 // only for debugging
435 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
436
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700437 inline const State& getDrawingState() const { return mDrawingState; }
438 inline const State& getCurrentState() const { return mCurrentState; }
439 inline State& getCurrentState() { return mCurrentState; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800440
441
442 /* always call base class first */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700443 void dump(String8& result, Colorizer& colorizer) const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000444#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -0700445 static void miniDumpHeader(String8& result);
446 void miniDump(String8& result, int32_t hwcId) const;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000447#endif
Svetoslavd85084b2014-03-20 10:28:31 -0700448 void dumpFrameStats(String8& result) const;
Brian Andersond6927fb2016-07-23 23:37:30 -0700449 void dumpFrameEvents(String8& result);
Svetoslavd85084b2014-03-20 10:28:31 -0700450 void clearFrameStats();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700451 void logFrameStats();
Svetoslavd85084b2014-03-20 10:28:31 -0700452 void getFrameStats(FrameStats* outStats) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700453
Dan Stozae77c7662016-05-13 11:37:28 -0700454 std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush);
455
Brian Anderson5ea5e592016-12-01 16:54:33 -0800456 void onDisconnect();
Brian Anderson3890c392016-07-25 12:48:08 -0700457 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newEntry,
458 FrameEventHistoryDelta* outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800459
Robert Carr367c5682016-06-20 11:55:28 -0700460 bool getTransformToDisplayInverse() const;
461
Robert Carr1f0a16a2016-10-24 16:27:39 -0700462 Transform getTransform() const;
463
464 void traverseInReverseZOrder(const std::function<void(Layer*)>& exec);
465 void traverseInZOrder(const std::function<void(Layer*)>& exec);
466
467 void addChild(const sp<Layer>& layer);
468 // Returns index if removed, or negative value otherwise
469 // for symmetry with Vector::remove
470 ssize_t removeChild(const sp<Layer>& layer);
471 sp<Layer> getParent() const { return mParent.promote(); }
472 bool hasParent() const { return getParent() != nullptr; }
473
474 Rect computeScreenBounds(bool reduceTransparentRegion = true) const;
475 bool setChildLayer(const sp<Layer>& childLayer, int32_t z);
476
477 // Copy the current list of children to the drawing state. Called by
478 // SurfaceFlinger to complete a transaction.
479 void commitChildList();
480
481 int32_t getZ() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800482protected:
483 // constant
484 sp<SurfaceFlinger> mFlinger;
Mathias Agopian13127d82013-03-05 17:47:11 -0800485 /*
486 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
487 * is called.
488 */
489 class LayerCleaner {
490 sp<SurfaceFlinger> mFlinger;
491 wp<Layer> mLayer;
492 protected:
Irvel468051e2016-06-13 16:44:44 -0700493 ~LayerCleaner() {
494 // destroy client resources
495 mFlinger->onLayerDestroyed(mLayer);
496 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800497 public:
Irvel468051e2016-06-13 16:44:44 -0700498 LayerCleaner(const sp<SurfaceFlinger>& flinger,
499 const sp<Layer>& layer)
500 : mFlinger(flinger), mLayer(layer) {
501 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800502 };
503
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800504
Irvel468051e2016-06-13 16:44:44 -0700505 virtual void onFirstRef();
506
507
508
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509private:
Irvel468051e2016-06-13 16:44:44 -0700510 friend class SurfaceInterceptor;
Jesse Hall399184a2014-03-03 15:42:54 -0800511 // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
Dan Stozadc13c5b2015-05-11 15:33:01 -0700512 virtual void onFrameAvailable(const BufferItem& item) override;
513 virtual void onFrameReplaced(const BufferItem& item) override;
Chia-I Wu06d63de2017-01-04 14:58:51 +0800514 virtual void onBuffersReleased() override;
Dan Stozadc13c5b2015-05-11 15:33:01 -0700515 virtual void onSidebandStreamChanged() override;
Mathias Agopian13127d82013-03-05 17:47:11 -0800516
Pablo Ceballos05289c22016-04-14 15:49:55 -0700517 void commitTransaction(const State& stateToCommit);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700518
519 // needsLinearFiltering - true if this surface's state requires filtering
520 bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800521
Mathias Agopian3330b202009-10-05 17:07:12 -0700522 uint32_t getEffectiveUsage(uint32_t usage) const;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700523
Dan Stoza5a423ea2017-02-16 14:10:39 -0800524 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700525 // Compute the initial crop as specified by parent layers and the SurfaceControl
526 // for this layer. Does not include buffer crop from the IGraphicBufferProducer
527 // client, as that should not affect child clipping. Returns in screen space.
528 Rect computeInitialCrop(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700529 bool isCropped() const;
530 static bool getOpacityForFormat(uint32_t format);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700531
Mathias Agopian13127d82013-03-05 17:47:11 -0800532 // drawing
Fabien Sanglard17487192016-12-07 13:03:32 -0800533 void clearWithOpenGL(const sp<const DisplayDevice>& hw,
Mathias Agopian3f844832013-08-07 21:24:32 -0700534 float r, float g, float b, float alpha) const;
Fabien Sanglard85789802016-12-07 13:08:24 -0800535 void drawWithOpenGL(const sp<const DisplayDevice>& hw,
Dan Stozac7014012014-02-14 15:03:43 -0800536 bool useIdentityTransform) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800537
Ruben Brunk1681d952014-06-27 15:51:55 -0700538 // Temporary - Used only for LEGACY camera mode.
539 uint32_t getProducerStickyTransform() const;
540
Dan Stozac5da2712016-07-20 15:38:12 -0700541 // Loads the corresponding system property once per process
542 static bool latchUnsignaledBuffers();
543
Robert Carr1f0a16a2016-10-24 16:27:39 -0700544 void setParent(const sp<Layer>& layer);
545
Dan Stoza7dde5992015-05-22 09:51:44 -0700546 // -----------------------------------------------------------------------
547
548 class SyncPoint
549 {
550 public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -0700551 explicit SyncPoint(uint64_t frameNumber) : mFrameNumber(frameNumber),
Dan Stoza7dde5992015-05-22 09:51:44 -0700552 mFrameIsAvailable(false), mTransactionIsApplied(false) {}
553
554 uint64_t getFrameNumber() const {
555 return mFrameNumber;
556 }
557
558 bool frameIsAvailable() const {
559 return mFrameIsAvailable;
560 }
561
562 void setFrameAvailable() {
563 mFrameIsAvailable = true;
564 }
565
566 bool transactionIsApplied() const {
567 return mTransactionIsApplied;
568 }
569
570 void setTransactionApplied() {
571 mTransactionIsApplied = true;
572 }
573
574 private:
575 const uint64_t mFrameNumber;
576 std::atomic<bool> mFrameIsAvailable;
577 std::atomic<bool> mTransactionIsApplied;
578 };
579
Dan Stozacac35382016-01-27 12:21:06 -0800580 // SyncPoints which will be signaled when the correct frame is at the head
581 // of the queue and dropped after the frame has been latched. Protected by
582 // mLocalSyncPointMutex.
583 Mutex mLocalSyncPointMutex;
Dan Stoza7dde5992015-05-22 09:51:44 -0700584 std::list<std::shared_ptr<SyncPoint>> mLocalSyncPoints;
585
Dan Stozacac35382016-01-27 12:21:06 -0800586 // SyncPoints which will be signaled and then dropped when the transaction
587 // is applied
Dan Stoza7dde5992015-05-22 09:51:44 -0700588 std::list<std::shared_ptr<SyncPoint>> mRemoteSyncPoints;
589
Dan Stozacac35382016-01-27 12:21:06 -0800590 uint64_t getHeadFrameNumber() const;
Dan Stoza1ce65812016-06-15 16:26:27 -0700591 bool headFenceHasSignaled() const;
Dan Stozacac35382016-01-27 12:21:06 -0800592
593 // Returns false if the relevant frame has already been latched
594 bool addSyncPoint(const std::shared_ptr<SyncPoint>& point);
Dan Stoza7dde5992015-05-22 09:51:44 -0700595
596 void pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700597 void popPendingState(State* stateToCommit);
598 bool applyPendingStates(State* stateToCommit);
Robert Carrc3574f72016-03-24 12:19:32 -0700599
Robert Carr1f0a16a2016-10-24 16:27:39 -0700600 void clearSyncPoints();
601
Robert Carrc3574f72016-03-24 12:19:32 -0700602 // Returns mCurrentScaling mode (originating from the
603 // Client) or mOverrideScalingMode mode (originating from
604 // the Surface Controller) if set.
605 uint32_t getEffectiveScalingMode() const;
Dan Stoza7dde5992015-05-22 09:51:44 -0700606public:
Irvel468051e2016-06-13 16:44:44 -0700607 /*
608 * The layer handle is just a BBinder object passed to the client
609 * (remote process) -- we don't keep any reference on our side such that
610 * the dtor is called when the remote side let go of its reference.
611 *
612 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
613 * this layer when the handle is destroyed.
614 */
615 class Handle : public BBinder, public LayerCleaner {
616 public:
617 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
618 : LayerCleaner(flinger, layer), owner(layer) {}
619
620 wp<Layer> owner;
621 };
622
623 sp<IBinder> getHandle();
624 sp<IGraphicBufferProducer> getProducer() const;
625 const String8& getName() const;
Dan Stoza7dde5992015-05-22 09:51:44 -0700626 void notifyAvailableFrames();
627private:
Igor Murashkina4a31492012-10-29 13:36:11 -0700628
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700629 // -----------------------------------------------------------------------
630
Fabien Sanglardcd6fd542016-10-13 12:47:39 -0700631 // Check all of the local sync points to ensure that all transactions
632 // which need to have been applied prior to the frame which is about to
633 // be latched have signaled
634 bool allTransactionsSignaled();
635
Mathias Agopiana67932f2011-04-20 14:20:59 -0700636 // constants
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800637 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
Dan Stozab9b08832014-03-13 11:55:57 -0700638 sp<IGraphicBufferProducer> mProducer;
Andy McFadden4df87bd2014-04-21 18:08:54 -0700639 uint32_t mTextureName; // from GLES
Mathias Agopian13127d82013-03-05 17:47:11 -0800640 bool mPremultipliedAlpha;
641 String8 mName;
Mathias Agopian13127d82013-03-05 17:47:11 -0800642 PixelFormat mFormat;
Mathias Agopian13127d82013-03-05 17:47:11 -0800643
644 // these are protected by an external lock
645 State mCurrentState;
646 State mDrawingState;
647 volatile int32_t mTransactionFlags;
Mathias Agopiand606de62010-05-10 20:06:11 -0700648
Dan Stoza7dde5992015-05-22 09:51:44 -0700649 // Accessed from main thread and binder threads
650 Mutex mPendingStateMutex;
651 Vector<State> mPendingStates;
652
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700653 // thread-safe
Mathias Agopiana67932f2011-04-20 14:20:59 -0700654 volatile int32_t mQueuedFrames;
Jesse Hall399184a2014-03-03 15:42:54 -0800655 volatile int32_t mSidebandStreamChanged; // used like an atomic boolean
Brian Andersond6927fb2016-07-23 23:37:30 -0700656
657 // Timestamp history for UIAutomation. Thread safe.
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800658 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700659
Brian Andersond6927fb2016-07-23 23:37:30 -0700660 // Timestamp history for the consumer to query.
661 // Accessed by both consumer and producer on main and binder threads.
662 Mutex mFrameEventHistoryMutex;
Brian Anderson3890c392016-07-25 12:48:08 -0700663 ConsumerFrameEventHistory mFrameEventHistory;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700664 FenceTimeline mAcquireTimeline;
665 FenceTimeline mReleaseTimeline;
Brian Andersond6927fb2016-07-23 23:37:30 -0700666
Mathias Agopiana67932f2011-04-20 14:20:59 -0700667 // main thread
Mathias Agopiana9347642017-02-13 16:42:28 -0800668 int mActiveBufferSlot;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700669 sp<GraphicBuffer> mActiveBuffer;
Jesse Hall399184a2014-03-03 15:42:54 -0800670 sp<NativeHandle> mSidebandStream;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700671 Rect mCurrentCrop;
672 uint32_t mCurrentTransform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700673 uint32_t mCurrentScalingMode;
Robert Carrc3574f72016-03-24 12:19:32 -0700674 // We encode unset as -1.
675 int32_t mOverrideScalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700676 bool mCurrentOpacity;
Brian Andersond6927fb2016-07-23 23:37:30 -0700677 bool mBufferLatched = false; // TODO: Use mActiveBuffer?
Dan Stozacac35382016-01-27 12:21:06 -0800678 std::atomic<uint64_t> mCurrentFrameNumber;
Brian Andersond6927fb2016-07-23 23:37:30 -0700679 uint64_t mPreviousFrameNumber; // Only accessed on the main thread.
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800680 bool mRefreshPending;
Jamie Gennise8696a42012-01-15 18:54:57 -0800681 bool mFrameLatencyNeeded;
Mathias Agopian13127d82013-03-05 17:47:11 -0800682 // Whether filtering is forced on or not
683 bool mFiltering;
684 // Whether filtering is needed b/c of the drawingstate
685 bool mNeedsFiltering;
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700686 // The mesh used to draw the layer in GLES composition mode
687 mutable Mesh mMesh;
Andy McFadden4df87bd2014-04-21 18:08:54 -0700688 // The texture used to draw the layer in GLES composition mode
Mathias Agopian49457ac2013-08-14 18:20:17 -0700689 mutable Texture mTexture;
Mathias Agopiand606de62010-05-10 20:06:11 -0700690
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000691#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800692 // HWC items, accessed from the main thread
693 struct HWCInfo {
694 HWCInfo()
695 : layer(),
696 forceClientComposition(false),
697 compositionType(HWC2::Composition::Invalid),
698 clearClientTarget(false) {}
699
700 std::shared_ptr<HWC2::Layer> layer;
701 bool forceClientComposition;
702 HWC2::Composition compositionType;
703 bool clearClientTarget;
Dan Stozae22aec72016-08-01 13:20:59 -0700704 Rect displayFrame;
Dan Stoza5a423ea2017-02-16 14:10:39 -0800705 FloatRect sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706 };
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800707
708 // A layer can be attached to multiple displays when operating in mirror mode
709 // (a.k.a: when several displays are attached with equal layerStack). In this
Fabien Sanglard4afbf412017-03-08 14:50:11 -0800710 // case we need to keep track. In non-mirror mode, a layer will have only one
Fabien Sanglardf0c53d62017-03-03 18:58:50 -0800711 // HWCInfo. This map key is a display layerStack.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 std::unordered_map<int32_t, HWCInfo> mHwcLayers;
Chia-I Wu06d63de2017-01-04 14:58:51 +0800713
714 // We need one HWComposerBufferCache for each HWC display. We cannot have
715 // HWComposerBufferCache in HWCInfo because HWCInfo can only be accessed
716 // from the main thread.
717 Mutex mHwcBufferCacheMutex;
718 std::unordered_map<int32_t, HWComposerBufferCache> mHwcBufferCaches;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000719#else
720 bool mIsGlesComposition;
721#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700723 // page-flip thread (currently main thread)
Glenn Kasten16f04532011-01-19 15:27:27 -0800724 bool mProtectedByApp; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -0800725
726 // protected by mLock
727 mutable Mutex mLock;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700728 // Set to true once we've returned this surface's handle
Mathias Agopian13127d82013-03-05 17:47:11 -0800729 mutable bool mHasSurface;
730 const wp<Client> mClientRef;
Riley Andrews03414a12014-07-01 14:22:59 -0700731
732 // This layer can be a cursor on some displays.
733 bool mPotentialCursor;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800734
735 // Local copy of the queued contents of the incoming BufferQueue
736 mutable Mutex mQueueItemLock;
Dan Stozaa4650a52015-05-12 12:56:16 -0700737 Condition mQueueItemCondition;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800738 Vector<BufferItem> mQueueItems;
Dan Stozacac35382016-01-27 12:21:06 -0800739 std::atomic<uint64_t> mLastFrameNumberReceived;
Brian Andersond6927fb2016-07-23 23:37:30 -0700740 bool mUpdateTexImageFailed; // This is only accessed on the main thread.
Pablo Ceballos06312182015-10-07 16:32:12 -0700741
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800742 bool mAutoRefresh;
Robert Carr82364e32016-05-15 11:27:47 -0700743 bool mFreezePositionUpdates;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700744
745 // Child list about to be committed/used for editing.
746 LayerVector mCurrentChildren;
747 // Child list used for rendering.
748 LayerVector mDrawingChildren;
749
750 wp<Layer> mParent;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751};
752
753// ---------------------------------------------------------------------------
754
755}; // namespace android
756
757#endif // ANDROID_LAYER_H