blob: 56abc516fdee9f5d8a1993057555ef2733ba284b [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>
Vishnu Nair71fcf912022-10-18 09:14:20 -070065#include <optional>
Alec Mourie60041e2019-06-14 18:59:51 -070066#include <sstream>
67
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070068#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080069#include "DisplayHardware/HWComposer.h"
Ady Abraham22c7b5c2020-09-22 19:33:40 -070070#include "FrameTimeline.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070071#include "FrameTracer/FrameTracer.h"
Vishnu Naircb8be502022-10-12 19:03:23 +000072#include "FrontEnd/LayerCreationArgs.h"
Vishnu Nair07e2a482022-10-18 19:18:16 +000073#include "FrontEnd/LayerHandle.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080074#include "LayerProtoHelper.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080076#include "TimeStats/TimeStats.h"
Robert Carr3e2a2992021-06-11 13:42:55 -070077#include "TunnelModeEnabledReporter.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070078
David Sodman41fdfc92017-11-06 16:09:56 -080079#define DEBUG_RESIZE 0
Patrick Williamsbb25f802022-08-30 23:02:34 +000080#define EARLY_RELEASE_ENABLED false
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010083namespace {
84constexpr int kDumpTableRowLength = 159;
Patrick Williamsbb25f802022-08-30 23:02:34 +000085
Prabir Pradhanda0f62c2022-07-22 19:53:04 +000086const ui::Transform kIdentityTransform;
Patrick Williamsbb25f802022-08-30 23:02:34 +000087
Patrick Williamsbb25f802022-08-30 23:02:34 +000088bool assignTransform(ui::Transform* dst, ui::Transform& from) {
89 if (*dst == from) {
90 return false;
91 }
92 *dst = from;
93 return true;
94}
95
96TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
97 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
98 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
99 const auto frameRateCompatibility = [frameRate] {
100 switch (frameRate.type) {
101 case Layer::FrameRateCompatibility::Default:
102 return FrameRateCompatibility::Default;
103 case Layer::FrameRateCompatibility::ExactOrMultiple:
104 return FrameRateCompatibility::ExactOrMultiple;
105 default:
106 return FrameRateCompatibility::Undefined;
107 }
108 }();
109
110 const auto seamlessness = [frameRate] {
111 switch (frameRate.seamlessness) {
112 case scheduler::Seamlessness::OnlySeamless:
113 return Seamlessness::ShouldBeSeamless;
114 case scheduler::Seamlessness::SeamedAndSeamless:
115 return Seamlessness::NotRequired;
116 default:
117 return Seamlessness::Undefined;
118 }
119 }();
120
121 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
122 .frameRateCompatibility = frameRateCompatibility,
123 .seamlessness = seamlessness};
124}
125
Marin Shalamanov1876e2e2020-12-04 13:23:59 +0100126} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700128using namespace ftl::flag_operators;
129
Yiwei Zhang5434a782018-12-05 18:06:32 -0800130using base::StringAppendF;
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800131using gui::GameMode;
132using gui::LayerMetadata;
chaviw3277faf2021-05-19 16:45:23 -0500133using gui::WindowInfo;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800134
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700135using PresentState = frametimeline::SurfaceFrame::PresentState;
136
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700137Layer::Layer(const LayerCreationArgs& args)
Vishnu Naircb8be502022-10-12 19:03:23 +0000138 : sequence(args.sequence),
Ady Abrahamd11bade2022-08-01 16:18:03 -0700139 mFlinger(sp<SurfaceFlinger>::fromExisting(args.flinger)),
Arthur Hung23e07502021-10-15 11:58:19 +0000140 mName(base::StringPrintf("%s#%d", args.name.c_str(), sequence)),
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700141 mClientRef(args.client),
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800142 mWindowType(static_cast<WindowInfo::Type>(
143 args.metadata.getInt32(gui::METADATA_WINDOW_TYPE, 0))),
Tianhao Yao67dd7122022-02-22 17:48:33 +0000144 mLayerCreationFlags(args.flags),
Patrick Williamsbb25f802022-08-30 23:02:34 +0000145 mBorderEnabled(false),
146 mTextureName(args.textureName),
Vishnu Naire14c6b32022-08-06 04:20:15 +0000147 mHwcSlotGenerator(sp<HwcSlotGenerator>::make()),
148 mLayerFE(args.flinger->getFactory().createLayerFE(mName)) {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000149 ALOGV("Creating Layer %s", getDebugName());
150
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700151 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700152 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
153 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
154 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -0700155 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
156 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Robert Carr6a160312021-05-17 12:08:20 -0700157 mDrawingState.flags = layerFlags;
Robert Carr6a160312021-05-17 12:08:20 -0700158 mDrawingState.crop.makeInvalid();
Robert Carr6a160312021-05-17 12:08:20 -0700159 mDrawingState.z = 0;
160 mDrawingState.color.a = 1.0f;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700161 mDrawingState.layerStack = ui::DEFAULT_LAYER_STACK;
Robert Carr6a160312021-05-17 12:08:20 -0700162 mDrawingState.sequence = 0;
Robert Carr6a160312021-05-17 12:08:20 -0700163 mDrawingState.transform.set(0, 0);
164 mDrawingState.frameNumber = 0;
165 mDrawingState.bufferTransform = 0;
166 mDrawingState.transformToDisplayInverse = false;
167 mDrawingState.crop.makeInvalid();
168 mDrawingState.acquireFence = sp<Fence>::make(-1);
169 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
170 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000171 mDrawingState.dataspaceRequested = false;
Robert Carr6a160312021-05-17 12:08:20 -0700172 mDrawingState.hdrMetadata.validTypes = 0;
173 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
174 mDrawingState.cornerRadius = 0.0f;
175 mDrawingState.backgroundBlurRadius = 0;
176 mDrawingState.api = -1;
177 mDrawingState.hasColorTransform = false;
178 mDrawingState.colorSpaceAgnostic = false;
179 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
180 mDrawingState.metadata = args.metadata;
181 mDrawingState.shadowRadius = 0.f;
182 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
183 mDrawingState.frameTimelineInfo = {};
184 mDrawingState.postTime = -1;
185 mDrawingState.destinationFrame.makeInvalid();
Robin Leeedb375d2021-09-10 12:03:42 +0000186 mDrawingState.isTrustedOverlay = false;
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -0700187 mDrawingState.dropInputMode = gui::DropInputMode::NONE;
Sally Qi81d95e62022-03-21 19:41:33 -0700188 mDrawingState.dimmingEnabled = true;
Andy Labrada096227e2022-06-15 16:58:11 +0000189 mDrawingState.defaultFrameRateCompatibility = FrameRateCompatibility::Default;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700190
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700191 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
192 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700193 mDrawingState.color.r = -1.0_hf;
194 mDrawingState.color.g = -1.0_hf;
195 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700196 }
Dominik Laskowski4376bd82022-07-07 11:50:20 -0700197
Dominik Laskowski596a2562022-10-28 11:26:12 -0400198 mFrameTracker.setDisplayRefreshPeriod(args.flinger->mScheduler->getLeaderVsyncPeriod());
Robert Carr2e102c92018-10-23 12:11:15 -0700199
Vishnu Naircb8be502022-10-12 19:03:23 +0000200 mOwnerUid = args.ownerUid;
201 mOwnerPid = args.ownerPid;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000202
203 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
204 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
205 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
206 mDrawingState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000207
208 mSnapshot->sequence = sequence;
209 mSnapshot->name = getDebugName();
210 mSnapshot->textureName = mTextureName;
211 mSnapshot->premultipliedAlpha = mPremultipliedAlpha;
212 mSnapshot->transform = {};
Dominik Laskowski75848362019-11-11 17:57:20 -0800213}
214
215void Layer::onFirstRef() {
216 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700217}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700218
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700219Layer::~Layer() {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000220 // The original layer and the clone layer share the same texture and buffer. Therefore, only
221 // one of the layers, in this case the original layer, needs to handle the deletion. The
222 // original layer and the clone should be removed at the same time so there shouldn't be any
223 // issue with the clone layer trying to use the texture.
224 if (mBufferInfo.mBuffer != nullptr) {
225 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
226 mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
227 mBufferInfo.mFence,
228 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
229 mOwnerUid));
230 }
231 if (!isClone()) {
232 // The original layer and the clone layer share the same texture. Therefore, only one of
233 // the layers, in this case the original layer, needs to handle the deletion. The original
234 // layer and the clone should be removed at the same time so there shouldn't be any issue
235 // with the clone layer trying to use the deleted texture.
236 mFlinger->deleteTextureAsync(mTextureName);
237 }
238 const int32_t layerId = getSequence();
239 mFlinger->mTimeStats->onDestroy(layerId);
240 mFlinger->mFrameTracer->onDestroy(layerId);
241
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000242 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700243 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700244
245 if (mDrawingState.sidebandStream != nullptr) {
246 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
247 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700248 if (mHadClonedChild) {
249 mFlinger->mNumClones--;
250 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700251}
252
Mathias Agopian13127d82013-03-05 17:47:11 -0800253// ---------------------------------------------------------------------------
254// callbacks
255// ---------------------------------------------------------------------------
256
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700257void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700258 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700259 return;
260 }
Robert Carr2e102c92018-10-23 12:11:15 -0700261
Robert Carr6a160312021-05-17 12:08:20 -0700262 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700263 if (strongRelative == nullptr) {
264 setZOrderRelativeOf(nullptr);
265 return;
266 }
267
268 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700269 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700270 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800271 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700272 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700273}
274
275void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700276 if (!mRemovedFromDrawingState) {
277 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700278 mFlinger->mScheduler->deregisterLayer(this);
279 }
Rob Carr4bba3702018-10-08 21:53:30 +0000280
Ady Abrahamd11bade2022-08-01 16:18:03 -0700281 mFlinger->markLayerPendingRemovalLocked(sp<Layer>::fromExisting(this));
Chia-I Wuc6657022017-08-15 11:18:17 -0700282}
Chia-I Wu38512252017-05-17 14:36:16 -0700283
chaviw68d4dab2020-06-08 15:07:32 -0700284sp<Layer> Layer::getRootLayer() {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000285 sp<Layer> parent = getParent();
chaviw68d4dab2020-06-08 15:07:32 -0700286 if (parent == nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700287 return sp<Layer>::fromExisting(this);
chaviw68d4dab2020-06-08 15:07:32 -0700288 }
289 return parent->getRootLayer();
290}
291
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700292void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700293 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
294 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700295 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700296
297 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700298 layer->removeFromCurrentState();
299 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700300 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700301}
302
chaviw61626f22018-11-15 16:26:27 -0800303void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700304 if (mRemovedFromDrawingState) {
305 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700306 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700307 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700308 }
chaviw61626f22018-11-15 16:26:27 -0800309
310 for (const auto& child : mCurrentChildren) {
311 child->addToCurrentState();
312 }
313}
314
Mathias Agopian13127d82013-03-05 17:47:11 -0800315// ---------------------------------------------------------------------------
316// set-up
317// ---------------------------------------------------------------------------
318
chaviw13fdc492017-06-27 12:40:18 -0700319bool Layer::getPremultipledAlpha() const {
320 return mPremultipliedAlpha;
321}
322
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700323sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800324 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700325 if (mGetHandleCalled) {
326 ALOGE("Get handle called twice" );
327 return nullptr;
328 }
329 mGetHandleCalled = true;
Vishnu Nair07e2a482022-10-18 19:18:16 +0000330 return sp<LayerHandle>::make(mFlinger, sp<Layer>::fromExisting(this));
Mathias Agopian13127d82013-03-05 17:47:11 -0800331}
332
333// ---------------------------------------------------------------------------
334// h/w composer set-up
335// ---------------------------------------------------------------------------
336
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700337static Rect reduce(const Rect& win, const Region& exclude) {
338 if (CC_LIKELY(exclude.isEmpty())) {
339 return win;
340 }
341 if (exclude.isRect()) {
342 return win.reduce(exclude.getBounds());
343 }
344 return Region(win).subtract(exclude).getBounds();
345}
346
Dan Stoza80d61162017-12-20 15:57:52 -0800347static FloatRect reduce(const FloatRect& win, const Region& exclude) {
348 if (CC_LIKELY(exclude.isEmpty())) {
349 return win;
350 }
351 // Convert through Rect (by rounding) for lack of FloatRegion
352 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
353}
354
Vishnu Nair4351ad52019-02-11 14:13:02 -0800355Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800356 if (!reduceTransparentRegion) {
357 return Rect{mScreenBounds};
358 }
359
360 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800361 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800362 // Transform to screen space.
363 bounds = t.transform(bounds);
364 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700365}
366
Vishnu Nair4351ad52019-02-11 14:13:02 -0800367FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000368 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800369 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700370}
371
Vishnu Nairf0c28512019-02-08 12:40:28 -0800372FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
373 // Subtract the transparent region and snap to the bounds.
374 return reduce(mBounds, activeTransparentRegion);
375}
376
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700377void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
378 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800379 const State& s(getDrawingState());
380
381 // Calculate effective layer transform
382 mEffectiveTransform = parentTransform * getActiveTransform(s);
383
Garfield Tan2c1782c2022-02-16 15:25:05 -0800384 if (CC_UNLIKELY(!isTransformValid())) {
385 ALOGW("Stop computing bounds for %s because it has invalid transformation.",
386 getDebugName());
387 return;
388 }
389
Vishnu Nair4351ad52019-02-11 14:13:02 -0800390 // Transform parent bounds to layer space
391 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
392
Vishnu Nairc652ff82019-03-15 12:48:54 -0700393 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800394 mSourceBounds = computeSourceBounds(parentBounds);
395
396 // Calculate bounds by croping diplay frame with layer crop and parent bounds
397 FloatRect bounds = mSourceBounds;
398 const Rect layerCrop = getCrop(s);
399 if (!layerCrop.isEmpty()) {
400 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
401 }
402 bounds = bounds.intersect(parentBounds);
403
404 mBounds = bounds;
405 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700406
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700407 // Use the layer's own shadow radius if set. Otherwise get the radius from
408 // parent.
409 if (s.shadowRadius > 0.f) {
410 mEffectiveShadowRadius = s.shadowRadius;
411 } else {
412 mEffectiveShadowRadius = parentShadowRadius;
413 }
414
415 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
416 // don't pass it to its children.
417 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
418
Vishnu Nair4351ad52019-02-11 14:13:02 -0800419 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700420 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800421 }
Vishnu Nairba354102022-08-01 00:14:18 +0000422
423 if (mPotentialCursor) {
424 prepareCursorCompositionState();
425 }
Vishnu Nair4351ad52019-02-11 14:13:02 -0800426}
427
Vishnu Nair60356342018-11-13 13:00:45 -0800428Rect Layer::getCroppedBufferSize(const State& s) const {
429 Rect size = getBufferSize(s);
430 Rect crop = getCrop(s);
431 if (!crop.isEmpty() && size.isValid()) {
432 size.intersect(crop, &size);
433 } else if (!crop.isEmpty()) {
434 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700435 }
Vishnu Nair60356342018-11-13 13:00:45 -0800436 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800437}
438
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700439void Layer::setupRoundedCornersCropCoordinates(Rect win,
440 const FloatRect& roundedCornersCrop) const {
441 // Translate win by the rounded corners rect coordinates, to have all values in
442 // layer coordinate space.
443 win.left -= roundedCornersCrop.left;
444 win.right -= roundedCornersCrop.left;
445 win.top -= roundedCornersCrop.top;
446 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700447}
448
Lloyd Piquede196652020-01-22 17:29:58 -0800449void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800450 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800451 const auto alpha = static_cast<float>(getAlpha());
452 const bool opaque = isOpaque(drawingState);
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700453 const bool usesRoundedCorners = hasRoundedCorners();
Lloyd Piquec6687342019-03-07 21:34:57 -0800454
455 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
456 if (!opaque || alpha != 1.0f) {
457 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
458 : Hwc2::IComposerClient::BlendMode::COVERAGE;
459 }
460
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000461 auto* snapshot = editLayerSnapshot();
462 snapshot->outputFilter = getOutputFilter();
463 snapshot->isVisible = isVisible();
464 snapshot->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
465 snapshot->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800466
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000467 snapshot->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800468 contentDirty = false;
469
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000470 snapshot->geomLayerBounds = mBounds;
471 snapshot->geomLayerTransform = getTransform();
472 snapshot->geomInverseLayerTransform = snapshot->geomLayerTransform.inverse();
473 snapshot->transparentRegionHint = getActiveTransparentRegion(drawingState);
Vishnu Naire7613db2022-11-04 00:07:06 -0700474 snapshot->blurRegionTransform = getActiveTransform(drawingState).inverse();
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000475 snapshot->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
476 snapshot->alpha = alpha;
477 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
478 snapshot->blurRegions = drawingState.blurRegions;
479 snapshot->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800480}
481
Lloyd Piquede196652020-01-22 17:29:58 -0800482void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800483 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000484 auto* snapshot = editLayerSnapshot();
David Sodman15094112018-10-11 09:39:37 -0700485
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000486 snapshot->geomBufferSize = getBufferSize(drawingState);
487 snapshot->geomContentCrop = getBufferCrop();
488 snapshot->geomCrop = getCrop(drawingState);
489 snapshot->geomBufferTransform = getBufferTransform();
490 snapshot->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
491 snapshot->geomUsesSourceCrop = usesSourceCrop();
492 snapshot->isSecure = isSecure();
Lloyd Piquede196652020-01-22 17:29:58 -0800493
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000494 snapshot->metadata.clear();
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800495 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
496 for (const auto& [key, mandatory] : supportedMetadata) {
497 const auto& genericLayerMetadataCompatibilityMap =
498 mFlinger->getGenericLayerMetadataKeyMap();
499 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
500 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
501 continue;
502 }
503 const uint32_t id = compatIter->second;
504
505 auto it = drawingState.metadata.mMap.find(id);
506 if (it == std::end(drawingState.metadata.mMap)) {
507 continue;
508 }
509
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000510 snapshot->metadata.emplace(key,
511 compositionengine::GenericLayerMetadataEntry{mandatory,
512 it->second});
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800513 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800514}
David Sodmanba340492018-08-05 21:51:33 -0700515
Lloyd Piquede196652020-01-22 17:29:58 -0800516void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800517 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000518 auto* snapshot = editLayerSnapshot();
Lloyd Piquef5275482019-01-29 18:42:42 -0800519
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000520 snapshot->forceClientComposition = false;
Lloyd Piquede196652020-01-22 17:29:58 -0800521
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000522 snapshot->isColorspaceAgnostic = isColorSpaceAgnostic();
523 snapshot->dataspace = getDataSpace();
524 snapshot->colorTransform = getColorTransform();
525 snapshot->colorTransformIsIdentity = !hasColorTransform();
526 snapshot->surfaceDamage = surfaceDamageRegion;
527 snapshot->hasProtectedContent = isProtected();
528 snapshot->dimmingEnabled = isDimmingEnabled();
Lloyd Pique688abd42019-02-15 15:42:24 -0800529
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700530 const bool usesRoundedCorners = hasRoundedCorners();
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700531
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000532 snapshot->isOpaque = isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800533
534 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400535 // Rounded corners no longer force client composition, since we may use a
536 // hole punch so that the layer will appear to have rounded corners.
537 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000538 snapshot->stretchEffect.hasEffect()) {
539 snapshot->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800540 }
Vishnu Nairb801a982021-11-02 15:12:08 -0700541 // If there are no visible region changes, we still need to update blur parameters.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000542 snapshot->blurRegions = drawingState.blurRegions;
543 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400544
545 // Layer framerate is used in caching decisions.
546 // Retrieve it from the scheduler which maintains an instance of LayerHistory, and store it in
547 // LayerFECompositionState where it would be visible to Flattener.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000548 snapshot->fps = mFlinger->getLayerFramerate(systemTime(), getSequence());
Patrick Williamsbb25f802022-08-30 23:02:34 +0000549
550 if (hasBufferOrSidebandStream()) {
551 preparePerFrameBufferCompositionState();
552 } else {
553 preparePerFrameEffectsCompositionState();
554 }
555}
556
557void Layer::preparePerFrameBufferCompositionState() {
558 // Sideband layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000559 auto* snapshot = editLayerSnapshot();
560 if (snapshot->sidebandStream.get() && !snapshot->sidebandStreamHasFrame) {
561 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000562 aidl::android::hardware::graphics::composer3::Composition::SIDEBAND;
563 return;
564 } else if ((mDrawingState.flags & layer_state_t::eLayerIsDisplayDecoration) != 0) {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000565 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000566 aidl::android::hardware::graphics::composer3::Composition::DISPLAY_DECORATION;
567 } else {
568 // Normal buffer layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000569 snapshot->hdrMetadata = mBufferInfo.mHdrMetadata;
570 snapshot->compositionType = mPotentialCursor
Patrick Williamsbb25f802022-08-30 23:02:34 +0000571 ? aidl::android::hardware::graphics::composer3::Composition::CURSOR
572 : aidl::android::hardware::graphics::composer3::Composition::DEVICE;
573 }
574
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000575 snapshot->buffer = getBuffer();
576 snapshot->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
Patrick Williamsbb25f802022-08-30 23:02:34 +0000577 ? 0
578 : mBufferInfo.mBufferSlot;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000579 snapshot->acquireFence = mBufferInfo.mFence;
580 snapshot->frameNumber = mBufferInfo.mFrameNumber;
581 snapshot->sidebandStreamHasFrame = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000582}
583
584void Layer::preparePerFrameEffectsCompositionState() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000585 auto* snapshot = editLayerSnapshot();
586 snapshot->color = getColor();
587 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000588 aidl::android::hardware::graphics::composer3::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800589}
590
Lloyd Piquede196652020-01-22 17:29:58 -0800591void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800592 const State& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000593 auto* snapshot = editLayerSnapshot();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800594
595 // Apply the layer's transform, followed by the display's global transform
596 // Here we're guaranteed that the layer's transform preserves rects
597 Rect win = getCroppedBufferSize(drawingState);
598 // Subtract the transparent region and snap to the bounds
599 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
600 Rect frame(getTransform().transform(bounds));
601
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000602 snapshot->cursorFrame = frame;
Lloyd Piquede196652020-01-22 17:29:58 -0800603}
604
Lloyd Piquea83776c2019-01-29 18:42:32 -0800605const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700606 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800607}
608
Mathias Agopian13127d82013-03-05 17:47:11 -0800609// ---------------------------------------------------------------------------
610// drawing...
611// ---------------------------------------------------------------------------
612
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500613aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType(
614 const DisplayDevice& display) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700615 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800616 if (outputLayer == nullptr) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500617 return aidl::android::hardware::graphics::composer3::Composition::INVALID;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800618 }
619 if (outputLayer->getState().hwc) {
620 return (*outputLayer->getState().hwc).hwcCompositionType;
621 } else {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500622 return aidl::android::hardware::graphics::composer3::Composition::CLIENT;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800623 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624}
625
Mathias Agopian13127d82013-03-05 17:47:11 -0800626// ----------------------------------------------------------------------------
627// local state
628// ----------------------------------------------------------------------------
629
David Sodman41fdfc92017-11-06 16:09:56 -0800630bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800631 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700632 if (s.flags & layer_state_t::eLayerSecure) {
633 return true;
634 }
635
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000636 const auto p = mDrawingParent.promote();
Garfield Tande619fa2020-10-02 17:13:53 -0700637 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700638}
639
Mathias Agopian13127d82013-03-05 17:47:11 -0800640// ----------------------------------------------------------------------------
641// transaction
642// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800643
Marissa Wall61c58622018-07-18 10:12:20 -0700644uint32_t Layer::doTransaction(uint32_t flags) {
645 ATRACE_CALL();
646
Robert Carr0758e5d2021-03-11 22:15:04 -0800647 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700648 mDrawingStateModified = mDrawingState.modified;
649 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700650
Alec Mourib416efd2018-09-06 21:01:59 +0000651 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700652
Robert Carr6a160312021-05-17 12:08:20 -0700653 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800654 // invalidate and recompute the visible regions if needed
655 flags |= Layer::eVisibleRegion;
656 }
657
Robert Carr6a160312021-05-17 12:08:20 -0700658 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800659 // invalidate and recompute the visible regions if needed
Arthur Hung9ed43392022-05-27 06:31:57 +0000660 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800661 flags |= eVisibleRegion;
662 this->contentDirty = true;
663
664 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700665 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800666 }
667
Arthur Hung9ed43392022-05-27 06:31:57 +0000668 if (!mPotentialCursor && (flags & Layer::eVisibleRegion)) {
669 mFlinger->mUpdateInputInfo = true;
670 }
671
Robert Carr6a160312021-05-17 12:08:20 -0700672 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800673
Mathias Agopian13127d82013-03-05 17:47:11 -0800674 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675}
676
Robert Carr6a160312021-05-17 12:08:20 -0700677void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000678 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
679 // bufferSurfaceFrameTX will be presented in latchBuffer.
680 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
681 if (surfaceFrame->getPresentState() != PresentState::Presented) {
682 // With applyPendingStates, we could end up having presented surfaceframes from previous
683 // states
684 surfaceFrame->setPresentState(PresentState::Presented);
685 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
686 }
687 }
Robert Carr6a160312021-05-17 12:08:20 -0700688 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700689}
690
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700691uint32_t Layer::clearTransactionFlags(uint32_t mask) {
692 const auto flags = mTransactionFlags & mask;
693 mTransactionFlags &= ~mask;
694 return flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800695}
696
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700697void Layer::setTransactionFlags(uint32_t mask) {
698 mTransactionFlags |= mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800699}
700
Robert Carr1f0a16a2016-10-24 16:27:39 -0700701bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
702 ssize_t idx = mCurrentChildren.indexOf(childLayer);
703 if (idx < 0) {
704 return false;
705 }
706 if (childLayer->setLayer(z)) {
707 mCurrentChildren.removeAt(idx);
708 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800709 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700710 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800711 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700712}
713
Robert Carr503c7042017-09-27 15:06:08 -0700714bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
715 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
716 ssize_t idx = mCurrentChildren.indexOf(childLayer);
717 if (idx < 0) {
718 return false;
719 }
720 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
721 mCurrentChildren.removeAt(idx);
722 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800723 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700724 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800725 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700726}
727
Robert Carrae060832016-11-28 10:51:00 -0800728bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700729 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
730 mDrawingState.sequence++;
731 mDrawingState.z = z;
732 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700733
Robert Carra70e91c2021-06-11 13:59:52 -0700734 mFlinger->mSomeChildrenChanged = true;
735
Robert Carrdb66e622017-04-10 16:55:57 -0700736 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700737 if (mDrawingState.zOrderRelativeOf != nullptr) {
738 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700739 if (strongRelative != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700740 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700741 }
chaviw606e5cf2019-03-01 10:12:10 -0800742 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700743 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800744 setTransactionFlags(eTransactionNeeded);
745 return true;
746}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700747
Robert Carrdb66e622017-04-10 16:55:57 -0700748void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700749 mDrawingState.zOrderRelatives.remove(relative);
750 mDrawingState.sequence++;
751 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700752 setTransactionFlags(eTransactionNeeded);
753}
754
755void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700756 mDrawingState.zOrderRelatives.add(relative);
757 mDrawingState.modified = true;
758 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700759 setTransactionFlags(eTransactionNeeded);
760}
761
chaviw606e5cf2019-03-01 10:12:10 -0800762void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700763 mDrawingState.zOrderRelativeOf = relativeOf;
764 mDrawingState.sequence++;
765 mDrawingState.modified = true;
766 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700767
chaviw606e5cf2019-03-01 10:12:10 -0800768 setTransactionFlags(eTransactionNeeded);
769}
770
Robert Carr503d2bd2017-12-04 15:49:47 -0800771bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nair07e2a482022-10-18 19:18:16 +0000772 sp<Layer> relative = LayerHandle::getLayer(relativeToHandle);
Robert Carrdb66e622017-04-10 16:55:57 -0700773 if (relative == nullptr) {
774 return false;
775 }
776
Robert Carr6a160312021-05-17 12:08:20 -0700777 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
778 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800779 return false;
780 }
781
Robert Carr88b85e12022-03-21 15:47:35 -0700782 if (CC_UNLIKELY(relative->usingRelativeZ(LayerVector::StateSet::Drawing)) &&
783 (relative->mDrawingState.zOrderRelativeOf == this)) {
784 ALOGE("Detected relative layer loop between %s and %s",
785 mName.c_str(), relative->mName.c_str());
786 ALOGE("Ignoring new call to set relative layer");
787 return false;
788 }
789
Robert Carra70e91c2021-06-11 13:59:52 -0700790 mFlinger->mSomeChildrenChanged = true;
791
Robert Carr6a160312021-05-17 12:08:20 -0700792 mDrawingState.sequence++;
793 mDrawingState.modified = true;
794 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700795
Robert Carr6a160312021-05-17 12:08:20 -0700796 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700797 if (oldZOrderRelativeOf != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700798 oldZOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
chaviw9ab4bd12017-11-03 13:11:00 -0700799 }
chaviw606e5cf2019-03-01 10:12:10 -0800800 setZOrderRelativeOf(relative);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700801 relative->addZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700802
803 setTransactionFlags(eTransactionNeeded);
804
805 return true;
806}
807
Winson Chunga30f7c92021-06-29 15:42:56 -0700808bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
809 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
810 mDrawingState.isTrustedOverlay = isTrustedOverlay;
811 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +0000812 mFlinger->mUpdateInputInfo = true;
Winson Chunga30f7c92021-06-29 15:42:56 -0700813 setTransactionFlags(eTransactionNeeded);
814 return true;
815}
816
817bool Layer::isTrustedOverlay() const {
818 if (getDrawingState().isTrustedOverlay) {
819 return true;
820 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000821 const auto& p = mDrawingParent.promote();
Winson Chunga30f7c92021-06-29 15:42:56 -0700822 return (p != nullptr) && p->isTrustedOverlay();
823}
824
Dan Stoza9e56aa02015-11-02 13:00:03 -0800825bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700826 if (mDrawingState.color.a == alpha) return false;
827 mDrawingState.sequence++;
828 mDrawingState.color.a = alpha;
829 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800830 setTransactionFlags(eTransactionNeeded);
831 return true;
832}
chaviw13fdc492017-06-27 12:40:18 -0700833
Valerie Haudd0b7572019-01-29 14:59:27 -0800834bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700835 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700836 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800837 }
Robert Carr6a160312021-05-17 12:08:20 -0700838 mDrawingState.sequence++;
839 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800840 setTransactionFlags(eTransactionNeeded);
841
Robert Carr6a160312021-05-17 12:08:20 -0700842 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800843 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800844 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700845 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700846 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800847 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700848 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700849
Valerie Haudd0b7572019-01-29 14:59:27 -0800850 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700851 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800852 mFlinger->mLayersAdded = true;
853 // set up SF to handle added color layer
854 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700855 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800856 }
857 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700858 } else if (mDrawingState.bgColorLayer && alpha == 0) {
859 mDrawingState.bgColorLayer->reparent(nullptr);
860 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800861 return true;
862 }
863
Robert Carr6a160312021-05-17 12:08:20 -0700864 mDrawingState.bgColorLayer->setColor(color);
865 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
866 mDrawingState.bgColorLayer->setAlpha(alpha);
867 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800868
chaviw13fdc492017-06-27 12:40:18 -0700869 return true;
870}
871
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700872bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700873 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700874
Robert Carr6a160312021-05-17 12:08:20 -0700875 mDrawingState.sequence++;
876 mDrawingState.cornerRadius = cornerRadius;
877 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700878 setTransactionFlags(eTransactionNeeded);
879 return true;
880}
881
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800882bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700883 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Vishnu Nairb801a982021-11-02 15:12:08 -0700884 // If we start or stop drawing blur then the layer's visibility state may change so increment
885 // the magic sequence number.
886 if (mDrawingState.backgroundBlurRadius == 0 || backgroundBlurRadius == 0) {
887 mDrawingState.sequence++;
888 }
Robert Carr6a160312021-05-17 12:08:20 -0700889 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
890 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800891 setTransactionFlags(eTransactionNeeded);
892 return true;
893}
Marissa Wall61c58622018-07-18 10:12:20 -0700894
Mathias Agopian13127d82013-03-05 17:47:11 -0800895bool Layer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000896 mDrawingState.sequence++;
897 mDrawingState.transparentRegionHint = transparent;
Robert Carr6a160312021-05-17 12:08:20 -0700898 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 setTransactionFlags(eTransactionNeeded);
900 return true;
901}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100902
Lucas Dupinc3800b82020-10-02 16:24:48 -0700903bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Vishnu Nairb801a982021-11-02 15:12:08 -0700904 // If we start or stop drawing blur then the layer's visibility state may change so increment
905 // the magic sequence number.
906 if (mDrawingState.blurRegions.size() == 0 || blurRegions.size() == 0) {
907 mDrawingState.sequence++;
908 }
Robert Carr6a160312021-05-17 12:08:20 -0700909 mDrawingState.blurRegions = blurRegions;
910 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700911 setTransactionFlags(eTransactionNeeded);
912 return true;
913}
914
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800915bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700916 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
917 if (mDrawingState.flags == newFlags) return false;
918 mDrawingState.sequence++;
919 mDrawingState.flags = newFlags;
920 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800921 setTransactionFlags(eTransactionNeeded);
922 return true;
923}
Robert Carr99e27f02016-06-16 15:18:02 -0700924
chaviw25714502021-02-11 10:01:08 -0800925bool Layer::setCrop(const Rect& crop) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000926 if (mDrawingState.crop == crop) return false;
Robert Carr6a160312021-05-17 12:08:20 -0700927 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700928 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700929
Robert Carr6a160312021-05-17 12:08:20 -0700930 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800931 setTransactionFlags(eTransactionNeeded);
932 return true;
933}
Robert Carr8d5227b2017-03-16 15:41:03 -0700934
Evan Roskyef876c92019-01-25 17:46:06 -0800935bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700936 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
937 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -0800938 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800939 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500940}
941
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700942bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -0700943 if (mDrawingState.layerStack == layerStack) return false;
944 mDrawingState.sequence++;
945 mDrawingState.layerStack = layerStack;
946 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800947 setTransactionFlags(eTransactionNeeded);
948 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700949}
950
Peiyong Linc502cb72019-03-01 15:00:23 -0800951bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -0700952 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800953 return false;
954 }
Robert Carr6a160312021-05-17 12:08:20 -0700955 mDrawingState.sequence++;
956 mDrawingState.colorSpaceAgnostic = agnostic;
957 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -0800958 setTransactionFlags(eTransactionNeeded);
959 return true;
960}
961
Sally Qi81d95e62022-03-21 19:41:33 -0700962bool Layer::setDimmingEnabled(const bool dimmingEnabled) {
963 if (mDrawingState.dimmingEnabled == dimmingEnabled) return false;
964
965 mDrawingState.sequence++;
966 mDrawingState.dimmingEnabled = dimmingEnabled;
967 mDrawingState.modified = true;
968 setTransactionFlags(eTransactionNeeded);
969 return true;
970}
971
Ana Krulecc84d09b2019-11-02 23:10:29 +0100972bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -0700973 if (mDrawingState.frameRateSelectionPriority == priority) return false;
974 mDrawingState.frameRateSelectionPriority = priority;
975 mDrawingState.sequence++;
976 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100977 setTransactionFlags(eTransactionNeeded);
978 return true;
979}
980
981int32_t Layer::getFrameRateSelectionPriority() const {
982 // Check if layer has priority set.
983 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
984 return mDrawingState.frameRateSelectionPriority;
985 }
986 // If not, search whether its parents have it set.
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000987 sp<Layer> parent = getParent();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100988 if (parent != nullptr) {
989 return parent->getFrameRateSelectionPriority();
990 }
991
992 return Layer::PRIORITY_UNSET;
993}
994
Andy Labrada096227e2022-06-15 16:58:11 +0000995bool Layer::setDefaultFrameRateCompatibility(FrameRateCompatibility compatibility) {
996 if (mDrawingState.defaultFrameRateCompatibility == compatibility) return false;
997 mDrawingState.defaultFrameRateCompatibility = compatibility;
998 mDrawingState.modified = true;
999 mFlinger->mScheduler->setDefaultFrameRateCompatibility(this);
1000 setTransactionFlags(eTransactionNeeded);
1001 return true;
1002}
1003
1004scheduler::LayerInfo::FrameRateCompatibility Layer::getDefaultFrameRateCompatibility() const {
1005 return mDrawingState.defaultFrameRateCompatibility;
1006}
1007
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001008bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1009 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1010};
1011
Vishnu Nair73908d12022-10-24 21:46:42 -07001012ui::LayerStack Layer::getLayerStack(LayerVector::StateSet state) const {
1013 bool useDrawing = state == LayerVector::StateSet::Drawing;
1014 const auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1015 if (parent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001016 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001017 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001018 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001019}
1020
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001021bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001022 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001023 return false;
1024 }
1025
Robert Carr6a160312021-05-17 12:08:20 -07001026 mDrawingState.sequence++;
1027 mDrawingState.shadowRadius = shadowRadius;
1028 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001029 setTransactionFlags(eTransactionNeeded);
1030 return true;
1031}
1032
Vishnu Nair6213bd92020-05-08 17:42:25 -07001033bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001034 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001035 return false;
1036 }
1037
Robert Carr6a160312021-05-17 12:08:20 -07001038 mDrawingState.sequence++;
1039 mDrawingState.fixedTransformHint = fixedTransformHint;
1040 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001041 setTransactionFlags(eTransactionNeeded);
1042 return true;
1043}
1044
John Reckcdb4ed72021-02-04 13:39:33 -05001045bool Layer::setStretchEffect(const StretchEffect& effect) {
1046 StretchEffect temp = effect;
1047 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001048 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001049 return false;
1050 }
Robert Carr6a160312021-05-17 12:08:20 -07001051 mDrawingState.sequence++;
1052 mDrawingState.stretchEffect = temp;
1053 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001054 setTransactionFlags(eTransactionNeeded);
1055 return true;
1056}
1057
John Reckc00c6692021-02-16 11:37:33 -05001058StretchEffect Layer::getStretchEffect() const {
1059 if (mDrawingState.stretchEffect.hasEffect()) {
1060 return mDrawingState.stretchEffect;
1061 }
1062
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001063 sp<Layer> parent = getParent();
John Reckc00c6692021-02-16 11:37:33 -05001064 if (parent != nullptr) {
1065 auto effect = parent->getStretchEffect();
1066 if (effect.hasEffect()) {
1067 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1068 return effect;
1069 }
1070 }
1071 return StretchEffect{};
1072}
1073
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001074bool Layer::enableBorder(bool shouldEnable, float width, const half4& color) {
1075 if (mBorderEnabled == shouldEnable && mBorderWidth == width && mBorderColor == color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00001076 return false;
1077 }
1078 mBorderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001079 mBorderWidth = width;
1080 mBorderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001081 return true;
1082}
1083
1084bool Layer::isBorderEnabled() {
1085 return mBorderEnabled;
1086}
1087
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001088float Layer::getBorderWidth() {
1089 return mBorderWidth;
1090}
1091
1092const half4& Layer::getBorderColor() {
1093 return mBorderColor;
1094}
1095
Ady Abrahama850c182021-08-04 13:04:37 -07001096bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1097 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1098 const auto frameRate = [&] {
1099 if (mDrawingState.frameRate.rate.isValid() ||
1100 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1101 return mDrawingState.frameRate;
1102 }
1103
1104 return parentFrameRate;
1105 }();
1106
1107 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1108
1109 // The frame rate is propagated to the children
1110 bool childrenHaveFrameRate = false;
1111 for (const sp<Layer>& child : mCurrentChildren) {
1112 childrenHaveFrameRate |=
1113 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1114 }
1115
1116 // If we don't have a valid frame rate, but the children do, we set this
1117 // layer as NoVote to allow the children to control the refresh rate
1118 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1119 childrenHaveFrameRate) {
1120 *transactionNeeded |=
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001121 setFrameRateForLayerTree(FrameRate(Fps(), FrameRateCompatibility::NoVote));
Ady Abrahama850c182021-08-04 13:04:37 -07001122 }
1123
1124 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1125 // the same reason we are allowing touch boost for those layers. See
Ady Abraham68636062022-11-16 17:07:25 -08001126 // RefreshRateSelector::rankFrameRates for details.
Ady Abrahama850c182021-08-04 13:04:37 -07001127 const auto layerVotedWithDefaultCompatibility =
1128 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1129 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1130 const auto layerVotedWithExactCompatibility =
1131 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1132 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1133 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1134}
1135
Ady Abraham60e42ea2020-03-09 19:17:31 -07001136void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001137 const auto root = [&]() -> sp<Layer> {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001138 sp<Layer> layer = sp<Layer>::fromExisting(this);
Ady Abrahama850c182021-08-04 13:04:37 -07001139 while (auto parent = layer->getParent()) {
1140 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001141 }
Ady Abrahama850c182021-08-04 13:04:37 -07001142 return layer;
1143 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001144
Ady Abraham60e42ea2020-03-09 19:17:31 -07001145 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001146 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001147
Ady Abrahama850c182021-08-04 13:04:37 -07001148 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001149 if (transactionNeeded) {
1150 mFlinger->setTransactionFlags(eTraversalNeeded);
1151 }
1152}
1153
Ady Abraham71c437d2020-01-31 15:56:57 -08001154bool Layer::setFrameRate(FrameRate frameRate) {
Robert Carr6a160312021-05-17 12:08:20 -07001155 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001156 return false;
1157 }
1158
Robert Carr6a160312021-05-17 12:08:20 -07001159 mDrawingState.sequence++;
1160 mDrawingState.frameRate = frameRate;
1161 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001162
1163 updateTreeHasFrameRateVote();
1164
Steven Thomas3172e202020-01-06 19:25:30 -08001165 setTransactionFlags(eTransactionNeeded);
1166 return true;
1167}
1168
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001169void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1170 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001171 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001172
1173 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1174 // there are two transactions with the same token, the first one without a buffer and the
1175 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1176 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001177 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1178 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001179 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001180 mDrawingState.bufferSurfaceFrameTX = it->second;
1181 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1182 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1183 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001184 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001185 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001186 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1187 }
1188}
1189
1190void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1191 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001192 mDrawingState.frameTimelineInfo = info;
1193 mDrawingState.postTime = postTime;
1194 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001195 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001196
Robert Carr6a160312021-05-17 12:08:20 -07001197 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001198 bufferSurfaceFrameTX != nullptr) {
1199 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1200 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1201 // being used for BufferSurfaceFrame, don't create a new one.
1202 return;
1203 }
1204 }
1205 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1206 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1207 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001208 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1209 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001210 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001211 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001212 } else {
1213 if (it->second->getPresentState() == PresentState::Presented) {
1214 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1215 // have been from previous vsync.
1216 it->second = createSurfaceFrameForTransaction(info, postTime);
1217 }
1218 }
1219}
1220
1221void Layer::addSurfaceFrameDroppedForBuffer(
1222 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001223 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001224 surfaceFrame->setPresentState(PresentState::Dropped);
1225 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1226}
1227
1228void Layer::addSurfaceFramePresentedForBuffer(
1229 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1230 nsecs_t currentLatchTime) {
1231 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1232 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1233 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1234 mLastLatchTime = currentLatchTime;
1235}
1236
1237std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1238 const FrameTimelineInfo& info, nsecs_t postTime) {
1239 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001240 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1241 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001242 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001243 /*isBuffer*/ false, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001244 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001245 // For Transactions, the post time is considered to be both queue and acquire fence time.
1246 surfaceFrame->setActualQueueTime(postTime);
1247 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001248 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1249 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001250 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001251 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001252 onSurfaceFrameCreated(surfaceFrame);
1253 return surfaceFrame;
1254}
1255
1256std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1257 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1258 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001259 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001260 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001261 /*isBuffer*/ true, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001262 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001263 // For buffers, acquire fence time will set during latch.
1264 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001265 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1266 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001267 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001268 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001269 onSurfaceFrameCreated(surfaceFrame);
1270 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001271}
1272
Ady Abrahama850c182021-08-04 13:04:37 -07001273bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1274 if (mDrawingState.frameRateForLayerTree == frameRate) {
1275 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001276 }
1277
Ady Abrahama850c182021-08-04 13:04:37 -07001278 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001279
Ady Abrahama850c182021-08-04 13:04:37 -07001280 // TODO(b/195668952): we probably don't need to dirty visible regions here
1281 // or even store frameRateForLayerTree in mDrawingState
1282 mDrawingState.sequence++;
1283 mDrawingState.modified = true;
1284 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001285
Dominik Laskowski068173d2021-08-11 17:22:59 -07001286 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
1287 mFlinger->mScheduler->recordLayerHistory(this, systemTime(), LayerUpdateType::SetFrameRate);
Ady Abrahama850c182021-08-04 13:04:37 -07001288
1289 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001290}
1291
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001292Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1293 return getDrawingState().frameRateForLayerTree;
1294}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295
Robert Carr1f0a16a2016-10-24 16:27:39 -07001296bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001297 const State& s(mDrawingState);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001298 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001299 if (parent != nullptr && parent->isHiddenByPolicy()) {
1300 return true;
1301 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001302 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1303 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1304 if (zOrderRelativeOf != nullptr) {
1305 if (zOrderRelativeOf->isHiddenByPolicy()) {
1306 return true;
1307 }
1308 }
1309 }
Garfield Tan2c1782c2022-02-16 15:25:05 -08001310 if (CC_UNLIKELY(!isTransformValid())) {
1311 ALOGW("Hide layer %s because it has invalid transformation.", getDebugName());
1312 return true;
1313 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001314 return s.flags & layer_state_t::eLayerHidden;
1315}
1316
David Sodman41fdfc92017-11-06 16:09:56 -08001317uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001318 // TODO: should we do something special if mSecure is set?
1319 if (mProtectedByApp) {
1320 // need a hardware-protected path to external video sink
1321 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001322 }
Riley Andrews03414a12014-07-01 14:22:59 -07001323 if (mPotentialCursor) {
1324 usage |= GraphicBuffer::USAGE_CURSOR;
1325 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001326 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001327 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001328}
1329
Vishnu Nair71fcf912022-10-18 09:14:20 -07001330void Layer::skipReportingTransformHint() {
1331 mSkipReportingTransformHint = true;
1332}
1333
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001334void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1335 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1336 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001337 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001338
Vishnu Nair6213bd92020-05-08 17:42:25 -07001339 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001340}
1341
Mathias Agopian13127d82013-03-05 17:47:11 -08001342// ----------------------------------------------------------------------------
1343// debugging
1344// ----------------------------------------------------------------------------
1345
Marissa Wall61c58622018-07-18 10:12:20 -07001346// TODO(marissaw): add new layer state info to layer debugging
Huihong Luo05539a12022-02-23 10:29:40 -08001347gui::LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001348 using namespace std::string_literals;
1349
Huihong Luo05539a12022-02-23 10:29:40 -08001350 gui::LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001351 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001352 info.mName = getName();
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001353 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001354 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001355 info.mType = getType();
Lloyd Piquea2468662019-03-07 21:31:06 -08001356
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001357 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001358 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001359 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001360 info.mX = ds.transform.tx();
1361 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001362 info.mZ = ds.z;
chaviw25714502021-02-11 10:01:08 -08001363 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001364 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001365 info.mFlags = ds.flags;
1366 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001367 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001368 info.mMatrix[0][0] = ds.transform[0][0];
1369 info.mMatrix[0][1] = ds.transform[0][1];
1370 info.mMatrix[1][0] = ds.transform[1][0];
1371 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001372 {
chaviwd62d3062019-09-04 14:48:02 -07001373 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001374 if (buffer != 0) {
1375 info.mActiveBufferWidth = buffer->getWidth();
1376 info.mActiveBufferHeight = buffer->getHeight();
1377 info.mActiveBufferStride = buffer->getStride();
1378 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001379 } else {
1380 info.mActiveBufferWidth = 0;
1381 info.mActiveBufferHeight = 0;
1382 info.mActiveBufferStride = 0;
1383 info.mActiveBufferFormat = 0;
1384 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001385 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001386 info.mNumQueuedFrames = getQueuedFrameCount();
Kalle Raitaa099a242017-01-11 11:17:29 -08001387 info.mIsOpaque = isOpaque(ds);
1388 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001389 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001390 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001391}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001392
Yiwei Zhang5434a782018-12-05 18:06:32 -08001393void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001394 result.append(kDumpTableRowLength, '-');
1395 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001396 result.append(" Layer name\n");
1397 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001398 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001399 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001400 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001401 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001402 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001403 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1404 result.append(kDumpTableRowLength, '-');
1405 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001406}
1407
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001408void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1409 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001410 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001411 return;
1412 }
1413
Yiwei Zhang5434a782018-12-05 18:06:32 -08001414 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001415 if (mName.length() > 77) {
1416 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001417 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001418 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001419 shortened.append(mName, mName.length() - 36);
1420 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001421 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001422 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001423 }
1424
Yiwei Zhang5434a782018-12-05 18:06:32 -08001425 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001426
Alec Mourib416efd2018-09-06 21:01:59 +00001427 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001428 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001429
Chia-I Wu1e043612018-03-01 09:45:09 -08001430 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001431 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001432 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001433 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001434 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001435 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001436 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001437 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1438 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001439 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001440 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001441 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001442 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001443 const auto frameRate = getFrameRateForLayerTree();
1444 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1445 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001446 ftl::enum_string(frameRate.type).c_str(),
1447 ftl::enum_string(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001448 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001449 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001450 }
Dan Stozae22aec72016-08-01 13:20:59 -07001451
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001452 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1453 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1454
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001455 result.append(kDumpTableRowLength, '-');
1456 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001457}
Dan Stozae22aec72016-08-01 13:20:59 -07001458
Yiwei Zhang5434a782018-12-05 18:06:32 -08001459void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001460 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001461}
1462
Svetoslavd85084b2014-03-20 10:28:31 -07001463void Layer::clearFrameStats() {
1464 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001465}
1466
Jamie Gennis6547ff42013-07-16 20:12:42 -07001467void Layer::logFrameStats() {
1468 mFrameTracker.logAndResetStats(mName);
1469}
1470
Svetoslavd85084b2014-03-20 10:28:31 -07001471void Layer::getFrameStats(FrameStats* outStats) const {
1472 mFrameTracker.getStats(outStats);
1473}
1474
Vishnu Nair0f085c62019-08-30 08:49:12 -07001475void Layer::dumpCallingUidPid(std::string& result) const {
Vishnu Naircb8be502022-10-12 19:03:23 +00001476 StringAppendF(&result, "Layer %s (%s) ownerPid:%d ownerUid:%d\n", getName().c_str(), getType(),
1477 mOwnerPid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001478}
1479
Brian Anderson5ea5e592016-12-01 16:54:33 -08001480void Layer::onDisconnect() {
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001481 const int32_t layerId = getSequence();
1482 mFlinger->mTimeStats->onDestroy(layerId);
1483 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001484}
1485
Chia-I Wu98f1c102017-05-30 14:54:08 -07001486size_t Layer::getChildrenCount() const {
1487 size_t count = 0;
1488 for (const sp<Layer>& child : mCurrentChildren) {
1489 count += 1 + child->getChildrenCount();
1490 }
1491 return count;
1492}
1493
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001494void Layer::setGameModeForTree(GameMode gameMode) {
1495 const auto& currentState = getDrawingState();
Huihong Luod3d8f8e2022-03-08 14:48:46 -08001496 if (currentState.metadata.has(gui::METADATA_GAME_MODE)) {
1497 gameMode =
1498 static_cast<GameMode>(currentState.metadata.getInt32(gui::METADATA_GAME_MODE, 0));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001499 }
1500 setGameMode(gameMode);
1501 for (const sp<Layer>& child : mCurrentChildren) {
1502 child->setGameModeForTree(gameMode);
1503 }
1504}
1505
Robert Carr1f0a16a2016-10-24 16:27:39 -07001506void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001507 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001508 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001509
Robert Carr1f0a16a2016-10-24 16:27:39 -07001510 mCurrentChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07001511 layer->setParent(sp<Layer>::fromExisting(this));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001512 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001513 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001514}
1515
1516ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001517 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001518 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001519
Robert Carr1323c952019-01-28 18:13:27 -08001520 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001521 const auto removeResult = mCurrentChildren.remove(layer);
1522
1523 updateTreeHasFrameRateVote();
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001524 layer->setGameModeForTree(GameMode::Unsupported);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001525 layer->updateTreeHasFrameRateVote();
1526
1527 return removeResult;
1528}
1529
Robert Carr15eae092018-03-23 13:43:53 -07001530void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001531 for (const sp<Layer>& child : mDrawingChildren) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001532 child->mDrawingParent = newParent;
xinying1f4200e62022-04-26 14:41:30 +08001533 const float parentShadowRadius =
1534 newParent->canDrawShadows() ? 0.f : newParent->mEffectiveShadowRadius;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001535 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
xinying1f4200e62022-04-26 14:41:30 +08001536 parentShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001537 }
1538}
1539
chaviwf1961f72017-09-18 16:41:07 -07001540bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001541 sp<Layer> newParent;
1542 if (newParentHandle != nullptr) {
Vishnu Nair07e2a482022-10-18 19:18:16 +00001543 newParent = LayerHandle::getLayer(newParentHandle);
Robert Carr54cf5b12019-01-25 14:02:28 -08001544 if (newParent == nullptr) {
1545 ALOGE("Unable to promote Layer handle");
1546 return false;
1547 }
1548 if (newParent == this) {
1549 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1550 return false;
1551 }
1552 }
1553
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001554 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001555 if (parent != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001556 parent->removeChild(sp<Layer>::fromExisting(this));
chaviw06178942017-07-27 10:25:59 -07001557 }
1558
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001559 if (newParentHandle != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001560 newParent->addChild(sp<Layer>::fromExisting(this));
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001561 if (!newParent->isRemovedFromCurrentState()) {
1562 addToCurrentState();
1563 } else {
1564 onRemovedFromCurrentState();
1565 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001566 } else {
1567 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001568 }
1569
chaviw06178942017-07-27 10:25:59 -07001570 return true;
1571}
1572
Peiyong Lind3788632018-09-18 16:01:31 -07001573bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001574 static const mat4 identityMatrix = mat4();
1575
Robert Carr6a160312021-05-17 12:08:20 -07001576 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001577 return false;
1578 }
Robert Carr6a160312021-05-17 12:08:20 -07001579 ++mDrawingState.sequence;
1580 mDrawingState.colorTransform = matrix;
1581 mDrawingState.hasColorTransform = matrix != identityMatrix;
1582 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001583 setTransactionFlags(eTransactionNeeded);
1584 return true;
1585}
1586
chaviwf66724d2018-11-28 16:35:21 -08001587mat4 Layer::getColorTransform() const {
1588 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001589 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001590 colorTransform = parent->getColorTransform() * colorTransform;
1591 }
1592 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001593}
1594
1595bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001596 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001597 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001598 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1599 }
1600 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001601}
1602
Chia-I Wu11481472018-05-04 10:43:19 -07001603bool Layer::isLegacyDataSpace() const {
1604 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001605 return !(getDataSpace() &
1606 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1607 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001608}
1609
Robert Carr1f0a16a2016-10-24 16:27:39 -07001610void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001611 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001612}
1613
Robert Carr6a160312021-05-17 12:08:20 -07001614int32_t Layer::getZ(LayerVector::StateSet) const {
1615 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001616}
1617
Robert Carr1c5481e2019-07-01 14:42:27 -07001618bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001619 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001620 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001621 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001622}
1623
David Sodman41fdfc92017-11-06 16:09:56 -08001624__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001625 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001626 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1627 "makeTraversalList received invalid stateSet");
1628 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1629 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001630 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001631
Robert Carr29abff82017-12-04 13:51:20 -08001632 if (state.zOrderRelatives.size() == 0) {
1633 *outSkipRelativeZUsers = true;
1634 return children;
1635 }
1636
chaviwfd462612018-05-31 16:11:27 -07001637 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001638 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001639 sp<Layer> strongRelative = weakRelative.promote();
1640 if (strongRelative != nullptr) {
1641 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001642 }
1643 }
1644
Dan Stoza412903f2017-04-27 13:42:17 -07001645 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001646 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001647 continue;
1648 }
Robert Carrdb66e622017-04-10 16:55:57 -07001649 traverse.add(child);
1650 }
1651
1652 return traverse;
1653}
1654
Robert Carr1f0a16a2016-10-24 16:27:39 -07001655/**
Robert Carrdb66e622017-04-10 16:55:57 -07001656 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657 */
Dan Stoza412903f2017-04-27 13:42:17 -07001658void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001659 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1660 // produce a new list for traversing, including our relatives, and not including our children
1661 // who are relatives of another surface. In the case that there are no relative Z,
1662 // makeTraversalList returns our children directly to avoid significant overhead.
1663 // However in this case we need to take the responsibility for filtering children which
1664 // are relatives of another surface here.
1665 bool skipRelativeZUsers = false;
1666 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001667
Robert Carr1f0a16a2016-10-24 16:27:39 -07001668 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001669 for (; i < list.size(); i++) {
1670 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001671 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1672 continue;
1673 }
1674
chaviw301b1d82019-11-06 13:15:09 -08001675 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001677 }
Dan Stoza412903f2017-04-27 13:42:17 -07001678 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001679 }
Robert Carr29abff82017-12-04 13:51:20 -08001680
Dan Stoza412903f2017-04-27 13:42:17 -07001681 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001682 for (; i < list.size(); i++) {
1683 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001684
Robert Carr29abff82017-12-04 13:51:20 -08001685 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1686 continue;
1687 }
Dan Stoza412903f2017-04-27 13:42:17 -07001688 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001689 }
1690}
1691
1692/**
Robert Carrdb66e622017-04-10 16:55:57 -07001693 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001694 */
Dan Stoza412903f2017-04-27 13:42:17 -07001695void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1696 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001697 // See traverseInZOrder for documentation.
1698 bool skipRelativeZUsers = false;
1699 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001700
Robert Carr1f0a16a2016-10-24 16:27:39 -07001701 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001702 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001703 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001704
1705 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1706 continue;
1707 }
1708
chaviw301b1d82019-11-06 13:15:09 -08001709 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710 break;
1711 }
Dan Stoza412903f2017-04-27 13:42:17 -07001712 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001713 }
Dan Stoza412903f2017-04-27 13:42:17 -07001714 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001715 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001716 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001717
1718 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1719 continue;
1720 }
1721
Dan Stoza412903f2017-04-27 13:42:17 -07001722 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723 }
1724}
1725
Edgar Arriaga844fa672020-01-16 14:21:42 -08001726void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1727 visitor(this);
1728 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001729 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001730 for (const sp<Layer>& child : children) {
1731 child->traverse(state, visitor);
1732 }
1733}
1734
chaviw4b129c22018-04-09 16:19:43 -07001735LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1736 const std::vector<Layer*>& layersInTree) {
1737 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1738 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001739 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1740 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001741 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001742
chaviwfd462612018-05-31 16:11:27 -07001743 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001744 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1745 sp<Layer> strongRelative = weakRelative.promote();
1746 // Only add relative layers that are also descendents of the top most parent of the tree.
1747 // If a relative layer is not a descendent, then it should be ignored.
1748 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1749 traverse.add(strongRelative);
1750 }
1751 }
1752
1753 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001754 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001755 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1756 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1757 // the child here since it won't be added later as a relative.
1758 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1759 childState.zOrderRelativeOf.promote().get())) {
1760 continue;
1761 }
1762 traverse.add(child);
1763 }
1764
1765 return traverse;
1766}
1767
1768void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1769 LayerVector::StateSet stateSet,
1770 const LayerVector::Visitor& visitor) {
1771 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001772
1773 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001774 for (; i < list.size(); i++) {
1775 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001776 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001777 break;
1778 }
chaviw4b129c22018-04-09 16:19:43 -07001779 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001780 }
chaviw4b129c22018-04-09 16:19:43 -07001781
chaviwa76b2712017-09-20 12:02:26 -07001782 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001783 for (; i < list.size(); i++) {
1784 const auto& relative = list[i];
1785 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001786 }
1787}
1788
chaviw4b129c22018-04-09 16:19:43 -07001789std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1790 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1791 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1792
1793 std::vector<Layer*> layersInTree = {this};
1794 for (size_t i = 0; i < children.size(); i++) {
1795 const auto& child = children[i];
1796 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1797 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1798 }
1799
1800 return layersInTree;
1801}
1802
1803void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1804 const LayerVector::Visitor& visitor) {
1805 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1806 std::sort(layersInTree.begin(), layersInTree.end());
1807 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1808}
1809
Peiyong Linefefaac2018-08-17 12:27:51 -07001810ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001811 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001812}
1813
Garfield Tan2c1782c2022-02-16 15:25:05 -08001814bool Layer::isTransformValid() const {
1815 float transformDet = getTransform().det();
1816 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
1817}
1818
chaviw13fdc492017-06-27 12:40:18 -07001819half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001820 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001821
chaviw13fdc492017-06-27 12:40:18 -07001822 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1823 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001824}
Robert Carr6452f122017-03-21 10:41:29 -07001825
Vishnu Nair6213bd92020-05-08 17:42:25 -07001826ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001827 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001828 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1829 return fixedTransformHint;
1830 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001831 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001832 if (!p) return fixedTransformHint;
1833 return p->getFixedTransformHint();
1834}
1835
chaviw13fdc492017-06-27 12:40:18 -07001836half4 Layer::getColor() const {
1837 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001838 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001839}
Robert Carr6452f122017-03-21 10:41:29 -07001840
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001841int32_t Layer::getBackgroundBlurRadius() const {
Vishnu Nair29810f72022-07-01 16:38:41 +00001842 if (getDrawingState().backgroundBlurRadius == 0) {
1843 return 0;
1844 }
Galia Peychevada7de0e2020-12-03 17:24:35 +01001845
Vishnu Nair29810f72022-07-01 16:38:41 +00001846 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001847 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1848 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001849}
1850
Galia Peychevae0acf382021-04-12 21:22:34 +02001851const std::vector<BlurRegion> Layer::getBlurRegions() const {
1852 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001853 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001854 for (auto& region : regionsCopy) {
1855 region.alpha = region.alpha * layerAlpha;
1856 }
1857 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001858}
1859
Vishnu Naire14c6b32022-08-06 04:20:15 +00001860RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001861 // Get parent settings
1862 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001863 const auto& parent = mDrawingParent.promote();
1864 if (parent != nullptr) {
1865 parentSettings = parent->getRoundedCornerState();
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001866 if (parentSettings.hasRoundedCorners()) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001867 ui::Transform t = getActiveTransform(getDrawingState());
1868 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001869 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001870 parentSettings.radius.x *= t.getScaleX();
1871 parentSettings.radius.y *= t.getScaleY();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001872 }
1873 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001874
1875 // Get layer settings
1876 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001877 const vec2 radius(getDrawingState().cornerRadius, getDrawingState().cornerRadius);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001878 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001879 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001880
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001881 if (layerSettingsValid && parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001882 // If the parent and the layer have rounded corner settings, use the parent settings if the
1883 // parent crop is entirely inside the layer crop.
1884 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1885 if (parentSettings.cropRect.left > layerCropRect.left &&
1886 parentSettings.cropRect.top > layerCropRect.top &&
1887 parentSettings.cropRect.right < layerCropRect.right &&
1888 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1889 return parentSettings;
1890 } else {
1891 return layerSettings;
1892 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001893 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001894 return layerSettings;
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001895 } else if (parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001896 return parentSettings;
1897 }
1898 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001899}
1900
Robert Carr88b85e12022-03-21 15:47:35 -07001901bool Layer::findInHierarchy(const sp<Layer>& l) {
1902 if (l == this) {
1903 return true;
1904 }
1905 for (auto& child : mDrawingChildren) {
1906 if (child->findInHierarchy(l)) {
1907 return true;
1908 }
1909 }
1910 return false;
1911}
1912
Robert Carr1f0a16a2016-10-24 16:27:39 -07001913void Layer::commitChildList() {
1914 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1915 const auto& child = mCurrentChildren[i];
1916 child->commitChildList();
1917 }
1918 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001919 mDrawingParent = mCurrentParent;
Robert Carr88b85e12022-03-21 15:47:35 -07001920 if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) {
1921 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1922 if (zOrderRelativeOf == nullptr) return;
1923 if (findInHierarchy(zOrderRelativeOf)) {
1924 ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(),
1925 zOrderRelativeOf->mName.c_str());
1926 ALOGE("Severing rel Z loop, potentially dangerous");
1927 mDrawingState.isRelativeOf = false;
Ady Abrahamd11bade2022-08-01 16:18:03 -07001928 zOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carr88b85e12022-03-21 15:47:35 -07001929 }
1930 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001931}
1932
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001933
chaviw3277faf2021-05-19 16:45:23 -05001934void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001935 mDrawingState.inputInfo = info;
Vishnu Nair07e2a482022-10-18 19:18:16 +00001936 mDrawingState.touchableRegionCrop =
1937 LayerHandle::getLayer(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07001938 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +00001939 mFlinger->mUpdateInputInfo = true;
Robert Carr720e5062018-07-30 17:45:14 -07001940 setTransactionFlags(eTransactionNeeded);
1941}
1942
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001943LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) {
chaviw08f3cb22020-01-13 13:17:21 -08001944 LayerProto* layerProto = layersProto.add_layers();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001945 writeToProtoDrawingState(layerProto);
chaviw08f3cb22020-01-13 13:17:21 -08001946 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1947
Vishnu Nair00b90132021-11-05 14:03:40 -07001948 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001949 ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread.
1950
Vishnu Nair60db8c02020-04-02 11:55:16 -07001951 // Only populate for the primary display.
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001952 if (const auto display = mFlinger->getDefaultDisplayDeviceLocked()) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05001953 const auto compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07001954 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001955 LayerProtoHelper::writeToProto(getVisibleRegion(display.get()),
1956 [&]() { return layerProto->mutable_visible_region(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07001957 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08001958 }
1959
chaviw08f3cb22020-01-13 13:17:21 -08001960 for (const sp<Layer>& layer : mDrawingChildren) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001961 layer->writeToProto(layersProto, traceFlags);
chaviw08f3cb22020-01-13 13:17:21 -08001962 }
chaviw6d89e2d2020-01-14 14:42:01 -08001963
1964 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08001965}
1966
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001967void Layer::writeToProtoDrawingState(LayerProto* layerInfo) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001968 const ui::Transform transform = getTransform();
Vishnu Naird37343b2022-01-12 16:18:56 -08001969 auto buffer = getExternalTexture();
Vishnu Nair00b90132021-11-05 14:03:40 -07001970 if (buffer != nullptr) {
Vishnu Naird37343b2022-01-12 16:18:56 -08001971 LayerProtoHelper::writeToProto(*buffer,
Vishnu Nair00b90132021-11-05 14:03:40 -07001972 [&]() { return layerInfo->mutable_active_buffer(); });
1973 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
1974 layerInfo->mutable_buffer_transform());
1975 }
1976 layerInfo->set_invalidate(contentDirty);
1977 layerInfo->set_is_protected(isProtected());
1978 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
1979 layerInfo->set_queued_frames(getQueuedFrameCount());
Vishnu Nair00b90132021-11-05 14:03:40 -07001980 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nair00b90132021-11-05 14:03:40 -07001981 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001982 layerInfo->set_corner_radius(
1983 (getRoundedCornerState().radius.x + getRoundedCornerState().radius.y) / 2.0);
Vishnu Nair00b90132021-11-05 14:03:40 -07001984 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
1985 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
1986 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
1987 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1988 [&]() { return layerInfo->mutable_position(); });
1989 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair00b90132021-11-05 14:03:40 -07001990 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1991 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07001992
Vishnu Nair00b90132021-11-05 14:03:40 -07001993 if (hasColorTransform()) {
1994 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07001995 }
1996
Vishnu Nair60db8c02020-04-02 11:55:16 -07001997 LayerProtoHelper::writeToProto(mSourceBounds,
1998 [&]() { return layerInfo->mutable_source_bounds(); });
1999 LayerProtoHelper::writeToProto(mScreenBounds,
2000 [&]() { return layerInfo->mutable_screen_bounds(); });
2001 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2002 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2003 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002004}
2005
2006void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002007 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002008 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2009 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002010 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002011
chaviw766c9c52021-02-10 17:36:47 -08002012 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002013
Vishnu Nair00b90132021-11-05 14:03:40 -07002014 layerInfo->set_id(sequence);
2015 layerInfo->set_name(getName().c_str());
2016 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002017
Vishnu Nair00b90132021-11-05 14:03:40 -07002018 for (const auto& child : children) {
2019 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002020 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002021
Vishnu Nair00b90132021-11-05 14:03:40 -07002022 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2023 sp<Layer> strongRelative = weakRelative.promote();
2024 if (strongRelative != nullptr) {
2025 layerInfo->add_relatives(strongRelative->sequence);
2026 }
2027 }
2028
Vishnu Nairea04b6f2022-08-19 21:28:17 +00002029 LayerProtoHelper::writeToProto(state.transparentRegionHint,
Vishnu Nair00b90132021-11-05 14:03:40 -07002030 [&]() { return layerInfo->mutable_transparent_region(); });
2031
2032 layerInfo->set_layer_stack(getLayerStack().id);
2033 layerInfo->set_z(state.z);
2034
2035 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2036 return layerInfo->mutable_requested_position();
2037 });
2038
Vishnu Nair00b90132021-11-05 14:03:40 -07002039 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2040
2041 layerInfo->set_is_opaque(isOpaque(state));
2042
2043 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2044 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2045 LayerProtoHelper::writeToProto(state.color,
2046 [&]() { return layerInfo->mutable_requested_color(); });
2047 layerInfo->set_flags(state.flags);
2048
2049 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2050 layerInfo->mutable_requested_transform());
2051
2052 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2053 if (parent != nullptr) {
2054 layerInfo->set_parent(parent->sequence);
2055 } else {
2056 layerInfo->set_parent(-1);
2057 }
2058
2059 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2060 if (zOrderRelativeOf != nullptr) {
2061 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2062 } else {
2063 layerInfo->set_z_order_relative_of(-1);
2064 }
2065
2066 layerInfo->set_is_relative_of(state.isRelativeOf);
2067
2068 layerInfo->set_owner_uid(mOwnerUid);
2069
Arthur Hung4cf2d8c2022-04-07 14:52:00 +00002070 if ((traceFlags & LayerTracing::TRACE_INPUT) && needsInputInfo()) {
chaviw3277faf2021-05-19 16:45:23 -05002071 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002072 if (useDrawing) {
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002073 info = fillInputInfo(
2074 InputDisplayArgs{.transform = &kIdentityTransform, .isSecure = true});
chaviw4c34a092020-07-08 11:30:06 -07002075 } else {
2076 info = state.inputInfo;
2077 }
2078
2079 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002080 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002081 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002082
Vishnu Nair00b90132021-11-05 14:03:40 -07002083 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002084 auto protoMap = layerInfo->mutable_metadata();
2085 for (const auto& entry : state.metadata.mMap) {
2086 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2087 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002088 }
Vishnu Naird2aaab12022-02-10 14:49:09 -08002089
2090 LayerProtoHelper::writeToProto(state.destinationFrame,
2091 [&]() { return layerInfo->mutable_destination_frame(); });
chaviw1d044282017-09-27 12:19:28 -07002092}
2093
Robert Carr2e102c92018-10-23 12:11:15 -07002094bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002095 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002096}
2097
Prabir Pradhan33da9462022-06-14 14:55:57 +00002098// Applies the given transform to the region, while protecting against overflows caused by any
2099// offsets. If applying the offset in the transform to any of the Rects in the region would result
2100// in an overflow, they are not added to the output Region.
2101static Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
2102 const std::string& debugWindowName) {
2103 // Round the translation using the same rounding strategy used by ui::Transform.
2104 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
2105 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
2106
2107 ui::Transform transformWithoutOffset = t;
2108 transformWithoutOffset.set(0.f, 0.f);
2109
2110 const Region transformed = transformWithoutOffset.transform(r);
2111
2112 // Apply the translation to each of the Rects in the region while discarding any that overflow.
2113 Region ret;
2114 for (const auto& rect : transformed) {
2115 Rect newRect;
2116 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
2117 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
2118 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
2119 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
2120 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
2121 debugWindowName.c_str());
2122 continue;
2123 }
2124 ret.orSelf(newRect);
2125 }
2126 return ret;
2127}
2128
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002129void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& screenToDisplay) {
2130 Rect tmpBounds = getInputBounds();
2131 if (!tmpBounds.isValid()) {
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08002132 info.touchableRegion.clear();
2133 // A layer could have invalid input bounds and still expect to receive touch input if it has
2134 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
2135 // correctly to determine the coordinate space for input events. Use an empty rect so that
2136 // the layer will receive input in its own layer space.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002137 tmpBounds = Rect::EMPTY_RECT;
chaviw7e72caf2020-12-02 16:50:43 -08002138 }
2139
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002140 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
2141 // frame and transform used for the layer, which determines the bounds and the coordinate space
2142 // within which the layer will receive input.
2143 //
2144 // The coordinate space within which each of the bounds are specified is explicitly documented
2145 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
2146 // Transform converts one coordinate space to another, which is apparent in its naming. For
2147 // example, "layerToDisplay" transforms layer space to display space.
2148 //
2149 // Coordinate space definitions:
2150 // - display: The display device's coordinate space. Correlates to pixels on the display.
2151 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
2152 // - layer: The coordinate space of this layer.
2153 // - input: The coordinate space in which this layer will receive input events. This could be
2154 // different than layer space if a surfaceInset is used, which changes the origin
2155 // of the input space.
2156 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
chaviw7e72caf2020-12-02 16:50:43 -08002157
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002158 // Clamp surface inset to the input bounds.
2159 const auto surfaceInset = static_cast<float>(info.surfaceInset);
2160 const float xSurfaceInset =
2161 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
2162 const float ySurfaceInset =
2163 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
chaviw1ff3d1e2020-07-01 15:53:47 -07002164
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002165 // Apply the insets to the input bounds.
2166 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
2167 inputBoundsInLayer.top + ySurfaceInset,
2168 inputBoundsInLayer.right - xSurfaceInset,
2169 inputBoundsInLayer.bottom - ySurfaceInset);
Ady Abraham282f1d72019-07-24 18:05:56 -07002170
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002171 // Crop the input bounds to ensure it is within the parent's bounds.
2172 const FloatRect croppedInsetBoundsInLayer = mBounds.intersect(insetBoundsInLayer);
Ady Abraham282f1d72019-07-24 18:05:56 -07002173
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002174 const ui::Transform layerToScreen = getInputTransform();
2175 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
Vishnu Nair8033a492018-12-05 07:27:23 -08002176
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002177 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
2178 info.frameLeft = roundedFrameInDisplay.left;
2179 info.frameTop = roundedFrameInDisplay.top;
2180 info.frameRight = roundedFrameInDisplay.right;
2181 info.frameBottom = roundedFrameInDisplay.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -07002182
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002183 ui::Transform inputToLayer;
2184 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
2185 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
chaviw39cfa2e2020-11-04 14:19:02 -08002186
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002187 // InputDispatcher expects a display-to-input transform.
2188 info.transform = inputToDisplay.inverse();
2189
2190 // The touchable region is specified in the input coordinate space. Change it to display space.
Prabir Pradhan33da9462022-06-14 14:55:57 +00002191 info.touchableRegion =
2192 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, mName);
chaviw7e72caf2020-12-02 16:50:43 -08002193}
2194
chaviw3277faf2021-05-19 16:45:23 -05002195void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002196 sp<Layer> p = sp<Layer>::fromExisting(this);
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002197 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002198 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002199 }
2200 if (p != nullptr) {
2201 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2202 }
2203}
2204
Vishnu Naira066d902021-09-13 18:40:17 -07002205gui::DropInputMode Layer::getDropInputMode() const {
2206 gui::DropInputMode mode = mDrawingState.dropInputMode;
2207 if (mode == gui::DropInputMode::ALL) {
2208 return mode;
2209 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002210 sp<Layer> parent = mDrawingParent.promote();
2211 if (parent) {
2212 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002213 if (parentMode != gui::DropInputMode::NONE) {
2214 return parentMode;
2215 }
2216 }
2217 return mode;
2218}
2219
2220void Layer::handleDropInputMode(gui::WindowInfo& info) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002221 if (mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Vishnu Naira066d902021-09-13 18:40:17 -07002222 return;
2223 }
2224
2225 // Check if we need to drop input unconditionally
2226 gui::DropInputMode dropInputMode = getDropInputMode();
2227 if (dropInputMode == gui::DropInputMode::ALL) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002228 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002229 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2230 return;
2231 }
2232
2233 // Check if we need to check if the window is obscured by parent
2234 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2235 return;
2236 }
2237
2238 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002239 sp<Layer> parent = mDrawingParent.promote();
2240 if (parent && parent->getAlpha() != 1.0_hf) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002241 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002242 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2243 static_cast<float>(getAlpha()));
2244 }
2245
2246 // Check if the parent has cropped the buffer
2247 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2248 if (!bufferSize.isValid()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002249 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002250 return;
2251 }
2252
2253 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2254 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2255 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2256 // match then the layer has not been cropped by its parents.
2257 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2258 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2259
2260 if (croppedByParent) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002261 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002262 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2263 getDebugName());
2264 } else {
2265 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2266 // input if the window is obscured. This check should be done in surfaceflinger but the
2267 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2268 // drop input events if the window is obscured.
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002269 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002270 }
2271}
2272
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002273WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) {
chaviw7e72caf2020-12-02 16:50:43 -08002274 if (!hasInputInfo()) {
2275 mDrawingState.inputInfo.name = getName();
2276 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2277 mDrawingState.inputInfo.ownerPid = mOwnerPid;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002278 mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002279 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002280 }
2281
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002282 const ui::Transform& displayTransform =
2283 displayArgs.transform != nullptr ? *displayArgs.transform : kIdentityTransform;
2284
chaviw3277faf2021-05-19 16:45:23 -05002285 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002286 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002287 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002288
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002289 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002290
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002291 if (displayArgs.transform == nullptr) {
2292 // Do not let the window receive touches if it is not associated with a valid display
2293 // transform. We still allow the window to receive keys and prevent ANRs.
2294 info.inputConfig |= WindowInfo::InputConfig::NOT_TOUCHABLE;
2295 }
2296
Robert Carr5dc426e2020-06-10 14:29:14 -07002297 // For compatibility reasons we let layers which can receive input
2298 // receive input before they have actually submitted a buffer. Because
2299 // of this we use canReceiveInput instead of isVisible to check the
2300 // policy-visibility, ignoring the buffer state. However for layers with
2301 // hasInputInfo()==false we can use the real visibility state.
2302 // We are just using these layers for occlusion detection in
2303 // InputDispatcher, and obviously if they aren't visible they can't occlude
2304 // anything.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002305 const bool visible = hasInputInfo() ? canReceiveInput() : isVisible();
2306 info.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
2307
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002308 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002309 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002310 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002311
Vishnu Nair16a938f2021-09-24 07:14:54 -07002312 // If the window will be blacked out on a display because the display does not have the secure
2313 // flag and the layer has the secure flag set, then drop input.
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002314 if (!displayArgs.isSecure && isSecure()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002315 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Nair16a938f2021-09-24 07:14:54 -07002316 }
2317
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002318 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2319 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002320 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2321 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002322 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002323 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002324 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002325 }
2326
Winson Chunga30f7c92021-06-29 15:42:56 -07002327 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2328 // if it was set by WM for a known system overlay
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002329 if (isTrustedOverlay()) {
2330 info.inputConfig |= WindowInfo::InputConfig::TRUSTED_OVERLAY;
2331 }
Winson Chunga30f7c92021-06-29 15:42:56 -07002332
chaviwaf87b3e2019-10-01 16:59:28 -07002333 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2334 // touches from going outside the cloned area.
2335 if (isClone()) {
chaviw8577ea82022-06-01 16:32:26 -05002336 info.inputConfig |= WindowInfo::InputConfig::CLONE;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002337 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2338 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002339 info.touchableRegion = info.touchableRegion.intersect(rect);
2340 }
2341 }
2342
Robert Carr720e5062018-07-30 17:45:14 -07002343 return info;
2344}
2345
chaviwaf87b3e2019-10-01 16:59:28 -07002346sp<Layer> Layer::getClonedRoot() {
2347 if (mClonedChild != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002348 return sp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002349 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002350 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002351 return nullptr;
2352 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002353 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002354}
2355
Robert Carredd13602020-04-13 17:24:34 -07002356bool Layer::hasInputInfo() const {
Linus Tufvessona1858822022-03-04 09:32:07 +00002357 return mDrawingState.inputInfo.token != nullptr ||
2358 mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Robert Carr720e5062018-07-30 17:45:14 -07002359}
2360
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002361compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002362 const DisplayDevice* display) const {
2363 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002364 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002365}
2366
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002367Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2368 const auto outputLayer = findOutputLayerForDisplay(display);
2369 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002370}
2371
chaviwb4c6e582019-08-16 14:35:07 -07002372void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002373 cloneDrawingState(clonedFrom.get());
chaviwb4c6e582019-08-16 14:35:07 -07002374 mClonedFrom = clonedFrom;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002375 mPremultipliedAlpha = clonedFrom->mPremultipliedAlpha;
2376 mPotentialCursor = clonedFrom->mPotentialCursor;
2377 mProtectedByApp = clonedFrom->mProtectedByApp;
2378 updateCloneBufferInfo();
2379}
2380
2381void Layer::updateCloneBufferInfo() {
2382 if (!isClone() || !isClonedFromAlive()) {
2383 return;
2384 }
2385
2386 sp<Layer> clonedFrom = getClonedFrom();
2387 mBufferInfo = clonedFrom->mBufferInfo;
2388 mSidebandStream = clonedFrom->mSidebandStream;
2389 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
2390 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
2391 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
2392
2393 // After buffer info is updated, the drawingState from the real layer needs to be copied into
2394 // the cloned. This is because some properties of drawingState can change when latchBuffer is
2395 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
2396 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
2397 // the cloned drawingState again.
2398 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
2399 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
2400 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
2401 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
2402
2403 cloneDrawingState(clonedFrom.get());
2404
2405 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
2406 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
2407 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
2408 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -07002409}
chaviw74b03172019-08-19 11:09:03 -07002410
2411void Layer::updateMirrorInfo() {
2412 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2413 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2414 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2415 // that case, we want to delete the reference to the clone since we want it to get
2416 // destroyed. The root, this layer, will still be around since the client can continue
2417 // to hold a reference, but no cloned layers will be displayed.
2418 mClonedChild = nullptr;
2419 return;
2420 }
2421
2422 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2423 // If the real layer exists and is in current state, add the clone as a child of the root.
2424 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2425 // copied to drawingState for the root layer. So the clonedChild is always removed from
2426 // drawingState and then needs to be added back each traversal.
2427 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2428 addChildToDrawing(mClonedChild);
2429 }
2430
2431 mClonedChild->updateClonedDrawingState(clonedLayersMap);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002432 mClonedChild->updateClonedChildren(sp<Layer>::fromExisting(this), clonedLayersMap);
chaviw74b03172019-08-19 11:09:03 -07002433 mClonedChild->updateClonedRelatives(clonedLayersMap);
2434}
2435
2436void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2437 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2438 // to the clone. If the real layer is no longer alive, continue traversing the children
2439 // since we may be able to pull out other children that are still alive.
2440 if (isClonedFromAlive()) {
2441 sp<Layer> clonedFrom = getClonedFrom();
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002442 cloneDrawingState(clonedFrom.get());
Ady Abrahamd11bade2022-08-01 16:18:03 -07002443 clonedLayersMap.emplace(clonedFrom, sp<Layer>::fromExisting(this));
chaviw74b03172019-08-19 11:09:03 -07002444 }
2445
2446 // The clone layer may have children in drawingState since they may have been created and
2447 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2448 // that already exist, since we can just re-use them.
2449 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2450 // not updated in the regular traversal. They are skipped since the root will lose the
2451 // reference to them when it copies its currentChildren to drawing.
2452 for (sp<Layer>& child : mDrawingChildren) {
2453 child->updateClonedDrawingState(clonedLayersMap);
2454 }
2455}
2456
2457void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2458 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2459 mDrawingChildren.clear();
2460
2461 if (!isClonedFromAlive()) {
2462 return;
2463 }
2464
2465 sp<Layer> clonedFrom = getClonedFrom();
2466 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2467 if (child == mirrorRoot) {
2468 // This is to avoid cyclical mirroring.
2469 continue;
2470 }
2471 sp<Layer> clonedChild = clonedLayersMap[child];
2472 if (clonedChild == nullptr) {
2473 clonedChild = child->createClone();
2474 clonedLayersMap[child] = clonedChild;
2475 }
2476 addChildToDrawing(clonedChild);
2477 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2478 }
2479}
2480
chaviwaf87b3e2019-10-01 16:59:28 -07002481void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2482 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2483 if (cropLayer != nullptr) {
2484 if (clonedLayersMap.count(cropLayer) == 0) {
2485 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2486 // self as crop layer to avoid going outside bounds.
Ady Abrahamd11bade2022-08-01 16:18:03 -07002487 mDrawingState.touchableRegionCrop = wp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002488 } else {
2489 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2490 mDrawingState.touchableRegionCrop = clonedCropLayer;
2491 }
2492 }
2493 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2494 // WM or the client.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002495 mDrawingState.inputInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, false);
chaviwaf87b3e2019-10-01 16:59:28 -07002496}
2497
2498void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002499 mDrawingState.zOrderRelativeOf = wp<Layer>();
chaviw74b03172019-08-19 11:09:03 -07002500 mDrawingState.zOrderRelatives.clear();
2501
2502 if (!isClonedFromAlive()) {
2503 return;
2504 }
2505
chaviwaf87b3e2019-10-01 16:59:28 -07002506 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002507 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002508 const sp<Layer>& relative = relativeWeak.promote();
2509 if (clonedLayersMap.count(relative) > 0) {
2510 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002511 mDrawingState.zOrderRelatives.add(clonedRelative);
2512 }
2513 }
2514
2515 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2516 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2517 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2518 // still traverse the children, but the layer with the missing relativeOf will not be shown
2519 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002520 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2521 if (clonedLayersMap.count(relativeOf) > 0) {
2522 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002523 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2524 }
2525
chaviwaf87b3e2019-10-01 16:59:28 -07002526 updateClonedInputInfo(clonedLayersMap);
2527
chaviw74b03172019-08-19 11:09:03 -07002528 for (sp<Layer>& child : mDrawingChildren) {
2529 child->updateClonedRelatives(clonedLayersMap);
2530 }
2531}
2532
2533void Layer::addChildToDrawing(const sp<Layer>& layer) {
2534 mDrawingChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002535 layer->mDrawingParent = sp<Layer>::fromExisting(this);
chaviw74b03172019-08-19 11:09:03 -07002536}
2537
Steven Thomas62a4cf82020-01-31 12:04:03 -08002538Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2539 switch (compatibility) {
2540 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2541 return FrameRateCompatibility::Default;
2542 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2543 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002544 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2545 return FrameRateCompatibility::Exact;
Andy Labrada096227e2022-06-15 16:58:11 +00002546 case ANATIVEWINDOW_FRAME_RATE_MIN:
2547 return FrameRateCompatibility::Min;
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002548 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
2549 return FrameRateCompatibility::NoVote;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002550 default:
2551 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2552 return FrameRateCompatibility::Default;
2553 }
2554}
2555
Marin Shalamanovc5986772021-03-16 16:09:49 +01002556scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2557 switch (strategy) {
2558 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2559 return Seamlessness::OnlySeamless;
2560 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2561 return Seamlessness::SeamedAndSeamless;
2562 default:
2563 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2564 return Seamlessness::Default;
2565 }
2566}
2567
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002568bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002569 const State& s(mDrawingState);
2570 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2571 return true;
2572 }
2573
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002574 sp<Layer> parent = mDrawingParent.promote();
2575 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002576}
2577
Robert Carr6a0382d2021-07-01 15:57:17 -07002578void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2579 mClonedChild = clonedChild;
2580 mHadClonedChild = true;
2581 mFlinger->mNumClones++;
2582}
2583
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002584bool Layer::setDropInputMode(gui::DropInputMode mode) {
2585 if (mDrawingState.dropInputMode == mode) {
2586 return false;
2587 }
2588 mDrawingState.dropInputMode = mode;
2589 return true;
2590}
2591
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002592void Layer::cloneDrawingState(const Layer* from) {
2593 mDrawingState = from->mDrawingState;
2594 // Skip callback info since they are not applicable for cloned layers.
2595 mDrawingState.releaseBufferListener = nullptr;
2596 mDrawingState.callbackHandles = {};
2597}
2598
Patrick Williamsbb25f802022-08-30 23:02:34 +00002599void Layer::callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
2600 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
2601 const sp<Fence>& releaseFence,
2602 uint32_t currentMaxAcquiredBufferCount) {
2603 if (!listener) {
2604 return;
2605 }
2606 ATRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, getDebugName(), framenumber);
Huihong Luoc50b4982021-11-24 12:34:52 -08002607 std::optional<os::ParcelFileDescriptor> fenceFd;
2608 if (releaseFence) {
2609 fenceFd = os::ParcelFileDescriptor(base::unique_fd(::dup(releaseFence->get())));
2610 }
2611 listener->onReleaseBuffer({buffer->getId(), framenumber}, fenceFd,
2612 static_cast<int32_t>(currentMaxAcquiredBufferCount));
Patrick Williamsbb25f802022-08-30 23:02:34 +00002613}
2614
2615void Layer::onLayerDisplayed(ftl::SharedFuture<FenceResult> futureFenceResult) {
2616 // If we are displayed on multiple displays in a single composition cycle then we would
2617 // need to do careful tracking to enable the use of the mLastClientCompositionFence.
2618 // For example we can only use it if all the displays are client comp, and we need
2619 // to merge all the client comp fences. We could do this, but for now we just
2620 // disable the optimization when a layer is composed on multiple displays.
2621 if (mClearClientCompositionFenceOnLayerDisplayed) {
2622 mLastClientCompositionFence = nullptr;
2623 } else {
2624 mClearClientCompositionFenceOnLayerDisplayed = true;
2625 }
2626
2627 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
2628 // buffer that was presented on this layer. The first transaction that came in this frame that
2629 // replaced the previous buffer on this layer needs this release fence, because the fence will
2630 // let the client know when that previous buffer is removed from the screen.
2631 //
2632 // Every other transaction on this layer does not need a release fence because no other
2633 // Transactions that were set on this layer this frame are going to have their preceding buffer
2634 // removed from the display this frame.
2635 //
2636 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
2637 // buffer so it doesn't need a previous release fence because the layer still needs the previous
2638 // buffer. The second transaction contains a buffer so it needs a previous release fence because
2639 // the previous buffer will be released this frame. The third transaction also contains a
2640 // buffer. It replaces the buffer in the second transaction. The buffer in the second
2641 // transaction will now no longer be presented so it is released immediately and the third
2642 // transaction doesn't need a previous release fence.
2643 sp<CallbackHandle> ch;
2644 for (auto& handle : mDrawingState.callbackHandles) {
2645 if (handle->releasePreviousBuffer &&
2646 mDrawingState.releaseBufferEndpoint == handle->listener) {
2647 ch = handle;
2648 break;
2649 }
2650 }
2651
2652 // Prevent tracing the same release multiple times.
2653 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
2654 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
2655 }
2656
2657 if (ch != nullptr) {
2658 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2659 ch->previousReleaseFences.emplace_back(std::move(futureFenceResult));
2660 ch->name = mName;
2661 }
2662}
2663
2664void Layer::onSurfaceFrameCreated(
2665 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Vishnu Nair22491b82022-10-18 14:59:14 -07002666 if (!hasBufferOrSidebandStreamInDrawing()) {
2667 return;
2668 }
2669
Patrick Williamsbb25f802022-08-30 23:02:34 +00002670 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
2671 // Too many SurfaceFrames pending classification. The front of the deque is probably not
2672 // tracked by FrameTimeline and will never be presented. This will only result in a memory
2673 // leak.
2674 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
2675 mName.c_str());
2676 std::string miniDump = mPendingJankClassifications.front()->miniDump();
2677 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
2678 mPendingJankClassifications.pop_front();
2679 }
2680 mPendingJankClassifications.emplace_back(surfaceFrame);
2681}
2682
2683void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
2684 for (const auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair71fcf912022-10-18 09:14:20 -07002685 handle->transformHint = mSkipReportingTransformHint
2686 ? std::nullopt
2687 : std::make_optional<uint32_t>(mTransformHint);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002688 handle->dequeueReadyTime = dequeueReadyTime;
2689 handle->currentMaxAcquiredBufferCount =
2690 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
2691 ATRACE_FORMAT_INSTANT("releasePendingBuffer %s - %" PRIu64, getDebugName(),
2692 handle->previousReleaseCallbackId.framenumber);
2693 }
2694
2695 for (auto& handle : mDrawingState.callbackHandles) {
2696 if (handle->releasePreviousBuffer &&
2697 mDrawingState.releaseBufferEndpoint == handle->listener) {
2698 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2699 break;
2700 }
2701 }
2702
2703 std::vector<JankData> jankData;
2704 jankData.reserve(mPendingJankClassifications.size());
2705 while (!mPendingJankClassifications.empty() &&
2706 mPendingJankClassifications.front()->getJankType()) {
2707 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
2708 mPendingJankClassifications.front();
2709 mPendingJankClassifications.pop_front();
2710 jankData.emplace_back(
2711 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
2712 }
2713
2714 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(mDrawingState.callbackHandles,
2715 jankData);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002716 mDrawingState.callbackHandles = {};
2717}
2718
2719bool Layer::willPresentCurrentTransaction() const {
2720 // Returns true if the most recent Transaction applied to CurrentState will be presented.
2721 return (getSidebandStreamChanged() || getAutoRefresh() ||
2722 (mDrawingState.modified &&
2723 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
2724}
2725
2726bool Layer::setTransform(uint32_t transform) {
2727 if (mDrawingState.bufferTransform == transform) return false;
2728 mDrawingState.bufferTransform = transform;
2729 mDrawingState.modified = true;
2730 setTransactionFlags(eTransactionNeeded);
2731 return true;
2732}
2733
2734bool Layer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
2735 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
2736 mDrawingState.sequence++;
2737 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
2738 mDrawingState.modified = true;
2739 setTransactionFlags(eTransactionNeeded);
2740 return true;
2741}
2742
2743bool Layer::setBufferCrop(const Rect& bufferCrop) {
2744 if (mDrawingState.bufferCrop == bufferCrop) return false;
2745
2746 mDrawingState.sequence++;
2747 mDrawingState.bufferCrop = bufferCrop;
2748
2749 mDrawingState.modified = true;
2750 setTransactionFlags(eTransactionNeeded);
2751 return true;
2752}
2753
2754bool Layer::setDestinationFrame(const Rect& destinationFrame) {
2755 if (mDrawingState.destinationFrame == destinationFrame) return false;
2756
2757 mDrawingState.sequence++;
2758 mDrawingState.destinationFrame = destinationFrame;
2759
2760 mDrawingState.modified = true;
2761 setTransactionFlags(eTransactionNeeded);
2762 return true;
2763}
2764
2765// Translate destination frame into scale and position. If a destination frame is not set, use the
2766// provided scale and position
2767bool Layer::updateGeometry() {
2768 if ((mDrawingState.flags & layer_state_t::eIgnoreDestinationFrame) ||
2769 mDrawingState.destinationFrame.isEmpty()) {
2770 // If destination frame is not set, use the requested transform set via
2771 // Layer::setPosition and Layer::setMatrix.
2772 return assignTransform(&mDrawingState.transform, mRequestedTransform);
2773 }
2774
2775 Rect destRect = mDrawingState.destinationFrame;
2776 int32_t destW = destRect.width();
2777 int32_t destH = destRect.height();
2778 if (destRect.left < 0) {
2779 destRect.left = 0;
2780 destRect.right = destW;
2781 }
2782 if (destRect.top < 0) {
2783 destRect.top = 0;
2784 destRect.bottom = destH;
2785 }
2786
2787 if (!mDrawingState.buffer) {
2788 ui::Transform t;
2789 t.set(destRect.left, destRect.top);
2790 return assignTransform(&mDrawingState.transform, t);
2791 }
2792
2793 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
2794 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
2795 // Undo any transformations on the buffer.
2796 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
2797 std::swap(bufferWidth, bufferHeight);
2798 }
2799 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
2800 if (mDrawingState.transformToDisplayInverse) {
2801 if (invTransform & ui::Transform::ROT_90) {
2802 std::swap(bufferWidth, bufferHeight);
2803 }
2804 }
2805
2806 float sx = destW / static_cast<float>(bufferWidth);
2807 float sy = destH / static_cast<float>(bufferHeight);
2808 ui::Transform t;
2809 t.set(sx, 0, 0, sy);
2810 t.set(destRect.left, destRect.top);
2811 return assignTransform(&mDrawingState.transform, t);
2812}
2813
2814bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
2815 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
2816 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
2817 return false;
2818 }
2819
2820 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
2821
2822 mDrawingState.sequence++;
2823 mDrawingState.modified = true;
2824 setTransactionFlags(eTransactionNeeded);
2825
2826 return true;
2827}
2828
2829bool Layer::setPosition(float x, float y) {
2830 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
2831 return false;
2832 }
2833
2834 mRequestedTransform.set(x, y);
2835
2836 mDrawingState.sequence++;
2837 mDrawingState.modified = true;
2838 setTransactionFlags(eTransactionNeeded);
2839
2840 return true;
2841}
2842
2843bool Layer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
2844 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
2845 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
Vishnu Nairc6086dd2022-11-04 04:39:35 +00002846 const FrameTimelineInfo& info, int hwcBufferSlot) {
Chavi Weingarten009619f2022-09-15 20:27:25 +00002847 ATRACE_FORMAT("setBuffer %s - hasBuffer=%s", getDebugName(), (buffer ? "true" : "false"));
Patrick Williamsbb25f802022-08-30 23:02:34 +00002848 if (!buffer) {
2849 return false;
2850 }
2851
2852 const bool frameNumberChanged =
2853 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
2854 const uint64_t frameNumber =
2855 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
Chavi Weingarten009619f2022-09-15 20:27:25 +00002856 ATRACE_FORMAT_INSTANT("setBuffer %s - %" PRIu64, getDebugName(), frameNumber);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002857
2858 if (mDrawingState.buffer) {
2859 mReleasePreviousBuffer = true;
2860 if (!mBufferInfo.mBuffer ||
2861 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
2862 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
2863 // If mDrawingState has a buffer, and we are about to update again
2864 // before swapping to drawing state, then the first buffer will be
2865 // dropped and we should decrement the pending buffer count and
2866 // call any release buffer callbacks if set.
2867 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2868 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2869 mDrawingState.acquireFence,
2870 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2871 mOwnerUid));
2872 decrementPendingBufferCount();
2873 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
2874 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
2875 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
2876 mDrawingState.bufferSurfaceFrameTX.reset();
2877 }
2878 } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) {
2879 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2880 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2881 mLastClientCompositionFence,
2882 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2883 mOwnerUid));
2884 mLastClientCompositionFence = nullptr;
2885 }
2886 }
2887
2888 mDrawingState.frameNumber = frameNumber;
2889 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
2890 mDrawingState.buffer = std::move(buffer);
2891 mDrawingState.clientCacheId = bufferData.cachedBuffer;
Vishnu Nairc6086dd2022-11-04 04:39:35 +00002892 mDrawingState.hwcBufferSlot = hwcBufferSlot;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002893 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
2894 ? bufferData.acquireFence
2895 : Fence::NO_FENCE;
2896 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
2897 if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
2898 // We latched this buffer unsiganled, so we need to pass the acquire fence
2899 // on the callback instead of just the acquire time, since it's unknown at
2900 // this point.
2901 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence;
2902 } else {
2903 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime();
2904 }
2905
2906 mDrawingState.modified = true;
2907 setTransactionFlags(eTransactionNeeded);
2908
2909 const int32_t layerId = getSequence();
2910 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
2911 mOwnerUid, postTime, getGameMode());
2912 mDrawingState.desiredPresentTime = desiredPresentTime;
2913 mDrawingState.isAutoTimestamp = isAutoTimestamp;
2914
2915 const nsecs_t presentTime = [&] {
2916 if (!isAutoTimestamp) return desiredPresentTime;
2917
2918 const auto prediction =
2919 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
2920 if (prediction.has_value()) return prediction->presentTime;
2921
2922 return static_cast<nsecs_t>(0);
2923 }();
2924
2925 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
2926 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
2927
2928 setFrameTimelineVsyncForBufferTransaction(info, postTime);
2929
2930 if (dequeueTime && *dequeueTime != 0) {
2931 const uint64_t bufferId = mDrawingState.buffer->getId();
2932 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
2933 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
2934 FrameTracer::FrameEvent::DEQUEUE);
2935 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
2936 FrameTracer::FrameEvent::QUEUE);
2937 }
2938
2939 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
2940 return true;
2941}
2942
2943bool Layer::setDataspace(ui::Dataspace dataspace) {
2944 mDrawingState.dataspaceRequested = true;
2945 if (mDrawingState.dataspace == dataspace) return false;
2946 mDrawingState.dataspace = dataspace;
2947 mDrawingState.modified = true;
2948 setTransactionFlags(eTransactionNeeded);
2949 return true;
2950}
2951
2952bool Layer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
2953 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
2954 mDrawingState.hdrMetadata = hdrMetadata;
2955 mDrawingState.modified = true;
2956 setTransactionFlags(eTransactionNeeded);
2957 return true;
2958}
2959
2960bool Layer::setSurfaceDamageRegion(const Region& surfaceDamage) {
2961 mDrawingState.surfaceDamageRegion = surfaceDamage;
2962 mDrawingState.modified = true;
2963 setTransactionFlags(eTransactionNeeded);
2964 return true;
2965}
2966
2967bool Layer::setApi(int32_t api) {
2968 if (mDrawingState.api == api) return false;
2969 mDrawingState.api = api;
2970 mDrawingState.modified = true;
2971 setTransactionFlags(eTransactionNeeded);
2972 return true;
2973}
2974
2975bool Layer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
2976 if (mDrawingState.sidebandStream == sidebandStream) return false;
2977
2978 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
2979 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
2980 } else if (sidebandStream != nullptr) {
2981 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
2982 }
2983
2984 mDrawingState.sidebandStream = sidebandStream;
2985 mDrawingState.modified = true;
2986 setTransactionFlags(eTransactionNeeded);
2987 if (!mSidebandStreamChanged.exchange(true)) {
2988 // mSidebandStreamChanged was false
2989 mFlinger->onLayerUpdate();
2990 }
2991 return true;
2992}
2993
2994bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
2995 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
2996 if (handles.empty()) {
2997 mReleasePreviousBuffer = false;
2998 return false;
2999 }
3000
3001 const bool willPresent = willPresentCurrentTransaction();
3002
3003 for (const auto& handle : handles) {
3004 // If this transaction set a buffer on this layer, release its previous buffer
3005 handle->releasePreviousBuffer = mReleasePreviousBuffer;
3006
3007 // If this layer will be presented in this frame
3008 if (willPresent) {
3009 // If this transaction set an acquire fence on this layer, set its acquire time
3010 handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence;
3011 handle->frameNumber = mDrawingState.frameNumber;
3012
3013 // Store so latched time and release fence can be set
3014 mDrawingState.callbackHandles.push_back(handle);
3015
3016 } else { // If this layer will NOT need to be relatched and presented this frame
3017 // Notify the transaction completed thread this handle is done
3018 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
3019 }
3020 }
3021
3022 mReleasePreviousBuffer = false;
3023 mCallbackHandleAcquireTimeOrFence = -1;
3024
3025 return willPresent;
3026}
3027
3028Rect Layer::getBufferSize(const State& /*s*/) const {
3029 // for buffer state layers we use the display frame size as the buffer size.
3030
3031 if (mBufferInfo.mBuffer == nullptr) {
3032 return Rect::INVALID_RECT;
3033 }
3034
3035 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3036 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3037
3038 // Undo any transformations on the buffer and return the result.
3039 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
3040 std::swap(bufWidth, bufHeight);
3041 }
3042
3043 if (getTransformToDisplayInverse()) {
3044 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3045 if (invTransform & ui::Transform::ROT_90) {
3046 std::swap(bufWidth, bufHeight);
3047 }
3048 }
3049
3050 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
3051}
3052
3053FloatRect Layer::computeSourceBounds(const FloatRect& parentBounds) const {
3054 if (mBufferInfo.mBuffer == nullptr) {
3055 return parentBounds;
3056 }
3057
3058 return getBufferSize(getDrawingState()).toFloatRect();
3059}
3060
3061bool Layer::fenceHasSignaled() const {
3062 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
3063 return true;
3064 }
3065
3066 const bool fenceSignaled =
3067 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
3068 if (!fenceSignaled) {
3069 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
3070 TimeStats::LatchSkipReason::LateAcquire);
3071 }
3072
3073 return fenceSignaled;
3074}
3075
Vishnu Naire14c6b32022-08-06 04:20:15 +00003076bool Layer::onPreComposition(nsecs_t refreshStartTime) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003077 for (const auto& handle : mDrawingState.callbackHandles) {
3078 handle->refreshStartTime = refreshStartTime;
3079 }
3080 return hasReadyFrame();
3081}
3082
3083void Layer::setAutoRefresh(bool autoRefresh) {
3084 mDrawingState.autoRefresh = autoRefresh;
3085}
3086
3087bool Layer::latchSidebandStream(bool& recomputeVisibleRegions) {
3088 // 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 +00003089 auto* snapshot = editLayerSnapshot();
3090 snapshot->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003091
3092 if (mSidebandStreamChanged.exchange(false)) {
3093 const State& s(getDrawingState());
3094 // mSidebandStreamChanged was true
3095 mSidebandStream = s.sidebandStream;
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003096 snapshot->sidebandStream = mSidebandStream;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003097 if (mSidebandStream != nullptr) {
3098 setTransactionFlags(eTransactionNeeded);
3099 mFlinger->setTransactionFlags(eTraversalNeeded);
3100 }
3101 recomputeVisibleRegions = true;
3102
3103 return true;
3104 }
3105 return false;
3106}
3107
3108bool Layer::hasFrameUpdate() const {
3109 const State& c(getDrawingState());
3110 return (mDrawingStateModified || mDrawingState.modified) &&
3111 (c.buffer != nullptr || c.bgColorLayer != nullptr);
3112}
3113
3114void Layer::updateTexImage(nsecs_t latchTime) {
3115 const State& s(getDrawingState());
3116
3117 if (!s.buffer) {
3118 if (s.bgColorLayer) {
3119 for (auto& handle : mDrawingState.callbackHandles) {
3120 handle->latchTime = latchTime;
3121 }
3122 }
3123 return;
3124 }
3125
3126 for (auto& handle : mDrawingState.callbackHandles) {
3127 if (handle->frameNumber == mDrawingState.frameNumber) {
3128 handle->latchTime = latchTime;
3129 }
3130 }
3131
3132 const int32_t layerId = getSequence();
3133 const uint64_t bufferId = mDrawingState.buffer->getId();
3134 const uint64_t frameNumber = mDrawingState.frameNumber;
3135 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3136 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
3137 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
3138
3139 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
3140 FrameTracer::FrameEvent::ACQUIRE_FENCE);
3141 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
3142 FrameTracer::FrameEvent::LATCH);
3143
3144 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
3145 if (bufferSurfaceFrame != nullptr &&
3146 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
3147 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
3148 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
3149 // are processing the next state.
3150 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
3151 mDrawingState.acquireFenceTime->getSignalTime(),
3152 latchTime);
3153 mDrawingState.bufferSurfaceFrameTX.reset();
3154 }
3155
3156 std::deque<sp<CallbackHandle>> remainingHandles;
3157 mFlinger->getTransactionCallbackInvoker()
3158 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
3159 mDrawingState.callbackHandles = remainingHandles;
3160
3161 mDrawingStateModified = false;
3162}
3163
3164void Layer::gatherBufferInfo() {
3165 if (!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
3166 decrementPendingBufferCount();
3167 }
3168
3169 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
3170 mBufferInfo.mBuffer = mDrawingState.buffer;
3171 mBufferInfo.mFence = mDrawingState.acquireFence;
3172 mBufferInfo.mFrameNumber = mDrawingState.frameNumber;
3173 mBufferInfo.mPixelFormat =
3174 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat();
3175 mBufferInfo.mFrameLatencyNeeded = true;
3176 mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime;
3177 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3178 mBufferInfo.mFence = mDrawingState.acquireFence;
3179 mBufferInfo.mTransform = mDrawingState.bufferTransform;
3180 auto lastDataspace = mBufferInfo.mDataspace;
3181 mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace);
3182 if (lastDataspace != mBufferInfo.mDataspace) {
3183 mFlinger->mSomeDataspaceChanged = true;
3184 }
3185 mBufferInfo.mCrop = computeBufferCrop(mDrawingState);
3186 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
3187 mBufferInfo.mSurfaceDamage = mDrawingState.surfaceDamageRegion;
3188 mBufferInfo.mHdrMetadata = mDrawingState.hdrMetadata;
3189 mBufferInfo.mApi = mDrawingState.api;
3190 mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse;
Vishnu Nairc6086dd2022-11-04 04:39:35 +00003191 mBufferInfo.mBufferSlot = mDrawingState.hwcBufferSlot;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003192}
3193
3194Rect Layer::computeBufferCrop(const State& s) {
3195 if (s.buffer && !s.bufferCrop.isEmpty()) {
3196 Rect bufferCrop;
3197 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
3198 return bufferCrop;
3199 } else if (s.buffer) {
3200 return s.buffer->getBounds();
3201 } else {
3202 return s.bufferCrop;
3203 }
3204}
3205
3206sp<Layer> Layer::createClone() {
3207 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
3208 args.textureName = mTextureName;
Patrick Williams83f36b22022-09-14 17:57:35 +00003209 sp<Layer> layer = mFlinger->getFactory().createBufferStateLayer(args);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003210 layer->setInitialValuesForClone(sp<Layer>::fromExisting(this));
3211 return layer;
3212}
3213
3214bool Layer::bufferNeedsFiltering() const {
3215 const State& s(getDrawingState());
3216 if (!s.buffer) {
3217 return false;
3218 }
3219
3220 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
3221 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
3222
3223 // Undo any transformations on the buffer and return the result.
3224 if (s.bufferTransform & ui::Transform::ROT_90) {
3225 std::swap(bufferWidth, bufferHeight);
3226 }
3227
3228 if (s.transformToDisplayInverse) {
3229 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3230 if (invTransform & ui::Transform::ROT_90) {
3231 std::swap(bufferWidth, bufferHeight);
3232 }
3233 }
3234
3235 const Rect layerSize{getBounds()};
3236 int32_t layerWidth = layerSize.getWidth();
3237 int32_t layerHeight = layerSize.getHeight();
3238
3239 // Align the layer orientation with the buffer before comparism
3240 if (mTransformHint & ui::Transform::ROT_90) {
3241 std::swap(layerWidth, layerHeight);
3242 }
3243
3244 return layerWidth != bufferWidth || layerHeight != bufferHeight;
3245}
3246
3247void Layer::decrementPendingBufferCount() {
3248 int32_t pendingBuffers = --mPendingBufferTransactions;
3249 tracePendingBufferCount(pendingBuffers);
3250}
3251
3252void Layer::tracePendingBufferCount(int32_t pendingBuffers) {
3253 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
3254}
3255
3256/*
3257 * We don't want to send the layer's transform to input, but rather the
3258 * parent's transform. This is because Layer's transform is
3259 * information about how the buffer is placed on screen. The parent's
3260 * transform makes more sense to send since it's information about how the
3261 * layer is placed on screen. This transform is used by input to determine
3262 * how to go from screen space back to window space.
3263 */
3264ui::Transform Layer::getInputTransform() const {
3265 if (!hasBufferOrSidebandStream()) {
3266 return getTransform();
3267 }
3268 sp<Layer> parent = mDrawingParent.promote();
3269 if (parent == nullptr) {
3270 return ui::Transform();
3271 }
3272
3273 return parent->getTransform();
3274}
3275
3276/**
3277 * Similar to getInputTransform, we need to update the bounds to include the transform.
3278 * This is because bounds don't include the buffer transform, where the input assumes
3279 * that's already included.
3280 */
3281Rect Layer::getInputBounds() const {
3282 if (!hasBufferOrSidebandStream()) {
3283 return getCroppedBufferSize(getDrawingState());
3284 }
3285
3286 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
3287 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
3288 return bufferBounds;
3289 }
3290 return mDrawingState.transform.transform(bufferBounds);
3291}
3292
3293bool Layer::simpleBufferUpdate(const layer_state_t& s) const {
3294 const uint64_t requiredFlags = layer_state_t::eBufferChanged;
3295
3296 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
3297 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
3298 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
3299 layer_state_t::eLayerStackChanged | layer_state_t::eAutoRefreshChanged |
3300 layer_state_t::eReparent;
3301
3302 const uint64_t allowedFlags = layer_state_t::eHasListenerCallbacksChanged |
3303 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFrameRateChanged |
3304 layer_state_t::eSurfaceDamageRegionChanged | layer_state_t::eApiChanged |
3305 layer_state_t::eMetadataChanged | layer_state_t::eDropInputModeChanged |
3306 layer_state_t::eInputInfoChanged;
3307
3308 if ((s.what & requiredFlags) != requiredFlags) {
3309 ALOGV("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
3310 (s.what | requiredFlags) & ~s.what);
3311 return false;
3312 }
3313
3314 if (s.what & deniedFlags) {
3315 ALOGV("%s: false [has denied flags 0x%" PRIx64 "]", __func__, s.what & deniedFlags);
3316 return false;
3317 }
3318
3319 if (s.what & allowedFlags) {
3320 ALOGV("%s: [has allowed flags 0x%" PRIx64 "]", __func__, s.what & allowedFlags);
3321 }
3322
3323 if (s.what & layer_state_t::ePositionChanged) {
3324 if (mRequestedTransform.tx() != s.x || mRequestedTransform.ty() != s.y) {
3325 ALOGV("%s: false [ePositionChanged changed]", __func__);
3326 return false;
3327 }
3328 }
3329
3330 if (s.what & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003331 if (mDrawingState.color.a != s.color.a) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003332 ALOGV("%s: false [eAlphaChanged changed]", __func__);
3333 return false;
3334 }
3335 }
3336
3337 if (s.what & layer_state_t::eColorTransformChanged) {
3338 if (mDrawingState.colorTransform != s.colorTransform) {
3339 ALOGV("%s: false [eColorTransformChanged changed]", __func__);
3340 return false;
3341 }
3342 }
3343
3344 if (s.what & layer_state_t::eBackgroundColorChanged) {
3345 if (mDrawingState.bgColorLayer || s.bgColorAlpha != 0) {
3346 ALOGV("%s: false [eBackgroundColorChanged changed]", __func__);
3347 return false;
3348 }
3349 }
3350
3351 if (s.what & layer_state_t::eMatrixChanged) {
3352 if (mRequestedTransform.dsdx() != s.matrix.dsdx ||
3353 mRequestedTransform.dtdy() != s.matrix.dtdy ||
3354 mRequestedTransform.dtdx() != s.matrix.dtdx ||
3355 mRequestedTransform.dsdy() != s.matrix.dsdy) {
3356 ALOGV("%s: false [eMatrixChanged changed]", __func__);
3357 return false;
3358 }
3359 }
3360
3361 if (s.what & layer_state_t::eCornerRadiusChanged) {
3362 if (mDrawingState.cornerRadius != s.cornerRadius) {
3363 ALOGV("%s: false [eCornerRadiusChanged changed]", __func__);
3364 return false;
3365 }
3366 }
3367
3368 if (s.what & layer_state_t::eBackgroundBlurRadiusChanged) {
3369 if (mDrawingState.backgroundBlurRadius != static_cast<int>(s.backgroundBlurRadius)) {
3370 ALOGV("%s: false [eBackgroundBlurRadiusChanged changed]", __func__);
3371 return false;
3372 }
3373 }
3374
Vishnu Nairbbceb462022-10-10 04:52:13 +00003375 if (s.what & layer_state_t::eBufferTransformChanged) {
3376 if (mDrawingState.bufferTransform != s.bufferTransform) {
3377 ALOGV("%s: false [eBufferTransformChanged changed]", __func__);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003378 return false;
3379 }
3380 }
3381
3382 if (s.what & layer_state_t::eTransformToDisplayInverseChanged) {
3383 if (mDrawingState.transformToDisplayInverse != s.transformToDisplayInverse) {
3384 ALOGV("%s: false [eTransformToDisplayInverseChanged changed]", __func__);
3385 return false;
3386 }
3387 }
3388
3389 if (s.what & layer_state_t::eCropChanged) {
3390 if (mDrawingState.crop != s.crop) {
3391 ALOGV("%s: false [eCropChanged changed]", __func__);
3392 return false;
3393 }
3394 }
3395
3396 if (s.what & layer_state_t::eDataspaceChanged) {
3397 if (mDrawingState.dataspace != s.dataspace) {
3398 ALOGV("%s: false [eDataspaceChanged changed]", __func__);
3399 return false;
3400 }
3401 }
3402
3403 if (s.what & layer_state_t::eHdrMetadataChanged) {
3404 if (mDrawingState.hdrMetadata != s.hdrMetadata) {
3405 ALOGV("%s: false [eHdrMetadataChanged changed]", __func__);
3406 return false;
3407 }
3408 }
3409
3410 if (s.what & layer_state_t::eSidebandStreamChanged) {
3411 if (mDrawingState.sidebandStream != s.sidebandStream) {
3412 ALOGV("%s: false [eSidebandStreamChanged changed]", __func__);
3413 return false;
3414 }
3415 }
3416
3417 if (s.what & layer_state_t::eColorSpaceAgnosticChanged) {
3418 if (mDrawingState.colorSpaceAgnostic != s.colorSpaceAgnostic) {
3419 ALOGV("%s: false [eColorSpaceAgnosticChanged changed]", __func__);
3420 return false;
3421 }
3422 }
3423
3424 if (s.what & layer_state_t::eShadowRadiusChanged) {
3425 if (mDrawingState.shadowRadius != s.shadowRadius) {
3426 ALOGV("%s: false [eShadowRadiusChanged changed]", __func__);
3427 return false;
3428 }
3429 }
3430
3431 if (s.what & layer_state_t::eFixedTransformHintChanged) {
3432 if (mDrawingState.fixedTransformHint != s.fixedTransformHint) {
3433 ALOGV("%s: false [eFixedTransformHintChanged changed]", __func__);
3434 return false;
3435 }
3436 }
3437
3438 if (s.what & layer_state_t::eTrustedOverlayChanged) {
3439 if (mDrawingState.isTrustedOverlay != s.isTrustedOverlay) {
3440 ALOGV("%s: false [eTrustedOverlayChanged changed]", __func__);
3441 return false;
3442 }
3443 }
3444
3445 if (s.what & layer_state_t::eStretchChanged) {
3446 StretchEffect temp = s.stretchEffect;
3447 temp.sanitize();
3448 if (mDrawingState.stretchEffect != temp) {
3449 ALOGV("%s: false [eStretchChanged changed]", __func__);
3450 return false;
3451 }
3452 }
3453
3454 if (s.what & layer_state_t::eBufferCropChanged) {
3455 if (mDrawingState.bufferCrop != s.bufferCrop) {
3456 ALOGV("%s: false [eBufferCropChanged changed]", __func__);
3457 return false;
3458 }
3459 }
3460
3461 if (s.what & layer_state_t::eDestinationFrameChanged) {
3462 if (mDrawingState.destinationFrame != s.destinationFrame) {
3463 ALOGV("%s: false [eDestinationFrameChanged changed]", __func__);
3464 return false;
3465 }
3466 }
3467
3468 if (s.what & layer_state_t::eDimmingEnabledChanged) {
3469 if (mDrawingState.dimmingEnabled != s.dimmingEnabled) {
3470 ALOGV("%s: false [eDimmingEnabledChanged changed]", __func__);
3471 return false;
3472 }
3473 }
3474
3475 ALOGV("%s: true", __func__);
3476 return true;
3477}
3478
3479bool Layer::isHdrY410() const {
3480 // pixel format is HDR Y410 masquerading as RGBA_1010102
3481 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
3482 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
3483 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
3484}
3485
Vishnu Naire14c6b32022-08-06 04:20:15 +00003486sp<LayerFE> Layer::getCompositionEngineLayerFE() const {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003487 // 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 +00003488 return hasSomethingToDraw() ? mLayerFE : nullptr;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003489}
3490
Vishnu Naire14c6b32022-08-06 04:20:15 +00003491const LayerSnapshot* Layer::getLayerSnapshot() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003492 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003493}
3494
Vishnu Naire14c6b32022-08-06 04:20:15 +00003495LayerSnapshot* Layer::editLayerSnapshot() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003496 return mSnapshot.get();
3497}
Vishnu Naire14c6b32022-08-06 04:20:15 +00003498
Patrick Williamsbb25f802022-08-30 23:02:34 +00003499const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003500 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003501}
3502
Patrick Williams7584c6a2022-10-29 02:10:58 +00003503sp<LayerFE> Layer::copyCompositionEngineLayerFE() const {
3504 auto result = mFlinger->getFactory().createLayerFE(mLayerFE->getDebugName());
3505 result->mSnapshot = std::make_unique<LayerSnapshot>(*mSnapshot);
3506 return result;
3507}
3508
Patrick Williamsbb25f802022-08-30 23:02:34 +00003509void Layer::useSurfaceDamage() {
3510 if (mFlinger->mForceFullDamage) {
3511 surfaceDamageRegion = Region::INVALID_REGION;
3512 } else {
3513 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
3514 }
3515}
3516
3517void Layer::useEmptyDamage() {
3518 surfaceDamageRegion.clear();
3519}
3520
3521bool Layer::isOpaque(const Layer::State& s) const {
3522 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
3523 // layer's opaque flag.
3524 if (!hasSomethingToDraw()) {
3525 return false;
3526 }
3527
3528 // if the layer has the opaque flag, then we're always opaque
3529 if ((s.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque) {
3530 return true;
3531 }
3532
3533 // If the buffer has no alpha channel, then we are opaque
3534 if (hasBufferOrSidebandStream() && isOpaqueFormat(getPixelFormat())) {
3535 return true;
3536 }
3537
3538 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
3539 return fillsColor() && getAlpha() == 1.0_hf;
3540}
3541
3542bool Layer::canReceiveInput() const {
3543 return !isHiddenByPolicy() && (mBufferInfo.mBuffer == nullptr || getAlpha() > 0.0f);
3544}
3545
3546bool Layer::isVisible() const {
3547 if (!hasSomethingToDraw()) {
3548 return false;
3549 }
3550
3551 if (isHiddenByPolicy()) {
3552 return false;
3553 }
3554
3555 return getAlpha() > 0.0f || hasBlur();
3556}
3557
3558void Layer::onPostComposition(const DisplayDevice* display,
3559 const std::shared_ptr<FenceTime>& glDoneFence,
3560 const std::shared_ptr<FenceTime>& presentFence,
3561 const CompositorTiming& compositorTiming) {
3562 // mFrameLatencyNeeded is true when a new frame was latched for the
3563 // composition.
3564 if (!mBufferInfo.mFrameLatencyNeeded) return;
3565
3566 for (const auto& handle : mDrawingState.callbackHandles) {
3567 handle->gpuCompositionDoneFence = glDoneFence;
3568 handle->compositorTiming = compositorTiming;
3569 }
3570
3571 // Update mFrameTracker.
3572 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
3573 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
3574
3575 const int32_t layerId = getSequence();
3576 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
3577
3578 const auto outputLayer = findOutputLayerForDisplay(display);
3579 if (outputLayer && outputLayer->requiresClientComposition()) {
3580 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
3581 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3582 clientCompositionTimestamp,
3583 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
3584 // Update the SurfaceFrames in the drawing state
3585 if (mDrawingState.bufferSurfaceFrameTX) {
3586 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
3587 }
3588 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
3589 surfaceFrame->setGpuComposition();
3590 }
3591 }
3592
3593 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
3594 if (frameReadyFence->isValid()) {
3595 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
3596 } else {
3597 // There was no fence for this frame, so assume that it was ready
3598 // to be presented at the desired present time.
3599 mFrameTracker.setFrameReadyTime(desiredPresentTime);
3600 }
3601
3602 if (display) {
Ady Abrahamace3d052022-11-17 16:25:05 -08003603 const Fps refreshRate = display->refreshRateSelector().getActiveMode().fps;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003604 const std::optional<Fps> renderRate =
3605 mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
3606
3607 const auto vote = frameRateToSetFrameRateVotePayload(mDrawingState.frameRate);
3608 const auto gameMode = getGameMode();
3609
3610 if (presentFence->isValid()) {
3611 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
3612 refreshRate, renderRate, vote, gameMode);
3613 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3614 presentFence,
3615 FrameTracer::FrameEvent::PRESENT_FENCE);
3616 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
3617 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
3618 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Dominik Laskowskidc2bb802022-09-28 16:02:59 -04003619 // The HWC doesn't support present fences, so use the present timestamp instead.
3620 const nsecs_t presentTimestamp =
3621 mFlinger->getHwComposer().getPresentTimestamp(*displayId);
3622
3623 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
3624 const nsecs_t vsyncPeriod = display->getVsyncPeriodFromHWC();
3625 const nsecs_t actualPresentTime = now - ((now - presentTimestamp) % vsyncPeriod);
3626
Patrick Williamsbb25f802022-08-30 23:02:34 +00003627 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
3628 refreshRate, renderRate, vote, gameMode);
3629 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(),
3630 mCurrentFrameNumber, actualPresentTime,
3631 FrameTracer::FrameEvent::PRESENT_FENCE);
3632 mFrameTracker.setActualPresentTime(actualPresentTime);
3633 }
3634 }
3635
3636 mFrameTracker.advanceFrame();
3637 mBufferInfo.mFrameLatencyNeeded = false;
3638}
3639
3640bool Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
3641 ATRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(),
3642 getDrawingState().frameNumber);
3643
3644 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
3645
3646 if (refreshRequired) {
3647 return refreshRequired;
3648 }
3649
3650 // If the head buffer's acquire fence hasn't signaled yet, return and
3651 // try again later
3652 if (!fenceHasSignaled()) {
3653 ATRACE_NAME("!fenceHasSignaled()");
3654 mFlinger->onLayerUpdate();
3655 return false;
3656 }
3657
3658 updateTexImage(latchTime);
3659 if (mDrawingState.buffer == nullptr) {
3660 return false;
3661 }
3662
3663 // Capture the old state of the layer for comparisons later
3664 BufferInfo oldBufferInfo = mBufferInfo;
3665 const bool oldOpacity = isOpaque(mDrawingState);
3666 mPreviousFrameNumber = mCurrentFrameNumber;
3667 mCurrentFrameNumber = mDrawingState.frameNumber;
3668 gatherBufferInfo();
3669
3670 if (oldBufferInfo.mBuffer == nullptr) {
3671 // the first time we receive a buffer, we need to trigger a
3672 // geometry invalidation.
3673 recomputeVisibleRegions = true;
3674 }
3675
3676 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
3677 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
3678 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
3679 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
3680 recomputeVisibleRegions = true;
3681 }
3682
3683 if (oldBufferInfo.mBuffer != nullptr) {
3684 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3685 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3686 if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
3687 bufHeight != oldBufferInfo.mBuffer->getHeight()) {
3688 recomputeVisibleRegions = true;
3689 }
3690 }
3691
3692 if (oldOpacity != isOpaque(mDrawingState)) {
3693 recomputeVisibleRegions = true;
3694 }
3695
3696 return true;
3697}
3698
3699bool Layer::hasReadyFrame() const {
3700 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
3701}
3702
3703bool Layer::isProtected() const {
3704 return (mBufferInfo.mBuffer != nullptr) &&
3705 (mBufferInfo.mBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
3706}
3707
3708// As documented in libhardware header, formats in the range
3709// 0x100 - 0x1FF are specific to the HAL implementation, and
3710// are known to have no alpha channel
3711// TODO: move definition for device-specific range into
3712// hardware.h, instead of using hard-coded values here.
3713#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
3714
3715bool Layer::isOpaqueFormat(PixelFormat format) {
3716 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
3717 return true;
3718 }
3719 switch (format) {
3720 case PIXEL_FORMAT_RGBA_8888:
3721 case PIXEL_FORMAT_BGRA_8888:
3722 case PIXEL_FORMAT_RGBA_FP16:
3723 case PIXEL_FORMAT_RGBA_1010102:
3724 case PIXEL_FORMAT_R_8:
3725 return false;
3726 }
3727 // in all other case, we have no blending (also for unknown formats)
3728 return true;
3729}
3730
3731bool Layer::needsFiltering(const DisplayDevice* display) const {
3732 if (!hasBufferOrSidebandStream()) {
3733 return false;
3734 }
3735 const auto outputLayer = findOutputLayerForDisplay(display);
3736 if (outputLayer == nullptr) {
3737 return false;
3738 }
3739
3740 // We need filtering if the sourceCrop rectangle size does not match the
3741 // displayframe rectangle size (not a 1:1 render)
3742 const auto& compositionState = outputLayer->getState();
3743 const auto displayFrame = compositionState.displayFrame;
3744 const auto sourceCrop = compositionState.sourceCrop;
3745 return sourceCrop.getHeight() != displayFrame.getHeight() ||
3746 sourceCrop.getWidth() != displayFrame.getWidth();
3747}
3748
3749bool Layer::needsFilteringForScreenshots(const DisplayDevice* display,
3750 const ui::Transform& inverseParentTransform) const {
3751 if (!hasBufferOrSidebandStream()) {
3752 return false;
3753 }
3754 const auto outputLayer = findOutputLayerForDisplay(display);
3755 if (outputLayer == nullptr) {
3756 return false;
3757 }
3758
3759 // We need filtering if the sourceCrop rectangle size does not match the
3760 // viewport rectangle size (not a 1:1 render)
3761 const auto& compositionState = outputLayer->getState();
3762 const ui::Transform& displayTransform = display->getTransform();
3763 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
3764 // Undo the transformation of the displayFrame so that we're back into
3765 // layer-stack space.
3766 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
3767 const FloatRect sourceCrop = compositionState.sourceCrop;
3768
3769 int32_t frameHeight = frame.getHeight();
3770 int32_t frameWidth = frame.getWidth();
3771 // If the display transform had a rotational component then undo the
3772 // rotation so that the orientation matches the source crop.
3773 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
3774 std::swap(frameHeight, frameWidth);
3775 }
3776 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
3777}
3778
3779void Layer::latchAndReleaseBuffer() {
3780 if (hasReadyFrame()) {
3781 bool ignored = false;
3782 latchBuffer(ignored, systemTime());
3783 }
3784 releasePendingBuffer(systemTime());
3785}
3786
3787PixelFormat Layer::getPixelFormat() const {
3788 return mBufferInfo.mPixelFormat;
3789}
3790
3791bool Layer::getTransformToDisplayInverse() const {
3792 return mBufferInfo.mTransformToDisplayInverse;
3793}
3794
3795Rect Layer::getBufferCrop() const {
3796 // this is the crop rectangle that applies to the buffer
3797 // itself (as opposed to the window)
3798 if (!mBufferInfo.mCrop.isEmpty()) {
3799 // if the buffer crop is defined, we use that
3800 return mBufferInfo.mCrop;
3801 } else if (mBufferInfo.mBuffer != nullptr) {
3802 // otherwise we use the whole buffer
3803 return mBufferInfo.mBuffer->getBounds();
3804 } else {
3805 // if we don't have a buffer yet, we use an empty/invalid crop
3806 return Rect();
3807 }
3808}
3809
3810uint32_t Layer::getBufferTransform() const {
3811 return mBufferInfo.mTransform;
3812}
3813
3814ui::Dataspace Layer::getDataSpace() const {
3815 return mDrawingState.dataspaceRequested ? getRequestedDataSpace() : ui::Dataspace::UNKNOWN;
3816}
3817
3818ui::Dataspace Layer::getRequestedDataSpace() const {
3819 return hasBufferOrSidebandStream() ? mBufferInfo.mDataspace : mDrawingState.dataspace;
3820}
3821
3822ui::Dataspace Layer::translateDataspace(ui::Dataspace dataspace) {
3823 ui::Dataspace updatedDataspace = dataspace;
3824 // translate legacy dataspaces to modern dataspaces
3825 switch (dataspace) {
3826 case ui::Dataspace::SRGB:
3827 updatedDataspace = ui::Dataspace::V0_SRGB;
3828 break;
3829 case ui::Dataspace::SRGB_LINEAR:
3830 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
3831 break;
3832 case ui::Dataspace::JFIF:
3833 updatedDataspace = ui::Dataspace::V0_JFIF;
3834 break;
3835 case ui::Dataspace::BT601_625:
3836 updatedDataspace = ui::Dataspace::V0_BT601_625;
3837 break;
3838 case ui::Dataspace::BT601_525:
3839 updatedDataspace = ui::Dataspace::V0_BT601_525;
3840 break;
3841 case ui::Dataspace::BT709:
3842 updatedDataspace = ui::Dataspace::V0_BT709;
3843 break;
3844 default:
3845 break;
3846 }
3847
3848 return updatedDataspace;
3849}
3850
3851sp<GraphicBuffer> Layer::getBuffer() const {
3852 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
3853}
3854
Patrick Williamsbb25f802022-08-30 23:02:34 +00003855void Layer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
3856 mTransformHint = getFixedTransformHint();
3857 if (mTransformHint == ui::Transform::ROT_INVALID) {
3858 mTransformHint = displayTransformHint;
3859 }
Vishnu Nair71fcf912022-10-18 09:14:20 -07003860 mSkipReportingTransformHint = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003861}
3862
3863const std::shared_ptr<renderengine::ExternalTexture>& Layer::getExternalTexture() const {
3864 return mBufferInfo.mBuffer;
3865}
3866
3867bool Layer::setColor(const half3& color) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003868 if (mDrawingState.color.rgb == color) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003869 return false;
3870 }
3871
3872 mDrawingState.sequence++;
Vishnu Nairbbceb462022-10-10 04:52:13 +00003873 mDrawingState.color.rgb = color;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003874 mDrawingState.modified = true;
3875 setTransactionFlags(eTransactionNeeded);
3876 return true;
3877}
3878
3879bool Layer::fillsColor() const {
3880 return !hasBufferOrSidebandStream() && mDrawingState.color.r >= 0.0_hf &&
3881 mDrawingState.color.g >= 0.0_hf && mDrawingState.color.b >= 0.0_hf;
3882}
3883
3884bool Layer::hasBlur() const {
3885 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
3886}
3887
Vishnu Nairba354102022-08-01 00:14:18 +00003888void Layer::updateSnapshot(bool updateGeometry) {
3889 if (!getCompositionEngineLayerFE()) {
3890 return;
3891 }
3892
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003893 auto* snapshot = editLayerSnapshot();
Vishnu Nairba354102022-08-01 00:14:18 +00003894 if (updateGeometry) {
3895 prepareBasicGeometryCompositionState();
3896 prepareGeometryCompositionState();
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003897 snapshot->roundedCorner = getRoundedCornerState();
3898 snapshot->stretchEffect = getStretchEffect();
3899 snapshot->transformedBounds = mScreenBounds;
3900 if (mEffectiveShadowRadius > 0.f) {
3901 snapshot->shadowSettings = mFlinger->mDrawingState.globalShadowSettings;
3902
3903 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
3904 // it if transparent regions are present. This may not be necessary since shadows are
3905 // typically cast by layers without transparent regions.
3906 snapshot->shadowSettings.boundaries = mBounds;
3907
3908 const float casterAlpha = snapshot->alpha;
3909 const bool casterIsOpaque =
3910 ((mBufferInfo.mBuffer != nullptr) && isOpaque(mDrawingState));
3911
3912 // If the casting layer is translucent, we need to fill in the shadow underneath the
3913 // layer. Otherwise the generated shadow will only be shown around the casting layer.
3914 snapshot->shadowSettings.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
3915 snapshot->shadowSettings.ambientColor *= casterAlpha;
3916 snapshot->shadowSettings.spotColor *= casterAlpha;
3917 }
3918 snapshot->shadowSettings.length = mEffectiveShadowRadius;
Vishnu Nairba354102022-08-01 00:14:18 +00003919 }
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003920 snapshot->contentOpaque = isOpaque(mDrawingState);
3921 snapshot->isHdrY410 = isHdrY410();
3922 snapshot->bufferNeedsFiltering = bufferNeedsFiltering();
3923 sp<Layer> p = mDrawingParent.promote();
3924 if (p != nullptr) {
3925 snapshot->transform = p->getTransform();
3926 } else {
3927 snapshot->transform.reset();
3928 }
3929 snapshot->bufferSize = getBufferSize(mDrawingState);
3930 snapshot->externalTexture = mBufferInfo.mBuffer;
Vishnu Naire14c6b32022-08-06 04:20:15 +00003931 snapshot->hasReadyFrame = hasReadyFrame();
Vishnu Nairba354102022-08-01 00:14:18 +00003932 preparePerFrameCompositionState();
3933}
3934
Vishnu Naire14c6b32022-08-06 04:20:15 +00003935void Layer::updateChildrenSnapshots(bool updateGeometry) {
3936 for (const sp<Layer>& child : mDrawingChildren) {
3937 child->updateSnapshot(updateGeometry);
3938 child->updateChildrenSnapshots(updateGeometry);
3939 }
3940}
3941
Vishnu Nair0a4fb002022-08-08 02:40:42 +00003942void Layer::updateMetadataSnapshot(const LayerMetadata& parentMetadata) {
3943 mSnapshot->layerMetadata = parentMetadata;
3944 mSnapshot->layerMetadata.merge(mDrawingState.metadata);
3945 for (const sp<Layer>& child : mDrawingChildren) {
3946 child->updateMetadataSnapshot(mSnapshot->layerMetadata);
3947 }
3948}
3949
3950void Layer::updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMetadata,
3951 std::unordered_set<Layer*>& visited) {
3952 if (visited.find(this) != visited.end()) {
3953 ALOGW("Cycle containing layer %s detected in z-order relatives", getDebugName());
3954 return;
3955 }
3956 visited.insert(this);
3957
3958 mSnapshot->relativeLayerMetadata = relativeLayerMetadata;
3959
3960 if (mDrawingState.zOrderRelatives.empty()) {
3961 return;
3962 }
3963 LayerMetadata childRelativeLayerMetadata = mSnapshot->relativeLayerMetadata;
3964 childRelativeLayerMetadata.merge(mSnapshot->layerMetadata);
3965 for (wp<Layer> weakRelative : mDrawingState.zOrderRelatives) {
3966 sp<Layer> relative = weakRelative.promote();
3967 if (!relative) {
3968 continue;
3969 }
3970 relative->updateRelativeMetadataSnapshot(childRelativeLayerMetadata, visited);
3971 }
3972}
3973
Vishnu Nairc6086dd2022-11-04 04:39:35 +00003974int Layer::getHwcCacheSlot(const client_cache_t& clientCacheId) {
3975 return mHwcSlotGenerator->getHwcCacheSlot(clientCacheId);
3976}
3977
Patrick Williams377c0142022-10-13 17:33:38 +00003978LayerSnapshotGuard::LayerSnapshotGuard(Layer* layer) : mLayer(layer) {
Patrick Williams4da60852022-11-17 17:19:38 +00003979 if (mLayer) {
3980 mLayer->mLayerFE->mSnapshot = std::move(mLayer->mSnapshot);
3981 }
Patrick Williams377c0142022-10-13 17:33:38 +00003982}
3983
3984LayerSnapshotGuard::~LayerSnapshotGuard() {
3985 if (mLayer) {
3986 mLayer->mSnapshot = std::move(mLayer->mLayerFE->mSnapshot);
3987 }
3988}
3989
3990LayerSnapshotGuard::LayerSnapshotGuard(LayerSnapshotGuard&& other) : mLayer(other.mLayer) {
3991 other.mLayer = nullptr;
3992}
3993
3994LayerSnapshotGuard& LayerSnapshotGuard::operator=(LayerSnapshotGuard&& other) {
3995 mLayer = other.mLayer;
3996 other.mLayer = nullptr;
3997 return *this;
3998}
3999
Mathias Agopian13127d82013-03-05 17:47:11 -08004000// ---------------------------------------------------------------------------
4001
Marin Shalamanov46084422020-10-13 12:33:42 +02004002std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07004003 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
4004 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02004005}
4006
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07004007} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07004008
4009#if defined(__gl_h_)
4010#error "don't include gl/gl.h in this file"
4011#endif
4012
4013#if defined(__gl2_h_)
4014#error "don't include gl2/gl2.h in this file"
4015#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08004016
4017// TODO(b/129481165): remove the #pragma below and fix conversion issues
4018#pragma clang diagnostic pop // ignored "-Wconversion"