blob: 67f9a92385c1ad4e81de075b8a1881284bbc4013 [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>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080040#include <gui/BufferItem.h>
41#include <gui/LayerDebugInfo.h>
42#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070043#include <math.h>
chaviw250bcbb2020-08-05 11:17:54 -070044#include <private/android_filesystem_config.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080045#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070046#include <stdint.h>
47#include <stdlib.h>
48#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080049#include <ui/DebugUtils.h>
50#include <ui/GraphicBuffer.h>
51#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/Errors.h>
53#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080054#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080056#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Alec Mourie60041e2019-06-14 18:59:51 -070058#include <algorithm>
59#include <mutex>
60#include <sstream>
61
Yiwei Zhang60d1a192018-03-07 14:52:28 -080062#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020063#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080065#include "DisplayHardware/HWComposer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080066#include "EffectLayer.h"
Ady Abraham22c7b5c2020-09-22 19:33:40 -070067#include "FrameTimeline.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070068#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080069#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070070#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070071#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080073#include "TimeStats/TimeStats.h"
Robert Carr3e2a2992021-06-11 13:42:55 -070074#include "TunnelModeEnabledReporter.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070075
David Sodman41fdfc92017-11-06 16:09:56 -080076#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010079namespace {
80constexpr int kDumpTableRowLength = 159;
81} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082
Yiwei Zhang5434a782018-12-05 18:06:32 -080083using base::StringAppendF;
Michael Wright44753b12020-07-08 13:48:11 +010084using namespace android::flag_operators;
Ady Abraham22c7b5c2020-09-22 19:33:40 -070085using PresentState = frametimeline::SurfaceFrame::PresentState;
chaviw3277faf2021-05-19 16:45:23 -050086using gui::WindowInfo;
Yiwei Zhang5434a782018-12-05 18:06:32 -080087
Lloyd Piquef1c675b2018-09-12 20:45:39 -070088std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080089
Lloyd Pique42ab75e2018-09-12 20:46:03 -070090Layer::Layer(const LayerCreationArgs& args)
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -080091 : sequence(args.sequence.value_or(sSequence++)),
92 mFlinger(args.flinger),
Arthur Hung23e07502021-10-15 11:58:19 +000093 mName(base::StringPrintf("%s#%d", args.name.c_str(), sequence)),
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070094 mClientRef(args.client),
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -080095 mWindowType(static_cast<WindowInfo::Type>(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0))),
96 mLayerCreationFlags(args.flags) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070097 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070098 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
99 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
100 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -0700101 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
102 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700103
Robert Carr6a160312021-05-17 12:08:20 -0700104 mDrawingState.flags = layerFlags;
105 mDrawingState.active_legacy.transform.set(0, 0);
106 mDrawingState.crop.makeInvalid();
107 mDrawingState.requestedCrop = mDrawingState.crop;
108 mDrawingState.z = 0;
109 mDrawingState.color.a = 1.0f;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700110 mDrawingState.layerStack = ui::DEFAULT_LAYER_STACK;
Robert Carr6a160312021-05-17 12:08:20 -0700111 mDrawingState.sequence = 0;
112 mDrawingState.requested_legacy = mDrawingState.active_legacy;
113 mDrawingState.width = UINT32_MAX;
114 mDrawingState.height = UINT32_MAX;
115 mDrawingState.transform.set(0, 0);
116 mDrawingState.frameNumber = 0;
117 mDrawingState.bufferTransform = 0;
118 mDrawingState.transformToDisplayInverse = false;
119 mDrawingState.crop.makeInvalid();
120 mDrawingState.acquireFence = sp<Fence>::make(-1);
121 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
122 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
123 mDrawingState.hdrMetadata.validTypes = 0;
124 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
125 mDrawingState.cornerRadius = 0.0f;
126 mDrawingState.backgroundBlurRadius = 0;
127 mDrawingState.api = -1;
128 mDrawingState.hasColorTransform = false;
129 mDrawingState.colorSpaceAgnostic = false;
130 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
131 mDrawingState.metadata = args.metadata;
132 mDrawingState.shadowRadius = 0.f;
133 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
134 mDrawingState.frameTimelineInfo = {};
135 mDrawingState.postTime = -1;
136 mDrawingState.destinationFrame.makeInvalid();
Robin Leeedb375d2021-09-10 12:03:42 +0000137 mDrawingState.isTrustedOverlay = false;
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -0700138 mDrawingState.dropInputMode = gui::DropInputMode::NONE;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700139
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700140 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
141 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700142 mDrawingState.color.r = -1.0_hf;
143 mDrawingState.color.g = -1.0_hf;
144 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700145 }
146
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800147 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700148 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800149 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200150 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700151
Vishnu Nair0f085c62019-08-30 08:49:12 -0700152 mCallingPid = args.callingPid;
153 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700154
155 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
156 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
157 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700158 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700159 } else {
160 // A create layer request from a non system request cannot specify the owner uid
161 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700162 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700163 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800164}
165
166void Layer::onFirstRef() {
167 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700168}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700170Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800171 sp<Client> c(mClientRef.promote());
172 if (c != 0) {
173 c->detachLayer(this);
174 }
175
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000176 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700177 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700178
179 if (mDrawingState.sidebandStream != nullptr) {
180 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
181 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700182 if (mHadClonedChild) {
183 mFlinger->mNumClones--;
184 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700185}
186
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700187LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800188 uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700189 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700190 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700191 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700192 flags(flags),
193 metadata(std::move(metadata)) {
194 IPCThreadState* ipc = IPCThreadState::self();
195 callingPid = ipc->getCallingPid();
196 callingUid = ipc->getCallingUid();
197}
198
Mathias Agopian13127d82013-03-05 17:47:11 -0800199// ---------------------------------------------------------------------------
200// callbacks
201// ---------------------------------------------------------------------------
202
David Sodmaneb085e02017-10-05 18:49:04 -0700203/*
204 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
205 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800206 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700207 */
Sally Qi59a9f502021-10-12 18:53:23 +0000208void Layer::onLayerDisplayed(
209 std::shared_future<renderengine::RenderEngineResult> /*futureRenderEngineResult*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800210
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700211void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700212 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700213 return;
214 }
Robert Carr2e102c92018-10-23 12:11:15 -0700215
Robert Carr6a160312021-05-17 12:08:20 -0700216 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700217 if (strongRelative == nullptr) {
218 setZOrderRelativeOf(nullptr);
219 return;
220 }
221
222 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
223 strongRelative->removeZOrderRelative(this);
224 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800225 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700226 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700227}
228
229void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700230 if (!mRemovedFromDrawingState) {
231 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700232 mFlinger->mScheduler->deregisterLayer(this);
233 }
Rob Carr4bba3702018-10-08 21:53:30 +0000234
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800235 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700236}
Chia-I Wu38512252017-05-17 14:36:16 -0700237
chaviw68d4dab2020-06-08 15:07:32 -0700238sp<Layer> Layer::getRootLayer() {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000239 sp<Layer> parent = getParent();
chaviw68d4dab2020-06-08 15:07:32 -0700240 if (parent == nullptr) {
241 return this;
242 }
243 return parent->getRootLayer();
244}
245
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700246void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700247 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
248 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700249 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700250
251 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700252 layer->removeFromCurrentState();
253 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700254 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700255}
256
chaviw61626f22018-11-15 16:26:27 -0800257void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700258 if (mRemovedFromDrawingState) {
259 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700260 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700261 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700262 }
chaviw61626f22018-11-15 16:26:27 -0800263
264 for (const auto& child : mCurrentChildren) {
265 child->addToCurrentState();
266 }
267}
268
Mathias Agopian13127d82013-03-05 17:47:11 -0800269// ---------------------------------------------------------------------------
270// set-up
271// ---------------------------------------------------------------------------
272
chaviw13fdc492017-06-27 12:40:18 -0700273bool Layer::getPremultipledAlpha() const {
274 return mPremultipliedAlpha;
275}
276
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700277sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800278 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700279 if (mGetHandleCalled) {
280 ALOGE("Get handle called twice" );
281 return nullptr;
282 }
283 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700284 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800285}
286
287// ---------------------------------------------------------------------------
288// h/w composer set-up
289// ---------------------------------------------------------------------------
290
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700291static Rect reduce(const Rect& win, const Region& exclude) {
292 if (CC_LIKELY(exclude.isEmpty())) {
293 return win;
294 }
295 if (exclude.isRect()) {
296 return win.reduce(exclude.getBounds());
297 }
298 return Region(win).subtract(exclude).getBounds();
299}
300
Dan Stoza80d61162017-12-20 15:57:52 -0800301static FloatRect reduce(const FloatRect& win, const Region& exclude) {
302 if (CC_LIKELY(exclude.isEmpty())) {
303 return win;
304 }
305 // Convert through Rect (by rounding) for lack of FloatRegion
306 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
307}
308
Vishnu Nair4351ad52019-02-11 14:13:02 -0800309Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800310 if (!reduceTransparentRegion) {
311 return Rect{mScreenBounds};
312 }
313
314 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800315 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800316 // Transform to screen space.
317 bounds = t.transform(bounds);
318 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700319}
320
Vishnu Nair4351ad52019-02-11 14:13:02 -0800321FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000322 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800323 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700324}
325
Vishnu Nairf0c28512019-02-08 12:40:28 -0800326FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
327 // Subtract the transparent region and snap to the bounds.
328 return reduce(mBounds, activeTransparentRegion);
329}
330
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700331void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
332 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800333 const State& s(getDrawingState());
334
335 // Calculate effective layer transform
336 mEffectiveTransform = parentTransform * getActiveTransform(s);
337
338 // Transform parent bounds to layer space
339 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
340
Vishnu Nairc652ff82019-03-15 12:48:54 -0700341 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800342 mSourceBounds = computeSourceBounds(parentBounds);
343
344 // Calculate bounds by croping diplay frame with layer crop and parent bounds
345 FloatRect bounds = mSourceBounds;
346 const Rect layerCrop = getCrop(s);
347 if (!layerCrop.isEmpty()) {
348 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
349 }
350 bounds = bounds.intersect(parentBounds);
351
352 mBounds = bounds;
353 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700354
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700355 // Use the layer's own shadow radius if set. Otherwise get the radius from
356 // parent.
357 if (s.shadowRadius > 0.f) {
358 mEffectiveShadowRadius = s.shadowRadius;
359 } else {
360 mEffectiveShadowRadius = parentShadowRadius;
361 }
362
363 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
364 // don't pass it to its children.
365 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
366
Vishnu Nair4351ad52019-02-11 14:13:02 -0800367 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700368 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800369 }
370}
371
Vishnu Nair60356342018-11-13 13:00:45 -0800372Rect Layer::getCroppedBufferSize(const State& s) const {
373 Rect size = getBufferSize(s);
374 Rect crop = getCrop(s);
375 if (!crop.isEmpty() && size.isValid()) {
376 size.intersect(crop, &size);
377 } else if (!crop.isEmpty()) {
378 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700379 }
Vishnu Nair60356342018-11-13 13:00:45 -0800380 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800381}
382
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700383void Layer::setupRoundedCornersCropCoordinates(Rect win,
384 const FloatRect& roundedCornersCrop) const {
385 // Translate win by the rounded corners rect coordinates, to have all values in
386 // layer coordinate space.
387 win.left -= roundedCornersCrop.left;
388 win.right -= roundedCornersCrop.left;
389 win.top -= roundedCornersCrop.top;
390 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700391}
392
Lloyd Piquede196652020-01-22 17:29:58 -0800393void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800394 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800395 const auto alpha = static_cast<float>(getAlpha());
396 const bool opaque = isOpaque(drawingState);
397 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
398
399 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
400 if (!opaque || alpha != 1.0f) {
401 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
402 : Hwc2::IComposerClient::BlendMode::COVERAGE;
403 }
404
Lloyd Piquede196652020-01-22 17:29:58 -0800405 auto* compositionState = editCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700406 compositionState->outputFilter = getOutputFilter();
Lloyd Piquede196652020-01-22 17:29:58 -0800407 compositionState->isVisible = isVisible();
408 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
409 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800410
Lloyd Piquede196652020-01-22 17:29:58 -0800411 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800412 contentDirty = false;
413
Lloyd Piquede196652020-01-22 17:29:58 -0800414 compositionState->geomLayerBounds = mBounds;
415 compositionState->geomLayerTransform = getTransform();
416 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
417 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800418
Lloyd Piquede196652020-01-22 17:29:58 -0800419 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
420 compositionState->alpha = alpha;
John Reckc00c6692021-02-16 11:37:33 -0500421 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800422}
423
Lloyd Piquede196652020-01-22 17:29:58 -0800424void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800425 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800426 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700427
Lloyd Piquede196652020-01-22 17:29:58 -0800428 compositionState->geomBufferSize = getBufferSize(drawingState);
429 compositionState->geomContentCrop = getBufferCrop();
430 compositionState->geomCrop = getCrop(drawingState);
431 compositionState->geomBufferTransform = getBufferTransform();
432 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
433 compositionState->geomUsesSourceCrop = usesSourceCrop();
434 compositionState->isSecure = isSecure();
435
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800436 compositionState->metadata.clear();
437 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
438 for (const auto& [key, mandatory] : supportedMetadata) {
439 const auto& genericLayerMetadataCompatibilityMap =
440 mFlinger->getGenericLayerMetadataKeyMap();
441 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
442 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
443 continue;
444 }
445 const uint32_t id = compatIter->second;
446
447 auto it = drawingState.metadata.mMap.find(id);
448 if (it == std::end(drawingState.metadata.mMap)) {
449 continue;
450 }
451
452 compositionState->metadata
453 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
454 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800455}
David Sodmanba340492018-08-05 21:51:33 -0700456
Lloyd Piquede196652020-01-22 17:29:58 -0800457void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800458 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800459 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800460
Lloyd Piquede196652020-01-22 17:29:58 -0800461 compositionState->forceClientComposition = false;
462
463 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
464 compositionState->dataspace = getDataSpace();
465 compositionState->colorTransform = getColorTransform();
466 compositionState->colorTransformIsIdentity = !hasColorTransform();
467 compositionState->surfaceDamage = surfaceDamageRegion;
468 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800469
470 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700471
Lloyd Piquede196652020-01-22 17:29:58 -0800472 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800473 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800474
475 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400476 // Rounded corners no longer force client composition, since we may use a
477 // hole punch so that the layer will appear to have rounded corners.
478 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500479 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800480 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 }
Vishnu Nairb801a982021-11-02 15:12:08 -0700482 // If there are no visible region changes, we still need to update blur parameters.
483 compositionState->blurRegions = drawingState.blurRegions;
484 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lloyd Piquef5275482019-01-29 18:42:42 -0800485}
486
Lloyd Piquede196652020-01-22 17:29:58 -0800487void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800488 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800489 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800490
491 // Apply the layer's transform, followed by the display's global transform
492 // Here we're guaranteed that the layer's transform preserves rects
493 Rect win = getCroppedBufferSize(drawingState);
494 // Subtract the transparent region and snap to the bounds
495 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
496 Rect frame(getTransform().transform(bounds));
497
Lloyd Piquede196652020-01-22 17:29:58 -0800498 compositionState->cursorFrame = frame;
499}
500
501sp<compositionengine::LayerFE> Layer::asLayerFE() const {
502 return const_cast<compositionengine::LayerFE*>(
503 static_cast<const compositionengine::LayerFE*>(this));
504}
505
506sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
507 return nullptr;
508}
509
510compositionengine::LayerFECompositionState* Layer::editCompositionState() {
511 return nullptr;
512}
513
514const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
515 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800516}
517
Lloyd Piquef16688f2019-02-19 17:47:57 -0800518bool Layer::onPreComposition(nsecs_t) {
519 return false;
520}
521
Lloyd Piquede196652020-01-22 17:29:58 -0800522void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800523 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800524
Lloyd Piquec6687342019-03-07 21:34:57 -0800525 switch (subset) {
526 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800527 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800528 break;
529
530 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800531 prepareBasicGeometryCompositionState();
532 prepareGeometryCompositionState();
533 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800534 break;
535
536 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800537 preparePerFrameCompositionState();
538 break;
539
540 case StateSubset::Cursor:
541 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800542 break;
543 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800544}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700545
Lloyd Piquea83776c2019-01-29 18:42:32 -0800546const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700547 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800548}
549
Mathias Agopian13127d82013-03-05 17:47:11 -0800550// ---------------------------------------------------------------------------
551// drawing...
552// ---------------------------------------------------------------------------
553
Vishnu Nair9b079a22020-01-21 14:36:08 -0800554std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100555 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800556 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800557 return {};
558 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800559
Vishnu Nair4351ad52019-02-11 14:13:02 -0800560 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000561 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800562
563 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800564 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700565 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000566
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400567 // skip drawing content if the targetSettings indicate the content will be occluded
Ady Abrahambaa6cf52021-07-13 14:02:19 -0700568 const bool drawContent = targetSettings.realContentIsVisible || targetSettings.clearContent;
569 layerSettings.skipContentDraw = !drawContent;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400570
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000571 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800572 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000573 }
574
575 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800576 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
577 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000578
Lloyd Piquef16688f2019-02-19 17:47:57 -0800579 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700580 layerSettings.sourceDataspace = getDataSpace();
Alec Mourif54453c2021-05-13 16:28:28 -0700581 switch (targetSettings.blurSetting) {
582 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Enabled:
583 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
584 layerSettings.blurRegions = getBlurRegions();
585 layerSettings.blurRegionTransform =
586 getActiveTransform(getDrawingState()).inverse().asMatrix4();
587 break;
588 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BackgroundBlurOnly:
589 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
590 break;
591 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BlurRegionsOnly:
592 layerSettings.blurRegions = getBlurRegions();
593 layerSettings.blurRegionTransform =
594 getActiveTransform(getDrawingState()).inverse().asMatrix4();
595 break;
596 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled:
597 default:
598 break;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100599 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700600 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800601 // Record the name of the layer for debugging further down the stack.
602 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800603 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800604}
605
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800606void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
607 bool blackout) const {
608 layerSettings.source.buffer.buffer = nullptr;
609 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
610 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800611 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800612 layerSettings.frameNumber = 0;
613
614 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
615 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800616 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800617}
618
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400619// TODO(b/188891810): This method now only ever returns 0 or 1 layers so we should return
620// std::optional instead of a vector. Additionally, we should consider removing
621// this method entirely in favor of calling prepareClientComposition directly.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800622std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
623 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
624 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
625 prepareClientComposition(targetSettings);
626 // Nothing to render.
627 if (!layerSettings) {
628 return {};
629 }
630
631 // HWC requests to clear this layer.
632 if (targetSettings.clearContent) {
633 prepareClearClientComposition(*layerSettings, false /* blackout */);
634 return {*layerSettings};
635 }
636
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400637 // set the shadow for the layer if needed
638 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800639
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400640 return {*layerSettings};
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800641}
642
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700643Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
644 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800645 if (outputLayer == nullptr) {
646 return Hwc2::IComposerClient::Composition::INVALID;
647 }
648 if (outputLayer->getState().hwc) {
649 return (*outputLayer->getState().hwc).hwcCompositionType;
650 } else {
651 return Hwc2::IComposerClient::Composition::CLIENT;
652 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653}
654
Mathias Agopian13127d82013-03-05 17:47:11 -0800655// ----------------------------------------------------------------------------
656// local state
657// ----------------------------------------------------------------------------
658
David Sodman41fdfc92017-11-06 16:09:56 -0800659bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800660 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700661 if (s.flags & layer_state_t::eLayerSecure) {
662 return true;
663 }
664
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000665 const auto p = mDrawingParent.promote();
Garfield Tande619fa2020-10-02 17:13:53 -0700666 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700667}
668
Mathias Agopian13127d82013-03-05 17:47:11 -0800669// ----------------------------------------------------------------------------
670// transaction
671// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800672
Marissa Wall61c58622018-07-18 10:12:20 -0700673uint32_t Layer::doTransaction(uint32_t flags) {
674 ATRACE_CALL();
675
Robert Carr0758e5d2021-03-11 22:15:04 -0800676 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700677 mDrawingStateModified = mDrawingState.modified;
678 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700679
Alec Mourib416efd2018-09-06 21:01:59 +0000680 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700681
Robert Carr6a160312021-05-17 12:08:20 -0700682 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800683 // invalidate and recompute the visible regions if needed
684 flags |= Layer::eVisibleRegion;
685 }
686
Robert Carr6a160312021-05-17 12:08:20 -0700687 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800688 // invalidate and recompute the visible regions if needed
Robert Carr6a160312021-05-17 12:08:20 -0700689 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800690 flags |= eVisibleRegion;
691 this->contentDirty = true;
692
693 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700694 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800695 }
696
Robert Carr6a160312021-05-17 12:08:20 -0700697 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800698
Mathias Agopian13127d82013-03-05 17:47:11 -0800699 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700}
701
Robert Carr6a160312021-05-17 12:08:20 -0700702void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000703 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
704 // bufferSurfaceFrameTX will be presented in latchBuffer.
705 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
706 if (surfaceFrame->getPresentState() != PresentState::Presented) {
707 // With applyPendingStates, we could end up having presented surfaceframes from previous
708 // states
709 surfaceFrame->setPresentState(PresentState::Presented);
710 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
711 }
712 }
Robert Carr6a160312021-05-17 12:08:20 -0700713 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700714}
715
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700716uint32_t Layer::clearTransactionFlags(uint32_t mask) {
717 const auto flags = mTransactionFlags & mask;
718 mTransactionFlags &= ~mask;
719 return flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800720}
721
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700722void Layer::setTransactionFlags(uint32_t mask) {
723 mTransactionFlags |= mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800724}
725
chaviw214c89d2019-09-04 16:03:53 -0700726bool Layer::setPosition(float x, float y) {
Robert Carr6a160312021-05-17 12:08:20 -0700727 if (mDrawingState.transform.tx() == x && mDrawingState.transform.ty() == y) return false;
728 mDrawingState.sequence++;
729 mDrawingState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700730
Robert Carr6a160312021-05-17 12:08:20 -0700731 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800732 setTransactionFlags(eTransactionNeeded);
733 return true;
734}
Robert Carr82364e32016-05-15 11:27:47 -0700735
Robert Carr1f0a16a2016-10-24 16:27:39 -0700736bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
737 ssize_t idx = mCurrentChildren.indexOf(childLayer);
738 if (idx < 0) {
739 return false;
740 }
741 if (childLayer->setLayer(z)) {
742 mCurrentChildren.removeAt(idx);
743 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800744 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700745 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800746 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700747}
748
Robert Carr503c7042017-09-27 15:06:08 -0700749bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
750 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
751 ssize_t idx = mCurrentChildren.indexOf(childLayer);
752 if (idx < 0) {
753 return false;
754 }
755 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
756 mCurrentChildren.removeAt(idx);
757 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800758 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700759 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800760 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700761}
762
Robert Carrae060832016-11-28 10:51:00 -0800763bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700764 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
765 mDrawingState.sequence++;
766 mDrawingState.z = z;
767 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700768
Robert Carra70e91c2021-06-11 13:59:52 -0700769 mFlinger->mSomeChildrenChanged = true;
770
Robert Carrdb66e622017-04-10 16:55:57 -0700771 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700772 if (mDrawingState.zOrderRelativeOf != nullptr) {
773 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700774 if (strongRelative != nullptr) {
775 strongRelative->removeZOrderRelative(this);
776 }
chaviw606e5cf2019-03-01 10:12:10 -0800777 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700778 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800779 setTransactionFlags(eTransactionNeeded);
780 return true;
781}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700782
Robert Carrdb66e622017-04-10 16:55:57 -0700783void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700784 mDrawingState.zOrderRelatives.remove(relative);
785 mDrawingState.sequence++;
786 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700787 setTransactionFlags(eTransactionNeeded);
788}
789
790void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700791 mDrawingState.zOrderRelatives.add(relative);
792 mDrawingState.modified = true;
793 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700794 setTransactionFlags(eTransactionNeeded);
795}
796
chaviw606e5cf2019-03-01 10:12:10 -0800797void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700798 mDrawingState.zOrderRelativeOf = relativeOf;
799 mDrawingState.sequence++;
800 mDrawingState.modified = true;
801 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700802
chaviw606e5cf2019-03-01 10:12:10 -0800803 setTransactionFlags(eTransactionNeeded);
804}
805
Robert Carr503d2bd2017-12-04 15:49:47 -0800806bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nairf9096652021-07-20 18:49:42 -0700807 sp<Layer> relative = fromHandle(relativeToHandle).promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700808 if (relative == nullptr) {
809 return false;
810 }
811
Robert Carr6a160312021-05-17 12:08:20 -0700812 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
813 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800814 return false;
815 }
816
Robert Carra70e91c2021-06-11 13:59:52 -0700817 mFlinger->mSomeChildrenChanged = true;
818
Robert Carr6a160312021-05-17 12:08:20 -0700819 mDrawingState.sequence++;
820 mDrawingState.modified = true;
821 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700822
Robert Carr6a160312021-05-17 12:08:20 -0700823 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700824 if (oldZOrderRelativeOf != nullptr) {
825 oldZOrderRelativeOf->removeZOrderRelative(this);
826 }
chaviw606e5cf2019-03-01 10:12:10 -0800827 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700828 relative->addZOrderRelative(this);
829
830 setTransactionFlags(eTransactionNeeded);
831
832 return true;
833}
834
Winson Chunga30f7c92021-06-29 15:42:56 -0700835bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
836 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
837 mDrawingState.isTrustedOverlay = isTrustedOverlay;
838 mDrawingState.modified = true;
839 mFlinger->mInputInfoChanged = true;
840 setTransactionFlags(eTransactionNeeded);
841 return true;
842}
843
844bool Layer::isTrustedOverlay() const {
845 if (getDrawingState().isTrustedOverlay) {
846 return true;
847 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000848 const auto& p = mDrawingParent.promote();
Winson Chunga30f7c92021-06-29 15:42:56 -0700849 return (p != nullptr) && p->isTrustedOverlay();
850}
851
Mathias Agopian13127d82013-03-05 17:47:11 -0800852bool Layer::setSize(uint32_t w, uint32_t h) {
Robert Carr6a160312021-05-17 12:08:20 -0700853 if (mDrawingState.requested_legacy.w == w && mDrawingState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700854 return false;
Robert Carr6a160312021-05-17 12:08:20 -0700855 mDrawingState.requested_legacy.w = w;
856 mDrawingState.requested_legacy.h = h;
857 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800858 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700859
860 // record the new size, from this point on, when the client request
861 // a buffer, it'll get the new size.
Robert Carr6a160312021-05-17 12:08:20 -0700862 setDefaultBufferSize(mDrawingState.requested_legacy.w, mDrawingState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800863 return true;
864}
chaviw4e765532021-04-30 12:11:39 -0500865
Dan Stoza9e56aa02015-11-02 13:00:03 -0800866bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700867 if (mDrawingState.color.a == alpha) return false;
868 mDrawingState.sequence++;
869 mDrawingState.color.a = alpha;
870 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800871 setTransactionFlags(eTransactionNeeded);
872 return true;
873}
chaviw13fdc492017-06-27 12:40:18 -0700874
Valerie Haudd0b7572019-01-29 14:59:27 -0800875bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700876 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700877 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800878 }
Robert Carr6a160312021-05-17 12:08:20 -0700879 mDrawingState.sequence++;
880 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800881 setTransactionFlags(eTransactionNeeded);
882
Robert Carr6a160312021-05-17 12:08:20 -0700883 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800884 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800885 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700886 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700887 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800888 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700889 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700890
Valerie Haudd0b7572019-01-29 14:59:27 -0800891 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700892 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800893 mFlinger->mLayersAdded = true;
894 // set up SF to handle added color layer
895 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700896 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800897 }
898 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700899 } else if (mDrawingState.bgColorLayer && alpha == 0) {
900 mDrawingState.bgColorLayer->reparent(nullptr);
901 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800902 return true;
903 }
904
Robert Carr6a160312021-05-17 12:08:20 -0700905 mDrawingState.bgColorLayer->setColor(color);
906 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
907 mDrawingState.bgColorLayer->setAlpha(alpha);
908 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800909
chaviw13fdc492017-06-27 12:40:18 -0700910 return true;
911}
912
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700913bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700914 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700915
Robert Carr6a160312021-05-17 12:08:20 -0700916 mDrawingState.sequence++;
917 mDrawingState.cornerRadius = cornerRadius;
918 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700919 setTransactionFlags(eTransactionNeeded);
920 return true;
921}
922
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800923bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700924 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Vishnu Nairb801a982021-11-02 15:12:08 -0700925 // If we start or stop drawing blur then the layer's visibility state may change so increment
926 // the magic sequence number.
927 if (mDrawingState.backgroundBlurRadius == 0 || backgroundBlurRadius == 0) {
928 mDrawingState.sequence++;
929 }
Robert Carr6a160312021-05-17 12:08:20 -0700930 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
931 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800932 setTransactionFlags(eTransactionNeeded);
933 return true;
934}
935
Robert Carrd4ae7f32018-06-07 16:10:57 -0700936bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
937 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700938 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -0700939 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
940
941 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -0800942 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
943 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -0700944 return false;
945 }
Robert Carr6a160312021-05-17 12:08:20 -0700946 mDrawingState.sequence++;
947 mDrawingState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
948 mDrawingState.modified = true;
949
Mathias Agopian13127d82013-03-05 17:47:11 -0800950 setTransactionFlags(eTransactionNeeded);
951 return true;
952}
Marissa Wall61c58622018-07-18 10:12:20 -0700953
Mathias Agopian13127d82013-03-05 17:47:11 -0800954bool Layer::setTransparentRegionHint(const Region& transparent) {
Robert Carr6a160312021-05-17 12:08:20 -0700955 mDrawingState.requestedTransparentRegion_legacy = transparent;
956 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800957 setTransactionFlags(eTransactionNeeded);
958 return true;
959}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100960
Lucas Dupinc3800b82020-10-02 16:24:48 -0700961bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Vishnu Nairb801a982021-11-02 15:12:08 -0700962 // If we start or stop drawing blur then the layer's visibility state may change so increment
963 // the magic sequence number.
964 if (mDrawingState.blurRegions.size() == 0 || blurRegions.size() == 0) {
965 mDrawingState.sequence++;
966 }
Robert Carr6a160312021-05-17 12:08:20 -0700967 mDrawingState.blurRegions = blurRegions;
968 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700969 setTransactionFlags(eTransactionNeeded);
970 return true;
971}
972
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800973bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700974 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
975 if (mDrawingState.flags == newFlags) return false;
976 mDrawingState.sequence++;
977 mDrawingState.flags = newFlags;
978 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800979 setTransactionFlags(eTransactionNeeded);
980 return true;
981}
Robert Carr99e27f02016-06-16 15:18:02 -0700982
chaviw25714502021-02-11 10:01:08 -0800983bool Layer::setCrop(const Rect& crop) {
Robert Carr6a160312021-05-17 12:08:20 -0700984 if (mDrawingState.requestedCrop == crop) return false;
985 mDrawingState.sequence++;
986 mDrawingState.requestedCrop = crop;
987 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700988
Robert Carr6a160312021-05-17 12:08:20 -0700989 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800990 setTransactionFlags(eTransactionNeeded);
991 return true;
992}
Robert Carr8d5227b2017-03-16 15:41:03 -0700993
Evan Roskyef876c92019-01-25 17:46:06 -0800994bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700995 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
996 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -0800997 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800998 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500999}
1000
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001001bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -07001002 if (mDrawingState.layerStack == layerStack) return false;
1003 mDrawingState.sequence++;
1004 mDrawingState.layerStack = layerStack;
1005 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001006 setTransactionFlags(eTransactionNeeded);
1007 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001008}
1009
Peiyong Linc502cb72019-03-01 15:00:23 -08001010bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -07001011 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -08001012 return false;
1013 }
Robert Carr6a160312021-05-17 12:08:20 -07001014 mDrawingState.sequence++;
1015 mDrawingState.colorSpaceAgnostic = agnostic;
1016 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -08001017 setTransactionFlags(eTransactionNeeded);
1018 return true;
1019}
1020
Ana Krulecc84d09b2019-11-02 23:10:29 +01001021bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -07001022 if (mDrawingState.frameRateSelectionPriority == priority) return false;
1023 mDrawingState.frameRateSelectionPriority = priority;
1024 mDrawingState.sequence++;
1025 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +01001026 setTransactionFlags(eTransactionNeeded);
1027 return true;
1028}
1029
1030int32_t Layer::getFrameRateSelectionPriority() const {
1031 // Check if layer has priority set.
1032 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1033 return mDrawingState.frameRateSelectionPriority;
1034 }
1035 // If not, search whether its parents have it set.
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001036 sp<Layer> parent = getParent();
Ana Krulecc84d09b2019-11-02 23:10:29 +01001037 if (parent != nullptr) {
1038 return parent->getFrameRateSelectionPriority();
1039 }
1040
1041 return Layer::PRIORITY_UNSET;
1042}
1043
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001044bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1045 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1046};
1047
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001048ui::LayerStack Layer::getLayerStack() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001049 if (const auto parent = mDrawingParent.promote()) {
1050 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001051 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001052 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001053}
1054
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001055bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001056 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001057 return false;
1058 }
1059
Robert Carr6a160312021-05-17 12:08:20 -07001060 mDrawingState.sequence++;
1061 mDrawingState.shadowRadius = shadowRadius;
1062 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001063 setTransactionFlags(eTransactionNeeded);
1064 return true;
1065}
1066
Vishnu Nair6213bd92020-05-08 17:42:25 -07001067bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001068 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001069 return false;
1070 }
1071
Robert Carr6a160312021-05-17 12:08:20 -07001072 mDrawingState.sequence++;
1073 mDrawingState.fixedTransformHint = fixedTransformHint;
1074 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001075 setTransactionFlags(eTransactionNeeded);
1076 return true;
1077}
1078
John Reckcdb4ed72021-02-04 13:39:33 -05001079bool Layer::setStretchEffect(const StretchEffect& effect) {
1080 StretchEffect temp = effect;
1081 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001082 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001083 return false;
1084 }
Robert Carr6a160312021-05-17 12:08:20 -07001085 mDrawingState.sequence++;
1086 mDrawingState.stretchEffect = temp;
1087 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001088 setTransactionFlags(eTransactionNeeded);
1089 return true;
1090}
1091
John Reckc00c6692021-02-16 11:37:33 -05001092StretchEffect Layer::getStretchEffect() const {
1093 if (mDrawingState.stretchEffect.hasEffect()) {
1094 return mDrawingState.stretchEffect;
1095 }
1096
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001097 sp<Layer> parent = getParent();
John Reckc00c6692021-02-16 11:37:33 -05001098 if (parent != nullptr) {
1099 auto effect = parent->getStretchEffect();
1100 if (effect.hasEffect()) {
1101 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1102 return effect;
1103 }
1104 }
1105 return StretchEffect{};
1106}
1107
Ady Abrahama850c182021-08-04 13:04:37 -07001108bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1109 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1110 const auto frameRate = [&] {
1111 if (mDrawingState.frameRate.rate.isValid() ||
1112 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1113 return mDrawingState.frameRate;
1114 }
1115
1116 return parentFrameRate;
1117 }();
1118
1119 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1120
1121 // The frame rate is propagated to the children
1122 bool childrenHaveFrameRate = false;
1123 for (const sp<Layer>& child : mCurrentChildren) {
1124 childrenHaveFrameRate |=
1125 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1126 }
1127
1128 // If we don't have a valid frame rate, but the children do, we set this
1129 // layer as NoVote to allow the children to control the refresh rate
1130 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1131 childrenHaveFrameRate) {
1132 *transactionNeeded |=
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001133 setFrameRateForLayerTree(FrameRate(Fps(), FrameRateCompatibility::NoVote));
Ady Abrahama850c182021-08-04 13:04:37 -07001134 }
1135
1136 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1137 // the same reason we are allowing touch boost for those layers. See
1138 // RefreshRateConfigs::getBestRefreshRate for more details.
1139 const auto layerVotedWithDefaultCompatibility =
1140 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1141 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1142 const auto layerVotedWithExactCompatibility =
1143 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1144 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1145 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1146}
1147
Ady Abraham60e42ea2020-03-09 19:17:31 -07001148void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001149 const auto root = [&]() -> sp<Layer> {
1150 sp<Layer> layer = this;
1151 while (auto parent = layer->getParent()) {
1152 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001153 }
Ady Abrahama850c182021-08-04 13:04:37 -07001154 return layer;
1155 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001156
Ady Abraham60e42ea2020-03-09 19:17:31 -07001157 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001158 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001159
Ady Abrahama850c182021-08-04 13:04:37 -07001160 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001161 if (transactionNeeded) {
1162 mFlinger->setTransactionFlags(eTraversalNeeded);
1163 }
1164}
1165
Ady Abraham71c437d2020-01-31 15:56:57 -08001166bool Layer::setFrameRate(FrameRate frameRate) {
Robert Carr6a160312021-05-17 12:08:20 -07001167 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001168 return false;
1169 }
1170
Robert Carr6a160312021-05-17 12:08:20 -07001171 mDrawingState.sequence++;
1172 mDrawingState.frameRate = frameRate;
1173 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001174
1175 updateTreeHasFrameRateVote();
1176
Steven Thomas3172e202020-01-06 19:25:30 -08001177 setTransactionFlags(eTransactionNeeded);
1178 return true;
1179}
1180
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001181void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1182 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001183 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001184
1185 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1186 // there are two transactions with the same token, the first one without a buffer and the
1187 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1188 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001189 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1190 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001191 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001192 mDrawingState.bufferSurfaceFrameTX = it->second;
1193 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1194 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1195 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001196 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001197 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001198 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1199 }
1200}
1201
1202void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1203 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001204 mDrawingState.frameTimelineInfo = info;
1205 mDrawingState.postTime = postTime;
1206 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001207 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001208
Robert Carr6a160312021-05-17 12:08:20 -07001209 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001210 bufferSurfaceFrameTX != nullptr) {
1211 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1212 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1213 // being used for BufferSurfaceFrame, don't create a new one.
1214 return;
1215 }
1216 }
1217 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1218 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1219 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001220 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1221 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001222 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001223 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001224 } else {
1225 if (it->second->getPresentState() == PresentState::Presented) {
1226 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1227 // have been from previous vsync.
1228 it->second = createSurfaceFrameForTransaction(info, postTime);
1229 }
1230 }
1231}
1232
1233void Layer::addSurfaceFrameDroppedForBuffer(
1234 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001235 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001236 surfaceFrame->setPresentState(PresentState::Dropped);
1237 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1238}
1239
1240void Layer::addSurfaceFramePresentedForBuffer(
1241 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1242 nsecs_t currentLatchTime) {
1243 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1244 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1245 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1246 mLastLatchTime = currentLatchTime;
1247}
1248
1249std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1250 const FrameTimelineInfo& info, nsecs_t postTime) {
1251 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001252 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1253 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001254 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001255 /*isBuffer*/ false, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001256 // For Transactions, the post time is considered to be both queue and acquire fence time.
1257 surfaceFrame->setActualQueueTime(postTime);
1258 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001259 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1260 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001261 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001262 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001263 onSurfaceFrameCreated(surfaceFrame);
1264 return surfaceFrame;
1265}
1266
1267std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1268 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1269 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001270 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001271 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001272 /*isBuffer*/ true, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001273 // For buffers, acquire fence time will set during latch.
1274 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001275 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1276 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001277 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001278 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001279 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1280 onSurfaceFrameCreated(surfaceFrame);
1281 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001282}
1283
Ady Abrahama850c182021-08-04 13:04:37 -07001284bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1285 if (mDrawingState.frameRateForLayerTree == frameRate) {
1286 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001287 }
1288
Ady Abrahama850c182021-08-04 13:04:37 -07001289 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001290
Ady Abrahama850c182021-08-04 13:04:37 -07001291 // TODO(b/195668952): we probably don't need to dirty visible regions here
1292 // or even store frameRateForLayerTree in mDrawingState
1293 mDrawingState.sequence++;
1294 mDrawingState.modified = true;
1295 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001296
Ady Abrahama850c182021-08-04 13:04:37 -07001297 mFlinger->mScheduler->recordLayerHistory(this, systemTime(),
1298 LayerHistory::LayerUpdateType::SetFrameRate);
1299
1300 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001301}
1302
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001303Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1304 return getDrawingState().frameRateForLayerTree;
1305}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306
Robert Carr1f0a16a2016-10-24 16:27:39 -07001307bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001308 const State& s(mDrawingState);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001309 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001310 if (parent != nullptr && parent->isHiddenByPolicy()) {
1311 return true;
1312 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001313 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1314 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1315 if (zOrderRelativeOf != nullptr) {
1316 if (zOrderRelativeOf->isHiddenByPolicy()) {
1317 return true;
1318 }
1319 }
1320 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001321 return s.flags & layer_state_t::eLayerHidden;
1322}
1323
David Sodman41fdfc92017-11-06 16:09:56 -08001324uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001325 // TODO: should we do something special if mSecure is set?
1326 if (mProtectedByApp) {
1327 // need a hardware-protected path to external video sink
1328 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001329 }
Riley Andrews03414a12014-07-01 14:22:59 -07001330 if (mPotentialCursor) {
1331 usage |= GraphicBuffer::USAGE_CURSOR;
1332 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001333 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001334 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001335}
1336
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001337void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1338 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1339 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001340 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001341
Vishnu Nair6213bd92020-05-08 17:42:25 -07001342 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001343}
1344
Mathias Agopian13127d82013-03-05 17:47:11 -08001345// ----------------------------------------------------------------------------
1346// debugging
1347// ----------------------------------------------------------------------------
1348
Marissa Wall61c58622018-07-18 10:12:20 -07001349// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001350LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001351 using namespace std::string_literals;
1352
Kalle Raitaa099a242017-01-11 11:17:29 -08001353 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001354 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001355 info.mName = getName();
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001356 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001357 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001358 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001359 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001360
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001361 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001362 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001363 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001364 info.mX = ds.transform.tx();
1365 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001366 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001367 info.mWidth = ds.width;
1368 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001369 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001370 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001371 info.mFlags = ds.flags;
1372 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001373 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001374 info.mMatrix[0][0] = ds.transform[0][0];
1375 info.mMatrix[0][1] = ds.transform[0][1];
1376 info.mMatrix[1][0] = ds.transform[1][0];
1377 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001378 {
chaviwd62d3062019-09-04 14:48:02 -07001379 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001380 if (buffer != 0) {
1381 info.mActiveBufferWidth = buffer->getWidth();
1382 info.mActiveBufferHeight = buffer->getHeight();
1383 info.mActiveBufferStride = buffer->getStride();
1384 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001385 } else {
1386 info.mActiveBufferWidth = 0;
1387 info.mActiveBufferHeight = 0;
1388 info.mActiveBufferStride = 0;
1389 info.mActiveBufferFormat = 0;
1390 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001391 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001392 info.mNumQueuedFrames = getQueuedFrameCount();
1393 info.mRefreshPending = isBufferLatched();
1394 info.mIsOpaque = isOpaque(ds);
1395 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001396 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001397 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001398}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001399
Yiwei Zhang5434a782018-12-05 18:06:32 -08001400void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001401 result.append(kDumpTableRowLength, '-');
1402 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001403 result.append(" Layer name\n");
1404 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001405 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001406 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001407 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001408 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001409 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001410 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1411 result.append(kDumpTableRowLength, '-');
1412 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001413}
1414
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001415void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1416 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001417 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001418 return;
1419 }
1420
Yiwei Zhang5434a782018-12-05 18:06:32 -08001421 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001422 if (mName.length() > 77) {
1423 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001424 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001425 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001426 shortened.append(mName, mName.length() - 36);
1427 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001428 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001429 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001430 }
1431
Yiwei Zhang5434a782018-12-05 18:06:32 -08001432 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001433
Alec Mourib416efd2018-09-06 21:01:59 +00001434 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001435 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001436
Chia-I Wu1e043612018-03-01 09:45:09 -08001437 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001438 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001439 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001440 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001441 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001442 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001443 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001444 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1445 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001446 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001447 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001448 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001449 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001450 const auto frameRate = getFrameRateForLayerTree();
1451 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1452 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001453 ftl::enum_string(frameRate.type).c_str(),
1454 ftl::enum_string(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001455 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001456 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001457 }
Dan Stozae22aec72016-08-01 13:20:59 -07001458
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001459 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1460 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1461
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001462 result.append(kDumpTableRowLength, '-');
1463 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001464}
Dan Stozae22aec72016-08-01 13:20:59 -07001465
Yiwei Zhang5434a782018-12-05 18:06:32 -08001466void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001467 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001468}
1469
Svetoslavd85084b2014-03-20 10:28:31 -07001470void Layer::clearFrameStats() {
1471 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001472}
1473
Jamie Gennis6547ff42013-07-16 20:12:42 -07001474void Layer::logFrameStats() {
1475 mFrameTracker.logAndResetStats(mName);
1476}
1477
Svetoslavd85084b2014-03-20 10:28:31 -07001478void Layer::getFrameStats(FrameStats* outStats) const {
1479 mFrameTracker.getStats(outStats);
1480}
1481
Yiwei Zhang5434a782018-12-05 18:06:32 -08001482void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001483 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001484 Mutex::Autolock lock(mFrameEventHistoryMutex);
1485 mFrameEventHistory.checkFencesForCompletion();
1486 mFrameEventHistory.dump(result);
1487}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001488
Vishnu Nair0f085c62019-08-30 08:49:12 -07001489void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001490 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1491 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001492}
1493
Brian Anderson5ea5e592016-12-01 16:54:33 -08001494void Layer::onDisconnect() {
1495 Mutex::Autolock lock(mFrameEventHistoryMutex);
1496 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001497 const int32_t layerId = getSequence();
1498 mFlinger->mTimeStats->onDestroy(layerId);
1499 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001500}
1501
Brian Anderson3890c392016-07-25 12:48:08 -07001502void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001503 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001504 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001505 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001506 getName().c_str(), mOwnerUid, newTimestamps->postedTime,
1507 getGameMode());
Yiwei Zhang487340f2019-11-18 17:42:12 -08001508 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1509 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001510 }
1511
Brian Andersond6927fb2016-07-23 23:37:30 -07001512 Mutex::Autolock lock(mFrameEventHistoryMutex);
1513 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001514 // If there are any unsignaled fences in the aquire timeline at this
1515 // point, the previously queued frame hasn't been latched yet. Go ahead
1516 // and try to get the signal time here so the syscall is taken out of
1517 // the main thread's critical path.
1518 mAcquireTimeline.updateSignalTimes();
1519 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001520 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001521 mFrameEventHistory.addQueue(*newTimestamps);
1522 }
1523
Brian Anderson3890c392016-07-25 12:48:08 -07001524 if (outDelta) {
1525 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001526 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001527}
Dan Stozae77c7662016-05-13 11:37:28 -07001528
Chia-I Wu98f1c102017-05-30 14:54:08 -07001529size_t Layer::getChildrenCount() const {
1530 size_t count = 0;
1531 for (const sp<Layer>& child : mCurrentChildren) {
1532 count += 1 + child->getChildrenCount();
1533 }
1534 return count;
1535}
1536
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001537void Layer::setGameModeForTree(GameMode gameMode) {
1538 const auto& currentState = getDrawingState();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001539 if (currentState.metadata.has(METADATA_GAME_MODE)) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001540 gameMode = static_cast<GameMode>(currentState.metadata.getInt32(METADATA_GAME_MODE, 0));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001541 }
1542 setGameMode(gameMode);
1543 for (const sp<Layer>& child : mCurrentChildren) {
1544 child->setGameModeForTree(gameMode);
1545 }
1546}
1547
Robert Carr1f0a16a2016-10-24 16:27:39 -07001548void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001549 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001550 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001551
Robert Carr1f0a16a2016-10-24 16:27:39 -07001552 mCurrentChildren.add(layer);
1553 layer->setParent(this);
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001554 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001555 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001556}
1557
1558ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001559 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001560 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001561
Robert Carr1323c952019-01-28 18:13:27 -08001562 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001563 const auto removeResult = mCurrentChildren.remove(layer);
1564
1565 updateTreeHasFrameRateVote();
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001566 layer->setGameModeForTree(GameMode::Unsupported);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001567 layer->updateTreeHasFrameRateVote();
1568
1569 return removeResult;
1570}
1571
Robert Carr15eae092018-03-23 13:43:53 -07001572void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001573 for (const sp<Layer>& child : mDrawingChildren) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001574 child->mDrawingParent = newParent;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001575 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001576 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001577 }
1578}
1579
chaviwf1961f72017-09-18 16:41:07 -07001580bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001581 sp<Layer> newParent;
1582 if (newParentHandle != nullptr) {
Vishnu Nairf9096652021-07-20 18:49:42 -07001583 newParent = fromHandle(newParentHandle).promote();
Robert Carr54cf5b12019-01-25 14:02:28 -08001584 if (newParent == nullptr) {
1585 ALOGE("Unable to promote Layer handle");
1586 return false;
1587 }
1588 if (newParent == this) {
1589 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1590 return false;
1591 }
1592 }
1593
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001594 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001595 if (parent != nullptr) {
1596 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001597 }
1598
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001599 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001600 newParent->addChild(this);
1601 if (!newParent->isRemovedFromCurrentState()) {
1602 addToCurrentState();
1603 } else {
1604 onRemovedFromCurrentState();
1605 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001606 } else {
1607 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001608 }
1609
chaviw06178942017-07-27 10:25:59 -07001610 return true;
1611}
1612
Peiyong Lind3788632018-09-18 16:01:31 -07001613bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001614 static const mat4 identityMatrix = mat4();
1615
Robert Carr6a160312021-05-17 12:08:20 -07001616 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001617 return false;
1618 }
Robert Carr6a160312021-05-17 12:08:20 -07001619 ++mDrawingState.sequence;
1620 mDrawingState.colorTransform = matrix;
1621 mDrawingState.hasColorTransform = matrix != identityMatrix;
1622 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001623 setTransactionFlags(eTransactionNeeded);
1624 return true;
1625}
1626
chaviwf66724d2018-11-28 16:35:21 -08001627mat4 Layer::getColorTransform() const {
1628 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001629 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001630 colorTransform = parent->getColorTransform() * colorTransform;
1631 }
1632 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001633}
1634
1635bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001636 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001637 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001638 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1639 }
1640 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001641}
1642
Chia-I Wu11481472018-05-04 10:43:19 -07001643bool Layer::isLegacyDataSpace() const {
1644 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001645 return !(getDataSpace() &
1646 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1647 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001648}
1649
Robert Carr1f0a16a2016-10-24 16:27:39 -07001650void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001651 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001652}
1653
Robert Carr6a160312021-05-17 12:08:20 -07001654int32_t Layer::getZ(LayerVector::StateSet) const {
1655 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656}
1657
Robert Carr1c5481e2019-07-01 14:42:27 -07001658bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001659 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001660 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001661 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001662}
1663
David Sodman41fdfc92017-11-06 16:09:56 -08001664__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001665 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001666 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1667 "makeTraversalList received invalid stateSet");
1668 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1669 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001670 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001671
Robert Carr29abff82017-12-04 13:51:20 -08001672 if (state.zOrderRelatives.size() == 0) {
1673 *outSkipRelativeZUsers = true;
1674 return children;
1675 }
1676
chaviwfd462612018-05-31 16:11:27 -07001677 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001678 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001679 sp<Layer> strongRelative = weakRelative.promote();
1680 if (strongRelative != nullptr) {
1681 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001682 }
1683 }
1684
Dan Stoza412903f2017-04-27 13:42:17 -07001685 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001686 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001687 continue;
1688 }
Robert Carrdb66e622017-04-10 16:55:57 -07001689 traverse.add(child);
1690 }
1691
1692 return traverse;
1693}
1694
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695/**
Robert Carrdb66e622017-04-10 16:55:57 -07001696 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001697 */
Dan Stoza412903f2017-04-27 13:42:17 -07001698void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001699 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1700 // produce a new list for traversing, including our relatives, and not including our children
1701 // who are relatives of another surface. In the case that there are no relative Z,
1702 // makeTraversalList returns our children directly to avoid significant overhead.
1703 // However in this case we need to take the responsibility for filtering children which
1704 // are relatives of another surface here.
1705 bool skipRelativeZUsers = false;
1706 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001707
Robert Carr1f0a16a2016-10-24 16:27:39 -07001708 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001709 for (; i < list.size(); i++) {
1710 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001711 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1712 continue;
1713 }
1714
chaviw301b1d82019-11-06 13:15:09 -08001715 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001716 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001717 }
Dan Stoza412903f2017-04-27 13:42:17 -07001718 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001719 }
Robert Carr29abff82017-12-04 13:51:20 -08001720
Dan Stoza412903f2017-04-27 13:42:17 -07001721 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001722 for (; i < list.size(); i++) {
1723 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001724
Robert Carr29abff82017-12-04 13:51:20 -08001725 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1726 continue;
1727 }
Dan Stoza412903f2017-04-27 13:42:17 -07001728 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001729 }
1730}
1731
1732/**
Robert Carrdb66e622017-04-10 16:55:57 -07001733 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001734 */
Dan Stoza412903f2017-04-27 13:42:17 -07001735void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1736 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001737 // See traverseInZOrder for documentation.
1738 bool skipRelativeZUsers = false;
1739 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001740
Robert Carr1f0a16a2016-10-24 16:27:39 -07001741 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001742 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001743 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001744
1745 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1746 continue;
1747 }
1748
chaviw301b1d82019-11-06 13:15:09 -08001749 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001750 break;
1751 }
Dan Stoza412903f2017-04-27 13:42:17 -07001752 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001753 }
Dan Stoza412903f2017-04-27 13:42:17 -07001754 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001755 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001756 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001757
1758 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1759 continue;
1760 }
1761
Dan Stoza412903f2017-04-27 13:42:17 -07001762 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763 }
1764}
1765
Edgar Arriaga844fa672020-01-16 14:21:42 -08001766void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1767 visitor(this);
1768 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001769 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001770 for (const sp<Layer>& child : children) {
1771 child->traverse(state, visitor);
1772 }
1773}
1774
chaviw4b129c22018-04-09 16:19:43 -07001775LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1776 const std::vector<Layer*>& layersInTree) {
1777 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1778 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001779 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1780 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001781 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001782
chaviwfd462612018-05-31 16:11:27 -07001783 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001784 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1785 sp<Layer> strongRelative = weakRelative.promote();
1786 // Only add relative layers that are also descendents of the top most parent of the tree.
1787 // If a relative layer is not a descendent, then it should be ignored.
1788 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1789 traverse.add(strongRelative);
1790 }
1791 }
1792
1793 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001794 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001795 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1796 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1797 // the child here since it won't be added later as a relative.
1798 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1799 childState.zOrderRelativeOf.promote().get())) {
1800 continue;
1801 }
1802 traverse.add(child);
1803 }
1804
1805 return traverse;
1806}
1807
1808void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1809 LayerVector::StateSet stateSet,
1810 const LayerVector::Visitor& visitor) {
1811 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001812
1813 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001814 for (; i < list.size(); i++) {
1815 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001816 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001817 break;
1818 }
chaviw4b129c22018-04-09 16:19:43 -07001819 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001820 }
chaviw4b129c22018-04-09 16:19:43 -07001821
chaviwa76b2712017-09-20 12:02:26 -07001822 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001823 for (; i < list.size(); i++) {
1824 const auto& relative = list[i];
1825 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001826 }
1827}
1828
chaviw4b129c22018-04-09 16:19:43 -07001829std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1830 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1831 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1832
1833 std::vector<Layer*> layersInTree = {this};
1834 for (size_t i = 0; i < children.size(); i++) {
1835 const auto& child = children[i];
1836 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1837 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1838 }
1839
1840 return layersInTree;
1841}
1842
1843void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1844 const LayerVector::Visitor& visitor) {
1845 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1846 std::sort(layersInTree.begin(), layersInTree.end());
1847 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1848}
1849
Peiyong Linefefaac2018-08-17 12:27:51 -07001850ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001851 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001852}
1853
chaviw13fdc492017-06-27 12:40:18 -07001854half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001855 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001856
chaviw13fdc492017-06-27 12:40:18 -07001857 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1858 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001859}
Robert Carr6452f122017-03-21 10:41:29 -07001860
Vishnu Nair6213bd92020-05-08 17:42:25 -07001861ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001862 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001863 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1864 return fixedTransformHint;
1865 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001866 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001867 if (!p) return fixedTransformHint;
1868 return p->getFixedTransformHint();
1869}
1870
chaviw13fdc492017-06-27 12:40:18 -07001871half4 Layer::getColor() const {
1872 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001873 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001874}
Robert Carr6452f122017-03-21 10:41:29 -07001875
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001876int32_t Layer::getBackgroundBlurRadius() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001877 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001878
1879 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1880 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001881}
1882
Galia Peychevae0acf382021-04-12 21:22:34 +02001883const std::vector<BlurRegion> Layer::getBlurRegions() const {
1884 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001885 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001886 for (auto& region : regionsCopy) {
1887 region.alpha = region.alpha * layerAlpha;
1888 }
1889 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001890}
1891
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001892Layer::RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001893 // Get parent settings
1894 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001895 const auto& parent = mDrawingParent.promote();
1896 if (parent != nullptr) {
1897 parentSettings = parent->getRoundedCornerState();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001898 if (parentSettings.radius > 0) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001899 ui::Transform t = getActiveTransform(getDrawingState());
1900 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001901 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001902 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1903 // but a transform matrix can define horizontal and vertical scales.
1904 // Let's take the average between both of them and pass into the shader, practically we
1905 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07001906 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
1907 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001908 parentSettings.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001909 }
1910 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001911
1912 // Get layer settings
1913 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001914 const float radius = getDrawingState().cornerRadius;
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001915 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nairb1845592021-10-07 12:17:57 -07001916 const bool layerSettingsValid = layerSettings.radius > 0 && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001917
Vishnu Nairb1845592021-10-07 12:17:57 -07001918 if (layerSettingsValid && parentSettings.radius > 0) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001919 // If the parent and the layer have rounded corner settings, use the parent settings if the
1920 // parent crop is entirely inside the layer crop.
1921 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1922 if (parentSettings.cropRect.left > layerCropRect.left &&
1923 parentSettings.cropRect.top > layerCropRect.top &&
1924 parentSettings.cropRect.right < layerCropRect.right &&
1925 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1926 return parentSettings;
1927 } else {
1928 return layerSettings;
1929 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001930 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001931 return layerSettings;
1932 } else if (parentSettings.radius > 0) {
1933 return parentSettings;
1934 }
1935 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001936}
1937
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001938void Layer::prepareShadowClientComposition(LayerFE::LayerSettings& caster,
1939 const Rect& layerStackRect) {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001940 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1941
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001942 // Note: this preserves existing behavior of shadowing the entire layer and not cropping it if
1943 // transparent regions are present. This may not be necessary since shadows are only cast by
1944 // SurfaceFlinger's EffectLayers, which do not typically use transparent regions.
1945 state.boundaries = mBounds;
1946
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001947 // Shift the spot light x-position to the middle of the display and then
1948 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001949 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001950 state.lightPos.y -= mScreenBounds.top;
1951
1952 state.length = mEffectiveShadowRadius;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001953
1954 if (state.length > 0.f) {
1955 const float casterAlpha = caster.alpha;
1956 const bool casterIsOpaque =
1957 ((caster.source.buffer.buffer != nullptr) && caster.source.buffer.isOpaque);
1958
1959 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
1960 // Otherwise the generated shadow will only be shown around the casting layer.
1961 state.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
1962 state.ambientColor *= casterAlpha;
1963 state.spotColor *= casterAlpha;
1964
1965 if (state.ambientColor.a > 0.f && state.spotColor.a > 0.f) {
1966 caster.shadow = state;
1967 }
1968 }
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001969}
1970
Robert Carr1f0a16a2016-10-24 16:27:39 -07001971void Layer::commitChildList() {
1972 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1973 const auto& child = mCurrentChildren[i];
1974 child->commitChildList();
1975 }
1976 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001977 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001978}
1979
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001980
chaviw3277faf2021-05-19 16:45:23 -05001981void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001982 mDrawingState.inputInfo = info;
Vishnu Nairf9096652021-07-20 18:49:42 -07001983 mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07001984 mDrawingState.modified = true;
Robert Carrbf14dbb2021-06-11 13:26:56 -07001985 mFlinger->mInputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001986 setTransactionFlags(eTransactionNeeded);
1987}
1988
Alec Mouri6b9e9912020-01-21 10:50:24 -08001989LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07001990 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08001991 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001992 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08001993 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1994
Vishnu Nair00b90132021-11-05 14:03:40 -07001995 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Vishnu Nair60db8c02020-04-02 11:55:16 -07001996 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001997 if (display) {
1998 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07001999 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2000 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002001 }
2002
chaviw08f3cb22020-01-13 13:17:21 -08002003 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002004 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002005 }
chaviw6d89e2d2020-01-14 14:42:01 -08002006
2007 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002008}
2009
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002010void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002011 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002012 const ui::Transform transform = getTransform();
Vishnu Nair00b90132021-11-05 14:03:40 -07002013 auto buffer = getBuffer();
2014 if (buffer != nullptr) {
2015 LayerProtoHelper::writeToProto(buffer,
2016 [&]() { return layerInfo->mutable_active_buffer(); });
2017 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
2018 layerInfo->mutable_buffer_transform());
2019 }
2020 layerInfo->set_invalidate(contentDirty);
2021 layerInfo->set_is_protected(isProtected());
2022 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
2023 layerInfo->set_queued_frames(getQueuedFrameCount());
2024 layerInfo->set_refresh_pending(isBufferLatched());
2025 layerInfo->set_curr_frame(mCurrentFrameNumber);
2026 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002027
Vishnu Nair00b90132021-11-05 14:03:40 -07002028 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
2029 layerInfo->set_corner_radius(getRoundedCornerState().radius);
2030 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
2031 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
2032 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
2033 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2034 [&]() { return layerInfo->mutable_position(); });
2035 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
2036 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
2037 LayerProtoHelper::writeToProto(getVisibleRegion(display),
2038 [&]() { return layerInfo->mutable_visible_region(); });
2039 }
2040 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2041 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07002042
Vishnu Nair00b90132021-11-05 14:03:40 -07002043 if (hasColorTransform()) {
2044 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002045 }
2046
Vishnu Nair60db8c02020-04-02 11:55:16 -07002047 LayerProtoHelper::writeToProto(mSourceBounds,
2048 [&]() { return layerInfo->mutable_source_bounds(); });
2049 LayerProtoHelper::writeToProto(mScreenBounds,
2050 [&]() { return layerInfo->mutable_screen_bounds(); });
2051 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2052 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2053 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002054}
2055
2056void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002057 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002058 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2059 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002060 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002061
chaviw766c9c52021-02-10 17:36:47 -08002062 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002063
Vishnu Nair00b90132021-11-05 14:03:40 -07002064 layerInfo->set_id(sequence);
2065 layerInfo->set_name(getName().c_str());
2066 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002067
Vishnu Nair00b90132021-11-05 14:03:40 -07002068 for (const auto& child : children) {
2069 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002070 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002071
Vishnu Nair00b90132021-11-05 14:03:40 -07002072 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2073 sp<Layer> strongRelative = weakRelative.promote();
2074 if (strongRelative != nullptr) {
2075 layerInfo->add_relatives(strongRelative->sequence);
2076 }
2077 }
2078
2079 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2080 [&]() { return layerInfo->mutable_transparent_region(); });
2081
2082 layerInfo->set_layer_stack(getLayerStack().id);
2083 layerInfo->set_z(state.z);
2084
2085 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2086 return layerInfo->mutable_requested_position();
2087 });
2088
2089 LayerProtoHelper::writeSizeToProto(state.width, state.height,
2090 [&]() { return layerInfo->mutable_size(); });
2091
2092 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2093
2094 layerInfo->set_is_opaque(isOpaque(state));
2095
2096 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2097 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2098 LayerProtoHelper::writeToProto(state.color,
2099 [&]() { return layerInfo->mutable_requested_color(); });
2100 layerInfo->set_flags(state.flags);
2101
2102 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2103 layerInfo->mutable_requested_transform());
2104
2105 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2106 if (parent != nullptr) {
2107 layerInfo->set_parent(parent->sequence);
2108 } else {
2109 layerInfo->set_parent(-1);
2110 }
2111
2112 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2113 if (zOrderRelativeOf != nullptr) {
2114 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2115 } else {
2116 layerInfo->set_z_order_relative_of(-1);
2117 }
2118
2119 layerInfo->set_is_relative_of(state.isRelativeOf);
2120
2121 layerInfo->set_owner_uid(mOwnerUid);
2122
2123 if (traceFlags & LayerTracing::TRACE_INPUT) {
chaviw3277faf2021-05-19 16:45:23 -05002124 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002125 if (useDrawing) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07002126 info = fillInputInfo(ui::Transform(), /* displayIsSecure */ true);
chaviw4c34a092020-07-08 11:30:06 -07002127 } else {
2128 info = state.inputInfo;
2129 }
2130
2131 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002132 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002133 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002134
Vishnu Nair00b90132021-11-05 14:03:40 -07002135 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002136 auto protoMap = layerInfo->mutable_metadata();
2137 for (const auto& entry : state.metadata.mMap) {
2138 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2139 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002140 }
chaviw1d044282017-09-27 12:19:28 -07002141}
2142
Robert Carr2e102c92018-10-23 12:11:15 -07002143bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002144 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002145}
2146
chaviw39d01472021-04-08 14:26:24 -05002147ui::Transform Layer::getInputTransform() const {
2148 return getTransform();
2149}
2150
2151Rect Layer::getInputBounds() const {
2152 return getCroppedBufferSize(getDrawingState());
2153}
2154
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002155void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& displayTransform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002156 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002157 // If this is a portal window, set the touchableRegion to the layerBounds.
2158 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002159 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002160 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002161 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002162 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002163 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002164
chaviw7e72caf2020-12-02 16:50:43 -08002165 if (!layerBounds.isValid()) {
2166 // If the layer bounds is empty, set the frame to empty and clear the transform
2167 info.frameLeft = 0;
2168 info.frameTop = 0;
2169 info.frameRight = 0;
2170 info.frameBottom = 0;
2171 info.transform.reset();
2172 return;
2173 }
2174
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002175 const ui::Transform layerTransform = getInputTransform();
Prabir Pradhan31e76ed2021-08-16 12:01:57 -07002176 // Transform that takes window coordinates to non-rotated display coordinates
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002177 ui::Transform t = displayTransform * layerTransform;
chaviw7e72caf2020-12-02 16:50:43 -08002178 int32_t xSurfaceInset = info.surfaceInset;
2179 int32_t ySurfaceInset = info.surfaceInset;
Prabir Pradhan31e76ed2021-08-16 12:01:57 -07002180 // Bring screenBounds into non-unrotated space
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002181 Rect screenBounds = displayTransform.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002182
chaviw1ff3d1e2020-07-01 15:53:47 -07002183 const float xScale = t.getScaleX();
2184 const float yScale = t.getScaleY();
2185 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002186 xSurfaceInset = std::round(xSurfaceInset * xScale);
2187 ySurfaceInset = std::round(ySurfaceInset * yScale);
2188 }
2189
Peiyong Lin74861ad2020-10-14 11:55:33 -07002190 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002191 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002192
2193 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002194 xSurfaceInset = (xSurfaceInset >= 0)
2195 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2196 : 0;
2197 ySurfaceInset = (ySurfaceInset >= 0)
2198 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2199 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002200
Robert Carra7242302020-09-01 18:26:29 -07002201 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2202 // in the overflow scenario?
2203 {
2204 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002205 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2206 transformedLayerBounds.left = tmp;
2207 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2208 transformedLayerBounds.right = tmp;
2209 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2210 transformedLayerBounds.top = tmp;
2211 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2212 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002213 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002214
chaviw44a6d2b2020-09-08 17:14:16 -07002215 // Compute the correct transform to send to input. This will allow it to transform the
2216 // input coordinates from display space into window space. Therefore, it needs to use the
2217 // final layer frame to create the inverse transform. Since surface insets are added later,
2218 // along with the overflow, the best way to ensure we get the correct transform is to use
2219 // the final frame calculated.
2220 // 1. Take the original transform set on the window and get the inverse transform. This is
2221 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002222 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002223 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2224 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2225 // be the new translation values for the transform.
2226 // 3. Update the translation of the original transform to the new translation values.
2227 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2228 // window space.
2229 ui::Transform inverseTransform = t.inverse();
2230 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2231 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002232 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002233 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002234 info.transform = inputTransform.inverse();
2235
chaviw39cfa2e2020-11-04 14:19:02 -08002236 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2237 // The frame should be the area the user sees on screen since it's used for occlusion
2238 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002239 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2240 info.frameLeft = transformedLayerBounds.left;
2241 info.frameTop = transformedLayerBounds.top;
2242 info.frameRight = transformedLayerBounds.right;
2243 info.frameBottom = transformedLayerBounds.bottom;
2244
Vishnu Nair8033a492018-12-05 07:27:23 -08002245 // Position the touchable region relative to frame screen location and restrict it to frame
2246 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002247 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002248}
2249
chaviw3277faf2021-05-19 16:45:23 -05002250void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002251 sp<Layer> p = this;
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002252 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002253 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002254 }
2255 if (p != nullptr) {
2256 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2257 }
2258}
2259
Vishnu Naira066d902021-09-13 18:40:17 -07002260gui::DropInputMode Layer::getDropInputMode() const {
2261 gui::DropInputMode mode = mDrawingState.dropInputMode;
2262 if (mode == gui::DropInputMode::ALL) {
2263 return mode;
2264 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002265 sp<Layer> parent = mDrawingParent.promote();
2266 if (parent) {
2267 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002268 if (parentMode != gui::DropInputMode::NONE) {
2269 return parentMode;
2270 }
2271 }
2272 return mode;
2273}
2274
2275void Layer::handleDropInputMode(gui::WindowInfo& info) const {
2276 if (mDrawingState.inputInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) {
2277 return;
2278 }
2279
2280 // Check if we need to drop input unconditionally
2281 gui::DropInputMode dropInputMode = getDropInputMode();
2282 if (dropInputMode == gui::DropInputMode::ALL) {
2283 info.inputFeatures |= WindowInfo::Feature::DROP_INPUT;
2284 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2285 return;
2286 }
2287
2288 // Check if we need to check if the window is obscured by parent
2289 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2290 return;
2291 }
2292
2293 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002294 sp<Layer> parent = mDrawingParent.promote();
2295 if (parent && parent->getAlpha() != 1.0_hf) {
Vishnu Naira066d902021-09-13 18:40:17 -07002296 info.inputFeatures |= WindowInfo::Feature::DROP_INPUT;
2297 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2298 static_cast<float>(getAlpha()));
2299 }
2300
2301 // Check if the parent has cropped the buffer
2302 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2303 if (!bufferSize.isValid()) {
2304 info.inputFeatures |= WindowInfo::Feature::DROP_INPUT_IF_OBSCURED;
2305 return;
2306 }
2307
2308 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2309 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2310 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2311 // match then the layer has not been cropped by its parents.
2312 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2313 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2314
2315 if (croppedByParent) {
2316 info.inputFeatures |= WindowInfo::Feature::DROP_INPUT;
2317 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2318 getDebugName());
2319 } else {
2320 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2321 // input if the window is obscured. This check should be done in surfaceflinger but the
2322 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2323 // drop input events if the window is obscured.
2324 info.inputFeatures |= WindowInfo::Feature::DROP_INPUT_IF_OBSCURED;
2325 }
2326}
2327
Vishnu Nair16a938f2021-09-24 07:14:54 -07002328WindowInfo Layer::fillInputInfo(const ui::Transform& displayTransform, bool displayIsSecure) {
chaviw7e72caf2020-12-02 16:50:43 -08002329 if (!hasInputInfo()) {
2330 mDrawingState.inputInfo.name = getName();
2331 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2332 mDrawingState.inputInfo.ownerPid = mOwnerPid;
chaviw3277faf2021-05-19 16:45:23 -05002333 mDrawingState.inputInfo.inputFeatures = WindowInfo::Feature::NO_INPUT_CHANNEL;
2334 mDrawingState.inputInfo.flags = WindowInfo::Flag::NOT_TOUCH_MODAL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002335 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002336 }
2337
chaviw3277faf2021-05-19 16:45:23 -05002338 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002339 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002340 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002341
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002342 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002343
Robert Carr5dc426e2020-06-10 14:29:14 -07002344 // For compatibility reasons we let layers which can receive input
2345 // receive input before they have actually submitted a buffer. Because
2346 // of this we use canReceiveInput instead of isVisible to check the
2347 // policy-visibility, ignoring the buffer state. However for layers with
2348 // hasInputInfo()==false we can use the real visibility state.
2349 // We are just using these layers for occlusion detection in
2350 // InputDispatcher, and obviously if they aren't visible they can't occlude
2351 // anything.
2352 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002353 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002354 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002355 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002356
Vishnu Nair16a938f2021-09-24 07:14:54 -07002357 // If the window will be blacked out on a display because the display does not have the secure
2358 // flag and the layer has the secure flag set, then drop input.
2359 if (!displayIsSecure && isSecure()) {
2360 info.inputFeatures |= WindowInfo::Feature::DROP_INPUT;
2361 }
2362
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002363 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2364 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002365 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2366 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002367 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002368 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002369 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002370 }
2371
Winson Chunga30f7c92021-06-29 15:42:56 -07002372 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2373 // if it was set by WM for a known system overlay
2374 info.trustedOverlay = info.trustedOverlay || isTrustedOverlay();
2375
2376
chaviwaf87b3e2019-10-01 16:59:28 -07002377 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2378 // touches from going outside the cloned area.
2379 if (isClone()) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002380 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2381 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002382 info.touchableRegion = info.touchableRegion.intersect(rect);
2383 }
2384 }
2385
Robert Carr720e5062018-07-30 17:45:14 -07002386 return info;
2387}
2388
chaviwaf87b3e2019-10-01 16:59:28 -07002389sp<Layer> Layer::getClonedRoot() {
2390 if (mClonedChild != nullptr) {
2391 return this;
2392 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002393 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002394 return nullptr;
2395 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002396 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002397}
2398
Robert Carredd13602020-04-13 17:24:34 -07002399bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002400 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002401}
2402
chaviw3e727cd2019-01-31 13:41:05 -08002403bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002404 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002405}
2406
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002407compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002408 const DisplayDevice* display) const {
2409 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002410 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002411}
2412
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002413Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2414 const auto outputLayer = findOutputLayerForDisplay(display);
2415 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002416}
2417
chaviwb4c6e582019-08-16 14:35:07 -07002418void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2419 // copy drawing state from cloned layer
2420 mDrawingState = clonedFrom->mDrawingState;
2421 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002422}
chaviw74b03172019-08-19 11:09:03 -07002423
2424void Layer::updateMirrorInfo() {
2425 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2426 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2427 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2428 // that case, we want to delete the reference to the clone since we want it to get
2429 // destroyed. The root, this layer, will still be around since the client can continue
2430 // to hold a reference, but no cloned layers will be displayed.
2431 mClonedChild = nullptr;
2432 return;
2433 }
2434
2435 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2436 // If the real layer exists and is in current state, add the clone as a child of the root.
2437 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2438 // copied to drawingState for the root layer. So the clonedChild is always removed from
2439 // drawingState and then needs to be added back each traversal.
2440 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2441 addChildToDrawing(mClonedChild);
2442 }
2443
2444 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2445 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2446 mClonedChild->updateClonedRelatives(clonedLayersMap);
2447}
2448
2449void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2450 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2451 // to the clone. If the real layer is no longer alive, continue traversing the children
2452 // since we may be able to pull out other children that are still alive.
2453 if (isClonedFromAlive()) {
2454 sp<Layer> clonedFrom = getClonedFrom();
2455 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002456 clonedLayersMap.emplace(clonedFrom, this);
2457 }
2458
2459 // The clone layer may have children in drawingState since they may have been created and
2460 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2461 // that already exist, since we can just re-use them.
2462 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2463 // not updated in the regular traversal. They are skipped since the root will lose the
2464 // reference to them when it copies its currentChildren to drawing.
2465 for (sp<Layer>& child : mDrawingChildren) {
2466 child->updateClonedDrawingState(clonedLayersMap);
2467 }
2468}
2469
2470void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2471 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2472 mDrawingChildren.clear();
2473
2474 if (!isClonedFromAlive()) {
2475 return;
2476 }
2477
2478 sp<Layer> clonedFrom = getClonedFrom();
2479 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2480 if (child == mirrorRoot) {
2481 // This is to avoid cyclical mirroring.
2482 continue;
2483 }
2484 sp<Layer> clonedChild = clonedLayersMap[child];
2485 if (clonedChild == nullptr) {
2486 clonedChild = child->createClone();
2487 clonedLayersMap[child] = clonedChild;
2488 }
2489 addChildToDrawing(clonedChild);
2490 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2491 }
2492}
2493
chaviwaf87b3e2019-10-01 16:59:28 -07002494void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2495 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2496 if (cropLayer != nullptr) {
2497 if (clonedLayersMap.count(cropLayer) == 0) {
2498 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2499 // self as crop layer to avoid going outside bounds.
2500 mDrawingState.touchableRegionCrop = this;
2501 } else {
2502 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2503 mDrawingState.touchableRegionCrop = clonedCropLayer;
2504 }
2505 }
2506 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2507 // WM or the client.
chaviw3277faf2021-05-19 16:45:23 -05002508 mDrawingState.inputInfo.flags &= ~WindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002509}
2510
2511void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002512 mDrawingState.zOrderRelativeOf = nullptr;
2513 mDrawingState.zOrderRelatives.clear();
2514
2515 if (!isClonedFromAlive()) {
2516 return;
2517 }
2518
chaviwaf87b3e2019-10-01 16:59:28 -07002519 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002520 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002521 const sp<Layer>& relative = relativeWeak.promote();
2522 if (clonedLayersMap.count(relative) > 0) {
2523 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002524 mDrawingState.zOrderRelatives.add(clonedRelative);
2525 }
2526 }
2527
2528 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2529 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2530 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2531 // still traverse the children, but the layer with the missing relativeOf will not be shown
2532 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002533 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2534 if (clonedLayersMap.count(relativeOf) > 0) {
2535 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002536 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2537 }
2538
chaviwaf87b3e2019-10-01 16:59:28 -07002539 updateClonedInputInfo(clonedLayersMap);
2540
chaviw74b03172019-08-19 11:09:03 -07002541 for (sp<Layer>& child : mDrawingChildren) {
2542 child->updateClonedRelatives(clonedLayersMap);
2543 }
2544}
2545
2546void Layer::addChildToDrawing(const sp<Layer>& layer) {
2547 mDrawingChildren.add(layer);
2548 layer->mDrawingParent = this;
2549}
2550
Steven Thomas62a4cf82020-01-31 12:04:03 -08002551Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2552 switch (compatibility) {
2553 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2554 return FrameRateCompatibility::Default;
2555 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2556 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002557 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2558 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002559 default:
2560 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2561 return FrameRateCompatibility::Default;
2562 }
2563}
2564
Marin Shalamanovc5986772021-03-16 16:09:49 +01002565scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2566 switch (strategy) {
2567 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2568 return Seamlessness::OnlySeamless;
2569 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2570 return Seamlessness::SeamedAndSeamless;
2571 default:
2572 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2573 return Seamlessness::Default;
2574 }
2575}
2576
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002577bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002578 const State& s(mDrawingState);
2579 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2580 return true;
2581 }
2582
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002583 sp<Layer> parent = mDrawingParent.promote();
2584 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002585}
2586
Robert Carr6a0382d2021-07-01 15:57:17 -07002587void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2588 mClonedChild = clonedChild;
2589 mHadClonedChild = true;
2590 mFlinger->mNumClones++;
2591}
2592
Vishnu Nairf9096652021-07-20 18:49:42 -07002593const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle");
2594
2595wp<Layer> Layer::fromHandle(const sp<IBinder>& handleBinder) {
2596 if (handleBinder == nullptr) {
2597 return nullptr;
2598 }
2599
2600 BBinder* b = handleBinder->localBinder();
2601 if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) {
2602 return nullptr;
2603 }
2604
2605 // We can safely cast this binder since its local and we verified its interface descriptor.
2606 sp<Handle> handle = static_cast<Handle*>(handleBinder.get());
2607 return handle->owner;
2608}
2609
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002610bool Layer::setDropInputMode(gui::DropInputMode mode) {
2611 if (mDrawingState.dropInputMode == mode) {
2612 return false;
2613 }
2614 mDrawingState.dropInputMode = mode;
2615 return true;
2616}
2617
Mathias Agopian13127d82013-03-05 17:47:11 -08002618// ---------------------------------------------------------------------------
2619
Marin Shalamanov46084422020-10-13 12:33:42 +02002620std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07002621 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
2622 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02002623}
2624
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07002625} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002626
2627#if defined(__gl_h_)
2628#error "don't include gl/gl.h in this file"
2629#endif
2630
2631#if defined(__gl2_h_)
2632#error "don't include gl2/gl2.h in this file"
2633#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002634
2635// TODO(b/129481165): remove the #pragma below and fix conversion issues
2636#pragma clang diagnostic pop // ignored "-Wconversion"