blob: f6eed6332bd23f92ac7e26a9763d351900f896c5 [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
Dominik Laskowskib7251f42020-04-20 17:42:59 -070017#pragma once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -070019#include <android/gui/DropInputMode.h>
Huihong Luod3d8f8e2022-03-08 14:48:46 -080020#include <android/gui/ISurfaceComposerClient.h>
Melody Hsue4ef87f2024-03-26 23:54:45 +000021#include <ftl/small_map.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070022#include <gui/BufferQueue.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070023#include <gui/LayerState.h>
chaviw98318de2021-05-19 16:45:23 -050024#include <gui/WindowInfo.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070025#include <layerproto/LayerProtoHeader.h>
26#include <math/vec4.h>
Alec Mouri1c8d7202019-06-01 18:51:35 -070027#include <sys/types.h>
Lucas Dupinc3800b82020-10-02 16:24:48 -070028#include <ui/BlurRegion.h>
Melody Hsue4ef87f2024-03-26 23:54:45 +000029#include <ui/DisplayMap.h>
Dan Stoza80d61162017-12-20 15:57:52 -080030#include <ui/FloatRect.h>
Svetoslavd85084b2014-03-20 10:28:31 -070031#include <ui/FrameStats.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080032#include <ui/GraphicBuffer.h>
Melody Hsue4ef87f2024-03-26 23:54:45 +000033#include <ui/LayerStack.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080034#include <ui/PixelFormat.h>
35#include <ui/Region.h>
John Reckcdb4ed72021-02-04 13:39:33 -050036#include <ui/StretchEffect.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070037#include <ui/Transform.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070038#include <utils/RefBase.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070039#include <utils/Timers.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080040
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080041#include <compositionengine/LayerFE.h>
Vishnu Nairbedb44b2022-08-02 21:47:40 +000042#include <compositionengine/LayerFECompositionState.h>
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080043#include <scheduler/Fps.h>
44#include <scheduler/Seamlessness.h>
45
KaiChieh Chuang948c6422020-10-13 10:38:48 +080046#include <chrono>
Robert Carrcdf83202018-03-07 12:48:34 -080047#include <cstdint>
Peiyong Lincbc184f2018-08-22 13:24:10 -070048#include <list>
Dominik Laskowski075d3172018-05-24 15:50:06 -070049#include <optional>
Peiyong Lincbc184f2018-08-22 13:24:10 -070050#include <vector>
Dan Stoza7dde5992015-05-22 09:51:44 -070051
Mathias Agopian13127d82013-03-05 17:47:11 -080052#include "Client.h"
Alec Mouri1c8d7202019-06-01 18:51:35 -070053#include "DisplayHardware/HWComposer.h"
David Sodman41fdfc92017-11-06 16:09:56 -080054#include "FrameTracker.h"
Vishnu Naire14c6b32022-08-06 04:20:15 +000055#include "LayerFE.h"
Robert Carr1f0a16a2016-10-24 16:27:39 -070056#include "LayerVector.h"
Ady Abrahambdda8f02021-04-01 16:06:11 -070057#include "Scheduler/LayerInfo.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080058#include "SurfaceFlinger.h"
Vishnu Nair00b90132021-11-05 14:03:40 -070059#include "Tracing/LayerTracing.h"
Robert Carr9a803c32021-01-14 16:57:58 -080060#include "TransactionCallbackInvoker.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
chaviw1d044282017-09-27 12:19:28 -070062using namespace android::surfaceflinger;
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064namespace android {
65
Mathias Agopian1f7bec62010-06-25 18:02:21 -070066class Client;
Mathias Agopian3e25fd82013-04-22 17:52:16 +020067class Colorizer;
Mathias Agopian13127d82013-03-05 17:47:11 -080068class DisplayDevice;
69class GraphicBuffer;
70class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Lloyd Piquefeb73d72018-12-04 17:23:44 -080072namespace compositionengine {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080073class OutputLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -080074struct LayerFECompositionState;
Lloyd Piquefeb73d72018-12-04 17:23:44 -080075}
76
Ady Abraham22c7b5c2020-09-22 19:33:40 -070077namespace frametimeline {
78class SurfaceFrame;
79} // namespace frametimeline
80
Vishnu Naire14c6b32022-08-06 04:20:15 +000081class Layer : public virtual RefBase {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000082public:
Ady Abrahamaae5ed52020-06-26 09:32:43 -070083 // The following constants represent priority of the window. SF uses this information when
84 // deciding which window has a priority when deciding about the refresh rate of the screen.
85 // Priority 0 is considered the highest priority. -1 means that the priority is unset.
Ana Krulecc84d09b2019-11-02 23:10:29 +010086 static constexpr int32_t PRIORITY_UNSET = -1;
Ady Abrahamaae5ed52020-06-26 09:32:43 -070087 // Windows that are in focus and voted for the preferred mode ID
88 static constexpr int32_t PRIORITY_FOCUSED_WITH_MODE = 0;
89 // // Windows that are in focus, but have not requested a specific mode ID.
90 static constexpr int32_t PRIORITY_FOCUSED_WITHOUT_MODE = 1;
91 // Windows that are not in focus, but voted for a specific mode ID.
92 static constexpr int32_t PRIORITY_NOT_FOCUSED_WITH_MODE = 2;
Mathias Agopian13127d82013-03-05 17:47:11 -080093
Mathias Agopian13127d82013-03-05 17:47:11 -080094 enum { // flags for doTransaction()
95 eDontUpdateGeometryState = 0x00000001,
96 eVisibleRegion = 0x00000002,
Robert Carr720e5062018-07-30 17:45:14 -070097 eInputInfoChanged = 0x00000004
Mathias Agopian13127d82013-03-05 17:47:11 -080098 };
99
100 struct Geometry {
101 uint32_t w;
102 uint32_t h;
Peiyong Linefefaac2018-08-17 12:27:51 -0700103 ui::Transform transform;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800104
David Sodman41fdfc92017-11-06 16:09:56 -0800105 inline bool operator==(const Geometry& rhs) const {
106 return (w == rhs.w && h == rhs.h) && (transform.tx() == rhs.transform.tx()) &&
Robert Carr99e27f02016-06-16 15:18:02 -0700107 (transform.ty() == rhs.transform.ty());
Mathias Agopian13127d82013-03-05 17:47:11 -0800108 }
David Sodman41fdfc92017-11-06 16:09:56 -0800109 inline bool operator!=(const Geometry& rhs) const { return !operator==(rhs); }
Mathias Agopian13127d82013-03-05 17:47:11 -0800110 };
111
Ady Abrahambdda8f02021-04-01 16:06:11 -0700112 using FrameRate = scheduler::LayerInfo::FrameRate;
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700113 using FrameRateCompatibility = scheduler::FrameRateCompatibility;
Rachel Lee58cc90d2023-09-05 18:50:20 -0700114 using FrameRateSelectionStrategy = scheduler::LayerInfo::FrameRateSelectionStrategy;
Ady Abraham71c437d2020-01-31 15:56:57 -0800115
Mathias Agopian13127d82013-03-05 17:47:11 -0800116 struct State {
Robert Carrae060832016-11-28 10:51:00 -0800117 int32_t z;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700118 ui::LayerStack layerStack;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800119 uint32_t flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800120 int32_t sequence; // changes when visible regions can change
Dan Stoza7dde5992015-05-22 09:51:44 -0700121 bool modified;
Fabien Sanglard4ed383c2016-12-13 14:02:41 -0800122 // Crop is expressed in layer space coordinate.
chaviw25714502021-02-11 10:01:08 -0800123 Rect crop;
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800124 LayerMetadata metadata;
Robert Carrdb66e622017-04-10 16:55:57 -0700125 // If non-null, a Surface this Surface's Z-order is interpreted relative to.
126 wp<Layer> zOrderRelativeOf;
chaviwe5ac40f2019-09-24 16:36:55 -0700127 bool isRelativeOf{false};
Robert Carrdb66e622017-04-10 16:55:57 -0700128
129 // A list of surfaces whose Z-order is interpreted relative to ours.
130 SortedVector<wp<Layer>> zOrderRelatives;
chaviw13fdc492017-06-27 12:40:18 -0700131 half4 color;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700132 float cornerRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800133 int backgroundBlurRadius;
chaviw98318de2021-05-19 16:45:23 -0500134 gui::WindowInfo inputInfo;
Vishnu Nair6fabeec2019-03-12 13:42:49 -0700135 wp<Layer> touchableRegionCrop;
Robert Carr720e5062018-07-30 17:45:14 -0700136
Valerie Haub153bab2019-03-05 10:47:28 -0800137 ui::Dataspace dataspace;
138
Valerie Hau134651a2020-01-28 16:21:22 -0800139 uint64_t frameNumber;
Alec Mouri21d94322023-10-17 19:51:39 +0000140 uint64_t previousFrameNumber;
Vishnu Nair63221212023-04-06 15:17:37 -0700141 // high watermark framenumber to use to check for barriers to protect ourselves
142 // from out of order transactions
143 uint64_t barrierFrameNumber;
chaviw766c9c52021-02-10 17:36:47 -0800144 ui::Transform transform;
liulijuneb489f62022-10-17 22:02:14 +0800145
146 uint32_t producerId = 0;
Vishnu Nair63221212023-04-06 15:17:37 -0700147 // high watermark producerId to use to check for barriers to protect ourselves
148 // from out of order transactions
149 uint32_t barrierProducerId = 0;
150
chaviw766c9c52021-02-10 17:36:47 -0800151 uint32_t bufferTransform;
Marissa Wall61c58622018-07-18 10:12:20 -0700152 bool transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700153 Region transparentRegionHint;
Alec Mouria90a5702021-04-16 16:36:21 +0000154 std::shared_ptr<renderengine::ExternalTexture> buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700155 sp<Fence> acquireFence;
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700156 std::shared_ptr<FenceTime> acquireFenceTime;
Marissa Wall61c58622018-07-18 10:12:20 -0700157 HdrMetadata hdrMetadata;
158 Region surfaceDamageRegion;
159 int32_t api;
Marissa Wall61c58622018-07-18 10:12:20 -0700160 sp<NativeHandle> sidebandStream;
Peiyong Lind3788632018-09-18 16:01:31 -0700161 mat4 colorTransform;
Peiyong Lin747321c2018-10-01 10:03:11 -0700162 bool hasColorTransform;
Valerie Haua72e2812019-01-23 13:40:39 -0800163 // pointer to background color layer that, if set, appears below the buffer state layer
164 // and the buffer state layer's children. Z order will be set to
165 // INT_MIN
166 sp<Layer> bgColorLayer;
Valerie Haued54efa2019-01-11 20:03:14 -0800167
Marissa Walle2ffb422018-10-12 11:33:52 -0700168 // The deque of callback handles for this frame. The back of the deque contains the most
169 // recent callback handle.
170 std::deque<sp<CallbackHandle>> callbackHandles;
Peiyong Linc502cb72019-03-01 15:00:23 -0800171 bool colorSpaceAgnostic;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800172 nsecs_t desiredPresentTime = 0;
173 bool isAutoTimestamp = true;
Vishnu Nair08f6eae2019-11-26 14:01:39 -0800174
175 // Length of the cast shadow. If the radius is > 0, a shadow of length shadowRadius will
176 // be rendered around the layer.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700177 float shadowRadius;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100178
Lucas Dupinc3800b82020-10-02 16:24:48 -0700179 // Layer regions that are made of custom materials, like frosted glass
180 std::vector<BlurRegion> blurRegions;
181
Ana Krulecc84d09b2019-11-02 23:10:29 +0100182 // Priority of the layer assigned by Window Manager.
183 int32_t frameRateSelectionPriority;
Steven Thomas3172e202020-01-06 19:25:30 -0800184
Andy Labrada096227e2022-06-15 16:58:11 +0000185 // Default frame rate compatibility used to set the layer refresh rate votetype.
186 FrameRateCompatibility defaultFrameRateCompatibility;
Ady Abraham71c437d2020-01-31 15:56:57 -0800187 FrameRate frameRate;
Ady Abraham60e42ea2020-03-09 19:17:31 -0700188
Ady Abraham59fd8ff2021-04-15 20:13:30 -0700189 // The combined frame rate of parents / children of this layer
190 FrameRate frameRateForLayerTree;
Vishnu Nair6213bd92020-05-08 17:42:25 -0700191
Rachel Lee58cc90d2023-09-05 18:50:20 -0700192 FrameRateSelectionStrategy frameRateSelectionStrategy;
193
Vishnu Nair6213bd92020-05-08 17:42:25 -0700194 // Set by window manager indicating the layer and all its children are
195 // in a different orientation than the display. The hint suggests that
196 // the graphic producers should receive a transform hint as if the
197 // display was in this orientation. When the display changes to match
198 // the layer orientation, the graphic producer may not need to allocate
199 // a buffer of a different size. ui::Transform::ROT_INVALID means the
200 // a fixed transform hint is not set.
201 ui::Transform::RotationFlags fixedTransformHint;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700202
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000203 // The vsync info that was used to start the transaction
204 FrameTimelineInfo frameTimelineInfo;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700205
206 // When the transaction was posted
207 nsecs_t postTime;
Vishnu Nair1506b182021-02-22 14:35:15 -0800208 sp<ITransactionCompletedListener> releaseBufferListener;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000209 // SurfaceFrame that tracks the timeline of Transactions that contain a Buffer. Only one
210 // such SurfaceFrame exists because only one buffer can be presented on the layer per vsync.
211 // If multiple buffers are queued, the prior ones will be dropped, along with the
212 // SurfaceFrame that's tracking them.
213 std::shared_ptr<frametimeline::SurfaceFrame> bufferSurfaceFrameTX;
214 // A map of token(frametimelineVsyncId) to the SurfaceFrame that's tracking a transaction
215 // that contains the token. Only one SurfaceFrame exisits for transactions that share the
216 // same token, unless they are presented in different vsyncs.
217 std::unordered_map<int64_t, std::shared_ptr<frametimeline::SurfaceFrame>>
218 bufferlessSurfaceFramesTX;
219 // An arbitrary threshold for the number of BufferlessSurfaceFrames in the state. Used to
220 // trigger a warning if the number of SurfaceFrames crosses the threshold.
221 static constexpr uint32_t kStateSurfaceFramesThreshold = 25;
John Reckcdb4ed72021-02-04 13:39:33 -0500222
223 // Stretch effect to apply to this layer
224 StretchEffect stretchEffect;
chaviwf3f40fe2021-04-27 15:54:02 -0500225
Winson Chunga30f7c92021-06-29 15:42:56 -0700226 // Whether or not this layer is a trusted overlay for input
227 bool isTrustedOverlay;
chaviwf3f40fe2021-04-27 15:54:02 -0500228 Rect bufferCrop;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700229 Rect destinationFrame;
chaviw0b06a8d2021-08-06 11:49:08 -0500230 sp<IBinder> releaseBufferEndpoint;
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -0700231 gui::DropInputMode dropInputMode;
Vishnu Nair86653e92021-11-03 17:19:36 -0700232 bool autoRefresh = false;
Sally Qi421ffb02022-03-21 19:41:33 -0700233 bool dimmingEnabled = true;
Sally Qi963049b2023-03-23 14:06:21 -0700234 float currentHdrSdrRatio = 1.f;
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000235 float desiredHdrSdrRatio = -1.f;
Alec Mourif4af03e2023-02-11 00:25:24 +0000236 gui::CachingHint cachingHint = gui::CachingHint::Enabled;
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000237 int64_t latchedVsyncId = 0;
Ady Abraham55269162023-05-09 11:26:06 -0700238 bool useVsyncIdForRefreshRateSelection = false;
Mathias Agopian13127d82013-03-05 17:47:11 -0800239 };
240
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000241 explicit Layer(const surfaceflinger::LayerCreationArgs& args);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700242 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243
Peiyong Linf243e5e2020-08-22 17:40:59 -0700244 static bool isLayerFocusedBasedOnPriority(int32_t priority);
245 static void miniDumpHeader(std::string& result);
Dominik Laskowski75848362019-11-11 17:57:20 -0800246
Peiyong Linf243e5e2020-08-22 17:40:59 -0700247 // Provide unique string for each class type in the Layer hierarchy
Patrick Williamsbb25f802022-08-30 23:02:34 +0000248 virtual const char* getType() const { return "Layer"; }
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700249
Peiyong Linf243e5e2020-08-22 17:40:59 -0700250 // true if this layer is visible, false otherwise
Patrick Williamsbb25f802022-08-30 23:02:34 +0000251 virtual bool isVisible() const;
Chia-I Wuab0c3192017-08-01 11:29:00 -0700252
Melody Hsu12ecd082024-04-17 19:03:13 +0000253 virtual sp<Layer> createClone();
Peiyong Linf243e5e2020-08-22 17:40:59 -0700254
Robert Carr7bf247e2017-05-18 14:02:49 -0700255 // Set a 2x2 transformation matrix on the layer. This transform
256 // will be applied after parent transforms, but before any final
257 // producer specified transform.
Patrick Williamsbb25f802022-08-30 23:02:34 +0000258 bool setMatrix(const layer_state_t::matrix22_t& matrix);
Robert Carr7bf247e2017-05-18 14:02:49 -0700259
260 // This second set of geometry attributes are controlled by
261 // setGeometryAppliesWithResize, and their default mode is to be
262 // immediate. If setGeometryAppliesWithResize is specified
263 // while a resize is pending, then update of these attributes will
264 // be delayed until the resize completes.
265
266 // setPosition operates in parent buffer space (pre parent-transform) or display
267 // space for top-level layers.
Patrick Williamsbb25f802022-08-30 23:02:34 +0000268 bool setPosition(float x, float y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700269 // Buffer space
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000270 bool setCrop(const Rect& crop);
Robert Carr8d5227b2017-03-16 15:41:03 -0700271
Robert Carr7bf247e2017-05-18 14:02:49 -0700272 // TODO(b/38182121): Could we eliminate the various latching modes by
273 // using the layer hierarchy?
274 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 virtual bool setLayer(int32_t z);
276 virtual bool setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ);
Robert Carrdb66e622017-04-10 16:55:57 -0700277
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800278 virtual bool setAlpha(float alpha);
Patrick Williamsbb25f802022-08-30 23:02:34 +0000279 bool setColor(const half3& /*color*/);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700280
281 // Set rounded corner radius for this layer and its children.
282 //
283 // We only support 1 radius per layer in the hierarchy, where parent layers have precedence.
284 // The shape of the rounded corner rectangle is specified by the crop rectangle of the layer
285 // from which we inferred the rounded corner radius.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800286 virtual bool setCornerRadius(float cornerRadius);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800287 // When non-zero, everything below this layer will be blurred by backgroundBlurRadius, which
288 // is specified in pixels.
289 virtual bool setBackgroundBlurRadius(int backgroundBlurRadius);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700290 virtual bool setBlurRegions(const std::vector<BlurRegion>& effectRegions);
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000291 bool setTransparentRegionHint(const Region& transparent);
Winson Chunga30f7c92021-06-29 15:42:56 -0700292 virtual bool setTrustedOverlay(bool);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800293 virtual bool setFlags(uint32_t flags, uint32_t mask);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700294 virtual bool setLayerStack(ui::LayerStack);
Vishnu Nair73908d12022-10-24 21:46:42 -0700295 virtual ui::LayerStack getLayerStack(
296 LayerVector::StateSet state = LayerVector::StateSet::Drawing) const;
297
Evan Roskyef876c92019-01-25 17:46:06 -0800298 virtual bool setMetadata(const LayerMetadata& data);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200299 virtual void setChildrenDrawingParent(const sp<Layer>&);
Josh Gao194ff392022-09-08 16:19:29 -0700300 virtual bool reparent(const sp<IBinder>& newParentHandle) REQUIRES(mFlinger->mStateLock);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800301 virtual bool setColorTransform(const mat4& matrix);
302 virtual mat4 getColorTransform() const;
303 virtual bool hasColorTransform() const;
Peiyong Linc502cb72019-03-01 15:00:23 -0800304 virtual bool isColorSpaceAgnostic() const { return mDrawingState.colorSpaceAgnostic; }
John Reck68796592023-01-25 13:47:12 -0500305 virtual bool isDimmingEnabled() const { return getDrawingState().dimmingEnabled; }
Sally Qi963049b2023-03-23 14:06:21 -0700306 float getDesiredHdrSdrRatio() const { return getDrawingState().desiredHdrSdrRatio; }
307 float getCurrentHdrSdrRatio() const { return getDrawingState().currentHdrSdrRatio; }
Alec Mourif4af03e2023-02-11 00:25:24 +0000308 gui::CachingHint getCachingHint() const { return getDrawingState().cachingHint; }
Marissa Wall61c58622018-07-18 10:12:20 -0700309
Patrick Williamsbb25f802022-08-30 23:02:34 +0000310 bool setTransform(uint32_t /*transform*/);
311 bool setTransformToDisplayInverse(bool /*transformToDisplayInverse*/);
312 bool setBuffer(std::shared_ptr<renderengine::ExternalTexture>& /* buffer */,
313 const BufferData& /* bufferData */, nsecs_t /* postTime */,
314 nsecs_t /*desiredPresentTime*/, bool /*isAutoTimestamp*/,
Vishnu Nair39a74a92024-07-29 19:01:50 +0000315 const FrameTimelineInfo& /*info*/, gui::GameMode gameMode);
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000316 void setDesiredPresentTime(nsecs_t /*desiredPresentTime*/, bool /*isAutoTimestamp*/);
Patrick Williamsbb25f802022-08-30 23:02:34 +0000317 bool setDataspace(ui::Dataspace /*dataspace*/);
John Reck68796592023-01-25 13:47:12 -0500318 bool setExtendedRangeBrightness(float currentBufferRatio, float desiredRatio);
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000319 bool setDesiredHdrHeadroom(float desiredRatio);
Alec Mourif4af03e2023-02-11 00:25:24 +0000320 bool setCachingHint(gui::CachingHint cachingHint);
Patrick Williamsbb25f802022-08-30 23:02:34 +0000321 bool setHdrMetadata(const HdrMetadata& /*hdrMetadata*/);
322 bool setSurfaceDamageRegion(const Region& /*surfaceDamage*/);
323 bool setApi(int32_t /*api*/);
Dorin Drimuse5374e52023-08-02 17:52:43 +0000324 bool setSidebandStream(const sp<NativeHandle>& /*sidebandStream*/,
Vishnu Nair39a74a92024-07-29 19:01:50 +0000325 const FrameTimelineInfo& /* info*/, nsecs_t /* postTime */,
326 gui::GameMode gameMode);
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000327 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& /*handles*/,
328 bool willPresent);
Josh Gao194ff392022-09-08 16:19:29 -0700329 virtual bool setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace)
330 REQUIRES(mFlinger->mStateLock);
Peiyong Linc502cb72019-03-01 15:00:23 -0800331 virtual bool setColorSpaceAgnostic(const bool agnostic);
Sally Qi421ffb02022-03-21 19:41:33 -0700332 virtual bool setDimmingEnabled(const bool dimmingEnabled);
Vishnu Nair6213bd92020-05-08 17:42:25 -0700333 virtual bool setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint);
Patrick Williamsbb25f802022-08-30 23:02:34 +0000334 void setAutoRefresh(bool /* autoRefresh */);
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -0700335 bool setDropInputMode(gui::DropInputMode);
336
Patrick Williamsbb25f802022-08-30 23:02:34 +0000337 ui::Dataspace getDataSpace() const;
Chia-I Wu01591c92018-05-22 12:03:00 -0700338
Alec Mouri89f5d4e2023-10-20 17:12:49 +0000339 virtual bool isFrontBuffered() const;
340
Vishnu Naire14c6b32022-08-06 04:20:15 +0000341 virtual sp<LayerFE> getCompositionEngineLayerFE() const;
Patrick Williams7584c6a2022-10-29 02:10:58 +0000342 virtual sp<LayerFE> copyCompositionEngineLayerFE() const;
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000343 sp<LayerFE> getCompositionEngineLayerFE(const frontend::LayerHierarchy::TraversalPath&);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000344 sp<LayerFE> getOrCreateCompositionEngineLayerFE(const frontend::LayerHierarchy::TraversalPath&);
Vishnu Naire14c6b32022-08-06 04:20:15 +0000345
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000346 const frontend::LayerSnapshot* getLayerSnapshot() const;
347 frontend::LayerSnapshot* editLayerSnapshot();
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000348 std::unique_ptr<frontend::LayerSnapshot> stealLayerSnapshot();
349 void updateLayerSnapshot(std::unique_ptr<frontend::LayerSnapshot> snapshot);
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800350
Dan Stozaee44edd2015-03-23 15:50:23 -0700351 // If we have received a new buffer this frame, we will pass its surface
352 // damage down to hardware composer. Otherwise, we must send a region with
353 // one empty rect.
Patrick Williamsbb25f802022-08-30 23:02:34 +0000354 void useSurfaceDamage();
355 void useEmptyDamage();
Alec Mouriadebf5c2021-01-05 12:57:36 -0800356 Region getVisibleRegion(const DisplayDevice*) const;
Vishnu Naira156f482023-02-22 00:23:38 +0000357 void updateLastLatchTime(nsecs_t latchtime);
Dan Stozaee44edd2015-03-23 15:50:23 -0700358
Peiyong Linf243e5e2020-08-22 17:40:59 -0700359 /*
360 * isOpaque - true if this surface is opaque
361 *
362 * This takes into account the buffer format (i.e. whether or not the
363 * pixel format includes an alpha channel) and the "opaque" flag set
364 * on the layer. It does not examine the current plane alpha value.
365 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000366 bool isOpaque(const Layer::State&) const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700367
368 /*
369 * Returns whether this layer can receive input.
370 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000371 bool canReceiveInput() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700372
373 /*
Patrick Williams1caf3b72023-03-31 10:59:10 -0500374 * Whether or not the layer should be considered visible for input calculations.
375 */
376 virtual bool isVisibleForInput() const {
377 // For compatibility reasons we let layers which can receive input
378 // receive input before they have actually submitted a buffer. Because
379 // of this we use canReceiveInput instead of isVisible to check the
380 // policy-visibility, ignoring the buffer state. However for layers with
381 // hasInputInfo()==false we can use the real visibility state.
382 // We are just using these layers for occlusion detection in
383 // InputDispatcher, and obviously if they aren't visible they can't occlude
384 // anything.
385 return hasInputInfo() ? canReceiveInput() : isVisible();
386 }
387
388 /*
Peiyong Lin05cc0112020-10-14 16:16:37 -0700389 * isProtected - true if the layer may contain protected contents in the
Peiyong Linf243e5e2020-08-22 17:40:59 -0700390 * GRALLOC_USAGE_PROTECTED sense.
391 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000392 bool isProtected() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700393
394 /*
395 * isFixedSize - true if content has a fixed size
396 */
397 virtual bool isFixedSize() const { return true; }
398
399 /*
400 * usesSourceCrop - true if content should use a source crop
401 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000402 bool usesSourceCrop() const { return hasBufferOrSidebandStream(); }
Peiyong Linf243e5e2020-08-22 17:40:59 -0700403
404 // Most layers aren't created from the main thread, and therefore need to
405 // grab the SF state lock to access HWC, but ContainerLayer does, so we need
406 // to avoid grabbing the lock again to avoid deadlock
407 virtual bool isCreatedFromMainThread() const { return false; }
408
chaviw4e765532021-04-30 12:11:39 -0500409 ui::Transform getActiveTransform(const Layer::State& s) const { return s.transform; }
Patrick Williamsbb25f802022-08-30 23:02:34 +0000410 Region getActiveTransparentRegion(const Layer::State& s) const {
411 return s.transparentRegionHint;
412 }
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000413 Rect getCrop(const Layer::State& s) const { return s.crop; }
Patrick Williamsbb25f802022-08-30 23:02:34 +0000414 bool needsFiltering(const DisplayDevice*) const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700415
416 // True if this layer requires filtering
417 // This method is distinct from needsFiltering() in how the filter
418 // requirement is computed. needsFiltering() compares displayFrame and crop,
419 // where as this method transforms the displayFrame to layer-stack space
420 // first. This method should be used if there is no physical display to
421 // project onto when taking screenshots, as the filtering requirements are
422 // different.
423 // If the parent transform needs to be undone when capturing the layer, then
424 // the inverse parent transform is also required.
Patrick Williamsbb25f802022-08-30 23:02:34 +0000425 bool needsFilteringForScreenshots(const DisplayDevice*, const ui::Transform&) const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700426
Patrick Williamsbb25f802022-08-30 23:02:34 +0000427 // from graphics API
Vishnu Nair3996ee32023-08-14 04:32:31 +0000428 static ui::Dataspace translateDataspace(ui::Dataspace dataspace);
Patrick Williamsbb25f802022-08-30 23:02:34 +0000429 void updateCloneBufferInfo();
430 uint64_t mPreviousFrameNumber = 0;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700431
Leon Scroggins III5b581492023-10-31 14:29:41 -0400432 void onCompositionPresented(const DisplayDevice*,
433 const std::shared_ptr<FenceTime>& /*glDoneFence*/,
434 const std::shared_ptr<FenceTime>& /*presentFence*/,
Vishnu Nair39a74a92024-07-29 19:01:50 +0000435 const CompositorTiming&, gui::GameMode gameMode);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700436
437 // If a buffer was replaced this frame, release the former buffer
Patrick Williamsbb25f802022-08-30 23:02:34 +0000438 void releasePendingBuffer(nsecs_t /*dequeueReadyTime*/);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700439
Peiyong Linf243e5e2020-08-22 17:40:59 -0700440 /*
441 * latchBuffer - called each time the screen is redrawn and returns whether
442 * the visible regions need to be recomputed (this is a fairly heavy
443 * operation, so this should be set only if needed). Typically this is used
444 * to figure out if the content or size of a surface has changed.
445 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000446 bool latchBuffer(bool& /*recomputeVisibleRegions*/, nsecs_t /*latchTime*/);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700447
Vishnu Naird47bcee2023-02-24 18:08:51 +0000448 bool latchBufferImpl(bool& /*recomputeVisibleRegions*/, nsecs_t /*latchTime*/,
449 bool bgColorOnly);
450
Patrick Williamsbb25f802022-08-30 23:02:34 +0000451 /*
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700452 * Returns true if the currently presented buffer will be released when this layer state
453 * is latched. This will return false if there is no buffer currently presented.
454 */
455 bool willReleaseBufferOnLatch() const;
456
457 /*
Patrick Williamsbb25f802022-08-30 23:02:34 +0000458 * Calls latchBuffer if the buffer has a frame queued and then releases the buffer.
459 * This is used if the buffer is just latched and releases to free up the buffer
460 * and will not be shown on screen.
461 * Should only be called on the main thread.
462 */
463 void latchAndReleaseBuffer();
Peiyong Linf243e5e2020-08-22 17:40:59 -0700464
465 /*
466 * returns the rectangle that crops the content of the layer and scales it
467 * to the layer's size.
468 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000469 Rect getBufferCrop() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700470
471 /*
472 * Returns the transform applied to the buffer.
473 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000474 uint32_t getBufferTransform() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700475
Patrick Williamsbb25f802022-08-30 23:02:34 +0000476 sp<GraphicBuffer> getBuffer() const;
477 const std::shared_ptr<renderengine::ExternalTexture>& getExternalTexture() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700478
Peiyong Linf243e5e2020-08-22 17:40:59 -0700479 /*
480 * Returns if a frame is ready
481 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000482 bool hasReadyFrame() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700483
484 virtual int32_t getQueuedFrameCount() const { return 0; }
485
Peiyong Linf243e5e2020-08-22 17:40:59 -0700486 /**
487 * Returns active buffer size in the correct orientation. Buffer size is determined by undoing
Patrick Williamsbb25f802022-08-30 23:02:34 +0000488 * any buffer transformations. Returns Rect::INVALID_RECT if the layer has no buffer or the
489 * layer does not have a display frame and its parent is not bounded.
Peiyong Linf243e5e2020-08-22 17:40:59 -0700490 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000491 Rect getBufferSize(const Layer::State&) const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700492
493 /**
494 * Returns the source bounds. If the bounds are not defined, it is inferred from the
495 * buffer size. Failing that, the bounds are determined from the passed in parent bounds.
496 * For the root layer, this is the display viewport size.
497 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000498 FloatRect computeSourceBounds(const FloatRect& parentBounds) const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700499 virtual FrameRate getFrameRateForLayerTree() const;
500
Patrick Williamsbb25f802022-08-30 23:02:34 +0000501 bool getTransformToDisplayInverse() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700502
503 // Returns how rounded corners should be drawn for this layer.
Vishnu Nair9dbf8e82021-09-16 16:24:47 -0700504 // A layer can override its parent's rounded corner settings if the parent's rounded
505 // corner crop does not intersect with its own rounded corner crop.
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000506 virtual frontend::RoundedCornerState getRoundedCornerState() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700507
Vishnu Naire14c6b32022-08-06 04:20:15 +0000508 bool hasRoundedCorners() const { return getRoundedCornerState().hasRoundedCorners(); }
Leon Scroggins IIIf8a18312021-04-01 16:38:25 -0400509
Patrick Williamsbb25f802022-08-30 23:02:34 +0000510 PixelFormat getPixelFormat() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700511 /**
Patrick Williamsbb25f802022-08-30 23:02:34 +0000512 * Return whether this layer needs an input info. We generate InputWindowHandles for all
513 * non-cursor buffered layers regardless of whether they have an InputChannel. This is to enable
514 * the InputDispatcher to do PID based occlusion detection.
Peiyong Linf243e5e2020-08-22 17:40:59 -0700515 */
Patrick Williamsbb25f802022-08-30 23:02:34 +0000516 bool needsInputInfo() const {
517 return (hasInputInfo() || hasBufferOrSidebandStream()) && !mPotentialCursor;
518 }
Peiyong Linf243e5e2020-08-22 17:40:59 -0700519
520 // Implements RefBase.
521 void onFirstRef() override;
522
Patrick Williamsbb25f802022-08-30 23:02:34 +0000523 struct BufferInfo {
524 nsecs_t mDesiredPresentTime;
525 std::shared_ptr<FenceTime> mFenceTime;
526 sp<Fence> mFence;
527 uint32_t mTransform{0};
528 ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN};
529 Rect mCrop;
530 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
531 Region mSurfaceDamage;
532 HdrMetadata mHdrMetadata;
533 int mApi;
534 PixelFormat mPixelFormat{PIXEL_FORMAT_NONE};
535 bool mTransformToDisplayInverse{false};
536
537 std::shared_ptr<renderengine::ExternalTexture> mBuffer;
538 uint64_t mFrameNumber;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700539 sp<IBinder> mReleaseBufferEndpoint;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000540
541 bool mFrameLatencyNeeded{false};
Alec Mouri1b0d4e12024-02-12 22:27:19 +0000542 float mDesiredHdrSdrRatio = -1.f;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000543 };
544
545 BufferInfo mBufferInfo;
546
Peiyong Linf243e5e2020-08-22 17:40:59 -0700547 // implements compositionengine::LayerFE
Patrick Williamsbb25f802022-08-30 23:02:34 +0000548 const compositionengine::LayerFECompositionState* getCompositionState() const;
549 bool fenceHasSignaled() const;
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000550 void onPreComposition(nsecs_t refreshStartTime);
Alec Mouri9892aac2023-12-11 21:16:59 +0000551 void onLayerDisplayed(ftl::SharedFuture<FenceResult>, ui::LayerStack layerStack,
552 std::function<FenceResult(FenceResult)>&& continuation = nullptr);
Robert Carrccab4242021-09-28 16:53:03 -0700553
Melody Hsu793f8362024-01-08 20:00:35 +0000554 // Tracks mLastClientCompositionFence and gets the callback handle for this layer.
555 sp<CallbackHandle> findCallbackHandle();
556
557 // Adds the future release fence to a list of fences that are used to release the
558 // last presented buffer. Also keeps track of the layerstack in a list of previous
559 // layerstacks that have been presented.
560 void prepareReleaseCallbacks(ftl::Future<FenceResult>, ui::LayerStack layerStack);
561
Vishnu Naire14c6b32022-08-06 04:20:15 +0000562 void setWasClientComposed(const sp<Fence>& fence) {
Robert Carrccab4242021-09-28 16:53:03 -0700563 mLastClientCompositionFence = fence;
Robert Carr05da0082022-05-25 23:29:34 -0700564 mClearClientCompositionFenceOnLayerDisplayed = false;
Robert Carrccab4242021-09-28 16:53:03 -0700565 }
566
Vishnu Naire14c6b32022-08-06 04:20:15 +0000567 const char* getDebugName() const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700568
Peiyong Linf243e5e2020-08-22 17:40:59 -0700569 bool setShadowRadius(float shadowRadius);
570
571 // Before color management is introduced, contents on Android have to be
572 // desaturated in order to match what they appears like visually.
573 // With color management, these contents will appear desaturated, thus
574 // needed to be saturated so that they match what they are designed for
575 // visually.
576 bool isLegacyDataSpace() const;
577
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800578 uint32_t getTransactionFlags() const { return mTransactionFlags; }
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700579
Chavi Weingarten076acac2023-01-19 17:20:43 +0000580 static bool computeTrustedPresentationState(const FloatRect& bounds,
581 const FloatRect& sourceBounds,
582 const Region& coveredRegion,
583 const FloatRect& screenBounds, float,
584 const ui::Transform&,
585 const TrustedPresentationThresholds&);
Vishnu Nair781d7252023-01-30 18:16:01 +0000586 void updateTrustedPresentationState(const DisplayDevice* display,
587 const frontend::LayerSnapshot* snapshot, int64_t time_in_ms,
Chavi Weingarten076acac2023-01-19 17:20:43 +0000588 bool leaveState);
589
590 inline bool hasTrustedPresentationListener() {
591 return mTrustedPresentationListener.callbackInterface != nullptr;
592 }
593
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700594 // Sets the masked bits.
595 void setTransactionFlags(uint32_t mask);
596
597 // Clears and returns the masked bits.
598 uint32_t clearTransactionFlags(uint32_t mask);
Mathias Agopian13127d82013-03-05 17:47:11 -0800599
Vishnu Nair4351ad52019-02-11 14:13:02 -0800600 FloatRect getBounds(const Region& activeTransparentRegion) const;
601 FloatRect getBounds() const;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000602 Rect getInputBoundsInDisplaySpace(const FloatRect& insetBounds,
603 const ui::Transform& displayTransform);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800604
605 // Compute bounds for the layer and cache the results.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700606 void computeBounds(FloatRect parentBounds, ui::Transform parentTransform, float shadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800607
Vishnu Naire14c6b32022-08-06 04:20:15 +0000608 int32_t getSequence() const { return sequence; }
Pablo Ceballos40845df2016-01-25 17:41:15 -0800609
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700610 // For tracing.
611 // TODO: Replace with raw buffer id from buffer metadata when that becomes available.
612 // GraphicBuffer::getId() does not provide a reliable global identifier. Since the traces
613 // creates its tracks by buffer id and has no way of associating a buffer back to the process
614 // that created it, the current implementation is only sufficient for cases where a buffer is
615 // only used within a single layer.
chaviwd62d3062019-09-04 14:48:02 -0700616 uint64_t getCurrentBufferId() const { return getBuffer() ? getBuffer()->getId() : 0; }
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700617
Mathias Agopian13127d82013-03-05 17:47:11 -0800618 /*
619 * isSecure - true if this surface is secure, that is if it prevents
Peiyong Lin05cc0112020-10-14 16:16:37 -0700620 * screenshots or VNC servers. A surface can be set to be secure by the
621 * application, being secure doesn't mean the surface has DRM contents.
Mathias Agopian13127d82013-03-05 17:47:11 -0800622 */
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800623 bool isSecure() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800624
625 /*
Robert Carr1f0a16a2016-10-24 16:27:39 -0700626 * isHiddenByPolicy - true if this layer has been forced invisible.
627 * just because this is false, doesn't mean isVisible() is true.
628 * For example if this layer has no active buffer, it may not be hidden by
629 * policy, but it still can not be visible.
630 */
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800631 bool isHiddenByPolicy() const;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700632
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700633 // True if the layer should be skipped in screenshots, screen recordings,
634 // and mirroring to external or virtual displays.
635 bool isInternalDisplayOverlay() const;
636
637 ui::LayerFilter getOutputFilter() const {
638 return {getLayerStack(), isInternalDisplayOverlay()};
639 }
640
Robert Carr2e102c92018-10-23 12:11:15 -0700641 bool isRemovedFromCurrentState() const;
chaviw8b3871a2017-11-01 17:41:01 -0700642
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000643 perfetto::protos::LayerProto* writeToProto(perfetto::protos::LayersProto& layersProto,
644 uint32_t traceFlags);
645 void writeCompositionStateToProto(perfetto::protos::LayerProto* layerProto,
646 ui::LayerStack layerStack);
chaviw08f3cb22020-01-13 13:17:21 -0800647
Vishnu Nair8406fd72019-07-30 11:29:31 -0700648 // Write states that are modified by the main thread. This includes drawing
649 // state as well as buffer data. This should be called in the main or tracing
650 // thread.
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000651 void writeToProtoDrawingState(perfetto::protos::LayerProto* layerInfo);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700652 // Write drawing or current state. If writing current state, the caller should hold the
653 // external mStateLock. If writing drawing state, this function should be called on the
654 // main or tracing thread.
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000655 void writeToProtoCommonState(perfetto::protos::LayerProto* layerInfo, LayerVector::StateSet,
Vishnu Nair00b90132021-11-05 14:03:40 -0700656 uint32_t traceFlags = LayerTracing::TRACE_ALL);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800657
chaviw98318de2021-05-19 16:45:23 -0500658 gui::WindowInfo::Type getWindowType() const { return mWindowType; }
Peiyong Lin97067b02019-03-29 17:34:45 +0000659
Vishnu Nair3be61902023-04-26 19:47:29 -0700660 bool updateMirrorInfo(const std::deque<Layer*>& cloneRootsPendingUpdates);
chaviw74b03172019-08-19 11:09:03 -0700661
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700662 /*
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700663 * doTransaction - process the transaction. This is a good place to figure
664 * out which attributes of the surface have changed.
665 */
Vishnu Naira156f482023-02-22 00:23:38 +0000666 virtual uint32_t doTransaction(uint32_t transactionFlags);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700667
668 /*
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700669 * Remove relative z for the layer if its relative parent is not part of the
670 * provided layer tree.
671 */
672 void removeRelativeZ(const std::vector<Layer*>& layersInTree);
673
674 /*
675 * Remove from current state and mark for removal.
676 */
Josh Gao194ff392022-09-08 16:19:29 -0700677 void removeFromCurrentState() REQUIRES(mFlinger->mStateLock);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700678
679 /*
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800680 * called with the state lock from a binder thread when the layer is
Chia-I Wuc6657022017-08-15 11:18:17 -0700681 * removed from the current list to the pending removal list
682 */
Josh Gao194ff392022-09-08 16:19:29 -0700683 void onRemovedFromCurrentState() REQUIRES(mFlinger->mStateLock);
Chia-I Wuc6657022017-08-15 11:18:17 -0700684
chaviw61626f22018-11-15 16:26:27 -0800685 /*
686 * Called when the layer is added back to the current state list.
687 */
688 void addToCurrentState();
689
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800690 inline const State& getDrawingState() const { return mDrawingState; }
Robert Carr6a160312021-05-17 12:08:20 -0700691 inline State& getDrawingState() { return mDrawingState; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800692
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000693 void miniDump(std::string& result, const frontend::LayerSnapshot&, const DisplayDevice&) const;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800694 void dumpFrameStats(std::string& result) const;
Vishnu Nair76554eb2022-12-09 03:38:36 +0000695 void dumpOffscreenDebugInfo(std::string& result) const;
Svetoslavd85084b2014-03-20 10:28:31 -0700696 void clearFrameStats();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700697 void logFrameStats();
Svetoslavd85084b2014-03-20 10:28:31 -0700698 void getFrameStats(FrameStats* outStats) const;
Brian Anderson5ea5e592016-12-01 16:54:33 -0800699 void onDisconnect();
Pablo Ceballosce796e72016-02-04 19:10:51 -0800700
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800701 ui::Transform getTransform() const;
Garfield Tan2c1782c2022-02-16 15:25:05 -0800702 bool isTransformValid() const;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700703
Robert Carr6452f122017-03-21 10:41:29 -0700704 // Returns the Alpha of the Surface, accounting for the Alpha
705 // of parent Surfaces in the hierarchy (alpha's will be multiplied
706 // down the hierarchy).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800707 half getAlpha() const;
708 half4 getColor() const;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800709 int32_t getBackgroundBlurRadius() const;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800710 bool drawShadows() const { return mEffectiveShadowRadius > 0.f; };
Robert Carr6452f122017-03-21 10:41:29 -0700711
Vishnu Nair6213bd92020-05-08 17:42:25 -0700712 // Returns the transform hint set by Window Manager on the layer or one of its parents.
713 // This traverses the current state because the data is needed when creating
714 // the layer(off drawing thread) and the hint should be available before the producer
715 // is ready to acquire a buffer.
716 ui::Transform::RotationFlags getFixedTransformHint() const;
717
Edgar Arriaga844fa672020-01-16 14:21:42 -0800718 /**
719 * Traverse this layer and it's hierarchy of children directly. Unlike traverseInZOrder
720 * which will not emit children who have relativeZOrder to another layer, this method
721 * just directly emits all children. It also emits them in no particular order.
722 * So this method is not suitable for graphical operations, as it doesn't represent
723 * the scene state, but it's also more efficient than traverseInZOrder and so useful for
724 * book-keeping.
725 */
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200726 void traverse(LayerVector::StateSet, const LayerVector::Visitor&);
727 void traverseInReverseZOrder(LayerVector::StateSet, const LayerVector::Visitor&);
728 void traverseInZOrder(LayerVector::StateSet, const LayerVector::Visitor&);
Vishnu Nair787aa782023-03-17 13:46:46 -0700729 void traverseChildren(const LayerVector::Visitor&);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700730
chaviw4b129c22018-04-09 16:19:43 -0700731 /**
732 * Traverse only children in z order, ignoring relative layers that are not children of the
733 * parent.
734 */
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200735 void traverseChildrenInZOrder(LayerVector::StateSet, const LayerVector::Visitor&);
chaviwa76b2712017-09-20 12:02:26 -0700736
Vishnu Nair787aa782023-03-17 13:46:46 -0700737 size_t getDescendantCount() const;
738 size_t getChildrenCount() const { return mDrawingChildren.size(); }
739 bool isHandleAlive() const { return mHandleAlive; }
740 bool onHandleDestroyed() { return mHandleAlive = false; }
Valerie Hauc5686802019-11-22 14:18:09 -0800741
742 // ONLY CALL THIS FROM THE LAYER DTOR!
743 // See b/141111965. We need to add current children to offscreen layers in
744 // the layer dtor so as not to dangle layers. Since the layer has not
745 // committed its transaction when the layer is destroyed, we must add
746 // current children. This is safe in the dtor as we will no longer update
747 // the current state, but should not be called anywhere else!
748 LayerVector& getCurrentChildren() { return mCurrentChildren; }
749
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200750 void addChild(const sp<Layer>&);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700751 // Returns index if removed, or negative value otherwise
752 // for symmetry with Vector::remove
753 ssize_t removeChild(const sp<Layer>& layer);
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000754 sp<Layer> getParent() const { return mCurrentParent.promote(); }
Vishnu Nair14d218b2021-07-13 13:57:39 -0700755
756 // Should be called with the surfaceflinger statelock held
757 bool isAtRoot() const { return mIsAtRoot; }
758 void setIsAtRoot(bool isAtRoot) { mIsAtRoot = isAtRoot; }
759
Robert Carr1f0a16a2016-10-24 16:27:39 -0700760 bool hasParent() const { return getParent() != nullptr; }
Vishnu Nair4351ad52019-02-11 14:13:02 -0800761 Rect getScreenBounds(bool reduceTransparentRegion = true) const;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700762 bool setChildLayer(const sp<Layer>& childLayer, int32_t z);
Robert Carr503c7042017-09-27 15:06:08 -0700763 bool setChildRelativeLayer(const sp<Layer>& childLayer,
764 const sp<IBinder>& relativeToHandle, int32_t relativeZ);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700765
766 // Copy the current list of children to the drawing state. Called by
767 // SurfaceFlinger to complete a transaction.
768 void commitChildList();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200769 int32_t getZ(LayerVector::StateSet) const;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800770
Vishnu Nairefc42e22019-12-03 17:36:12 -0800771 /**
772 * Returns the cropped buffer size or the layer crop if the layer has no buffer. Return
773 * INVALID_RECT if the layer has no buffer and no crop.
774 * A layer with an invalid buffer size and no crop is considered to be boundless. The layer
775 * bounds are constrained by its parent bounds.
776 */
777 Rect getCroppedBufferSize(const Layer::State& s) const;
778
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000779 virtual void setFrameTimelineInfoForBuffer(const FrameTimelineInfo& /*info*/) {}
Vishnu Nair39a74a92024-07-29 19:01:50 +0000780 void setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info, nsecs_t postTime,
781 gui::GameMode gameMode);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000782 void setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
Vishnu Nair39a74a92024-07-29 19:01:50 +0000783 nsecs_t postTime, gui::GameMode gameMode);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000784
Ady Abraham88930f92023-06-07 10:32:08 -0700785 void addSurfaceFrameDroppedForBuffer(std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame,
786 nsecs_t dropTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000787 void addSurfaceFramePresentedForBuffer(
788 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
789 nsecs_t currentLatchTime);
790
791 std::shared_ptr<frametimeline::SurfaceFrame> createSurfaceFrameForTransaction(
Vishnu Nair39a74a92024-07-29 19:01:50 +0000792 const FrameTimelineInfo& info, nsecs_t postTime, gui::GameMode gameMode);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000793 std::shared_ptr<frametimeline::SurfaceFrame> createSurfaceFrameForBuffer(
Vishnu Nair39a74a92024-07-29 19:01:50 +0000794 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName,
795 gui::GameMode gameMode);
Ady Abraham88930f92023-06-07 10:32:08 -0700796 void setFrameTimelineVsyncForSkippedFrames(const FrameTimelineInfo& info, nsecs_t postTime,
Vishnu Nair39a74a92024-07-29 19:01:50 +0000797 std::string debugName, gui::GameMode gameMode);
Ady Abraham74e17562020-08-24 18:18:19 -0700798
Chavi Weingarten328a8312023-01-26 21:17:52 +0000799 bool setTrustedPresentationInfo(TrustedPresentationThresholds const& thresholds,
Chavi Weingarten076acac2023-01-19 17:20:43 +0000800 TrustedPresentationListener const& listener);
801
Peiyong Linf243e5e2020-08-22 17:40:59 -0700802 // Creates a new handle each time, so we only expect
803 // this to be called once.
804 sp<IBinder> getHandle();
805 const std::string& getName() const { return mName; }
806 bool getPremultipledAlpha() const;
chaviw98318de2021-05-19 16:45:23 -0500807 void setInputInfo(const gui::WindowInfo& info);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700808
Prabir Pradhanda0f62c2022-07-22 19:53:04 +0000809 struct InputDisplayArgs {
810 const ui::Transform* transform = nullptr;
811 bool isSecure = false;
812 };
813 gui::WindowInfo fillInputInfo(const InputDisplayArgs& displayArgs);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700814
Peiyong Linf243e5e2020-08-22 17:40:59 -0700815 /**
816 * Returns whether this layer has an explicitly set input-info.
817 */
818 bool hasInputInfo() const;
819
Ady Abraham62a0be22020-12-08 16:54:10 -0800820 virtual uid_t getOwnerUid() const { return mOwnerUid; }
Peiyong Linf243e5e2020-08-22 17:40:59 -0700821
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700822 pid_t getOwnerPid() { return mOwnerPid; }
823
Tony Huangf3621102023-09-04 17:14:22 +0800824 int32_t getOwnerAppId() { return mOwnerAppId; }
825
Peiyong Linf243e5e2020-08-22 17:40:59 -0700826 // This layer is not a clone, but it's the parent to the cloned hierarchy. The
827 // variable mClonedChild represents the top layer that will be cloned so this
828 // layer will be the parent of mClonedChild.
829 // The layers in the cloned hierarchy will match the lifetime of the real layers. That is
830 // if the real layer is destroyed, then the clone layer will also be destroyed.
831 sp<Layer> mClonedChild;
Robert Carr6a0382d2021-07-01 15:57:17 -0700832 bool mHadClonedChild = false;
833 void setClonedChild(const sp<Layer>& mClonedChild);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700834
835 mutable bool contentDirty{false};
836 Region surfaceDamageRegion;
837
Arthur Hungc70bee22023-06-02 01:35:52 +0000838 // True when the surfaceDamageRegion is recognized as a small area update.
839 bool mSmallDirty{false};
840 // Used to check if mUsedVsyncIdForRefreshRateSelection should be expired when it stop updating.
841 nsecs_t mMaxTimeForUseVsyncId = 0;
842 // True when DrawState.useVsyncIdForRefreshRateSelection previously set to true during updating
843 // buffer.
844 bool mUsedVsyncIdForRefreshRateSelection{false};
845
Peiyong Linf243e5e2020-08-22 17:40:59 -0700846 // Layer serial number. This gives layers an explicit ordering, so we
847 // have a stable sort order when their layer stack and Z-order are
848 // the same.
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800849 const int32_t sequence;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700850
851 bool mPendingHWCDestroy{false};
Steven Thomas3172e202020-01-06 19:25:30 -0800852
Vishnu Nair59f6d2d2022-10-05 16:59:56 -0700853 bool backpressureEnabled() const {
854 return mDrawingState.flags & layer_state_t::eEnableBackpressure;
855 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800856
John Reckcdb4ed72021-02-04 13:39:33 -0500857 bool setStretchEffect(const StretchEffect& effect);
John Reckc00c6692021-02-16 11:37:33 -0500858 StretchEffect getStretchEffect() const;
John Reckcdb4ed72021-02-04 13:39:33 -0500859
Patrick Williamsbb25f802022-08-30 23:02:34 +0000860 bool setBufferCrop(const Rect& /* bufferCrop */);
861 bool setDestinationFrame(const Rect& /* destinationFrame */);
862 // See mPendingBufferTransactions
863 void decrementPendingBufferCount();
864 std::atomic<int32_t>* getPendingBufferCounter() { return &mPendingBufferTransactions; }
865 std::string getPendingBufferCounterName() { return mBlastTransactionName; }
866 bool updateGeometry();
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800867
Ady Abraham005398b2023-06-05 13:59:41 -0700868 bool isSimpleBufferUpdate(const layer_state_t& s) const;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000869
870 static bool isOpaqueFormat(PixelFormat format);
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000871
872 // Updates the LayerSnapshot. This must be called prior to sending layer data to
873 // CompositionEngine or RenderEngine (i.e. before calling CompositionEngine::present or
Vishnu Naire14c6b32022-08-06 04:20:15 +0000874 // LayerFE::prepareClientComposition).
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000875 //
876 // TODO(b/238781169) Remove direct calls to RenderEngine::drawLayers that don't go through
877 // CompositionEngine to create a single path for composing layers.
Vishnu Nairba354102022-08-01 00:14:18 +0000878 void updateSnapshot(bool updateGeometry);
Vishnu Nairfa9e2ef2023-05-22 15:10:20 -0700879 void updateChildrenSnapshots(bool updateGeometry);
Vishnu Nair332e6ba2023-01-25 17:16:19 -0800880 sp<Layer> getClonedFrom() const {
881 return mClonedFrom != nullptr ? mClonedFrom.promote() : nullptr;
882 }
883 bool isClone() { return mClonedFrom != nullptr; }
884
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000885 bool willPresentCurrentTransaction() const;
Ady Abraham9dada822022-02-03 10:26:59 -0800886
liulijuneb489f62022-10-17 22:02:14 +0800887 void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
888 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
889 const sp<Fence>& releaseFence);
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700890 bool setFrameRateForLayerTree(FrameRate, const scheduler::LayerProps&, nsecs_t now);
891 void recordLayerHistoryBufferUpdate(const scheduler::LayerProps&, nsecs_t now);
892 void recordLayerHistoryAnimationTx(const scheduler::LayerProps&, nsecs_t now);
Vishnu Nairc09c0232023-03-02 03:22:35 +0000893 bool hasBuffer() const { return mBufferInfo.mBuffer != nullptr; }
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700894 void setTransformHint(std::optional<ui::Transform::RotationFlags> transformHint) {
895 mTransformHint = transformHint;
896 }
Vishnu Nairc1d19d72023-08-10 12:35:11 -0700897 void commitTransaction();
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700898 // Keeps track of the previously presented layer stacks. This is used to get
899 // the release fences from the correct displays when we release the last buffer
900 // from the layer.
901 std::vector<ui::LayerStack> mPreviouslyPresentedLayerStacks;
Melody Hsu793f8362024-01-08 20:00:35 +0000902
Alec Mouri9892aac2023-12-11 21:16:59 +0000903 struct FenceAndContinuation {
904 ftl::SharedFuture<FenceResult> future;
905 std::function<FenceResult(FenceResult)> continuation;
906
907 ftl::SharedFuture<FenceResult> chain() const {
908 if (continuation) {
909 return ftl::Future(future).then(continuation).share();
910 } else {
911 return future;
912 }
913 }
914 };
Melody Hsu793f8362024-01-08 20:00:35 +0000915 std::vector<FenceAndContinuation> mPreviousReleaseFenceAndContinuations;
916
917 // Release fences for buffers that have not yet received a release
918 // callback. A release callback may not be given when capturing
919 // screenshots asynchronously. There may be no buffer update for the
920 // layer, but the layer will still be composited on the screen in every
921 // frame. Kepping track of these fences ensures that they are not dropped
Melody Hsue4ef87f2024-03-26 23:54:45 +0000922 // and can be dispatched to the client at a later time. Older fences are
923 // dropped when a layer stack receives a new fence.
924 // TODO(b/300533018): Track fence per multi-instance RenderEngine
925 ftl::SmallMap<ui::LayerStack, ftl::Future<FenceResult>, ui::kDisplayCapacity>
926 mAdditionalPreviousReleaseFences;
Melody Hsu793f8362024-01-08 20:00:35 +0000927
Josh Gao194ff392022-09-08 16:19:29 -0700928 // Exposed so SurfaceFlinger can assert that it's held
929 const sp<SurfaceFlinger> mFlinger;
930
Arthur Hungc70bee22023-06-02 01:35:52 +0000931 // Check if the damage region is a small dirty.
Arthur Hung69f95222023-10-04 07:39:02 +0000932 void setIsSmallDirty(const Region& damageRegion, const ui::Transform& layerToDisplayTransform);
933 void setIsSmallDirty(frontend::LayerSnapshot* snapshot);
Arthur Hungc70bee22023-06-02 01:35:52 +0000934
Mathias Agopian13127d82013-03-05 17:47:11 -0800935protected:
Peiyong Linf243e5e2020-08-22 17:40:59 -0700936 // For unit tests
937 friend class TestableSurfaceFlinger;
Alec Mouriadebf5c2021-01-05 12:57:36 -0800938 friend class FpsReporterTest;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700939 friend class RefreshRateSelectionTest;
940 friend class SetFrameRateTest;
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000941 friend class TransactionFrameTracerTest;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000942 friend class TransactionSurfaceFrameTest;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700943
Patrick Williamsbb25f802022-08-30 23:02:34 +0000944 void preparePerFrameCompositionState();
945 void preparePerFrameBufferCompositionState();
946 void preparePerFrameEffectsCompositionState();
Patrick Williamsbb25f802022-08-30 23:02:34 +0000947 void gatherBufferInfo();
Peiyong Linf243e5e2020-08-22 17:40:59 -0700948
Peiyong Linf243e5e2020-08-22 17:40:59 -0700949 bool isClonedFromAlive() { return getClonedFrom() != nullptr; }
950
Vishnu Nair3bb11d02021-11-26 09:24:11 -0800951 void cloneDrawingState(const Layer* from);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700952 void updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap);
953 void updateClonedChildren(const sp<Layer>& mirrorRoot,
954 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap);
955 void updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap);
956 void addChildToDrawing(const sp<Layer>&);
957 void updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap);
958
Peiyong Linf243e5e2020-08-22 17:40:59 -0700959 void prepareBasicGeometryCompositionState();
960 void prepareGeometryCompositionState();
961 void prepareCursorCompositionState();
962
963 uint32_t getEffectiveUsage(uint32_t usage) const;
964
965 /**
966 * Setup rounded corners coordinates of this layer, taking into account the layer bounds and
967 * crop coordinates, transforming them into layer space.
968 */
969 void setupRoundedCornersCropCoordinates(Rect win, const FloatRect& roundedCornersCrop) const;
970 void setParent(const sp<Layer>&);
971 LayerVector makeTraversalList(LayerVector::StateSet, bool* outSkipRelativeZUsers);
972 void addZOrderRelative(const wp<Layer>& relative);
973 void removeZOrderRelative(const wp<Layer>& relative);
974 compositionengine::OutputLayer* findOutputLayerForDisplay(const DisplayDevice*) const;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000975 compositionengine::OutputLayer* findOutputLayerForDisplay(
976 const DisplayDevice*, const frontend::LayerHierarchy::TraversalPath& path) const;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700977 bool usingRelativeZ(LayerVector::StateSet) const;
978
Patrick Williamsbb25f802022-08-30 23:02:34 +0000979 virtual ui::Transform getInputTransform() const;
Prabir Pradhan6fa425a2021-12-16 07:16:04 -0800980 /**
981 * Get the bounds in layer space within which this layer can receive input.
982 *
983 * These bounds are used to:
984 * - Determine the input frame for the layer to be used for occlusion detection; and
985 * - Determine the coordinate space within which the layer will receive input. The top-left of
986 * this rect will be the origin of the coordinate space that the input events sent to the
987 * layer will be in (prior to accounting for surface insets).
988 *
989 * The layer can still receive touch input if these bounds are invalid if
990 * "replaceTouchableRegionWithCrop" is specified. In this case, the layer will receive input
991 * in this layer's space, regardless of the specified crop layer.
992 */
Vishnu Nairfed7c122023-03-18 01:54:43 +0000993 std::pair<FloatRect, bool> getInputBounds(bool fillParentBounds) const;
chaviw39d01472021-04-08 14:26:24 -0500994
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700995 bool mPremultipliedAlpha{true};
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700996 const std::string mName;
997 const std::string mTransactionName{"TX - " + mName};
Mathias Agopian13127d82013-03-05 17:47:11 -0800998
Vishnu Nair8406fd72019-07-30 11:29:31 -0700999 // These are only accessed by the main thread or the tracing thread.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001000 State mDrawingState;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001001
Chavi Weingarten076acac2023-01-19 17:20:43 +00001002 TrustedPresentationThresholds mTrustedPresentationThresholds;
1003 TrustedPresentationListener mTrustedPresentationListener;
1004 bool mLastComputedTrustedPresentationState = false;
1005 bool mLastReportedTrustedPresentationState = false;
1006 int64_t mEnteredTrustedPresentationStateTime = -1;
1007
Robert Carr38d25002021-06-11 14:30:09 -07001008 uint32_t mTransactionFlags{0};
Robert Carr6a160312021-05-17 12:08:20 -07001009 // Updated in doTransaction, used to track the last sequence number we
1010 // committed. Currently this is really only used for updating visible
1011 // regions.
1012 int32_t mLastCommittedTxSequence = -1;
Dan Stoza7dde5992015-05-22 09:51:44 -07001013
Brian Andersond6927fb2016-07-23 23:37:30 -07001014 // Timestamp history for UIAutomation. Thread safe.
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001015 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001016
1017 // main thread
Lloyd Pique0b785d82018-12-04 17:25:27 -08001018 sp<NativeHandle> mSidebandStream;
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001019 // False if the buffer and its contents have been previously used for GPU
1020 // composition, true otherwise.
1021 bool mIsActiveBufferUpdatedForGpu = true;
1022
Robert Carrc3574f72016-03-24 12:19:32 -07001023 // We encode unset as -1.
Lloyd Pique42ab75e2018-09-12 20:46:03 -07001024 std::atomic<uint64_t> mCurrentFrameNumber{0};
Mathias Agopian13127d82013-03-05 17:47:11 -08001025 // Whether filtering is needed b/c of the drawingstate
Lloyd Pique42ab75e2018-09-12 20:46:03 -07001026 bool mNeedsFiltering{false};
David Sodman0c69cad2017-08-21 12:12:51 -07001027
Robert Carr6a160312021-05-17 12:08:20 -07001028 std::atomic<bool> mRemovedFromDrawingState{false};
chaviw8b3871a2017-11-01 17:41:01 -07001029
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001030 // page-flip thread (currently main thread)
Lloyd Pique42ab75e2018-09-12 20:46:03 -07001031 bool mProtectedByApp{false}; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -08001032
1033 // protected by mLock
1034 mutable Mutex mLock;
David Sodman0c69cad2017-08-21 12:12:51 -07001035
Mathias Agopian13127d82013-03-05 17:47:11 -08001036 const wp<Client> mClientRef;
Riley Andrews03414a12014-07-01 14:22:59 -07001037
1038 // This layer can be a cursor on some displays.
Lloyd Pique42ab75e2018-09-12 20:46:03 -07001039 bool mPotentialCursor{false};
Dan Stoza6b9454d2014-11-07 16:00:59 -08001040
Robert Carr88d76cd2021-06-22 16:23:51 -07001041 LayerVector mCurrentChildren{LayerVector::StateSet::Current};
Lloyd Pique42ab75e2018-09-12 20:46:03 -07001042 LayerVector mDrawingChildren{LayerVector::StateSet::Drawing};
Robert Carr1f0a16a2016-10-24 16:27:39 -07001043
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001044 wp<Layer> mCurrentParent;
1045 wp<Layer> mDrawingParent;
David Sodman9eeae692017-11-02 10:53:32 -07001046
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001047 // Window types from WindowManager.LayoutParams
chaviw98318de2021-05-19 16:45:23 -05001048 const gui::WindowInfo::Type mWindowType;
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001049
Alec Mouri9a29e672020-09-14 12:39:14 -07001050 // The owner of the layer. If created from a non system process, it will be the calling uid.
1051 // If created from a system process, the value can be passed in.
1052 uid_t mOwnerUid;
1053
Adithya Srinivasan9febda82020-10-19 10:49:41 -07001054 // The owner pid of the layer. If created from a non system process, it will be the calling pid.
1055 // If created from a system process, the value can be passed in.
1056 pid_t mOwnerPid;
1057
Tony Huangf3621102023-09-04 17:14:22 +08001058 int32_t mOwnerAppId;
1059
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001060 // Keeps track of the time SF latched the last buffer from this layer.
1061 // Used in buffer stuffing analysis in FrameTimeline.
1062 nsecs_t mLastLatchTime = 0;
1063
Robert Carr6a160312021-05-17 12:08:20 -07001064 mutable bool mDrawingStateModified = false;
Robert Carr0758e5d2021-03-11 22:15:04 -08001065
Robert Carrccab4242021-09-28 16:53:03 -07001066 sp<Fence> mLastClientCompositionFence;
Robert Carr05da0082022-05-25 23:29:34 -07001067 bool mClearClientCompositionFenceOnLayerDisplayed = false;
chaviw4b129c22018-04-09 16:19:43 -07001068private:
Tony Huangf3621102023-09-04 17:14:22 +08001069 // Range of uids allocated for a user.
1070 // This value is taken from android.os.UserHandle#PER_USER_RANGE.
1071 static constexpr int32_t PER_USER_RANGE = 100000;
1072
Patrick Williamsbb25f802022-08-30 23:02:34 +00001073 friend class SlotGenerationTest;
1074 friend class TransactionFrameTracerTest;
1075 friend class TransactionSurfaceFrameTest;
1076
1077 bool getAutoRefresh() const { return mDrawingState.autoRefresh; }
1078 bool getSidebandStreamChanged() const { return mSidebandStreamChanged; }
1079
1080 std::atomic<bool> mSidebandStreamChanged{false};
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001081
Peiyong Linf243e5e2020-08-22 17:40:59 -07001082 // Returns true if the layer can draw shadows on its border.
1083 virtual bool canDrawShadows() const { return true; }
1084
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05001085 aidl::android::hardware::graphics::composer3::Composition getCompositionType(
1086 const DisplayDevice&) const;
Vishnu Nair3cc15a42023-06-30 06:20:22 +00001087 aidl::android::hardware::graphics::composer3::Composition getCompositionType(
1088 const compositionengine::OutputLayer*) const;
chaviw4b129c22018-04-09 16:19:43 -07001089 /**
1090 * Returns an unsorted vector of all layers that are part of this tree.
1091 * That includes the current layer and all its descendants.
1092 */
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001093 std::vector<Layer*> getLayersInTree(LayerVector::StateSet);
chaviw4b129c22018-04-09 16:19:43 -07001094 /**
1095 * Traverses layers that are part of this tree in the correct z order.
1096 * layersInTree must be sorted before calling this method.
1097 */
1098 void traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001099 LayerVector::StateSet, const LayerVector::Visitor&);
1100 LayerVector makeChildrenTraversalList(LayerVector::StateSet,
chaviw4b129c22018-04-09 16:19:43 -07001101 const std::vector<Layer*>& layersInTree);
Vishnu Nair4351ad52019-02-11 14:13:02 -08001102
Rachel Lee0faad6c2023-09-08 13:34:34 -07001103 bool propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool overrideChildren,
1104 bool* transactionNeeded);
Peiyong Linf243e5e2020-08-22 17:40:59 -07001105 void setZOrderRelativeOf(const wp<Layer>& relativeOf);
Winson Chunga30f7c92021-06-29 15:42:56 -07001106 bool isTrustedOverlay() const;
Vishnu Naira066d902021-09-13 18:40:17 -07001107 gui::DropInputMode getDropInputMode() const;
1108 void handleDropInputMode(gui::WindowInfo& info) const;
Peiyong Linf243e5e2020-08-22 17:40:59 -07001109
1110 // Find the root of the cloned hierarchy, this means the first non cloned parent.
1111 // This will return null if first non cloned parent is not found.
1112 sp<Layer> getClonedRoot();
1113
1114 // Finds the top most layer in the hierarchy. This will find the root Layer where the parent is
1115 // null.
1116 sp<Layer> getRootLayer();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001117
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01001118 // Fills in the touch occlusion mode of the first parent (including this layer) that
1119 // hasInputInfo() or no-op if no such parent is found.
chaviw98318de2021-05-19 16:45:23 -05001120 void fillTouchOcclusionMode(gui::WindowInfo& info);
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01001121
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001122 // Fills in the frame and transform info for the gui::WindowInfo.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07001123 void fillInputFrameInfo(gui::WindowInfo&, const ui::Transform& screenToDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08001124
Patrick Williamsbb25f802022-08-30 23:02:34 +00001125 inline void tracePendingBufferCount(int32_t pendingBuffers);
1126
1127 // Latch sideband stream and returns true if the dirty region should be updated.
1128 bool latchSidebandStream(bool& recomputeVisibleRegions);
1129
1130 bool hasFrameUpdate() const;
1131
Vishnu Naird47bcee2023-02-24 18:08:51 +00001132 void updateTexImage(nsecs_t latchTime, bool bgColorOnly = false);
Patrick Williamsbb25f802022-08-30 23:02:34 +00001133
1134 // Crop that applies to the buffer
1135 Rect computeBufferCrop(const State& s);
1136
Patrick Williamsbb25f802022-08-30 23:02:34 +00001137 void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
1138 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
1139 const sp<Fence>& releaseFence,
1140 uint32_t currentMaxAcquiredBufferCount);
1141
liulijuneb489f62022-10-17 22:02:14 +08001142 // Returns true if the transformed buffer size does not match the layer size and we need
1143 // to apply filtering.
1144 bool bufferNeedsFiltering() const;
1145
Patrick Williamsbb25f802022-08-30 23:02:34 +00001146 // Returns true if there is a valid color to fill.
1147 bool fillsColor() const;
1148 // Returns true if this layer has a blur value.
1149 bool hasBlur() const;
1150 bool hasEffect() const { return fillsColor() || drawShadows() || hasBlur(); }
1151 bool hasBufferOrSidebandStream() const {
1152 return ((mSidebandStream != nullptr) || (mBufferInfo.mBuffer != nullptr));
1153 }
1154
Vishnu Nair22491b82022-10-18 14:59:14 -07001155 bool hasBufferOrSidebandStreamInDrawing() const {
1156 return ((mDrawingState.sidebandStream != nullptr) || (mDrawingState.buffer != nullptr));
1157 }
1158
Patrick Williamsbb25f802022-08-30 23:02:34 +00001159 bool hasSomethingToDraw() const { return hasEffect() || hasBufferOrSidebandStream(); }
Vishnu Naire14c6b32022-08-06 04:20:15 +00001160
Rachel Lee0faad6c2023-09-08 13:34:34 -07001161 bool shouldOverrideChildrenFrameRate() const {
1162 return getDrawingState().frameRateSelectionStrategy ==
1163 FrameRateSelectionStrategy::OverrideChildren;
1164 }
1165
Rachel Leea021bb02023-11-20 21:51:09 -08001166 bool shouldPropagateFrameRate() const {
Rachel Lee70f7b692023-11-22 11:24:02 -08001167 return getDrawingState().frameRateSelectionStrategy != FrameRateSelectionStrategy::Self;
Rachel Leea021bb02023-11-20 21:51:09 -08001168 }
1169
Vishnu Nair4351ad52019-02-11 14:13:02 -08001170 // Cached properties computed from drawing state
Peiyong Lin74861ad2020-10-14 11:55:33 -07001171 // Effective transform taking into account parent transforms and any parent scaling, which is
1172 // a transform from the current layer coordinate space to display(screen) coordinate space.
Vishnu Nair4351ad52019-02-11 14:13:02 -08001173 ui::Transform mEffectiveTransform;
1174
1175 // Bounds of the layer before any transformation is applied and before it has been cropped
1176 // by its parents.
1177 FloatRect mSourceBounds;
1178
1179 // Bounds of the layer in layer space. This is the mSourceBounds cropped by its layer crop and
1180 // its parent bounds.
1181 FloatRect mBounds;
1182
1183 // Layer bounds in screen space.
1184 FloatRect mScreenBounds;
chaviw606e5cf2019-03-01 10:12:10 -08001185
Robert Carrc0df3122019-04-11 13:18:21 -07001186 bool mGetHandleCalled = false;
chaviw43cb3cb2019-05-31 15:23:41 -07001187
chaviwb4c6e582019-08-16 14:35:07 -07001188 // The current layer is a clone of mClonedFrom. This means that this layer will update it's
1189 // properties based on mClonedFrom. When mClonedFrom latches a new buffer for BufferLayers,
1190 // this layer will update it's buffer. When mClonedFrom updates it's drawing state, children,
1191 // and relatives, this layer will update as well.
1192 wp<Layer> mClonedFrom;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001193
1194 // The inherited shadow radius after taking into account the layer hierarchy. This is the
1195 // final shadow radius for this layer. If a shadow is specified for a layer, then effective
1196 // shadow radius is the set shadow radius, otherwise its the parent's shadow radius.
Vishnu Nairf37dcfb2019-11-16 07:37:09 -08001197 float mEffectiveShadowRadius = 0.f;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001198
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001199 // Game mode for the layer. Set by WindowManagerShell and recorded by SurfaceFlingerStats.
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001200 gui::GameMode mGameMode = gui::GameMode::Unsupported;
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001201
Lucas Dupinc3800b82020-10-02 16:24:48 -07001202 // A list of regions on this layer that should have blurs.
Galia Peychevae0acf382021-04-12 21:22:34 +02001203 const std::vector<BlurRegion> getBlurRegions() const;
Vishnu Nair14d218b2021-07-13 13:57:39 -07001204
1205 bool mIsAtRoot = false;
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -08001206
1207 uint32_t mLayerCreationFlags;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001208
Robert Carr88b85e12022-03-21 15:47:35 -07001209 bool findInHierarchy(const sp<Layer>&);
Tianhao Yao67dd7122022-02-22 17:48:33 +00001210
Dorin Drimuse5374e52023-08-02 17:52:43 +00001211 void releasePreviousBuffer();
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001212 void resetDrawingStateBufferInfo();
Patrick Williamsbb25f802022-08-30 23:02:34 +00001213
Patrick Williamsbb25f802022-08-30 23:02:34 +00001214 // Transform hint provided to the producer. This must be accessed holding
1215 // the mStateLock.
Vishnu Nairb76d99a2023-03-19 18:22:31 -07001216 std::optional<ui::Transform::RotationFlags> mTransformHint = std::nullopt;
Patrick Williamsbb25f802022-08-30 23:02:34 +00001217
Patrick Williamsbb25f802022-08-30 23:02:34 +00001218 ReleaseCallbackId mPreviousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001219 sp<IBinder> mPreviousReleaseBufferEndpoint;
Patrick Williamsbb25f802022-08-30 23:02:34 +00001220
1221 bool mReleasePreviousBuffer = false;
1222
1223 // Stores the last set acquire fence signal time used to populate the callback handle's acquire
1224 // time.
1225 std::variant<nsecs_t, sp<Fence>> mCallbackHandleAcquireTimeOrFence = -1;
1226
Patrick Williamsbb25f802022-08-30 23:02:34 +00001227 const std::string mBlastTransactionName{"BufferTX - " + mName};
1228 // This integer is incremented everytime a buffer arrives at the server for this layer,
1229 // and decremented when a buffer is dropped or latched. When changed the integer is exported
Vishnu Nair2665ca92024-07-09 22:08:15 +00001230 // to systrace with SFTRACE_INT and mBlastTransactionName. This way when debugging perf it is
Patrick Williamsbb25f802022-08-30 23:02:34 +00001231 // possible to see when a buffer arrived at the server, and in which frame it latched.
1232 //
1233 // You can understand the trace this way:
1234 // - If the integer increases, a buffer arrived at the server.
1235 // - If the integer decreases in latchBuffer, that buffer was latched
1236 // - If the integer decreases in setBuffer or doTransaction, a buffer was dropped
1237 std::atomic<int32_t> mPendingBufferTransactions{0};
1238
1239 // Contains requested position and matrix updates. This will be applied if the client does
1240 // not specify a destination frame.
1241 ui::Transform mRequestedTransform;
1242
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001243 sp<LayerFE> mLegacyLayerFE;
1244 std::vector<std::pair<frontend::LayerHierarchy::TraversalPath, sp<LayerFE>>> mLayerFEs;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001245 std::unique_ptr<frontend::LayerSnapshot> mSnapshot =
1246 std::make_unique<frontend::LayerSnapshot>();
Vishnu Nair787aa782023-03-17 13:46:46 -07001247 bool mHandleAlive = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248};
1249
Marin Shalamanov46084422020-10-13 12:33:42 +02001250std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate);
1251
Dominik Laskowski34157762018-10-31 13:07:19 -07001252} // namespace android