blob: aff94d132ee5f7466c29540239b3c32e19c4eb09 [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;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001590 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001591 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001592 }
1593}
1594
chaviwf1961f72017-09-18 16:41:07 -07001595bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001596 sp<Layer> newParent;
1597 if (newParentHandle != nullptr) {
Vishnu Nairf9096652021-07-20 18:49:42 -07001598 newParent = fromHandle(newParentHandle).promote();
Robert Carr54cf5b12019-01-25 14:02:28 -08001599 if (newParent == nullptr) {
1600 ALOGE("Unable to promote Layer handle");
1601 return false;
1602 }
1603 if (newParent == this) {
1604 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1605 return false;
1606 }
1607 }
1608
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001609 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001610 if (parent != nullptr) {
1611 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001612 }
1613
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001614 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001615 newParent->addChild(this);
1616 if (!newParent->isRemovedFromCurrentState()) {
1617 addToCurrentState();
1618 } else {
1619 onRemovedFromCurrentState();
1620 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001621 } else {
1622 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001623 }
1624
chaviw06178942017-07-27 10:25:59 -07001625 return true;
1626}
1627
Peiyong Lind3788632018-09-18 16:01:31 -07001628bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001629 static const mat4 identityMatrix = mat4();
1630
Robert Carr6a160312021-05-17 12:08:20 -07001631 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001632 return false;
1633 }
Robert Carr6a160312021-05-17 12:08:20 -07001634 ++mDrawingState.sequence;
1635 mDrawingState.colorTransform = matrix;
1636 mDrawingState.hasColorTransform = matrix != identityMatrix;
1637 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001638 setTransactionFlags(eTransactionNeeded);
1639 return true;
1640}
1641
chaviwf66724d2018-11-28 16:35:21 -08001642mat4 Layer::getColorTransform() const {
1643 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001644 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001645 colorTransform = parent->getColorTransform() * colorTransform;
1646 }
1647 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001648}
1649
1650bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001651 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001652 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001653 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1654 }
1655 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001656}
1657
Chia-I Wu11481472018-05-04 10:43:19 -07001658bool Layer::isLegacyDataSpace() const {
1659 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001660 return !(getDataSpace() &
1661 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1662 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001663}
1664
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001666 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667}
1668
Robert Carr6a160312021-05-17 12:08:20 -07001669int32_t Layer::getZ(LayerVector::StateSet) const {
1670 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671}
1672
Robert Carr1c5481e2019-07-01 14:42:27 -07001673bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001674 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001675 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001676 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001677}
1678
David Sodman41fdfc92017-11-06 16:09:56 -08001679__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001680 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001681 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1682 "makeTraversalList received invalid stateSet");
1683 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1684 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001685 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001686
Robert Carr29abff82017-12-04 13:51:20 -08001687 if (state.zOrderRelatives.size() == 0) {
1688 *outSkipRelativeZUsers = true;
1689 return children;
1690 }
1691
chaviwfd462612018-05-31 16:11:27 -07001692 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001693 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001694 sp<Layer> strongRelative = weakRelative.promote();
1695 if (strongRelative != nullptr) {
1696 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001697 }
1698 }
1699
Dan Stoza412903f2017-04-27 13:42:17 -07001700 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001701 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001702 continue;
1703 }
Robert Carrdb66e622017-04-10 16:55:57 -07001704 traverse.add(child);
1705 }
1706
1707 return traverse;
1708}
1709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710/**
Robert Carrdb66e622017-04-10 16:55:57 -07001711 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712 */
Dan Stoza412903f2017-04-27 13:42:17 -07001713void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001714 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1715 // produce a new list for traversing, including our relatives, and not including our children
1716 // who are relatives of another surface. In the case that there are no relative Z,
1717 // makeTraversalList returns our children directly to avoid significant overhead.
1718 // However in this case we need to take the responsibility for filtering children which
1719 // are relatives of another surface here.
1720 bool skipRelativeZUsers = false;
1721 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001722
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001724 for (; i < list.size(); i++) {
1725 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001726 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1727 continue;
1728 }
1729
chaviw301b1d82019-11-06 13:15:09 -08001730 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001731 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001732 }
Dan Stoza412903f2017-04-27 13:42:17 -07001733 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001734 }
Robert Carr29abff82017-12-04 13:51:20 -08001735
Dan Stoza412903f2017-04-27 13:42:17 -07001736 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001737 for (; i < list.size(); i++) {
1738 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001739
Robert Carr29abff82017-12-04 13:51:20 -08001740 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1741 continue;
1742 }
Dan Stoza412903f2017-04-27 13:42:17 -07001743 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001744 }
1745}
1746
1747/**
Robert Carrdb66e622017-04-10 16:55:57 -07001748 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001749 */
Dan Stoza412903f2017-04-27 13:42:17 -07001750void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1751 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001752 // See traverseInZOrder for documentation.
1753 bool skipRelativeZUsers = false;
1754 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001755
Robert Carr1f0a16a2016-10-24 16:27:39 -07001756 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001757 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001758 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001759
1760 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1761 continue;
1762 }
1763
chaviw301b1d82019-11-06 13:15:09 -08001764 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001765 break;
1766 }
Dan Stoza412903f2017-04-27 13:42:17 -07001767 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768 }
Dan Stoza412903f2017-04-27 13:42:17 -07001769 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001770 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001771 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001772
1773 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1774 continue;
1775 }
1776
Dan Stoza412903f2017-04-27 13:42:17 -07001777 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778 }
1779}
1780
Edgar Arriaga844fa672020-01-16 14:21:42 -08001781void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1782 visitor(this);
1783 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001784 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001785 for (const sp<Layer>& child : children) {
1786 child->traverse(state, visitor);
1787 }
1788}
1789
chaviw4b129c22018-04-09 16:19:43 -07001790LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1791 const std::vector<Layer*>& layersInTree) {
1792 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1793 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001794 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1795 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001796 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001797
chaviwfd462612018-05-31 16:11:27 -07001798 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001799 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1800 sp<Layer> strongRelative = weakRelative.promote();
1801 // Only add relative layers that are also descendents of the top most parent of the tree.
1802 // If a relative layer is not a descendent, then it should be ignored.
1803 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1804 traverse.add(strongRelative);
1805 }
1806 }
1807
1808 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001809 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001810 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1811 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1812 // the child here since it won't be added later as a relative.
1813 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1814 childState.zOrderRelativeOf.promote().get())) {
1815 continue;
1816 }
1817 traverse.add(child);
1818 }
1819
1820 return traverse;
1821}
1822
1823void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1824 LayerVector::StateSet stateSet,
1825 const LayerVector::Visitor& visitor) {
1826 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001827
1828 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001829 for (; i < list.size(); i++) {
1830 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001831 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001832 break;
1833 }
chaviw4b129c22018-04-09 16:19:43 -07001834 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001835 }
chaviw4b129c22018-04-09 16:19:43 -07001836
chaviwa76b2712017-09-20 12:02:26 -07001837 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001838 for (; i < list.size(); i++) {
1839 const auto& relative = list[i];
1840 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001841 }
1842}
1843
chaviw4b129c22018-04-09 16:19:43 -07001844std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1845 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1846 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1847
1848 std::vector<Layer*> layersInTree = {this};
1849 for (size_t i = 0; i < children.size(); i++) {
1850 const auto& child = children[i];
1851 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1852 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1853 }
1854
1855 return layersInTree;
1856}
1857
1858void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1859 const LayerVector::Visitor& visitor) {
1860 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1861 std::sort(layersInTree.begin(), layersInTree.end());
1862 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1863}
1864
Peiyong Linefefaac2018-08-17 12:27:51 -07001865ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001866 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867}
1868
Garfield Tan2c1782c2022-02-16 15:25:05 -08001869bool Layer::isTransformValid() const {
1870 float transformDet = getTransform().det();
1871 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
1872}
1873
chaviw13fdc492017-06-27 12:40:18 -07001874half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001875 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001876
chaviw13fdc492017-06-27 12:40:18 -07001877 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1878 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001879}
Robert Carr6452f122017-03-21 10:41:29 -07001880
Vishnu Nair6213bd92020-05-08 17:42:25 -07001881ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001882 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001883 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1884 return fixedTransformHint;
1885 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001886 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001887 if (!p) return fixedTransformHint;
1888 return p->getFixedTransformHint();
1889}
1890
chaviw13fdc492017-06-27 12:40:18 -07001891half4 Layer::getColor() const {
1892 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001893 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001894}
Robert Carr6452f122017-03-21 10:41:29 -07001895
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001896int32_t Layer::getBackgroundBlurRadius() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001897 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001898
1899 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1900 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001901}
1902
Galia Peychevae0acf382021-04-12 21:22:34 +02001903const std::vector<BlurRegion> Layer::getBlurRegions() const {
1904 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001905 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001906 for (auto& region : regionsCopy) {
1907 region.alpha = region.alpha * layerAlpha;
1908 }
1909 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001910}
1911
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001912Layer::RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001913 // Get parent settings
1914 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001915 const auto& parent = mDrawingParent.promote();
1916 if (parent != nullptr) {
1917 parentSettings = parent->getRoundedCornerState();
Vishnu Nairadb70392022-07-11 15:04:07 -07001918 if (parentSettings.hasRoundedCorners()) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001919 ui::Transform t = getActiveTransform(getDrawingState());
1920 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001921 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Vishnu Nairadb70392022-07-11 15:04:07 -07001922 parentSettings.radius.x *= t.getScaleX();
1923 parentSettings.radius.y *= t.getScaleY();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001924 }
1925 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001926
1927 // Get layer settings
1928 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Vishnu Nairadb70392022-07-11 15:04:07 -07001929 const vec2 radius(getDrawingState().cornerRadius, getDrawingState().cornerRadius);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001930 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nairadb70392022-07-11 15:04:07 -07001931 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001932
Vishnu Nairadb70392022-07-11 15:04:07 -07001933 if (layerSettingsValid && parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001934 // If the parent and the layer have rounded corner settings, use the parent settings if the
1935 // parent crop is entirely inside the layer crop.
1936 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1937 if (parentSettings.cropRect.left > layerCropRect.left &&
1938 parentSettings.cropRect.top > layerCropRect.top &&
1939 parentSettings.cropRect.right < layerCropRect.right &&
1940 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1941 return parentSettings;
1942 } else {
1943 return layerSettings;
1944 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001945 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001946 return layerSettings;
Vishnu Nairadb70392022-07-11 15:04:07 -07001947 } else if (parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001948 return parentSettings;
1949 }
1950 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001951}
1952
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001953void Layer::prepareShadowClientComposition(LayerFE::LayerSettings& caster,
1954 const Rect& layerStackRect) {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001955 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1956
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001957 // Note: this preserves existing behavior of shadowing the entire layer and not cropping it if
1958 // transparent regions are present. This may not be necessary since shadows are only cast by
1959 // SurfaceFlinger's EffectLayers, which do not typically use transparent regions.
1960 state.boundaries = mBounds;
1961
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001962 // Shift the spot light x-position to the middle of the display and then
1963 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001964 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001965 state.lightPos.y -= mScreenBounds.top;
1966
1967 state.length = mEffectiveShadowRadius;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001968
1969 if (state.length > 0.f) {
1970 const float casterAlpha = caster.alpha;
1971 const bool casterIsOpaque =
1972 ((caster.source.buffer.buffer != nullptr) && caster.source.buffer.isOpaque);
1973
1974 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
1975 // Otherwise the generated shadow will only be shown around the casting layer.
1976 state.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
1977 state.ambientColor *= casterAlpha;
1978 state.spotColor *= casterAlpha;
1979
1980 if (state.ambientColor.a > 0.f && state.spotColor.a > 0.f) {
1981 caster.shadow = state;
1982 }
1983 }
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001984}
1985
Robert Carr88b85e12022-03-21 15:47:35 -07001986bool Layer::findInHierarchy(const sp<Layer>& l) {
1987 if (l == this) {
1988 return true;
1989 }
1990 for (auto& child : mDrawingChildren) {
1991 if (child->findInHierarchy(l)) {
1992 return true;
1993 }
1994 }
1995 return false;
1996}
1997
Robert Carr1f0a16a2016-10-24 16:27:39 -07001998void Layer::commitChildList() {
1999 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2000 const auto& child = mCurrentChildren[i];
2001 child->commitChildList();
2002 }
2003 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002004 mDrawingParent = mCurrentParent;
Robert Carr88b85e12022-03-21 15:47:35 -07002005 if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) {
2006 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
2007 if (zOrderRelativeOf == nullptr) return;
2008 if (findInHierarchy(zOrderRelativeOf)) {
2009 ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(),
2010 zOrderRelativeOf->mName.c_str());
2011 ALOGE("Severing rel Z loop, potentially dangerous");
2012 mDrawingState.isRelativeOf = false;
2013 zOrderRelativeOf->removeZOrderRelative(this);
2014 }
2015 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002016}
2017
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002018
chaviw3277faf2021-05-19 16:45:23 -05002019void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07002020 mDrawingState.inputInfo = info;
Vishnu Nairf9096652021-07-20 18:49:42 -07002021 mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07002022 mDrawingState.modified = true;
Robert Carrbf14dbb2021-06-11 13:26:56 -07002023 mFlinger->mInputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002024 setTransactionFlags(eTransactionNeeded);
2025}
2026
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002027LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) {
chaviw08f3cb22020-01-13 13:17:21 -08002028 LayerProto* layerProto = layersProto.add_layers();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002029 writeToProtoDrawingState(layerProto);
chaviw08f3cb22020-01-13 13:17:21 -08002030 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2031
Vishnu Nair00b90132021-11-05 14:03:40 -07002032 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Dominik Laskowski298b08e2022-02-15 13:45:02 -08002033 ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread.
2034
Vishnu Nair60db8c02020-04-02 11:55:16 -07002035 // Only populate for the primary display.
Dominik Laskowski298b08e2022-02-15 13:45:02 -08002036 if (const auto display = mFlinger->getDefaultDisplayDeviceLocked()) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05002037 const auto compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002038 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002039 LayerProtoHelper::writeToProto(getVisibleRegion(display.get()),
2040 [&]() { return layerProto->mutable_visible_region(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002041 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002042 }
2043
chaviw08f3cb22020-01-13 13:17:21 -08002044 for (const sp<Layer>& layer : mDrawingChildren) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002045 layer->writeToProto(layersProto, traceFlags);
chaviw08f3cb22020-01-13 13:17:21 -08002046 }
chaviw6d89e2d2020-01-14 14:42:01 -08002047
2048 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002049}
2050
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08002051void Layer::writeToProtoDrawingState(LayerProto* layerInfo) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002052 const ui::Transform transform = getTransform();
Vishnu Naird37343b2022-01-12 16:18:56 -08002053 auto buffer = getExternalTexture();
Vishnu Nair00b90132021-11-05 14:03:40 -07002054 if (buffer != nullptr) {
Vishnu Naird37343b2022-01-12 16:18:56 -08002055 LayerProtoHelper::writeToProto(*buffer,
Vishnu Nair00b90132021-11-05 14:03:40 -07002056 [&]() { return layerInfo->mutable_active_buffer(); });
2057 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
2058 layerInfo->mutable_buffer_transform());
2059 }
2060 layerInfo->set_invalidate(contentDirty);
2061 layerInfo->set_is_protected(isProtected());
2062 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
2063 layerInfo->set_queued_frames(getQueuedFrameCount());
Vishnu Nair00b90132021-11-05 14:03:40 -07002064 layerInfo->set_curr_frame(mCurrentFrameNumber);
2065 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002066
Vishnu Nair00b90132021-11-05 14:03:40 -07002067 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nairadb70392022-07-11 15:04:07 -07002068 layerInfo->set_corner_radius(
2069 (getRoundedCornerState().radius.x + getRoundedCornerState().radius.y) / 2.0);
Vishnu Nair00b90132021-11-05 14:03:40 -07002070 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
2071 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
2072 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
2073 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2074 [&]() { return layerInfo->mutable_position(); });
2075 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair00b90132021-11-05 14:03:40 -07002076 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2077 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07002078
Vishnu Nair00b90132021-11-05 14:03:40 -07002079 if (hasColorTransform()) {
2080 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002081 }
2082
Vishnu Nair60db8c02020-04-02 11:55:16 -07002083 LayerProtoHelper::writeToProto(mSourceBounds,
2084 [&]() { return layerInfo->mutable_source_bounds(); });
2085 LayerProtoHelper::writeToProto(mScreenBounds,
2086 [&]() { return layerInfo->mutable_screen_bounds(); });
2087 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2088 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2089 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002090}
2091
2092void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002093 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002094 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2095 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002096 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002097
chaviw766c9c52021-02-10 17:36:47 -08002098 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002099
Vishnu Nair00b90132021-11-05 14:03:40 -07002100 layerInfo->set_id(sequence);
2101 layerInfo->set_name(getName().c_str());
2102 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002103
Vishnu Nair00b90132021-11-05 14:03:40 -07002104 for (const auto& child : children) {
2105 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002106 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002107
Vishnu Nair00b90132021-11-05 14:03:40 -07002108 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2109 sp<Layer> strongRelative = weakRelative.promote();
2110 if (strongRelative != nullptr) {
2111 layerInfo->add_relatives(strongRelative->sequence);
2112 }
2113 }
2114
2115 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2116 [&]() { return layerInfo->mutable_transparent_region(); });
2117
2118 layerInfo->set_layer_stack(getLayerStack().id);
2119 layerInfo->set_z(state.z);
2120
2121 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2122 return layerInfo->mutable_requested_position();
2123 });
2124
2125 LayerProtoHelper::writeSizeToProto(state.width, state.height,
2126 [&]() { return layerInfo->mutable_size(); });
2127
2128 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2129
2130 layerInfo->set_is_opaque(isOpaque(state));
2131
2132 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2133 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2134 LayerProtoHelper::writeToProto(state.color,
2135 [&]() { return layerInfo->mutable_requested_color(); });
2136 layerInfo->set_flags(state.flags);
2137
2138 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2139 layerInfo->mutable_requested_transform());
2140
2141 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2142 if (parent != nullptr) {
2143 layerInfo->set_parent(parent->sequence);
2144 } else {
2145 layerInfo->set_parent(-1);
2146 }
2147
2148 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2149 if (zOrderRelativeOf != nullptr) {
2150 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2151 } else {
2152 layerInfo->set_z_order_relative_of(-1);
2153 }
2154
2155 layerInfo->set_is_relative_of(state.isRelativeOf);
2156
2157 layerInfo->set_owner_uid(mOwnerUid);
2158
Arthur Hung4cf2d8c2022-04-07 14:52:00 +00002159 if ((traceFlags & LayerTracing::TRACE_INPUT) && needsInputInfo()) {
chaviw3277faf2021-05-19 16:45:23 -05002160 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002161 if (useDrawing) {
Prabir Pradhan83aca382022-07-22 19:53:04 +00002162 info = fillInputInfo(
2163 InputDisplayArgs{.transform = &kIdentityTransform, .isSecure = true});
chaviw4c34a092020-07-08 11:30:06 -07002164 } else {
2165 info = state.inputInfo;
2166 }
2167
2168 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002169 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002170 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002171
Vishnu Nair00b90132021-11-05 14:03:40 -07002172 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002173 auto protoMap = layerInfo->mutable_metadata();
2174 for (const auto& entry : state.metadata.mMap) {
2175 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2176 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002177 }
Vishnu Naird2aaab12022-02-10 14:49:09 -08002178
2179 LayerProtoHelper::writeToProto(state.destinationFrame,
2180 [&]() { return layerInfo->mutable_destination_frame(); });
chaviw1d044282017-09-27 12:19:28 -07002181}
2182
Robert Carr2e102c92018-10-23 12:11:15 -07002183bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002184 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002185}
2186
chaviw39d01472021-04-08 14:26:24 -05002187ui::Transform Layer::getInputTransform() const {
2188 return getTransform();
2189}
2190
2191Rect Layer::getInputBounds() const {
2192 return getCroppedBufferSize(getDrawingState());
2193}
2194
Prabir Pradhan33da9462022-06-14 14:55:57 +00002195// Applies the given transform to the region, while protecting against overflows caused by any
2196// offsets. If applying the offset in the transform to any of the Rects in the region would result
2197// in an overflow, they are not added to the output Region.
2198static Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
2199 const std::string& debugWindowName) {
2200 // Round the translation using the same rounding strategy used by ui::Transform.
2201 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
2202 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
2203
2204 ui::Transform transformWithoutOffset = t;
2205 transformWithoutOffset.set(0.f, 0.f);
2206
2207 const Region transformed = transformWithoutOffset.transform(r);
2208
2209 // Apply the translation to each of the Rects in the region while discarding any that overflow.
2210 Region ret;
2211 for (const auto& rect : transformed) {
2212 Rect newRect;
2213 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
2214 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
2215 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
2216 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
2217 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
2218 debugWindowName.c_str());
2219 continue;
2220 }
2221 ret.orSelf(newRect);
2222 }
2223 return ret;
2224}
2225
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002226void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& screenToDisplay) {
2227 Rect tmpBounds = getInputBounds();
2228 if (!tmpBounds.isValid()) {
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08002229 info.touchableRegion.clear();
2230 // A layer could have invalid input bounds and still expect to receive touch input if it has
2231 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
2232 // correctly to determine the coordinate space for input events. Use an empty rect so that
2233 // the layer will receive input in its own layer space.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002234 tmpBounds = Rect::EMPTY_RECT;
chaviw7e72caf2020-12-02 16:50:43 -08002235 }
2236
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002237 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
2238 // frame and transform used for the layer, which determines the bounds and the coordinate space
2239 // within which the layer will receive input.
2240 //
2241 // The coordinate space within which each of the bounds are specified is explicitly documented
2242 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
2243 // Transform converts one coordinate space to another, which is apparent in its naming. For
2244 // example, "layerToDisplay" transforms layer space to display space.
2245 //
2246 // Coordinate space definitions:
2247 // - display: The display device's coordinate space. Correlates to pixels on the display.
2248 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
2249 // - layer: The coordinate space of this layer.
2250 // - input: The coordinate space in which this layer will receive input events. This could be
2251 // different than layer space if a surfaceInset is used, which changes the origin
2252 // of the input space.
2253 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
chaviw7e72caf2020-12-02 16:50:43 -08002254
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002255 // Clamp surface inset to the input bounds.
2256 const auto surfaceInset = static_cast<float>(info.surfaceInset);
2257 const float xSurfaceInset =
2258 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
2259 const float ySurfaceInset =
2260 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
chaviw1ff3d1e2020-07-01 15:53:47 -07002261
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002262 // Apply the insets to the input bounds.
2263 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
2264 inputBoundsInLayer.top + ySurfaceInset,
2265 inputBoundsInLayer.right - xSurfaceInset,
2266 inputBoundsInLayer.bottom - ySurfaceInset);
Ady Abraham282f1d72019-07-24 18:05:56 -07002267
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002268 // Crop the input bounds to ensure it is within the parent's bounds.
2269 const FloatRect croppedInsetBoundsInLayer = mBounds.intersect(insetBoundsInLayer);
Ady Abraham282f1d72019-07-24 18:05:56 -07002270
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002271 const ui::Transform layerToScreen = getInputTransform();
2272 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
Vishnu Nair8033a492018-12-05 07:27:23 -08002273
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002274 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
2275 info.frameLeft = roundedFrameInDisplay.left;
2276 info.frameTop = roundedFrameInDisplay.top;
2277 info.frameRight = roundedFrameInDisplay.right;
2278 info.frameBottom = roundedFrameInDisplay.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -07002279
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002280 ui::Transform inputToLayer;
2281 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
2282 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
chaviw39cfa2e2020-11-04 14:19:02 -08002283
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002284 // InputDispatcher expects a display-to-input transform.
2285 info.transform = inputToDisplay.inverse();
2286
2287 // The touchable region is specified in the input coordinate space. Change it to display space.
Prabir Pradhan33da9462022-06-14 14:55:57 +00002288 info.touchableRegion =
2289 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, mName);
chaviw7e72caf2020-12-02 16:50:43 -08002290}
2291
chaviw3277faf2021-05-19 16:45:23 -05002292void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002293 sp<Layer> p = this;
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002294 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002295 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002296 }
2297 if (p != nullptr) {
2298 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2299 }
2300}
2301
Vishnu Naira066d902021-09-13 18:40:17 -07002302gui::DropInputMode Layer::getDropInputMode() const {
2303 gui::DropInputMode mode = mDrawingState.dropInputMode;
2304 if (mode == gui::DropInputMode::ALL) {
2305 return mode;
2306 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002307 sp<Layer> parent = mDrawingParent.promote();
2308 if (parent) {
2309 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002310 if (parentMode != gui::DropInputMode::NONE) {
2311 return parentMode;
2312 }
2313 }
2314 return mode;
2315}
2316
2317void Layer::handleDropInputMode(gui::WindowInfo& info) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002318 if (mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Vishnu Naira066d902021-09-13 18:40:17 -07002319 return;
2320 }
2321
2322 // Check if we need to drop input unconditionally
2323 gui::DropInputMode dropInputMode = getDropInputMode();
2324 if (dropInputMode == gui::DropInputMode::ALL) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002325 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002326 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2327 return;
2328 }
2329
2330 // Check if we need to check if the window is obscured by parent
2331 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2332 return;
2333 }
2334
2335 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002336 sp<Layer> parent = mDrawingParent.promote();
2337 if (parent && parent->getAlpha() != 1.0_hf) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002338 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002339 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2340 static_cast<float>(getAlpha()));
2341 }
2342
2343 // Check if the parent has cropped the buffer
2344 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2345 if (!bufferSize.isValid()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002346 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002347 return;
2348 }
2349
2350 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2351 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2352 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2353 // match then the layer has not been cropped by its parents.
2354 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2355 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2356
2357 if (croppedByParent) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002358 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002359 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2360 getDebugName());
2361 } else {
2362 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2363 // input if the window is obscured. This check should be done in surfaceflinger but the
2364 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2365 // drop input events if the window is obscured.
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002366 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002367 }
2368}
2369
Prabir Pradhan83aca382022-07-22 19:53:04 +00002370WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) {
chaviw7e72caf2020-12-02 16:50:43 -08002371 if (!hasInputInfo()) {
2372 mDrawingState.inputInfo.name = getName();
2373 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2374 mDrawingState.inputInfo.ownerPid = mOwnerPid;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002375 mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002376 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002377 }
2378
Prabir Pradhan83aca382022-07-22 19:53:04 +00002379 const ui::Transform& displayTransform =
2380 displayArgs.transform != nullptr ? *displayArgs.transform : kIdentityTransform;
2381
chaviw3277faf2021-05-19 16:45:23 -05002382 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002383 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002384 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002385
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002386 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002387
Prabir Pradhan83aca382022-07-22 19:53:04 +00002388 if (displayArgs.transform == nullptr) {
2389 // Do not let the window receive touches if it is not associated with a valid display
2390 // transform. We still allow the window to receive keys and prevent ANRs.
2391 info.inputConfig |= WindowInfo::InputConfig::NOT_TOUCHABLE;
2392 }
2393
Robert Carr5dc426e2020-06-10 14:29:14 -07002394 // For compatibility reasons we let layers which can receive input
2395 // receive input before they have actually submitted a buffer. Because
2396 // of this we use canReceiveInput instead of isVisible to check the
2397 // policy-visibility, ignoring the buffer state. However for layers with
2398 // hasInputInfo()==false we can use the real visibility state.
2399 // We are just using these layers for occlusion detection in
2400 // InputDispatcher, and obviously if they aren't visible they can't occlude
2401 // anything.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002402 const bool visible = hasInputInfo() ? canReceiveInput() : isVisible();
2403 info.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
2404
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002405 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002406 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002407 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002408
Vishnu Nair16a938f2021-09-24 07:14:54 -07002409 // If the window will be blacked out on a display because the display does not have the secure
2410 // flag and the layer has the secure flag set, then drop input.
Prabir Pradhan83aca382022-07-22 19:53:04 +00002411 if (!displayArgs.isSecure && isSecure()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002412 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Nair16a938f2021-09-24 07:14:54 -07002413 }
2414
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002415 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2416 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002417 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2418 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002419 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002420 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002421 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002422 }
2423
Winson Chunga30f7c92021-06-29 15:42:56 -07002424 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2425 // if it was set by WM for a known system overlay
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002426 if (isTrustedOverlay()) {
2427 info.inputConfig |= WindowInfo::InputConfig::TRUSTED_OVERLAY;
2428 }
Winson Chunga30f7c92021-06-29 15:42:56 -07002429
chaviwaf87b3e2019-10-01 16:59:28 -07002430 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2431 // touches from going outside the cloned area.
2432 if (isClone()) {
chaviwc3f39b12021-09-10 13:41:39 -05002433 info.isClone = true;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002434 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2435 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002436 info.touchableRegion = info.touchableRegion.intersect(rect);
2437 }
2438 }
2439
Robert Carr720e5062018-07-30 17:45:14 -07002440 return info;
2441}
2442
chaviwaf87b3e2019-10-01 16:59:28 -07002443sp<Layer> Layer::getClonedRoot() {
2444 if (mClonedChild != nullptr) {
2445 return this;
2446 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002447 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002448 return nullptr;
2449 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002450 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002451}
2452
Robert Carredd13602020-04-13 17:24:34 -07002453bool Layer::hasInputInfo() const {
Linus Tufvessona1858822022-03-04 09:32:07 +00002454 return mDrawingState.inputInfo.token != nullptr ||
2455 mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Robert Carr720e5062018-07-30 17:45:14 -07002456}
2457
chaviw3e727cd2019-01-31 13:41:05 -08002458bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002459 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002460}
2461
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002462compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002463 const DisplayDevice* display) const {
2464 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002465 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002466}
2467
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002468Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2469 const auto outputLayer = findOutputLayerForDisplay(display);
2470 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002471}
2472
chaviwb4c6e582019-08-16 14:35:07 -07002473void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002474 cloneDrawingState(clonedFrom.get());
chaviwb4c6e582019-08-16 14:35:07 -07002475 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002476}
chaviw74b03172019-08-19 11:09:03 -07002477
2478void Layer::updateMirrorInfo() {
2479 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2480 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2481 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2482 // that case, we want to delete the reference to the clone since we want it to get
2483 // destroyed. The root, this layer, will still be around since the client can continue
2484 // to hold a reference, but no cloned layers will be displayed.
2485 mClonedChild = nullptr;
2486 return;
2487 }
2488
2489 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2490 // If the real layer exists and is in current state, add the clone as a child of the root.
2491 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2492 // copied to drawingState for the root layer. So the clonedChild is always removed from
2493 // drawingState and then needs to be added back each traversal.
2494 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2495 addChildToDrawing(mClonedChild);
2496 }
2497
2498 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2499 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2500 mClonedChild->updateClonedRelatives(clonedLayersMap);
2501}
2502
2503void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2504 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2505 // to the clone. If the real layer is no longer alive, continue traversing the children
2506 // since we may be able to pull out other children that are still alive.
2507 if (isClonedFromAlive()) {
2508 sp<Layer> clonedFrom = getClonedFrom();
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002509 cloneDrawingState(clonedFrom.get());
chaviw74b03172019-08-19 11:09:03 -07002510 clonedLayersMap.emplace(clonedFrom, this);
2511 }
2512
2513 // The clone layer may have children in drawingState since they may have been created and
2514 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2515 // that already exist, since we can just re-use them.
2516 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2517 // not updated in the regular traversal. They are skipped since the root will lose the
2518 // reference to them when it copies its currentChildren to drawing.
2519 for (sp<Layer>& child : mDrawingChildren) {
2520 child->updateClonedDrawingState(clonedLayersMap);
2521 }
2522}
2523
2524void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2525 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2526 mDrawingChildren.clear();
2527
2528 if (!isClonedFromAlive()) {
2529 return;
2530 }
2531
2532 sp<Layer> clonedFrom = getClonedFrom();
2533 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2534 if (child == mirrorRoot) {
2535 // This is to avoid cyclical mirroring.
2536 continue;
2537 }
2538 sp<Layer> clonedChild = clonedLayersMap[child];
2539 if (clonedChild == nullptr) {
2540 clonedChild = child->createClone();
2541 clonedLayersMap[child] = clonedChild;
2542 }
2543 addChildToDrawing(clonedChild);
2544 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2545 }
2546}
2547
chaviwaf87b3e2019-10-01 16:59:28 -07002548void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2549 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2550 if (cropLayer != nullptr) {
2551 if (clonedLayersMap.count(cropLayer) == 0) {
2552 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2553 // self as crop layer to avoid going outside bounds.
2554 mDrawingState.touchableRegionCrop = this;
2555 } else {
2556 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2557 mDrawingState.touchableRegionCrop = clonedCropLayer;
2558 }
2559 }
2560 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2561 // WM or the client.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002562 mDrawingState.inputInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, false);
chaviwaf87b3e2019-10-01 16:59:28 -07002563}
2564
2565void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002566 mDrawingState.zOrderRelativeOf = nullptr;
2567 mDrawingState.zOrderRelatives.clear();
2568
2569 if (!isClonedFromAlive()) {
2570 return;
2571 }
2572
chaviwaf87b3e2019-10-01 16:59:28 -07002573 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002574 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002575 const sp<Layer>& relative = relativeWeak.promote();
2576 if (clonedLayersMap.count(relative) > 0) {
2577 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002578 mDrawingState.zOrderRelatives.add(clonedRelative);
2579 }
2580 }
2581
2582 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2583 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2584 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2585 // still traverse the children, but the layer with the missing relativeOf will not be shown
2586 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002587 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2588 if (clonedLayersMap.count(relativeOf) > 0) {
2589 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002590 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2591 }
2592
chaviwaf87b3e2019-10-01 16:59:28 -07002593 updateClonedInputInfo(clonedLayersMap);
2594
chaviw74b03172019-08-19 11:09:03 -07002595 for (sp<Layer>& child : mDrawingChildren) {
2596 child->updateClonedRelatives(clonedLayersMap);
2597 }
2598}
2599
2600void Layer::addChildToDrawing(const sp<Layer>& layer) {
2601 mDrawingChildren.add(layer);
2602 layer->mDrawingParent = this;
2603}
2604
Steven Thomas62a4cf82020-01-31 12:04:03 -08002605Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2606 switch (compatibility) {
2607 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2608 return FrameRateCompatibility::Default;
2609 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2610 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002611 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2612 return FrameRateCompatibility::Exact;
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002613 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
2614 return FrameRateCompatibility::NoVote;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002615 default:
2616 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2617 return FrameRateCompatibility::Default;
2618 }
2619}
2620
Marin Shalamanovc5986772021-03-16 16:09:49 +01002621scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2622 switch (strategy) {
2623 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2624 return Seamlessness::OnlySeamless;
2625 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2626 return Seamlessness::SeamedAndSeamless;
2627 default:
2628 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2629 return Seamlessness::Default;
2630 }
2631}
2632
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002633bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002634 const State& s(mDrawingState);
2635 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2636 return true;
2637 }
2638
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002639 sp<Layer> parent = mDrawingParent.promote();
2640 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002641}
2642
Robert Carr6a0382d2021-07-01 15:57:17 -07002643void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2644 mClonedChild = clonedChild;
2645 mHadClonedChild = true;
2646 mFlinger->mNumClones++;
2647}
2648
Vishnu Nairf9096652021-07-20 18:49:42 -07002649const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle");
2650
2651wp<Layer> Layer::fromHandle(const sp<IBinder>& handleBinder) {
2652 if (handleBinder == nullptr) {
2653 return nullptr;
2654 }
2655
2656 BBinder* b = handleBinder->localBinder();
2657 if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) {
2658 return nullptr;
2659 }
2660
2661 // We can safely cast this binder since its local and we verified its interface descriptor.
2662 sp<Handle> handle = static_cast<Handle*>(handleBinder.get());
2663 return handle->owner;
2664}
2665
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002666bool Layer::setDropInputMode(gui::DropInputMode mode) {
2667 if (mDrawingState.dropInputMode == mode) {
2668 return false;
2669 }
2670 mDrawingState.dropInputMode = mode;
2671 return true;
2672}
2673
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002674void Layer::cloneDrawingState(const Layer* from) {
2675 mDrawingState = from->mDrawingState;
2676 // Skip callback info since they are not applicable for cloned layers.
2677 mDrawingState.releaseBufferListener = nullptr;
2678 mDrawingState.callbackHandles = {};
2679}
2680
chaviw30fd9b92022-04-22 12:41:17 -05002681bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
2682 if (handles.empty()) {
2683 return false;
2684 }
2685
2686 for (const auto& handle : handles) {
2687 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
2688 }
2689
2690 return true;
2691}
2692
Mathias Agopian13127d82013-03-05 17:47:11 -08002693// ---------------------------------------------------------------------------
2694
Marin Shalamanov46084422020-10-13 12:33:42 +02002695std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07002696 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
2697 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02002698}
2699
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07002700} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002701
2702#if defined(__gl_h_)
2703#error "don't include gl/gl.h in this file"
2704#endif
2705
2706#if defined(__gl2_h_)
2707#error "don't include gl2/gl2.h in this file"
2708#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002709
2710// TODO(b/129481165): remove the #pragma below and fix conversion issues
2711#pragma clang diagnostic pop // ignored "-Wconversion"