blob: c2b0a112d622cea153a9a6f056cceff9866425ba [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Dan Stoza9e56aa02015-11-02 13:00:03 -080021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080024#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Alec Mourie60041e2019-06-14 18:59:51 -070026#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
[1;3C2b9fc252021-02-04 16:16:50 -080028#include <android-base/properties.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Steven Thomas62a4cf82020-01-31 12:04:03 -080030#include <android/native_window.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070031#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/Display.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080033#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/OutputLayer.h>
35#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070039#include <ftl/enum.h>
Dominik Laskowski298b08e2022-02-15 13:45:02 -080040#include <ftl/fake_guard.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080041#include <gui/BufferItem.h>
42#include <gui/LayerDebugInfo.h>
43#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070044#include <math.h>
chaviw250bcbb2020-08-05 11:17:54 -070045#include <private/android_filesystem_config.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080046#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070047#include <stdint.h>
48#include <stdlib.h>
49#include <sys/types.h>
Alec Mouridda07d92022-04-25 22:39:25 +000050#include <system/graphics-base-v1.0.h>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070051#include <ui/DataspaceUtils.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080052#include <ui/DebugUtils.h>
53#include <ui/GraphicBuffer.h>
54#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include <utils/Errors.h>
56#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080057#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080059#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Alec Mourie60041e2019-06-14 18:59:51 -070061#include <algorithm>
62#include <mutex>
63#include <sstream>
64
Yiwei Zhang60d1a192018-03-07 14:52:28 -080065#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020066#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070067#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080068#include "DisplayHardware/HWComposer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080069#include "EffectLayer.h"
Ady Abraham22c7b5c2020-09-22 19:33:40 -070070#include "FrameTimeline.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070071#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080072#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070073#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070074#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080076#include "TimeStats/TimeStats.h"
Robert Carr3e2a2992021-06-11 13:42:55 -070077#include "TunnelModeEnabledReporter.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070078
David Sodman41fdfc92017-11-06 16:09:56 -080079#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010082namespace {
83constexpr int kDumpTableRowLength = 159;
Prabir Pradhan83aca382022-07-22 19:53:04 +000084const ui::Transform kIdentityTransform;
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010085} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086
Dominik Laskowski2f01d772022-03-23 16:01:29 -070087using namespace ftl::flag_operators;
88
Yiwei Zhang5434a782018-12-05 18:06:32 -080089using base::StringAppendF;
chaviw3277faf2021-05-19 16:45:23 -050090using gui::WindowInfo;
Yiwei Zhang5434a782018-12-05 18:06:32 -080091
Dominik Laskowski2f01d772022-03-23 16:01:29 -070092using PresentState = frametimeline::SurfaceFrame::PresentState;
93
Lloyd Piquef1c675b2018-09-12 20:45:39 -070094std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080095
Lloyd Pique42ab75e2018-09-12 20:46:03 -070096Layer::Layer(const LayerCreationArgs& args)
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -080097 : sequence(args.sequence.value_or(sSequence++)),
98 mFlinger(args.flinger),
Arthur Hung23e07502021-10-15 11:58:19 +000099 mName(base::StringPrintf("%s#%d", args.name.c_str(), sequence)),
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700100 mClientRef(args.client),
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800101 mWindowType(static_cast<WindowInfo::Type>(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0))),
102 mLayerCreationFlags(args.flags) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700103 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700104 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
105 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
106 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -0700107 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
108 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Vishnu Naira84a2202022-01-12 20:12:55 -0800109 if (args.sequence) {
110 sSequence = *args.sequence + 1;
111 }
Robert Carr6a160312021-05-17 12:08:20 -0700112 mDrawingState.flags = layerFlags;
113 mDrawingState.active_legacy.transform.set(0, 0);
114 mDrawingState.crop.makeInvalid();
115 mDrawingState.requestedCrop = mDrawingState.crop;
116 mDrawingState.z = 0;
117 mDrawingState.color.a = 1.0f;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700118 mDrawingState.layerStack = ui::DEFAULT_LAYER_STACK;
Robert Carr6a160312021-05-17 12:08:20 -0700119 mDrawingState.sequence = 0;
120 mDrawingState.requested_legacy = mDrawingState.active_legacy;
121 mDrawingState.width = UINT32_MAX;
122 mDrawingState.height = UINT32_MAX;
123 mDrawingState.transform.set(0, 0);
124 mDrawingState.frameNumber = 0;
125 mDrawingState.bufferTransform = 0;
126 mDrawingState.transformToDisplayInverse = false;
127 mDrawingState.crop.makeInvalid();
128 mDrawingState.acquireFence = sp<Fence>::make(-1);
129 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
130 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
131 mDrawingState.hdrMetadata.validTypes = 0;
132 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
133 mDrawingState.cornerRadius = 0.0f;
134 mDrawingState.backgroundBlurRadius = 0;
135 mDrawingState.api = -1;
136 mDrawingState.hasColorTransform = false;
137 mDrawingState.colorSpaceAgnostic = false;
138 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
139 mDrawingState.metadata = args.metadata;
140 mDrawingState.shadowRadius = 0.f;
141 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
142 mDrawingState.frameTimelineInfo = {};
143 mDrawingState.postTime = -1;
144 mDrawingState.destinationFrame.makeInvalid();
Robin Leeedb375d2021-09-10 12:03:42 +0000145 mDrawingState.isTrustedOverlay = false;
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -0700146 mDrawingState.dropInputMode = gui::DropInputMode::NONE;
Sally Qi81d95e62022-03-21 19:41:33 -0700147 mDrawingState.dimmingEnabled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700148
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700149 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
150 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700151 mDrawingState.color.r = -1.0_hf;
152 mDrawingState.color.g = -1.0_hf;
153 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700154 }
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800155 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700156 args.flinger->getCompositorTiming(&compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200157 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700158
Vishnu Nair0f085c62019-08-30 08:49:12 -0700159 mCallingPid = args.callingPid;
160 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700161
162 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
163 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
164 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700165 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700166 } else {
167 // A create layer request from a non system request cannot specify the owner uid
168 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700169 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700170 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800171}
172
173void Layer::onFirstRef() {
174 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700175}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700176
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700177Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800178 sp<Client> c(mClientRef.promote());
179 if (c != 0) {
180 c->detachLayer(this);
181 }
182
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000183 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700184 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700185
186 if (mDrawingState.sidebandStream != nullptr) {
187 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
188 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700189 if (mHadClonedChild) {
190 mFlinger->mNumClones--;
191 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700192}
193
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700194LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800195 uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700196 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700197 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700198 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700199 flags(flags),
200 metadata(std::move(metadata)) {
201 IPCThreadState* ipc = IPCThreadState::self();
202 callingPid = ipc->getCallingPid();
203 callingUid = ipc->getCallingUid();
204}
205
Mathias Agopian13127d82013-03-05 17:47:11 -0800206// ---------------------------------------------------------------------------
207// callbacks
208// ---------------------------------------------------------------------------
209
David Sodmaneb085e02017-10-05 18:49:04 -0700210/*
211 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
212 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800213 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700214 */
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700215void Layer::onLayerDisplayed(ftl::SharedFuture<FenceResult>) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800216
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700217void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700218 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700219 return;
220 }
Robert Carr2e102c92018-10-23 12:11:15 -0700221
Robert Carr6a160312021-05-17 12:08:20 -0700222 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700223 if (strongRelative == nullptr) {
224 setZOrderRelativeOf(nullptr);
225 return;
226 }
227
228 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
229 strongRelative->removeZOrderRelative(this);
230 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800231 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700232 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700233}
234
235void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700236 if (!mRemovedFromDrawingState) {
237 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700238 mFlinger->mScheduler->deregisterLayer(this);
239 }
Rob Carr4bba3702018-10-08 21:53:30 +0000240
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800241 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700242}
Chia-I Wu38512252017-05-17 14:36:16 -0700243
chaviw68d4dab2020-06-08 15:07:32 -0700244sp<Layer> Layer::getRootLayer() {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000245 sp<Layer> parent = getParent();
chaviw68d4dab2020-06-08 15:07:32 -0700246 if (parent == nullptr) {
247 return this;
248 }
249 return parent->getRootLayer();
250}
251
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700252void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700253 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
254 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700255 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700256
257 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700258 layer->removeFromCurrentState();
259 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700260 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700261}
262
chaviw61626f22018-11-15 16:26:27 -0800263void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700264 if (mRemovedFromDrawingState) {
265 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700266 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700267 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700268 }
chaviw61626f22018-11-15 16:26:27 -0800269
270 for (const auto& child : mCurrentChildren) {
271 child->addToCurrentState();
272 }
273}
274
Mathias Agopian13127d82013-03-05 17:47:11 -0800275// ---------------------------------------------------------------------------
276// set-up
277// ---------------------------------------------------------------------------
278
chaviw13fdc492017-06-27 12:40:18 -0700279bool Layer::getPremultipledAlpha() const {
280 return mPremultipliedAlpha;
281}
282
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700283sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800284 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700285 if (mGetHandleCalled) {
286 ALOGE("Get handle called twice" );
287 return nullptr;
288 }
289 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700290 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800291}
292
293// ---------------------------------------------------------------------------
294// h/w composer set-up
295// ---------------------------------------------------------------------------
296
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700297static Rect reduce(const Rect& win, const Region& exclude) {
298 if (CC_LIKELY(exclude.isEmpty())) {
299 return win;
300 }
301 if (exclude.isRect()) {
302 return win.reduce(exclude.getBounds());
303 }
304 return Region(win).subtract(exclude).getBounds();
305}
306
Dan Stoza80d61162017-12-20 15:57:52 -0800307static FloatRect reduce(const FloatRect& win, const Region& exclude) {
308 if (CC_LIKELY(exclude.isEmpty())) {
309 return win;
310 }
311 // Convert through Rect (by rounding) for lack of FloatRegion
312 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
313}
314
Vishnu Nair4351ad52019-02-11 14:13:02 -0800315Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800316 if (!reduceTransparentRegion) {
317 return Rect{mScreenBounds};
318 }
319
320 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800321 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800322 // Transform to screen space.
323 bounds = t.transform(bounds);
324 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700325}
326
Vishnu Nair4351ad52019-02-11 14:13:02 -0800327FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000328 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800329 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700330}
331
Vishnu Nairf0c28512019-02-08 12:40:28 -0800332FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
333 // Subtract the transparent region and snap to the bounds.
334 return reduce(mBounds, activeTransparentRegion);
335}
336
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700337void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
338 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800339 const State& s(getDrawingState());
340
341 // Calculate effective layer transform
342 mEffectiveTransform = parentTransform * getActiveTransform(s);
343
Garfield Tan2c1782c2022-02-16 15:25:05 -0800344 if (CC_UNLIKELY(!isTransformValid())) {
345 ALOGW("Stop computing bounds for %s because it has invalid transformation.",
346 getDebugName());
347 return;
348 }
349
Vishnu Nair4351ad52019-02-11 14:13:02 -0800350 // Transform parent bounds to layer space
351 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
352
Vishnu Nairc652ff82019-03-15 12:48:54 -0700353 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800354 mSourceBounds = computeSourceBounds(parentBounds);
355
356 // Calculate bounds by croping diplay frame with layer crop and parent bounds
357 FloatRect bounds = mSourceBounds;
358 const Rect layerCrop = getCrop(s);
359 if (!layerCrop.isEmpty()) {
360 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
361 }
362 bounds = bounds.intersect(parentBounds);
363
364 mBounds = bounds;
365 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700366
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700367 // Use the layer's own shadow radius if set. Otherwise get the radius from
368 // parent.
369 if (s.shadowRadius > 0.f) {
370 mEffectiveShadowRadius = s.shadowRadius;
371 } else {
372 mEffectiveShadowRadius = parentShadowRadius;
373 }
374
375 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
376 // don't pass it to its children.
377 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
378
Vishnu Nair4351ad52019-02-11 14:13:02 -0800379 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700380 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800381 }
382}
383
Vishnu Nair60356342018-11-13 13:00:45 -0800384Rect Layer::getCroppedBufferSize(const State& s) const {
385 Rect size = getBufferSize(s);
386 Rect crop = getCrop(s);
387 if (!crop.isEmpty() && size.isValid()) {
388 size.intersect(crop, &size);
389 } else if (!crop.isEmpty()) {
390 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700391 }
Vishnu Nair60356342018-11-13 13:00:45 -0800392 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800393}
394
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700395void Layer::setupRoundedCornersCropCoordinates(Rect win,
396 const FloatRect& roundedCornersCrop) const {
397 // Translate win by the rounded corners rect coordinates, to have all values in
398 // layer coordinate space.
399 win.left -= roundedCornersCrop.left;
400 win.right -= roundedCornersCrop.left;
401 win.top -= roundedCornersCrop.top;
402 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700403}
404
Lloyd Piquede196652020-01-22 17:29:58 -0800405void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800406 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800407 const auto alpha = static_cast<float>(getAlpha());
408 const bool opaque = isOpaque(drawingState);
Vishnu Nairadb70392022-07-11 15:04:07 -0700409 const bool usesRoundedCorners = hasRoundedCorners();
Lloyd Piquec6687342019-03-07 21:34:57 -0800410
411 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
412 if (!opaque || alpha != 1.0f) {
413 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
414 : Hwc2::IComposerClient::BlendMode::COVERAGE;
415 }
416
Lloyd Piquede196652020-01-22 17:29:58 -0800417 auto* compositionState = editCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700418 compositionState->outputFilter = getOutputFilter();
Lloyd Piquede196652020-01-22 17:29:58 -0800419 compositionState->isVisible = isVisible();
420 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
421 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800422
Lloyd Piquede196652020-01-22 17:29:58 -0800423 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800424 contentDirty = false;
425
Lloyd Piquede196652020-01-22 17:29:58 -0800426 compositionState->geomLayerBounds = mBounds;
427 compositionState->geomLayerTransform = getTransform();
428 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
429 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800430
Lloyd Piquede196652020-01-22 17:29:58 -0800431 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
432 compositionState->alpha = alpha;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400433 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
434 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500435 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800436}
437
Lloyd Piquede196652020-01-22 17:29:58 -0800438void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800439 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800440 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700441
Lloyd Piquede196652020-01-22 17:29:58 -0800442 compositionState->geomBufferSize = getBufferSize(drawingState);
443 compositionState->geomContentCrop = getBufferCrop();
444 compositionState->geomCrop = getCrop(drawingState);
445 compositionState->geomBufferTransform = getBufferTransform();
446 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
447 compositionState->geomUsesSourceCrop = usesSourceCrop();
448 compositionState->isSecure = isSecure();
449
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800450 compositionState->metadata.clear();
451 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
452 for (const auto& [key, mandatory] : supportedMetadata) {
453 const auto& genericLayerMetadataCompatibilityMap =
454 mFlinger->getGenericLayerMetadataKeyMap();
455 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
456 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
457 continue;
458 }
459 const uint32_t id = compatIter->second;
460
461 auto it = drawingState.metadata.mMap.find(id);
462 if (it == std::end(drawingState.metadata.mMap)) {
463 continue;
464 }
465
466 compositionState->metadata
467 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
468 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800469}
David Sodmanba340492018-08-05 21:51:33 -0700470
Lloyd Piquede196652020-01-22 17:29:58 -0800471void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800472 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800473 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800474
Lloyd Piquede196652020-01-22 17:29:58 -0800475 compositionState->forceClientComposition = false;
476
477 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
478 compositionState->dataspace = getDataSpace();
479 compositionState->colorTransform = getColorTransform();
480 compositionState->colorTransformIsIdentity = !hasColorTransform();
481 compositionState->surfaceDamage = surfaceDamageRegion;
482 compositionState->hasProtectedContent = isProtected();
Sally Qi81d95e62022-03-21 19:41:33 -0700483 compositionState->dimmingEnabled = isDimmingEnabled();
Lloyd Pique688abd42019-02-15 15:42:24 -0800484
Vishnu Nairadb70392022-07-11 15:04:07 -0700485 const bool usesRoundedCorners = hasRoundedCorners();
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700486
Lloyd Piquede196652020-01-22 17:29:58 -0800487 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800488 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800489
490 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400491 // Rounded corners no longer force client composition, since we may use a
492 // hole punch so that the layer will appear to have rounded corners.
493 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500494 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800495 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800496 }
Vishnu Nairb801a982021-11-02 15:12:08 -0700497 // If there are no visible region changes, we still need to update blur parameters.
498 compositionState->blurRegions = drawingState.blurRegions;
499 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400500
501 // Layer framerate is used in caching decisions.
502 // Retrieve it from the scheduler which maintains an instance of LayerHistory, and store it in
503 // LayerFECompositionState where it would be visible to Flattener.
504 compositionState->fps = mFlinger->getLayerFramerate(systemTime(), getSequence());
Lloyd Piquef5275482019-01-29 18:42:42 -0800505}
506
Lloyd Piquede196652020-01-22 17:29:58 -0800507void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800508 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800509 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800510
511 // Apply the layer's transform, followed by the display's global transform
512 // Here we're guaranteed that the layer's transform preserves rects
513 Rect win = getCroppedBufferSize(drawingState);
514 // Subtract the transparent region and snap to the bounds
515 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
516 Rect frame(getTransform().transform(bounds));
517
Lloyd Piquede196652020-01-22 17:29:58 -0800518 compositionState->cursorFrame = frame;
519}
520
521sp<compositionengine::LayerFE> Layer::asLayerFE() const {
522 return const_cast<compositionengine::LayerFE*>(
523 static_cast<const compositionengine::LayerFE*>(this));
524}
525
526sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
527 return nullptr;
528}
529
530compositionengine::LayerFECompositionState* Layer::editCompositionState() {
531 return nullptr;
532}
533
534const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
535 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800536}
537
Lloyd Piquef16688f2019-02-19 17:47:57 -0800538bool Layer::onPreComposition(nsecs_t) {
539 return false;
540}
541
Lloyd Piquede196652020-01-22 17:29:58 -0800542void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800543 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800544
Lloyd Piquec6687342019-03-07 21:34:57 -0800545 switch (subset) {
546 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800547 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800548 break;
549
550 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800551 prepareBasicGeometryCompositionState();
552 prepareGeometryCompositionState();
553 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800554 break;
555
556 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800557 preparePerFrameCompositionState();
558 break;
559
560 case StateSubset::Cursor:
561 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800562 break;
563 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800564}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700565
Lloyd Piquea83776c2019-01-29 18:42:32 -0800566const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700567 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800568}
569
Mathias Agopian13127d82013-03-05 17:47:11 -0800570// ---------------------------------------------------------------------------
571// drawing...
572// ---------------------------------------------------------------------------
573
Vishnu Nair9b079a22020-01-21 14:36:08 -0800574std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100575 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800576 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800577 return {};
578 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800579
Vishnu Nair4351ad52019-02-11 14:13:02 -0800580 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000581 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800582
583 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800584 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700585 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000586
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400587 // skip drawing content if the targetSettings indicate the content will be occluded
Ady Abrahambaa6cf52021-07-13 14:02:19 -0700588 const bool drawContent = targetSettings.realContentIsVisible || targetSettings.clearContent;
589 layerSettings.skipContentDraw = !drawContent;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400590
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000591 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800592 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000593 }
594
595 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800596 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
597 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000598
Lloyd Piquef16688f2019-02-19 17:47:57 -0800599 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700600 layerSettings.sourceDataspace = getDataSpace();
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700601
Alec Mouridda07d92022-04-25 22:39:25 +0000602 // Override the dataspace transfer from 170M to sRGB if the device configuration requests this.
603 // We do this here instead of in buffer info so that dumpsys can still report layers that are
604 // using the 170M transfer.
605 if (mFlinger->mTreat170mAsSrgb &&
606 (layerSettings.sourceDataspace & HAL_DATASPACE_TRANSFER_MASK) ==
607 HAL_DATASPACE_TRANSFER_SMPTE_170M) {
608 layerSettings.sourceDataspace = static_cast<ui::Dataspace>(
609 (layerSettings.sourceDataspace & HAL_DATASPACE_STANDARD_MASK) |
610 (layerSettings.sourceDataspace & HAL_DATASPACE_RANGE_MASK) |
611 HAL_DATASPACE_TRANSFER_SRGB);
612 }
613
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700614 layerSettings.whitePointNits = targetSettings.whitePointNits;
Alec Mourif54453c2021-05-13 16:28:28 -0700615 switch (targetSettings.blurSetting) {
616 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Enabled:
617 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
618 layerSettings.blurRegions = getBlurRegions();
619 layerSettings.blurRegionTransform =
620 getActiveTransform(getDrawingState()).inverse().asMatrix4();
621 break;
622 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BackgroundBlurOnly:
623 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
624 break;
625 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BlurRegionsOnly:
626 layerSettings.blurRegions = getBlurRegions();
627 layerSettings.blurRegionTransform =
628 getActiveTransform(getDrawingState()).inverse().asMatrix4();
629 break;
630 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled:
631 default:
632 break;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100633 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700634 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800635 // Record the name of the layer for debugging further down the stack.
636 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800637 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800638}
639
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800640void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
641 bool blackout) const {
642 layerSettings.source.buffer.buffer = nullptr;
643 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
644 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800645 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800646 layerSettings.frameNumber = 0;
647
648 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
649 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800650 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800651}
652
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400653// TODO(b/188891810): This method now only ever returns 0 or 1 layers so we should return
654// std::optional instead of a vector. Additionally, we should consider removing
655// this method entirely in favor of calling prepareClientComposition directly.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800656std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
657 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
658 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
659 prepareClientComposition(targetSettings);
660 // Nothing to render.
661 if (!layerSettings) {
662 return {};
663 }
664
665 // HWC requests to clear this layer.
666 if (targetSettings.clearContent) {
667 prepareClearClientComposition(*layerSettings, false /* blackout */);
668 return {*layerSettings};
669 }
670
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400671 // set the shadow for the layer if needed
672 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800673
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400674 return {*layerSettings};
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800675}
676
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500677aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType(
678 const DisplayDevice& display) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700679 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800680 if (outputLayer == nullptr) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500681 return aidl::android::hardware::graphics::composer3::Composition::INVALID;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800682 }
683 if (outputLayer->getState().hwc) {
684 return (*outputLayer->getState().hwc).hwcCompositionType;
685 } else {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500686 return aidl::android::hardware::graphics::composer3::Composition::CLIENT;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800687 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688}
689
Mathias Agopian13127d82013-03-05 17:47:11 -0800690// ----------------------------------------------------------------------------
691// local state
692// ----------------------------------------------------------------------------
693
David Sodman41fdfc92017-11-06 16:09:56 -0800694bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800695 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700696 if (s.flags & layer_state_t::eLayerSecure) {
697 return true;
698 }
699
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000700 const auto p = mDrawingParent.promote();
Garfield Tande619fa2020-10-02 17:13:53 -0700701 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700702}
703
Mathias Agopian13127d82013-03-05 17:47:11 -0800704// ----------------------------------------------------------------------------
705// transaction
706// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800707
Marissa Wall61c58622018-07-18 10:12:20 -0700708uint32_t Layer::doTransaction(uint32_t flags) {
709 ATRACE_CALL();
710
Robert Carr0758e5d2021-03-11 22:15:04 -0800711 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700712 mDrawingStateModified = mDrawingState.modified;
713 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700714
Alec Mourib416efd2018-09-06 21:01:59 +0000715 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700716
Robert Carr6a160312021-05-17 12:08:20 -0700717 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800718 // invalidate and recompute the visible regions if needed
719 flags |= Layer::eVisibleRegion;
720 }
721
Robert Carr6a160312021-05-17 12:08:20 -0700722 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800723 // invalidate and recompute the visible regions if needed
Robert Carr6a160312021-05-17 12:08:20 -0700724 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800725 flags |= eVisibleRegion;
726 this->contentDirty = true;
727
728 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700729 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800730 }
731
Robert Carr6a160312021-05-17 12:08:20 -0700732 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800733
Mathias Agopian13127d82013-03-05 17:47:11 -0800734 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735}
736
Robert Carr6a160312021-05-17 12:08:20 -0700737void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000738 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
739 // bufferSurfaceFrameTX will be presented in latchBuffer.
740 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
741 if (surfaceFrame->getPresentState() != PresentState::Presented) {
742 // With applyPendingStates, we could end up having presented surfaceframes from previous
743 // states
744 surfaceFrame->setPresentState(PresentState::Presented);
745 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
746 }
747 }
Robert Carr6a160312021-05-17 12:08:20 -0700748 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700749}
750
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700751uint32_t Layer::clearTransactionFlags(uint32_t mask) {
752 const auto flags = mTransactionFlags & mask;
753 mTransactionFlags &= ~mask;
754 return flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800755}
756
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700757void Layer::setTransactionFlags(uint32_t mask) {
758 mTransactionFlags |= mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800759}
760
chaviw214c89d2019-09-04 16:03:53 -0700761bool Layer::setPosition(float x, float y) {
Robert Carr6a160312021-05-17 12:08:20 -0700762 if (mDrawingState.transform.tx() == x && mDrawingState.transform.ty() == y) return false;
763 mDrawingState.sequence++;
764 mDrawingState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700765
Robert Carr6a160312021-05-17 12:08:20 -0700766 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800767 setTransactionFlags(eTransactionNeeded);
768 return true;
769}
Robert Carr82364e32016-05-15 11:27:47 -0700770
Robert Carr1f0a16a2016-10-24 16:27:39 -0700771bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
772 ssize_t idx = mCurrentChildren.indexOf(childLayer);
773 if (idx < 0) {
774 return false;
775 }
776 if (childLayer->setLayer(z)) {
777 mCurrentChildren.removeAt(idx);
778 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800779 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700780 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800781 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700782}
783
Robert Carr503c7042017-09-27 15:06:08 -0700784bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
785 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
786 ssize_t idx = mCurrentChildren.indexOf(childLayer);
787 if (idx < 0) {
788 return false;
789 }
790 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
791 mCurrentChildren.removeAt(idx);
792 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800793 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700794 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800795 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700796}
797
Robert Carrae060832016-11-28 10:51:00 -0800798bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700799 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
800 mDrawingState.sequence++;
801 mDrawingState.z = z;
802 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700803
Robert Carra70e91c2021-06-11 13:59:52 -0700804 mFlinger->mSomeChildrenChanged = true;
805
Robert Carrdb66e622017-04-10 16:55:57 -0700806 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700807 if (mDrawingState.zOrderRelativeOf != nullptr) {
808 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700809 if (strongRelative != nullptr) {
810 strongRelative->removeZOrderRelative(this);
811 }
chaviw606e5cf2019-03-01 10:12:10 -0800812 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700813 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800814 setTransactionFlags(eTransactionNeeded);
815 return true;
816}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700817
Robert Carrdb66e622017-04-10 16:55:57 -0700818void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700819 mDrawingState.zOrderRelatives.remove(relative);
820 mDrawingState.sequence++;
821 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700822 setTransactionFlags(eTransactionNeeded);
823}
824
825void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700826 mDrawingState.zOrderRelatives.add(relative);
827 mDrawingState.modified = true;
828 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700829 setTransactionFlags(eTransactionNeeded);
830}
831
chaviw606e5cf2019-03-01 10:12:10 -0800832void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700833 mDrawingState.zOrderRelativeOf = relativeOf;
834 mDrawingState.sequence++;
835 mDrawingState.modified = true;
836 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700837
chaviw606e5cf2019-03-01 10:12:10 -0800838 setTransactionFlags(eTransactionNeeded);
839}
840
Robert Carr503d2bd2017-12-04 15:49:47 -0800841bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nairf9096652021-07-20 18:49:42 -0700842 sp<Layer> relative = fromHandle(relativeToHandle).promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700843 if (relative == nullptr) {
844 return false;
845 }
846
Robert Carr6a160312021-05-17 12:08:20 -0700847 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
848 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800849 return false;
850 }
851
Robert Carr88b85e12022-03-21 15:47:35 -0700852 if (CC_UNLIKELY(relative->usingRelativeZ(LayerVector::StateSet::Drawing)) &&
853 (relative->mDrawingState.zOrderRelativeOf == this)) {
854 ALOGE("Detected relative layer loop between %s and %s",
855 mName.c_str(), relative->mName.c_str());
856 ALOGE("Ignoring new call to set relative layer");
857 return false;
858 }
859
Robert Carra70e91c2021-06-11 13:59:52 -0700860 mFlinger->mSomeChildrenChanged = true;
861
Robert Carr6a160312021-05-17 12:08:20 -0700862 mDrawingState.sequence++;
863 mDrawingState.modified = true;
864 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700865
Robert Carr6a160312021-05-17 12:08:20 -0700866 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700867 if (oldZOrderRelativeOf != nullptr) {
868 oldZOrderRelativeOf->removeZOrderRelative(this);
869 }
chaviw606e5cf2019-03-01 10:12:10 -0800870 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700871 relative->addZOrderRelative(this);
872
873 setTransactionFlags(eTransactionNeeded);
874
875 return true;
876}
877
Winson Chunga30f7c92021-06-29 15:42:56 -0700878bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
879 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
880 mDrawingState.isTrustedOverlay = isTrustedOverlay;
881 mDrawingState.modified = true;
882 mFlinger->mInputInfoChanged = true;
883 setTransactionFlags(eTransactionNeeded);
884 return true;
885}
886
887bool Layer::isTrustedOverlay() const {
888 if (getDrawingState().isTrustedOverlay) {
889 return true;
890 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000891 const auto& p = mDrawingParent.promote();
Winson Chunga30f7c92021-06-29 15:42:56 -0700892 return (p != nullptr) && p->isTrustedOverlay();
893}
894
Mathias Agopian13127d82013-03-05 17:47:11 -0800895bool Layer::setSize(uint32_t w, uint32_t h) {
Robert Carr6a160312021-05-17 12:08:20 -0700896 if (mDrawingState.requested_legacy.w == w && mDrawingState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700897 return false;
Robert Carr6a160312021-05-17 12:08:20 -0700898 mDrawingState.requested_legacy.w = w;
899 mDrawingState.requested_legacy.h = h;
900 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800901 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700902
903 // record the new size, from this point on, when the client request
904 // a buffer, it'll get the new size.
Robert Carr6a160312021-05-17 12:08:20 -0700905 setDefaultBufferSize(mDrawingState.requested_legacy.w, mDrawingState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800906 return true;
907}
chaviw4e765532021-04-30 12:11:39 -0500908
Dan Stoza9e56aa02015-11-02 13:00:03 -0800909bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700910 if (mDrawingState.color.a == alpha) return false;
911 mDrawingState.sequence++;
912 mDrawingState.color.a = alpha;
913 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800914 setTransactionFlags(eTransactionNeeded);
915 return true;
916}
chaviw13fdc492017-06-27 12:40:18 -0700917
Valerie Haudd0b7572019-01-29 14:59:27 -0800918bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700919 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700920 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800921 }
Robert Carr6a160312021-05-17 12:08:20 -0700922 mDrawingState.sequence++;
923 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800924 setTransactionFlags(eTransactionNeeded);
925
Robert Carr6a160312021-05-17 12:08:20 -0700926 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800927 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800928 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700929 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700930 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800931 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700932 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700933
Valerie Haudd0b7572019-01-29 14:59:27 -0800934 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700935 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800936 mFlinger->mLayersAdded = true;
937 // set up SF to handle added color layer
938 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700939 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800940 }
941 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700942 } else if (mDrawingState.bgColorLayer && alpha == 0) {
943 mDrawingState.bgColorLayer->reparent(nullptr);
944 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800945 return true;
946 }
947
Robert Carr6a160312021-05-17 12:08:20 -0700948 mDrawingState.bgColorLayer->setColor(color);
949 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
950 mDrawingState.bgColorLayer->setAlpha(alpha);
951 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800952
chaviw13fdc492017-06-27 12:40:18 -0700953 return true;
954}
955
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700956bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700957 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700958
Robert Carr6a160312021-05-17 12:08:20 -0700959 mDrawingState.sequence++;
960 mDrawingState.cornerRadius = cornerRadius;
961 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700962 setTransactionFlags(eTransactionNeeded);
963 return true;
964}
965
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800966bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700967 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Vishnu Nairb801a982021-11-02 15:12:08 -0700968 // If we start or stop drawing blur then the layer's visibility state may change so increment
969 // the magic sequence number.
970 if (mDrawingState.backgroundBlurRadius == 0 || backgroundBlurRadius == 0) {
971 mDrawingState.sequence++;
972 }
Robert Carr6a160312021-05-17 12:08:20 -0700973 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
974 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800975 setTransactionFlags(eTransactionNeeded);
976 return true;
977}
Robert Carrde6d7b42022-01-07 18:23:06 -0800978bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700979 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -0700980 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
981
Robert Carr6a160312021-05-17 12:08:20 -0700982 mDrawingState.sequence++;
983 mDrawingState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
984 mDrawingState.modified = true;
985
Mathias Agopian13127d82013-03-05 17:47:11 -0800986 setTransactionFlags(eTransactionNeeded);
987 return true;
988}
Marissa Wall61c58622018-07-18 10:12:20 -0700989
Mathias Agopian13127d82013-03-05 17:47:11 -0800990bool Layer::setTransparentRegionHint(const Region& transparent) {
Robert Carr6a160312021-05-17 12:08:20 -0700991 mDrawingState.requestedTransparentRegion_legacy = transparent;
992 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800993 setTransactionFlags(eTransactionNeeded);
994 return true;
995}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100996
Lucas Dupinc3800b82020-10-02 16:24:48 -0700997bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Vishnu Nairb801a982021-11-02 15:12:08 -0700998 // If we start or stop drawing blur then the layer's visibility state may change so increment
999 // the magic sequence number.
1000 if (mDrawingState.blurRegions.size() == 0 || blurRegions.size() == 0) {
1001 mDrawingState.sequence++;
1002 }
Robert Carr6a160312021-05-17 12:08:20 -07001003 mDrawingState.blurRegions = blurRegions;
1004 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001005 setTransactionFlags(eTransactionNeeded);
1006 return true;
1007}
1008
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001009bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -07001010 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
1011 if (mDrawingState.flags == newFlags) return false;
1012 mDrawingState.sequence++;
1013 mDrawingState.flags = newFlags;
1014 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001015 setTransactionFlags(eTransactionNeeded);
1016 return true;
1017}
Robert Carr99e27f02016-06-16 15:18:02 -07001018
chaviw25714502021-02-11 10:01:08 -08001019bool Layer::setCrop(const Rect& crop) {
Robert Carr6a160312021-05-17 12:08:20 -07001020 if (mDrawingState.requestedCrop == crop) return false;
1021 mDrawingState.sequence++;
1022 mDrawingState.requestedCrop = crop;
1023 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001024
Robert Carr6a160312021-05-17 12:08:20 -07001025 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001026 setTransactionFlags(eTransactionNeeded);
1027 return true;
1028}
Robert Carr8d5227b2017-03-16 15:41:03 -07001029
Evan Roskyef876c92019-01-25 17:46:06 -08001030bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -07001031 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
1032 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001033 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001034 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001035}
1036
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001037bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -07001038 if (mDrawingState.layerStack == layerStack) return false;
1039 mDrawingState.sequence++;
1040 mDrawingState.layerStack = layerStack;
1041 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001042 setTransactionFlags(eTransactionNeeded);
1043 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001044}
1045
Peiyong Linc502cb72019-03-01 15:00:23 -08001046bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -07001047 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -08001048 return false;
1049 }
Robert Carr6a160312021-05-17 12:08:20 -07001050 mDrawingState.sequence++;
1051 mDrawingState.colorSpaceAgnostic = agnostic;
1052 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -08001053 setTransactionFlags(eTransactionNeeded);
1054 return true;
1055}
1056
Sally Qi81d95e62022-03-21 19:41:33 -07001057bool Layer::setDimmingEnabled(const bool dimmingEnabled) {
1058 if (mDrawingState.dimmingEnabled == dimmingEnabled) return false;
1059
1060 mDrawingState.sequence++;
1061 mDrawingState.dimmingEnabled = dimmingEnabled;
1062 mDrawingState.modified = true;
1063 setTransactionFlags(eTransactionNeeded);
1064 return true;
1065}
1066
Ana Krulecc84d09b2019-11-02 23:10:29 +01001067bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -07001068 if (mDrawingState.frameRateSelectionPriority == priority) return false;
1069 mDrawingState.frameRateSelectionPriority = priority;
1070 mDrawingState.sequence++;
1071 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +01001072 setTransactionFlags(eTransactionNeeded);
1073 return true;
1074}
1075
1076int32_t Layer::getFrameRateSelectionPriority() const {
1077 // Check if layer has priority set.
1078 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1079 return mDrawingState.frameRateSelectionPriority;
1080 }
1081 // If not, search whether its parents have it set.
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001082 sp<Layer> parent = getParent();
Ana Krulecc84d09b2019-11-02 23:10:29 +01001083 if (parent != nullptr) {
1084 return parent->getFrameRateSelectionPriority();
1085 }
1086
1087 return Layer::PRIORITY_UNSET;
1088}
1089
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001090bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1091 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1092};
1093
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001094ui::LayerStack Layer::getLayerStack() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001095 if (const auto parent = mDrawingParent.promote()) {
1096 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001097 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001098 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001099}
1100
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001101bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001102 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001103 return false;
1104 }
1105
Robert Carr6a160312021-05-17 12:08:20 -07001106 mDrawingState.sequence++;
1107 mDrawingState.shadowRadius = shadowRadius;
1108 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001109 setTransactionFlags(eTransactionNeeded);
1110 return true;
1111}
1112
Vishnu Nair6213bd92020-05-08 17:42:25 -07001113bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001114 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001115 return false;
1116 }
1117
Robert Carr6a160312021-05-17 12:08:20 -07001118 mDrawingState.sequence++;
1119 mDrawingState.fixedTransformHint = fixedTransformHint;
1120 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001121 setTransactionFlags(eTransactionNeeded);
1122 return true;
1123}
1124
John Reckcdb4ed72021-02-04 13:39:33 -05001125bool Layer::setStretchEffect(const StretchEffect& effect) {
1126 StretchEffect temp = effect;
1127 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001128 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001129 return false;
1130 }
Robert Carr6a160312021-05-17 12:08:20 -07001131 mDrawingState.sequence++;
1132 mDrawingState.stretchEffect = temp;
1133 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001134 setTransactionFlags(eTransactionNeeded);
1135 return true;
1136}
1137
John Reckc00c6692021-02-16 11:37:33 -05001138StretchEffect Layer::getStretchEffect() const {
1139 if (mDrawingState.stretchEffect.hasEffect()) {
1140 return mDrawingState.stretchEffect;
1141 }
1142
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001143 sp<Layer> parent = getParent();
John Reckc00c6692021-02-16 11:37:33 -05001144 if (parent != nullptr) {
1145 auto effect = parent->getStretchEffect();
1146 if (effect.hasEffect()) {
1147 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1148 return effect;
1149 }
1150 }
1151 return StretchEffect{};
1152}
1153
Ady Abrahama850c182021-08-04 13:04:37 -07001154bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1155 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1156 const auto frameRate = [&] {
1157 if (mDrawingState.frameRate.rate.isValid() ||
1158 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1159 return mDrawingState.frameRate;
1160 }
1161
1162 return parentFrameRate;
1163 }();
1164
1165 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1166
1167 // The frame rate is propagated to the children
1168 bool childrenHaveFrameRate = false;
1169 for (const sp<Layer>& child : mCurrentChildren) {
1170 childrenHaveFrameRate |=
1171 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1172 }
1173
1174 // If we don't have a valid frame rate, but the children do, we set this
1175 // layer as NoVote to allow the children to control the refresh rate
1176 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1177 childrenHaveFrameRate) {
1178 *transactionNeeded |=
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001179 setFrameRateForLayerTree(FrameRate(Fps(), FrameRateCompatibility::NoVote));
Ady Abrahama850c182021-08-04 13:04:37 -07001180 }
1181
1182 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1183 // the same reason we are allowing touch boost for those layers. See
1184 // RefreshRateConfigs::getBestRefreshRate for more details.
1185 const auto layerVotedWithDefaultCompatibility =
1186 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1187 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1188 const auto layerVotedWithExactCompatibility =
1189 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1190 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1191 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1192}
1193
Ady Abraham60e42ea2020-03-09 19:17:31 -07001194void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001195 const auto root = [&]() -> sp<Layer> {
1196 sp<Layer> layer = this;
1197 while (auto parent = layer->getParent()) {
1198 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001199 }
Ady Abrahama850c182021-08-04 13:04:37 -07001200 return layer;
1201 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001202
Ady Abraham60e42ea2020-03-09 19:17:31 -07001203 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001204 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001205
Ady Abrahama850c182021-08-04 13:04:37 -07001206 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001207 if (transactionNeeded) {
1208 mFlinger->setTransactionFlags(eTraversalNeeded);
1209 }
1210}
1211
Ady Abraham71c437d2020-01-31 15:56:57 -08001212bool Layer::setFrameRate(FrameRate frameRate) {
Robert Carr6a160312021-05-17 12:08:20 -07001213 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001214 return false;
1215 }
1216
Robert Carr6a160312021-05-17 12:08:20 -07001217 mDrawingState.sequence++;
1218 mDrawingState.frameRate = frameRate;
1219 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001220
1221 updateTreeHasFrameRateVote();
1222
Steven Thomas3172e202020-01-06 19:25:30 -08001223 setTransactionFlags(eTransactionNeeded);
1224 return true;
1225}
1226
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001227void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1228 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001229 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001230
1231 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1232 // there are two transactions with the same token, the first one without a buffer and the
1233 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1234 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001235 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1236 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001237 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001238 mDrawingState.bufferSurfaceFrameTX = it->second;
1239 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1240 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1241 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001242 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001243 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001244 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1245 }
1246}
1247
1248void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1249 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001250 mDrawingState.frameTimelineInfo = info;
1251 mDrawingState.postTime = postTime;
1252 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001253 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001254
Robert Carr6a160312021-05-17 12:08:20 -07001255 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001256 bufferSurfaceFrameTX != nullptr) {
1257 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1258 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1259 // being used for BufferSurfaceFrame, don't create a new one.
1260 return;
1261 }
1262 }
1263 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1264 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1265 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001266 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1267 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001268 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001269 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001270 } else {
1271 if (it->second->getPresentState() == PresentState::Presented) {
1272 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1273 // have been from previous vsync.
1274 it->second = createSurfaceFrameForTransaction(info, postTime);
1275 }
1276 }
1277}
1278
1279void Layer::addSurfaceFrameDroppedForBuffer(
1280 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001281 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001282 surfaceFrame->setPresentState(PresentState::Dropped);
1283 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1284}
1285
1286void Layer::addSurfaceFramePresentedForBuffer(
1287 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1288 nsecs_t currentLatchTime) {
1289 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1290 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1291 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1292 mLastLatchTime = currentLatchTime;
1293}
1294
1295std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1296 const FrameTimelineInfo& info, nsecs_t postTime) {
1297 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001298 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1299 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001300 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001301 /*isBuffer*/ false, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001302 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001303 // For Transactions, the post time is considered to be both queue and acquire fence time.
1304 surfaceFrame->setActualQueueTime(postTime);
1305 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001306 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1307 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001308 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001309 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001310 onSurfaceFrameCreated(surfaceFrame);
1311 return surfaceFrame;
1312}
1313
1314std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1315 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1316 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001317 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001318 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001319 /*isBuffer*/ true, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001320 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001321 // For buffers, acquire fence time will set during latch.
1322 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001323 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1324 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001325 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001326 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001327 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1328 onSurfaceFrameCreated(surfaceFrame);
1329 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001330}
1331
Ady Abrahama850c182021-08-04 13:04:37 -07001332bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1333 if (mDrawingState.frameRateForLayerTree == frameRate) {
1334 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001335 }
1336
Ady Abrahama850c182021-08-04 13:04:37 -07001337 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001338
Ady Abrahama850c182021-08-04 13:04:37 -07001339 // TODO(b/195668952): we probably don't need to dirty visible regions here
1340 // or even store frameRateForLayerTree in mDrawingState
1341 mDrawingState.sequence++;
1342 mDrawingState.modified = true;
1343 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001344
Dominik Laskowski068173d2021-08-11 17:22:59 -07001345 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
1346 mFlinger->mScheduler->recordLayerHistory(this, systemTime(), LayerUpdateType::SetFrameRate);
Ady Abrahama850c182021-08-04 13:04:37 -07001347
1348 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001349}
1350
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001351Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1352 return getDrawingState().frameRateForLayerTree;
1353}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354
Robert Carr1f0a16a2016-10-24 16:27:39 -07001355bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001356 const State& s(mDrawingState);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001357 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001358 if (parent != nullptr && parent->isHiddenByPolicy()) {
1359 return true;
1360 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001361 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1362 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1363 if (zOrderRelativeOf != nullptr) {
1364 if (zOrderRelativeOf->isHiddenByPolicy()) {
1365 return true;
1366 }
1367 }
1368 }
Garfield Tan2c1782c2022-02-16 15:25:05 -08001369 if (CC_UNLIKELY(!isTransformValid())) {
1370 ALOGW("Hide layer %s because it has invalid transformation.", getDebugName());
1371 return true;
1372 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001373 return s.flags & layer_state_t::eLayerHidden;
1374}
1375
David Sodman41fdfc92017-11-06 16:09:56 -08001376uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001377 // TODO: should we do something special if mSecure is set?
1378 if (mProtectedByApp) {
1379 // need a hardware-protected path to external video sink
1380 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001381 }
Riley Andrews03414a12014-07-01 14:22:59 -07001382 if (mPotentialCursor) {
1383 usage |= GraphicBuffer::USAGE_CURSOR;
1384 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001385 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001386 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001387}
1388
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001389void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1390 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1391 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001392 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001393
Vishnu Nair6213bd92020-05-08 17:42:25 -07001394 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001395}
1396
Mathias Agopian13127d82013-03-05 17:47:11 -08001397// ----------------------------------------------------------------------------
1398// debugging
1399// ----------------------------------------------------------------------------
1400
Marissa Wall61c58622018-07-18 10:12:20 -07001401// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001402LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001403 using namespace std::string_literals;
1404
Kalle Raitaa099a242017-01-11 11:17:29 -08001405 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001406 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001407 info.mName = getName();
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001408 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001409 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001410 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001411 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001412
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001413 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001414 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001415 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001416 info.mX = ds.transform.tx();
1417 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001418 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001419 info.mWidth = ds.width;
1420 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001421 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001422 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001423 info.mFlags = ds.flags;
1424 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001425 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001426 info.mMatrix[0][0] = ds.transform[0][0];
1427 info.mMatrix[0][1] = ds.transform[0][1];
1428 info.mMatrix[1][0] = ds.transform[1][0];
1429 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001430 {
chaviwd62d3062019-09-04 14:48:02 -07001431 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001432 if (buffer != 0) {
1433 info.mActiveBufferWidth = buffer->getWidth();
1434 info.mActiveBufferHeight = buffer->getHeight();
1435 info.mActiveBufferStride = buffer->getStride();
1436 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001437 } else {
1438 info.mActiveBufferWidth = 0;
1439 info.mActiveBufferHeight = 0;
1440 info.mActiveBufferStride = 0;
1441 info.mActiveBufferFormat = 0;
1442 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001443 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001444 info.mNumQueuedFrames = getQueuedFrameCount();
Kalle Raitaa099a242017-01-11 11:17:29 -08001445 info.mIsOpaque = isOpaque(ds);
1446 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001447 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001448 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001449}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001450
Yiwei Zhang5434a782018-12-05 18:06:32 -08001451void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001452 result.append(kDumpTableRowLength, '-');
1453 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001454 result.append(" Layer name\n");
1455 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001456 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001457 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001458 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001459 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001460 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001461 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1462 result.append(kDumpTableRowLength, '-');
1463 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001464}
1465
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001466void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1467 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001468 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001469 return;
1470 }
1471
Yiwei Zhang5434a782018-12-05 18:06:32 -08001472 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001473 if (mName.length() > 77) {
1474 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001475 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001476 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001477 shortened.append(mName, mName.length() - 36);
1478 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001479 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001480 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001481 }
1482
Yiwei Zhang5434a782018-12-05 18:06:32 -08001483 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001484
Alec Mourib416efd2018-09-06 21:01:59 +00001485 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001486 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001487
Chia-I Wu1e043612018-03-01 09:45:09 -08001488 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001489 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001490 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001491 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001492 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001493 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001494 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001495 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1496 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001497 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001498 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001499 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001500 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001501 const auto frameRate = getFrameRateForLayerTree();
1502 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1503 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001504 ftl::enum_string(frameRate.type).c_str(),
1505 ftl::enum_string(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001506 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001507 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001508 }
Dan Stozae22aec72016-08-01 13:20:59 -07001509
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001510 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1511 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1512
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001513 result.append(kDumpTableRowLength, '-');
1514 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001515}
Dan Stozae22aec72016-08-01 13:20:59 -07001516
Yiwei Zhang5434a782018-12-05 18:06:32 -08001517void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001518 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001519}
1520
Svetoslavd85084b2014-03-20 10:28:31 -07001521void Layer::clearFrameStats() {
1522 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001523}
1524
Jamie Gennis6547ff42013-07-16 20:12:42 -07001525void Layer::logFrameStats() {
1526 mFrameTracker.logAndResetStats(mName);
1527}
1528
Svetoslavd85084b2014-03-20 10:28:31 -07001529void Layer::getFrameStats(FrameStats* outStats) const {
1530 mFrameTracker.getStats(outStats);
1531}
1532
Vishnu Nair0f085c62019-08-30 08:49:12 -07001533void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001534 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1535 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001536}
1537
Brian Anderson5ea5e592016-12-01 16:54:33 -08001538void Layer::onDisconnect() {
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001539 const int32_t layerId = getSequence();
1540 mFlinger->mTimeStats->onDestroy(layerId);
1541 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001542}
1543
Chia-I Wu98f1c102017-05-30 14:54:08 -07001544size_t Layer::getChildrenCount() const {
1545 size_t count = 0;
1546 for (const sp<Layer>& child : mCurrentChildren) {
1547 count += 1 + child->getChildrenCount();
1548 }
1549 return count;
1550}
1551
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001552void Layer::setGameModeForTree(GameMode gameMode) {
1553 const auto& currentState = getDrawingState();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001554 if (currentState.metadata.has(METADATA_GAME_MODE)) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001555 gameMode = static_cast<GameMode>(currentState.metadata.getInt32(METADATA_GAME_MODE, 0));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001556 }
1557 setGameMode(gameMode);
1558 for (const sp<Layer>& child : mCurrentChildren) {
1559 child->setGameModeForTree(gameMode);
1560 }
1561}
1562
Robert Carr1f0a16a2016-10-24 16:27:39 -07001563void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001564 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001565 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001566
Robert Carr1f0a16a2016-10-24 16:27:39 -07001567 mCurrentChildren.add(layer);
1568 layer->setParent(this);
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001569 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001570 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001571}
1572
1573ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001574 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001575 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001576
Robert Carr1323c952019-01-28 18:13:27 -08001577 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001578 const auto removeResult = mCurrentChildren.remove(layer);
1579
1580 updateTreeHasFrameRateVote();
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001581 layer->setGameModeForTree(GameMode::Unsupported);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001582 layer->updateTreeHasFrameRateVote();
1583
1584 return removeResult;
1585}
1586
Robert Carr15eae092018-03-23 13:43:53 -07001587void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001588 for (const sp<Layer>& child : mDrawingChildren) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001589 child->mDrawingParent = newParent;
xinying1f4200e62022-04-26 14:41:30 +08001590 const float parentShadowRadius =
1591 newParent->canDrawShadows() ? 0.f : newParent->mEffectiveShadowRadius;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001592 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
xinying1f4200e62022-04-26 14:41:30 +08001593 parentShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001594 }
1595}
1596
chaviwf1961f72017-09-18 16:41:07 -07001597bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001598 sp<Layer> newParent;
1599 if (newParentHandle != nullptr) {
Vishnu Nairf9096652021-07-20 18:49:42 -07001600 newParent = fromHandle(newParentHandle).promote();
Robert Carr54cf5b12019-01-25 14:02:28 -08001601 if (newParent == nullptr) {
1602 ALOGE("Unable to promote Layer handle");
1603 return false;
1604 }
1605 if (newParent == this) {
1606 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1607 return false;
1608 }
1609 }
1610
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001611 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001612 if (parent != nullptr) {
1613 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001614 }
1615
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001616 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001617 newParent->addChild(this);
1618 if (!newParent->isRemovedFromCurrentState()) {
1619 addToCurrentState();
1620 } else {
1621 onRemovedFromCurrentState();
1622 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001623 } else {
1624 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001625 }
1626
chaviw06178942017-07-27 10:25:59 -07001627 return true;
1628}
1629
Peiyong Lind3788632018-09-18 16:01:31 -07001630bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001631 static const mat4 identityMatrix = mat4();
1632
Robert Carr6a160312021-05-17 12:08:20 -07001633 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001634 return false;
1635 }
Robert Carr6a160312021-05-17 12:08:20 -07001636 ++mDrawingState.sequence;
1637 mDrawingState.colorTransform = matrix;
1638 mDrawingState.hasColorTransform = matrix != identityMatrix;
1639 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001640 setTransactionFlags(eTransactionNeeded);
1641 return true;
1642}
1643
chaviwf66724d2018-11-28 16:35:21 -08001644mat4 Layer::getColorTransform() const {
1645 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001646 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001647 colorTransform = parent->getColorTransform() * colorTransform;
1648 }
1649 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001650}
1651
1652bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001653 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001654 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001655 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1656 }
1657 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001658}
1659
Chia-I Wu11481472018-05-04 10:43:19 -07001660bool Layer::isLegacyDataSpace() const {
1661 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001662 return !(getDataSpace() &
1663 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1664 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001665}
1666
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001668 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669}
1670
Robert Carr6a160312021-05-17 12:08:20 -07001671int32_t Layer::getZ(LayerVector::StateSet) const {
1672 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673}
1674
Robert Carr1c5481e2019-07-01 14:42:27 -07001675bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001676 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001677 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001678 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001679}
1680
David Sodman41fdfc92017-11-06 16:09:56 -08001681__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001682 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001683 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1684 "makeTraversalList received invalid stateSet");
1685 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1686 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001687 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001688
Robert Carr29abff82017-12-04 13:51:20 -08001689 if (state.zOrderRelatives.size() == 0) {
1690 *outSkipRelativeZUsers = true;
1691 return children;
1692 }
1693
chaviwfd462612018-05-31 16:11:27 -07001694 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001695 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001696 sp<Layer> strongRelative = weakRelative.promote();
1697 if (strongRelative != nullptr) {
1698 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001699 }
1700 }
1701
Dan Stoza412903f2017-04-27 13:42:17 -07001702 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001703 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001704 continue;
1705 }
Robert Carrdb66e622017-04-10 16:55:57 -07001706 traverse.add(child);
1707 }
1708
1709 return traverse;
1710}
1711
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712/**
Robert Carrdb66e622017-04-10 16:55:57 -07001713 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001714 */
Dan Stoza412903f2017-04-27 13:42:17 -07001715void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001716 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1717 // produce a new list for traversing, including our relatives, and not including our children
1718 // who are relatives of another surface. In the case that there are no relative Z,
1719 // makeTraversalList returns our children directly to avoid significant overhead.
1720 // However in this case we need to take the responsibility for filtering children which
1721 // are relatives of another surface here.
1722 bool skipRelativeZUsers = false;
1723 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001724
Robert Carr1f0a16a2016-10-24 16:27:39 -07001725 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001726 for (; i < list.size(); i++) {
1727 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001728 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1729 continue;
1730 }
1731
chaviw301b1d82019-11-06 13:15:09 -08001732 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001733 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001734 }
Dan Stoza412903f2017-04-27 13:42:17 -07001735 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 }
Robert Carr29abff82017-12-04 13:51:20 -08001737
Dan Stoza412903f2017-04-27 13:42:17 -07001738 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001739 for (; i < list.size(); i++) {
1740 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001741
Robert Carr29abff82017-12-04 13:51:20 -08001742 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1743 continue;
1744 }
Dan Stoza412903f2017-04-27 13:42:17 -07001745 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001746 }
1747}
1748
1749/**
Robert Carrdb66e622017-04-10 16:55:57 -07001750 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001751 */
Dan Stoza412903f2017-04-27 13:42:17 -07001752void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1753 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001754 // See traverseInZOrder for documentation.
1755 bool skipRelativeZUsers = false;
1756 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001757
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001759 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001760 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001761
1762 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1763 continue;
1764 }
1765
chaviw301b1d82019-11-06 13:15:09 -08001766 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001767 break;
1768 }
Dan Stoza412903f2017-04-27 13:42:17 -07001769 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 }
Dan Stoza412903f2017-04-27 13:42:17 -07001771 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001772 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001773 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001774
1775 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1776 continue;
1777 }
1778
Dan Stoza412903f2017-04-27 13:42:17 -07001779 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780 }
1781}
1782
Edgar Arriaga844fa672020-01-16 14:21:42 -08001783void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1784 visitor(this);
1785 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001786 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001787 for (const sp<Layer>& child : children) {
1788 child->traverse(state, visitor);
1789 }
1790}
1791
chaviw4b129c22018-04-09 16:19:43 -07001792LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1793 const std::vector<Layer*>& layersInTree) {
1794 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1795 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001796 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1797 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001798 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001799
chaviwfd462612018-05-31 16:11:27 -07001800 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001801 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1802 sp<Layer> strongRelative = weakRelative.promote();
1803 // Only add relative layers that are also descendents of the top most parent of the tree.
1804 // If a relative layer is not a descendent, then it should be ignored.
1805 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1806 traverse.add(strongRelative);
1807 }
1808 }
1809
1810 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001811 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001812 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1813 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1814 // the child here since it won't be added later as a relative.
1815 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1816 childState.zOrderRelativeOf.promote().get())) {
1817 continue;
1818 }
1819 traverse.add(child);
1820 }
1821
1822 return traverse;
1823}
1824
1825void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1826 LayerVector::StateSet stateSet,
1827 const LayerVector::Visitor& visitor) {
1828 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001829
1830 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001831 for (; i < list.size(); i++) {
1832 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001833 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001834 break;
1835 }
chaviw4b129c22018-04-09 16:19:43 -07001836 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001837 }
chaviw4b129c22018-04-09 16:19:43 -07001838
chaviwa76b2712017-09-20 12:02:26 -07001839 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001840 for (; i < list.size(); i++) {
1841 const auto& relative = list[i];
1842 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001843 }
1844}
1845
chaviw4b129c22018-04-09 16:19:43 -07001846std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1847 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1848 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1849
1850 std::vector<Layer*> layersInTree = {this};
1851 for (size_t i = 0; i < children.size(); i++) {
1852 const auto& child = children[i];
1853 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1854 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1855 }
1856
1857 return layersInTree;
1858}
1859
1860void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1861 const LayerVector::Visitor& visitor) {
1862 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1863 std::sort(layersInTree.begin(), layersInTree.end());
1864 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1865}
1866
Peiyong Linefefaac2018-08-17 12:27:51 -07001867ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001868 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001869}
1870
Garfield Tan2c1782c2022-02-16 15:25:05 -08001871bool Layer::isTransformValid() const {
1872 float transformDet = getTransform().det();
1873 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
1874}
1875
chaviw13fdc492017-06-27 12:40:18 -07001876half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001877 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001878
chaviw13fdc492017-06-27 12:40:18 -07001879 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1880 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001881}
Robert Carr6452f122017-03-21 10:41:29 -07001882
Vishnu Nair6213bd92020-05-08 17:42:25 -07001883ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001884 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001885 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1886 return fixedTransformHint;
1887 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001888 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001889 if (!p) return fixedTransformHint;
1890 return p->getFixedTransformHint();
1891}
1892
chaviw13fdc492017-06-27 12:40:18 -07001893half4 Layer::getColor() const {
1894 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001895 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001896}
Robert Carr6452f122017-03-21 10:41:29 -07001897
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001898int32_t Layer::getBackgroundBlurRadius() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001899 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001900
1901 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1902 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001903}
1904
Galia Peychevae0acf382021-04-12 21:22:34 +02001905const std::vector<BlurRegion> Layer::getBlurRegions() const {
1906 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001907 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001908 for (auto& region : regionsCopy) {
1909 region.alpha = region.alpha * layerAlpha;
1910 }
1911 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001912}
1913
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001914Layer::RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001915 // Get parent settings
1916 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001917 const auto& parent = mDrawingParent.promote();
1918 if (parent != nullptr) {
1919 parentSettings = parent->getRoundedCornerState();
Vishnu Nairadb70392022-07-11 15:04:07 -07001920 if (parentSettings.hasRoundedCorners()) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001921 ui::Transform t = getActiveTransform(getDrawingState());
1922 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001923 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Vishnu Nairadb70392022-07-11 15:04:07 -07001924 parentSettings.radius.x *= t.getScaleX();
1925 parentSettings.radius.y *= t.getScaleY();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001926 }
1927 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001928
1929 // Get layer settings
1930 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Vishnu Nairadb70392022-07-11 15:04:07 -07001931 const vec2 radius(getDrawingState().cornerRadius, getDrawingState().cornerRadius);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001932 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nairadb70392022-07-11 15:04:07 -07001933 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001934
Vishnu Nairadb70392022-07-11 15:04:07 -07001935 if (layerSettingsValid && parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001936 // If the parent and the layer have rounded corner settings, use the parent settings if the
1937 // parent crop is entirely inside the layer crop.
1938 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1939 if (parentSettings.cropRect.left > layerCropRect.left &&
1940 parentSettings.cropRect.top > layerCropRect.top &&
1941 parentSettings.cropRect.right < layerCropRect.right &&
1942 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1943 return parentSettings;
1944 } else {
1945 return layerSettings;
1946 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001947 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001948 return layerSettings;
Vishnu Nairadb70392022-07-11 15:04:07 -07001949 } else if (parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001950 return parentSettings;
1951 }
1952 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001953}
1954
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001955void Layer::prepareShadowClientComposition(LayerFE::LayerSettings& caster,
1956 const Rect& layerStackRect) {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001957 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1958
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001959 // Note: this preserves existing behavior of shadowing the entire layer and not cropping it if
1960 // transparent regions are present. This may not be necessary since shadows are only cast by
1961 // SurfaceFlinger's EffectLayers, which do not typically use transparent regions.
1962 state.boundaries = mBounds;
1963
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001964 // Shift the spot light x-position to the middle of the display and then
1965 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001966 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001967 state.lightPos.y -= mScreenBounds.top;
1968
1969 state.length = mEffectiveShadowRadius;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001970
1971 if (state.length > 0.f) {
1972 const float casterAlpha = caster.alpha;
1973 const bool casterIsOpaque =
1974 ((caster.source.buffer.buffer != nullptr) && caster.source.buffer.isOpaque);
1975
1976 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
1977 // Otherwise the generated shadow will only be shown around the casting layer.
1978 state.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
1979 state.ambientColor *= casterAlpha;
1980 state.spotColor *= casterAlpha;
1981
1982 if (state.ambientColor.a > 0.f && state.spotColor.a > 0.f) {
1983 caster.shadow = state;
1984 }
1985 }
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001986}
1987
Robert Carr88b85e12022-03-21 15:47:35 -07001988bool Layer::findInHierarchy(const sp<Layer>& l) {
1989 if (l == this) {
1990 return true;
1991 }
1992 for (auto& child : mDrawingChildren) {
1993 if (child->findInHierarchy(l)) {
1994 return true;
1995 }
1996 }
1997 return false;
1998}
1999
Robert Carr1f0a16a2016-10-24 16:27:39 -07002000void Layer::commitChildList() {
2001 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2002 const auto& child = mCurrentChildren[i];
2003 child->commitChildList();
2004 }
2005 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002006 mDrawingParent = mCurrentParent;
Robert Carr88b85e12022-03-21 15:47:35 -07002007 if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) {
2008 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
2009 if (zOrderRelativeOf == nullptr) return;
2010 if (findInHierarchy(zOrderRelativeOf)) {
2011 ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(),
2012 zOrderRelativeOf->mName.c_str());
2013 ALOGE("Severing rel Z loop, potentially dangerous");
2014 mDrawingState.isRelativeOf = false;
2015 zOrderRelativeOf->removeZOrderRelative(this);
2016 }
2017 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002018}
2019
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002020
chaviw3277faf2021-05-19 16:45:23 -05002021void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07002022 mDrawingState.inputInfo = info;
Vishnu Nairf9096652021-07-20 18:49:42 -07002023 mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07002024 mDrawingState.modified = true;
Robert Carrbf14dbb2021-06-11 13:26:56 -07002025 mFlinger->mInputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002026 setTransactionFlags(eTransactionNeeded);
2027}
2028
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002029LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) {
chaviw08f3cb22020-01-13 13:17:21 -08002030 LayerProto* layerProto = layersProto.add_layers();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002031 writeToProtoDrawingState(layerProto);
chaviw08f3cb22020-01-13 13:17:21 -08002032 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2033
Vishnu Nair00b90132021-11-05 14:03:40 -07002034 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Dominik Laskowski298b08e2022-02-15 13:45:02 -08002035 ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread.
2036
Vishnu Nair60db8c02020-04-02 11:55:16 -07002037 // Only populate for the primary display.
Dominik Laskowski298b08e2022-02-15 13:45:02 -08002038 if (const auto display = mFlinger->getDefaultDisplayDeviceLocked()) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05002039 const auto compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002040 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002041 LayerProtoHelper::writeToProto(getVisibleRegion(display.get()),
2042 [&]() { return layerProto->mutable_visible_region(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002043 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002044 }
2045
chaviw08f3cb22020-01-13 13:17:21 -08002046 for (const sp<Layer>& layer : mDrawingChildren) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002047 layer->writeToProto(layersProto, traceFlags);
chaviw08f3cb22020-01-13 13:17:21 -08002048 }
chaviw6d89e2d2020-01-14 14:42:01 -08002049
2050 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002051}
2052
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002053void Layer::writeToProtoDrawingState(LayerProto* layerInfo) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002054 const ui::Transform transform = getTransform();
Vishnu Naird37343b2022-01-12 16:18:56 -08002055 auto buffer = getExternalTexture();
Vishnu Nair00b90132021-11-05 14:03:40 -07002056 if (buffer != nullptr) {
Vishnu Naird37343b2022-01-12 16:18:56 -08002057 LayerProtoHelper::writeToProto(*buffer,
Vishnu Nair00b90132021-11-05 14:03:40 -07002058 [&]() { return layerInfo->mutable_active_buffer(); });
2059 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
2060 layerInfo->mutable_buffer_transform());
2061 }
2062 layerInfo->set_invalidate(contentDirty);
2063 layerInfo->set_is_protected(isProtected());
2064 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
2065 layerInfo->set_queued_frames(getQueuedFrameCount());
Vishnu Nair00b90132021-11-05 14:03:40 -07002066 layerInfo->set_curr_frame(mCurrentFrameNumber);
2067 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002068
Vishnu Nair00b90132021-11-05 14:03:40 -07002069 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nairadb70392022-07-11 15:04:07 -07002070 layerInfo->set_corner_radius(
2071 (getRoundedCornerState().radius.x + getRoundedCornerState().radius.y) / 2.0);
Vishnu Nair00b90132021-11-05 14:03:40 -07002072 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
2073 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
2074 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
2075 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2076 [&]() { return layerInfo->mutable_position(); });
2077 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair00b90132021-11-05 14:03:40 -07002078 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2079 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07002080
Vishnu Nair00b90132021-11-05 14:03:40 -07002081 if (hasColorTransform()) {
2082 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002083 }
2084
Vishnu Nair60db8c02020-04-02 11:55:16 -07002085 LayerProtoHelper::writeToProto(mSourceBounds,
2086 [&]() { return layerInfo->mutable_source_bounds(); });
2087 LayerProtoHelper::writeToProto(mScreenBounds,
2088 [&]() { return layerInfo->mutable_screen_bounds(); });
2089 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2090 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2091 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002092}
2093
2094void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002095 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002096 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2097 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002098 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002099
chaviw766c9c52021-02-10 17:36:47 -08002100 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002101
Vishnu Nair00b90132021-11-05 14:03:40 -07002102 layerInfo->set_id(sequence);
2103 layerInfo->set_name(getName().c_str());
2104 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002105
Vishnu Nair00b90132021-11-05 14:03:40 -07002106 for (const auto& child : children) {
2107 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002108 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002109
Vishnu Nair00b90132021-11-05 14:03:40 -07002110 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2111 sp<Layer> strongRelative = weakRelative.promote();
2112 if (strongRelative != nullptr) {
2113 layerInfo->add_relatives(strongRelative->sequence);
2114 }
2115 }
2116
2117 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2118 [&]() { return layerInfo->mutable_transparent_region(); });
2119
2120 layerInfo->set_layer_stack(getLayerStack().id);
2121 layerInfo->set_z(state.z);
2122
2123 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2124 return layerInfo->mutable_requested_position();
2125 });
2126
2127 LayerProtoHelper::writeSizeToProto(state.width, state.height,
2128 [&]() { return layerInfo->mutable_size(); });
2129
2130 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2131
2132 layerInfo->set_is_opaque(isOpaque(state));
2133
2134 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2135 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2136 LayerProtoHelper::writeToProto(state.color,
2137 [&]() { return layerInfo->mutable_requested_color(); });
2138 layerInfo->set_flags(state.flags);
2139
2140 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2141 layerInfo->mutable_requested_transform());
2142
2143 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2144 if (parent != nullptr) {
2145 layerInfo->set_parent(parent->sequence);
2146 } else {
2147 layerInfo->set_parent(-1);
2148 }
2149
2150 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2151 if (zOrderRelativeOf != nullptr) {
2152 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2153 } else {
2154 layerInfo->set_z_order_relative_of(-1);
2155 }
2156
2157 layerInfo->set_is_relative_of(state.isRelativeOf);
2158
2159 layerInfo->set_owner_uid(mOwnerUid);
2160
Arthur Hung4cf2d8c2022-04-07 14:52:00 +00002161 if ((traceFlags & LayerTracing::TRACE_INPUT) && needsInputInfo()) {
chaviw3277faf2021-05-19 16:45:23 -05002162 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002163 if (useDrawing) {
Prabir Pradhan83aca382022-07-22 19:53:04 +00002164 info = fillInputInfo(
2165 InputDisplayArgs{.transform = &kIdentityTransform, .isSecure = true});
chaviw4c34a092020-07-08 11:30:06 -07002166 } else {
2167 info = state.inputInfo;
2168 }
2169
2170 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002171 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002172 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002173
Vishnu Nair00b90132021-11-05 14:03:40 -07002174 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002175 auto protoMap = layerInfo->mutable_metadata();
2176 for (const auto& entry : state.metadata.mMap) {
2177 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2178 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002179 }
Vishnu Naird2aaab12022-02-10 14:49:09 -08002180
2181 LayerProtoHelper::writeToProto(state.destinationFrame,
2182 [&]() { return layerInfo->mutable_destination_frame(); });
chaviw1d044282017-09-27 12:19:28 -07002183}
2184
Robert Carr2e102c92018-10-23 12:11:15 -07002185bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002186 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002187}
2188
chaviw39d01472021-04-08 14:26:24 -05002189ui::Transform Layer::getInputTransform() const {
2190 return getTransform();
2191}
2192
2193Rect Layer::getInputBounds() const {
2194 return getCroppedBufferSize(getDrawingState());
2195}
2196
Prabir Pradhan33da9462022-06-14 14:55:57 +00002197// Applies the given transform to the region, while protecting against overflows caused by any
2198// offsets. If applying the offset in the transform to any of the Rects in the region would result
2199// in an overflow, they are not added to the output Region.
2200static Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
2201 const std::string& debugWindowName) {
2202 // Round the translation using the same rounding strategy used by ui::Transform.
2203 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
2204 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
2205
2206 ui::Transform transformWithoutOffset = t;
2207 transformWithoutOffset.set(0.f, 0.f);
2208
2209 const Region transformed = transformWithoutOffset.transform(r);
2210
2211 // Apply the translation to each of the Rects in the region while discarding any that overflow.
2212 Region ret;
2213 for (const auto& rect : transformed) {
2214 Rect newRect;
2215 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
2216 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
2217 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
2218 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
2219 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
2220 debugWindowName.c_str());
2221 continue;
2222 }
2223 ret.orSelf(newRect);
2224 }
2225 return ret;
2226}
2227
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002228void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& screenToDisplay) {
2229 Rect tmpBounds = getInputBounds();
2230 if (!tmpBounds.isValid()) {
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08002231 info.touchableRegion.clear();
2232 // A layer could have invalid input bounds and still expect to receive touch input if it has
2233 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
2234 // correctly to determine the coordinate space for input events. Use an empty rect so that
2235 // the layer will receive input in its own layer space.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002236 tmpBounds = Rect::EMPTY_RECT;
chaviw7e72caf2020-12-02 16:50:43 -08002237 }
2238
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002239 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
2240 // frame and transform used for the layer, which determines the bounds and the coordinate space
2241 // within which the layer will receive input.
2242 //
2243 // The coordinate space within which each of the bounds are specified is explicitly documented
2244 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
2245 // Transform converts one coordinate space to another, which is apparent in its naming. For
2246 // example, "layerToDisplay" transforms layer space to display space.
2247 //
2248 // Coordinate space definitions:
2249 // - display: The display device's coordinate space. Correlates to pixels on the display.
2250 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
2251 // - layer: The coordinate space of this layer.
2252 // - input: The coordinate space in which this layer will receive input events. This could be
2253 // different than layer space if a surfaceInset is used, which changes the origin
2254 // of the input space.
2255 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
chaviw7e72caf2020-12-02 16:50:43 -08002256
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002257 // Clamp surface inset to the input bounds.
2258 const auto surfaceInset = static_cast<float>(info.surfaceInset);
2259 const float xSurfaceInset =
2260 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
2261 const float ySurfaceInset =
2262 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
chaviw1ff3d1e2020-07-01 15:53:47 -07002263
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002264 // Apply the insets to the input bounds.
2265 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
2266 inputBoundsInLayer.top + ySurfaceInset,
2267 inputBoundsInLayer.right - xSurfaceInset,
2268 inputBoundsInLayer.bottom - ySurfaceInset);
Ady Abraham282f1d72019-07-24 18:05:56 -07002269
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002270 // Crop the input bounds to ensure it is within the parent's bounds.
2271 const FloatRect croppedInsetBoundsInLayer = mBounds.intersect(insetBoundsInLayer);
Ady Abraham282f1d72019-07-24 18:05:56 -07002272
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002273 const ui::Transform layerToScreen = getInputTransform();
2274 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
Vishnu Nair8033a492018-12-05 07:27:23 -08002275
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002276 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
2277 info.frameLeft = roundedFrameInDisplay.left;
2278 info.frameTop = roundedFrameInDisplay.top;
2279 info.frameRight = roundedFrameInDisplay.right;
2280 info.frameBottom = roundedFrameInDisplay.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -07002281
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002282 ui::Transform inputToLayer;
2283 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
2284 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
chaviw39cfa2e2020-11-04 14:19:02 -08002285
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002286 // InputDispatcher expects a display-to-input transform.
2287 info.transform = inputToDisplay.inverse();
2288
2289 // The touchable region is specified in the input coordinate space. Change it to display space.
Prabir Pradhan33da9462022-06-14 14:55:57 +00002290 info.touchableRegion =
2291 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, mName);
chaviw7e72caf2020-12-02 16:50:43 -08002292}
2293
chaviw3277faf2021-05-19 16:45:23 -05002294void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002295 sp<Layer> p = this;
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002296 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002297 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002298 }
2299 if (p != nullptr) {
2300 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2301 }
2302}
2303
Vishnu Naira066d902021-09-13 18:40:17 -07002304gui::DropInputMode Layer::getDropInputMode() const {
2305 gui::DropInputMode mode = mDrawingState.dropInputMode;
2306 if (mode == gui::DropInputMode::ALL) {
2307 return mode;
2308 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002309 sp<Layer> parent = mDrawingParent.promote();
2310 if (parent) {
2311 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002312 if (parentMode != gui::DropInputMode::NONE) {
2313 return parentMode;
2314 }
2315 }
2316 return mode;
2317}
2318
2319void Layer::handleDropInputMode(gui::WindowInfo& info) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002320 if (mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Vishnu Naira066d902021-09-13 18:40:17 -07002321 return;
2322 }
2323
2324 // Check if we need to drop input unconditionally
2325 gui::DropInputMode dropInputMode = getDropInputMode();
2326 if (dropInputMode == gui::DropInputMode::ALL) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002327 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002328 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2329 return;
2330 }
2331
2332 // Check if we need to check if the window is obscured by parent
2333 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2334 return;
2335 }
2336
2337 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002338 sp<Layer> parent = mDrawingParent.promote();
2339 if (parent && parent->getAlpha() != 1.0_hf) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002340 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002341 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2342 static_cast<float>(getAlpha()));
2343 }
2344
2345 // Check if the parent has cropped the buffer
2346 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2347 if (!bufferSize.isValid()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002348 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002349 return;
2350 }
2351
2352 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2353 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2354 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2355 // match then the layer has not been cropped by its parents.
2356 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2357 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2358
2359 if (croppedByParent) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002360 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002361 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2362 getDebugName());
2363 } else {
2364 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2365 // input if the window is obscured. This check should be done in surfaceflinger but the
2366 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2367 // drop input events if the window is obscured.
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002368 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002369 }
2370}
2371
Prabir Pradhan83aca382022-07-22 19:53:04 +00002372WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) {
chaviw7e72caf2020-12-02 16:50:43 -08002373 if (!hasInputInfo()) {
2374 mDrawingState.inputInfo.name = getName();
2375 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2376 mDrawingState.inputInfo.ownerPid = mOwnerPid;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002377 mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002378 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002379 }
2380
Prabir Pradhan83aca382022-07-22 19:53:04 +00002381 const ui::Transform& displayTransform =
2382 displayArgs.transform != nullptr ? *displayArgs.transform : kIdentityTransform;
2383
chaviw3277faf2021-05-19 16:45:23 -05002384 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002385 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002386 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002387
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002388 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002389
Prabir Pradhan83aca382022-07-22 19:53:04 +00002390 if (displayArgs.transform == nullptr) {
2391 // Do not let the window receive touches if it is not associated with a valid display
2392 // transform. We still allow the window to receive keys and prevent ANRs.
2393 info.inputConfig |= WindowInfo::InputConfig::NOT_TOUCHABLE;
2394 }
2395
Robert Carr5dc426e2020-06-10 14:29:14 -07002396 // For compatibility reasons we let layers which can receive input
2397 // receive input before they have actually submitted a buffer. Because
2398 // of this we use canReceiveInput instead of isVisible to check the
2399 // policy-visibility, ignoring the buffer state. However for layers with
2400 // hasInputInfo()==false we can use the real visibility state.
2401 // We are just using these layers for occlusion detection in
2402 // InputDispatcher, and obviously if they aren't visible they can't occlude
2403 // anything.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002404 const bool visible = hasInputInfo() ? canReceiveInput() : isVisible();
2405 info.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
2406
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002407 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002408 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002409 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002410
Vishnu Nair16a938f2021-09-24 07:14:54 -07002411 // If the window will be blacked out on a display because the display does not have the secure
2412 // flag and the layer has the secure flag set, then drop input.
Prabir Pradhan83aca382022-07-22 19:53:04 +00002413 if (!displayArgs.isSecure && isSecure()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002414 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Nair16a938f2021-09-24 07:14:54 -07002415 }
2416
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002417 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2418 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002419 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2420 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002421 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002422 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002423 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002424 }
2425
Winson Chunga30f7c92021-06-29 15:42:56 -07002426 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2427 // if it was set by WM for a known system overlay
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002428 if (isTrustedOverlay()) {
2429 info.inputConfig |= WindowInfo::InputConfig::TRUSTED_OVERLAY;
2430 }
Winson Chunga30f7c92021-06-29 15:42:56 -07002431
chaviwaf87b3e2019-10-01 16:59:28 -07002432 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2433 // touches from going outside the cloned area.
2434 if (isClone()) {
chaviwc3f39b12021-09-10 13:41:39 -05002435 info.isClone = true;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002436 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2437 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002438 info.touchableRegion = info.touchableRegion.intersect(rect);
2439 }
2440 }
2441
Robert Carr720e5062018-07-30 17:45:14 -07002442 return info;
2443}
2444
chaviwaf87b3e2019-10-01 16:59:28 -07002445sp<Layer> Layer::getClonedRoot() {
2446 if (mClonedChild != nullptr) {
2447 return this;
2448 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002449 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002450 return nullptr;
2451 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002452 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002453}
2454
Robert Carredd13602020-04-13 17:24:34 -07002455bool Layer::hasInputInfo() const {
Linus Tufvessona1858822022-03-04 09:32:07 +00002456 return mDrawingState.inputInfo.token != nullptr ||
2457 mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Robert Carr720e5062018-07-30 17:45:14 -07002458}
2459
chaviw3e727cd2019-01-31 13:41:05 -08002460bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002461 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002462}
2463
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002464compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002465 const DisplayDevice* display) const {
2466 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002467 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002468}
2469
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002470Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2471 const auto outputLayer = findOutputLayerForDisplay(display);
2472 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002473}
2474
chaviwb4c6e582019-08-16 14:35:07 -07002475void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002476 cloneDrawingState(clonedFrom.get());
chaviwb4c6e582019-08-16 14:35:07 -07002477 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002478}
chaviw74b03172019-08-19 11:09:03 -07002479
2480void Layer::updateMirrorInfo() {
2481 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2482 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2483 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2484 // that case, we want to delete the reference to the clone since we want it to get
2485 // destroyed. The root, this layer, will still be around since the client can continue
2486 // to hold a reference, but no cloned layers will be displayed.
2487 mClonedChild = nullptr;
2488 return;
2489 }
2490
2491 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2492 // If the real layer exists and is in current state, add the clone as a child of the root.
2493 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2494 // copied to drawingState for the root layer. So the clonedChild is always removed from
2495 // drawingState and then needs to be added back each traversal.
2496 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2497 addChildToDrawing(mClonedChild);
2498 }
2499
2500 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2501 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2502 mClonedChild->updateClonedRelatives(clonedLayersMap);
2503}
2504
2505void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2506 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2507 // to the clone. If the real layer is no longer alive, continue traversing the children
2508 // since we may be able to pull out other children that are still alive.
2509 if (isClonedFromAlive()) {
2510 sp<Layer> clonedFrom = getClonedFrom();
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002511 cloneDrawingState(clonedFrom.get());
chaviw74b03172019-08-19 11:09:03 -07002512 clonedLayersMap.emplace(clonedFrom, this);
2513 }
2514
2515 // The clone layer may have children in drawingState since they may have been created and
2516 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2517 // that already exist, since we can just re-use them.
2518 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2519 // not updated in the regular traversal. They are skipped since the root will lose the
2520 // reference to them when it copies its currentChildren to drawing.
2521 for (sp<Layer>& child : mDrawingChildren) {
2522 child->updateClonedDrawingState(clonedLayersMap);
2523 }
2524}
2525
2526void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2527 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2528 mDrawingChildren.clear();
2529
2530 if (!isClonedFromAlive()) {
2531 return;
2532 }
2533
2534 sp<Layer> clonedFrom = getClonedFrom();
2535 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2536 if (child == mirrorRoot) {
2537 // This is to avoid cyclical mirroring.
2538 continue;
2539 }
2540 sp<Layer> clonedChild = clonedLayersMap[child];
2541 if (clonedChild == nullptr) {
2542 clonedChild = child->createClone();
2543 clonedLayersMap[child] = clonedChild;
2544 }
2545 addChildToDrawing(clonedChild);
2546 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2547 }
2548}
2549
chaviwaf87b3e2019-10-01 16:59:28 -07002550void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2551 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2552 if (cropLayer != nullptr) {
2553 if (clonedLayersMap.count(cropLayer) == 0) {
2554 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2555 // self as crop layer to avoid going outside bounds.
2556 mDrawingState.touchableRegionCrop = this;
2557 } else {
2558 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2559 mDrawingState.touchableRegionCrop = clonedCropLayer;
2560 }
2561 }
2562 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2563 // WM or the client.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002564 mDrawingState.inputInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, false);
chaviwaf87b3e2019-10-01 16:59:28 -07002565}
2566
2567void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002568 mDrawingState.zOrderRelativeOf = nullptr;
2569 mDrawingState.zOrderRelatives.clear();
2570
2571 if (!isClonedFromAlive()) {
2572 return;
2573 }
2574
chaviwaf87b3e2019-10-01 16:59:28 -07002575 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002576 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002577 const sp<Layer>& relative = relativeWeak.promote();
2578 if (clonedLayersMap.count(relative) > 0) {
2579 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002580 mDrawingState.zOrderRelatives.add(clonedRelative);
2581 }
2582 }
2583
2584 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2585 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2586 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2587 // still traverse the children, but the layer with the missing relativeOf will not be shown
2588 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002589 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2590 if (clonedLayersMap.count(relativeOf) > 0) {
2591 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002592 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2593 }
2594
chaviwaf87b3e2019-10-01 16:59:28 -07002595 updateClonedInputInfo(clonedLayersMap);
2596
chaviw74b03172019-08-19 11:09:03 -07002597 for (sp<Layer>& child : mDrawingChildren) {
2598 child->updateClonedRelatives(clonedLayersMap);
2599 }
2600}
2601
2602void Layer::addChildToDrawing(const sp<Layer>& layer) {
2603 mDrawingChildren.add(layer);
2604 layer->mDrawingParent = this;
2605}
2606
Steven Thomas62a4cf82020-01-31 12:04:03 -08002607Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2608 switch (compatibility) {
2609 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2610 return FrameRateCompatibility::Default;
2611 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2612 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002613 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2614 return FrameRateCompatibility::Exact;
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002615 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
2616 return FrameRateCompatibility::NoVote;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002617 default:
2618 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2619 return FrameRateCompatibility::Default;
2620 }
2621}
2622
Marin Shalamanovc5986772021-03-16 16:09:49 +01002623scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2624 switch (strategy) {
2625 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2626 return Seamlessness::OnlySeamless;
2627 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2628 return Seamlessness::SeamedAndSeamless;
2629 default:
2630 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2631 return Seamlessness::Default;
2632 }
2633}
2634
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002635bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002636 const State& s(mDrawingState);
2637 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2638 return true;
2639 }
2640
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002641 sp<Layer> parent = mDrawingParent.promote();
2642 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002643}
2644
Robert Carr6a0382d2021-07-01 15:57:17 -07002645void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2646 mClonedChild = clonedChild;
2647 mHadClonedChild = true;
2648 mFlinger->mNumClones++;
2649}
2650
Vishnu Nairf9096652021-07-20 18:49:42 -07002651const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle");
2652
2653wp<Layer> Layer::fromHandle(const sp<IBinder>& handleBinder) {
2654 if (handleBinder == nullptr) {
2655 return nullptr;
2656 }
2657
2658 BBinder* b = handleBinder->localBinder();
2659 if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) {
2660 return nullptr;
2661 }
2662
2663 // We can safely cast this binder since its local and we verified its interface descriptor.
2664 sp<Handle> handle = static_cast<Handle*>(handleBinder.get());
2665 return handle->owner;
2666}
2667
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002668bool Layer::setDropInputMode(gui::DropInputMode mode) {
2669 if (mDrawingState.dropInputMode == mode) {
2670 return false;
2671 }
2672 mDrawingState.dropInputMode = mode;
2673 return true;
2674}
2675
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002676void Layer::cloneDrawingState(const Layer* from) {
2677 mDrawingState = from->mDrawingState;
2678 // Skip callback info since they are not applicable for cloned layers.
2679 mDrawingState.releaseBufferListener = nullptr;
2680 mDrawingState.callbackHandles = {};
2681}
2682
chaviw30fd9b92022-04-22 12:41:17 -05002683bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
2684 if (handles.empty()) {
2685 return false;
2686 }
2687
2688 for (const auto& handle : handles) {
2689 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
2690 }
2691
2692 return true;
2693}
2694
Mathias Agopian13127d82013-03-05 17:47:11 -08002695// ---------------------------------------------------------------------------
2696
Marin Shalamanov46084422020-10-13 12:33:42 +02002697std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07002698 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
2699 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02002700}
2701
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07002702} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002703
2704#if defined(__gl_h_)
2705#error "don't include gl/gl.h in this file"
2706#endif
2707
2708#if defined(__gl2_h_)
2709#error "don't include gl2/gl2.h in this file"
2710#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002711
2712// TODO(b/129481165): remove the #pragma below and fix conversion issues
2713#pragma clang diagnostic pop // ignored "-Wconversion"