blob: 2a18521b5bbd18303898c2cdd67f488e3e5feac8 [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
198 mFrameTracker.setDisplayRefreshPeriod(
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400199 args.flinger->mScheduler->getVsyncPeriodFromRefreshRateSelector());
Robert Carr2e102c92018-10-23 12:11:15 -0700200
Vishnu Naircb8be502022-10-12 19:03:23 +0000201 mOwnerUid = args.ownerUid;
202 mOwnerPid = args.ownerPid;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000203
204 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
205 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
206 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
207 mDrawingState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000208
209 mSnapshot->sequence = sequence;
210 mSnapshot->name = getDebugName();
211 mSnapshot->textureName = mTextureName;
212 mSnapshot->premultipliedAlpha = mPremultipliedAlpha;
213 mSnapshot->transform = {};
Dominik Laskowski75848362019-11-11 17:57:20 -0800214}
215
216void Layer::onFirstRef() {
217 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700218}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700219
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700220Layer::~Layer() {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000221 // The original layer and the clone layer share the same texture and buffer. Therefore, only
222 // one of the layers, in this case the original layer, needs to handle the deletion. The
223 // original layer and the clone should be removed at the same time so there shouldn't be any
224 // issue with the clone layer trying to use the texture.
225 if (mBufferInfo.mBuffer != nullptr) {
226 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
227 mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
228 mBufferInfo.mFence,
229 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
230 mOwnerUid));
231 }
232 if (!isClone()) {
233 // The original layer and the clone layer share the same texture. Therefore, only one of
234 // the layers, in this case the original layer, needs to handle the deletion. The original
235 // layer and the clone should be removed at the same time so there shouldn't be any issue
236 // with the clone layer trying to use the deleted texture.
237 mFlinger->deleteTextureAsync(mTextureName);
238 }
239 const int32_t layerId = getSequence();
240 mFlinger->mTimeStats->onDestroy(layerId);
241 mFlinger->mFrameTracer->onDestroy(layerId);
242
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000243 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700244 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700245
246 if (mDrawingState.sidebandStream != nullptr) {
247 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
248 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700249 if (mHadClonedChild) {
250 mFlinger->mNumClones--;
251 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700252}
253
Mathias Agopian13127d82013-03-05 17:47:11 -0800254// ---------------------------------------------------------------------------
255// callbacks
256// ---------------------------------------------------------------------------
257
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700258void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700259 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700260 return;
261 }
Robert Carr2e102c92018-10-23 12:11:15 -0700262
Robert Carr6a160312021-05-17 12:08:20 -0700263 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700264 if (strongRelative == nullptr) {
265 setZOrderRelativeOf(nullptr);
266 return;
267 }
268
269 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700270 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700271 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800272 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700273 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700274}
275
276void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700277 if (!mRemovedFromDrawingState) {
278 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700279 mFlinger->mScheduler->deregisterLayer(this);
280 }
Rob Carr4bba3702018-10-08 21:53:30 +0000281
Ady Abrahamd11bade2022-08-01 16:18:03 -0700282 mFlinger->markLayerPendingRemovalLocked(sp<Layer>::fromExisting(this));
Chia-I Wuc6657022017-08-15 11:18:17 -0700283}
Chia-I Wu38512252017-05-17 14:36:16 -0700284
chaviw68d4dab2020-06-08 15:07:32 -0700285sp<Layer> Layer::getRootLayer() {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000286 sp<Layer> parent = getParent();
chaviw68d4dab2020-06-08 15:07:32 -0700287 if (parent == nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700288 return sp<Layer>::fromExisting(this);
chaviw68d4dab2020-06-08 15:07:32 -0700289 }
290 return parent->getRootLayer();
291}
292
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700293void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700294 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
295 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700296 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700297
298 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700299 layer->removeFromCurrentState();
300 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700301 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700302}
303
chaviw61626f22018-11-15 16:26:27 -0800304void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700305 if (mRemovedFromDrawingState) {
306 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700307 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700308 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700309 }
chaviw61626f22018-11-15 16:26:27 -0800310
311 for (const auto& child : mCurrentChildren) {
312 child->addToCurrentState();
313 }
314}
315
Mathias Agopian13127d82013-03-05 17:47:11 -0800316// ---------------------------------------------------------------------------
317// set-up
318// ---------------------------------------------------------------------------
319
chaviw13fdc492017-06-27 12:40:18 -0700320bool Layer::getPremultipledAlpha() const {
321 return mPremultipliedAlpha;
322}
323
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700324sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800325 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700326 if (mGetHandleCalled) {
327 ALOGE("Get handle called twice" );
328 return nullptr;
329 }
330 mGetHandleCalled = true;
Vishnu Nair07e2a482022-10-18 19:18:16 +0000331 return sp<LayerHandle>::make(mFlinger, sp<Layer>::fromExisting(this));
Mathias Agopian13127d82013-03-05 17:47:11 -0800332}
333
334// ---------------------------------------------------------------------------
335// h/w composer set-up
336// ---------------------------------------------------------------------------
337
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700338static Rect reduce(const Rect& win, const Region& exclude) {
339 if (CC_LIKELY(exclude.isEmpty())) {
340 return win;
341 }
342 if (exclude.isRect()) {
343 return win.reduce(exclude.getBounds());
344 }
345 return Region(win).subtract(exclude).getBounds();
346}
347
Dan Stoza80d61162017-12-20 15:57:52 -0800348static FloatRect reduce(const FloatRect& win, const Region& exclude) {
349 if (CC_LIKELY(exclude.isEmpty())) {
350 return win;
351 }
352 // Convert through Rect (by rounding) for lack of FloatRegion
353 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
354}
355
Vishnu Nair4351ad52019-02-11 14:13:02 -0800356Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800357 if (!reduceTransparentRegion) {
358 return Rect{mScreenBounds};
359 }
360
361 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800362 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800363 // Transform to screen space.
364 bounds = t.transform(bounds);
365 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700366}
367
Vishnu Nair4351ad52019-02-11 14:13:02 -0800368FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000369 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800370 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700371}
372
Vishnu Nairf0c28512019-02-08 12:40:28 -0800373FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
374 // Subtract the transparent region and snap to the bounds.
375 return reduce(mBounds, activeTransparentRegion);
376}
377
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700378void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
379 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800380 const State& s(getDrawingState());
381
382 // Calculate effective layer transform
383 mEffectiveTransform = parentTransform * getActiveTransform(s);
384
Garfield Tan2c1782c2022-02-16 15:25:05 -0800385 if (CC_UNLIKELY(!isTransformValid())) {
386 ALOGW("Stop computing bounds for %s because it has invalid transformation.",
387 getDebugName());
388 return;
389 }
390
Vishnu Nair4351ad52019-02-11 14:13:02 -0800391 // Transform parent bounds to layer space
392 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
393
Vishnu Nairc652ff82019-03-15 12:48:54 -0700394 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800395 mSourceBounds = computeSourceBounds(parentBounds);
396
397 // Calculate bounds by croping diplay frame with layer crop and parent bounds
398 FloatRect bounds = mSourceBounds;
399 const Rect layerCrop = getCrop(s);
400 if (!layerCrop.isEmpty()) {
401 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
402 }
403 bounds = bounds.intersect(parentBounds);
404
405 mBounds = bounds;
406 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700407
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700408 // Use the layer's own shadow radius if set. Otherwise get the radius from
409 // parent.
410 if (s.shadowRadius > 0.f) {
411 mEffectiveShadowRadius = s.shadowRadius;
412 } else {
413 mEffectiveShadowRadius = parentShadowRadius;
414 }
415
416 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
417 // don't pass it to its children.
418 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
419
Vishnu Nair4351ad52019-02-11 14:13:02 -0800420 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700421 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800422 }
Vishnu Nairba354102022-08-01 00:14:18 +0000423
424 if (mPotentialCursor) {
425 prepareCursorCompositionState();
426 }
Vishnu Nair4351ad52019-02-11 14:13:02 -0800427}
428
Vishnu Nair60356342018-11-13 13:00:45 -0800429Rect Layer::getCroppedBufferSize(const State& s) const {
430 Rect size = getBufferSize(s);
431 Rect crop = getCrop(s);
432 if (!crop.isEmpty() && size.isValid()) {
433 size.intersect(crop, &size);
434 } else if (!crop.isEmpty()) {
435 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700436 }
Vishnu Nair60356342018-11-13 13:00:45 -0800437 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800438}
439
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700440void Layer::setupRoundedCornersCropCoordinates(Rect win,
441 const FloatRect& roundedCornersCrop) const {
442 // Translate win by the rounded corners rect coordinates, to have all values in
443 // layer coordinate space.
444 win.left -= roundedCornersCrop.left;
445 win.right -= roundedCornersCrop.left;
446 win.top -= roundedCornersCrop.top;
447 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700448}
449
Lloyd Piquede196652020-01-22 17:29:58 -0800450void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800451 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800452 const auto alpha = static_cast<float>(getAlpha());
453 const bool opaque = isOpaque(drawingState);
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700454 const bool usesRoundedCorners = hasRoundedCorners();
Lloyd Piquec6687342019-03-07 21:34:57 -0800455
456 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
457 if (!opaque || alpha != 1.0f) {
458 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
459 : Hwc2::IComposerClient::BlendMode::COVERAGE;
460 }
461
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000462 auto* snapshot = editLayerSnapshot();
463 snapshot->outputFilter = getOutputFilter();
464 snapshot->isVisible = isVisible();
465 snapshot->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
466 snapshot->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800467
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000468 snapshot->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800469 contentDirty = false;
470
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000471 snapshot->geomLayerBounds = mBounds;
472 snapshot->geomLayerTransform = getTransform();
473 snapshot->geomInverseLayerTransform = snapshot->geomLayerTransform.inverse();
474 snapshot->transparentRegionHint = getActiveTransparentRegion(drawingState);
Vishnu Naire7613db2022-11-04 00:07:06 -0700475 snapshot->blurRegionTransform = getActiveTransform(drawingState).inverse();
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000476 snapshot->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
477 snapshot->alpha = alpha;
478 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
479 snapshot->blurRegions = drawingState.blurRegions;
480 snapshot->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800481}
482
Lloyd Piquede196652020-01-22 17:29:58 -0800483void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800484 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000485 auto* snapshot = editLayerSnapshot();
David Sodman15094112018-10-11 09:39:37 -0700486
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000487 snapshot->geomBufferSize = getBufferSize(drawingState);
488 snapshot->geomContentCrop = getBufferCrop();
489 snapshot->geomCrop = getCrop(drawingState);
490 snapshot->geomBufferTransform = getBufferTransform();
491 snapshot->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
492 snapshot->geomUsesSourceCrop = usesSourceCrop();
493 snapshot->isSecure = isSecure();
Lloyd Piquede196652020-01-22 17:29:58 -0800494
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000495 snapshot->metadata.clear();
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800496 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
497 for (const auto& [key, mandatory] : supportedMetadata) {
498 const auto& genericLayerMetadataCompatibilityMap =
499 mFlinger->getGenericLayerMetadataKeyMap();
500 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
501 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
502 continue;
503 }
504 const uint32_t id = compatIter->second;
505
506 auto it = drawingState.metadata.mMap.find(id);
507 if (it == std::end(drawingState.metadata.mMap)) {
508 continue;
509 }
510
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000511 snapshot->metadata.emplace(key,
512 compositionengine::GenericLayerMetadataEntry{mandatory,
513 it->second});
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800514 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800515}
David Sodmanba340492018-08-05 21:51:33 -0700516
Lloyd Piquede196652020-01-22 17:29:58 -0800517void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800518 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000519 auto* snapshot = editLayerSnapshot();
Lloyd Piquef5275482019-01-29 18:42:42 -0800520
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000521 snapshot->forceClientComposition = false;
Lloyd Piquede196652020-01-22 17:29:58 -0800522
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000523 snapshot->isColorspaceAgnostic = isColorSpaceAgnostic();
524 snapshot->dataspace = getDataSpace();
525 snapshot->colorTransform = getColorTransform();
526 snapshot->colorTransformIsIdentity = !hasColorTransform();
527 snapshot->surfaceDamage = surfaceDamageRegion;
528 snapshot->hasProtectedContent = isProtected();
529 snapshot->dimmingEnabled = isDimmingEnabled();
Lloyd Pique688abd42019-02-15 15:42:24 -0800530
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700531 const bool usesRoundedCorners = hasRoundedCorners();
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700532
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000533 snapshot->isOpaque = isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800534
535 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400536 // Rounded corners no longer force client composition, since we may use a
537 // hole punch so that the layer will appear to have rounded corners.
538 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000539 snapshot->stretchEffect.hasEffect()) {
540 snapshot->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800541 }
Vishnu Nairb801a982021-11-02 15:12:08 -0700542 // If there are no visible region changes, we still need to update blur parameters.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000543 snapshot->blurRegions = drawingState.blurRegions;
544 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400545
546 // Layer framerate is used in caching decisions.
547 // Retrieve it from the scheduler which maintains an instance of LayerHistory, and store it in
548 // LayerFECompositionState where it would be visible to Flattener.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000549 snapshot->fps = mFlinger->getLayerFramerate(systemTime(), getSequence());
Patrick Williamsbb25f802022-08-30 23:02:34 +0000550
551 if (hasBufferOrSidebandStream()) {
552 preparePerFrameBufferCompositionState();
553 } else {
554 preparePerFrameEffectsCompositionState();
555 }
556}
557
558void Layer::preparePerFrameBufferCompositionState() {
559 // Sideband layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000560 auto* snapshot = editLayerSnapshot();
561 if (snapshot->sidebandStream.get() && !snapshot->sidebandStreamHasFrame) {
562 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000563 aidl::android::hardware::graphics::composer3::Composition::SIDEBAND;
564 return;
565 } else if ((mDrawingState.flags & layer_state_t::eLayerIsDisplayDecoration) != 0) {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000566 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000567 aidl::android::hardware::graphics::composer3::Composition::DISPLAY_DECORATION;
568 } else {
569 // Normal buffer layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000570 snapshot->hdrMetadata = mBufferInfo.mHdrMetadata;
571 snapshot->compositionType = mPotentialCursor
Patrick Williamsbb25f802022-08-30 23:02:34 +0000572 ? aidl::android::hardware::graphics::composer3::Composition::CURSOR
573 : aidl::android::hardware::graphics::composer3::Composition::DEVICE;
574 }
575
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000576 snapshot->buffer = getBuffer();
577 snapshot->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
Patrick Williamsbb25f802022-08-30 23:02:34 +0000578 ? 0
579 : mBufferInfo.mBufferSlot;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000580 snapshot->acquireFence = mBufferInfo.mFence;
581 snapshot->frameNumber = mBufferInfo.mFrameNumber;
582 snapshot->sidebandStreamHasFrame = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000583}
584
585void Layer::preparePerFrameEffectsCompositionState() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000586 auto* snapshot = editLayerSnapshot();
587 snapshot->color = getColor();
588 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000589 aidl::android::hardware::graphics::composer3::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800590}
591
Lloyd Piquede196652020-01-22 17:29:58 -0800592void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800593 const State& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000594 auto* snapshot = editLayerSnapshot();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800595
596 // Apply the layer's transform, followed by the display's global transform
597 // Here we're guaranteed that the layer's transform preserves rects
598 Rect win = getCroppedBufferSize(drawingState);
599 // Subtract the transparent region and snap to the bounds
600 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
601 Rect frame(getTransform().transform(bounds));
602
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000603 snapshot->cursorFrame = frame;
Lloyd Piquede196652020-01-22 17:29:58 -0800604}
605
Lloyd Piquea83776c2019-01-29 18:42:32 -0800606const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700607 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800608}
609
Mathias Agopian13127d82013-03-05 17:47:11 -0800610// ---------------------------------------------------------------------------
611// drawing...
612// ---------------------------------------------------------------------------
613
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500614aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType(
615 const DisplayDevice& display) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700616 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800617 if (outputLayer == nullptr) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500618 return aidl::android::hardware::graphics::composer3::Composition::INVALID;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800619 }
620 if (outputLayer->getState().hwc) {
621 return (*outputLayer->getState().hwc).hwcCompositionType;
622 } else {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500623 return aidl::android::hardware::graphics::composer3::Composition::CLIENT;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800624 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800625}
626
Mathias Agopian13127d82013-03-05 17:47:11 -0800627// ----------------------------------------------------------------------------
628// local state
629// ----------------------------------------------------------------------------
630
David Sodman41fdfc92017-11-06 16:09:56 -0800631bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800632 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700633 if (s.flags & layer_state_t::eLayerSecure) {
634 return true;
635 }
636
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000637 const auto p = mDrawingParent.promote();
Garfield Tande619fa2020-10-02 17:13:53 -0700638 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700639}
640
Mathias Agopian13127d82013-03-05 17:47:11 -0800641// ----------------------------------------------------------------------------
642// transaction
643// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800644
Marissa Wall61c58622018-07-18 10:12:20 -0700645uint32_t Layer::doTransaction(uint32_t flags) {
646 ATRACE_CALL();
647
Robert Carr0758e5d2021-03-11 22:15:04 -0800648 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700649 mDrawingStateModified = mDrawingState.modified;
650 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700651
Alec Mourib416efd2018-09-06 21:01:59 +0000652 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700653
Robert Carr6a160312021-05-17 12:08:20 -0700654 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800655 // invalidate and recompute the visible regions if needed
656 flags |= Layer::eVisibleRegion;
657 }
658
Robert Carr6a160312021-05-17 12:08:20 -0700659 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800660 // invalidate and recompute the visible regions if needed
Arthur Hung9ed43392022-05-27 06:31:57 +0000661 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800662 flags |= eVisibleRegion;
663 this->contentDirty = true;
664
665 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700666 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800667 }
668
Arthur Hung9ed43392022-05-27 06:31:57 +0000669 if (!mPotentialCursor && (flags & Layer::eVisibleRegion)) {
670 mFlinger->mUpdateInputInfo = true;
671 }
672
Robert Carr6a160312021-05-17 12:08:20 -0700673 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800674
Mathias Agopian13127d82013-03-05 17:47:11 -0800675 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676}
677
Robert Carr6a160312021-05-17 12:08:20 -0700678void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000679 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
680 // bufferSurfaceFrameTX will be presented in latchBuffer.
681 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
682 if (surfaceFrame->getPresentState() != PresentState::Presented) {
683 // With applyPendingStates, we could end up having presented surfaceframes from previous
684 // states
685 surfaceFrame->setPresentState(PresentState::Presented);
686 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
687 }
688 }
Robert Carr6a160312021-05-17 12:08:20 -0700689 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700690}
691
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700692uint32_t Layer::clearTransactionFlags(uint32_t mask) {
693 const auto flags = mTransactionFlags & mask;
694 mTransactionFlags &= ~mask;
695 return flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800696}
697
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700698void Layer::setTransactionFlags(uint32_t mask) {
699 mTransactionFlags |= mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800700}
701
Robert Carr1f0a16a2016-10-24 16:27:39 -0700702bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
703 ssize_t idx = mCurrentChildren.indexOf(childLayer);
704 if (idx < 0) {
705 return false;
706 }
707 if (childLayer->setLayer(z)) {
708 mCurrentChildren.removeAt(idx);
709 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800710 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700711 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800712 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700713}
714
Robert Carr503c7042017-09-27 15:06:08 -0700715bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
716 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
717 ssize_t idx = mCurrentChildren.indexOf(childLayer);
718 if (idx < 0) {
719 return false;
720 }
721 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
722 mCurrentChildren.removeAt(idx);
723 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800724 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700725 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800726 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700727}
728
Robert Carrae060832016-11-28 10:51:00 -0800729bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700730 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
731 mDrawingState.sequence++;
732 mDrawingState.z = z;
733 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700734
Robert Carra70e91c2021-06-11 13:59:52 -0700735 mFlinger->mSomeChildrenChanged = true;
736
Robert Carrdb66e622017-04-10 16:55:57 -0700737 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700738 if (mDrawingState.zOrderRelativeOf != nullptr) {
739 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700740 if (strongRelative != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700741 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700742 }
chaviw606e5cf2019-03-01 10:12:10 -0800743 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700744 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800745 setTransactionFlags(eTransactionNeeded);
746 return true;
747}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700748
Robert Carrdb66e622017-04-10 16:55:57 -0700749void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700750 mDrawingState.zOrderRelatives.remove(relative);
751 mDrawingState.sequence++;
752 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700753 setTransactionFlags(eTransactionNeeded);
754}
755
756void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700757 mDrawingState.zOrderRelatives.add(relative);
758 mDrawingState.modified = true;
759 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700760 setTransactionFlags(eTransactionNeeded);
761}
762
chaviw606e5cf2019-03-01 10:12:10 -0800763void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700764 mDrawingState.zOrderRelativeOf = relativeOf;
765 mDrawingState.sequence++;
766 mDrawingState.modified = true;
767 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700768
chaviw606e5cf2019-03-01 10:12:10 -0800769 setTransactionFlags(eTransactionNeeded);
770}
771
Robert Carr503d2bd2017-12-04 15:49:47 -0800772bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nair07e2a482022-10-18 19:18:16 +0000773 sp<Layer> relative = LayerHandle::getLayer(relativeToHandle);
Robert Carrdb66e622017-04-10 16:55:57 -0700774 if (relative == nullptr) {
775 return false;
776 }
777
Robert Carr6a160312021-05-17 12:08:20 -0700778 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
779 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800780 return false;
781 }
782
Robert Carr88b85e12022-03-21 15:47:35 -0700783 if (CC_UNLIKELY(relative->usingRelativeZ(LayerVector::StateSet::Drawing)) &&
784 (relative->mDrawingState.zOrderRelativeOf == this)) {
785 ALOGE("Detected relative layer loop between %s and %s",
786 mName.c_str(), relative->mName.c_str());
787 ALOGE("Ignoring new call to set relative layer");
788 return false;
789 }
790
Robert Carra70e91c2021-06-11 13:59:52 -0700791 mFlinger->mSomeChildrenChanged = true;
792
Robert Carr6a160312021-05-17 12:08:20 -0700793 mDrawingState.sequence++;
794 mDrawingState.modified = true;
795 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700796
Robert Carr6a160312021-05-17 12:08:20 -0700797 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700798 if (oldZOrderRelativeOf != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700799 oldZOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
chaviw9ab4bd12017-11-03 13:11:00 -0700800 }
chaviw606e5cf2019-03-01 10:12:10 -0800801 setZOrderRelativeOf(relative);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700802 relative->addZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700803
804 setTransactionFlags(eTransactionNeeded);
805
806 return true;
807}
808
Winson Chunga30f7c92021-06-29 15:42:56 -0700809bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
810 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
811 mDrawingState.isTrustedOverlay = isTrustedOverlay;
812 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +0000813 mFlinger->mUpdateInputInfo = true;
Winson Chunga30f7c92021-06-29 15:42:56 -0700814 setTransactionFlags(eTransactionNeeded);
815 return true;
816}
817
818bool Layer::isTrustedOverlay() const {
819 if (getDrawingState().isTrustedOverlay) {
820 return true;
821 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000822 const auto& p = mDrawingParent.promote();
Winson Chunga30f7c92021-06-29 15:42:56 -0700823 return (p != nullptr) && p->isTrustedOverlay();
824}
825
Dan Stoza9e56aa02015-11-02 13:00:03 -0800826bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700827 if (mDrawingState.color.a == alpha) return false;
828 mDrawingState.sequence++;
829 mDrawingState.color.a = alpha;
830 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800831 setTransactionFlags(eTransactionNeeded);
832 return true;
833}
chaviw13fdc492017-06-27 12:40:18 -0700834
Valerie Haudd0b7572019-01-29 14:59:27 -0800835bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700836 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700837 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800838 }
Robert Carr6a160312021-05-17 12:08:20 -0700839 mDrawingState.sequence++;
840 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800841 setTransactionFlags(eTransactionNeeded);
842
Robert Carr6a160312021-05-17 12:08:20 -0700843 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800844 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800845 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700846 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700847 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800848 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700849 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700850
Valerie Haudd0b7572019-01-29 14:59:27 -0800851 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700852 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800853 mFlinger->mLayersAdded = true;
854 // set up SF to handle added color layer
855 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700856 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800857 }
858 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700859 } else if (mDrawingState.bgColorLayer && alpha == 0) {
860 mDrawingState.bgColorLayer->reparent(nullptr);
861 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800862 return true;
863 }
864
Robert Carr6a160312021-05-17 12:08:20 -0700865 mDrawingState.bgColorLayer->setColor(color);
866 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
867 mDrawingState.bgColorLayer->setAlpha(alpha);
868 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800869
chaviw13fdc492017-06-27 12:40:18 -0700870 return true;
871}
872
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700873bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700874 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700875
Robert Carr6a160312021-05-17 12:08:20 -0700876 mDrawingState.sequence++;
877 mDrawingState.cornerRadius = cornerRadius;
878 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700879 setTransactionFlags(eTransactionNeeded);
880 return true;
881}
882
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800883bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700884 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Vishnu Nairb801a982021-11-02 15:12:08 -0700885 // If we start or stop drawing blur then the layer's visibility state may change so increment
886 // the magic sequence number.
887 if (mDrawingState.backgroundBlurRadius == 0 || backgroundBlurRadius == 0) {
888 mDrawingState.sequence++;
889 }
Robert Carr6a160312021-05-17 12:08:20 -0700890 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
891 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800892 setTransactionFlags(eTransactionNeeded);
893 return true;
894}
Marissa Wall61c58622018-07-18 10:12:20 -0700895
Mathias Agopian13127d82013-03-05 17:47:11 -0800896bool Layer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000897 mDrawingState.sequence++;
898 mDrawingState.transparentRegionHint = transparent;
Robert Carr6a160312021-05-17 12:08:20 -0700899 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800900 setTransactionFlags(eTransactionNeeded);
901 return true;
902}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100903
Lucas Dupinc3800b82020-10-02 16:24:48 -0700904bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Vishnu Nairb801a982021-11-02 15:12:08 -0700905 // If we start or stop drawing blur then the layer's visibility state may change so increment
906 // the magic sequence number.
907 if (mDrawingState.blurRegions.size() == 0 || blurRegions.size() == 0) {
908 mDrawingState.sequence++;
909 }
Robert Carr6a160312021-05-17 12:08:20 -0700910 mDrawingState.blurRegions = blurRegions;
911 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700912 setTransactionFlags(eTransactionNeeded);
913 return true;
914}
915
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800916bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700917 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
918 if (mDrawingState.flags == newFlags) return false;
919 mDrawingState.sequence++;
920 mDrawingState.flags = newFlags;
921 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800922 setTransactionFlags(eTransactionNeeded);
923 return true;
924}
Robert Carr99e27f02016-06-16 15:18:02 -0700925
chaviw25714502021-02-11 10:01:08 -0800926bool Layer::setCrop(const Rect& crop) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000927 if (mDrawingState.crop == crop) return false;
Robert Carr6a160312021-05-17 12:08:20 -0700928 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700929 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700930
Robert Carr6a160312021-05-17 12:08:20 -0700931 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800932 setTransactionFlags(eTransactionNeeded);
933 return true;
934}
Robert Carr8d5227b2017-03-16 15:41:03 -0700935
Evan Roskyef876c92019-01-25 17:46:06 -0800936bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700937 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
938 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -0800939 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800940 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500941}
942
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700943bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -0700944 if (mDrawingState.layerStack == layerStack) return false;
945 mDrawingState.sequence++;
946 mDrawingState.layerStack = layerStack;
947 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800948 setTransactionFlags(eTransactionNeeded);
949 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700950}
951
Peiyong Linc502cb72019-03-01 15:00:23 -0800952bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -0700953 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800954 return false;
955 }
Robert Carr6a160312021-05-17 12:08:20 -0700956 mDrawingState.sequence++;
957 mDrawingState.colorSpaceAgnostic = agnostic;
958 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -0800959 setTransactionFlags(eTransactionNeeded);
960 return true;
961}
962
Sally Qi81d95e62022-03-21 19:41:33 -0700963bool Layer::setDimmingEnabled(const bool dimmingEnabled) {
964 if (mDrawingState.dimmingEnabled == dimmingEnabled) return false;
965
966 mDrawingState.sequence++;
967 mDrawingState.dimmingEnabled = dimmingEnabled;
968 mDrawingState.modified = true;
969 setTransactionFlags(eTransactionNeeded);
970 return true;
971}
972
Ana Krulecc84d09b2019-11-02 23:10:29 +0100973bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -0700974 if (mDrawingState.frameRateSelectionPriority == priority) return false;
975 mDrawingState.frameRateSelectionPriority = priority;
976 mDrawingState.sequence++;
977 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100978 setTransactionFlags(eTransactionNeeded);
979 return true;
980}
981
982int32_t Layer::getFrameRateSelectionPriority() const {
983 // Check if layer has priority set.
984 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
985 return mDrawingState.frameRateSelectionPriority;
986 }
987 // If not, search whether its parents have it set.
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000988 sp<Layer> parent = getParent();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100989 if (parent != nullptr) {
990 return parent->getFrameRateSelectionPriority();
991 }
992
993 return Layer::PRIORITY_UNSET;
994}
995
Andy Labrada096227e2022-06-15 16:58:11 +0000996bool Layer::setDefaultFrameRateCompatibility(FrameRateCompatibility compatibility) {
997 if (mDrawingState.defaultFrameRateCompatibility == compatibility) return false;
998 mDrawingState.defaultFrameRateCompatibility = compatibility;
999 mDrawingState.modified = true;
1000 mFlinger->mScheduler->setDefaultFrameRateCompatibility(this);
1001 setTransactionFlags(eTransactionNeeded);
1002 return true;
1003}
1004
1005scheduler::LayerInfo::FrameRateCompatibility Layer::getDefaultFrameRateCompatibility() const {
1006 return mDrawingState.defaultFrameRateCompatibility;
1007}
1008
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001009bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1010 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1011};
1012
Vishnu Nair73908d12022-10-24 21:46:42 -07001013ui::LayerStack Layer::getLayerStack(LayerVector::StateSet state) const {
1014 bool useDrawing = state == LayerVector::StateSet::Drawing;
1015 const auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1016 if (parent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001017 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001018 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001019 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001020}
1021
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001022bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001023 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001024 return false;
1025 }
1026
Robert Carr6a160312021-05-17 12:08:20 -07001027 mDrawingState.sequence++;
1028 mDrawingState.shadowRadius = shadowRadius;
1029 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001030 setTransactionFlags(eTransactionNeeded);
1031 return true;
1032}
1033
Vishnu Nair6213bd92020-05-08 17:42:25 -07001034bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001035 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001036 return false;
1037 }
1038
Robert Carr6a160312021-05-17 12:08:20 -07001039 mDrawingState.sequence++;
1040 mDrawingState.fixedTransformHint = fixedTransformHint;
1041 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001042 setTransactionFlags(eTransactionNeeded);
1043 return true;
1044}
1045
John Reckcdb4ed72021-02-04 13:39:33 -05001046bool Layer::setStretchEffect(const StretchEffect& effect) {
1047 StretchEffect temp = effect;
1048 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001049 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001050 return false;
1051 }
Robert Carr6a160312021-05-17 12:08:20 -07001052 mDrawingState.sequence++;
1053 mDrawingState.stretchEffect = temp;
1054 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001055 setTransactionFlags(eTransactionNeeded);
1056 return true;
1057}
1058
John Reckc00c6692021-02-16 11:37:33 -05001059StretchEffect Layer::getStretchEffect() const {
1060 if (mDrawingState.stretchEffect.hasEffect()) {
1061 return mDrawingState.stretchEffect;
1062 }
1063
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001064 sp<Layer> parent = getParent();
John Reckc00c6692021-02-16 11:37:33 -05001065 if (parent != nullptr) {
1066 auto effect = parent->getStretchEffect();
1067 if (effect.hasEffect()) {
1068 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1069 return effect;
1070 }
1071 }
1072 return StretchEffect{};
1073}
1074
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001075bool Layer::enableBorder(bool shouldEnable, float width, const half4& color) {
1076 if (mBorderEnabled == shouldEnable && mBorderWidth == width && mBorderColor == color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00001077 return false;
1078 }
1079 mBorderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001080 mBorderWidth = width;
1081 mBorderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001082 return true;
1083}
1084
1085bool Layer::isBorderEnabled() {
1086 return mBorderEnabled;
1087}
1088
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001089float Layer::getBorderWidth() {
1090 return mBorderWidth;
1091}
1092
1093const half4& Layer::getBorderColor() {
1094 return mBorderColor;
1095}
1096
Ady Abrahama850c182021-08-04 13:04:37 -07001097bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1098 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1099 const auto frameRate = [&] {
1100 if (mDrawingState.frameRate.rate.isValid() ||
1101 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1102 return mDrawingState.frameRate;
1103 }
1104
1105 return parentFrameRate;
1106 }();
1107
1108 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1109
1110 // The frame rate is propagated to the children
1111 bool childrenHaveFrameRate = false;
1112 for (const sp<Layer>& child : mCurrentChildren) {
1113 childrenHaveFrameRate |=
1114 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1115 }
1116
1117 // If we don't have a valid frame rate, but the children do, we set this
1118 // layer as NoVote to allow the children to control the refresh rate
1119 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1120 childrenHaveFrameRate) {
1121 *transactionNeeded |=
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001122 setFrameRateForLayerTree(FrameRate(Fps(), FrameRateCompatibility::NoVote));
Ady Abrahama850c182021-08-04 13:04:37 -07001123 }
1124
1125 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1126 // the same reason we are allowing touch boost for those layers. See
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001127 // RefreshRateSelector::rankRefreshRates for details.
Ady Abrahama850c182021-08-04 13:04:37 -07001128 const auto layerVotedWithDefaultCompatibility =
1129 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1130 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1131 const auto layerVotedWithExactCompatibility =
1132 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1133 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1134 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1135}
1136
Ady Abraham60e42ea2020-03-09 19:17:31 -07001137void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001138 const auto root = [&]() -> sp<Layer> {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001139 sp<Layer> layer = sp<Layer>::fromExisting(this);
Ady Abrahama850c182021-08-04 13:04:37 -07001140 while (auto parent = layer->getParent()) {
1141 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001142 }
Ady Abrahama850c182021-08-04 13:04:37 -07001143 return layer;
1144 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001145
Ady Abraham60e42ea2020-03-09 19:17:31 -07001146 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001147 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001148
Ady Abrahama850c182021-08-04 13:04:37 -07001149 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001150 if (transactionNeeded) {
1151 mFlinger->setTransactionFlags(eTraversalNeeded);
1152 }
1153}
1154
Ady Abraham71c437d2020-01-31 15:56:57 -08001155bool Layer::setFrameRate(FrameRate frameRate) {
Robert Carr6a160312021-05-17 12:08:20 -07001156 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001157 return false;
1158 }
1159
Robert Carr6a160312021-05-17 12:08:20 -07001160 mDrawingState.sequence++;
1161 mDrawingState.frameRate = frameRate;
1162 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001163
1164 updateTreeHasFrameRateVote();
1165
Steven Thomas3172e202020-01-06 19:25:30 -08001166 setTransactionFlags(eTransactionNeeded);
1167 return true;
1168}
1169
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001170void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1171 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001172 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001173
1174 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1175 // there are two transactions with the same token, the first one without a buffer and the
1176 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1177 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001178 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1179 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001180 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001181 mDrawingState.bufferSurfaceFrameTX = it->second;
1182 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1183 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1184 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001185 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001186 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001187 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1188 }
1189}
1190
1191void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1192 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001193 mDrawingState.frameTimelineInfo = info;
1194 mDrawingState.postTime = postTime;
1195 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001196 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001197
Robert Carr6a160312021-05-17 12:08:20 -07001198 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001199 bufferSurfaceFrameTX != nullptr) {
1200 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1201 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1202 // being used for BufferSurfaceFrame, don't create a new one.
1203 return;
1204 }
1205 }
1206 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1207 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1208 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001209 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1210 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001211 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001212 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001213 } else {
1214 if (it->second->getPresentState() == PresentState::Presented) {
1215 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1216 // have been from previous vsync.
1217 it->second = createSurfaceFrameForTransaction(info, postTime);
1218 }
1219 }
1220}
1221
1222void Layer::addSurfaceFrameDroppedForBuffer(
1223 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001224 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001225 surfaceFrame->setPresentState(PresentState::Dropped);
1226 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1227}
1228
1229void Layer::addSurfaceFramePresentedForBuffer(
1230 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1231 nsecs_t currentLatchTime) {
1232 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1233 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1234 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1235 mLastLatchTime = currentLatchTime;
1236}
1237
1238std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1239 const FrameTimelineInfo& info, nsecs_t postTime) {
1240 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001241 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1242 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001243 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001244 /*isBuffer*/ false, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001245 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001246 // For Transactions, the post time is considered to be both queue and acquire fence time.
1247 surfaceFrame->setActualQueueTime(postTime);
1248 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001249 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1250 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001251 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001252 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001253 onSurfaceFrameCreated(surfaceFrame);
1254 return surfaceFrame;
1255}
1256
1257std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1258 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1259 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001260 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001261 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001262 /*isBuffer*/ true, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001263 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001264 // For buffers, acquire fence time will set during latch.
1265 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001266 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1267 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001268 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001269 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001270 onSurfaceFrameCreated(surfaceFrame);
1271 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001272}
1273
Ady Abrahama850c182021-08-04 13:04:37 -07001274bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1275 if (mDrawingState.frameRateForLayerTree == frameRate) {
1276 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001277 }
1278
Ady Abrahama850c182021-08-04 13:04:37 -07001279 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001280
Ady Abrahama850c182021-08-04 13:04:37 -07001281 // TODO(b/195668952): we probably don't need to dirty visible regions here
1282 // or even store frameRateForLayerTree in mDrawingState
1283 mDrawingState.sequence++;
1284 mDrawingState.modified = true;
1285 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001286
Dominik Laskowski068173d2021-08-11 17:22:59 -07001287 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
1288 mFlinger->mScheduler->recordLayerHistory(this, systemTime(), LayerUpdateType::SetFrameRate);
Ady Abrahama850c182021-08-04 13:04:37 -07001289
1290 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001291}
1292
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001293Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1294 return getDrawingState().frameRateForLayerTree;
1295}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296
Robert Carr1f0a16a2016-10-24 16:27:39 -07001297bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001298 const State& s(mDrawingState);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001299 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001300 if (parent != nullptr && parent->isHiddenByPolicy()) {
1301 return true;
1302 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001303 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1304 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1305 if (zOrderRelativeOf != nullptr) {
1306 if (zOrderRelativeOf->isHiddenByPolicy()) {
1307 return true;
1308 }
1309 }
1310 }
Garfield Tan2c1782c2022-02-16 15:25:05 -08001311 if (CC_UNLIKELY(!isTransformValid())) {
1312 ALOGW("Hide layer %s because it has invalid transformation.", getDebugName());
1313 return true;
1314 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001315 return s.flags & layer_state_t::eLayerHidden;
1316}
1317
David Sodman41fdfc92017-11-06 16:09:56 -08001318uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001319 // TODO: should we do something special if mSecure is set?
1320 if (mProtectedByApp) {
1321 // need a hardware-protected path to external video sink
1322 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001323 }
Riley Andrews03414a12014-07-01 14:22:59 -07001324 if (mPotentialCursor) {
1325 usage |= GraphicBuffer::USAGE_CURSOR;
1326 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001327 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001328 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001329}
1330
Vishnu Nair71fcf912022-10-18 09:14:20 -07001331void Layer::skipReportingTransformHint() {
1332 mSkipReportingTransformHint = true;
1333}
1334
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001335void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1336 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1337 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001338 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001339
Vishnu Nair6213bd92020-05-08 17:42:25 -07001340 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001341}
1342
Mathias Agopian13127d82013-03-05 17:47:11 -08001343// ----------------------------------------------------------------------------
1344// debugging
1345// ----------------------------------------------------------------------------
1346
Marissa Wall61c58622018-07-18 10:12:20 -07001347// TODO(marissaw): add new layer state info to layer debugging
Huihong Luo05539a12022-02-23 10:29:40 -08001348gui::LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001349 using namespace std::string_literals;
1350
Huihong Luo05539a12022-02-23 10:29:40 -08001351 gui::LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001352 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001353 info.mName = getName();
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001354 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001355 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001356 info.mType = getType();
Lloyd Piquea2468662019-03-07 21:31:06 -08001357
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001358 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001359 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001360 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001361 info.mX = ds.transform.tx();
1362 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001363 info.mZ = ds.z;
chaviw25714502021-02-11 10:01:08 -08001364 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001365 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001366 info.mFlags = ds.flags;
1367 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001368 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001369 info.mMatrix[0][0] = ds.transform[0][0];
1370 info.mMatrix[0][1] = ds.transform[0][1];
1371 info.mMatrix[1][0] = ds.transform[1][0];
1372 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001373 {
chaviwd62d3062019-09-04 14:48:02 -07001374 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001375 if (buffer != 0) {
1376 info.mActiveBufferWidth = buffer->getWidth();
1377 info.mActiveBufferHeight = buffer->getHeight();
1378 info.mActiveBufferStride = buffer->getStride();
1379 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001380 } else {
1381 info.mActiveBufferWidth = 0;
1382 info.mActiveBufferHeight = 0;
1383 info.mActiveBufferStride = 0;
1384 info.mActiveBufferFormat = 0;
1385 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001386 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001387 info.mNumQueuedFrames = getQueuedFrameCount();
Kalle Raitaa099a242017-01-11 11:17:29 -08001388 info.mIsOpaque = isOpaque(ds);
1389 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001390 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001391 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001392}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001393
Yiwei Zhang5434a782018-12-05 18:06:32 -08001394void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001395 result.append(kDumpTableRowLength, '-');
1396 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001397 result.append(" Layer name\n");
1398 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001399 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001400 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001401 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001402 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001403 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001404 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1405 result.append(kDumpTableRowLength, '-');
1406 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001407}
1408
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001409void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1410 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001411 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001412 return;
1413 }
1414
Yiwei Zhang5434a782018-12-05 18:06:32 -08001415 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001416 if (mName.length() > 77) {
1417 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001418 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001419 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001420 shortened.append(mName, mName.length() - 36);
1421 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001422 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001423 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001424 }
1425
Yiwei Zhang5434a782018-12-05 18:06:32 -08001426 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001427
Alec Mourib416efd2018-09-06 21:01:59 +00001428 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001429 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001430
Chia-I Wu1e043612018-03-01 09:45:09 -08001431 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001432 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001433 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001434 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001435 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001436 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001437 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001438 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1439 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001440 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001441 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001442 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001443 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001444 const auto frameRate = getFrameRateForLayerTree();
1445 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1446 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001447 ftl::enum_string(frameRate.type).c_str(),
1448 ftl::enum_string(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001449 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001450 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001451 }
Dan Stozae22aec72016-08-01 13:20:59 -07001452
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001453 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1454 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1455
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001456 result.append(kDumpTableRowLength, '-');
1457 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001458}
Dan Stozae22aec72016-08-01 13:20:59 -07001459
Yiwei Zhang5434a782018-12-05 18:06:32 -08001460void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001461 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001462}
1463
Svetoslavd85084b2014-03-20 10:28:31 -07001464void Layer::clearFrameStats() {
1465 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001466}
1467
Jamie Gennis6547ff42013-07-16 20:12:42 -07001468void Layer::logFrameStats() {
1469 mFrameTracker.logAndResetStats(mName);
1470}
1471
Svetoslavd85084b2014-03-20 10:28:31 -07001472void Layer::getFrameStats(FrameStats* outStats) const {
1473 mFrameTracker.getStats(outStats);
1474}
1475
Vishnu Nair0f085c62019-08-30 08:49:12 -07001476void Layer::dumpCallingUidPid(std::string& result) const {
Vishnu Naircb8be502022-10-12 19:03:23 +00001477 StringAppendF(&result, "Layer %s (%s) ownerPid:%d ownerUid:%d\n", getName().c_str(), getType(),
1478 mOwnerPid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001479}
1480
Brian Anderson5ea5e592016-12-01 16:54:33 -08001481void Layer::onDisconnect() {
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001482 const int32_t layerId = getSequence();
1483 mFlinger->mTimeStats->onDestroy(layerId);
1484 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001485}
1486
Chia-I Wu98f1c102017-05-30 14:54:08 -07001487size_t Layer::getChildrenCount() const {
1488 size_t count = 0;
1489 for (const sp<Layer>& child : mCurrentChildren) {
1490 count += 1 + child->getChildrenCount();
1491 }
1492 return count;
1493}
1494
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001495void Layer::setGameModeForTree(GameMode gameMode) {
1496 const auto& currentState = getDrawingState();
Huihong Luod3d8f8e2022-03-08 14:48:46 -08001497 if (currentState.metadata.has(gui::METADATA_GAME_MODE)) {
1498 gameMode =
1499 static_cast<GameMode>(currentState.metadata.getInt32(gui::METADATA_GAME_MODE, 0));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001500 }
1501 setGameMode(gameMode);
1502 for (const sp<Layer>& child : mCurrentChildren) {
1503 child->setGameModeForTree(gameMode);
1504 }
1505}
1506
Robert Carr1f0a16a2016-10-24 16:27:39 -07001507void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001508 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001509 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001510
Robert Carr1f0a16a2016-10-24 16:27:39 -07001511 mCurrentChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07001512 layer->setParent(sp<Layer>::fromExisting(this));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001513 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001514 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001515}
1516
1517ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001518 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001519 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001520
Robert Carr1323c952019-01-28 18:13:27 -08001521 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001522 const auto removeResult = mCurrentChildren.remove(layer);
1523
1524 updateTreeHasFrameRateVote();
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001525 layer->setGameModeForTree(GameMode::Unsupported);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001526 layer->updateTreeHasFrameRateVote();
1527
1528 return removeResult;
1529}
1530
Robert Carr15eae092018-03-23 13:43:53 -07001531void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001532 for (const sp<Layer>& child : mDrawingChildren) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001533 child->mDrawingParent = newParent;
xinying1f4200e62022-04-26 14:41:30 +08001534 const float parentShadowRadius =
1535 newParent->canDrawShadows() ? 0.f : newParent->mEffectiveShadowRadius;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001536 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
xinying1f4200e62022-04-26 14:41:30 +08001537 parentShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001538 }
1539}
1540
chaviwf1961f72017-09-18 16:41:07 -07001541bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001542 sp<Layer> newParent;
1543 if (newParentHandle != nullptr) {
Vishnu Nair07e2a482022-10-18 19:18:16 +00001544 newParent = LayerHandle::getLayer(newParentHandle);
Robert Carr54cf5b12019-01-25 14:02:28 -08001545 if (newParent == nullptr) {
1546 ALOGE("Unable to promote Layer handle");
1547 return false;
1548 }
1549 if (newParent == this) {
1550 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1551 return false;
1552 }
1553 }
1554
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001555 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001556 if (parent != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001557 parent->removeChild(sp<Layer>::fromExisting(this));
chaviw06178942017-07-27 10:25:59 -07001558 }
1559
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001560 if (newParentHandle != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001561 newParent->addChild(sp<Layer>::fromExisting(this));
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001562 if (!newParent->isRemovedFromCurrentState()) {
1563 addToCurrentState();
1564 } else {
1565 onRemovedFromCurrentState();
1566 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001567 } else {
1568 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001569 }
1570
chaviw06178942017-07-27 10:25:59 -07001571 return true;
1572}
1573
Peiyong Lind3788632018-09-18 16:01:31 -07001574bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001575 static const mat4 identityMatrix = mat4();
1576
Robert Carr6a160312021-05-17 12:08:20 -07001577 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001578 return false;
1579 }
Robert Carr6a160312021-05-17 12:08:20 -07001580 ++mDrawingState.sequence;
1581 mDrawingState.colorTransform = matrix;
1582 mDrawingState.hasColorTransform = matrix != identityMatrix;
1583 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001584 setTransactionFlags(eTransactionNeeded);
1585 return true;
1586}
1587
chaviwf66724d2018-11-28 16:35:21 -08001588mat4 Layer::getColorTransform() const {
1589 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001590 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001591 colorTransform = parent->getColorTransform() * colorTransform;
1592 }
1593 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001594}
1595
1596bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001597 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001598 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001599 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1600 }
1601 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001602}
1603
Chia-I Wu11481472018-05-04 10:43:19 -07001604bool Layer::isLegacyDataSpace() const {
1605 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001606 return !(getDataSpace() &
1607 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1608 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001609}
1610
Robert Carr1f0a16a2016-10-24 16:27:39 -07001611void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001612 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001613}
1614
Robert Carr6a160312021-05-17 12:08:20 -07001615int32_t Layer::getZ(LayerVector::StateSet) const {
1616 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001617}
1618
Robert Carr1c5481e2019-07-01 14:42:27 -07001619bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001620 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001621 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001622 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001623}
1624
David Sodman41fdfc92017-11-06 16:09:56 -08001625__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001626 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001627 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1628 "makeTraversalList received invalid stateSet");
1629 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1630 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001631 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001632
Robert Carr29abff82017-12-04 13:51:20 -08001633 if (state.zOrderRelatives.size() == 0) {
1634 *outSkipRelativeZUsers = true;
1635 return children;
1636 }
1637
chaviwfd462612018-05-31 16:11:27 -07001638 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001639 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001640 sp<Layer> strongRelative = weakRelative.promote();
1641 if (strongRelative != nullptr) {
1642 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001643 }
1644 }
1645
Dan Stoza412903f2017-04-27 13:42:17 -07001646 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001647 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001648 continue;
1649 }
Robert Carrdb66e622017-04-10 16:55:57 -07001650 traverse.add(child);
1651 }
1652
1653 return traverse;
1654}
1655
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656/**
Robert Carrdb66e622017-04-10 16:55:57 -07001657 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001658 */
Dan Stoza412903f2017-04-27 13:42:17 -07001659void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001660 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1661 // produce a new list for traversing, including our relatives, and not including our children
1662 // who are relatives of another surface. In the case that there are no relative Z,
1663 // makeTraversalList returns our children directly to avoid significant overhead.
1664 // However in this case we need to take the responsibility for filtering children which
1665 // are relatives of another surface here.
1666 bool skipRelativeZUsers = false;
1667 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001668
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001670 for (; i < list.size(); i++) {
1671 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001672 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1673 continue;
1674 }
1675
chaviw301b1d82019-11-06 13:15:09 -08001676 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001678 }
Dan Stoza412903f2017-04-27 13:42:17 -07001679 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001680 }
Robert Carr29abff82017-12-04 13:51:20 -08001681
Dan Stoza412903f2017-04-27 13:42:17 -07001682 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001683 for (; i < list.size(); i++) {
1684 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001685
Robert Carr29abff82017-12-04 13:51:20 -08001686 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1687 continue;
1688 }
Dan Stoza412903f2017-04-27 13:42:17 -07001689 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001690 }
1691}
1692
1693/**
Robert Carrdb66e622017-04-10 16:55:57 -07001694 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695 */
Dan Stoza412903f2017-04-27 13:42:17 -07001696void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1697 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001698 // See traverseInZOrder for documentation.
1699 bool skipRelativeZUsers = false;
1700 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001701
Robert Carr1f0a16a2016-10-24 16:27:39 -07001702 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001703 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001704 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001705
1706 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1707 continue;
1708 }
1709
chaviw301b1d82019-11-06 13:15:09 -08001710 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711 break;
1712 }
Dan Stoza412903f2017-04-27 13:42:17 -07001713 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001714 }
Dan Stoza412903f2017-04-27 13:42:17 -07001715 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001716 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001717 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001718
1719 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1720 continue;
1721 }
1722
Dan Stoza412903f2017-04-27 13:42:17 -07001723 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001724 }
1725}
1726
Edgar Arriaga844fa672020-01-16 14:21:42 -08001727void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1728 visitor(this);
1729 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001730 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001731 for (const sp<Layer>& child : children) {
1732 child->traverse(state, visitor);
1733 }
1734}
1735
chaviw4b129c22018-04-09 16:19:43 -07001736LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1737 const std::vector<Layer*>& layersInTree) {
1738 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1739 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001740 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1741 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001742 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001743
chaviwfd462612018-05-31 16:11:27 -07001744 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001745 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1746 sp<Layer> strongRelative = weakRelative.promote();
1747 // Only add relative layers that are also descendents of the top most parent of the tree.
1748 // If a relative layer is not a descendent, then it should be ignored.
1749 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1750 traverse.add(strongRelative);
1751 }
1752 }
1753
1754 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001755 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001756 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1757 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1758 // the child here since it won't be added later as a relative.
1759 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1760 childState.zOrderRelativeOf.promote().get())) {
1761 continue;
1762 }
1763 traverse.add(child);
1764 }
1765
1766 return traverse;
1767}
1768
1769void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1770 LayerVector::StateSet stateSet,
1771 const LayerVector::Visitor& visitor) {
1772 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001773
1774 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001775 for (; i < list.size(); i++) {
1776 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001777 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001778 break;
1779 }
chaviw4b129c22018-04-09 16:19:43 -07001780 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001781 }
chaviw4b129c22018-04-09 16:19:43 -07001782
chaviwa76b2712017-09-20 12:02:26 -07001783 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001784 for (; i < list.size(); i++) {
1785 const auto& relative = list[i];
1786 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001787 }
1788}
1789
chaviw4b129c22018-04-09 16:19:43 -07001790std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1791 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1792 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1793
1794 std::vector<Layer*> layersInTree = {this};
1795 for (size_t i = 0; i < children.size(); i++) {
1796 const auto& child = children[i];
1797 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1798 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1799 }
1800
1801 return layersInTree;
1802}
1803
1804void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1805 const LayerVector::Visitor& visitor) {
1806 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1807 std::sort(layersInTree.begin(), layersInTree.end());
1808 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1809}
1810
Peiyong Linefefaac2018-08-17 12:27:51 -07001811ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001812 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001813}
1814
Garfield Tan2c1782c2022-02-16 15:25:05 -08001815bool Layer::isTransformValid() const {
1816 float transformDet = getTransform().det();
1817 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
1818}
1819
chaviw13fdc492017-06-27 12:40:18 -07001820half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001821 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001822
chaviw13fdc492017-06-27 12:40:18 -07001823 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1824 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001825}
Robert Carr6452f122017-03-21 10:41:29 -07001826
Vishnu Nair6213bd92020-05-08 17:42:25 -07001827ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001828 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001829 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1830 return fixedTransformHint;
1831 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001832 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001833 if (!p) return fixedTransformHint;
1834 return p->getFixedTransformHint();
1835}
1836
chaviw13fdc492017-06-27 12:40:18 -07001837half4 Layer::getColor() const {
1838 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001839 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001840}
Robert Carr6452f122017-03-21 10:41:29 -07001841
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001842int32_t Layer::getBackgroundBlurRadius() const {
Vishnu Nair29810f72022-07-01 16:38:41 +00001843 if (getDrawingState().backgroundBlurRadius == 0) {
1844 return 0;
1845 }
Galia Peychevada7de0e2020-12-03 17:24:35 +01001846
Vishnu Nair29810f72022-07-01 16:38:41 +00001847 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001848 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1849 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001850}
1851
Galia Peychevae0acf382021-04-12 21:22:34 +02001852const std::vector<BlurRegion> Layer::getBlurRegions() const {
1853 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001854 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001855 for (auto& region : regionsCopy) {
1856 region.alpha = region.alpha * layerAlpha;
1857 }
1858 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001859}
1860
Vishnu Naire14c6b32022-08-06 04:20:15 +00001861RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001862 // Get parent settings
1863 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001864 const auto& parent = mDrawingParent.promote();
1865 if (parent != nullptr) {
1866 parentSettings = parent->getRoundedCornerState();
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001867 if (parentSettings.hasRoundedCorners()) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001868 ui::Transform t = getActiveTransform(getDrawingState());
1869 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001870 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001871 parentSettings.radius.x *= t.getScaleX();
1872 parentSettings.radius.y *= t.getScaleY();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001873 }
1874 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001875
1876 // Get layer settings
1877 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001878 const vec2 radius(getDrawingState().cornerRadius, getDrawingState().cornerRadius);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001879 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001880 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001881
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001882 if (layerSettingsValid && parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001883 // If the parent and the layer have rounded corner settings, use the parent settings if the
1884 // parent crop is entirely inside the layer crop.
1885 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1886 if (parentSettings.cropRect.left > layerCropRect.left &&
1887 parentSettings.cropRect.top > layerCropRect.top &&
1888 parentSettings.cropRect.right < layerCropRect.right &&
1889 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1890 return parentSettings;
1891 } else {
1892 return layerSettings;
1893 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001894 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001895 return layerSettings;
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001896 } else if (parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001897 return parentSettings;
1898 }
1899 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001900}
1901
Robert Carr88b85e12022-03-21 15:47:35 -07001902bool Layer::findInHierarchy(const sp<Layer>& l) {
1903 if (l == this) {
1904 return true;
1905 }
1906 for (auto& child : mDrawingChildren) {
1907 if (child->findInHierarchy(l)) {
1908 return true;
1909 }
1910 }
1911 return false;
1912}
1913
Robert Carr1f0a16a2016-10-24 16:27:39 -07001914void Layer::commitChildList() {
1915 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1916 const auto& child = mCurrentChildren[i];
1917 child->commitChildList();
1918 }
1919 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001920 mDrawingParent = mCurrentParent;
Robert Carr88b85e12022-03-21 15:47:35 -07001921 if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) {
1922 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1923 if (zOrderRelativeOf == nullptr) return;
1924 if (findInHierarchy(zOrderRelativeOf)) {
1925 ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(),
1926 zOrderRelativeOf->mName.c_str());
1927 ALOGE("Severing rel Z loop, potentially dangerous");
1928 mDrawingState.isRelativeOf = false;
Ady Abrahamd11bade2022-08-01 16:18:03 -07001929 zOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carr88b85e12022-03-21 15:47:35 -07001930 }
1931 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001932}
1933
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001934
chaviw3277faf2021-05-19 16:45:23 -05001935void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001936 mDrawingState.inputInfo = info;
Vishnu Nair07e2a482022-10-18 19:18:16 +00001937 mDrawingState.touchableRegionCrop =
1938 LayerHandle::getLayer(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07001939 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +00001940 mFlinger->mUpdateInputInfo = true;
Robert Carr720e5062018-07-30 17:45:14 -07001941 setTransactionFlags(eTransactionNeeded);
1942}
1943
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001944LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) {
chaviw08f3cb22020-01-13 13:17:21 -08001945 LayerProto* layerProto = layersProto.add_layers();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001946 writeToProtoDrawingState(layerProto);
chaviw08f3cb22020-01-13 13:17:21 -08001947 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1948
Vishnu Nair00b90132021-11-05 14:03:40 -07001949 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001950 ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread.
1951
Vishnu Nair60db8c02020-04-02 11:55:16 -07001952 // Only populate for the primary display.
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001953 if (const auto display = mFlinger->getDefaultDisplayDeviceLocked()) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05001954 const auto compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07001955 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001956 LayerProtoHelper::writeToProto(getVisibleRegion(display.get()),
1957 [&]() { return layerProto->mutable_visible_region(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07001958 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08001959 }
1960
chaviw08f3cb22020-01-13 13:17:21 -08001961 for (const sp<Layer>& layer : mDrawingChildren) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001962 layer->writeToProto(layersProto, traceFlags);
chaviw08f3cb22020-01-13 13:17:21 -08001963 }
chaviw6d89e2d2020-01-14 14:42:01 -08001964
1965 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08001966}
1967
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001968void Layer::writeToProtoDrawingState(LayerProto* layerInfo) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001969 const ui::Transform transform = getTransform();
Vishnu Naird37343b2022-01-12 16:18:56 -08001970 auto buffer = getExternalTexture();
Vishnu Nair00b90132021-11-05 14:03:40 -07001971 if (buffer != nullptr) {
Vishnu Naird37343b2022-01-12 16:18:56 -08001972 LayerProtoHelper::writeToProto(*buffer,
Vishnu Nair00b90132021-11-05 14:03:40 -07001973 [&]() { return layerInfo->mutable_active_buffer(); });
1974 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
1975 layerInfo->mutable_buffer_transform());
1976 }
1977 layerInfo->set_invalidate(contentDirty);
1978 layerInfo->set_is_protected(isProtected());
1979 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
1980 layerInfo->set_queued_frames(getQueuedFrameCount());
Vishnu Nair00b90132021-11-05 14:03:40 -07001981 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nair00b90132021-11-05 14:03:40 -07001982 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001983 layerInfo->set_corner_radius(
1984 (getRoundedCornerState().radius.x + getRoundedCornerState().radius.y) / 2.0);
Vishnu Nair00b90132021-11-05 14:03:40 -07001985 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
1986 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
1987 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
1988 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1989 [&]() { return layerInfo->mutable_position(); });
1990 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair00b90132021-11-05 14:03:40 -07001991 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1992 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07001993
Vishnu Nair00b90132021-11-05 14:03:40 -07001994 if (hasColorTransform()) {
1995 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07001996 }
1997
Vishnu Nair60db8c02020-04-02 11:55:16 -07001998 LayerProtoHelper::writeToProto(mSourceBounds,
1999 [&]() { return layerInfo->mutable_source_bounds(); });
2000 LayerProtoHelper::writeToProto(mScreenBounds,
2001 [&]() { return layerInfo->mutable_screen_bounds(); });
2002 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2003 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2004 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002005}
2006
2007void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002008 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002009 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2010 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002011 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002012
chaviw766c9c52021-02-10 17:36:47 -08002013 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002014
Vishnu Nair00b90132021-11-05 14:03:40 -07002015 layerInfo->set_id(sequence);
2016 layerInfo->set_name(getName().c_str());
2017 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002018
Vishnu Nair00b90132021-11-05 14:03:40 -07002019 for (const auto& child : children) {
2020 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002021 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002022
Vishnu Nair00b90132021-11-05 14:03:40 -07002023 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2024 sp<Layer> strongRelative = weakRelative.promote();
2025 if (strongRelative != nullptr) {
2026 layerInfo->add_relatives(strongRelative->sequence);
2027 }
2028 }
2029
Vishnu Nairea04b6f2022-08-19 21:28:17 +00002030 LayerProtoHelper::writeToProto(state.transparentRegionHint,
Vishnu Nair00b90132021-11-05 14:03:40 -07002031 [&]() { return layerInfo->mutable_transparent_region(); });
2032
2033 layerInfo->set_layer_stack(getLayerStack().id);
2034 layerInfo->set_z(state.z);
2035
2036 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2037 return layerInfo->mutable_requested_position();
2038 });
2039
Vishnu Nair00b90132021-11-05 14:03:40 -07002040 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2041
2042 layerInfo->set_is_opaque(isOpaque(state));
2043
2044 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2045 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2046 LayerProtoHelper::writeToProto(state.color,
2047 [&]() { return layerInfo->mutable_requested_color(); });
2048 layerInfo->set_flags(state.flags);
2049
2050 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2051 layerInfo->mutable_requested_transform());
2052
2053 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2054 if (parent != nullptr) {
2055 layerInfo->set_parent(parent->sequence);
2056 } else {
2057 layerInfo->set_parent(-1);
2058 }
2059
2060 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2061 if (zOrderRelativeOf != nullptr) {
2062 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2063 } else {
2064 layerInfo->set_z_order_relative_of(-1);
2065 }
2066
2067 layerInfo->set_is_relative_of(state.isRelativeOf);
2068
2069 layerInfo->set_owner_uid(mOwnerUid);
2070
Arthur Hung4cf2d8c2022-04-07 14:52:00 +00002071 if ((traceFlags & LayerTracing::TRACE_INPUT) && needsInputInfo()) {
chaviw3277faf2021-05-19 16:45:23 -05002072 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002073 if (useDrawing) {
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002074 info = fillInputInfo(
2075 InputDisplayArgs{.transform = &kIdentityTransform, .isSecure = true});
chaviw4c34a092020-07-08 11:30:06 -07002076 } else {
2077 info = state.inputInfo;
2078 }
2079
2080 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002081 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002082 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002083
Vishnu Nair00b90132021-11-05 14:03:40 -07002084 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002085 auto protoMap = layerInfo->mutable_metadata();
2086 for (const auto& entry : state.metadata.mMap) {
2087 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2088 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002089 }
Vishnu Naird2aaab12022-02-10 14:49:09 -08002090
2091 LayerProtoHelper::writeToProto(state.destinationFrame,
2092 [&]() { return layerInfo->mutable_destination_frame(); });
chaviw1d044282017-09-27 12:19:28 -07002093}
2094
Robert Carr2e102c92018-10-23 12:11:15 -07002095bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002096 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002097}
2098
Prabir Pradhan33da9462022-06-14 14:55:57 +00002099// Applies the given transform to the region, while protecting against overflows caused by any
2100// offsets. If applying the offset in the transform to any of the Rects in the region would result
2101// in an overflow, they are not added to the output Region.
2102static Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
2103 const std::string& debugWindowName) {
2104 // Round the translation using the same rounding strategy used by ui::Transform.
2105 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
2106 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
2107
2108 ui::Transform transformWithoutOffset = t;
2109 transformWithoutOffset.set(0.f, 0.f);
2110
2111 const Region transformed = transformWithoutOffset.transform(r);
2112
2113 // Apply the translation to each of the Rects in the region while discarding any that overflow.
2114 Region ret;
2115 for (const auto& rect : transformed) {
2116 Rect newRect;
2117 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
2118 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
2119 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
2120 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
2121 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
2122 debugWindowName.c_str());
2123 continue;
2124 }
2125 ret.orSelf(newRect);
2126 }
2127 return ret;
2128}
2129
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002130void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& screenToDisplay) {
2131 Rect tmpBounds = getInputBounds();
2132 if (!tmpBounds.isValid()) {
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08002133 info.touchableRegion.clear();
2134 // A layer could have invalid input bounds and still expect to receive touch input if it has
2135 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
2136 // correctly to determine the coordinate space for input events. Use an empty rect so that
2137 // the layer will receive input in its own layer space.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002138 tmpBounds = Rect::EMPTY_RECT;
chaviw7e72caf2020-12-02 16:50:43 -08002139 }
2140
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002141 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
2142 // frame and transform used for the layer, which determines the bounds and the coordinate space
2143 // within which the layer will receive input.
2144 //
2145 // The coordinate space within which each of the bounds are specified is explicitly documented
2146 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
2147 // Transform converts one coordinate space to another, which is apparent in its naming. For
2148 // example, "layerToDisplay" transforms layer space to display space.
2149 //
2150 // Coordinate space definitions:
2151 // - display: The display device's coordinate space. Correlates to pixels on the display.
2152 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
2153 // - layer: The coordinate space of this layer.
2154 // - input: The coordinate space in which this layer will receive input events. This could be
2155 // different than layer space if a surfaceInset is used, which changes the origin
2156 // of the input space.
2157 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
chaviw7e72caf2020-12-02 16:50:43 -08002158
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002159 // Clamp surface inset to the input bounds.
2160 const auto surfaceInset = static_cast<float>(info.surfaceInset);
2161 const float xSurfaceInset =
2162 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
2163 const float ySurfaceInset =
2164 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
chaviw1ff3d1e2020-07-01 15:53:47 -07002165
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002166 // Apply the insets to the input bounds.
2167 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
2168 inputBoundsInLayer.top + ySurfaceInset,
2169 inputBoundsInLayer.right - xSurfaceInset,
2170 inputBoundsInLayer.bottom - ySurfaceInset);
Ady Abraham282f1d72019-07-24 18:05:56 -07002171
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002172 // Crop the input bounds to ensure it is within the parent's bounds.
2173 const FloatRect croppedInsetBoundsInLayer = mBounds.intersect(insetBoundsInLayer);
Ady Abraham282f1d72019-07-24 18:05:56 -07002174
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002175 const ui::Transform layerToScreen = getInputTransform();
2176 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
Vishnu Nair8033a492018-12-05 07:27:23 -08002177
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002178 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
2179 info.frameLeft = roundedFrameInDisplay.left;
2180 info.frameTop = roundedFrameInDisplay.top;
2181 info.frameRight = roundedFrameInDisplay.right;
2182 info.frameBottom = roundedFrameInDisplay.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -07002183
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002184 ui::Transform inputToLayer;
2185 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
2186 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
chaviw39cfa2e2020-11-04 14:19:02 -08002187
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002188 // InputDispatcher expects a display-to-input transform.
2189 info.transform = inputToDisplay.inverse();
2190
2191 // The touchable region is specified in the input coordinate space. Change it to display space.
Prabir Pradhan33da9462022-06-14 14:55:57 +00002192 info.touchableRegion =
2193 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, mName);
chaviw7e72caf2020-12-02 16:50:43 -08002194}
2195
chaviw3277faf2021-05-19 16:45:23 -05002196void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002197 sp<Layer> p = sp<Layer>::fromExisting(this);
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002198 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002199 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002200 }
2201 if (p != nullptr) {
2202 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2203 }
2204}
2205
Vishnu Naira066d902021-09-13 18:40:17 -07002206gui::DropInputMode Layer::getDropInputMode() const {
2207 gui::DropInputMode mode = mDrawingState.dropInputMode;
2208 if (mode == gui::DropInputMode::ALL) {
2209 return mode;
2210 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002211 sp<Layer> parent = mDrawingParent.promote();
2212 if (parent) {
2213 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002214 if (parentMode != gui::DropInputMode::NONE) {
2215 return parentMode;
2216 }
2217 }
2218 return mode;
2219}
2220
2221void Layer::handleDropInputMode(gui::WindowInfo& info) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002222 if (mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Vishnu Naira066d902021-09-13 18:40:17 -07002223 return;
2224 }
2225
2226 // Check if we need to drop input unconditionally
2227 gui::DropInputMode dropInputMode = getDropInputMode();
2228 if (dropInputMode == gui::DropInputMode::ALL) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002229 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002230 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2231 return;
2232 }
2233
2234 // Check if we need to check if the window is obscured by parent
2235 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2236 return;
2237 }
2238
2239 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002240 sp<Layer> parent = mDrawingParent.promote();
2241 if (parent && parent->getAlpha() != 1.0_hf) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002242 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002243 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2244 static_cast<float>(getAlpha()));
2245 }
2246
2247 // Check if the parent has cropped the buffer
2248 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2249 if (!bufferSize.isValid()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002250 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002251 return;
2252 }
2253
2254 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2255 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2256 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2257 // match then the layer has not been cropped by its parents.
2258 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2259 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2260
2261 if (croppedByParent) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002262 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002263 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2264 getDebugName());
2265 } else {
2266 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2267 // input if the window is obscured. This check should be done in surfaceflinger but the
2268 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2269 // drop input events if the window is obscured.
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002270 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002271 }
2272}
2273
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002274WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) {
chaviw7e72caf2020-12-02 16:50:43 -08002275 if (!hasInputInfo()) {
2276 mDrawingState.inputInfo.name = getName();
2277 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2278 mDrawingState.inputInfo.ownerPid = mOwnerPid;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002279 mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002280 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002281 }
2282
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002283 const ui::Transform& displayTransform =
2284 displayArgs.transform != nullptr ? *displayArgs.transform : kIdentityTransform;
2285
chaviw3277faf2021-05-19 16:45:23 -05002286 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002287 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002288 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002289
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002290 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002291
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002292 if (displayArgs.transform == nullptr) {
2293 // Do not let the window receive touches if it is not associated with a valid display
2294 // transform. We still allow the window to receive keys and prevent ANRs.
2295 info.inputConfig |= WindowInfo::InputConfig::NOT_TOUCHABLE;
2296 }
2297
Robert Carr5dc426e2020-06-10 14:29:14 -07002298 // For compatibility reasons we let layers which can receive input
2299 // receive input before they have actually submitted a buffer. Because
2300 // of this we use canReceiveInput instead of isVisible to check the
2301 // policy-visibility, ignoring the buffer state. However for layers with
2302 // hasInputInfo()==false we can use the real visibility state.
2303 // We are just using these layers for occlusion detection in
2304 // InputDispatcher, and obviously if they aren't visible they can't occlude
2305 // anything.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002306 const bool visible = hasInputInfo() ? canReceiveInput() : isVisible();
2307 info.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
2308
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002309 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002310 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002311 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002312
Vishnu Nair16a938f2021-09-24 07:14:54 -07002313 // If the window will be blacked out on a display because the display does not have the secure
2314 // flag and the layer has the secure flag set, then drop input.
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002315 if (!displayArgs.isSecure && isSecure()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002316 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Nair16a938f2021-09-24 07:14:54 -07002317 }
2318
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002319 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2320 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002321 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2322 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002323 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002324 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002325 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002326 }
2327
Winson Chunga30f7c92021-06-29 15:42:56 -07002328 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2329 // if it was set by WM for a known system overlay
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002330 if (isTrustedOverlay()) {
2331 info.inputConfig |= WindowInfo::InputConfig::TRUSTED_OVERLAY;
2332 }
Winson Chunga30f7c92021-06-29 15:42:56 -07002333
chaviwaf87b3e2019-10-01 16:59:28 -07002334 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2335 // touches from going outside the cloned area.
2336 if (isClone()) {
chaviw8577ea82022-06-01 16:32:26 -05002337 info.inputConfig |= WindowInfo::InputConfig::CLONE;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002338 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2339 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002340 info.touchableRegion = info.touchableRegion.intersect(rect);
2341 }
2342 }
2343
Robert Carr720e5062018-07-30 17:45:14 -07002344 return info;
2345}
2346
chaviwaf87b3e2019-10-01 16:59:28 -07002347sp<Layer> Layer::getClonedRoot() {
2348 if (mClonedChild != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002349 return sp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002350 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002351 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002352 return nullptr;
2353 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002354 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002355}
2356
Robert Carredd13602020-04-13 17:24:34 -07002357bool Layer::hasInputInfo() const {
Linus Tufvessona1858822022-03-04 09:32:07 +00002358 return mDrawingState.inputInfo.token != nullptr ||
2359 mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Robert Carr720e5062018-07-30 17:45:14 -07002360}
2361
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002362compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002363 const DisplayDevice* display) const {
2364 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002365 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002366}
2367
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002368Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2369 const auto outputLayer = findOutputLayerForDisplay(display);
2370 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002371}
2372
chaviwb4c6e582019-08-16 14:35:07 -07002373void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002374 cloneDrawingState(clonedFrom.get());
chaviwb4c6e582019-08-16 14:35:07 -07002375 mClonedFrom = clonedFrom;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002376 mPremultipliedAlpha = clonedFrom->mPremultipliedAlpha;
2377 mPotentialCursor = clonedFrom->mPotentialCursor;
2378 mProtectedByApp = clonedFrom->mProtectedByApp;
2379 updateCloneBufferInfo();
2380}
2381
2382void Layer::updateCloneBufferInfo() {
2383 if (!isClone() || !isClonedFromAlive()) {
2384 return;
2385 }
2386
2387 sp<Layer> clonedFrom = getClonedFrom();
2388 mBufferInfo = clonedFrom->mBufferInfo;
2389 mSidebandStream = clonedFrom->mSidebandStream;
2390 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
2391 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
2392 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
2393
2394 // After buffer info is updated, the drawingState from the real layer needs to be copied into
2395 // the cloned. This is because some properties of drawingState can change when latchBuffer is
2396 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
2397 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
2398 // the cloned drawingState again.
2399 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
2400 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
2401 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
2402 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
2403
2404 cloneDrawingState(clonedFrom.get());
2405
2406 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
2407 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
2408 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
2409 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -07002410}
chaviw74b03172019-08-19 11:09:03 -07002411
2412void Layer::updateMirrorInfo() {
2413 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2414 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2415 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2416 // that case, we want to delete the reference to the clone since we want it to get
2417 // destroyed. The root, this layer, will still be around since the client can continue
2418 // to hold a reference, but no cloned layers will be displayed.
2419 mClonedChild = nullptr;
2420 return;
2421 }
2422
2423 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2424 // If the real layer exists and is in current state, add the clone as a child of the root.
2425 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2426 // copied to drawingState for the root layer. So the clonedChild is always removed from
2427 // drawingState and then needs to be added back each traversal.
2428 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2429 addChildToDrawing(mClonedChild);
2430 }
2431
2432 mClonedChild->updateClonedDrawingState(clonedLayersMap);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002433 mClonedChild->updateClonedChildren(sp<Layer>::fromExisting(this), clonedLayersMap);
chaviw74b03172019-08-19 11:09:03 -07002434 mClonedChild->updateClonedRelatives(clonedLayersMap);
2435}
2436
2437void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2438 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2439 // to the clone. If the real layer is no longer alive, continue traversing the children
2440 // since we may be able to pull out other children that are still alive.
2441 if (isClonedFromAlive()) {
2442 sp<Layer> clonedFrom = getClonedFrom();
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002443 cloneDrawingState(clonedFrom.get());
Ady Abrahamd11bade2022-08-01 16:18:03 -07002444 clonedLayersMap.emplace(clonedFrom, sp<Layer>::fromExisting(this));
chaviw74b03172019-08-19 11:09:03 -07002445 }
2446
2447 // The clone layer may have children in drawingState since they may have been created and
2448 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2449 // that already exist, since we can just re-use them.
2450 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2451 // not updated in the regular traversal. They are skipped since the root will lose the
2452 // reference to them when it copies its currentChildren to drawing.
2453 for (sp<Layer>& child : mDrawingChildren) {
2454 child->updateClonedDrawingState(clonedLayersMap);
2455 }
2456}
2457
2458void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2459 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2460 mDrawingChildren.clear();
2461
2462 if (!isClonedFromAlive()) {
2463 return;
2464 }
2465
2466 sp<Layer> clonedFrom = getClonedFrom();
2467 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2468 if (child == mirrorRoot) {
2469 // This is to avoid cyclical mirroring.
2470 continue;
2471 }
2472 sp<Layer> clonedChild = clonedLayersMap[child];
2473 if (clonedChild == nullptr) {
2474 clonedChild = child->createClone();
2475 clonedLayersMap[child] = clonedChild;
2476 }
2477 addChildToDrawing(clonedChild);
2478 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2479 }
2480}
2481
chaviwaf87b3e2019-10-01 16:59:28 -07002482void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2483 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2484 if (cropLayer != nullptr) {
2485 if (clonedLayersMap.count(cropLayer) == 0) {
2486 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2487 // self as crop layer to avoid going outside bounds.
Ady Abrahamd11bade2022-08-01 16:18:03 -07002488 mDrawingState.touchableRegionCrop = wp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002489 } else {
2490 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2491 mDrawingState.touchableRegionCrop = clonedCropLayer;
2492 }
2493 }
2494 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2495 // WM or the client.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002496 mDrawingState.inputInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, false);
chaviwaf87b3e2019-10-01 16:59:28 -07002497}
2498
2499void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002500 mDrawingState.zOrderRelativeOf = wp<Layer>();
chaviw74b03172019-08-19 11:09:03 -07002501 mDrawingState.zOrderRelatives.clear();
2502
2503 if (!isClonedFromAlive()) {
2504 return;
2505 }
2506
chaviwaf87b3e2019-10-01 16:59:28 -07002507 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002508 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002509 const sp<Layer>& relative = relativeWeak.promote();
2510 if (clonedLayersMap.count(relative) > 0) {
2511 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002512 mDrawingState.zOrderRelatives.add(clonedRelative);
2513 }
2514 }
2515
2516 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2517 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2518 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2519 // still traverse the children, but the layer with the missing relativeOf will not be shown
2520 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002521 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2522 if (clonedLayersMap.count(relativeOf) > 0) {
2523 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002524 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2525 }
2526
chaviwaf87b3e2019-10-01 16:59:28 -07002527 updateClonedInputInfo(clonedLayersMap);
2528
chaviw74b03172019-08-19 11:09:03 -07002529 for (sp<Layer>& child : mDrawingChildren) {
2530 child->updateClonedRelatives(clonedLayersMap);
2531 }
2532}
2533
2534void Layer::addChildToDrawing(const sp<Layer>& layer) {
2535 mDrawingChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002536 layer->mDrawingParent = sp<Layer>::fromExisting(this);
chaviw74b03172019-08-19 11:09:03 -07002537}
2538
Steven Thomas62a4cf82020-01-31 12:04:03 -08002539Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2540 switch (compatibility) {
2541 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2542 return FrameRateCompatibility::Default;
2543 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2544 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002545 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2546 return FrameRateCompatibility::Exact;
Andy Labrada096227e2022-06-15 16:58:11 +00002547 case ANATIVEWINDOW_FRAME_RATE_MIN:
2548 return FrameRateCompatibility::Min;
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002549 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
2550 return FrameRateCompatibility::NoVote;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002551 default:
2552 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2553 return FrameRateCompatibility::Default;
2554 }
2555}
2556
Marin Shalamanovc5986772021-03-16 16:09:49 +01002557scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2558 switch (strategy) {
2559 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2560 return Seamlessness::OnlySeamless;
2561 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2562 return Seamlessness::SeamedAndSeamless;
2563 default:
2564 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2565 return Seamlessness::Default;
2566 }
2567}
2568
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002569bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002570 const State& s(mDrawingState);
2571 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2572 return true;
2573 }
2574
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002575 sp<Layer> parent = mDrawingParent.promote();
2576 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002577}
2578
Robert Carr6a0382d2021-07-01 15:57:17 -07002579void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2580 mClonedChild = clonedChild;
2581 mHadClonedChild = true;
2582 mFlinger->mNumClones++;
2583}
2584
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002585bool Layer::setDropInputMode(gui::DropInputMode mode) {
2586 if (mDrawingState.dropInputMode == mode) {
2587 return false;
2588 }
2589 mDrawingState.dropInputMode = mode;
2590 return true;
2591}
2592
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002593void Layer::cloneDrawingState(const Layer* from) {
2594 mDrawingState = from->mDrawingState;
2595 // Skip callback info since they are not applicable for cloned layers.
2596 mDrawingState.releaseBufferListener = nullptr;
2597 mDrawingState.callbackHandles = {};
2598}
2599
Patrick Williamsbb25f802022-08-30 23:02:34 +00002600void Layer::callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
2601 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
2602 const sp<Fence>& releaseFence,
2603 uint32_t currentMaxAcquiredBufferCount) {
2604 if (!listener) {
2605 return;
2606 }
2607 ATRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, getDebugName(), framenumber);
2608 listener->onReleaseBuffer({buffer->getId(), framenumber},
2609 releaseFence ? releaseFence : Fence::NO_FENCE,
2610 currentMaxAcquiredBufferCount);
2611}
2612
2613void Layer::onLayerDisplayed(ftl::SharedFuture<FenceResult> futureFenceResult) {
2614 // If we are displayed on multiple displays in a single composition cycle then we would
2615 // need to do careful tracking to enable the use of the mLastClientCompositionFence.
2616 // For example we can only use it if all the displays are client comp, and we need
2617 // to merge all the client comp fences. We could do this, but for now we just
2618 // disable the optimization when a layer is composed on multiple displays.
2619 if (mClearClientCompositionFenceOnLayerDisplayed) {
2620 mLastClientCompositionFence = nullptr;
2621 } else {
2622 mClearClientCompositionFenceOnLayerDisplayed = true;
2623 }
2624
2625 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
2626 // buffer that was presented on this layer. The first transaction that came in this frame that
2627 // replaced the previous buffer on this layer needs this release fence, because the fence will
2628 // let the client know when that previous buffer is removed from the screen.
2629 //
2630 // Every other transaction on this layer does not need a release fence because no other
2631 // Transactions that were set on this layer this frame are going to have their preceding buffer
2632 // removed from the display this frame.
2633 //
2634 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
2635 // buffer so it doesn't need a previous release fence because the layer still needs the previous
2636 // buffer. The second transaction contains a buffer so it needs a previous release fence because
2637 // the previous buffer will be released this frame. The third transaction also contains a
2638 // buffer. It replaces the buffer in the second transaction. The buffer in the second
2639 // transaction will now no longer be presented so it is released immediately and the third
2640 // transaction doesn't need a previous release fence.
2641 sp<CallbackHandle> ch;
2642 for (auto& handle : mDrawingState.callbackHandles) {
2643 if (handle->releasePreviousBuffer &&
2644 mDrawingState.releaseBufferEndpoint == handle->listener) {
2645 ch = handle;
2646 break;
2647 }
2648 }
2649
2650 // Prevent tracing the same release multiple times.
2651 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
2652 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
2653 }
2654
2655 if (ch != nullptr) {
2656 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2657 ch->previousReleaseFences.emplace_back(std::move(futureFenceResult));
2658 ch->name = mName;
2659 }
2660}
2661
2662void Layer::onSurfaceFrameCreated(
2663 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Vishnu Nair22491b82022-10-18 14:59:14 -07002664 if (!hasBufferOrSidebandStreamInDrawing()) {
2665 return;
2666 }
2667
Patrick Williamsbb25f802022-08-30 23:02:34 +00002668 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
2669 // Too many SurfaceFrames pending classification. The front of the deque is probably not
2670 // tracked by FrameTimeline and will never be presented. This will only result in a memory
2671 // leak.
2672 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
2673 mName.c_str());
2674 std::string miniDump = mPendingJankClassifications.front()->miniDump();
2675 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
2676 mPendingJankClassifications.pop_front();
2677 }
2678 mPendingJankClassifications.emplace_back(surfaceFrame);
2679}
2680
2681void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
2682 for (const auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair71fcf912022-10-18 09:14:20 -07002683 handle->transformHint = mSkipReportingTransformHint
2684 ? std::nullopt
2685 : std::make_optional<uint32_t>(mTransformHint);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002686 handle->dequeueReadyTime = dequeueReadyTime;
2687 handle->currentMaxAcquiredBufferCount =
2688 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
2689 ATRACE_FORMAT_INSTANT("releasePendingBuffer %s - %" PRIu64, getDebugName(),
2690 handle->previousReleaseCallbackId.framenumber);
2691 }
2692
2693 for (auto& handle : mDrawingState.callbackHandles) {
2694 if (handle->releasePreviousBuffer &&
2695 mDrawingState.releaseBufferEndpoint == handle->listener) {
2696 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2697 break;
2698 }
2699 }
2700
2701 std::vector<JankData> jankData;
2702 jankData.reserve(mPendingJankClassifications.size());
2703 while (!mPendingJankClassifications.empty() &&
2704 mPendingJankClassifications.front()->getJankType()) {
2705 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
2706 mPendingJankClassifications.front();
2707 mPendingJankClassifications.pop_front();
2708 jankData.emplace_back(
2709 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
2710 }
2711
2712 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(mDrawingState.callbackHandles,
2713 jankData);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002714 mDrawingState.callbackHandles = {};
2715}
2716
2717bool Layer::willPresentCurrentTransaction() const {
2718 // Returns true if the most recent Transaction applied to CurrentState will be presented.
2719 return (getSidebandStreamChanged() || getAutoRefresh() ||
2720 (mDrawingState.modified &&
2721 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
2722}
2723
2724bool Layer::setTransform(uint32_t transform) {
2725 if (mDrawingState.bufferTransform == transform) return false;
2726 mDrawingState.bufferTransform = transform;
2727 mDrawingState.modified = true;
2728 setTransactionFlags(eTransactionNeeded);
2729 return true;
2730}
2731
2732bool Layer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
2733 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
2734 mDrawingState.sequence++;
2735 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
2736 mDrawingState.modified = true;
2737 setTransactionFlags(eTransactionNeeded);
2738 return true;
2739}
2740
2741bool Layer::setBufferCrop(const Rect& bufferCrop) {
2742 if (mDrawingState.bufferCrop == bufferCrop) return false;
2743
2744 mDrawingState.sequence++;
2745 mDrawingState.bufferCrop = bufferCrop;
2746
2747 mDrawingState.modified = true;
2748 setTransactionFlags(eTransactionNeeded);
2749 return true;
2750}
2751
2752bool Layer::setDestinationFrame(const Rect& destinationFrame) {
2753 if (mDrawingState.destinationFrame == destinationFrame) return false;
2754
2755 mDrawingState.sequence++;
2756 mDrawingState.destinationFrame = destinationFrame;
2757
2758 mDrawingState.modified = true;
2759 setTransactionFlags(eTransactionNeeded);
2760 return true;
2761}
2762
2763// Translate destination frame into scale and position. If a destination frame is not set, use the
2764// provided scale and position
2765bool Layer::updateGeometry() {
2766 if ((mDrawingState.flags & layer_state_t::eIgnoreDestinationFrame) ||
2767 mDrawingState.destinationFrame.isEmpty()) {
2768 // If destination frame is not set, use the requested transform set via
2769 // Layer::setPosition and Layer::setMatrix.
2770 return assignTransform(&mDrawingState.transform, mRequestedTransform);
2771 }
2772
2773 Rect destRect = mDrawingState.destinationFrame;
2774 int32_t destW = destRect.width();
2775 int32_t destH = destRect.height();
2776 if (destRect.left < 0) {
2777 destRect.left = 0;
2778 destRect.right = destW;
2779 }
2780 if (destRect.top < 0) {
2781 destRect.top = 0;
2782 destRect.bottom = destH;
2783 }
2784
2785 if (!mDrawingState.buffer) {
2786 ui::Transform t;
2787 t.set(destRect.left, destRect.top);
2788 return assignTransform(&mDrawingState.transform, t);
2789 }
2790
2791 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
2792 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
2793 // Undo any transformations on the buffer.
2794 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
2795 std::swap(bufferWidth, bufferHeight);
2796 }
2797 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
2798 if (mDrawingState.transformToDisplayInverse) {
2799 if (invTransform & ui::Transform::ROT_90) {
2800 std::swap(bufferWidth, bufferHeight);
2801 }
2802 }
2803
2804 float sx = destW / static_cast<float>(bufferWidth);
2805 float sy = destH / static_cast<float>(bufferHeight);
2806 ui::Transform t;
2807 t.set(sx, 0, 0, sy);
2808 t.set(destRect.left, destRect.top);
2809 return assignTransform(&mDrawingState.transform, t);
2810}
2811
2812bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
2813 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
2814 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
2815 return false;
2816 }
2817
2818 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
2819
2820 mDrawingState.sequence++;
2821 mDrawingState.modified = true;
2822 setTransactionFlags(eTransactionNeeded);
2823
2824 return true;
2825}
2826
2827bool Layer::setPosition(float x, float y) {
2828 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
2829 return false;
2830 }
2831
2832 mRequestedTransform.set(x, y);
2833
2834 mDrawingState.sequence++;
2835 mDrawingState.modified = true;
2836 setTransactionFlags(eTransactionNeeded);
2837
2838 return true;
2839}
2840
2841bool Layer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
2842 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
2843 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
2844 const FrameTimelineInfo& info) {
Chavi Weingarten009619f2022-09-15 20:27:25 +00002845 ATRACE_FORMAT("setBuffer %s - hasBuffer=%s", getDebugName(), (buffer ? "true" : "false"));
Patrick Williamsbb25f802022-08-30 23:02:34 +00002846 if (!buffer) {
2847 return false;
2848 }
2849
2850 const bool frameNumberChanged =
2851 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
2852 const uint64_t frameNumber =
2853 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
Chavi Weingarten009619f2022-09-15 20:27:25 +00002854 ATRACE_FORMAT_INSTANT("setBuffer %s - %" PRIu64, getDebugName(), frameNumber);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002855
2856 if (mDrawingState.buffer) {
2857 mReleasePreviousBuffer = true;
2858 if (!mBufferInfo.mBuffer ||
2859 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
2860 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
2861 // If mDrawingState has a buffer, and we are about to update again
2862 // before swapping to drawing state, then the first buffer will be
2863 // dropped and we should decrement the pending buffer count and
2864 // call any release buffer callbacks if set.
2865 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2866 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2867 mDrawingState.acquireFence,
2868 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2869 mOwnerUid));
2870 decrementPendingBufferCount();
2871 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
2872 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
2873 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
2874 mDrawingState.bufferSurfaceFrameTX.reset();
2875 }
2876 } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) {
2877 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2878 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2879 mLastClientCompositionFence,
2880 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2881 mOwnerUid));
2882 mLastClientCompositionFence = nullptr;
2883 }
2884 }
2885
2886 mDrawingState.frameNumber = frameNumber;
2887 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
2888 mDrawingState.buffer = std::move(buffer);
2889 mDrawingState.clientCacheId = bufferData.cachedBuffer;
2890
2891 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
2892 ? bufferData.acquireFence
2893 : Fence::NO_FENCE;
2894 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
2895 if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
2896 // We latched this buffer unsiganled, so we need to pass the acquire fence
2897 // on the callback instead of just the acquire time, since it's unknown at
2898 // this point.
2899 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence;
2900 } else {
2901 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime();
2902 }
2903
2904 mDrawingState.modified = true;
2905 setTransactionFlags(eTransactionNeeded);
2906
2907 const int32_t layerId = getSequence();
2908 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
2909 mOwnerUid, postTime, getGameMode());
2910 mDrawingState.desiredPresentTime = desiredPresentTime;
2911 mDrawingState.isAutoTimestamp = isAutoTimestamp;
2912
2913 const nsecs_t presentTime = [&] {
2914 if (!isAutoTimestamp) return desiredPresentTime;
2915
2916 const auto prediction =
2917 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
2918 if (prediction.has_value()) return prediction->presentTime;
2919
2920 return static_cast<nsecs_t>(0);
2921 }();
2922
2923 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
2924 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
2925
2926 setFrameTimelineVsyncForBufferTransaction(info, postTime);
2927
2928 if (dequeueTime && *dequeueTime != 0) {
2929 const uint64_t bufferId = mDrawingState.buffer->getId();
2930 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
2931 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
2932 FrameTracer::FrameEvent::DEQUEUE);
2933 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
2934 FrameTracer::FrameEvent::QUEUE);
2935 }
2936
2937 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
2938 return true;
2939}
2940
2941bool Layer::setDataspace(ui::Dataspace dataspace) {
2942 mDrawingState.dataspaceRequested = true;
2943 if (mDrawingState.dataspace == dataspace) return false;
2944 mDrawingState.dataspace = dataspace;
2945 mDrawingState.modified = true;
2946 setTransactionFlags(eTransactionNeeded);
2947 return true;
2948}
2949
2950bool Layer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
2951 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
2952 mDrawingState.hdrMetadata = hdrMetadata;
2953 mDrawingState.modified = true;
2954 setTransactionFlags(eTransactionNeeded);
2955 return true;
2956}
2957
2958bool Layer::setSurfaceDamageRegion(const Region& surfaceDamage) {
2959 mDrawingState.surfaceDamageRegion = surfaceDamage;
2960 mDrawingState.modified = true;
2961 setTransactionFlags(eTransactionNeeded);
2962 return true;
2963}
2964
2965bool Layer::setApi(int32_t api) {
2966 if (mDrawingState.api == api) return false;
2967 mDrawingState.api = api;
2968 mDrawingState.modified = true;
2969 setTransactionFlags(eTransactionNeeded);
2970 return true;
2971}
2972
2973bool Layer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
2974 if (mDrawingState.sidebandStream == sidebandStream) return false;
2975
2976 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
2977 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
2978 } else if (sidebandStream != nullptr) {
2979 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
2980 }
2981
2982 mDrawingState.sidebandStream = sidebandStream;
2983 mDrawingState.modified = true;
2984 setTransactionFlags(eTransactionNeeded);
2985 if (!mSidebandStreamChanged.exchange(true)) {
2986 // mSidebandStreamChanged was false
2987 mFlinger->onLayerUpdate();
2988 }
2989 return true;
2990}
2991
2992bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
2993 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
2994 if (handles.empty()) {
2995 mReleasePreviousBuffer = false;
2996 return false;
2997 }
2998
2999 const bool willPresent = willPresentCurrentTransaction();
3000
3001 for (const auto& handle : handles) {
3002 // If this transaction set a buffer on this layer, release its previous buffer
3003 handle->releasePreviousBuffer = mReleasePreviousBuffer;
3004
3005 // If this layer will be presented in this frame
3006 if (willPresent) {
3007 // If this transaction set an acquire fence on this layer, set its acquire time
3008 handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence;
3009 handle->frameNumber = mDrawingState.frameNumber;
3010
3011 // Store so latched time and release fence can be set
3012 mDrawingState.callbackHandles.push_back(handle);
3013
3014 } else { // If this layer will NOT need to be relatched and presented this frame
3015 // Notify the transaction completed thread this handle is done
3016 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
3017 }
3018 }
3019
3020 mReleasePreviousBuffer = false;
3021 mCallbackHandleAcquireTimeOrFence = -1;
3022
3023 return willPresent;
3024}
3025
3026Rect Layer::getBufferSize(const State& /*s*/) const {
3027 // for buffer state layers we use the display frame size as the buffer size.
3028
3029 if (mBufferInfo.mBuffer == nullptr) {
3030 return Rect::INVALID_RECT;
3031 }
3032
3033 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3034 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3035
3036 // Undo any transformations on the buffer and return the result.
3037 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
3038 std::swap(bufWidth, bufHeight);
3039 }
3040
3041 if (getTransformToDisplayInverse()) {
3042 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3043 if (invTransform & ui::Transform::ROT_90) {
3044 std::swap(bufWidth, bufHeight);
3045 }
3046 }
3047
3048 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
3049}
3050
3051FloatRect Layer::computeSourceBounds(const FloatRect& parentBounds) const {
3052 if (mBufferInfo.mBuffer == nullptr) {
3053 return parentBounds;
3054 }
3055
3056 return getBufferSize(getDrawingState()).toFloatRect();
3057}
3058
3059bool Layer::fenceHasSignaled() const {
3060 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
3061 return true;
3062 }
3063
3064 const bool fenceSignaled =
3065 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
3066 if (!fenceSignaled) {
3067 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
3068 TimeStats::LatchSkipReason::LateAcquire);
3069 }
3070
3071 return fenceSignaled;
3072}
3073
Vishnu Naire14c6b32022-08-06 04:20:15 +00003074bool Layer::onPreComposition(nsecs_t refreshStartTime) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003075 for (const auto& handle : mDrawingState.callbackHandles) {
3076 handle->refreshStartTime = refreshStartTime;
3077 }
3078 return hasReadyFrame();
3079}
3080
3081void Layer::setAutoRefresh(bool autoRefresh) {
3082 mDrawingState.autoRefresh = autoRefresh;
3083}
3084
3085bool Layer::latchSidebandStream(bool& recomputeVisibleRegions) {
3086 // 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 +00003087 auto* snapshot = editLayerSnapshot();
3088 snapshot->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003089
3090 if (mSidebandStreamChanged.exchange(false)) {
3091 const State& s(getDrawingState());
3092 // mSidebandStreamChanged was true
3093 mSidebandStream = s.sidebandStream;
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003094 snapshot->sidebandStream = mSidebandStream;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003095 if (mSidebandStream != nullptr) {
3096 setTransactionFlags(eTransactionNeeded);
3097 mFlinger->setTransactionFlags(eTraversalNeeded);
3098 }
3099 recomputeVisibleRegions = true;
3100
3101 return true;
3102 }
3103 return false;
3104}
3105
3106bool Layer::hasFrameUpdate() const {
3107 const State& c(getDrawingState());
3108 return (mDrawingStateModified || mDrawingState.modified) &&
3109 (c.buffer != nullptr || c.bgColorLayer != nullptr);
3110}
3111
3112void Layer::updateTexImage(nsecs_t latchTime) {
3113 const State& s(getDrawingState());
3114
3115 if (!s.buffer) {
3116 if (s.bgColorLayer) {
3117 for (auto& handle : mDrawingState.callbackHandles) {
3118 handle->latchTime = latchTime;
3119 }
3120 }
3121 return;
3122 }
3123
3124 for (auto& handle : mDrawingState.callbackHandles) {
3125 if (handle->frameNumber == mDrawingState.frameNumber) {
3126 handle->latchTime = latchTime;
3127 }
3128 }
3129
3130 const int32_t layerId = getSequence();
3131 const uint64_t bufferId = mDrawingState.buffer->getId();
3132 const uint64_t frameNumber = mDrawingState.frameNumber;
3133 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3134 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
3135 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
3136
3137 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
3138 FrameTracer::FrameEvent::ACQUIRE_FENCE);
3139 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
3140 FrameTracer::FrameEvent::LATCH);
3141
3142 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
3143 if (bufferSurfaceFrame != nullptr &&
3144 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
3145 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
3146 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
3147 // are processing the next state.
3148 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
3149 mDrawingState.acquireFenceTime->getSignalTime(),
3150 latchTime);
3151 mDrawingState.bufferSurfaceFrameTX.reset();
3152 }
3153
3154 std::deque<sp<CallbackHandle>> remainingHandles;
3155 mFlinger->getTransactionCallbackInvoker()
3156 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
3157 mDrawingState.callbackHandles = remainingHandles;
3158
3159 mDrawingStateModified = false;
3160}
3161
3162void Layer::gatherBufferInfo() {
3163 if (!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
3164 decrementPendingBufferCount();
3165 }
3166
3167 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
3168 mBufferInfo.mBuffer = mDrawingState.buffer;
3169 mBufferInfo.mFence = mDrawingState.acquireFence;
3170 mBufferInfo.mFrameNumber = mDrawingState.frameNumber;
3171 mBufferInfo.mPixelFormat =
3172 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat();
3173 mBufferInfo.mFrameLatencyNeeded = true;
3174 mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime;
3175 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3176 mBufferInfo.mFence = mDrawingState.acquireFence;
3177 mBufferInfo.mTransform = mDrawingState.bufferTransform;
3178 auto lastDataspace = mBufferInfo.mDataspace;
3179 mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace);
3180 if (lastDataspace != mBufferInfo.mDataspace) {
3181 mFlinger->mSomeDataspaceChanged = true;
3182 }
3183 mBufferInfo.mCrop = computeBufferCrop(mDrawingState);
3184 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
3185 mBufferInfo.mSurfaceDamage = mDrawingState.surfaceDamageRegion;
3186 mBufferInfo.mHdrMetadata = mDrawingState.hdrMetadata;
3187 mBufferInfo.mApi = mDrawingState.api;
3188 mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse;
3189 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(mDrawingState.clientCacheId);
3190}
3191
3192Rect Layer::computeBufferCrop(const State& s) {
3193 if (s.buffer && !s.bufferCrop.isEmpty()) {
3194 Rect bufferCrop;
3195 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
3196 return bufferCrop;
3197 } else if (s.buffer) {
3198 return s.buffer->getBounds();
3199 } else {
3200 return s.bufferCrop;
3201 }
3202}
3203
3204sp<Layer> Layer::createClone() {
3205 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
3206 args.textureName = mTextureName;
Patrick Williams83f36b22022-09-14 17:57:35 +00003207 sp<Layer> layer = mFlinger->getFactory().createBufferStateLayer(args);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003208 layer->mHwcSlotGenerator = mHwcSlotGenerator;
3209 layer->setInitialValuesForClone(sp<Layer>::fromExisting(this));
3210 return layer;
3211}
3212
3213bool Layer::bufferNeedsFiltering() const {
3214 const State& s(getDrawingState());
3215 if (!s.buffer) {
3216 return false;
3217 }
3218
3219 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
3220 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
3221
3222 // Undo any transformations on the buffer and return the result.
3223 if (s.bufferTransform & ui::Transform::ROT_90) {
3224 std::swap(bufferWidth, bufferHeight);
3225 }
3226
3227 if (s.transformToDisplayInverse) {
3228 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3229 if (invTransform & ui::Transform::ROT_90) {
3230 std::swap(bufferWidth, bufferHeight);
3231 }
3232 }
3233
3234 const Rect layerSize{getBounds()};
3235 int32_t layerWidth = layerSize.getWidth();
3236 int32_t layerHeight = layerSize.getHeight();
3237
3238 // Align the layer orientation with the buffer before comparism
3239 if (mTransformHint & ui::Transform::ROT_90) {
3240 std::swap(layerWidth, layerHeight);
3241 }
3242
3243 return layerWidth != bufferWidth || layerHeight != bufferHeight;
3244}
3245
3246void Layer::decrementPendingBufferCount() {
3247 int32_t pendingBuffers = --mPendingBufferTransactions;
3248 tracePendingBufferCount(pendingBuffers);
3249}
3250
3251void Layer::tracePendingBufferCount(int32_t pendingBuffers) {
3252 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
3253}
3254
3255/*
3256 * We don't want to send the layer's transform to input, but rather the
3257 * parent's transform. This is because Layer's transform is
3258 * information about how the buffer is placed on screen. The parent's
3259 * transform makes more sense to send since it's information about how the
3260 * layer is placed on screen. This transform is used by input to determine
3261 * how to go from screen space back to window space.
3262 */
3263ui::Transform Layer::getInputTransform() const {
3264 if (!hasBufferOrSidebandStream()) {
3265 return getTransform();
3266 }
3267 sp<Layer> parent = mDrawingParent.promote();
3268 if (parent == nullptr) {
3269 return ui::Transform();
3270 }
3271
3272 return parent->getTransform();
3273}
3274
3275/**
3276 * Similar to getInputTransform, we need to update the bounds to include the transform.
3277 * This is because bounds don't include the buffer transform, where the input assumes
3278 * that's already included.
3279 */
3280Rect Layer::getInputBounds() const {
3281 if (!hasBufferOrSidebandStream()) {
3282 return getCroppedBufferSize(getDrawingState());
3283 }
3284
3285 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
3286 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
3287 return bufferBounds;
3288 }
3289 return mDrawingState.transform.transform(bufferBounds);
3290}
3291
3292bool Layer::simpleBufferUpdate(const layer_state_t& s) const {
3293 const uint64_t requiredFlags = layer_state_t::eBufferChanged;
3294
3295 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
3296 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
3297 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
3298 layer_state_t::eLayerStackChanged | layer_state_t::eAutoRefreshChanged |
3299 layer_state_t::eReparent;
3300
3301 const uint64_t allowedFlags = layer_state_t::eHasListenerCallbacksChanged |
3302 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFrameRateChanged |
3303 layer_state_t::eSurfaceDamageRegionChanged | layer_state_t::eApiChanged |
3304 layer_state_t::eMetadataChanged | layer_state_t::eDropInputModeChanged |
3305 layer_state_t::eInputInfoChanged;
3306
3307 if ((s.what & requiredFlags) != requiredFlags) {
3308 ALOGV("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
3309 (s.what | requiredFlags) & ~s.what);
3310 return false;
3311 }
3312
3313 if (s.what & deniedFlags) {
3314 ALOGV("%s: false [has denied flags 0x%" PRIx64 "]", __func__, s.what & deniedFlags);
3315 return false;
3316 }
3317
3318 if (s.what & allowedFlags) {
3319 ALOGV("%s: [has allowed flags 0x%" PRIx64 "]", __func__, s.what & allowedFlags);
3320 }
3321
3322 if (s.what & layer_state_t::ePositionChanged) {
3323 if (mRequestedTransform.tx() != s.x || mRequestedTransform.ty() != s.y) {
3324 ALOGV("%s: false [ePositionChanged changed]", __func__);
3325 return false;
3326 }
3327 }
3328
3329 if (s.what & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003330 if (mDrawingState.color.a != s.color.a) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003331 ALOGV("%s: false [eAlphaChanged changed]", __func__);
3332 return false;
3333 }
3334 }
3335
3336 if (s.what & layer_state_t::eColorTransformChanged) {
3337 if (mDrawingState.colorTransform != s.colorTransform) {
3338 ALOGV("%s: false [eColorTransformChanged changed]", __func__);
3339 return false;
3340 }
3341 }
3342
3343 if (s.what & layer_state_t::eBackgroundColorChanged) {
3344 if (mDrawingState.bgColorLayer || s.bgColorAlpha != 0) {
3345 ALOGV("%s: false [eBackgroundColorChanged changed]", __func__);
3346 return false;
3347 }
3348 }
3349
3350 if (s.what & layer_state_t::eMatrixChanged) {
3351 if (mRequestedTransform.dsdx() != s.matrix.dsdx ||
3352 mRequestedTransform.dtdy() != s.matrix.dtdy ||
3353 mRequestedTransform.dtdx() != s.matrix.dtdx ||
3354 mRequestedTransform.dsdy() != s.matrix.dsdy) {
3355 ALOGV("%s: false [eMatrixChanged changed]", __func__);
3356 return false;
3357 }
3358 }
3359
3360 if (s.what & layer_state_t::eCornerRadiusChanged) {
3361 if (mDrawingState.cornerRadius != s.cornerRadius) {
3362 ALOGV("%s: false [eCornerRadiusChanged changed]", __func__);
3363 return false;
3364 }
3365 }
3366
3367 if (s.what & layer_state_t::eBackgroundBlurRadiusChanged) {
3368 if (mDrawingState.backgroundBlurRadius != static_cast<int>(s.backgroundBlurRadius)) {
3369 ALOGV("%s: false [eBackgroundBlurRadiusChanged changed]", __func__);
3370 return false;
3371 }
3372 }
3373
Vishnu Nairbbceb462022-10-10 04:52:13 +00003374 if (s.what & layer_state_t::eBufferTransformChanged) {
3375 if (mDrawingState.bufferTransform != s.bufferTransform) {
3376 ALOGV("%s: false [eBufferTransformChanged changed]", __func__);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003377 return false;
3378 }
3379 }
3380
3381 if (s.what & layer_state_t::eTransformToDisplayInverseChanged) {
3382 if (mDrawingState.transformToDisplayInverse != s.transformToDisplayInverse) {
3383 ALOGV("%s: false [eTransformToDisplayInverseChanged changed]", __func__);
3384 return false;
3385 }
3386 }
3387
3388 if (s.what & layer_state_t::eCropChanged) {
3389 if (mDrawingState.crop != s.crop) {
3390 ALOGV("%s: false [eCropChanged changed]", __func__);
3391 return false;
3392 }
3393 }
3394
3395 if (s.what & layer_state_t::eDataspaceChanged) {
3396 if (mDrawingState.dataspace != s.dataspace) {
3397 ALOGV("%s: false [eDataspaceChanged changed]", __func__);
3398 return false;
3399 }
3400 }
3401
3402 if (s.what & layer_state_t::eHdrMetadataChanged) {
3403 if (mDrawingState.hdrMetadata != s.hdrMetadata) {
3404 ALOGV("%s: false [eHdrMetadataChanged changed]", __func__);
3405 return false;
3406 }
3407 }
3408
3409 if (s.what & layer_state_t::eSidebandStreamChanged) {
3410 if (mDrawingState.sidebandStream != s.sidebandStream) {
3411 ALOGV("%s: false [eSidebandStreamChanged changed]", __func__);
3412 return false;
3413 }
3414 }
3415
3416 if (s.what & layer_state_t::eColorSpaceAgnosticChanged) {
3417 if (mDrawingState.colorSpaceAgnostic != s.colorSpaceAgnostic) {
3418 ALOGV("%s: false [eColorSpaceAgnosticChanged changed]", __func__);
3419 return false;
3420 }
3421 }
3422
3423 if (s.what & layer_state_t::eShadowRadiusChanged) {
3424 if (mDrawingState.shadowRadius != s.shadowRadius) {
3425 ALOGV("%s: false [eShadowRadiusChanged changed]", __func__);
3426 return false;
3427 }
3428 }
3429
3430 if (s.what & layer_state_t::eFixedTransformHintChanged) {
3431 if (mDrawingState.fixedTransformHint != s.fixedTransformHint) {
3432 ALOGV("%s: false [eFixedTransformHintChanged changed]", __func__);
3433 return false;
3434 }
3435 }
3436
3437 if (s.what & layer_state_t::eTrustedOverlayChanged) {
3438 if (mDrawingState.isTrustedOverlay != s.isTrustedOverlay) {
3439 ALOGV("%s: false [eTrustedOverlayChanged changed]", __func__);
3440 return false;
3441 }
3442 }
3443
3444 if (s.what & layer_state_t::eStretchChanged) {
3445 StretchEffect temp = s.stretchEffect;
3446 temp.sanitize();
3447 if (mDrawingState.stretchEffect != temp) {
3448 ALOGV("%s: false [eStretchChanged changed]", __func__);
3449 return false;
3450 }
3451 }
3452
3453 if (s.what & layer_state_t::eBufferCropChanged) {
3454 if (mDrawingState.bufferCrop != s.bufferCrop) {
3455 ALOGV("%s: false [eBufferCropChanged changed]", __func__);
3456 return false;
3457 }
3458 }
3459
3460 if (s.what & layer_state_t::eDestinationFrameChanged) {
3461 if (mDrawingState.destinationFrame != s.destinationFrame) {
3462 ALOGV("%s: false [eDestinationFrameChanged changed]", __func__);
3463 return false;
3464 }
3465 }
3466
3467 if (s.what & layer_state_t::eDimmingEnabledChanged) {
3468 if (mDrawingState.dimmingEnabled != s.dimmingEnabled) {
3469 ALOGV("%s: false [eDimmingEnabledChanged changed]", __func__);
3470 return false;
3471 }
3472 }
3473
3474 ALOGV("%s: true", __func__);
3475 return true;
3476}
3477
3478bool Layer::isHdrY410() const {
3479 // pixel format is HDR Y410 masquerading as RGBA_1010102
3480 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
3481 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
3482 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
3483}
3484
Vishnu Naire14c6b32022-08-06 04:20:15 +00003485sp<LayerFE> Layer::getCompositionEngineLayerFE() const {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003486 // 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 +00003487 return hasSomethingToDraw() ? mLayerFE : nullptr;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003488}
3489
Vishnu Naire14c6b32022-08-06 04:20:15 +00003490const LayerSnapshot* Layer::getLayerSnapshot() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003491 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003492}
3493
Vishnu Naire14c6b32022-08-06 04:20:15 +00003494LayerSnapshot* Layer::editLayerSnapshot() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003495 return mSnapshot.get();
3496}
Vishnu Naire14c6b32022-08-06 04:20:15 +00003497
Patrick Williamsbb25f802022-08-30 23:02:34 +00003498const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003499 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003500}
3501
Patrick Williams7584c6a2022-10-29 02:10:58 +00003502sp<LayerFE> Layer::copyCompositionEngineLayerFE() const {
3503 auto result = mFlinger->getFactory().createLayerFE(mLayerFE->getDebugName());
3504 result->mSnapshot = std::make_unique<LayerSnapshot>(*mSnapshot);
3505 return result;
3506}
3507
Patrick Williamsbb25f802022-08-30 23:02:34 +00003508void Layer::useSurfaceDamage() {
3509 if (mFlinger->mForceFullDamage) {
3510 surfaceDamageRegion = Region::INVALID_REGION;
3511 } else {
3512 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
3513 }
3514}
3515
3516void Layer::useEmptyDamage() {
3517 surfaceDamageRegion.clear();
3518}
3519
3520bool Layer::isOpaque(const Layer::State& s) const {
3521 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
3522 // layer's opaque flag.
3523 if (!hasSomethingToDraw()) {
3524 return false;
3525 }
3526
3527 // if the layer has the opaque flag, then we're always opaque
3528 if ((s.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque) {
3529 return true;
3530 }
3531
3532 // If the buffer has no alpha channel, then we are opaque
3533 if (hasBufferOrSidebandStream() && isOpaqueFormat(getPixelFormat())) {
3534 return true;
3535 }
3536
3537 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
3538 return fillsColor() && getAlpha() == 1.0_hf;
3539}
3540
3541bool Layer::canReceiveInput() const {
3542 return !isHiddenByPolicy() && (mBufferInfo.mBuffer == nullptr || getAlpha() > 0.0f);
3543}
3544
3545bool Layer::isVisible() const {
3546 if (!hasSomethingToDraw()) {
3547 return false;
3548 }
3549
3550 if (isHiddenByPolicy()) {
3551 return false;
3552 }
3553
3554 return getAlpha() > 0.0f || hasBlur();
3555}
3556
3557void Layer::onPostComposition(const DisplayDevice* display,
3558 const std::shared_ptr<FenceTime>& glDoneFence,
3559 const std::shared_ptr<FenceTime>& presentFence,
3560 const CompositorTiming& compositorTiming) {
3561 // mFrameLatencyNeeded is true when a new frame was latched for the
3562 // composition.
3563 if (!mBufferInfo.mFrameLatencyNeeded) return;
3564
3565 for (const auto& handle : mDrawingState.callbackHandles) {
3566 handle->gpuCompositionDoneFence = glDoneFence;
3567 handle->compositorTiming = compositorTiming;
3568 }
3569
3570 // Update mFrameTracker.
3571 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
3572 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
3573
3574 const int32_t layerId = getSequence();
3575 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
3576
3577 const auto outputLayer = findOutputLayerForDisplay(display);
3578 if (outputLayer && outputLayer->requiresClientComposition()) {
3579 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
3580 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3581 clientCompositionTimestamp,
3582 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
3583 // Update the SurfaceFrames in the drawing state
3584 if (mDrawingState.bufferSurfaceFrameTX) {
3585 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
3586 }
3587 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
3588 surfaceFrame->setGpuComposition();
3589 }
3590 }
3591
3592 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
3593 if (frameReadyFence->isValid()) {
3594 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
3595 } else {
3596 // There was no fence for this frame, so assume that it was ready
3597 // to be presented at the desired present time.
3598 mFrameTracker.setFrameReadyTime(desiredPresentTime);
3599 }
3600
3601 if (display) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04003602 const Fps refreshRate = display->refreshRateSelector().getActiveModePtr()->getFps();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003603 const std::optional<Fps> renderRate =
3604 mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
3605
3606 const auto vote = frameRateToSetFrameRateVotePayload(mDrawingState.frameRate);
3607 const auto gameMode = getGameMode();
3608
3609 if (presentFence->isValid()) {
3610 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
3611 refreshRate, renderRate, vote, gameMode);
3612 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3613 presentFence,
3614 FrameTracer::FrameEvent::PRESENT_FENCE);
3615 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
3616 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
3617 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Dominik Laskowskidc2bb802022-09-28 16:02:59 -04003618 // The HWC doesn't support present fences, so use the present timestamp instead.
3619 const nsecs_t presentTimestamp =
3620 mFlinger->getHwComposer().getPresentTimestamp(*displayId);
3621
3622 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
3623 const nsecs_t vsyncPeriod = display->getVsyncPeriodFromHWC();
3624 const nsecs_t actualPresentTime = now - ((now - presentTimestamp) % vsyncPeriod);
3625
Patrick Williamsbb25f802022-08-30 23:02:34 +00003626 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
3627 refreshRate, renderRate, vote, gameMode);
3628 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(),
3629 mCurrentFrameNumber, actualPresentTime,
3630 FrameTracer::FrameEvent::PRESENT_FENCE);
3631 mFrameTracker.setActualPresentTime(actualPresentTime);
3632 }
3633 }
3634
3635 mFrameTracker.advanceFrame();
3636 mBufferInfo.mFrameLatencyNeeded = false;
3637}
3638
3639bool Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
3640 ATRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(),
3641 getDrawingState().frameNumber);
3642
3643 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
3644
3645 if (refreshRequired) {
3646 return refreshRequired;
3647 }
3648
3649 // If the head buffer's acquire fence hasn't signaled yet, return and
3650 // try again later
3651 if (!fenceHasSignaled()) {
3652 ATRACE_NAME("!fenceHasSignaled()");
3653 mFlinger->onLayerUpdate();
3654 return false;
3655 }
3656
3657 updateTexImage(latchTime);
3658 if (mDrawingState.buffer == nullptr) {
3659 return false;
3660 }
3661
3662 // Capture the old state of the layer for comparisons later
3663 BufferInfo oldBufferInfo = mBufferInfo;
3664 const bool oldOpacity = isOpaque(mDrawingState);
3665 mPreviousFrameNumber = mCurrentFrameNumber;
3666 mCurrentFrameNumber = mDrawingState.frameNumber;
3667 gatherBufferInfo();
3668
3669 if (oldBufferInfo.mBuffer == nullptr) {
3670 // the first time we receive a buffer, we need to trigger a
3671 // geometry invalidation.
3672 recomputeVisibleRegions = true;
3673 }
3674
3675 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
3676 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
3677 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
3678 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
3679 recomputeVisibleRegions = true;
3680 }
3681
3682 if (oldBufferInfo.mBuffer != nullptr) {
3683 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3684 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3685 if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
3686 bufHeight != oldBufferInfo.mBuffer->getHeight()) {
3687 recomputeVisibleRegions = true;
3688 }
3689 }
3690
3691 if (oldOpacity != isOpaque(mDrawingState)) {
3692 recomputeVisibleRegions = true;
3693 }
3694
3695 return true;
3696}
3697
3698bool Layer::hasReadyFrame() const {
3699 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
3700}
3701
3702bool Layer::isProtected() const {
3703 return (mBufferInfo.mBuffer != nullptr) &&
3704 (mBufferInfo.mBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
3705}
3706
3707// As documented in libhardware header, formats in the range
3708// 0x100 - 0x1FF are specific to the HAL implementation, and
3709// are known to have no alpha channel
3710// TODO: move definition for device-specific range into
3711// hardware.h, instead of using hard-coded values here.
3712#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
3713
3714bool Layer::isOpaqueFormat(PixelFormat format) {
3715 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
3716 return true;
3717 }
3718 switch (format) {
3719 case PIXEL_FORMAT_RGBA_8888:
3720 case PIXEL_FORMAT_BGRA_8888:
3721 case PIXEL_FORMAT_RGBA_FP16:
3722 case PIXEL_FORMAT_RGBA_1010102:
3723 case PIXEL_FORMAT_R_8:
3724 return false;
3725 }
3726 // in all other case, we have no blending (also for unknown formats)
3727 return true;
3728}
3729
3730bool Layer::needsFiltering(const DisplayDevice* display) const {
3731 if (!hasBufferOrSidebandStream()) {
3732 return false;
3733 }
3734 const auto outputLayer = findOutputLayerForDisplay(display);
3735 if (outputLayer == nullptr) {
3736 return false;
3737 }
3738
3739 // We need filtering if the sourceCrop rectangle size does not match the
3740 // displayframe rectangle size (not a 1:1 render)
3741 const auto& compositionState = outputLayer->getState();
3742 const auto displayFrame = compositionState.displayFrame;
3743 const auto sourceCrop = compositionState.sourceCrop;
3744 return sourceCrop.getHeight() != displayFrame.getHeight() ||
3745 sourceCrop.getWidth() != displayFrame.getWidth();
3746}
3747
3748bool Layer::needsFilteringForScreenshots(const DisplayDevice* display,
3749 const ui::Transform& inverseParentTransform) const {
3750 if (!hasBufferOrSidebandStream()) {
3751 return false;
3752 }
3753 const auto outputLayer = findOutputLayerForDisplay(display);
3754 if (outputLayer == nullptr) {
3755 return false;
3756 }
3757
3758 // We need filtering if the sourceCrop rectangle size does not match the
3759 // viewport rectangle size (not a 1:1 render)
3760 const auto& compositionState = outputLayer->getState();
3761 const ui::Transform& displayTransform = display->getTransform();
3762 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
3763 // Undo the transformation of the displayFrame so that we're back into
3764 // layer-stack space.
3765 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
3766 const FloatRect sourceCrop = compositionState.sourceCrop;
3767
3768 int32_t frameHeight = frame.getHeight();
3769 int32_t frameWidth = frame.getWidth();
3770 // If the display transform had a rotational component then undo the
3771 // rotation so that the orientation matches the source crop.
3772 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
3773 std::swap(frameHeight, frameWidth);
3774 }
3775 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
3776}
3777
3778void Layer::latchAndReleaseBuffer() {
3779 if (hasReadyFrame()) {
3780 bool ignored = false;
3781 latchBuffer(ignored, systemTime());
3782 }
3783 releasePendingBuffer(systemTime());
3784}
3785
3786PixelFormat Layer::getPixelFormat() const {
3787 return mBufferInfo.mPixelFormat;
3788}
3789
3790bool Layer::getTransformToDisplayInverse() const {
3791 return mBufferInfo.mTransformToDisplayInverse;
3792}
3793
3794Rect Layer::getBufferCrop() const {
3795 // this is the crop rectangle that applies to the buffer
3796 // itself (as opposed to the window)
3797 if (!mBufferInfo.mCrop.isEmpty()) {
3798 // if the buffer crop is defined, we use that
3799 return mBufferInfo.mCrop;
3800 } else if (mBufferInfo.mBuffer != nullptr) {
3801 // otherwise we use the whole buffer
3802 return mBufferInfo.mBuffer->getBounds();
3803 } else {
3804 // if we don't have a buffer yet, we use an empty/invalid crop
3805 return Rect();
3806 }
3807}
3808
3809uint32_t Layer::getBufferTransform() const {
3810 return mBufferInfo.mTransform;
3811}
3812
3813ui::Dataspace Layer::getDataSpace() const {
3814 return mDrawingState.dataspaceRequested ? getRequestedDataSpace() : ui::Dataspace::UNKNOWN;
3815}
3816
3817ui::Dataspace Layer::getRequestedDataSpace() const {
3818 return hasBufferOrSidebandStream() ? mBufferInfo.mDataspace : mDrawingState.dataspace;
3819}
3820
3821ui::Dataspace Layer::translateDataspace(ui::Dataspace dataspace) {
3822 ui::Dataspace updatedDataspace = dataspace;
3823 // translate legacy dataspaces to modern dataspaces
3824 switch (dataspace) {
3825 case ui::Dataspace::SRGB:
3826 updatedDataspace = ui::Dataspace::V0_SRGB;
3827 break;
3828 case ui::Dataspace::SRGB_LINEAR:
3829 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
3830 break;
3831 case ui::Dataspace::JFIF:
3832 updatedDataspace = ui::Dataspace::V0_JFIF;
3833 break;
3834 case ui::Dataspace::BT601_625:
3835 updatedDataspace = ui::Dataspace::V0_BT601_625;
3836 break;
3837 case ui::Dataspace::BT601_525:
3838 updatedDataspace = ui::Dataspace::V0_BT601_525;
3839 break;
3840 case ui::Dataspace::BT709:
3841 updatedDataspace = ui::Dataspace::V0_BT709;
3842 break;
3843 default:
3844 break;
3845 }
3846
3847 return updatedDataspace;
3848}
3849
3850sp<GraphicBuffer> Layer::getBuffer() const {
3851 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
3852}
3853
Patrick Williamsbb25f802022-08-30 23:02:34 +00003854void Layer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
3855 mTransformHint = getFixedTransformHint();
3856 if (mTransformHint == ui::Transform::ROT_INVALID) {
3857 mTransformHint = displayTransformHint;
3858 }
Vishnu Nair71fcf912022-10-18 09:14:20 -07003859 mSkipReportingTransformHint = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003860}
3861
3862const std::shared_ptr<renderengine::ExternalTexture>& Layer::getExternalTexture() const {
3863 return mBufferInfo.mBuffer;
3864}
3865
3866bool Layer::setColor(const half3& color) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003867 if (mDrawingState.color.rgb == color) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003868 return false;
3869 }
3870
3871 mDrawingState.sequence++;
Vishnu Nairbbceb462022-10-10 04:52:13 +00003872 mDrawingState.color.rgb = color;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003873 mDrawingState.modified = true;
3874 setTransactionFlags(eTransactionNeeded);
3875 return true;
3876}
3877
3878bool Layer::fillsColor() const {
3879 return !hasBufferOrSidebandStream() && mDrawingState.color.r >= 0.0_hf &&
3880 mDrawingState.color.g >= 0.0_hf && mDrawingState.color.b >= 0.0_hf;
3881}
3882
3883bool Layer::hasBlur() const {
3884 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
3885}
3886
Vishnu Nairba354102022-08-01 00:14:18 +00003887void Layer::updateSnapshot(bool updateGeometry) {
3888 if (!getCompositionEngineLayerFE()) {
3889 return;
3890 }
3891
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003892 auto* snapshot = editLayerSnapshot();
Vishnu Nairba354102022-08-01 00:14:18 +00003893 if (updateGeometry) {
3894 prepareBasicGeometryCompositionState();
3895 prepareGeometryCompositionState();
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003896 snapshot->roundedCorner = getRoundedCornerState();
3897 snapshot->stretchEffect = getStretchEffect();
3898 snapshot->transformedBounds = mScreenBounds;
3899 if (mEffectiveShadowRadius > 0.f) {
3900 snapshot->shadowSettings = mFlinger->mDrawingState.globalShadowSettings;
3901
3902 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
3903 // it if transparent regions are present. This may not be necessary since shadows are
3904 // typically cast by layers without transparent regions.
3905 snapshot->shadowSettings.boundaries = mBounds;
3906
3907 const float casterAlpha = snapshot->alpha;
3908 const bool casterIsOpaque =
3909 ((mBufferInfo.mBuffer != nullptr) && isOpaque(mDrawingState));
3910
3911 // If the casting layer is translucent, we need to fill in the shadow underneath the
3912 // layer. Otherwise the generated shadow will only be shown around the casting layer.
3913 snapshot->shadowSettings.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
3914 snapshot->shadowSettings.ambientColor *= casterAlpha;
3915 snapshot->shadowSettings.spotColor *= casterAlpha;
3916 }
3917 snapshot->shadowSettings.length = mEffectiveShadowRadius;
Vishnu Nairba354102022-08-01 00:14:18 +00003918 }
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003919 snapshot->contentOpaque = isOpaque(mDrawingState);
3920 snapshot->isHdrY410 = isHdrY410();
3921 snapshot->bufferNeedsFiltering = bufferNeedsFiltering();
3922 sp<Layer> p = mDrawingParent.promote();
3923 if (p != nullptr) {
3924 snapshot->transform = p->getTransform();
3925 } else {
3926 snapshot->transform.reset();
3927 }
3928 snapshot->bufferSize = getBufferSize(mDrawingState);
3929 snapshot->externalTexture = mBufferInfo.mBuffer;
Vishnu Naire14c6b32022-08-06 04:20:15 +00003930 snapshot->hasReadyFrame = hasReadyFrame();
Vishnu Nairba354102022-08-01 00:14:18 +00003931 preparePerFrameCompositionState();
3932}
3933
Vishnu Naire14c6b32022-08-06 04:20:15 +00003934void Layer::updateChildrenSnapshots(bool updateGeometry) {
3935 for (const sp<Layer>& child : mDrawingChildren) {
3936 child->updateSnapshot(updateGeometry);
3937 child->updateChildrenSnapshots(updateGeometry);
3938 }
3939}
3940
Vishnu Nair0a4fb002022-08-08 02:40:42 +00003941void Layer::updateMetadataSnapshot(const LayerMetadata& parentMetadata) {
3942 mSnapshot->layerMetadata = parentMetadata;
3943 mSnapshot->layerMetadata.merge(mDrawingState.metadata);
3944 for (const sp<Layer>& child : mDrawingChildren) {
3945 child->updateMetadataSnapshot(mSnapshot->layerMetadata);
3946 }
3947}
3948
3949void Layer::updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMetadata,
3950 std::unordered_set<Layer*>& visited) {
3951 if (visited.find(this) != visited.end()) {
3952 ALOGW("Cycle containing layer %s detected in z-order relatives", getDebugName());
3953 return;
3954 }
3955 visited.insert(this);
3956
3957 mSnapshot->relativeLayerMetadata = relativeLayerMetadata;
3958
3959 if (mDrawingState.zOrderRelatives.empty()) {
3960 return;
3961 }
3962 LayerMetadata childRelativeLayerMetadata = mSnapshot->relativeLayerMetadata;
3963 childRelativeLayerMetadata.merge(mSnapshot->layerMetadata);
3964 for (wp<Layer> weakRelative : mDrawingState.zOrderRelatives) {
3965 sp<Layer> relative = weakRelative.promote();
3966 if (!relative) {
3967 continue;
3968 }
3969 relative->updateRelativeMetadataSnapshot(childRelativeLayerMetadata, visited);
3970 }
3971}
3972
Patrick Williams377c0142022-10-13 17:33:38 +00003973LayerSnapshotGuard::LayerSnapshotGuard(Layer* layer) : mLayer(layer) {
Patrick Williams5c259c02022-11-07 23:47:01 +00003974 LOG_ALWAYS_FATAL_IF(!mLayer, "LayerSnapshotGuard received a null layer.");
3975 mLayer->mLayerFE->mSnapshot = std::move(mLayer->mSnapshot);
3976 LOG_ALWAYS_FATAL_IF(!mLayer->mLayerFE->mSnapshot,
3977 "LayerFE snapshot null after taking ownership from layer");
Patrick Williams377c0142022-10-13 17:33:38 +00003978}
3979
3980LayerSnapshotGuard::~LayerSnapshotGuard() {
3981 if (mLayer) {
3982 mLayer->mSnapshot = std::move(mLayer->mLayerFE->mSnapshot);
Patrick Williams5c259c02022-11-07 23:47:01 +00003983 LOG_ALWAYS_FATAL_IF(!mLayer->mSnapshot,
3984 "Layer snapshot null after taking ownership from LayerFE");
Patrick Williams377c0142022-10-13 17:33:38 +00003985 }
3986}
3987
3988LayerSnapshotGuard::LayerSnapshotGuard(LayerSnapshotGuard&& other) : mLayer(other.mLayer) {
3989 other.mLayer = nullptr;
3990}
3991
3992LayerSnapshotGuard& LayerSnapshotGuard::operator=(LayerSnapshotGuard&& other) {
3993 mLayer = other.mLayer;
3994 other.mLayer = nullptr;
3995 return *this;
3996}
3997
Mathias Agopian13127d82013-03-05 17:47:11 -08003998// ---------------------------------------------------------------------------
3999
Marin Shalamanov46084422020-10-13 12:33:42 +02004000std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07004001 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
4002 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02004003}
4004
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07004005} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07004006
4007#if defined(__gl_h_)
4008#error "don't include gl/gl.h in this file"
4009#endif
4010
4011#if defined(__gl2_h_)
4012#error "don't include gl2/gl2.h in this file"
4013#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08004014
4015// TODO(b/129481165): remove the #pragma below and fix conversion issues
4016#pragma clang diagnostic pop // ignored "-Wconversion"