blob: c2a0e3082d3177f6da3acb32a7213cfba57cf06b [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>
Patrick Williamsbb25f802022-08-30 23:02:34 +000032#include <compositionengine/CompositionEngine.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/Display.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080034#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <compositionengine/OutputLayer.h>
36#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070039#include <cutils/properties.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070040#include <ftl/enum.h>
Dominik Laskowski298b08e2022-02-15 13:45:02 -080041#include <ftl/fake_guard.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <gui/BufferItem.h>
43#include <gui/LayerDebugInfo.h>
44#include <gui/Surface.h>
Patrick Williamsbb25f802022-08-30 23:02:34 +000045#include <gui/TraceUtils.h>
Alec Mourie60041e2019-06-14 18:59:51 -070046#include <math.h>
chaviw250bcbb2020-08-05 11:17:54 -070047#include <private/android_filesystem_config.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080048#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070049#include <stdint.h>
50#include <stdlib.h>
51#include <sys/types.h>
Alec Mouridda07d92022-04-25 22:39:25 +000052#include <system/graphics-base-v1.0.h>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070053#include <ui/DataspaceUtils.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080054#include <ui/DebugUtils.h>
55#include <ui/GraphicBuffer.h>
56#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include <utils/Errors.h>
58#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080059#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080061#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Alec Mourie60041e2019-06-14 18:59:51 -070063#include <algorithm>
64#include <mutex>
65#include <sstream>
66
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070067#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080068#include "DisplayHardware/HWComposer.h"
Ady Abraham22c7b5c2020-09-22 19:33:40 -070069#include "FrameTimeline.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070070#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080071#include "LayerProtoHelper.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
Patrick Williamsbb25f802022-08-30 23:02:34 +000077#define EARLY_RELEASE_ENABLED false
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010080namespace {
81constexpr int kDumpTableRowLength = 159;
Patrick Williamsbb25f802022-08-30 23:02:34 +000082
Prabir Pradhanda0f62c2022-07-22 19:53:04 +000083const ui::Transform kIdentityTransform;
Patrick Williamsbb25f802022-08-30 23:02:34 +000084
Patrick Williamsbb25f802022-08-30 23:02:34 +000085bool assignTransform(ui::Transform* dst, ui::Transform& from) {
86 if (*dst == from) {
87 return false;
88 }
89 *dst = from;
90 return true;
91}
92
93TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
94 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
95 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
96 const auto frameRateCompatibility = [frameRate] {
97 switch (frameRate.type) {
98 case Layer::FrameRateCompatibility::Default:
99 return FrameRateCompatibility::Default;
100 case Layer::FrameRateCompatibility::ExactOrMultiple:
101 return FrameRateCompatibility::ExactOrMultiple;
102 default:
103 return FrameRateCompatibility::Undefined;
104 }
105 }();
106
107 const auto seamlessness = [frameRate] {
108 switch (frameRate.seamlessness) {
109 case scheduler::Seamlessness::OnlySeamless:
110 return Seamlessness::ShouldBeSeamless;
111 case scheduler::Seamlessness::SeamedAndSeamless:
112 return Seamlessness::NotRequired;
113 default:
114 return Seamlessness::Undefined;
115 }
116 }();
117
118 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
119 .frameRateCompatibility = frameRateCompatibility,
120 .seamlessness = seamlessness};
121}
122
Marin Shalamanov1876e2e2020-12-04 13:23:59 +0100123} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700125using namespace ftl::flag_operators;
126
Yiwei Zhang5434a782018-12-05 18:06:32 -0800127using base::StringAppendF;
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800128using gui::GameMode;
129using gui::LayerMetadata;
chaviw3277faf2021-05-19 16:45:23 -0500130using gui::WindowInfo;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800131
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700132using PresentState = frametimeline::SurfaceFrame::PresentState;
133
Lloyd Piquef1c675b2018-09-12 20:45:39 -0700134std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -0800135
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700136Layer::Layer(const LayerCreationArgs& args)
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800137 : sequence(args.sequence.value_or(sSequence++)),
Ady Abrahamd11bade2022-08-01 16:18:03 -0700138 mFlinger(sp<SurfaceFlinger>::fromExisting(args.flinger)),
Arthur Hung23e07502021-10-15 11:58:19 +0000139 mName(base::StringPrintf("%s#%d", args.name.c_str(), sequence)),
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700140 mClientRef(args.client),
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800141 mWindowType(static_cast<WindowInfo::Type>(
142 args.metadata.getInt32(gui::METADATA_WINDOW_TYPE, 0))),
Tianhao Yao67dd7122022-02-22 17:48:33 +0000143 mLayerCreationFlags(args.flags),
Patrick Williamsbb25f802022-08-30 23:02:34 +0000144 mBorderEnabled(false),
145 mTextureName(args.textureName),
Vishnu Naire14c6b32022-08-06 04:20:15 +0000146 mHwcSlotGenerator(sp<HwcSlotGenerator>::make()),
147 mLayerFE(args.flinger->getFactory().createLayerFE(mName)) {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000148 ALOGV("Creating Layer %s", getDebugName());
149
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700150 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700151 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
152 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
153 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -0700154 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
155 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Vishnu Naira84a2202022-01-12 20:12:55 -0800156 if (args.sequence) {
157 sSequence = *args.sequence + 1;
158 }
Robert Carr6a160312021-05-17 12:08:20 -0700159 mDrawingState.flags = layerFlags;
Robert Carr6a160312021-05-17 12:08:20 -0700160 mDrawingState.crop.makeInvalid();
Robert Carr6a160312021-05-17 12:08:20 -0700161 mDrawingState.z = 0;
162 mDrawingState.color.a = 1.0f;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700163 mDrawingState.layerStack = ui::DEFAULT_LAYER_STACK;
Robert Carr6a160312021-05-17 12:08:20 -0700164 mDrawingState.sequence = 0;
Robert Carr6a160312021-05-17 12:08:20 -0700165 mDrawingState.transform.set(0, 0);
166 mDrawingState.frameNumber = 0;
167 mDrawingState.bufferTransform = 0;
168 mDrawingState.transformToDisplayInverse = false;
169 mDrawingState.crop.makeInvalid();
170 mDrawingState.acquireFence = sp<Fence>::make(-1);
171 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
172 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000173 mDrawingState.dataspaceRequested = false;
Robert Carr6a160312021-05-17 12:08:20 -0700174 mDrawingState.hdrMetadata.validTypes = 0;
175 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
176 mDrawingState.cornerRadius = 0.0f;
177 mDrawingState.backgroundBlurRadius = 0;
178 mDrawingState.api = -1;
179 mDrawingState.hasColorTransform = false;
180 mDrawingState.colorSpaceAgnostic = false;
181 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
182 mDrawingState.metadata = args.metadata;
183 mDrawingState.shadowRadius = 0.f;
184 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
185 mDrawingState.frameTimelineInfo = {};
186 mDrawingState.postTime = -1;
187 mDrawingState.destinationFrame.makeInvalid();
Robin Leeedb375d2021-09-10 12:03:42 +0000188 mDrawingState.isTrustedOverlay = false;
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -0700189 mDrawingState.dropInputMode = gui::DropInputMode::NONE;
Sally Qi81d95e62022-03-21 19:41:33 -0700190 mDrawingState.dimmingEnabled = true;
Andy Labrada096227e2022-06-15 16:58:11 +0000191 mDrawingState.defaultFrameRateCompatibility = FrameRateCompatibility::Default;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700192
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700193 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
194 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700195 mDrawingState.color.r = -1.0_hf;
196 mDrawingState.color.g = -1.0_hf;
197 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700198 }
Dominik Laskowski4376bd82022-07-07 11:50:20 -0700199
200 mFrameTracker.setDisplayRefreshPeriod(
201 args.flinger->mScheduler->getVsyncPeriodFromRefreshRateConfigs());
Robert Carr2e102c92018-10-23 12:11:15 -0700202
Vishnu Nair0f085c62019-08-30 08:49:12 -0700203 mCallingPid = args.callingPid;
204 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700205
206 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
207 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800208 mOwnerUid = args.metadata.getInt32(gui::METADATA_OWNER_UID, mCallingUid);
209 mOwnerPid = args.metadata.getInt32(gui::METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700210 } else {
211 // A create layer request from a non system request cannot specify the owner uid
212 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700213 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700214 }
Patrick Williamsbb25f802022-08-30 23:02:34 +0000215
216 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
217 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
218 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
219 mDrawingState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000220
221 mSnapshot->sequence = sequence;
222 mSnapshot->name = getDebugName();
223 mSnapshot->textureName = mTextureName;
224 mSnapshot->premultipliedAlpha = mPremultipliedAlpha;
225 mSnapshot->transform = {};
Dominik Laskowski75848362019-11-11 17:57:20 -0800226}
227
228void Layer::onFirstRef() {
229 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700230}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700231
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700232Layer::~Layer() {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000233 // The original layer and the clone layer share the same texture and buffer. Therefore, only
234 // one of the layers, in this case the original layer, needs to handle the deletion. The
235 // original layer and the clone should be removed at the same time so there shouldn't be any
236 // issue with the clone layer trying to use the texture.
237 if (mBufferInfo.mBuffer != nullptr) {
238 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
239 mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
240 mBufferInfo.mFence,
241 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
242 mOwnerUid));
243 }
244 if (!isClone()) {
245 // The original layer and the clone layer share the same texture. Therefore, only one of
246 // the layers, in this case the original layer, needs to handle the deletion. The original
247 // layer and the clone should be removed at the same time so there shouldn't be any issue
248 // with the clone layer trying to use the deleted texture.
249 mFlinger->deleteTextureAsync(mTextureName);
250 }
251 const int32_t layerId = getSequence();
252 mFlinger->mTimeStats->onDestroy(layerId);
253 mFlinger->mFrameTracer->onDestroy(layerId);
254
David Sodman577c8962017-12-08 14:50:53 -0800255 sp<Client> c(mClientRef.promote());
256 if (c != 0) {
257 c->detachLayer(this);
258 }
259
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000260 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700261 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700262
263 if (mDrawingState.sidebandStream != nullptr) {
264 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
265 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700266 if (mHadClonedChild) {
267 mFlinger->mNumClones--;
268 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700269}
270
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700271LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800272 uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700273 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700274 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700275 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700276 flags(flags),
277 metadata(std::move(metadata)) {
278 IPCThreadState* ipc = IPCThreadState::self();
279 callingPid = ipc->getCallingPid();
280 callingUid = ipc->getCallingUid();
281}
282
Mathias Agopian13127d82013-03-05 17:47:11 -0800283// ---------------------------------------------------------------------------
284// callbacks
285// ---------------------------------------------------------------------------
286
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700287void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700288 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700289 return;
290 }
Robert Carr2e102c92018-10-23 12:11:15 -0700291
Robert Carr6a160312021-05-17 12:08:20 -0700292 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700293 if (strongRelative == nullptr) {
294 setZOrderRelativeOf(nullptr);
295 return;
296 }
297
298 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700299 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700300 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800301 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700302 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700303}
304
305void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700306 if (!mRemovedFromDrawingState) {
307 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700308 mFlinger->mScheduler->deregisterLayer(this);
309 }
Rob Carr4bba3702018-10-08 21:53:30 +0000310
Ady Abrahamd11bade2022-08-01 16:18:03 -0700311 mFlinger->markLayerPendingRemovalLocked(sp<Layer>::fromExisting(this));
Chia-I Wuc6657022017-08-15 11:18:17 -0700312}
Chia-I Wu38512252017-05-17 14:36:16 -0700313
chaviw68d4dab2020-06-08 15:07:32 -0700314sp<Layer> Layer::getRootLayer() {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000315 sp<Layer> parent = getParent();
chaviw68d4dab2020-06-08 15:07:32 -0700316 if (parent == nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700317 return sp<Layer>::fromExisting(this);
chaviw68d4dab2020-06-08 15:07:32 -0700318 }
319 return parent->getRootLayer();
320}
321
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700322void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700323 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
324 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700325 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700326
327 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700328 layer->removeFromCurrentState();
329 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700330 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700331}
332
chaviw61626f22018-11-15 16:26:27 -0800333void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700334 if (mRemovedFromDrawingState) {
335 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700336 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700337 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700338 }
chaviw61626f22018-11-15 16:26:27 -0800339
340 for (const auto& child : mCurrentChildren) {
341 child->addToCurrentState();
342 }
343}
344
Mathias Agopian13127d82013-03-05 17:47:11 -0800345// ---------------------------------------------------------------------------
346// set-up
347// ---------------------------------------------------------------------------
348
chaviw13fdc492017-06-27 12:40:18 -0700349bool Layer::getPremultipledAlpha() const {
350 return mPremultipliedAlpha;
351}
352
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700353sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800354 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700355 if (mGetHandleCalled) {
356 ALOGE("Get handle called twice" );
357 return nullptr;
358 }
359 mGetHandleCalled = true;
Ady Abrahamd11bade2022-08-01 16:18:03 -0700360 return sp<Handle>::make(mFlinger, sp<Layer>::fromExisting(this));
Mathias Agopian13127d82013-03-05 17:47:11 -0800361}
362
363// ---------------------------------------------------------------------------
364// h/w composer set-up
365// ---------------------------------------------------------------------------
366
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700367static Rect reduce(const Rect& win, const Region& exclude) {
368 if (CC_LIKELY(exclude.isEmpty())) {
369 return win;
370 }
371 if (exclude.isRect()) {
372 return win.reduce(exclude.getBounds());
373 }
374 return Region(win).subtract(exclude).getBounds();
375}
376
Dan Stoza80d61162017-12-20 15:57:52 -0800377static FloatRect reduce(const FloatRect& win, const Region& exclude) {
378 if (CC_LIKELY(exclude.isEmpty())) {
379 return win;
380 }
381 // Convert through Rect (by rounding) for lack of FloatRegion
382 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
383}
384
Vishnu Nair4351ad52019-02-11 14:13:02 -0800385Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800386 if (!reduceTransparentRegion) {
387 return Rect{mScreenBounds};
388 }
389
390 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800391 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800392 // Transform to screen space.
393 bounds = t.transform(bounds);
394 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700395}
396
Vishnu Nair4351ad52019-02-11 14:13:02 -0800397FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000398 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800399 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700400}
401
Vishnu Nairf0c28512019-02-08 12:40:28 -0800402FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
403 // Subtract the transparent region and snap to the bounds.
404 return reduce(mBounds, activeTransparentRegion);
405}
406
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700407void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
408 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800409 const State& s(getDrawingState());
410
411 // Calculate effective layer transform
412 mEffectiveTransform = parentTransform * getActiveTransform(s);
413
Garfield Tan2c1782c2022-02-16 15:25:05 -0800414 if (CC_UNLIKELY(!isTransformValid())) {
415 ALOGW("Stop computing bounds for %s because it has invalid transformation.",
416 getDebugName());
417 return;
418 }
419
Vishnu Nair4351ad52019-02-11 14:13:02 -0800420 // Transform parent bounds to layer space
421 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
422
Vishnu Nairc652ff82019-03-15 12:48:54 -0700423 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800424 mSourceBounds = computeSourceBounds(parentBounds);
425
426 // Calculate bounds by croping diplay frame with layer crop and parent bounds
427 FloatRect bounds = mSourceBounds;
428 const Rect layerCrop = getCrop(s);
429 if (!layerCrop.isEmpty()) {
430 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
431 }
432 bounds = bounds.intersect(parentBounds);
433
434 mBounds = bounds;
435 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700436
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700437 // Use the layer's own shadow radius if set. Otherwise get the radius from
438 // parent.
439 if (s.shadowRadius > 0.f) {
440 mEffectiveShadowRadius = s.shadowRadius;
441 } else {
442 mEffectiveShadowRadius = parentShadowRadius;
443 }
444
445 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
446 // don't pass it to its children.
447 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
448
Vishnu Nair4351ad52019-02-11 14:13:02 -0800449 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700450 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800451 }
Vishnu Nairba354102022-08-01 00:14:18 +0000452
453 if (mPotentialCursor) {
454 prepareCursorCompositionState();
455 }
Vishnu Nair4351ad52019-02-11 14:13:02 -0800456}
457
Vishnu Nair60356342018-11-13 13:00:45 -0800458Rect Layer::getCroppedBufferSize(const State& s) const {
459 Rect size = getBufferSize(s);
460 Rect crop = getCrop(s);
461 if (!crop.isEmpty() && size.isValid()) {
462 size.intersect(crop, &size);
463 } else if (!crop.isEmpty()) {
464 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700465 }
Vishnu Nair60356342018-11-13 13:00:45 -0800466 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800467}
468
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700469void Layer::setupRoundedCornersCropCoordinates(Rect win,
470 const FloatRect& roundedCornersCrop) const {
471 // Translate win by the rounded corners rect coordinates, to have all values in
472 // layer coordinate space.
473 win.left -= roundedCornersCrop.left;
474 win.right -= roundedCornersCrop.left;
475 win.top -= roundedCornersCrop.top;
476 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700477}
478
Lloyd Piquede196652020-01-22 17:29:58 -0800479void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800480 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800481 const auto alpha = static_cast<float>(getAlpha());
482 const bool opaque = isOpaque(drawingState);
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700483 const bool usesRoundedCorners = hasRoundedCorners();
Lloyd Piquec6687342019-03-07 21:34:57 -0800484
485 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
486 if (!opaque || alpha != 1.0f) {
487 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
488 : Hwc2::IComposerClient::BlendMode::COVERAGE;
489 }
490
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000491 auto* snapshot = editLayerSnapshot();
492 snapshot->outputFilter = getOutputFilter();
493 snapshot->isVisible = isVisible();
494 snapshot->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
495 snapshot->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800496
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000497 snapshot->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800498 contentDirty = false;
499
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000500 snapshot->geomLayerBounds = mBounds;
501 snapshot->geomLayerTransform = getTransform();
502 snapshot->geomInverseLayerTransform = snapshot->geomLayerTransform.inverse();
503 snapshot->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800504
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000505 snapshot->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
506 snapshot->alpha = alpha;
507 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
508 snapshot->blurRegions = drawingState.blurRegions;
509 snapshot->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800510}
511
Lloyd Piquede196652020-01-22 17:29:58 -0800512void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800513 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000514 auto* snapshot = editLayerSnapshot();
David Sodman15094112018-10-11 09:39:37 -0700515
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000516 snapshot->geomBufferSize = getBufferSize(drawingState);
517 snapshot->geomContentCrop = getBufferCrop();
518 snapshot->geomCrop = getCrop(drawingState);
519 snapshot->geomBufferTransform = getBufferTransform();
520 snapshot->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
521 snapshot->geomUsesSourceCrop = usesSourceCrop();
522 snapshot->isSecure = isSecure();
Lloyd Piquede196652020-01-22 17:29:58 -0800523
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000524 snapshot->metadata.clear();
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800525 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
526 for (const auto& [key, mandatory] : supportedMetadata) {
527 const auto& genericLayerMetadataCompatibilityMap =
528 mFlinger->getGenericLayerMetadataKeyMap();
529 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
530 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
531 continue;
532 }
533 const uint32_t id = compatIter->second;
534
535 auto it = drawingState.metadata.mMap.find(id);
536 if (it == std::end(drawingState.metadata.mMap)) {
537 continue;
538 }
539
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000540 snapshot->metadata.emplace(key,
541 compositionengine::GenericLayerMetadataEntry{mandatory,
542 it->second});
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800543 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800544}
David Sodmanba340492018-08-05 21:51:33 -0700545
Lloyd Piquede196652020-01-22 17:29:58 -0800546void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800547 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000548 auto* snapshot = editLayerSnapshot();
Lloyd Piquef5275482019-01-29 18:42:42 -0800549
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000550 snapshot->forceClientComposition = false;
Lloyd Piquede196652020-01-22 17:29:58 -0800551
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000552 snapshot->isColorspaceAgnostic = isColorSpaceAgnostic();
553 snapshot->dataspace = getDataSpace();
554 snapshot->colorTransform = getColorTransform();
555 snapshot->colorTransformIsIdentity = !hasColorTransform();
556 snapshot->surfaceDamage = surfaceDamageRegion;
557 snapshot->hasProtectedContent = isProtected();
558 snapshot->dimmingEnabled = isDimmingEnabled();
Lloyd Pique688abd42019-02-15 15:42:24 -0800559
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700560 const bool usesRoundedCorners = hasRoundedCorners();
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700561
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000562 snapshot->isOpaque = isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800563
564 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400565 // Rounded corners no longer force client composition, since we may use a
566 // hole punch so that the layer will appear to have rounded corners.
567 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000568 snapshot->stretchEffect.hasEffect()) {
569 snapshot->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800570 }
Vishnu Nairb801a982021-11-02 15:12:08 -0700571 // If there are no visible region changes, we still need to update blur parameters.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000572 snapshot->blurRegions = drawingState.blurRegions;
573 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400574
575 // Layer framerate is used in caching decisions.
576 // Retrieve it from the scheduler which maintains an instance of LayerHistory, and store it in
577 // LayerFECompositionState where it would be visible to Flattener.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000578 snapshot->fps = mFlinger->getLayerFramerate(systemTime(), getSequence());
Patrick Williamsbb25f802022-08-30 23:02:34 +0000579
580 if (hasBufferOrSidebandStream()) {
581 preparePerFrameBufferCompositionState();
582 } else {
583 preparePerFrameEffectsCompositionState();
584 }
585}
586
587void Layer::preparePerFrameBufferCompositionState() {
588 // Sideband layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000589 auto* snapshot = editLayerSnapshot();
590 if (snapshot->sidebandStream.get() && !snapshot->sidebandStreamHasFrame) {
591 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000592 aidl::android::hardware::graphics::composer3::Composition::SIDEBAND;
593 return;
594 } else if ((mDrawingState.flags & layer_state_t::eLayerIsDisplayDecoration) != 0) {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000595 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000596 aidl::android::hardware::graphics::composer3::Composition::DISPLAY_DECORATION;
597 } else {
598 // Normal buffer layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000599 snapshot->hdrMetadata = mBufferInfo.mHdrMetadata;
600 snapshot->compositionType = mPotentialCursor
Patrick Williamsbb25f802022-08-30 23:02:34 +0000601 ? aidl::android::hardware::graphics::composer3::Composition::CURSOR
602 : aidl::android::hardware::graphics::composer3::Composition::DEVICE;
603 }
604
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000605 snapshot->buffer = getBuffer();
606 snapshot->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
Patrick Williamsbb25f802022-08-30 23:02:34 +0000607 ? 0
608 : mBufferInfo.mBufferSlot;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000609 snapshot->acquireFence = mBufferInfo.mFence;
610 snapshot->frameNumber = mBufferInfo.mFrameNumber;
611 snapshot->sidebandStreamHasFrame = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000612}
613
614void Layer::preparePerFrameEffectsCompositionState() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000615 auto* snapshot = editLayerSnapshot();
616 snapshot->color = getColor();
617 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000618 aidl::android::hardware::graphics::composer3::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800619}
620
Lloyd Piquede196652020-01-22 17:29:58 -0800621void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800622 const State& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000623 auto* snapshot = editLayerSnapshot();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800624
625 // Apply the layer's transform, followed by the display's global transform
626 // Here we're guaranteed that the layer's transform preserves rects
627 Rect win = getCroppedBufferSize(drawingState);
628 // Subtract the transparent region and snap to the bounds
629 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
630 Rect frame(getTransform().transform(bounds));
631
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000632 snapshot->cursorFrame = frame;
Lloyd Piquede196652020-01-22 17:29:58 -0800633}
634
Lloyd Piquea83776c2019-01-29 18:42:32 -0800635const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700636 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800637}
638
Mathias Agopian13127d82013-03-05 17:47:11 -0800639// ---------------------------------------------------------------------------
640// drawing...
641// ---------------------------------------------------------------------------
642
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500643aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType(
644 const DisplayDevice& display) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700645 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800646 if (outputLayer == nullptr) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500647 return aidl::android::hardware::graphics::composer3::Composition::INVALID;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800648 }
649 if (outputLayer->getState().hwc) {
650 return (*outputLayer->getState().hwc).hwcCompositionType;
651 } else {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500652 return aidl::android::hardware::graphics::composer3::Composition::CLIENT;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800653 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654}
655
Mathias Agopian13127d82013-03-05 17:47:11 -0800656// ----------------------------------------------------------------------------
657// local state
658// ----------------------------------------------------------------------------
659
David Sodman41fdfc92017-11-06 16:09:56 -0800660bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800661 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700662 if (s.flags & layer_state_t::eLayerSecure) {
663 return true;
664 }
665
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000666 const auto p = mDrawingParent.promote();
Garfield Tande619fa2020-10-02 17:13:53 -0700667 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700668}
669
Mathias Agopian13127d82013-03-05 17:47:11 -0800670// ----------------------------------------------------------------------------
671// transaction
672// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800673
Marissa Wall61c58622018-07-18 10:12:20 -0700674uint32_t Layer::doTransaction(uint32_t flags) {
675 ATRACE_CALL();
676
Robert Carr0758e5d2021-03-11 22:15:04 -0800677 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700678 mDrawingStateModified = mDrawingState.modified;
679 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700680
Alec Mourib416efd2018-09-06 21:01:59 +0000681 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700682
Robert Carr6a160312021-05-17 12:08:20 -0700683 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800684 // invalidate and recompute the visible regions if needed
685 flags |= Layer::eVisibleRegion;
686 }
687
Robert Carr6a160312021-05-17 12:08:20 -0700688 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800689 // invalidate and recompute the visible regions if needed
Arthur Hung9ed43392022-05-27 06:31:57 +0000690 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800691 flags |= eVisibleRegion;
692 this->contentDirty = true;
693
694 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700695 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800696 }
697
Arthur Hung9ed43392022-05-27 06:31:57 +0000698 if (!mPotentialCursor && (flags & Layer::eVisibleRegion)) {
699 mFlinger->mUpdateInputInfo = true;
700 }
701
Robert Carr6a160312021-05-17 12:08:20 -0700702 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800703
Mathias Agopian13127d82013-03-05 17:47:11 -0800704 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705}
706
Robert Carr6a160312021-05-17 12:08:20 -0700707void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000708 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
709 // bufferSurfaceFrameTX will be presented in latchBuffer.
710 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
711 if (surfaceFrame->getPresentState() != PresentState::Presented) {
712 // With applyPendingStates, we could end up having presented surfaceframes from previous
713 // states
714 surfaceFrame->setPresentState(PresentState::Presented);
715 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
716 }
717 }
Robert Carr6a160312021-05-17 12:08:20 -0700718 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700719}
720
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700721uint32_t Layer::clearTransactionFlags(uint32_t mask) {
722 const auto flags = mTransactionFlags & mask;
723 mTransactionFlags &= ~mask;
724 return flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800725}
726
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700727void Layer::setTransactionFlags(uint32_t mask) {
728 mTransactionFlags |= mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
Robert Carr1f0a16a2016-10-24 16:27:39 -0700731bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
732 ssize_t idx = mCurrentChildren.indexOf(childLayer);
733 if (idx < 0) {
734 return false;
735 }
736 if (childLayer->setLayer(z)) {
737 mCurrentChildren.removeAt(idx);
738 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800739 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700740 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800741 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700742}
743
Robert Carr503c7042017-09-27 15:06:08 -0700744bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
745 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
746 ssize_t idx = mCurrentChildren.indexOf(childLayer);
747 if (idx < 0) {
748 return false;
749 }
750 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
751 mCurrentChildren.removeAt(idx);
752 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800753 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700754 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800755 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700756}
757
Robert Carrae060832016-11-28 10:51:00 -0800758bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700759 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
760 mDrawingState.sequence++;
761 mDrawingState.z = z;
762 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700763
Robert Carra70e91c2021-06-11 13:59:52 -0700764 mFlinger->mSomeChildrenChanged = true;
765
Robert Carrdb66e622017-04-10 16:55:57 -0700766 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700767 if (mDrawingState.zOrderRelativeOf != nullptr) {
768 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700769 if (strongRelative != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700770 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700771 }
chaviw606e5cf2019-03-01 10:12:10 -0800772 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700773 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800774 setTransactionFlags(eTransactionNeeded);
775 return true;
776}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700777
Robert Carrdb66e622017-04-10 16:55:57 -0700778void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700779 mDrawingState.zOrderRelatives.remove(relative);
780 mDrawingState.sequence++;
781 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700782 setTransactionFlags(eTransactionNeeded);
783}
784
785void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700786 mDrawingState.zOrderRelatives.add(relative);
787 mDrawingState.modified = true;
788 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700789 setTransactionFlags(eTransactionNeeded);
790}
791
chaviw606e5cf2019-03-01 10:12:10 -0800792void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700793 mDrawingState.zOrderRelativeOf = relativeOf;
794 mDrawingState.sequence++;
795 mDrawingState.modified = true;
796 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700797
chaviw606e5cf2019-03-01 10:12:10 -0800798 setTransactionFlags(eTransactionNeeded);
799}
800
Robert Carr503d2bd2017-12-04 15:49:47 -0800801bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nairf9096652021-07-20 18:49:42 -0700802 sp<Layer> relative = fromHandle(relativeToHandle).promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700803 if (relative == nullptr) {
804 return false;
805 }
806
Robert Carr6a160312021-05-17 12:08:20 -0700807 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
808 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800809 return false;
810 }
811
Robert Carr88b85e12022-03-21 15:47:35 -0700812 if (CC_UNLIKELY(relative->usingRelativeZ(LayerVector::StateSet::Drawing)) &&
813 (relative->mDrawingState.zOrderRelativeOf == this)) {
814 ALOGE("Detected relative layer loop between %s and %s",
815 mName.c_str(), relative->mName.c_str());
816 ALOGE("Ignoring new call to set relative layer");
817 return false;
818 }
819
Robert Carra70e91c2021-06-11 13:59:52 -0700820 mFlinger->mSomeChildrenChanged = true;
821
Robert Carr6a160312021-05-17 12:08:20 -0700822 mDrawingState.sequence++;
823 mDrawingState.modified = true;
824 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700825
Robert Carr6a160312021-05-17 12:08:20 -0700826 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700827 if (oldZOrderRelativeOf != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700828 oldZOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
chaviw9ab4bd12017-11-03 13:11:00 -0700829 }
chaviw606e5cf2019-03-01 10:12:10 -0800830 setZOrderRelativeOf(relative);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700831 relative->addZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700832
833 setTransactionFlags(eTransactionNeeded);
834
835 return true;
836}
837
Winson Chunga30f7c92021-06-29 15:42:56 -0700838bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
839 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
840 mDrawingState.isTrustedOverlay = isTrustedOverlay;
841 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +0000842 mFlinger->mUpdateInputInfo = true;
Winson Chunga30f7c92021-06-29 15:42:56 -0700843 setTransactionFlags(eTransactionNeeded);
844 return true;
845}
846
847bool Layer::isTrustedOverlay() const {
848 if (getDrawingState().isTrustedOverlay) {
849 return true;
850 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000851 const auto& p = mDrawingParent.promote();
Winson Chunga30f7c92021-06-29 15:42:56 -0700852 return (p != nullptr) && p->isTrustedOverlay();
853}
854
Dan Stoza9e56aa02015-11-02 13:00:03 -0800855bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700856 if (mDrawingState.color.a == alpha) return false;
857 mDrawingState.sequence++;
858 mDrawingState.color.a = alpha;
859 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800860 setTransactionFlags(eTransactionNeeded);
861 return true;
862}
chaviw13fdc492017-06-27 12:40:18 -0700863
Valerie Haudd0b7572019-01-29 14:59:27 -0800864bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700865 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700866 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800867 }
Robert Carr6a160312021-05-17 12:08:20 -0700868 mDrawingState.sequence++;
869 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800870 setTransactionFlags(eTransactionNeeded);
871
Robert Carr6a160312021-05-17 12:08:20 -0700872 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800873 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800874 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700875 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700876 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800877 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700878 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700879
Valerie Haudd0b7572019-01-29 14:59:27 -0800880 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700881 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800882 mFlinger->mLayersAdded = true;
883 // set up SF to handle added color layer
884 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700885 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800886 }
887 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700888 } else if (mDrawingState.bgColorLayer && alpha == 0) {
889 mDrawingState.bgColorLayer->reparent(nullptr);
890 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800891 return true;
892 }
893
Robert Carr6a160312021-05-17 12:08:20 -0700894 mDrawingState.bgColorLayer->setColor(color);
895 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
896 mDrawingState.bgColorLayer->setAlpha(alpha);
897 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800898
chaviw13fdc492017-06-27 12:40:18 -0700899 return true;
900}
901
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700902bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700903 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700904
Robert Carr6a160312021-05-17 12:08:20 -0700905 mDrawingState.sequence++;
906 mDrawingState.cornerRadius = cornerRadius;
907 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700908 setTransactionFlags(eTransactionNeeded);
909 return true;
910}
911
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800912bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700913 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Vishnu Nairb801a982021-11-02 15:12:08 -0700914 // If we start or stop drawing blur then the layer's visibility state may change so increment
915 // the magic sequence number.
916 if (mDrawingState.backgroundBlurRadius == 0 || backgroundBlurRadius == 0) {
917 mDrawingState.sequence++;
918 }
Robert Carr6a160312021-05-17 12:08:20 -0700919 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
920 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800921 setTransactionFlags(eTransactionNeeded);
922 return true;
923}
Marissa Wall61c58622018-07-18 10:12:20 -0700924
Mathias Agopian13127d82013-03-05 17:47:11 -0800925bool Layer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000926 mDrawingState.sequence++;
927 mDrawingState.transparentRegionHint = transparent;
Robert Carr6a160312021-05-17 12:08:20 -0700928 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800929 setTransactionFlags(eTransactionNeeded);
930 return true;
931}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100932
Lucas Dupinc3800b82020-10-02 16:24:48 -0700933bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Vishnu Nairb801a982021-11-02 15:12:08 -0700934 // If we start or stop drawing blur then the layer's visibility state may change so increment
935 // the magic sequence number.
936 if (mDrawingState.blurRegions.size() == 0 || blurRegions.size() == 0) {
937 mDrawingState.sequence++;
938 }
Robert Carr6a160312021-05-17 12:08:20 -0700939 mDrawingState.blurRegions = blurRegions;
940 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700941 setTransactionFlags(eTransactionNeeded);
942 return true;
943}
944
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800945bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700946 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
947 if (mDrawingState.flags == newFlags) return false;
948 mDrawingState.sequence++;
949 mDrawingState.flags = newFlags;
950 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800951 setTransactionFlags(eTransactionNeeded);
952 return true;
953}
Robert Carr99e27f02016-06-16 15:18:02 -0700954
chaviw25714502021-02-11 10:01:08 -0800955bool Layer::setCrop(const Rect& crop) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000956 if (mDrawingState.crop == crop) return false;
Robert Carr6a160312021-05-17 12:08:20 -0700957 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700958 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700959
Robert Carr6a160312021-05-17 12:08:20 -0700960 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800961 setTransactionFlags(eTransactionNeeded);
962 return true;
963}
Robert Carr8d5227b2017-03-16 15:41:03 -0700964
Evan Roskyef876c92019-01-25 17:46:06 -0800965bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700966 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
967 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -0800968 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800969 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500970}
971
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700972bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -0700973 if (mDrawingState.layerStack == layerStack) return false;
974 mDrawingState.sequence++;
975 mDrawingState.layerStack = layerStack;
976 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800977 setTransactionFlags(eTransactionNeeded);
978 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700979}
980
Peiyong Linc502cb72019-03-01 15:00:23 -0800981bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -0700982 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800983 return false;
984 }
Robert Carr6a160312021-05-17 12:08:20 -0700985 mDrawingState.sequence++;
986 mDrawingState.colorSpaceAgnostic = agnostic;
987 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -0800988 setTransactionFlags(eTransactionNeeded);
989 return true;
990}
991
Sally Qi81d95e62022-03-21 19:41:33 -0700992bool Layer::setDimmingEnabled(const bool dimmingEnabled) {
993 if (mDrawingState.dimmingEnabled == dimmingEnabled) return false;
994
995 mDrawingState.sequence++;
996 mDrawingState.dimmingEnabled = dimmingEnabled;
997 mDrawingState.modified = true;
998 setTransactionFlags(eTransactionNeeded);
999 return true;
1000}
1001
Ana Krulecc84d09b2019-11-02 23:10:29 +01001002bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -07001003 if (mDrawingState.frameRateSelectionPriority == priority) return false;
1004 mDrawingState.frameRateSelectionPriority = priority;
1005 mDrawingState.sequence++;
1006 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +01001007 setTransactionFlags(eTransactionNeeded);
1008 return true;
1009}
1010
1011int32_t Layer::getFrameRateSelectionPriority() const {
1012 // Check if layer has priority set.
1013 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1014 return mDrawingState.frameRateSelectionPriority;
1015 }
1016 // If not, search whether its parents have it set.
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001017 sp<Layer> parent = getParent();
Ana Krulecc84d09b2019-11-02 23:10:29 +01001018 if (parent != nullptr) {
1019 return parent->getFrameRateSelectionPriority();
1020 }
1021
1022 return Layer::PRIORITY_UNSET;
1023}
1024
Andy Labrada096227e2022-06-15 16:58:11 +00001025bool Layer::setDefaultFrameRateCompatibility(FrameRateCompatibility compatibility) {
1026 if (mDrawingState.defaultFrameRateCompatibility == compatibility) return false;
1027 mDrawingState.defaultFrameRateCompatibility = compatibility;
1028 mDrawingState.modified = true;
1029 mFlinger->mScheduler->setDefaultFrameRateCompatibility(this);
1030 setTransactionFlags(eTransactionNeeded);
1031 return true;
1032}
1033
1034scheduler::LayerInfo::FrameRateCompatibility Layer::getDefaultFrameRateCompatibility() const {
1035 return mDrawingState.defaultFrameRateCompatibility;
1036}
1037
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001038bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1039 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1040};
1041
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001042ui::LayerStack Layer::getLayerStack() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001043 if (const auto parent = mDrawingParent.promote()) {
1044 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001045 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001046 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001047}
1048
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001049bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001050 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001051 return false;
1052 }
1053
Robert Carr6a160312021-05-17 12:08:20 -07001054 mDrawingState.sequence++;
1055 mDrawingState.shadowRadius = shadowRadius;
1056 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001057 setTransactionFlags(eTransactionNeeded);
1058 return true;
1059}
1060
Vishnu Nair6213bd92020-05-08 17:42:25 -07001061bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001062 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001063 return false;
1064 }
1065
Robert Carr6a160312021-05-17 12:08:20 -07001066 mDrawingState.sequence++;
1067 mDrawingState.fixedTransformHint = fixedTransformHint;
1068 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001069 setTransactionFlags(eTransactionNeeded);
1070 return true;
1071}
1072
John Reckcdb4ed72021-02-04 13:39:33 -05001073bool Layer::setStretchEffect(const StretchEffect& effect) {
1074 StretchEffect temp = effect;
1075 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001076 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001077 return false;
1078 }
Robert Carr6a160312021-05-17 12:08:20 -07001079 mDrawingState.sequence++;
1080 mDrawingState.stretchEffect = temp;
1081 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001082 setTransactionFlags(eTransactionNeeded);
1083 return true;
1084}
1085
John Reckc00c6692021-02-16 11:37:33 -05001086StretchEffect Layer::getStretchEffect() const {
1087 if (mDrawingState.stretchEffect.hasEffect()) {
1088 return mDrawingState.stretchEffect;
1089 }
1090
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001091 sp<Layer> parent = getParent();
John Reckc00c6692021-02-16 11:37:33 -05001092 if (parent != nullptr) {
1093 auto effect = parent->getStretchEffect();
1094 if (effect.hasEffect()) {
1095 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1096 return effect;
1097 }
1098 }
1099 return StretchEffect{};
1100}
1101
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001102bool Layer::enableBorder(bool shouldEnable, float width, const half4& color) {
1103 if (mBorderEnabled == shouldEnable && mBorderWidth == width && mBorderColor == color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00001104 return false;
1105 }
1106 mBorderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001107 mBorderWidth = width;
1108 mBorderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001109 return true;
1110}
1111
1112bool Layer::isBorderEnabled() {
1113 return mBorderEnabled;
1114}
1115
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001116float Layer::getBorderWidth() {
1117 return mBorderWidth;
1118}
1119
1120const half4& Layer::getBorderColor() {
1121 return mBorderColor;
1122}
1123
Ady Abrahama850c182021-08-04 13:04:37 -07001124bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1125 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1126 const auto frameRate = [&] {
1127 if (mDrawingState.frameRate.rate.isValid() ||
1128 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1129 return mDrawingState.frameRate;
1130 }
1131
1132 return parentFrameRate;
1133 }();
1134
1135 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1136
1137 // The frame rate is propagated to the children
1138 bool childrenHaveFrameRate = false;
1139 for (const sp<Layer>& child : mCurrentChildren) {
1140 childrenHaveFrameRate |=
1141 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1142 }
1143
1144 // If we don't have a valid frame rate, but the children do, we set this
1145 // layer as NoVote to allow the children to control the refresh rate
1146 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1147 childrenHaveFrameRate) {
1148 *transactionNeeded |=
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001149 setFrameRateForLayerTree(FrameRate(Fps(), FrameRateCompatibility::NoVote));
Ady Abrahama850c182021-08-04 13:04:37 -07001150 }
1151
1152 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1153 // the same reason we are allowing touch boost for those layers. See
1154 // RefreshRateConfigs::getBestRefreshRate for more details.
1155 const auto layerVotedWithDefaultCompatibility =
1156 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1157 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1158 const auto layerVotedWithExactCompatibility =
1159 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1160 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1161 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1162}
1163
Ady Abraham60e42ea2020-03-09 19:17:31 -07001164void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001165 const auto root = [&]() -> sp<Layer> {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001166 sp<Layer> layer = sp<Layer>::fromExisting(this);
Ady Abrahama850c182021-08-04 13:04:37 -07001167 while (auto parent = layer->getParent()) {
1168 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001169 }
Ady Abrahama850c182021-08-04 13:04:37 -07001170 return layer;
1171 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001172
Ady Abraham60e42ea2020-03-09 19:17:31 -07001173 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001174 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001175
Ady Abrahama850c182021-08-04 13:04:37 -07001176 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001177 if (transactionNeeded) {
1178 mFlinger->setTransactionFlags(eTraversalNeeded);
1179 }
1180}
1181
Ady Abraham71c437d2020-01-31 15:56:57 -08001182bool Layer::setFrameRate(FrameRate frameRate) {
Robert Carr6a160312021-05-17 12:08:20 -07001183 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001184 return false;
1185 }
1186
Robert Carr6a160312021-05-17 12:08:20 -07001187 mDrawingState.sequence++;
1188 mDrawingState.frameRate = frameRate;
1189 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001190
1191 updateTreeHasFrameRateVote();
1192
Steven Thomas3172e202020-01-06 19:25:30 -08001193 setTransactionFlags(eTransactionNeeded);
1194 return true;
1195}
1196
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001197void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1198 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001199 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001200
1201 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1202 // there are two transactions with the same token, the first one without a buffer and the
1203 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1204 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001205 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1206 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001207 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001208 mDrawingState.bufferSurfaceFrameTX = it->second;
1209 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1210 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1211 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001212 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001213 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001214 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1215 }
1216}
1217
1218void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1219 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001220 mDrawingState.frameTimelineInfo = info;
1221 mDrawingState.postTime = postTime;
1222 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001223 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001224
Robert Carr6a160312021-05-17 12:08:20 -07001225 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001226 bufferSurfaceFrameTX != nullptr) {
1227 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1228 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1229 // being used for BufferSurfaceFrame, don't create a new one.
1230 return;
1231 }
1232 }
1233 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1234 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1235 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001236 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1237 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001238 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001239 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001240 } else {
1241 if (it->second->getPresentState() == PresentState::Presented) {
1242 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1243 // have been from previous vsync.
1244 it->second = createSurfaceFrameForTransaction(info, postTime);
1245 }
1246 }
1247}
1248
1249void Layer::addSurfaceFrameDroppedForBuffer(
1250 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001251 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001252 surfaceFrame->setPresentState(PresentState::Dropped);
1253 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1254}
1255
1256void Layer::addSurfaceFramePresentedForBuffer(
1257 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1258 nsecs_t currentLatchTime) {
1259 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1260 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1261 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1262 mLastLatchTime = currentLatchTime;
1263}
1264
1265std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1266 const FrameTimelineInfo& info, nsecs_t postTime) {
1267 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001268 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1269 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001270 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001271 /*isBuffer*/ false, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001272 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001273 // For Transactions, the post time is considered to be both queue and acquire fence time.
1274 surfaceFrame->setActualQueueTime(postTime);
1275 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001276 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1277 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001278 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001279 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001280 onSurfaceFrameCreated(surfaceFrame);
1281 return surfaceFrame;
1282}
1283
1284std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1285 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1286 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001287 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001288 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001289 /*isBuffer*/ true, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001290 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001291 // For buffers, acquire fence time will set during latch.
1292 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001293 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1294 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001295 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001296 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001297 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1298 onSurfaceFrameCreated(surfaceFrame);
1299 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001300}
1301
Ady Abrahama850c182021-08-04 13:04:37 -07001302bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1303 if (mDrawingState.frameRateForLayerTree == frameRate) {
1304 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001305 }
1306
Ady Abrahama850c182021-08-04 13:04:37 -07001307 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001308
Ady Abrahama850c182021-08-04 13:04:37 -07001309 // TODO(b/195668952): we probably don't need to dirty visible regions here
1310 // or even store frameRateForLayerTree in mDrawingState
1311 mDrawingState.sequence++;
1312 mDrawingState.modified = true;
1313 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001314
Dominik Laskowski068173d2021-08-11 17:22:59 -07001315 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
1316 mFlinger->mScheduler->recordLayerHistory(this, systemTime(), LayerUpdateType::SetFrameRate);
Ady Abrahama850c182021-08-04 13:04:37 -07001317
1318 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001319}
1320
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001321Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1322 return getDrawingState().frameRateForLayerTree;
1323}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324
Robert Carr1f0a16a2016-10-24 16:27:39 -07001325bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001326 const State& s(mDrawingState);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001327 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001328 if (parent != nullptr && parent->isHiddenByPolicy()) {
1329 return true;
1330 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001331 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1332 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1333 if (zOrderRelativeOf != nullptr) {
1334 if (zOrderRelativeOf->isHiddenByPolicy()) {
1335 return true;
1336 }
1337 }
1338 }
Garfield Tan2c1782c2022-02-16 15:25:05 -08001339 if (CC_UNLIKELY(!isTransformValid())) {
1340 ALOGW("Hide layer %s because it has invalid transformation.", getDebugName());
1341 return true;
1342 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001343 return s.flags & layer_state_t::eLayerHidden;
1344}
1345
David Sodman41fdfc92017-11-06 16:09:56 -08001346uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001347 // TODO: should we do something special if mSecure is set?
1348 if (mProtectedByApp) {
1349 // need a hardware-protected path to external video sink
1350 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001351 }
Riley Andrews03414a12014-07-01 14:22:59 -07001352 if (mPotentialCursor) {
1353 usage |= GraphicBuffer::USAGE_CURSOR;
1354 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001355 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001356 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001357}
1358
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001359void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1360 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1361 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001362 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001363
Vishnu Nair6213bd92020-05-08 17:42:25 -07001364 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001365}
1366
Mathias Agopian13127d82013-03-05 17:47:11 -08001367// ----------------------------------------------------------------------------
1368// debugging
1369// ----------------------------------------------------------------------------
1370
Marissa Wall61c58622018-07-18 10:12:20 -07001371// TODO(marissaw): add new layer state info to layer debugging
Huihong Luo05539a12022-02-23 10:29:40 -08001372gui::LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001373 using namespace std::string_literals;
1374
Huihong Luo05539a12022-02-23 10:29:40 -08001375 gui::LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001376 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001377 info.mName = getName();
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001378 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001379 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001380 info.mType = getType();
Lloyd Piquea2468662019-03-07 21:31:06 -08001381
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001382 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001383 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001384 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001385 info.mX = ds.transform.tx();
1386 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001387 info.mZ = ds.z;
chaviw25714502021-02-11 10:01:08 -08001388 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001389 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001390 info.mFlags = ds.flags;
1391 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001392 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001393 info.mMatrix[0][0] = ds.transform[0][0];
1394 info.mMatrix[0][1] = ds.transform[0][1];
1395 info.mMatrix[1][0] = ds.transform[1][0];
1396 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001397 {
chaviwd62d3062019-09-04 14:48:02 -07001398 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001399 if (buffer != 0) {
1400 info.mActiveBufferWidth = buffer->getWidth();
1401 info.mActiveBufferHeight = buffer->getHeight();
1402 info.mActiveBufferStride = buffer->getStride();
1403 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001404 } else {
1405 info.mActiveBufferWidth = 0;
1406 info.mActiveBufferHeight = 0;
1407 info.mActiveBufferStride = 0;
1408 info.mActiveBufferFormat = 0;
1409 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001410 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001411 info.mNumQueuedFrames = getQueuedFrameCount();
Kalle Raitaa099a242017-01-11 11:17:29 -08001412 info.mIsOpaque = isOpaque(ds);
1413 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001414 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001415 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001416}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001417
Yiwei Zhang5434a782018-12-05 18:06:32 -08001418void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001419 result.append(kDumpTableRowLength, '-');
1420 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001421 result.append(" Layer name\n");
1422 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001423 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001424 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001425 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001426 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001427 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001428 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1429 result.append(kDumpTableRowLength, '-');
1430 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001431}
1432
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001433void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1434 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001435 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001436 return;
1437 }
1438
Yiwei Zhang5434a782018-12-05 18:06:32 -08001439 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001440 if (mName.length() > 77) {
1441 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001442 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001443 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001444 shortened.append(mName, mName.length() - 36);
1445 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001446 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001447 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001448 }
1449
Yiwei Zhang5434a782018-12-05 18:06:32 -08001450 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001451
Alec Mourib416efd2018-09-06 21:01:59 +00001452 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001453 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001454
Chia-I Wu1e043612018-03-01 09:45:09 -08001455 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001456 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001457 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001458 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001459 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001460 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001461 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001462 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1463 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001464 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001465 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001466 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001467 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001468 const auto frameRate = getFrameRateForLayerTree();
1469 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1470 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001471 ftl::enum_string(frameRate.type).c_str(),
1472 ftl::enum_string(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001473 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001474 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001475 }
Dan Stozae22aec72016-08-01 13:20:59 -07001476
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001477 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1478 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1479
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001480 result.append(kDumpTableRowLength, '-');
1481 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001482}
Dan Stozae22aec72016-08-01 13:20:59 -07001483
Yiwei Zhang5434a782018-12-05 18:06:32 -08001484void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001485 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001486}
1487
Svetoslavd85084b2014-03-20 10:28:31 -07001488void Layer::clearFrameStats() {
1489 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001490}
1491
Jamie Gennis6547ff42013-07-16 20:12:42 -07001492void Layer::logFrameStats() {
1493 mFrameTracker.logAndResetStats(mName);
1494}
1495
Svetoslavd85084b2014-03-20 10:28:31 -07001496void Layer::getFrameStats(FrameStats* outStats) const {
1497 mFrameTracker.getStats(outStats);
1498}
1499
Vishnu Nair0f085c62019-08-30 08:49:12 -07001500void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001501 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1502 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001503}
1504
Brian Anderson5ea5e592016-12-01 16:54:33 -08001505void Layer::onDisconnect() {
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001506 const int32_t layerId = getSequence();
1507 mFlinger->mTimeStats->onDestroy(layerId);
1508 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001509}
1510
Chia-I Wu98f1c102017-05-30 14:54:08 -07001511size_t Layer::getChildrenCount() const {
1512 size_t count = 0;
1513 for (const sp<Layer>& child : mCurrentChildren) {
1514 count += 1 + child->getChildrenCount();
1515 }
1516 return count;
1517}
1518
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001519void Layer::setGameModeForTree(GameMode gameMode) {
1520 const auto& currentState = getDrawingState();
Huihong Luod3d8f8e2022-03-08 14:48:46 -08001521 if (currentState.metadata.has(gui::METADATA_GAME_MODE)) {
1522 gameMode =
1523 static_cast<GameMode>(currentState.metadata.getInt32(gui::METADATA_GAME_MODE, 0));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001524 }
1525 setGameMode(gameMode);
1526 for (const sp<Layer>& child : mCurrentChildren) {
1527 child->setGameModeForTree(gameMode);
1528 }
1529}
1530
Robert Carr1f0a16a2016-10-24 16:27:39 -07001531void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001532 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001533 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001534
Robert Carr1f0a16a2016-10-24 16:27:39 -07001535 mCurrentChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07001536 layer->setParent(sp<Layer>::fromExisting(this));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001537 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001538 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001539}
1540
1541ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001542 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001543 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001544
Robert Carr1323c952019-01-28 18:13:27 -08001545 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001546 const auto removeResult = mCurrentChildren.remove(layer);
1547
1548 updateTreeHasFrameRateVote();
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001549 layer->setGameModeForTree(GameMode::Unsupported);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001550 layer->updateTreeHasFrameRateVote();
1551
1552 return removeResult;
1553}
1554
Robert Carr15eae092018-03-23 13:43:53 -07001555void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001556 for (const sp<Layer>& child : mDrawingChildren) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001557 child->mDrawingParent = newParent;
xinying1f4200e62022-04-26 14:41:30 +08001558 const float parentShadowRadius =
1559 newParent->canDrawShadows() ? 0.f : newParent->mEffectiveShadowRadius;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001560 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
xinying1f4200e62022-04-26 14:41:30 +08001561 parentShadowRadius);
Vishnu Naire14c6b32022-08-06 04:20:15 +00001562 child->updateSnapshot(true /* updateGeometry */);
1563 child->updateChildrenSnapshots(true /* updateGeometry */);
Robert Carr578038f2018-03-09 12:25:24 -08001564 }
1565}
1566
chaviwf1961f72017-09-18 16:41:07 -07001567bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001568 sp<Layer> newParent;
1569 if (newParentHandle != nullptr) {
Vishnu Nairf9096652021-07-20 18:49:42 -07001570 newParent = fromHandle(newParentHandle).promote();
Robert Carr54cf5b12019-01-25 14:02:28 -08001571 if (newParent == nullptr) {
1572 ALOGE("Unable to promote Layer handle");
1573 return false;
1574 }
1575 if (newParent == this) {
1576 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1577 return false;
1578 }
1579 }
1580
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001581 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001582 if (parent != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001583 parent->removeChild(sp<Layer>::fromExisting(this));
chaviw06178942017-07-27 10:25:59 -07001584 }
1585
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001586 if (newParentHandle != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001587 newParent->addChild(sp<Layer>::fromExisting(this));
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001588 if (!newParent->isRemovedFromCurrentState()) {
1589 addToCurrentState();
1590 } else {
1591 onRemovedFromCurrentState();
1592 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001593 } else {
1594 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001595 }
1596
chaviw06178942017-07-27 10:25:59 -07001597 return true;
1598}
1599
Peiyong Lind3788632018-09-18 16:01:31 -07001600bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001601 static const mat4 identityMatrix = mat4();
1602
Robert Carr6a160312021-05-17 12:08:20 -07001603 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001604 return false;
1605 }
Robert Carr6a160312021-05-17 12:08:20 -07001606 ++mDrawingState.sequence;
1607 mDrawingState.colorTransform = matrix;
1608 mDrawingState.hasColorTransform = matrix != identityMatrix;
1609 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001610 setTransactionFlags(eTransactionNeeded);
1611 return true;
1612}
1613
chaviwf66724d2018-11-28 16:35:21 -08001614mat4 Layer::getColorTransform() const {
1615 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001616 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001617 colorTransform = parent->getColorTransform() * colorTransform;
1618 }
1619 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001620}
1621
1622bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001623 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001624 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001625 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1626 }
1627 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001628}
1629
Chia-I Wu11481472018-05-04 10:43:19 -07001630bool Layer::isLegacyDataSpace() const {
1631 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001632 return !(getDataSpace() &
1633 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1634 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001635}
1636
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001638 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639}
1640
Robert Carr6a160312021-05-17 12:08:20 -07001641int32_t Layer::getZ(LayerVector::StateSet) const {
1642 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643}
1644
Robert Carr1c5481e2019-07-01 14:42:27 -07001645bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001646 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001647 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001648 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001649}
1650
David Sodman41fdfc92017-11-06 16:09:56 -08001651__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001652 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001653 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1654 "makeTraversalList received invalid stateSet");
1655 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1656 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001657 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001658
Robert Carr29abff82017-12-04 13:51:20 -08001659 if (state.zOrderRelatives.size() == 0) {
1660 *outSkipRelativeZUsers = true;
1661 return children;
1662 }
1663
chaviwfd462612018-05-31 16:11:27 -07001664 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001665 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001666 sp<Layer> strongRelative = weakRelative.promote();
1667 if (strongRelative != nullptr) {
1668 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001669 }
1670 }
1671
Dan Stoza412903f2017-04-27 13:42:17 -07001672 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001673 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001674 continue;
1675 }
Robert Carrdb66e622017-04-10 16:55:57 -07001676 traverse.add(child);
1677 }
1678
1679 return traverse;
1680}
1681
Robert Carr1f0a16a2016-10-24 16:27:39 -07001682/**
Robert Carrdb66e622017-04-10 16:55:57 -07001683 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001684 */
Dan Stoza412903f2017-04-27 13:42:17 -07001685void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001686 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1687 // produce a new list for traversing, including our relatives, and not including our children
1688 // who are relatives of another surface. In the case that there are no relative Z,
1689 // makeTraversalList returns our children directly to avoid significant overhead.
1690 // However in this case we need to take the responsibility for filtering children which
1691 // are relatives of another surface here.
1692 bool skipRelativeZUsers = false;
1693 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001694
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001696 for (; i < list.size(); i++) {
1697 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001698 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1699 continue;
1700 }
1701
chaviw301b1d82019-11-06 13:15:09 -08001702 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001703 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001704 }
Dan Stoza412903f2017-04-27 13:42:17 -07001705 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001706 }
Robert Carr29abff82017-12-04 13:51:20 -08001707
Dan Stoza412903f2017-04-27 13:42:17 -07001708 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001709 for (; i < list.size(); i++) {
1710 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001711
Robert Carr29abff82017-12-04 13:51:20 -08001712 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1713 continue;
1714 }
Dan Stoza412903f2017-04-27 13:42:17 -07001715 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001716 }
1717}
1718
1719/**
Robert Carrdb66e622017-04-10 16:55:57 -07001720 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001721 */
Dan Stoza412903f2017-04-27 13:42:17 -07001722void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1723 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001724 // See traverseInZOrder for documentation.
1725 bool skipRelativeZUsers = false;
1726 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001727
Robert Carr1f0a16a2016-10-24 16:27:39 -07001728 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001729 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001730 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001731
1732 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1733 continue;
1734 }
1735
chaviw301b1d82019-11-06 13:15:09 -08001736 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001737 break;
1738 }
Dan Stoza412903f2017-04-27 13:42:17 -07001739 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740 }
Dan Stoza412903f2017-04-27 13:42:17 -07001741 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001742 for (; 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
Dan Stoza412903f2017-04-27 13:42:17 -07001749 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001750 }
1751}
1752
Edgar Arriaga844fa672020-01-16 14:21:42 -08001753void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1754 visitor(this);
1755 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001756 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001757 for (const sp<Layer>& child : children) {
1758 child->traverse(state, visitor);
1759 }
1760}
1761
chaviw4b129c22018-04-09 16:19:43 -07001762LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1763 const std::vector<Layer*>& layersInTree) {
1764 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1765 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001766 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1767 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001768 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001769
chaviwfd462612018-05-31 16:11:27 -07001770 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001771 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1772 sp<Layer> strongRelative = weakRelative.promote();
1773 // Only add relative layers that are also descendents of the top most parent of the tree.
1774 // If a relative layer is not a descendent, then it should be ignored.
1775 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1776 traverse.add(strongRelative);
1777 }
1778 }
1779
1780 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001781 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001782 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1783 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1784 // the child here since it won't be added later as a relative.
1785 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1786 childState.zOrderRelativeOf.promote().get())) {
1787 continue;
1788 }
1789 traverse.add(child);
1790 }
1791
1792 return traverse;
1793}
1794
1795void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1796 LayerVector::StateSet stateSet,
1797 const LayerVector::Visitor& visitor) {
1798 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001799
1800 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001801 for (; i < list.size(); i++) {
1802 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001803 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001804 break;
1805 }
chaviw4b129c22018-04-09 16:19:43 -07001806 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001807 }
chaviw4b129c22018-04-09 16:19:43 -07001808
chaviwa76b2712017-09-20 12:02:26 -07001809 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001810 for (; i < list.size(); i++) {
1811 const auto& relative = list[i];
1812 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001813 }
1814}
1815
chaviw4b129c22018-04-09 16:19:43 -07001816std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1817 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1818 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1819
1820 std::vector<Layer*> layersInTree = {this};
1821 for (size_t i = 0; i < children.size(); i++) {
1822 const auto& child = children[i];
1823 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1824 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1825 }
1826
1827 return layersInTree;
1828}
1829
1830void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1831 const LayerVector::Visitor& visitor) {
1832 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1833 std::sort(layersInTree.begin(), layersInTree.end());
1834 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1835}
1836
Peiyong Linefefaac2018-08-17 12:27:51 -07001837ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001838 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001839}
1840
Garfield Tan2c1782c2022-02-16 15:25:05 -08001841bool Layer::isTransformValid() const {
1842 float transformDet = getTransform().det();
1843 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
1844}
1845
chaviw13fdc492017-06-27 12:40:18 -07001846half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001847 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001848
chaviw13fdc492017-06-27 12:40:18 -07001849 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1850 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001851}
Robert Carr6452f122017-03-21 10:41:29 -07001852
Vishnu Nair6213bd92020-05-08 17:42:25 -07001853ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001854 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001855 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1856 return fixedTransformHint;
1857 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001858 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001859 if (!p) return fixedTransformHint;
1860 return p->getFixedTransformHint();
1861}
1862
chaviw13fdc492017-06-27 12:40:18 -07001863half4 Layer::getColor() const {
1864 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001865 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001866}
Robert Carr6452f122017-03-21 10:41:29 -07001867
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001868int32_t Layer::getBackgroundBlurRadius() const {
Vishnu Nair29810f72022-07-01 16:38:41 +00001869 if (getDrawingState().backgroundBlurRadius == 0) {
1870 return 0;
1871 }
Galia Peychevada7de0e2020-12-03 17:24:35 +01001872
Vishnu Nair29810f72022-07-01 16:38:41 +00001873 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001874 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1875 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001876}
1877
Galia Peychevae0acf382021-04-12 21:22:34 +02001878const std::vector<BlurRegion> Layer::getBlurRegions() const {
1879 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001880 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001881 for (auto& region : regionsCopy) {
1882 region.alpha = region.alpha * layerAlpha;
1883 }
1884 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001885}
1886
Vishnu Naire14c6b32022-08-06 04:20:15 +00001887RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001888 // Get parent settings
1889 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001890 const auto& parent = mDrawingParent.promote();
1891 if (parent != nullptr) {
1892 parentSettings = parent->getRoundedCornerState();
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001893 if (parentSettings.hasRoundedCorners()) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001894 ui::Transform t = getActiveTransform(getDrawingState());
1895 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001896 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001897 parentSettings.radius.x *= t.getScaleX();
1898 parentSettings.radius.y *= t.getScaleY();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001899 }
1900 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001901
1902 // Get layer settings
1903 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001904 const vec2 radius(getDrawingState().cornerRadius, getDrawingState().cornerRadius);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001905 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001906 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001907
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001908 if (layerSettingsValid && parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001909 // If the parent and the layer have rounded corner settings, use the parent settings if the
1910 // parent crop is entirely inside the layer crop.
1911 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1912 if (parentSettings.cropRect.left > layerCropRect.left &&
1913 parentSettings.cropRect.top > layerCropRect.top &&
1914 parentSettings.cropRect.right < layerCropRect.right &&
1915 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1916 return parentSettings;
1917 } else {
1918 return layerSettings;
1919 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001920 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001921 return layerSettings;
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001922 } else if (parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001923 return parentSettings;
1924 }
1925 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001926}
1927
Robert Carr88b85e12022-03-21 15:47:35 -07001928bool Layer::findInHierarchy(const sp<Layer>& l) {
1929 if (l == this) {
1930 return true;
1931 }
1932 for (auto& child : mDrawingChildren) {
1933 if (child->findInHierarchy(l)) {
1934 return true;
1935 }
1936 }
1937 return false;
1938}
1939
Robert Carr1f0a16a2016-10-24 16:27:39 -07001940void Layer::commitChildList() {
1941 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1942 const auto& child = mCurrentChildren[i];
1943 child->commitChildList();
1944 }
1945 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001946 mDrawingParent = mCurrentParent;
Robert Carr88b85e12022-03-21 15:47:35 -07001947 if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) {
1948 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1949 if (zOrderRelativeOf == nullptr) return;
1950 if (findInHierarchy(zOrderRelativeOf)) {
1951 ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(),
1952 zOrderRelativeOf->mName.c_str());
1953 ALOGE("Severing rel Z loop, potentially dangerous");
1954 mDrawingState.isRelativeOf = false;
Ady Abrahamd11bade2022-08-01 16:18:03 -07001955 zOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carr88b85e12022-03-21 15:47:35 -07001956 }
1957 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001958}
1959
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001960
chaviw3277faf2021-05-19 16:45:23 -05001961void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001962 mDrawingState.inputInfo = info;
Vishnu Nairf9096652021-07-20 18:49:42 -07001963 mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07001964 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +00001965 mFlinger->mUpdateInputInfo = true;
Robert Carr720e5062018-07-30 17:45:14 -07001966 setTransactionFlags(eTransactionNeeded);
1967}
1968
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001969LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) {
chaviw08f3cb22020-01-13 13:17:21 -08001970 LayerProto* layerProto = layersProto.add_layers();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001971 writeToProtoDrawingState(layerProto);
chaviw08f3cb22020-01-13 13:17:21 -08001972 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1973
Vishnu Nair00b90132021-11-05 14:03:40 -07001974 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001975 ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread.
1976
Vishnu Nair60db8c02020-04-02 11:55:16 -07001977 // Only populate for the primary display.
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001978 if (const auto display = mFlinger->getDefaultDisplayDeviceLocked()) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05001979 const auto compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07001980 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001981 LayerProtoHelper::writeToProto(getVisibleRegion(display.get()),
1982 [&]() { return layerProto->mutable_visible_region(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07001983 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08001984 }
1985
chaviw08f3cb22020-01-13 13:17:21 -08001986 for (const sp<Layer>& layer : mDrawingChildren) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001987 layer->writeToProto(layersProto, traceFlags);
chaviw08f3cb22020-01-13 13:17:21 -08001988 }
chaviw6d89e2d2020-01-14 14:42:01 -08001989
1990 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08001991}
1992
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001993void Layer::writeToProtoDrawingState(LayerProto* layerInfo) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001994 const ui::Transform transform = getTransform();
Vishnu Naird37343b2022-01-12 16:18:56 -08001995 auto buffer = getExternalTexture();
Vishnu Nair00b90132021-11-05 14:03:40 -07001996 if (buffer != nullptr) {
Vishnu Naird37343b2022-01-12 16:18:56 -08001997 LayerProtoHelper::writeToProto(*buffer,
Vishnu Nair00b90132021-11-05 14:03:40 -07001998 [&]() { return layerInfo->mutable_active_buffer(); });
1999 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
2000 layerInfo->mutable_buffer_transform());
2001 }
2002 layerInfo->set_invalidate(contentDirty);
2003 layerInfo->set_is_protected(isProtected());
2004 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
2005 layerInfo->set_queued_frames(getQueuedFrameCount());
Vishnu Nair00b90132021-11-05 14:03:40 -07002006 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nair00b90132021-11-05 14:03:40 -07002007 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07002008 layerInfo->set_corner_radius(
2009 (getRoundedCornerState().radius.x + getRoundedCornerState().radius.y) / 2.0);
Vishnu Nair00b90132021-11-05 14:03:40 -07002010 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
2011 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
2012 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
2013 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2014 [&]() { return layerInfo->mutable_position(); });
2015 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair00b90132021-11-05 14:03:40 -07002016 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2017 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07002018
Vishnu Nair00b90132021-11-05 14:03:40 -07002019 if (hasColorTransform()) {
2020 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002021 }
2022
Vishnu Nair60db8c02020-04-02 11:55:16 -07002023 LayerProtoHelper::writeToProto(mSourceBounds,
2024 [&]() { return layerInfo->mutable_source_bounds(); });
2025 LayerProtoHelper::writeToProto(mScreenBounds,
2026 [&]() { return layerInfo->mutable_screen_bounds(); });
2027 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2028 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2029 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002030}
2031
2032void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002033 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002034 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2035 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002036 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002037
chaviw766c9c52021-02-10 17:36:47 -08002038 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002039
Vishnu Nair00b90132021-11-05 14:03:40 -07002040 layerInfo->set_id(sequence);
2041 layerInfo->set_name(getName().c_str());
2042 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002043
Vishnu Nair00b90132021-11-05 14:03:40 -07002044 for (const auto& child : children) {
2045 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002046 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002047
Vishnu Nair00b90132021-11-05 14:03:40 -07002048 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2049 sp<Layer> strongRelative = weakRelative.promote();
2050 if (strongRelative != nullptr) {
2051 layerInfo->add_relatives(strongRelative->sequence);
2052 }
2053 }
2054
Vishnu Nairea04b6f2022-08-19 21:28:17 +00002055 LayerProtoHelper::writeToProto(state.transparentRegionHint,
Vishnu Nair00b90132021-11-05 14:03:40 -07002056 [&]() { return layerInfo->mutable_transparent_region(); });
2057
2058 layerInfo->set_layer_stack(getLayerStack().id);
2059 layerInfo->set_z(state.z);
2060
2061 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2062 return layerInfo->mutable_requested_position();
2063 });
2064
Vishnu Nair00b90132021-11-05 14:03:40 -07002065 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2066
2067 layerInfo->set_is_opaque(isOpaque(state));
2068
2069 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2070 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2071 LayerProtoHelper::writeToProto(state.color,
2072 [&]() { return layerInfo->mutable_requested_color(); });
2073 layerInfo->set_flags(state.flags);
2074
2075 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2076 layerInfo->mutable_requested_transform());
2077
2078 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2079 if (parent != nullptr) {
2080 layerInfo->set_parent(parent->sequence);
2081 } else {
2082 layerInfo->set_parent(-1);
2083 }
2084
2085 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2086 if (zOrderRelativeOf != nullptr) {
2087 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2088 } else {
2089 layerInfo->set_z_order_relative_of(-1);
2090 }
2091
2092 layerInfo->set_is_relative_of(state.isRelativeOf);
2093
2094 layerInfo->set_owner_uid(mOwnerUid);
2095
Arthur Hung4cf2d8c2022-04-07 14:52:00 +00002096 if ((traceFlags & LayerTracing::TRACE_INPUT) && needsInputInfo()) {
chaviw3277faf2021-05-19 16:45:23 -05002097 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002098 if (useDrawing) {
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002099 info = fillInputInfo(
2100 InputDisplayArgs{.transform = &kIdentityTransform, .isSecure = true});
chaviw4c34a092020-07-08 11:30:06 -07002101 } else {
2102 info = state.inputInfo;
2103 }
2104
2105 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002106 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002107 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002108
Vishnu Nair00b90132021-11-05 14:03:40 -07002109 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002110 auto protoMap = layerInfo->mutable_metadata();
2111 for (const auto& entry : state.metadata.mMap) {
2112 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2113 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002114 }
Vishnu Naird2aaab12022-02-10 14:49:09 -08002115
2116 LayerProtoHelper::writeToProto(state.destinationFrame,
2117 [&]() { return layerInfo->mutable_destination_frame(); });
chaviw1d044282017-09-27 12:19:28 -07002118}
2119
Robert Carr2e102c92018-10-23 12:11:15 -07002120bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002121 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002122}
2123
Prabir Pradhan33da9462022-06-14 14:55:57 +00002124// Applies the given transform to the region, while protecting against overflows caused by any
2125// offsets. If applying the offset in the transform to any of the Rects in the region would result
2126// in an overflow, they are not added to the output Region.
2127static Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
2128 const std::string& debugWindowName) {
2129 // Round the translation using the same rounding strategy used by ui::Transform.
2130 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
2131 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
2132
2133 ui::Transform transformWithoutOffset = t;
2134 transformWithoutOffset.set(0.f, 0.f);
2135
2136 const Region transformed = transformWithoutOffset.transform(r);
2137
2138 // Apply the translation to each of the Rects in the region while discarding any that overflow.
2139 Region ret;
2140 for (const auto& rect : transformed) {
2141 Rect newRect;
2142 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
2143 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
2144 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
2145 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
2146 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
2147 debugWindowName.c_str());
2148 continue;
2149 }
2150 ret.orSelf(newRect);
2151 }
2152 return ret;
2153}
2154
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002155void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& screenToDisplay) {
2156 Rect tmpBounds = getInputBounds();
2157 if (!tmpBounds.isValid()) {
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08002158 info.touchableRegion.clear();
2159 // A layer could have invalid input bounds and still expect to receive touch input if it has
2160 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
2161 // correctly to determine the coordinate space for input events. Use an empty rect so that
2162 // the layer will receive input in its own layer space.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002163 tmpBounds = Rect::EMPTY_RECT;
chaviw7e72caf2020-12-02 16:50:43 -08002164 }
2165
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002166 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
2167 // frame and transform used for the layer, which determines the bounds and the coordinate space
2168 // within which the layer will receive input.
2169 //
2170 // The coordinate space within which each of the bounds are specified is explicitly documented
2171 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
2172 // Transform converts one coordinate space to another, which is apparent in its naming. For
2173 // example, "layerToDisplay" transforms layer space to display space.
2174 //
2175 // Coordinate space definitions:
2176 // - display: The display device's coordinate space. Correlates to pixels on the display.
2177 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
2178 // - layer: The coordinate space of this layer.
2179 // - input: The coordinate space in which this layer will receive input events. This could be
2180 // different than layer space if a surfaceInset is used, which changes the origin
2181 // of the input space.
2182 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
chaviw7e72caf2020-12-02 16:50:43 -08002183
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002184 // Clamp surface inset to the input bounds.
2185 const auto surfaceInset = static_cast<float>(info.surfaceInset);
2186 const float xSurfaceInset =
2187 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
2188 const float ySurfaceInset =
2189 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
chaviw1ff3d1e2020-07-01 15:53:47 -07002190
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002191 // Apply the insets to the input bounds.
2192 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
2193 inputBoundsInLayer.top + ySurfaceInset,
2194 inputBoundsInLayer.right - xSurfaceInset,
2195 inputBoundsInLayer.bottom - ySurfaceInset);
Ady Abraham282f1d72019-07-24 18:05:56 -07002196
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002197 // Crop the input bounds to ensure it is within the parent's bounds.
2198 const FloatRect croppedInsetBoundsInLayer = mBounds.intersect(insetBoundsInLayer);
Ady Abraham282f1d72019-07-24 18:05:56 -07002199
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002200 const ui::Transform layerToScreen = getInputTransform();
2201 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
Vishnu Nair8033a492018-12-05 07:27:23 -08002202
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002203 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
2204 info.frameLeft = roundedFrameInDisplay.left;
2205 info.frameTop = roundedFrameInDisplay.top;
2206 info.frameRight = roundedFrameInDisplay.right;
2207 info.frameBottom = roundedFrameInDisplay.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -07002208
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002209 ui::Transform inputToLayer;
2210 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
2211 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
chaviw39cfa2e2020-11-04 14:19:02 -08002212
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002213 // InputDispatcher expects a display-to-input transform.
2214 info.transform = inputToDisplay.inverse();
2215
2216 // The touchable region is specified in the input coordinate space. Change it to display space.
Prabir Pradhan33da9462022-06-14 14:55:57 +00002217 info.touchableRegion =
2218 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, mName);
chaviw7e72caf2020-12-02 16:50:43 -08002219}
2220
chaviw3277faf2021-05-19 16:45:23 -05002221void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002222 sp<Layer> p = sp<Layer>::fromExisting(this);
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002223 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002224 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002225 }
2226 if (p != nullptr) {
2227 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2228 }
2229}
2230
Vishnu Naira066d902021-09-13 18:40:17 -07002231gui::DropInputMode Layer::getDropInputMode() const {
2232 gui::DropInputMode mode = mDrawingState.dropInputMode;
2233 if (mode == gui::DropInputMode::ALL) {
2234 return mode;
2235 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002236 sp<Layer> parent = mDrawingParent.promote();
2237 if (parent) {
2238 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002239 if (parentMode != gui::DropInputMode::NONE) {
2240 return parentMode;
2241 }
2242 }
2243 return mode;
2244}
2245
2246void Layer::handleDropInputMode(gui::WindowInfo& info) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002247 if (mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Vishnu Naira066d902021-09-13 18:40:17 -07002248 return;
2249 }
2250
2251 // Check if we need to drop input unconditionally
2252 gui::DropInputMode dropInputMode = getDropInputMode();
2253 if (dropInputMode == gui::DropInputMode::ALL) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002254 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002255 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2256 return;
2257 }
2258
2259 // Check if we need to check if the window is obscured by parent
2260 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2261 return;
2262 }
2263
2264 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002265 sp<Layer> parent = mDrawingParent.promote();
2266 if (parent && parent->getAlpha() != 1.0_hf) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002267 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002268 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2269 static_cast<float>(getAlpha()));
2270 }
2271
2272 // Check if the parent has cropped the buffer
2273 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2274 if (!bufferSize.isValid()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002275 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002276 return;
2277 }
2278
2279 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2280 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2281 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2282 // match then the layer has not been cropped by its parents.
2283 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2284 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2285
2286 if (croppedByParent) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002287 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002288 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2289 getDebugName());
2290 } else {
2291 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2292 // input if the window is obscured. This check should be done in surfaceflinger but the
2293 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2294 // drop input events if the window is obscured.
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002295 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002296 }
2297}
2298
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002299WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) {
chaviw7e72caf2020-12-02 16:50:43 -08002300 if (!hasInputInfo()) {
2301 mDrawingState.inputInfo.name = getName();
2302 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2303 mDrawingState.inputInfo.ownerPid = mOwnerPid;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002304 mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002305 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002306 }
2307
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002308 const ui::Transform& displayTransform =
2309 displayArgs.transform != nullptr ? *displayArgs.transform : kIdentityTransform;
2310
chaviw3277faf2021-05-19 16:45:23 -05002311 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002312 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002313 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002314
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002315 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002316
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002317 if (displayArgs.transform == nullptr) {
2318 // Do not let the window receive touches if it is not associated with a valid display
2319 // transform. We still allow the window to receive keys and prevent ANRs.
2320 info.inputConfig |= WindowInfo::InputConfig::NOT_TOUCHABLE;
2321 }
2322
Robert Carr5dc426e2020-06-10 14:29:14 -07002323 // For compatibility reasons we let layers which can receive input
2324 // receive input before they have actually submitted a buffer. Because
2325 // of this we use canReceiveInput instead of isVisible to check the
2326 // policy-visibility, ignoring the buffer state. However for layers with
2327 // hasInputInfo()==false we can use the real visibility state.
2328 // We are just using these layers for occlusion detection in
2329 // InputDispatcher, and obviously if they aren't visible they can't occlude
2330 // anything.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002331 const bool visible = hasInputInfo() ? canReceiveInput() : isVisible();
2332 info.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
2333
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002334 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002335 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002336 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002337
Vishnu Nair16a938f2021-09-24 07:14:54 -07002338 // If the window will be blacked out on a display because the display does not have the secure
2339 // flag and the layer has the secure flag set, then drop input.
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002340 if (!displayArgs.isSecure && isSecure()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002341 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Nair16a938f2021-09-24 07:14:54 -07002342 }
2343
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002344 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2345 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002346 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2347 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002348 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002349 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002350 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002351 }
2352
Winson Chunga30f7c92021-06-29 15:42:56 -07002353 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2354 // if it was set by WM for a known system overlay
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002355 if (isTrustedOverlay()) {
2356 info.inputConfig |= WindowInfo::InputConfig::TRUSTED_OVERLAY;
2357 }
Winson Chunga30f7c92021-06-29 15:42:56 -07002358
chaviwaf87b3e2019-10-01 16:59:28 -07002359 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2360 // touches from going outside the cloned area.
2361 if (isClone()) {
chaviw8577ea82022-06-01 16:32:26 -05002362 info.inputConfig |= WindowInfo::InputConfig::CLONE;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002363 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2364 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002365 info.touchableRegion = info.touchableRegion.intersect(rect);
2366 }
2367 }
2368
Robert Carr720e5062018-07-30 17:45:14 -07002369 return info;
2370}
2371
chaviwaf87b3e2019-10-01 16:59:28 -07002372sp<Layer> Layer::getClonedRoot() {
2373 if (mClonedChild != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002374 return sp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002375 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002376 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002377 return nullptr;
2378 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002379 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002380}
2381
Robert Carredd13602020-04-13 17:24:34 -07002382bool Layer::hasInputInfo() const {
Linus Tufvessona1858822022-03-04 09:32:07 +00002383 return mDrawingState.inputInfo.token != nullptr ||
2384 mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Robert Carr720e5062018-07-30 17:45:14 -07002385}
2386
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002387compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002388 const DisplayDevice* display) const {
2389 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002390 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002391}
2392
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002393Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2394 const auto outputLayer = findOutputLayerForDisplay(display);
2395 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002396}
2397
chaviwb4c6e582019-08-16 14:35:07 -07002398void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002399 cloneDrawingState(clonedFrom.get());
chaviwb4c6e582019-08-16 14:35:07 -07002400 mClonedFrom = clonedFrom;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002401 mPremultipliedAlpha = clonedFrom->mPremultipliedAlpha;
2402 mPotentialCursor = clonedFrom->mPotentialCursor;
2403 mProtectedByApp = clonedFrom->mProtectedByApp;
2404 updateCloneBufferInfo();
2405}
2406
2407void Layer::updateCloneBufferInfo() {
2408 if (!isClone() || !isClonedFromAlive()) {
2409 return;
2410 }
2411
2412 sp<Layer> clonedFrom = getClonedFrom();
2413 mBufferInfo = clonedFrom->mBufferInfo;
2414 mSidebandStream = clonedFrom->mSidebandStream;
2415 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
2416 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
2417 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
2418
2419 // After buffer info is updated, the drawingState from the real layer needs to be copied into
2420 // the cloned. This is because some properties of drawingState can change when latchBuffer is
2421 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
2422 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
2423 // the cloned drawingState again.
2424 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
2425 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
2426 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
2427 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
2428
2429 cloneDrawingState(clonedFrom.get());
2430
2431 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
2432 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
2433 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
2434 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -07002435}
chaviw74b03172019-08-19 11:09:03 -07002436
2437void Layer::updateMirrorInfo() {
2438 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2439 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2440 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2441 // that case, we want to delete the reference to the clone since we want it to get
2442 // destroyed. The root, this layer, will still be around since the client can continue
2443 // to hold a reference, but no cloned layers will be displayed.
2444 mClonedChild = nullptr;
2445 return;
2446 }
2447
2448 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2449 // If the real layer exists and is in current state, add the clone as a child of the root.
2450 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2451 // copied to drawingState for the root layer. So the clonedChild is always removed from
2452 // drawingState and then needs to be added back each traversal.
2453 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2454 addChildToDrawing(mClonedChild);
2455 }
2456
2457 mClonedChild->updateClonedDrawingState(clonedLayersMap);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002458 mClonedChild->updateClonedChildren(sp<Layer>::fromExisting(this), clonedLayersMap);
chaviw74b03172019-08-19 11:09:03 -07002459 mClonedChild->updateClonedRelatives(clonedLayersMap);
2460}
2461
2462void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2463 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2464 // to the clone. If the real layer is no longer alive, continue traversing the children
2465 // since we may be able to pull out other children that are still alive.
2466 if (isClonedFromAlive()) {
2467 sp<Layer> clonedFrom = getClonedFrom();
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002468 cloneDrawingState(clonedFrom.get());
Ady Abrahamd11bade2022-08-01 16:18:03 -07002469 clonedLayersMap.emplace(clonedFrom, sp<Layer>::fromExisting(this));
chaviw74b03172019-08-19 11:09:03 -07002470 }
2471
2472 // The clone layer may have children in drawingState since they may have been created and
2473 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2474 // that already exist, since we can just re-use them.
2475 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2476 // not updated in the regular traversal. They are skipped since the root will lose the
2477 // reference to them when it copies its currentChildren to drawing.
2478 for (sp<Layer>& child : mDrawingChildren) {
2479 child->updateClonedDrawingState(clonedLayersMap);
2480 }
2481}
2482
2483void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2484 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2485 mDrawingChildren.clear();
2486
2487 if (!isClonedFromAlive()) {
2488 return;
2489 }
2490
2491 sp<Layer> clonedFrom = getClonedFrom();
2492 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2493 if (child == mirrorRoot) {
2494 // This is to avoid cyclical mirroring.
2495 continue;
2496 }
2497 sp<Layer> clonedChild = clonedLayersMap[child];
2498 if (clonedChild == nullptr) {
2499 clonedChild = child->createClone();
2500 clonedLayersMap[child] = clonedChild;
2501 }
2502 addChildToDrawing(clonedChild);
2503 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2504 }
2505}
2506
chaviwaf87b3e2019-10-01 16:59:28 -07002507void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2508 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2509 if (cropLayer != nullptr) {
2510 if (clonedLayersMap.count(cropLayer) == 0) {
2511 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2512 // self as crop layer to avoid going outside bounds.
Ady Abrahamd11bade2022-08-01 16:18:03 -07002513 mDrawingState.touchableRegionCrop = wp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002514 } else {
2515 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2516 mDrawingState.touchableRegionCrop = clonedCropLayer;
2517 }
2518 }
2519 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2520 // WM or the client.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002521 mDrawingState.inputInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, false);
chaviwaf87b3e2019-10-01 16:59:28 -07002522}
2523
2524void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002525 mDrawingState.zOrderRelativeOf = wp<Layer>();
chaviw74b03172019-08-19 11:09:03 -07002526 mDrawingState.zOrderRelatives.clear();
2527
2528 if (!isClonedFromAlive()) {
2529 return;
2530 }
2531
chaviwaf87b3e2019-10-01 16:59:28 -07002532 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002533 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002534 const sp<Layer>& relative = relativeWeak.promote();
2535 if (clonedLayersMap.count(relative) > 0) {
2536 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002537 mDrawingState.zOrderRelatives.add(clonedRelative);
2538 }
2539 }
2540
2541 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2542 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2543 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2544 // still traverse the children, but the layer with the missing relativeOf will not be shown
2545 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002546 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2547 if (clonedLayersMap.count(relativeOf) > 0) {
2548 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002549 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2550 }
2551
chaviwaf87b3e2019-10-01 16:59:28 -07002552 updateClonedInputInfo(clonedLayersMap);
2553
chaviw74b03172019-08-19 11:09:03 -07002554 for (sp<Layer>& child : mDrawingChildren) {
2555 child->updateClonedRelatives(clonedLayersMap);
2556 }
2557}
2558
2559void Layer::addChildToDrawing(const sp<Layer>& layer) {
2560 mDrawingChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002561 layer->mDrawingParent = sp<Layer>::fromExisting(this);
chaviw74b03172019-08-19 11:09:03 -07002562}
2563
Steven Thomas62a4cf82020-01-31 12:04:03 -08002564Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2565 switch (compatibility) {
2566 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2567 return FrameRateCompatibility::Default;
2568 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2569 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002570 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2571 return FrameRateCompatibility::Exact;
Andy Labrada096227e2022-06-15 16:58:11 +00002572 case ANATIVEWINDOW_FRAME_RATE_MIN:
2573 return FrameRateCompatibility::Min;
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002574 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
2575 return FrameRateCompatibility::NoVote;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002576 default:
2577 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2578 return FrameRateCompatibility::Default;
2579 }
2580}
2581
Marin Shalamanovc5986772021-03-16 16:09:49 +01002582scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2583 switch (strategy) {
2584 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2585 return Seamlessness::OnlySeamless;
2586 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2587 return Seamlessness::SeamedAndSeamless;
2588 default:
2589 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2590 return Seamlessness::Default;
2591 }
2592}
2593
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002594bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002595 const State& s(mDrawingState);
2596 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2597 return true;
2598 }
2599
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002600 sp<Layer> parent = mDrawingParent.promote();
2601 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002602}
2603
Robert Carr6a0382d2021-07-01 15:57:17 -07002604void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2605 mClonedChild = clonedChild;
2606 mHadClonedChild = true;
2607 mFlinger->mNumClones++;
2608}
2609
Vishnu Nairf9096652021-07-20 18:49:42 -07002610const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle");
2611
2612wp<Layer> Layer::fromHandle(const sp<IBinder>& handleBinder) {
2613 if (handleBinder == nullptr) {
2614 return nullptr;
2615 }
2616
2617 BBinder* b = handleBinder->localBinder();
2618 if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) {
2619 return nullptr;
2620 }
2621
2622 // We can safely cast this binder since its local and we verified its interface descriptor.
Ady Abrahamd11bade2022-08-01 16:18:03 -07002623 sp<Handle> handle = sp<Handle>::cast(handleBinder);
Vishnu Nairf9096652021-07-20 18:49:42 -07002624 return handle->owner;
2625}
2626
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002627bool Layer::setDropInputMode(gui::DropInputMode mode) {
2628 if (mDrawingState.dropInputMode == mode) {
2629 return false;
2630 }
2631 mDrawingState.dropInputMode = mode;
2632 return true;
2633}
2634
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002635void Layer::cloneDrawingState(const Layer* from) {
2636 mDrawingState = from->mDrawingState;
2637 // Skip callback info since they are not applicable for cloned layers.
2638 mDrawingState.releaseBufferListener = nullptr;
2639 mDrawingState.callbackHandles = {};
2640}
2641
Patrick Williamsbb25f802022-08-30 23:02:34 +00002642void Layer::callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
2643 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
2644 const sp<Fence>& releaseFence,
2645 uint32_t currentMaxAcquiredBufferCount) {
2646 if (!listener) {
2647 return;
2648 }
2649 ATRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, getDebugName(), framenumber);
2650 listener->onReleaseBuffer({buffer->getId(), framenumber},
2651 releaseFence ? releaseFence : Fence::NO_FENCE,
2652 currentMaxAcquiredBufferCount);
2653}
2654
2655void Layer::onLayerDisplayed(ftl::SharedFuture<FenceResult> futureFenceResult) {
2656 // If we are displayed on multiple displays in a single composition cycle then we would
2657 // need to do careful tracking to enable the use of the mLastClientCompositionFence.
2658 // For example we can only use it if all the displays are client comp, and we need
2659 // to merge all the client comp fences. We could do this, but for now we just
2660 // disable the optimization when a layer is composed on multiple displays.
2661 if (mClearClientCompositionFenceOnLayerDisplayed) {
2662 mLastClientCompositionFence = nullptr;
2663 } else {
2664 mClearClientCompositionFenceOnLayerDisplayed = true;
2665 }
2666
2667 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
2668 // buffer that was presented on this layer. The first transaction that came in this frame that
2669 // replaced the previous buffer on this layer needs this release fence, because the fence will
2670 // let the client know when that previous buffer is removed from the screen.
2671 //
2672 // Every other transaction on this layer does not need a release fence because no other
2673 // Transactions that were set on this layer this frame are going to have their preceding buffer
2674 // removed from the display this frame.
2675 //
2676 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
2677 // buffer so it doesn't need a previous release fence because the layer still needs the previous
2678 // buffer. The second transaction contains a buffer so it needs a previous release fence because
2679 // the previous buffer will be released this frame. The third transaction also contains a
2680 // buffer. It replaces the buffer in the second transaction. The buffer in the second
2681 // transaction will now no longer be presented so it is released immediately and the third
2682 // transaction doesn't need a previous release fence.
2683 sp<CallbackHandle> ch;
2684 for (auto& handle : mDrawingState.callbackHandles) {
2685 if (handle->releasePreviousBuffer &&
2686 mDrawingState.releaseBufferEndpoint == handle->listener) {
2687 ch = handle;
2688 break;
2689 }
2690 }
2691
2692 // Prevent tracing the same release multiple times.
2693 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
2694 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
2695 }
2696
2697 if (ch != nullptr) {
2698 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2699 ch->previousReleaseFences.emplace_back(std::move(futureFenceResult));
2700 ch->name = mName;
2701 }
2702}
2703
2704void Layer::onSurfaceFrameCreated(
2705 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
2706 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
2707 // Too many SurfaceFrames pending classification. The front of the deque is probably not
2708 // tracked by FrameTimeline and will never be presented. This will only result in a memory
2709 // leak.
2710 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
2711 mName.c_str());
2712 std::string miniDump = mPendingJankClassifications.front()->miniDump();
2713 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
2714 mPendingJankClassifications.pop_front();
2715 }
2716 mPendingJankClassifications.emplace_back(surfaceFrame);
2717}
2718
2719void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
2720 for (const auto& handle : mDrawingState.callbackHandles) {
2721 handle->transformHint = mTransformHint;
2722 handle->dequeueReadyTime = dequeueReadyTime;
2723 handle->currentMaxAcquiredBufferCount =
2724 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
2725 ATRACE_FORMAT_INSTANT("releasePendingBuffer %s - %" PRIu64, getDebugName(),
2726 handle->previousReleaseCallbackId.framenumber);
2727 }
2728
2729 for (auto& handle : mDrawingState.callbackHandles) {
2730 if (handle->releasePreviousBuffer &&
2731 mDrawingState.releaseBufferEndpoint == handle->listener) {
2732 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2733 break;
2734 }
2735 }
2736
2737 std::vector<JankData> jankData;
2738 jankData.reserve(mPendingJankClassifications.size());
2739 while (!mPendingJankClassifications.empty() &&
2740 mPendingJankClassifications.front()->getJankType()) {
2741 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
2742 mPendingJankClassifications.front();
2743 mPendingJankClassifications.pop_front();
2744 jankData.emplace_back(
2745 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
2746 }
2747
2748 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(mDrawingState.callbackHandles,
2749 jankData);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002750 mDrawingState.callbackHandles = {};
2751}
2752
2753bool Layer::willPresentCurrentTransaction() const {
2754 // Returns true if the most recent Transaction applied to CurrentState will be presented.
2755 return (getSidebandStreamChanged() || getAutoRefresh() ||
2756 (mDrawingState.modified &&
2757 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
2758}
2759
2760bool Layer::setTransform(uint32_t transform) {
2761 if (mDrawingState.bufferTransform == transform) return false;
2762 mDrawingState.bufferTransform = transform;
2763 mDrawingState.modified = true;
2764 setTransactionFlags(eTransactionNeeded);
2765 return true;
2766}
2767
2768bool Layer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
2769 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
2770 mDrawingState.sequence++;
2771 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
2772 mDrawingState.modified = true;
2773 setTransactionFlags(eTransactionNeeded);
2774 return true;
2775}
2776
2777bool Layer::setBufferCrop(const Rect& bufferCrop) {
2778 if (mDrawingState.bufferCrop == bufferCrop) return false;
2779
2780 mDrawingState.sequence++;
2781 mDrawingState.bufferCrop = bufferCrop;
2782
2783 mDrawingState.modified = true;
2784 setTransactionFlags(eTransactionNeeded);
2785 return true;
2786}
2787
2788bool Layer::setDestinationFrame(const Rect& destinationFrame) {
2789 if (mDrawingState.destinationFrame == destinationFrame) return false;
2790
2791 mDrawingState.sequence++;
2792 mDrawingState.destinationFrame = destinationFrame;
2793
2794 mDrawingState.modified = true;
2795 setTransactionFlags(eTransactionNeeded);
2796 return true;
2797}
2798
2799// Translate destination frame into scale and position. If a destination frame is not set, use the
2800// provided scale and position
2801bool Layer::updateGeometry() {
2802 if ((mDrawingState.flags & layer_state_t::eIgnoreDestinationFrame) ||
2803 mDrawingState.destinationFrame.isEmpty()) {
2804 // If destination frame is not set, use the requested transform set via
2805 // Layer::setPosition and Layer::setMatrix.
2806 return assignTransform(&mDrawingState.transform, mRequestedTransform);
2807 }
2808
2809 Rect destRect = mDrawingState.destinationFrame;
2810 int32_t destW = destRect.width();
2811 int32_t destH = destRect.height();
2812 if (destRect.left < 0) {
2813 destRect.left = 0;
2814 destRect.right = destW;
2815 }
2816 if (destRect.top < 0) {
2817 destRect.top = 0;
2818 destRect.bottom = destH;
2819 }
2820
2821 if (!mDrawingState.buffer) {
2822 ui::Transform t;
2823 t.set(destRect.left, destRect.top);
2824 return assignTransform(&mDrawingState.transform, t);
2825 }
2826
2827 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
2828 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
2829 // Undo any transformations on the buffer.
2830 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
2831 std::swap(bufferWidth, bufferHeight);
2832 }
2833 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
2834 if (mDrawingState.transformToDisplayInverse) {
2835 if (invTransform & ui::Transform::ROT_90) {
2836 std::swap(bufferWidth, bufferHeight);
2837 }
2838 }
2839
2840 float sx = destW / static_cast<float>(bufferWidth);
2841 float sy = destH / static_cast<float>(bufferHeight);
2842 ui::Transform t;
2843 t.set(sx, 0, 0, sy);
2844 t.set(destRect.left, destRect.top);
2845 return assignTransform(&mDrawingState.transform, t);
2846}
2847
2848bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
2849 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
2850 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
2851 return false;
2852 }
2853
2854 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
2855
2856 mDrawingState.sequence++;
2857 mDrawingState.modified = true;
2858 setTransactionFlags(eTransactionNeeded);
2859
2860 return true;
2861}
2862
2863bool Layer::setPosition(float x, float y) {
2864 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
2865 return false;
2866 }
2867
2868 mRequestedTransform.set(x, y);
2869
2870 mDrawingState.sequence++;
2871 mDrawingState.modified = true;
2872 setTransactionFlags(eTransactionNeeded);
2873
2874 return true;
2875}
2876
2877bool Layer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
2878 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
2879 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
2880 const FrameTimelineInfo& info) {
Chavi Weingarten009619f2022-09-15 20:27:25 +00002881 ATRACE_FORMAT("setBuffer %s - hasBuffer=%s", getDebugName(), (buffer ? "true" : "false"));
Patrick Williamsbb25f802022-08-30 23:02:34 +00002882 if (!buffer) {
2883 return false;
2884 }
2885
2886 const bool frameNumberChanged =
2887 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
2888 const uint64_t frameNumber =
2889 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
Chavi Weingarten009619f2022-09-15 20:27:25 +00002890 ATRACE_FORMAT_INSTANT("setBuffer %s - %" PRIu64, getDebugName(), frameNumber);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002891
2892 if (mDrawingState.buffer) {
2893 mReleasePreviousBuffer = true;
2894 if (!mBufferInfo.mBuffer ||
2895 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
2896 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
2897 // If mDrawingState has a buffer, and we are about to update again
2898 // before swapping to drawing state, then the first buffer will be
2899 // dropped and we should decrement the pending buffer count and
2900 // call any release buffer callbacks if set.
2901 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2902 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2903 mDrawingState.acquireFence,
2904 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2905 mOwnerUid));
2906 decrementPendingBufferCount();
2907 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
2908 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
2909 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
2910 mDrawingState.bufferSurfaceFrameTX.reset();
2911 }
2912 } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) {
2913 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2914 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2915 mLastClientCompositionFence,
2916 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2917 mOwnerUid));
2918 mLastClientCompositionFence = nullptr;
2919 }
2920 }
2921
2922 mDrawingState.frameNumber = frameNumber;
2923 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
2924 mDrawingState.buffer = std::move(buffer);
2925 mDrawingState.clientCacheId = bufferData.cachedBuffer;
2926
2927 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
2928 ? bufferData.acquireFence
2929 : Fence::NO_FENCE;
2930 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
2931 if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
2932 // We latched this buffer unsiganled, so we need to pass the acquire fence
2933 // on the callback instead of just the acquire time, since it's unknown at
2934 // this point.
2935 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence;
2936 } else {
2937 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime();
2938 }
2939
2940 mDrawingState.modified = true;
2941 setTransactionFlags(eTransactionNeeded);
2942
2943 const int32_t layerId = getSequence();
2944 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
2945 mOwnerUid, postTime, getGameMode());
2946 mDrawingState.desiredPresentTime = desiredPresentTime;
2947 mDrawingState.isAutoTimestamp = isAutoTimestamp;
2948
2949 const nsecs_t presentTime = [&] {
2950 if (!isAutoTimestamp) return desiredPresentTime;
2951
2952 const auto prediction =
2953 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
2954 if (prediction.has_value()) return prediction->presentTime;
2955
2956 return static_cast<nsecs_t>(0);
2957 }();
2958
2959 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
2960 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
2961
2962 setFrameTimelineVsyncForBufferTransaction(info, postTime);
2963
2964 if (dequeueTime && *dequeueTime != 0) {
2965 const uint64_t bufferId = mDrawingState.buffer->getId();
2966 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
2967 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
2968 FrameTracer::FrameEvent::DEQUEUE);
2969 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
2970 FrameTracer::FrameEvent::QUEUE);
2971 }
2972
2973 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
2974 return true;
2975}
2976
2977bool Layer::setDataspace(ui::Dataspace dataspace) {
2978 mDrawingState.dataspaceRequested = true;
2979 if (mDrawingState.dataspace == dataspace) return false;
2980 mDrawingState.dataspace = dataspace;
2981 mDrawingState.modified = true;
2982 setTransactionFlags(eTransactionNeeded);
2983 return true;
2984}
2985
2986bool Layer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
2987 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
2988 mDrawingState.hdrMetadata = hdrMetadata;
2989 mDrawingState.modified = true;
2990 setTransactionFlags(eTransactionNeeded);
2991 return true;
2992}
2993
2994bool Layer::setSurfaceDamageRegion(const Region& surfaceDamage) {
2995 mDrawingState.surfaceDamageRegion = surfaceDamage;
2996 mDrawingState.modified = true;
2997 setTransactionFlags(eTransactionNeeded);
2998 return true;
2999}
3000
3001bool Layer::setApi(int32_t api) {
3002 if (mDrawingState.api == api) return false;
3003 mDrawingState.api = api;
3004 mDrawingState.modified = true;
3005 setTransactionFlags(eTransactionNeeded);
3006 return true;
3007}
3008
3009bool Layer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
3010 if (mDrawingState.sidebandStream == sidebandStream) return false;
3011
3012 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
3013 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
3014 } else if (sidebandStream != nullptr) {
3015 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
3016 }
3017
3018 mDrawingState.sidebandStream = sidebandStream;
3019 mDrawingState.modified = true;
3020 setTransactionFlags(eTransactionNeeded);
3021 if (!mSidebandStreamChanged.exchange(true)) {
3022 // mSidebandStreamChanged was false
3023 mFlinger->onLayerUpdate();
3024 }
3025 return true;
3026}
3027
3028bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
3029 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
3030 if (handles.empty()) {
3031 mReleasePreviousBuffer = false;
3032 return false;
3033 }
3034
3035 const bool willPresent = willPresentCurrentTransaction();
3036
3037 for (const auto& handle : handles) {
3038 // If this transaction set a buffer on this layer, release its previous buffer
3039 handle->releasePreviousBuffer = mReleasePreviousBuffer;
3040
3041 // If this layer will be presented in this frame
3042 if (willPresent) {
3043 // If this transaction set an acquire fence on this layer, set its acquire time
3044 handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence;
3045 handle->frameNumber = mDrawingState.frameNumber;
3046
3047 // Store so latched time and release fence can be set
3048 mDrawingState.callbackHandles.push_back(handle);
3049
3050 } else { // If this layer will NOT need to be relatched and presented this frame
3051 // Notify the transaction completed thread this handle is done
3052 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
3053 }
3054 }
3055
3056 mReleasePreviousBuffer = false;
3057 mCallbackHandleAcquireTimeOrFence = -1;
3058
3059 return willPresent;
3060}
3061
3062Rect Layer::getBufferSize(const State& /*s*/) const {
3063 // for buffer state layers we use the display frame size as the buffer size.
3064
3065 if (mBufferInfo.mBuffer == nullptr) {
3066 return Rect::INVALID_RECT;
3067 }
3068
3069 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3070 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3071
3072 // Undo any transformations on the buffer and return the result.
3073 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
3074 std::swap(bufWidth, bufHeight);
3075 }
3076
3077 if (getTransformToDisplayInverse()) {
3078 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3079 if (invTransform & ui::Transform::ROT_90) {
3080 std::swap(bufWidth, bufHeight);
3081 }
3082 }
3083
3084 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
3085}
3086
3087FloatRect Layer::computeSourceBounds(const FloatRect& parentBounds) const {
3088 if (mBufferInfo.mBuffer == nullptr) {
3089 return parentBounds;
3090 }
3091
3092 return getBufferSize(getDrawingState()).toFloatRect();
3093}
3094
3095bool Layer::fenceHasSignaled() const {
3096 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
3097 return true;
3098 }
3099
3100 const bool fenceSignaled =
3101 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
3102 if (!fenceSignaled) {
3103 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
3104 TimeStats::LatchSkipReason::LateAcquire);
3105 }
3106
3107 return fenceSignaled;
3108}
3109
Vishnu Naire14c6b32022-08-06 04:20:15 +00003110bool Layer::onPreComposition(nsecs_t refreshStartTime) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003111 for (const auto& handle : mDrawingState.callbackHandles) {
3112 handle->refreshStartTime = refreshStartTime;
3113 }
3114 return hasReadyFrame();
3115}
3116
3117void Layer::setAutoRefresh(bool autoRefresh) {
3118 mDrawingState.autoRefresh = autoRefresh;
3119}
3120
3121bool Layer::latchSidebandStream(bool& recomputeVisibleRegions) {
3122 // We need to update the sideband stream if the layer has both a buffer and a sideband stream.
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003123 auto* snapshot = editLayerSnapshot();
3124 snapshot->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003125
3126 if (mSidebandStreamChanged.exchange(false)) {
3127 const State& s(getDrawingState());
3128 // mSidebandStreamChanged was true
3129 mSidebandStream = s.sidebandStream;
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003130 snapshot->sidebandStream = mSidebandStream;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003131 if (mSidebandStream != nullptr) {
3132 setTransactionFlags(eTransactionNeeded);
3133 mFlinger->setTransactionFlags(eTraversalNeeded);
3134 }
3135 recomputeVisibleRegions = true;
3136
3137 return true;
3138 }
3139 return false;
3140}
3141
3142bool Layer::hasFrameUpdate() const {
3143 const State& c(getDrawingState());
3144 return (mDrawingStateModified || mDrawingState.modified) &&
3145 (c.buffer != nullptr || c.bgColorLayer != nullptr);
3146}
3147
3148void Layer::updateTexImage(nsecs_t latchTime) {
3149 const State& s(getDrawingState());
3150
3151 if (!s.buffer) {
3152 if (s.bgColorLayer) {
3153 for (auto& handle : mDrawingState.callbackHandles) {
3154 handle->latchTime = latchTime;
3155 }
3156 }
3157 return;
3158 }
3159
3160 for (auto& handle : mDrawingState.callbackHandles) {
3161 if (handle->frameNumber == mDrawingState.frameNumber) {
3162 handle->latchTime = latchTime;
3163 }
3164 }
3165
3166 const int32_t layerId = getSequence();
3167 const uint64_t bufferId = mDrawingState.buffer->getId();
3168 const uint64_t frameNumber = mDrawingState.frameNumber;
3169 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3170 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
3171 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
3172
3173 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
3174 FrameTracer::FrameEvent::ACQUIRE_FENCE);
3175 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
3176 FrameTracer::FrameEvent::LATCH);
3177
3178 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
3179 if (bufferSurfaceFrame != nullptr &&
3180 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
3181 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
3182 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
3183 // are processing the next state.
3184 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
3185 mDrawingState.acquireFenceTime->getSignalTime(),
3186 latchTime);
3187 mDrawingState.bufferSurfaceFrameTX.reset();
3188 }
3189
3190 std::deque<sp<CallbackHandle>> remainingHandles;
3191 mFlinger->getTransactionCallbackInvoker()
3192 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
3193 mDrawingState.callbackHandles = remainingHandles;
3194
3195 mDrawingStateModified = false;
3196}
3197
3198void Layer::gatherBufferInfo() {
3199 if (!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
3200 decrementPendingBufferCount();
3201 }
3202
3203 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
3204 mBufferInfo.mBuffer = mDrawingState.buffer;
3205 mBufferInfo.mFence = mDrawingState.acquireFence;
3206 mBufferInfo.mFrameNumber = mDrawingState.frameNumber;
3207 mBufferInfo.mPixelFormat =
3208 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat();
3209 mBufferInfo.mFrameLatencyNeeded = true;
3210 mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime;
3211 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3212 mBufferInfo.mFence = mDrawingState.acquireFence;
3213 mBufferInfo.mTransform = mDrawingState.bufferTransform;
3214 auto lastDataspace = mBufferInfo.mDataspace;
3215 mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace);
3216 if (lastDataspace != mBufferInfo.mDataspace) {
3217 mFlinger->mSomeDataspaceChanged = true;
3218 }
3219 mBufferInfo.mCrop = computeBufferCrop(mDrawingState);
3220 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
3221 mBufferInfo.mSurfaceDamage = mDrawingState.surfaceDamageRegion;
3222 mBufferInfo.mHdrMetadata = mDrawingState.hdrMetadata;
3223 mBufferInfo.mApi = mDrawingState.api;
3224 mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse;
3225 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(mDrawingState.clientCacheId);
3226}
3227
3228Rect Layer::computeBufferCrop(const State& s) {
3229 if (s.buffer && !s.bufferCrop.isEmpty()) {
3230 Rect bufferCrop;
3231 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
3232 return bufferCrop;
3233 } else if (s.buffer) {
3234 return s.buffer->getBounds();
3235 } else {
3236 return s.bufferCrop;
3237 }
3238}
3239
3240sp<Layer> Layer::createClone() {
3241 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
3242 args.textureName = mTextureName;
Patrick Williams83f36b22022-09-14 17:57:35 +00003243 sp<Layer> layer = mFlinger->getFactory().createBufferStateLayer(args);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003244 layer->mHwcSlotGenerator = mHwcSlotGenerator;
3245 layer->setInitialValuesForClone(sp<Layer>::fromExisting(this));
3246 return layer;
3247}
3248
3249bool Layer::bufferNeedsFiltering() const {
3250 const State& s(getDrawingState());
3251 if (!s.buffer) {
3252 return false;
3253 }
3254
3255 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
3256 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
3257
3258 // Undo any transformations on the buffer and return the result.
3259 if (s.bufferTransform & ui::Transform::ROT_90) {
3260 std::swap(bufferWidth, bufferHeight);
3261 }
3262
3263 if (s.transformToDisplayInverse) {
3264 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3265 if (invTransform & ui::Transform::ROT_90) {
3266 std::swap(bufferWidth, bufferHeight);
3267 }
3268 }
3269
3270 const Rect layerSize{getBounds()};
3271 int32_t layerWidth = layerSize.getWidth();
3272 int32_t layerHeight = layerSize.getHeight();
3273
3274 // Align the layer orientation with the buffer before comparism
3275 if (mTransformHint & ui::Transform::ROT_90) {
3276 std::swap(layerWidth, layerHeight);
3277 }
3278
3279 return layerWidth != bufferWidth || layerHeight != bufferHeight;
3280}
3281
3282void Layer::decrementPendingBufferCount() {
3283 int32_t pendingBuffers = --mPendingBufferTransactions;
3284 tracePendingBufferCount(pendingBuffers);
3285}
3286
3287void Layer::tracePendingBufferCount(int32_t pendingBuffers) {
3288 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
3289}
3290
3291/*
3292 * We don't want to send the layer's transform to input, but rather the
3293 * parent's transform. This is because Layer's transform is
3294 * information about how the buffer is placed on screen. The parent's
3295 * transform makes more sense to send since it's information about how the
3296 * layer is placed on screen. This transform is used by input to determine
3297 * how to go from screen space back to window space.
3298 */
3299ui::Transform Layer::getInputTransform() const {
3300 if (!hasBufferOrSidebandStream()) {
3301 return getTransform();
3302 }
3303 sp<Layer> parent = mDrawingParent.promote();
3304 if (parent == nullptr) {
3305 return ui::Transform();
3306 }
3307
3308 return parent->getTransform();
3309}
3310
3311/**
3312 * Similar to getInputTransform, we need to update the bounds to include the transform.
3313 * This is because bounds don't include the buffer transform, where the input assumes
3314 * that's already included.
3315 */
3316Rect Layer::getInputBounds() const {
3317 if (!hasBufferOrSidebandStream()) {
3318 return getCroppedBufferSize(getDrawingState());
3319 }
3320
3321 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
3322 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
3323 return bufferBounds;
3324 }
3325 return mDrawingState.transform.transform(bufferBounds);
3326}
3327
3328bool Layer::simpleBufferUpdate(const layer_state_t& s) const {
3329 const uint64_t requiredFlags = layer_state_t::eBufferChanged;
3330
3331 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
3332 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
3333 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
3334 layer_state_t::eLayerStackChanged | layer_state_t::eAutoRefreshChanged |
3335 layer_state_t::eReparent;
3336
3337 const uint64_t allowedFlags = layer_state_t::eHasListenerCallbacksChanged |
3338 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFrameRateChanged |
3339 layer_state_t::eSurfaceDamageRegionChanged | layer_state_t::eApiChanged |
3340 layer_state_t::eMetadataChanged | layer_state_t::eDropInputModeChanged |
3341 layer_state_t::eInputInfoChanged;
3342
3343 if ((s.what & requiredFlags) != requiredFlags) {
3344 ALOGV("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
3345 (s.what | requiredFlags) & ~s.what);
3346 return false;
3347 }
3348
3349 if (s.what & deniedFlags) {
3350 ALOGV("%s: false [has denied flags 0x%" PRIx64 "]", __func__, s.what & deniedFlags);
3351 return false;
3352 }
3353
3354 if (s.what & allowedFlags) {
3355 ALOGV("%s: [has allowed flags 0x%" PRIx64 "]", __func__, s.what & allowedFlags);
3356 }
3357
3358 if (s.what & layer_state_t::ePositionChanged) {
3359 if (mRequestedTransform.tx() != s.x || mRequestedTransform.ty() != s.y) {
3360 ALOGV("%s: false [ePositionChanged changed]", __func__);
3361 return false;
3362 }
3363 }
3364
3365 if (s.what & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003366 if (mDrawingState.color.a != s.color.a) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003367 ALOGV("%s: false [eAlphaChanged changed]", __func__);
3368 return false;
3369 }
3370 }
3371
3372 if (s.what & layer_state_t::eColorTransformChanged) {
3373 if (mDrawingState.colorTransform != s.colorTransform) {
3374 ALOGV("%s: false [eColorTransformChanged changed]", __func__);
3375 return false;
3376 }
3377 }
3378
3379 if (s.what & layer_state_t::eBackgroundColorChanged) {
3380 if (mDrawingState.bgColorLayer || s.bgColorAlpha != 0) {
3381 ALOGV("%s: false [eBackgroundColorChanged changed]", __func__);
3382 return false;
3383 }
3384 }
3385
3386 if (s.what & layer_state_t::eMatrixChanged) {
3387 if (mRequestedTransform.dsdx() != s.matrix.dsdx ||
3388 mRequestedTransform.dtdy() != s.matrix.dtdy ||
3389 mRequestedTransform.dtdx() != s.matrix.dtdx ||
3390 mRequestedTransform.dsdy() != s.matrix.dsdy) {
3391 ALOGV("%s: false [eMatrixChanged changed]", __func__);
3392 return false;
3393 }
3394 }
3395
3396 if (s.what & layer_state_t::eCornerRadiusChanged) {
3397 if (mDrawingState.cornerRadius != s.cornerRadius) {
3398 ALOGV("%s: false [eCornerRadiusChanged changed]", __func__);
3399 return false;
3400 }
3401 }
3402
3403 if (s.what & layer_state_t::eBackgroundBlurRadiusChanged) {
3404 if (mDrawingState.backgroundBlurRadius != static_cast<int>(s.backgroundBlurRadius)) {
3405 ALOGV("%s: false [eBackgroundBlurRadiusChanged changed]", __func__);
3406 return false;
3407 }
3408 }
3409
Vishnu Nairbbceb462022-10-10 04:52:13 +00003410 if (s.what & layer_state_t::eBufferTransformChanged) {
3411 if (mDrawingState.bufferTransform != s.bufferTransform) {
3412 ALOGV("%s: false [eBufferTransformChanged changed]", __func__);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003413 return false;
3414 }
3415 }
3416
3417 if (s.what & layer_state_t::eTransformToDisplayInverseChanged) {
3418 if (mDrawingState.transformToDisplayInverse != s.transformToDisplayInverse) {
3419 ALOGV("%s: false [eTransformToDisplayInverseChanged changed]", __func__);
3420 return false;
3421 }
3422 }
3423
3424 if (s.what & layer_state_t::eCropChanged) {
3425 if (mDrawingState.crop != s.crop) {
3426 ALOGV("%s: false [eCropChanged changed]", __func__);
3427 return false;
3428 }
3429 }
3430
3431 if (s.what & layer_state_t::eDataspaceChanged) {
3432 if (mDrawingState.dataspace != s.dataspace) {
3433 ALOGV("%s: false [eDataspaceChanged changed]", __func__);
3434 return false;
3435 }
3436 }
3437
3438 if (s.what & layer_state_t::eHdrMetadataChanged) {
3439 if (mDrawingState.hdrMetadata != s.hdrMetadata) {
3440 ALOGV("%s: false [eHdrMetadataChanged changed]", __func__);
3441 return false;
3442 }
3443 }
3444
3445 if (s.what & layer_state_t::eSidebandStreamChanged) {
3446 if (mDrawingState.sidebandStream != s.sidebandStream) {
3447 ALOGV("%s: false [eSidebandStreamChanged changed]", __func__);
3448 return false;
3449 }
3450 }
3451
3452 if (s.what & layer_state_t::eColorSpaceAgnosticChanged) {
3453 if (mDrawingState.colorSpaceAgnostic != s.colorSpaceAgnostic) {
3454 ALOGV("%s: false [eColorSpaceAgnosticChanged changed]", __func__);
3455 return false;
3456 }
3457 }
3458
3459 if (s.what & layer_state_t::eShadowRadiusChanged) {
3460 if (mDrawingState.shadowRadius != s.shadowRadius) {
3461 ALOGV("%s: false [eShadowRadiusChanged changed]", __func__);
3462 return false;
3463 }
3464 }
3465
3466 if (s.what & layer_state_t::eFixedTransformHintChanged) {
3467 if (mDrawingState.fixedTransformHint != s.fixedTransformHint) {
3468 ALOGV("%s: false [eFixedTransformHintChanged changed]", __func__);
3469 return false;
3470 }
3471 }
3472
3473 if (s.what & layer_state_t::eTrustedOverlayChanged) {
3474 if (mDrawingState.isTrustedOverlay != s.isTrustedOverlay) {
3475 ALOGV("%s: false [eTrustedOverlayChanged changed]", __func__);
3476 return false;
3477 }
3478 }
3479
3480 if (s.what & layer_state_t::eStretchChanged) {
3481 StretchEffect temp = s.stretchEffect;
3482 temp.sanitize();
3483 if (mDrawingState.stretchEffect != temp) {
3484 ALOGV("%s: false [eStretchChanged changed]", __func__);
3485 return false;
3486 }
3487 }
3488
3489 if (s.what & layer_state_t::eBufferCropChanged) {
3490 if (mDrawingState.bufferCrop != s.bufferCrop) {
3491 ALOGV("%s: false [eBufferCropChanged changed]", __func__);
3492 return false;
3493 }
3494 }
3495
3496 if (s.what & layer_state_t::eDestinationFrameChanged) {
3497 if (mDrawingState.destinationFrame != s.destinationFrame) {
3498 ALOGV("%s: false [eDestinationFrameChanged changed]", __func__);
3499 return false;
3500 }
3501 }
3502
3503 if (s.what & layer_state_t::eDimmingEnabledChanged) {
3504 if (mDrawingState.dimmingEnabled != s.dimmingEnabled) {
3505 ALOGV("%s: false [eDimmingEnabledChanged changed]", __func__);
3506 return false;
3507 }
3508 }
3509
3510 ALOGV("%s: true", __func__);
3511 return true;
3512}
3513
3514bool Layer::isHdrY410() const {
3515 // pixel format is HDR Y410 masquerading as RGBA_1010102
3516 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
3517 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
3518 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
3519}
3520
Vishnu Naire14c6b32022-08-06 04:20:15 +00003521sp<LayerFE> Layer::getCompositionEngineLayerFE() const {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003522 // There's no need to get a CE Layer if the layer isn't going to draw anything.
Vishnu Naire14c6b32022-08-06 04:20:15 +00003523 return hasSomethingToDraw() ? mLayerFE : nullptr;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003524}
3525
Vishnu Naire14c6b32022-08-06 04:20:15 +00003526const LayerSnapshot* Layer::getLayerSnapshot() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003527 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003528}
3529
Vishnu Naire14c6b32022-08-06 04:20:15 +00003530LayerSnapshot* Layer::editLayerSnapshot() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003531 return mSnapshot.get();
3532}
Vishnu Naire14c6b32022-08-06 04:20:15 +00003533
Patrick Williamsbb25f802022-08-30 23:02:34 +00003534const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003535 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003536}
3537
Vishnu Naire14c6b32022-08-06 04:20:15 +00003538void Layer::moveSnapshotToLayerFE() {
3539 mLayerFE->mSnapshot = std::move(mSnapshot);
3540}
3541
3542void Layer::moveSnapshotToLayer() {
3543 mSnapshot = std::move(mLayerFE->mSnapshot);
3544}
3545
Patrick Williamsbb25f802022-08-30 23:02:34 +00003546void Layer::useSurfaceDamage() {
3547 if (mFlinger->mForceFullDamage) {
3548 surfaceDamageRegion = Region::INVALID_REGION;
3549 } else {
3550 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
3551 }
3552}
3553
3554void Layer::useEmptyDamage() {
3555 surfaceDamageRegion.clear();
3556}
3557
3558bool Layer::isOpaque(const Layer::State& s) const {
3559 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
3560 // layer's opaque flag.
3561 if (!hasSomethingToDraw()) {
3562 return false;
3563 }
3564
3565 // if the layer has the opaque flag, then we're always opaque
3566 if ((s.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque) {
3567 return true;
3568 }
3569
3570 // If the buffer has no alpha channel, then we are opaque
3571 if (hasBufferOrSidebandStream() && isOpaqueFormat(getPixelFormat())) {
3572 return true;
3573 }
3574
3575 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
3576 return fillsColor() && getAlpha() == 1.0_hf;
3577}
3578
3579bool Layer::canReceiveInput() const {
3580 return !isHiddenByPolicy() && (mBufferInfo.mBuffer == nullptr || getAlpha() > 0.0f);
3581}
3582
3583bool Layer::isVisible() const {
3584 if (!hasSomethingToDraw()) {
3585 return false;
3586 }
3587
3588 if (isHiddenByPolicy()) {
3589 return false;
3590 }
3591
3592 return getAlpha() > 0.0f || hasBlur();
3593}
3594
3595void Layer::onPostComposition(const DisplayDevice* display,
3596 const std::shared_ptr<FenceTime>& glDoneFence,
3597 const std::shared_ptr<FenceTime>& presentFence,
3598 const CompositorTiming& compositorTiming) {
3599 // mFrameLatencyNeeded is true when a new frame was latched for the
3600 // composition.
3601 if (!mBufferInfo.mFrameLatencyNeeded) return;
3602
3603 for (const auto& handle : mDrawingState.callbackHandles) {
3604 handle->gpuCompositionDoneFence = glDoneFence;
3605 handle->compositorTiming = compositorTiming;
3606 }
3607
3608 // Update mFrameTracker.
3609 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
3610 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
3611
3612 const int32_t layerId = getSequence();
3613 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
3614
3615 const auto outputLayer = findOutputLayerForDisplay(display);
3616 if (outputLayer && outputLayer->requiresClientComposition()) {
3617 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
3618 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3619 clientCompositionTimestamp,
3620 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
3621 // Update the SurfaceFrames in the drawing state
3622 if (mDrawingState.bufferSurfaceFrameTX) {
3623 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
3624 }
3625 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
3626 surfaceFrame->setGpuComposition();
3627 }
3628 }
3629
3630 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
3631 if (frameReadyFence->isValid()) {
3632 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
3633 } else {
3634 // There was no fence for this frame, so assume that it was ready
3635 // to be presented at the desired present time.
3636 mFrameTracker.setFrameReadyTime(desiredPresentTime);
3637 }
3638
3639 if (display) {
Dominik Laskowskif8734e02022-08-26 09:06:59 -07003640 const Fps refreshRate = display->refreshRateConfigs().getActiveModePtr()->getFps();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003641 const std::optional<Fps> renderRate =
3642 mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
3643
3644 const auto vote = frameRateToSetFrameRateVotePayload(mDrawingState.frameRate);
3645 const auto gameMode = getGameMode();
3646
3647 if (presentFence->isValid()) {
3648 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
3649 refreshRate, renderRate, vote, gameMode);
3650 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3651 presentFence,
3652 FrameTracer::FrameEvent::PRESENT_FENCE);
3653 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
3654 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
3655 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Dominik Laskowskidc2bb802022-09-28 16:02:59 -04003656 // The HWC doesn't support present fences, so use the present timestamp instead.
3657 const nsecs_t presentTimestamp =
3658 mFlinger->getHwComposer().getPresentTimestamp(*displayId);
3659
3660 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
3661 const nsecs_t vsyncPeriod = display->getVsyncPeriodFromHWC();
3662 const nsecs_t actualPresentTime = now - ((now - presentTimestamp) % vsyncPeriod);
3663
Patrick Williamsbb25f802022-08-30 23:02:34 +00003664 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
3665 refreshRate, renderRate, vote, gameMode);
3666 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(),
3667 mCurrentFrameNumber, actualPresentTime,
3668 FrameTracer::FrameEvent::PRESENT_FENCE);
3669 mFrameTracker.setActualPresentTime(actualPresentTime);
3670 }
3671 }
3672
3673 mFrameTracker.advanceFrame();
3674 mBufferInfo.mFrameLatencyNeeded = false;
3675}
3676
3677bool Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
3678 ATRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(),
3679 getDrawingState().frameNumber);
3680
3681 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
3682
3683 if (refreshRequired) {
3684 return refreshRequired;
3685 }
3686
3687 // If the head buffer's acquire fence hasn't signaled yet, return and
3688 // try again later
3689 if (!fenceHasSignaled()) {
3690 ATRACE_NAME("!fenceHasSignaled()");
3691 mFlinger->onLayerUpdate();
3692 return false;
3693 }
3694
3695 updateTexImage(latchTime);
3696 if (mDrawingState.buffer == nullptr) {
3697 return false;
3698 }
3699
3700 // Capture the old state of the layer for comparisons later
3701 BufferInfo oldBufferInfo = mBufferInfo;
3702 const bool oldOpacity = isOpaque(mDrawingState);
3703 mPreviousFrameNumber = mCurrentFrameNumber;
3704 mCurrentFrameNumber = mDrawingState.frameNumber;
3705 gatherBufferInfo();
3706
3707 if (oldBufferInfo.mBuffer == nullptr) {
3708 // the first time we receive a buffer, we need to trigger a
3709 // geometry invalidation.
3710 recomputeVisibleRegions = true;
3711 }
3712
3713 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
3714 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
3715 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
3716 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
3717 recomputeVisibleRegions = true;
3718 }
3719
3720 if (oldBufferInfo.mBuffer != nullptr) {
3721 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3722 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3723 if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
3724 bufHeight != oldBufferInfo.mBuffer->getHeight()) {
3725 recomputeVisibleRegions = true;
3726 }
3727 }
3728
3729 if (oldOpacity != isOpaque(mDrawingState)) {
3730 recomputeVisibleRegions = true;
3731 }
3732
3733 return true;
3734}
3735
3736bool Layer::hasReadyFrame() const {
3737 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
3738}
3739
3740bool Layer::isProtected() const {
3741 return (mBufferInfo.mBuffer != nullptr) &&
3742 (mBufferInfo.mBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
3743}
3744
3745// As documented in libhardware header, formats in the range
3746// 0x100 - 0x1FF are specific to the HAL implementation, and
3747// are known to have no alpha channel
3748// TODO: move definition for device-specific range into
3749// hardware.h, instead of using hard-coded values here.
3750#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
3751
3752bool Layer::isOpaqueFormat(PixelFormat format) {
3753 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
3754 return true;
3755 }
3756 switch (format) {
3757 case PIXEL_FORMAT_RGBA_8888:
3758 case PIXEL_FORMAT_BGRA_8888:
3759 case PIXEL_FORMAT_RGBA_FP16:
3760 case PIXEL_FORMAT_RGBA_1010102:
3761 case PIXEL_FORMAT_R_8:
3762 return false;
3763 }
3764 // in all other case, we have no blending (also for unknown formats)
3765 return true;
3766}
3767
3768bool Layer::needsFiltering(const DisplayDevice* display) const {
3769 if (!hasBufferOrSidebandStream()) {
3770 return false;
3771 }
3772 const auto outputLayer = findOutputLayerForDisplay(display);
3773 if (outputLayer == nullptr) {
3774 return false;
3775 }
3776
3777 // We need filtering if the sourceCrop rectangle size does not match the
3778 // displayframe rectangle size (not a 1:1 render)
3779 const auto& compositionState = outputLayer->getState();
3780 const auto displayFrame = compositionState.displayFrame;
3781 const auto sourceCrop = compositionState.sourceCrop;
3782 return sourceCrop.getHeight() != displayFrame.getHeight() ||
3783 sourceCrop.getWidth() != displayFrame.getWidth();
3784}
3785
3786bool Layer::needsFilteringForScreenshots(const DisplayDevice* display,
3787 const ui::Transform& inverseParentTransform) const {
3788 if (!hasBufferOrSidebandStream()) {
3789 return false;
3790 }
3791 const auto outputLayer = findOutputLayerForDisplay(display);
3792 if (outputLayer == nullptr) {
3793 return false;
3794 }
3795
3796 // We need filtering if the sourceCrop rectangle size does not match the
3797 // viewport rectangle size (not a 1:1 render)
3798 const auto& compositionState = outputLayer->getState();
3799 const ui::Transform& displayTransform = display->getTransform();
3800 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
3801 // Undo the transformation of the displayFrame so that we're back into
3802 // layer-stack space.
3803 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
3804 const FloatRect sourceCrop = compositionState.sourceCrop;
3805
3806 int32_t frameHeight = frame.getHeight();
3807 int32_t frameWidth = frame.getWidth();
3808 // If the display transform had a rotational component then undo the
3809 // rotation so that the orientation matches the source crop.
3810 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
3811 std::swap(frameHeight, frameWidth);
3812 }
3813 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
3814}
3815
3816void Layer::latchAndReleaseBuffer() {
3817 if (hasReadyFrame()) {
3818 bool ignored = false;
3819 latchBuffer(ignored, systemTime());
3820 }
3821 releasePendingBuffer(systemTime());
3822}
3823
3824PixelFormat Layer::getPixelFormat() const {
3825 return mBufferInfo.mPixelFormat;
3826}
3827
3828bool Layer::getTransformToDisplayInverse() const {
3829 return mBufferInfo.mTransformToDisplayInverse;
3830}
3831
3832Rect Layer::getBufferCrop() const {
3833 // this is the crop rectangle that applies to the buffer
3834 // itself (as opposed to the window)
3835 if (!mBufferInfo.mCrop.isEmpty()) {
3836 // if the buffer crop is defined, we use that
3837 return mBufferInfo.mCrop;
3838 } else if (mBufferInfo.mBuffer != nullptr) {
3839 // otherwise we use the whole buffer
3840 return mBufferInfo.mBuffer->getBounds();
3841 } else {
3842 // if we don't have a buffer yet, we use an empty/invalid crop
3843 return Rect();
3844 }
3845}
3846
3847uint32_t Layer::getBufferTransform() const {
3848 return mBufferInfo.mTransform;
3849}
3850
3851ui::Dataspace Layer::getDataSpace() const {
3852 return mDrawingState.dataspaceRequested ? getRequestedDataSpace() : ui::Dataspace::UNKNOWN;
3853}
3854
3855ui::Dataspace Layer::getRequestedDataSpace() const {
3856 return hasBufferOrSidebandStream() ? mBufferInfo.mDataspace : mDrawingState.dataspace;
3857}
3858
3859ui::Dataspace Layer::translateDataspace(ui::Dataspace dataspace) {
3860 ui::Dataspace updatedDataspace = dataspace;
3861 // translate legacy dataspaces to modern dataspaces
3862 switch (dataspace) {
3863 case ui::Dataspace::SRGB:
3864 updatedDataspace = ui::Dataspace::V0_SRGB;
3865 break;
3866 case ui::Dataspace::SRGB_LINEAR:
3867 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
3868 break;
3869 case ui::Dataspace::JFIF:
3870 updatedDataspace = ui::Dataspace::V0_JFIF;
3871 break;
3872 case ui::Dataspace::BT601_625:
3873 updatedDataspace = ui::Dataspace::V0_BT601_625;
3874 break;
3875 case ui::Dataspace::BT601_525:
3876 updatedDataspace = ui::Dataspace::V0_BT601_525;
3877 break;
3878 case ui::Dataspace::BT709:
3879 updatedDataspace = ui::Dataspace::V0_BT709;
3880 break;
3881 default:
3882 break;
3883 }
3884
3885 return updatedDataspace;
3886}
3887
3888sp<GraphicBuffer> Layer::getBuffer() const {
3889 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
3890}
3891
Patrick Williamsbb25f802022-08-30 23:02:34 +00003892void Layer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
3893 mTransformHint = getFixedTransformHint();
3894 if (mTransformHint == ui::Transform::ROT_INVALID) {
3895 mTransformHint = displayTransformHint;
3896 }
3897}
3898
3899const std::shared_ptr<renderengine::ExternalTexture>& Layer::getExternalTexture() const {
3900 return mBufferInfo.mBuffer;
3901}
3902
3903bool Layer::setColor(const half3& color) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003904 if (mDrawingState.color.rgb == color) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003905 return false;
3906 }
3907
3908 mDrawingState.sequence++;
Vishnu Nairbbceb462022-10-10 04:52:13 +00003909 mDrawingState.color.rgb = color;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003910 mDrawingState.modified = true;
3911 setTransactionFlags(eTransactionNeeded);
3912 return true;
3913}
3914
3915bool Layer::fillsColor() const {
3916 return !hasBufferOrSidebandStream() && mDrawingState.color.r >= 0.0_hf &&
3917 mDrawingState.color.g >= 0.0_hf && mDrawingState.color.b >= 0.0_hf;
3918}
3919
3920bool Layer::hasBlur() const {
3921 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
3922}
3923
Vishnu Nairba354102022-08-01 00:14:18 +00003924void Layer::updateSnapshot(bool updateGeometry) {
3925 if (!getCompositionEngineLayerFE()) {
3926 return;
3927 }
3928
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003929 auto* snapshot = editLayerSnapshot();
Vishnu Nairba354102022-08-01 00:14:18 +00003930 if (updateGeometry) {
3931 prepareBasicGeometryCompositionState();
3932 prepareGeometryCompositionState();
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003933 snapshot->roundedCorner = getRoundedCornerState();
3934 snapshot->stretchEffect = getStretchEffect();
3935 snapshot->transformedBounds = mScreenBounds;
3936 if (mEffectiveShadowRadius > 0.f) {
3937 snapshot->shadowSettings = mFlinger->mDrawingState.globalShadowSettings;
3938
3939 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
3940 // it if transparent regions are present. This may not be necessary since shadows are
3941 // typically cast by layers without transparent regions.
3942 snapshot->shadowSettings.boundaries = mBounds;
3943
3944 const float casterAlpha = snapshot->alpha;
3945 const bool casterIsOpaque =
3946 ((mBufferInfo.mBuffer != nullptr) && isOpaque(mDrawingState));
3947
3948 // If the casting layer is translucent, we need to fill in the shadow underneath the
3949 // layer. Otherwise the generated shadow will only be shown around the casting layer.
3950 snapshot->shadowSettings.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
3951 snapshot->shadowSettings.ambientColor *= casterAlpha;
3952 snapshot->shadowSettings.spotColor *= casterAlpha;
3953 }
3954 snapshot->shadowSettings.length = mEffectiveShadowRadius;
Vishnu Nairba354102022-08-01 00:14:18 +00003955 }
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003956 snapshot->contentOpaque = isOpaque(mDrawingState);
3957 snapshot->isHdrY410 = isHdrY410();
3958 snapshot->bufferNeedsFiltering = bufferNeedsFiltering();
3959 sp<Layer> p = mDrawingParent.promote();
3960 if (p != nullptr) {
3961 snapshot->transform = p->getTransform();
3962 } else {
3963 snapshot->transform.reset();
3964 }
3965 snapshot->bufferSize = getBufferSize(mDrawingState);
3966 snapshot->externalTexture = mBufferInfo.mBuffer;
Vishnu Naire14c6b32022-08-06 04:20:15 +00003967 snapshot->hasReadyFrame = hasReadyFrame();
Vishnu Nairba354102022-08-01 00:14:18 +00003968 preparePerFrameCompositionState();
3969}
3970
Vishnu Naire14c6b32022-08-06 04:20:15 +00003971void Layer::updateChildrenSnapshots(bool updateGeometry) {
3972 for (const sp<Layer>& child : mDrawingChildren) {
3973 child->updateSnapshot(updateGeometry);
3974 child->updateChildrenSnapshots(updateGeometry);
3975 }
3976}
3977
Vishnu Nair0a4fb002022-08-08 02:40:42 +00003978void Layer::updateMetadataSnapshot(const LayerMetadata& parentMetadata) {
3979 mSnapshot->layerMetadata = parentMetadata;
3980 mSnapshot->layerMetadata.merge(mDrawingState.metadata);
3981 for (const sp<Layer>& child : mDrawingChildren) {
3982 child->updateMetadataSnapshot(mSnapshot->layerMetadata);
3983 }
3984}
3985
3986void Layer::updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMetadata,
3987 std::unordered_set<Layer*>& visited) {
3988 if (visited.find(this) != visited.end()) {
3989 ALOGW("Cycle containing layer %s detected in z-order relatives", getDebugName());
3990 return;
3991 }
3992 visited.insert(this);
3993
3994 mSnapshot->relativeLayerMetadata = relativeLayerMetadata;
3995
3996 if (mDrawingState.zOrderRelatives.empty()) {
3997 return;
3998 }
3999 LayerMetadata childRelativeLayerMetadata = mSnapshot->relativeLayerMetadata;
4000 childRelativeLayerMetadata.merge(mSnapshot->layerMetadata);
4001 for (wp<Layer> weakRelative : mDrawingState.zOrderRelatives) {
4002 sp<Layer> relative = weakRelative.promote();
4003 if (!relative) {
4004 continue;
4005 }
4006 relative->updateRelativeMetadataSnapshot(childRelativeLayerMetadata, visited);
4007 }
4008}
4009
Mathias Agopian13127d82013-03-05 17:47:11 -08004010// ---------------------------------------------------------------------------
4011
Marin Shalamanov46084422020-10-13 12:33:42 +02004012std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07004013 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
4014 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02004015}
4016
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07004017} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07004018
4019#if defined(__gl_h_)
4020#error "don't include gl/gl.h in this file"
4021#endif
4022
4023#if defined(__gl2_h_)
4024#error "don't include gl2/gl2.h in this file"
4025#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08004026
4027// TODO(b/129481165): remove the #pragma below and fix conversion issues
4028#pragma clang diagnostic pop // ignored "-Wconversion"