blob: ba1d2c671bb4d4675b307e7a4bd2221e64413eb0 [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;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000131using frontend::LayerSnapshot;
132using frontend::RoundedCornerState;
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800133using gui::GameMode;
134using gui::LayerMetadata;
chaviw3277faf2021-05-19 16:45:23 -0500135using gui::WindowInfo;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800136
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700137using PresentState = frametimeline::SurfaceFrame::PresentState;
138
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700139Layer::Layer(const LayerCreationArgs& args)
Vishnu Naircb8be502022-10-12 19:03:23 +0000140 : sequence(args.sequence),
Ady Abrahamd11bade2022-08-01 16:18:03 -0700141 mFlinger(sp<SurfaceFlinger>::fromExisting(args.flinger)),
Arthur Hung23e07502021-10-15 11:58:19 +0000142 mName(base::StringPrintf("%s#%d", args.name.c_str(), sequence)),
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700143 mClientRef(args.client),
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800144 mWindowType(static_cast<WindowInfo::Type>(
145 args.metadata.getInt32(gui::METADATA_WINDOW_TYPE, 0))),
Tianhao Yao67dd7122022-02-22 17:48:33 +0000146 mLayerCreationFlags(args.flags),
Patrick Williamsbb25f802022-08-30 23:02:34 +0000147 mBorderEnabled(false),
148 mTextureName(args.textureName),
Vishnu Naire14c6b32022-08-06 04:20:15 +0000149 mLayerFE(args.flinger->getFactory().createLayerFE(mName)) {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000150 ALOGV("Creating Layer %s", getDebugName());
151
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700152 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700153 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
154 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
155 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -0700156 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
157 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Robert Carr6a160312021-05-17 12:08:20 -0700158 mDrawingState.flags = layerFlags;
Robert Carr6a160312021-05-17 12:08:20 -0700159 mDrawingState.crop.makeInvalid();
Robert Carr6a160312021-05-17 12:08:20 -0700160 mDrawingState.z = 0;
161 mDrawingState.color.a = 1.0f;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700162 mDrawingState.layerStack = ui::DEFAULT_LAYER_STACK;
Robert Carr6a160312021-05-17 12:08:20 -0700163 mDrawingState.sequence = 0;
Robert Carr6a160312021-05-17 12:08:20 -0700164 mDrawingState.transform.set(0, 0);
165 mDrawingState.frameNumber = 0;
166 mDrawingState.bufferTransform = 0;
167 mDrawingState.transformToDisplayInverse = false;
168 mDrawingState.crop.makeInvalid();
169 mDrawingState.acquireFence = sp<Fence>::make(-1);
170 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
171 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000172 mDrawingState.dataspaceRequested = false;
Robert Carr6a160312021-05-17 12:08:20 -0700173 mDrawingState.hdrMetadata.validTypes = 0;
174 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
175 mDrawingState.cornerRadius = 0.0f;
176 mDrawingState.backgroundBlurRadius = 0;
177 mDrawingState.api = -1;
178 mDrawingState.hasColorTransform = false;
179 mDrawingState.colorSpaceAgnostic = false;
180 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
181 mDrawingState.metadata = args.metadata;
182 mDrawingState.shadowRadius = 0.f;
183 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
184 mDrawingState.frameTimelineInfo = {};
185 mDrawingState.postTime = -1;
186 mDrawingState.destinationFrame.makeInvalid();
Robin Leeedb375d2021-09-10 12:03:42 +0000187 mDrawingState.isTrustedOverlay = false;
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -0700188 mDrawingState.dropInputMode = gui::DropInputMode::NONE;
Sally Qi81d95e62022-03-21 19:41:33 -0700189 mDrawingState.dimmingEnabled = true;
Andy Labrada096227e2022-06-15 16:58:11 +0000190 mDrawingState.defaultFrameRateCompatibility = FrameRateCompatibility::Default;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700191
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700192 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
193 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700194 mDrawingState.color.r = -1.0_hf;
195 mDrawingState.color.g = -1.0_hf;
196 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700197 }
Dominik Laskowski4376bd82022-07-07 11:50:20 -0700198
Dominik Laskowski596a2562022-10-28 11:26:12 -0400199 mFrameTracker.setDisplayRefreshPeriod(args.flinger->mScheduler->getLeaderVsyncPeriod());
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;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000213 mSnapshot->parentTransform = {};
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 Williams70093fd2022-12-12 16:51:52 +0000221 LOG_ALWAYS_FATAL_IF(std::this_thread::get_id() != mFlinger->mMainThreadId,
222 "Layer destructor called off the main thread.");
223
Patrick Williamsbb25f802022-08-30 23:02:34 +0000224 // The original layer and the clone layer share the same texture and buffer. Therefore, only
225 // one of the layers, in this case the original layer, needs to handle the deletion. The
226 // original layer and the clone should be removed at the same time so there shouldn't be any
227 // issue with the clone layer trying to use the texture.
228 if (mBufferInfo.mBuffer != nullptr) {
229 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
230 mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
231 mBufferInfo.mFence,
232 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
233 mOwnerUid));
234 }
235 if (!isClone()) {
236 // The original layer and the clone layer share the same texture. Therefore, only one of
237 // the layers, in this case the original layer, needs to handle the deletion. The original
238 // layer and the clone should be removed at the same time so there shouldn't be any issue
239 // with the clone layer trying to use the deleted texture.
240 mFlinger->deleteTextureAsync(mTextureName);
241 }
242 const int32_t layerId = getSequence();
243 mFlinger->mTimeStats->onDestroy(layerId);
244 mFlinger->mFrameTracer->onDestroy(layerId);
245
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000246 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700247 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700248
249 if (mDrawingState.sidebandStream != nullptr) {
250 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
251 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700252 if (mHadClonedChild) {
253 mFlinger->mNumClones--;
254 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700255}
256
Mathias Agopian13127d82013-03-05 17:47:11 -0800257// ---------------------------------------------------------------------------
258// callbacks
259// ---------------------------------------------------------------------------
260
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700261void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700262 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700263 return;
264 }
Robert Carr2e102c92018-10-23 12:11:15 -0700265
Robert Carr6a160312021-05-17 12:08:20 -0700266 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700267 if (strongRelative == nullptr) {
268 setZOrderRelativeOf(nullptr);
269 return;
270 }
271
272 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700273 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700274 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800275 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700276 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700277}
278
279void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700280 if (!mRemovedFromDrawingState) {
281 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700282 mFlinger->mScheduler->deregisterLayer(this);
283 }
Rob Carr4bba3702018-10-08 21:53:30 +0000284
Ady Abrahamd11bade2022-08-01 16:18:03 -0700285 mFlinger->markLayerPendingRemovalLocked(sp<Layer>::fromExisting(this));
Chia-I Wuc6657022017-08-15 11:18:17 -0700286}
Chia-I Wu38512252017-05-17 14:36:16 -0700287
chaviw68d4dab2020-06-08 15:07:32 -0700288sp<Layer> Layer::getRootLayer() {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000289 sp<Layer> parent = getParent();
chaviw68d4dab2020-06-08 15:07:32 -0700290 if (parent == nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700291 return sp<Layer>::fromExisting(this);
chaviw68d4dab2020-06-08 15:07:32 -0700292 }
293 return parent->getRootLayer();
294}
295
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700296void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700297 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
298 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700299 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700300
301 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700302 layer->removeFromCurrentState();
303 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700304 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700305}
306
chaviw61626f22018-11-15 16:26:27 -0800307void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700308 if (mRemovedFromDrawingState) {
309 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700310 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700311 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700312 }
chaviw61626f22018-11-15 16:26:27 -0800313
314 for (const auto& child : mCurrentChildren) {
315 child->addToCurrentState();
316 }
317}
318
Mathias Agopian13127d82013-03-05 17:47:11 -0800319// ---------------------------------------------------------------------------
320// set-up
321// ---------------------------------------------------------------------------
322
chaviw13fdc492017-06-27 12:40:18 -0700323bool Layer::getPremultipledAlpha() const {
324 return mPremultipliedAlpha;
325}
326
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700327sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800328 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700329 if (mGetHandleCalled) {
330 ALOGE("Get handle called twice" );
331 return nullptr;
332 }
333 mGetHandleCalled = true;
Vishnu Nair07e2a482022-10-18 19:18:16 +0000334 return sp<LayerHandle>::make(mFlinger, sp<Layer>::fromExisting(this));
Mathias Agopian13127d82013-03-05 17:47:11 -0800335}
336
337// ---------------------------------------------------------------------------
338// h/w composer set-up
339// ---------------------------------------------------------------------------
340
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700341static Rect reduce(const Rect& win, const Region& exclude) {
342 if (CC_LIKELY(exclude.isEmpty())) {
343 return win;
344 }
345 if (exclude.isRect()) {
346 return win.reduce(exclude.getBounds());
347 }
348 return Region(win).subtract(exclude).getBounds();
349}
350
Dan Stoza80d61162017-12-20 15:57:52 -0800351static FloatRect reduce(const FloatRect& win, const Region& exclude) {
352 if (CC_LIKELY(exclude.isEmpty())) {
353 return win;
354 }
355 // Convert through Rect (by rounding) for lack of FloatRegion
356 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
357}
358
Vishnu Nair4351ad52019-02-11 14:13:02 -0800359Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800360 if (!reduceTransparentRegion) {
361 return Rect{mScreenBounds};
362 }
363
364 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800365 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800366 // Transform to screen space.
367 bounds = t.transform(bounds);
368 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700369}
370
Vishnu Nair4351ad52019-02-11 14:13:02 -0800371FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000372 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800373 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700374}
375
Vishnu Nairf0c28512019-02-08 12:40:28 -0800376FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
377 // Subtract the transparent region and snap to the bounds.
378 return reduce(mBounds, activeTransparentRegion);
379}
380
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700381void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
382 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800383 const State& s(getDrawingState());
384
385 // Calculate effective layer transform
386 mEffectiveTransform = parentTransform * getActiveTransform(s);
387
Garfield Tan2c1782c2022-02-16 15:25:05 -0800388 if (CC_UNLIKELY(!isTransformValid())) {
389 ALOGW("Stop computing bounds for %s because it has invalid transformation.",
390 getDebugName());
391 return;
392 }
393
Vishnu Nair4351ad52019-02-11 14:13:02 -0800394 // Transform parent bounds to layer space
395 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
396
Vishnu Nairc652ff82019-03-15 12:48:54 -0700397 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800398 mSourceBounds = computeSourceBounds(parentBounds);
399
400 // Calculate bounds by croping diplay frame with layer crop and parent bounds
401 FloatRect bounds = mSourceBounds;
402 const Rect layerCrop = getCrop(s);
403 if (!layerCrop.isEmpty()) {
404 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
405 }
406 bounds = bounds.intersect(parentBounds);
407
408 mBounds = bounds;
409 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700410
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700411 // Use the layer's own shadow radius if set. Otherwise get the radius from
412 // parent.
413 if (s.shadowRadius > 0.f) {
414 mEffectiveShadowRadius = s.shadowRadius;
415 } else {
416 mEffectiveShadowRadius = parentShadowRadius;
417 }
418
419 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
420 // don't pass it to its children.
421 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
422
Vishnu Nair4351ad52019-02-11 14:13:02 -0800423 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700424 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800425 }
Vishnu Nairba354102022-08-01 00:14:18 +0000426
427 if (mPotentialCursor) {
428 prepareCursorCompositionState();
429 }
Vishnu Nair4351ad52019-02-11 14:13:02 -0800430}
431
Vishnu Nair60356342018-11-13 13:00:45 -0800432Rect Layer::getCroppedBufferSize(const State& s) const {
433 Rect size = getBufferSize(s);
434 Rect crop = getCrop(s);
435 if (!crop.isEmpty() && size.isValid()) {
436 size.intersect(crop, &size);
437 } else if (!crop.isEmpty()) {
438 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700439 }
Vishnu Nair60356342018-11-13 13:00:45 -0800440 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800441}
442
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700443void Layer::setupRoundedCornersCropCoordinates(Rect win,
444 const FloatRect& roundedCornersCrop) const {
445 // Translate win by the rounded corners rect coordinates, to have all values in
446 // layer coordinate space.
447 win.left -= roundedCornersCrop.left;
448 win.right -= roundedCornersCrop.left;
449 win.top -= roundedCornersCrop.top;
450 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700451}
452
Lloyd Piquede196652020-01-22 17:29:58 -0800453void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800454 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800455 const auto alpha = static_cast<float>(getAlpha());
456 const bool opaque = isOpaque(drawingState);
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700457 const bool usesRoundedCorners = hasRoundedCorners();
Lloyd Piquec6687342019-03-07 21:34:57 -0800458
459 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
460 if (!opaque || alpha != 1.0f) {
461 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
462 : Hwc2::IComposerClient::BlendMode::COVERAGE;
463 }
464
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000465 auto* snapshot = editLayerSnapshot();
466 snapshot->outputFilter = getOutputFilter();
467 snapshot->isVisible = isVisible();
468 snapshot->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
469 snapshot->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800470
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000471 snapshot->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800472 contentDirty = false;
473
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000474 snapshot->geomLayerBounds = mBounds;
475 snapshot->geomLayerTransform = getTransform();
476 snapshot->geomInverseLayerTransform = snapshot->geomLayerTransform.inverse();
477 snapshot->transparentRegionHint = getActiveTransparentRegion(drawingState);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000478 snapshot->localTransform = getActiveTransform(drawingState);
479 snapshot->localTransformInverse = snapshot->localTransform.inverse();
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000480 snapshot->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
481 snapshot->alpha = alpha;
482 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
483 snapshot->blurRegions = drawingState.blurRegions;
484 snapshot->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800485}
486
Lloyd Piquede196652020-01-22 17:29:58 -0800487void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800488 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000489 auto* snapshot = editLayerSnapshot();
David Sodman15094112018-10-11 09:39:37 -0700490
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000491 snapshot->geomBufferSize = getBufferSize(drawingState);
492 snapshot->geomContentCrop = getBufferCrop();
493 snapshot->geomCrop = getCrop(drawingState);
494 snapshot->geomBufferTransform = getBufferTransform();
495 snapshot->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
496 snapshot->geomUsesSourceCrop = usesSourceCrop();
497 snapshot->isSecure = isSecure();
Lloyd Piquede196652020-01-22 17:29:58 -0800498
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000499 snapshot->metadata.clear();
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800500 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
501 for (const auto& [key, mandatory] : supportedMetadata) {
502 const auto& genericLayerMetadataCompatibilityMap =
503 mFlinger->getGenericLayerMetadataKeyMap();
504 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
505 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
506 continue;
507 }
508 const uint32_t id = compatIter->second;
509
510 auto it = drawingState.metadata.mMap.find(id);
511 if (it == std::end(drawingState.metadata.mMap)) {
512 continue;
513 }
514
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000515 snapshot->metadata.emplace(key,
516 compositionengine::GenericLayerMetadataEntry{mandatory,
517 it->second});
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800518 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800519}
David Sodmanba340492018-08-05 21:51:33 -0700520
Lloyd Piquede196652020-01-22 17:29:58 -0800521void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800522 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000523 auto* snapshot = editLayerSnapshot();
Lloyd Piquef5275482019-01-29 18:42:42 -0800524
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000525 snapshot->forceClientComposition = false;
Lloyd Piquede196652020-01-22 17:29:58 -0800526
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000527 snapshot->isColorspaceAgnostic = isColorSpaceAgnostic();
528 snapshot->dataspace = getDataSpace();
529 snapshot->colorTransform = getColorTransform();
530 snapshot->colorTransformIsIdentity = !hasColorTransform();
531 snapshot->surfaceDamage = surfaceDamageRegion;
532 snapshot->hasProtectedContent = isProtected();
533 snapshot->dimmingEnabled = isDimmingEnabled();
Lloyd Pique688abd42019-02-15 15:42:24 -0800534
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700535 const bool usesRoundedCorners = hasRoundedCorners();
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700536
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000537 snapshot->isOpaque = isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800538
539 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400540 // Rounded corners no longer force client composition, since we may use a
541 // hole punch so that the layer will appear to have rounded corners.
542 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000543 snapshot->stretchEffect.hasEffect()) {
544 snapshot->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800545 }
Vishnu Nairb801a982021-11-02 15:12:08 -0700546 // If there are no visible region changes, we still need to update blur parameters.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000547 snapshot->blurRegions = drawingState.blurRegions;
548 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400549
550 // Layer framerate is used in caching decisions.
551 // Retrieve it from the scheduler which maintains an instance of LayerHistory, and store it in
552 // LayerFECompositionState where it would be visible to Flattener.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000553 snapshot->fps = mFlinger->getLayerFramerate(systemTime(), getSequence());
Patrick Williamsbb25f802022-08-30 23:02:34 +0000554
555 if (hasBufferOrSidebandStream()) {
556 preparePerFrameBufferCompositionState();
557 } else {
558 preparePerFrameEffectsCompositionState();
559 }
560}
561
562void Layer::preparePerFrameBufferCompositionState() {
563 // Sideband layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000564 auto* snapshot = editLayerSnapshot();
565 if (snapshot->sidebandStream.get() && !snapshot->sidebandStreamHasFrame) {
566 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000567 aidl::android::hardware::graphics::composer3::Composition::SIDEBAND;
568 return;
569 } else if ((mDrawingState.flags & layer_state_t::eLayerIsDisplayDecoration) != 0) {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000570 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000571 aidl::android::hardware::graphics::composer3::Composition::DISPLAY_DECORATION;
572 } else {
573 // Normal buffer layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000574 snapshot->hdrMetadata = mBufferInfo.mHdrMetadata;
575 snapshot->compositionType = mPotentialCursor
Patrick Williamsbb25f802022-08-30 23:02:34 +0000576 ? aidl::android::hardware::graphics::composer3::Composition::CURSOR
577 : aidl::android::hardware::graphics::composer3::Composition::DEVICE;
578 }
579
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000580 snapshot->buffer = getBuffer();
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000581 snapshot->acquireFence = mBufferInfo.mFence;
582 snapshot->frameNumber = mBufferInfo.mFrameNumber;
583 snapshot->sidebandStreamHasFrame = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000584}
585
586void Layer::preparePerFrameEffectsCompositionState() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000587 auto* snapshot = editLayerSnapshot();
588 snapshot->color = getColor();
589 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000590 aidl::android::hardware::graphics::composer3::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800591}
592
Lloyd Piquede196652020-01-22 17:29:58 -0800593void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800594 const State& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000595 auto* snapshot = editLayerSnapshot();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800596
597 // Apply the layer's transform, followed by the display's global transform
598 // Here we're guaranteed that the layer's transform preserves rects
599 Rect win = getCroppedBufferSize(drawingState);
600 // Subtract the transparent region and snap to the bounds
601 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
602 Rect frame(getTransform().transform(bounds));
603
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000604 snapshot->cursorFrame = frame;
Lloyd Piquede196652020-01-22 17:29:58 -0800605}
606
Lloyd Piquea83776c2019-01-29 18:42:32 -0800607const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700608 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800609}
610
Mathias Agopian13127d82013-03-05 17:47:11 -0800611// ---------------------------------------------------------------------------
612// drawing...
613// ---------------------------------------------------------------------------
614
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500615aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType(
616 const DisplayDevice& display) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700617 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800618 if (outputLayer == nullptr) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500619 return aidl::android::hardware::graphics::composer3::Composition::INVALID;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800620 }
621 if (outputLayer->getState().hwc) {
622 return (*outputLayer->getState().hwc).hwcCompositionType;
623 } else {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500624 return aidl::android::hardware::graphics::composer3::Composition::CLIENT;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800625 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800626}
627
Mathias Agopian13127d82013-03-05 17:47:11 -0800628// ----------------------------------------------------------------------------
629// local state
630// ----------------------------------------------------------------------------
631
David Sodman41fdfc92017-11-06 16:09:56 -0800632bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800633 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700634 if (s.flags & layer_state_t::eLayerSecure) {
635 return true;
636 }
637
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000638 const auto p = mDrawingParent.promote();
Garfield Tande619fa2020-10-02 17:13:53 -0700639 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700640}
641
Mathias Agopian13127d82013-03-05 17:47:11 -0800642// ----------------------------------------------------------------------------
643// transaction
644// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800645
Marissa Wall61c58622018-07-18 10:12:20 -0700646uint32_t Layer::doTransaction(uint32_t flags) {
647 ATRACE_CALL();
648
Robert Carr0758e5d2021-03-11 22:15:04 -0800649 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700650 mDrawingStateModified = mDrawingState.modified;
651 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700652
Alec Mourib416efd2018-09-06 21:01:59 +0000653 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700654
Robert Carr6a160312021-05-17 12:08:20 -0700655 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800656 // invalidate and recompute the visible regions if needed
657 flags |= Layer::eVisibleRegion;
658 }
659
Robert Carr6a160312021-05-17 12:08:20 -0700660 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800661 // invalidate and recompute the visible regions if needed
Arthur Hung9ed43392022-05-27 06:31:57 +0000662 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800663 flags |= eVisibleRegion;
664 this->contentDirty = true;
665
666 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700667 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800668 }
669
Arthur Hung9ed43392022-05-27 06:31:57 +0000670 if (!mPotentialCursor && (flags & Layer::eVisibleRegion)) {
671 mFlinger->mUpdateInputInfo = true;
672 }
673
Robert Carr6a160312021-05-17 12:08:20 -0700674 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800675
Mathias Agopian13127d82013-03-05 17:47:11 -0800676 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677}
678
Robert Carr6a160312021-05-17 12:08:20 -0700679void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000680 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
681 // bufferSurfaceFrameTX will be presented in latchBuffer.
682 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
683 if (surfaceFrame->getPresentState() != PresentState::Presented) {
684 // With applyPendingStates, we could end up having presented surfaceframes from previous
685 // states
686 surfaceFrame->setPresentState(PresentState::Presented);
687 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
688 }
689 }
Robert Carr6a160312021-05-17 12:08:20 -0700690 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700691}
692
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700693uint32_t Layer::clearTransactionFlags(uint32_t mask) {
694 const auto flags = mTransactionFlags & mask;
695 mTransactionFlags &= ~mask;
696 return flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800697}
698
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700699void Layer::setTransactionFlags(uint32_t mask) {
700 mTransactionFlags |= mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800701}
702
Robert Carr1f0a16a2016-10-24 16:27:39 -0700703bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
704 ssize_t idx = mCurrentChildren.indexOf(childLayer);
705 if (idx < 0) {
706 return false;
707 }
708 if (childLayer->setLayer(z)) {
709 mCurrentChildren.removeAt(idx);
710 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800711 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700712 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800713 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700714}
715
Robert Carr503c7042017-09-27 15:06:08 -0700716bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
717 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
718 ssize_t idx = mCurrentChildren.indexOf(childLayer);
719 if (idx < 0) {
720 return false;
721 }
722 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
723 mCurrentChildren.removeAt(idx);
724 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800725 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700726 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800727 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700728}
729
Robert Carrae060832016-11-28 10:51:00 -0800730bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700731 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
732 mDrawingState.sequence++;
733 mDrawingState.z = z;
734 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700735
Robert Carra70e91c2021-06-11 13:59:52 -0700736 mFlinger->mSomeChildrenChanged = true;
737
Robert Carrdb66e622017-04-10 16:55:57 -0700738 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700739 if (mDrawingState.zOrderRelativeOf != nullptr) {
740 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700741 if (strongRelative != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700742 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700743 }
chaviw606e5cf2019-03-01 10:12:10 -0800744 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700745 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800746 setTransactionFlags(eTransactionNeeded);
747 return true;
748}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700749
Robert Carrdb66e622017-04-10 16:55:57 -0700750void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700751 mDrawingState.zOrderRelatives.remove(relative);
752 mDrawingState.sequence++;
753 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700754 setTransactionFlags(eTransactionNeeded);
755}
756
757void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700758 mDrawingState.zOrderRelatives.add(relative);
759 mDrawingState.modified = true;
760 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700761 setTransactionFlags(eTransactionNeeded);
762}
763
chaviw606e5cf2019-03-01 10:12:10 -0800764void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700765 mDrawingState.zOrderRelativeOf = relativeOf;
766 mDrawingState.sequence++;
767 mDrawingState.modified = true;
768 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700769
chaviw606e5cf2019-03-01 10:12:10 -0800770 setTransactionFlags(eTransactionNeeded);
771}
772
Robert Carr503d2bd2017-12-04 15:49:47 -0800773bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nair07e2a482022-10-18 19:18:16 +0000774 sp<Layer> relative = LayerHandle::getLayer(relativeToHandle);
Robert Carrdb66e622017-04-10 16:55:57 -0700775 if (relative == nullptr) {
776 return false;
777 }
778
Robert Carr6a160312021-05-17 12:08:20 -0700779 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
780 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800781 return false;
782 }
783
Robert Carr88b85e12022-03-21 15:47:35 -0700784 if (CC_UNLIKELY(relative->usingRelativeZ(LayerVector::StateSet::Drawing)) &&
785 (relative->mDrawingState.zOrderRelativeOf == this)) {
786 ALOGE("Detected relative layer loop between %s and %s",
787 mName.c_str(), relative->mName.c_str());
788 ALOGE("Ignoring new call to set relative layer");
789 return false;
790 }
791
Robert Carra70e91c2021-06-11 13:59:52 -0700792 mFlinger->mSomeChildrenChanged = true;
793
Robert Carr6a160312021-05-17 12:08:20 -0700794 mDrawingState.sequence++;
795 mDrawingState.modified = true;
796 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700797
Robert Carr6a160312021-05-17 12:08:20 -0700798 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700799 if (oldZOrderRelativeOf != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700800 oldZOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
chaviw9ab4bd12017-11-03 13:11:00 -0700801 }
chaviw606e5cf2019-03-01 10:12:10 -0800802 setZOrderRelativeOf(relative);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700803 relative->addZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700804
805 setTransactionFlags(eTransactionNeeded);
806
807 return true;
808}
809
Winson Chunga30f7c92021-06-29 15:42:56 -0700810bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
811 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
812 mDrawingState.isTrustedOverlay = isTrustedOverlay;
813 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +0000814 mFlinger->mUpdateInputInfo = true;
Winson Chunga30f7c92021-06-29 15:42:56 -0700815 setTransactionFlags(eTransactionNeeded);
816 return true;
817}
818
819bool Layer::isTrustedOverlay() const {
820 if (getDrawingState().isTrustedOverlay) {
821 return true;
822 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000823 const auto& p = mDrawingParent.promote();
Winson Chunga30f7c92021-06-29 15:42:56 -0700824 return (p != nullptr) && p->isTrustedOverlay();
825}
826
Dan Stoza9e56aa02015-11-02 13:00:03 -0800827bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700828 if (mDrawingState.color.a == alpha) return false;
829 mDrawingState.sequence++;
830 mDrawingState.color.a = alpha;
831 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800832 setTransactionFlags(eTransactionNeeded);
833 return true;
834}
chaviw13fdc492017-06-27 12:40:18 -0700835
Valerie Haudd0b7572019-01-29 14:59:27 -0800836bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700837 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700838 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800839 }
Robert Carr6a160312021-05-17 12:08:20 -0700840 mDrawingState.sequence++;
841 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800842 setTransactionFlags(eTransactionNeeded);
843
Robert Carr6a160312021-05-17 12:08:20 -0700844 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800845 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800846 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700847 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700848 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800849 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700850 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700851
Valerie Haudd0b7572019-01-29 14:59:27 -0800852 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700853 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800854 mFlinger->mLayersAdded = true;
855 // set up SF to handle added color layer
856 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700857 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800858 }
859 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700860 } else if (mDrawingState.bgColorLayer && alpha == 0) {
861 mDrawingState.bgColorLayer->reparent(nullptr);
862 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800863 return true;
864 }
865
Robert Carr6a160312021-05-17 12:08:20 -0700866 mDrawingState.bgColorLayer->setColor(color);
867 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
868 mDrawingState.bgColorLayer->setAlpha(alpha);
869 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800870
chaviw13fdc492017-06-27 12:40:18 -0700871 return true;
872}
873
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700874bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700875 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700876
Robert Carr6a160312021-05-17 12:08:20 -0700877 mDrawingState.sequence++;
878 mDrawingState.cornerRadius = cornerRadius;
879 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700880 setTransactionFlags(eTransactionNeeded);
881 return true;
882}
883
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800884bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700885 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Vishnu Nairb801a982021-11-02 15:12:08 -0700886 // If we start or stop drawing blur then the layer's visibility state may change so increment
887 // the magic sequence number.
888 if (mDrawingState.backgroundBlurRadius == 0 || backgroundBlurRadius == 0) {
889 mDrawingState.sequence++;
890 }
Robert Carr6a160312021-05-17 12:08:20 -0700891 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
892 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800893 setTransactionFlags(eTransactionNeeded);
894 return true;
895}
Marissa Wall61c58622018-07-18 10:12:20 -0700896
Mathias Agopian13127d82013-03-05 17:47:11 -0800897bool Layer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000898 mDrawingState.sequence++;
899 mDrawingState.transparentRegionHint = transparent;
Robert Carr6a160312021-05-17 12:08:20 -0700900 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800901 setTransactionFlags(eTransactionNeeded);
902 return true;
903}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100904
Lucas Dupinc3800b82020-10-02 16:24:48 -0700905bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Vishnu Nairb801a982021-11-02 15:12:08 -0700906 // If we start or stop drawing blur then the layer's visibility state may change so increment
907 // the magic sequence number.
908 if (mDrawingState.blurRegions.size() == 0 || blurRegions.size() == 0) {
909 mDrawingState.sequence++;
910 }
Robert Carr6a160312021-05-17 12:08:20 -0700911 mDrawingState.blurRegions = blurRegions;
912 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700913 setTransactionFlags(eTransactionNeeded);
914 return true;
915}
916
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800917bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700918 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
919 if (mDrawingState.flags == newFlags) return false;
920 mDrawingState.sequence++;
921 mDrawingState.flags = newFlags;
922 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800923 setTransactionFlags(eTransactionNeeded);
924 return true;
925}
Robert Carr99e27f02016-06-16 15:18:02 -0700926
chaviw25714502021-02-11 10:01:08 -0800927bool Layer::setCrop(const Rect& crop) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000928 if (mDrawingState.crop == crop) return false;
Robert Carr6a160312021-05-17 12:08:20 -0700929 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700930 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700931
Robert Carr6a160312021-05-17 12:08:20 -0700932 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800933 setTransactionFlags(eTransactionNeeded);
934 return true;
935}
Robert Carr8d5227b2017-03-16 15:41:03 -0700936
Evan Roskyef876c92019-01-25 17:46:06 -0800937bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700938 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
939 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -0800940 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800941 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500942}
943
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700944bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -0700945 if (mDrawingState.layerStack == layerStack) return false;
946 mDrawingState.sequence++;
947 mDrawingState.layerStack = layerStack;
948 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800949 setTransactionFlags(eTransactionNeeded);
950 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700951}
952
Peiyong Linc502cb72019-03-01 15:00:23 -0800953bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -0700954 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800955 return false;
956 }
Robert Carr6a160312021-05-17 12:08:20 -0700957 mDrawingState.sequence++;
958 mDrawingState.colorSpaceAgnostic = agnostic;
959 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -0800960 setTransactionFlags(eTransactionNeeded);
961 return true;
962}
963
Sally Qi81d95e62022-03-21 19:41:33 -0700964bool Layer::setDimmingEnabled(const bool dimmingEnabled) {
965 if (mDrawingState.dimmingEnabled == dimmingEnabled) return false;
966
967 mDrawingState.sequence++;
968 mDrawingState.dimmingEnabled = dimmingEnabled;
969 mDrawingState.modified = true;
970 setTransactionFlags(eTransactionNeeded);
971 return true;
972}
973
Ana Krulecc84d09b2019-11-02 23:10:29 +0100974bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -0700975 if (mDrawingState.frameRateSelectionPriority == priority) return false;
976 mDrawingState.frameRateSelectionPriority = priority;
977 mDrawingState.sequence++;
978 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100979 setTransactionFlags(eTransactionNeeded);
980 return true;
981}
982
983int32_t Layer::getFrameRateSelectionPriority() const {
984 // Check if layer has priority set.
985 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
986 return mDrawingState.frameRateSelectionPriority;
987 }
988 // If not, search whether its parents have it set.
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000989 sp<Layer> parent = getParent();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100990 if (parent != nullptr) {
991 return parent->getFrameRateSelectionPriority();
992 }
993
994 return Layer::PRIORITY_UNSET;
995}
996
Andy Labrada096227e2022-06-15 16:58:11 +0000997bool Layer::setDefaultFrameRateCompatibility(FrameRateCompatibility compatibility) {
998 if (mDrawingState.defaultFrameRateCompatibility == compatibility) return false;
999 mDrawingState.defaultFrameRateCompatibility = compatibility;
1000 mDrawingState.modified = true;
1001 mFlinger->mScheduler->setDefaultFrameRateCompatibility(this);
1002 setTransactionFlags(eTransactionNeeded);
1003 return true;
1004}
1005
1006scheduler::LayerInfo::FrameRateCompatibility Layer::getDefaultFrameRateCompatibility() const {
1007 return mDrawingState.defaultFrameRateCompatibility;
1008}
1009
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001010bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1011 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1012};
1013
Vishnu Nair73908d12022-10-24 21:46:42 -07001014ui::LayerStack Layer::getLayerStack(LayerVector::StateSet state) const {
1015 bool useDrawing = state == LayerVector::StateSet::Drawing;
1016 const auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1017 if (parent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001018 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001019 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001020 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001021}
1022
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001023bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001024 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001025 return false;
1026 }
1027
Robert Carr6a160312021-05-17 12:08:20 -07001028 mDrawingState.sequence++;
1029 mDrawingState.shadowRadius = shadowRadius;
1030 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001031 setTransactionFlags(eTransactionNeeded);
1032 return true;
1033}
1034
Vishnu Nair6213bd92020-05-08 17:42:25 -07001035bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001036 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001037 return false;
1038 }
1039
Robert Carr6a160312021-05-17 12:08:20 -07001040 mDrawingState.sequence++;
1041 mDrawingState.fixedTransformHint = fixedTransformHint;
1042 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001043 setTransactionFlags(eTransactionNeeded);
1044 return true;
1045}
1046
John Reckcdb4ed72021-02-04 13:39:33 -05001047bool Layer::setStretchEffect(const StretchEffect& effect) {
1048 StretchEffect temp = effect;
1049 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001050 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001051 return false;
1052 }
Robert Carr6a160312021-05-17 12:08:20 -07001053 mDrawingState.sequence++;
1054 mDrawingState.stretchEffect = temp;
1055 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001056 setTransactionFlags(eTransactionNeeded);
1057 return true;
1058}
1059
John Reckc00c6692021-02-16 11:37:33 -05001060StretchEffect Layer::getStretchEffect() const {
1061 if (mDrawingState.stretchEffect.hasEffect()) {
1062 return mDrawingState.stretchEffect;
1063 }
1064
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001065 sp<Layer> parent = getParent();
John Reckc00c6692021-02-16 11:37:33 -05001066 if (parent != nullptr) {
1067 auto effect = parent->getStretchEffect();
1068 if (effect.hasEffect()) {
1069 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1070 return effect;
1071 }
1072 }
1073 return StretchEffect{};
1074}
1075
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001076bool Layer::enableBorder(bool shouldEnable, float width, const half4& color) {
1077 if (mBorderEnabled == shouldEnable && mBorderWidth == width && mBorderColor == color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00001078 return false;
1079 }
1080 mBorderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001081 mBorderWidth = width;
1082 mBorderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001083 return true;
1084}
1085
1086bool Layer::isBorderEnabled() {
1087 return mBorderEnabled;
1088}
1089
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001090float Layer::getBorderWidth() {
1091 return mBorderWidth;
1092}
1093
1094const half4& Layer::getBorderColor() {
1095 return mBorderColor;
1096}
1097
Ady Abrahama850c182021-08-04 13:04:37 -07001098bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1099 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1100 const auto frameRate = [&] {
1101 if (mDrawingState.frameRate.rate.isValid() ||
1102 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1103 return mDrawingState.frameRate;
1104 }
1105
1106 return parentFrameRate;
1107 }();
1108
1109 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1110
1111 // The frame rate is propagated to the children
1112 bool childrenHaveFrameRate = false;
1113 for (const sp<Layer>& child : mCurrentChildren) {
1114 childrenHaveFrameRate |=
1115 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1116 }
1117
1118 // If we don't have a valid frame rate, but the children do, we set this
1119 // layer as NoVote to allow the children to control the refresh rate
1120 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1121 childrenHaveFrameRate) {
1122 *transactionNeeded |=
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001123 setFrameRateForLayerTree(FrameRate(Fps(), FrameRateCompatibility::NoVote));
Ady Abrahama850c182021-08-04 13:04:37 -07001124 }
1125
1126 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1127 // the same reason we are allowing touch boost for those layers. See
Ady Abraham68636062022-11-16 17:07:25 -08001128 // RefreshRateSelector::rankFrameRates for details.
Ady Abrahama850c182021-08-04 13:04:37 -07001129 const auto layerVotedWithDefaultCompatibility =
1130 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1131 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1132 const auto layerVotedWithExactCompatibility =
1133 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1134 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1135 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1136}
1137
Ady Abraham60e42ea2020-03-09 19:17:31 -07001138void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001139 const auto root = [&]() -> sp<Layer> {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001140 sp<Layer> layer = sp<Layer>::fromExisting(this);
Ady Abrahama850c182021-08-04 13:04:37 -07001141 while (auto parent = layer->getParent()) {
1142 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001143 }
Ady Abrahama850c182021-08-04 13:04:37 -07001144 return layer;
1145 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001146
Ady Abraham60e42ea2020-03-09 19:17:31 -07001147 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001148 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001149
Ady Abrahama850c182021-08-04 13:04:37 -07001150 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001151 if (transactionNeeded) {
1152 mFlinger->setTransactionFlags(eTraversalNeeded);
1153 }
1154}
1155
Ady Abraham71c437d2020-01-31 15:56:57 -08001156bool Layer::setFrameRate(FrameRate frameRate) {
Robert Carr6a160312021-05-17 12:08:20 -07001157 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001158 return false;
1159 }
1160
Robert Carr6a160312021-05-17 12:08:20 -07001161 mDrawingState.sequence++;
1162 mDrawingState.frameRate = frameRate;
1163 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001164
1165 updateTreeHasFrameRateVote();
1166
Steven Thomas3172e202020-01-06 19:25:30 -08001167 setTransactionFlags(eTransactionNeeded);
1168 return true;
1169}
1170
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001171void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1172 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001173 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001174
1175 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1176 // there are two transactions with the same token, the first one without a buffer and the
1177 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1178 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001179 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1180 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001181 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001182 mDrawingState.bufferSurfaceFrameTX = it->second;
1183 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1184 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1185 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001186 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001187 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001188 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1189 }
1190}
1191
1192void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1193 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001194 mDrawingState.frameTimelineInfo = info;
1195 mDrawingState.postTime = postTime;
1196 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001197 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001198
Robert Carr6a160312021-05-17 12:08:20 -07001199 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001200 bufferSurfaceFrameTX != nullptr) {
1201 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1202 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1203 // being used for BufferSurfaceFrame, don't create a new one.
1204 return;
1205 }
1206 }
1207 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1208 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1209 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001210 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1211 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001212 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001213 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001214 } else {
1215 if (it->second->getPresentState() == PresentState::Presented) {
1216 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1217 // have been from previous vsync.
1218 it->second = createSurfaceFrameForTransaction(info, postTime);
1219 }
1220 }
1221}
1222
1223void Layer::addSurfaceFrameDroppedForBuffer(
1224 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001225 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001226 surfaceFrame->setPresentState(PresentState::Dropped);
1227 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1228}
1229
1230void Layer::addSurfaceFramePresentedForBuffer(
1231 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1232 nsecs_t currentLatchTime) {
1233 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1234 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1235 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1236 mLastLatchTime = currentLatchTime;
1237}
1238
1239std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1240 const FrameTimelineInfo& info, nsecs_t postTime) {
1241 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001242 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1243 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001244 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001245 /*isBuffer*/ false, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001246 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001247 // For Transactions, the post time is considered to be both queue and acquire fence time.
1248 surfaceFrame->setActualQueueTime(postTime);
1249 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001250 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1251 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001252 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001253 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001254 onSurfaceFrameCreated(surfaceFrame);
1255 return surfaceFrame;
1256}
1257
1258std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1259 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1260 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001261 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001262 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001263 /*isBuffer*/ true, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001264 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001265 // For buffers, acquire fence time will set during latch.
1266 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001267 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1268 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001269 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001270 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001271 onSurfaceFrameCreated(surfaceFrame);
1272 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001273}
1274
Ady Abrahama850c182021-08-04 13:04:37 -07001275bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1276 if (mDrawingState.frameRateForLayerTree == frameRate) {
1277 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001278 }
1279
Ady Abrahama850c182021-08-04 13:04:37 -07001280 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001281
Ady Abrahama850c182021-08-04 13:04:37 -07001282 // TODO(b/195668952): we probably don't need to dirty visible regions here
1283 // or even store frameRateForLayerTree in mDrawingState
1284 mDrawingState.sequence++;
1285 mDrawingState.modified = true;
1286 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001287
Dominik Laskowski068173d2021-08-11 17:22:59 -07001288 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
1289 mFlinger->mScheduler->recordLayerHistory(this, systemTime(), LayerUpdateType::SetFrameRate);
Ady Abrahama850c182021-08-04 13:04:37 -07001290
1291 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001292}
1293
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001294Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1295 return getDrawingState().frameRateForLayerTree;
1296}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297
Robert Carr1f0a16a2016-10-24 16:27:39 -07001298bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001299 const State& s(mDrawingState);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001300 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001301 if (parent != nullptr && parent->isHiddenByPolicy()) {
1302 return true;
1303 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001304 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1305 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1306 if (zOrderRelativeOf != nullptr) {
1307 if (zOrderRelativeOf->isHiddenByPolicy()) {
1308 return true;
1309 }
1310 }
1311 }
Garfield Tan2c1782c2022-02-16 15:25:05 -08001312 if (CC_UNLIKELY(!isTransformValid())) {
1313 ALOGW("Hide layer %s because it has invalid transformation.", getDebugName());
1314 return true;
1315 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001316 return s.flags & layer_state_t::eLayerHidden;
1317}
1318
David Sodman41fdfc92017-11-06 16:09:56 -08001319uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001320 // TODO: should we do something special if mSecure is set?
1321 if (mProtectedByApp) {
1322 // need a hardware-protected path to external video sink
1323 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001324 }
Riley Andrews03414a12014-07-01 14:22:59 -07001325 if (mPotentialCursor) {
1326 usage |= GraphicBuffer::USAGE_CURSOR;
1327 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001328 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001329 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001330}
1331
Vishnu Nair71fcf912022-10-18 09:14:20 -07001332void Layer::skipReportingTransformHint() {
1333 mSkipReportingTransformHint = true;
1334}
1335
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001336void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1337 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1338 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001339 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001340
Vishnu Nair6213bd92020-05-08 17:42:25 -07001341 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001342}
1343
Mathias Agopian13127d82013-03-05 17:47:11 -08001344// ----------------------------------------------------------------------------
1345// debugging
1346// ----------------------------------------------------------------------------
1347
Marissa Wall61c58622018-07-18 10:12:20 -07001348// TODO(marissaw): add new layer state info to layer debugging
Huihong Luo05539a12022-02-23 10:29:40 -08001349gui::LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001350 using namespace std::string_literals;
1351
Huihong Luo05539a12022-02-23 10:29:40 -08001352 gui::LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001353 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001354 info.mName = getName();
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001355 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001356 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001357 info.mType = getType();
Lloyd Piquea2468662019-03-07 21:31:06 -08001358
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001359 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001360 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001361 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001362 info.mX = ds.transform.tx();
1363 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001364 info.mZ = ds.z;
chaviw25714502021-02-11 10:01:08 -08001365 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001366 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001367 info.mFlags = ds.flags;
1368 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001369 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001370 info.mMatrix[0][0] = ds.transform[0][0];
1371 info.mMatrix[0][1] = ds.transform[0][1];
1372 info.mMatrix[1][0] = ds.transform[1][0];
1373 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001374 {
chaviwd62d3062019-09-04 14:48:02 -07001375 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001376 if (buffer != 0) {
1377 info.mActiveBufferWidth = buffer->getWidth();
1378 info.mActiveBufferHeight = buffer->getHeight();
1379 info.mActiveBufferStride = buffer->getStride();
1380 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001381 } else {
1382 info.mActiveBufferWidth = 0;
1383 info.mActiveBufferHeight = 0;
1384 info.mActiveBufferStride = 0;
1385 info.mActiveBufferFormat = 0;
1386 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001387 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001388 info.mNumQueuedFrames = getQueuedFrameCount();
Kalle Raitaa099a242017-01-11 11:17:29 -08001389 info.mIsOpaque = isOpaque(ds);
1390 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001391 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001392 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001393}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001394
Yiwei Zhang5434a782018-12-05 18:06:32 -08001395void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001396 result.append(kDumpTableRowLength, '-');
1397 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001398 result.append(" Layer name\n");
1399 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001400 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001401 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001402 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001403 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001404 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001405 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1406 result.append(kDumpTableRowLength, '-');
1407 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001408}
1409
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001410void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1411 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001412 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001413 return;
1414 }
1415
Yiwei Zhang5434a782018-12-05 18:06:32 -08001416 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001417 if (mName.length() > 77) {
1418 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001419 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001420 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001421 shortened.append(mName, mName.length() - 36);
1422 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001423 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001424 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001425 }
1426
Yiwei Zhang5434a782018-12-05 18:06:32 -08001427 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001428
Alec Mourib416efd2018-09-06 21:01:59 +00001429 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001430 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001431
Chia-I Wu1e043612018-03-01 09:45:09 -08001432 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001433 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001434 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001435 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001436 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001437 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001438 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001439 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1440 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001441 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001442 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001443 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001444 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001445 const auto frameRate = getFrameRateForLayerTree();
1446 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1447 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001448 ftl::enum_string(frameRate.type).c_str(),
1449 ftl::enum_string(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001450 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001451 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001452 }
Dan Stozae22aec72016-08-01 13:20:59 -07001453
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001454 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1455 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1456
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001457 result.append(kDumpTableRowLength, '-');
1458 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001459}
Dan Stozae22aec72016-08-01 13:20:59 -07001460
Yiwei Zhang5434a782018-12-05 18:06:32 -08001461void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001462 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001463}
1464
Svetoslavd85084b2014-03-20 10:28:31 -07001465void Layer::clearFrameStats() {
1466 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001467}
1468
Jamie Gennis6547ff42013-07-16 20:12:42 -07001469void Layer::logFrameStats() {
1470 mFrameTracker.logAndResetStats(mName);
1471}
1472
Svetoslavd85084b2014-03-20 10:28:31 -07001473void Layer::getFrameStats(FrameStats* outStats) const {
1474 mFrameTracker.getStats(outStats);
1475}
1476
Vishnu Nair76554eb2022-12-09 03:38:36 +00001477void Layer::dumpOffscreenDebugInfo(std::string& result) const {
1478 std::string hasBuffer = hasBufferOrSidebandStream() ? " (contains buffer)" : "";
1479 StringAppendF(&result, "Layer %s%s pid:%d uid:%d\n", getName().c_str(), hasBuffer.c_str(),
Vishnu Naircb8be502022-10-12 19:03:23 +00001480 mOwnerPid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001481}
1482
Brian Anderson5ea5e592016-12-01 16:54:33 -08001483void Layer::onDisconnect() {
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001484 const int32_t layerId = getSequence();
1485 mFlinger->mTimeStats->onDestroy(layerId);
1486 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001487}
1488
Chia-I Wu98f1c102017-05-30 14:54:08 -07001489size_t Layer::getChildrenCount() const {
1490 size_t count = 0;
1491 for (const sp<Layer>& child : mCurrentChildren) {
1492 count += 1 + child->getChildrenCount();
1493 }
1494 return count;
1495}
1496
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001497void Layer::setGameModeForTree(GameMode gameMode) {
1498 const auto& currentState = getDrawingState();
Huihong Luod3d8f8e2022-03-08 14:48:46 -08001499 if (currentState.metadata.has(gui::METADATA_GAME_MODE)) {
1500 gameMode =
1501 static_cast<GameMode>(currentState.metadata.getInt32(gui::METADATA_GAME_MODE, 0));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001502 }
1503 setGameMode(gameMode);
1504 for (const sp<Layer>& child : mCurrentChildren) {
1505 child->setGameModeForTree(gameMode);
1506 }
1507}
1508
Robert Carr1f0a16a2016-10-24 16:27:39 -07001509void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001510 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001511 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001512
Robert Carr1f0a16a2016-10-24 16:27:39 -07001513 mCurrentChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07001514 layer->setParent(sp<Layer>::fromExisting(this));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001515 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001516 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001517}
1518
1519ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001520 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001521 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001522
Robert Carr1323c952019-01-28 18:13:27 -08001523 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001524 const auto removeResult = mCurrentChildren.remove(layer);
1525
1526 updateTreeHasFrameRateVote();
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001527 layer->setGameModeForTree(GameMode::Unsupported);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001528 layer->updateTreeHasFrameRateVote();
1529
1530 return removeResult;
1531}
1532
Robert Carr15eae092018-03-23 13:43:53 -07001533void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001534 for (const sp<Layer>& child : mDrawingChildren) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001535 child->mDrawingParent = newParent;
xinying1f4200e62022-04-26 14:41:30 +08001536 const float parentShadowRadius =
1537 newParent->canDrawShadows() ? 0.f : newParent->mEffectiveShadowRadius;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001538 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
xinying1f4200e62022-04-26 14:41:30 +08001539 parentShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001540 }
1541}
1542
chaviwf1961f72017-09-18 16:41:07 -07001543bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001544 sp<Layer> newParent;
1545 if (newParentHandle != nullptr) {
Vishnu Nair07e2a482022-10-18 19:18:16 +00001546 newParent = LayerHandle::getLayer(newParentHandle);
Robert Carr54cf5b12019-01-25 14:02:28 -08001547 if (newParent == nullptr) {
1548 ALOGE("Unable to promote Layer handle");
1549 return false;
1550 }
1551 if (newParent == this) {
1552 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1553 return false;
1554 }
1555 }
1556
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001557 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001558 if (parent != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001559 parent->removeChild(sp<Layer>::fromExisting(this));
chaviw06178942017-07-27 10:25:59 -07001560 }
1561
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001562 if (newParentHandle != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001563 newParent->addChild(sp<Layer>::fromExisting(this));
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001564 if (!newParent->isRemovedFromCurrentState()) {
1565 addToCurrentState();
1566 } else {
1567 onRemovedFromCurrentState();
1568 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001569 } else {
1570 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001571 }
1572
chaviw06178942017-07-27 10:25:59 -07001573 return true;
1574}
1575
Peiyong Lind3788632018-09-18 16:01:31 -07001576bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001577 static const mat4 identityMatrix = mat4();
1578
Robert Carr6a160312021-05-17 12:08:20 -07001579 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001580 return false;
1581 }
Robert Carr6a160312021-05-17 12:08:20 -07001582 ++mDrawingState.sequence;
1583 mDrawingState.colorTransform = matrix;
1584 mDrawingState.hasColorTransform = matrix != identityMatrix;
1585 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001586 setTransactionFlags(eTransactionNeeded);
1587 return true;
1588}
1589
chaviwf66724d2018-11-28 16:35:21 -08001590mat4 Layer::getColorTransform() const {
1591 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001592 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001593 colorTransform = parent->getColorTransform() * colorTransform;
1594 }
1595 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001596}
1597
1598bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001599 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001600 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001601 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1602 }
1603 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001604}
1605
Chia-I Wu11481472018-05-04 10:43:19 -07001606bool Layer::isLegacyDataSpace() const {
1607 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001608 return !(getDataSpace() &
1609 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1610 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001611}
1612
Robert Carr1f0a16a2016-10-24 16:27:39 -07001613void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001614 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001615}
1616
Robert Carr6a160312021-05-17 12:08:20 -07001617int32_t Layer::getZ(LayerVector::StateSet) const {
1618 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001619}
1620
Robert Carr1c5481e2019-07-01 14:42:27 -07001621bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001622 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001623 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001624 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001625}
1626
David Sodman41fdfc92017-11-06 16:09:56 -08001627__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001628 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001629 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1630 "makeTraversalList received invalid stateSet");
1631 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1632 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001633 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001634
Robert Carr29abff82017-12-04 13:51:20 -08001635 if (state.zOrderRelatives.size() == 0) {
1636 *outSkipRelativeZUsers = true;
1637 return children;
1638 }
1639
chaviwfd462612018-05-31 16:11:27 -07001640 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001641 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001642 sp<Layer> strongRelative = weakRelative.promote();
1643 if (strongRelative != nullptr) {
1644 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001645 }
1646 }
1647
Dan Stoza412903f2017-04-27 13:42:17 -07001648 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001649 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001650 continue;
1651 }
Robert Carrdb66e622017-04-10 16:55:57 -07001652 traverse.add(child);
1653 }
1654
1655 return traverse;
1656}
1657
Robert Carr1f0a16a2016-10-24 16:27:39 -07001658/**
Robert Carrdb66e622017-04-10 16:55:57 -07001659 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001660 */
Dan Stoza412903f2017-04-27 13:42:17 -07001661void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001662 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1663 // produce a new list for traversing, including our relatives, and not including our children
1664 // who are relatives of another surface. In the case that there are no relative Z,
1665 // makeTraversalList returns our children directly to avoid significant overhead.
1666 // However in this case we need to take the responsibility for filtering children which
1667 // are relatives of another surface here.
1668 bool skipRelativeZUsers = false;
1669 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001670
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001672 for (; i < list.size(); i++) {
1673 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001674 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1675 continue;
1676 }
1677
chaviw301b1d82019-11-06 13:15:09 -08001678 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001679 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001680 }
Dan Stoza412903f2017-04-27 13:42:17 -07001681 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001682 }
Robert Carr29abff82017-12-04 13:51:20 -08001683
Dan Stoza412903f2017-04-27 13:42:17 -07001684 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001685 for (; i < list.size(); i++) {
1686 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001687
Robert Carr29abff82017-12-04 13:51:20 -08001688 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1689 continue;
1690 }
Dan Stoza412903f2017-04-27 13:42:17 -07001691 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001692 }
1693}
1694
1695/**
Robert Carrdb66e622017-04-10 16:55:57 -07001696 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001697 */
Dan Stoza412903f2017-04-27 13:42:17 -07001698void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1699 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001700 // See traverseInZOrder for documentation.
1701 bool skipRelativeZUsers = false;
1702 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001703
Robert Carr1f0a16a2016-10-24 16:27:39 -07001704 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001705 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001706 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001707
1708 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1709 continue;
1710 }
1711
chaviw301b1d82019-11-06 13:15:09 -08001712 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001713 break;
1714 }
Dan Stoza412903f2017-04-27 13:42:17 -07001715 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001716 }
Dan Stoza412903f2017-04-27 13:42:17 -07001717 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001718 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001719 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001720
1721 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1722 continue;
1723 }
1724
Dan Stoza412903f2017-04-27 13:42:17 -07001725 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001726 }
1727}
1728
Edgar Arriaga844fa672020-01-16 14:21:42 -08001729void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1730 visitor(this);
1731 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001732 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001733 for (const sp<Layer>& child : children) {
1734 child->traverse(state, visitor);
1735 }
1736}
1737
chaviw4b129c22018-04-09 16:19:43 -07001738LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1739 const std::vector<Layer*>& layersInTree) {
1740 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1741 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001742 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1743 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001744 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001745
chaviwfd462612018-05-31 16:11:27 -07001746 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001747 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1748 sp<Layer> strongRelative = weakRelative.promote();
1749 // Only add relative layers that are also descendents of the top most parent of the tree.
1750 // If a relative layer is not a descendent, then it should be ignored.
1751 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1752 traverse.add(strongRelative);
1753 }
1754 }
1755
1756 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001757 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001758 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1759 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1760 // the child here since it won't be added later as a relative.
1761 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1762 childState.zOrderRelativeOf.promote().get())) {
1763 continue;
1764 }
1765 traverse.add(child);
1766 }
1767
1768 return traverse;
1769}
1770
1771void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1772 LayerVector::StateSet stateSet,
1773 const LayerVector::Visitor& visitor) {
1774 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001775
1776 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001777 for (; i < list.size(); i++) {
1778 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001779 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001780 break;
1781 }
chaviw4b129c22018-04-09 16:19:43 -07001782 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001783 }
chaviw4b129c22018-04-09 16:19:43 -07001784
chaviwa76b2712017-09-20 12:02:26 -07001785 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001786 for (; i < list.size(); i++) {
1787 const auto& relative = list[i];
1788 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001789 }
1790}
1791
chaviw4b129c22018-04-09 16:19:43 -07001792std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1793 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1794 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1795
1796 std::vector<Layer*> layersInTree = {this};
1797 for (size_t i = 0; i < children.size(); i++) {
1798 const auto& child = children[i];
1799 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1800 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1801 }
1802
1803 return layersInTree;
1804}
1805
1806void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1807 const LayerVector::Visitor& visitor) {
1808 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1809 std::sort(layersInTree.begin(), layersInTree.end());
1810 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1811}
1812
Peiyong Linefefaac2018-08-17 12:27:51 -07001813ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001814 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001815}
1816
Garfield Tan2c1782c2022-02-16 15:25:05 -08001817bool Layer::isTransformValid() const {
1818 float transformDet = getTransform().det();
1819 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
1820}
1821
chaviw13fdc492017-06-27 12:40:18 -07001822half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001823 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001824
chaviw13fdc492017-06-27 12:40:18 -07001825 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1826 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001827}
Robert Carr6452f122017-03-21 10:41:29 -07001828
Vishnu Nair6213bd92020-05-08 17:42:25 -07001829ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001830 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001831 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1832 return fixedTransformHint;
1833 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001834 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001835 if (!p) return fixedTransformHint;
1836 return p->getFixedTransformHint();
1837}
1838
chaviw13fdc492017-06-27 12:40:18 -07001839half4 Layer::getColor() const {
1840 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001841 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001842}
Robert Carr6452f122017-03-21 10:41:29 -07001843
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001844int32_t Layer::getBackgroundBlurRadius() const {
Vishnu Nair29810f72022-07-01 16:38:41 +00001845 if (getDrawingState().backgroundBlurRadius == 0) {
1846 return 0;
1847 }
Galia Peychevada7de0e2020-12-03 17:24:35 +01001848
Vishnu Nair29810f72022-07-01 16:38:41 +00001849 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001850 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1851 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001852}
1853
Galia Peychevae0acf382021-04-12 21:22:34 +02001854const std::vector<BlurRegion> Layer::getBlurRegions() const {
1855 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001856 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001857 for (auto& region : regionsCopy) {
1858 region.alpha = region.alpha * layerAlpha;
1859 }
1860 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001861}
1862
Vishnu Naire14c6b32022-08-06 04:20:15 +00001863RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001864 // Get parent settings
1865 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001866 const auto& parent = mDrawingParent.promote();
1867 if (parent != nullptr) {
1868 parentSettings = parent->getRoundedCornerState();
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001869 if (parentSettings.hasRoundedCorners()) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001870 ui::Transform t = getActiveTransform(getDrawingState());
1871 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001872 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001873 parentSettings.radius.x *= t.getScaleX();
1874 parentSettings.radius.y *= t.getScaleY();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001875 }
1876 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001877
1878 // Get layer settings
1879 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001880 const vec2 radius(getDrawingState().cornerRadius, getDrawingState().cornerRadius);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001881 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001882 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001883
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001884 if (layerSettingsValid && parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001885 // If the parent and the layer have rounded corner settings, use the parent settings if the
1886 // parent crop is entirely inside the layer crop.
1887 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1888 if (parentSettings.cropRect.left > layerCropRect.left &&
1889 parentSettings.cropRect.top > layerCropRect.top &&
1890 parentSettings.cropRect.right < layerCropRect.right &&
1891 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1892 return parentSettings;
1893 } else {
1894 return layerSettings;
1895 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001896 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001897 return layerSettings;
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001898 } else if (parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001899 return parentSettings;
1900 }
1901 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001902}
1903
Robert Carr88b85e12022-03-21 15:47:35 -07001904bool Layer::findInHierarchy(const sp<Layer>& l) {
1905 if (l == this) {
1906 return true;
1907 }
1908 for (auto& child : mDrawingChildren) {
1909 if (child->findInHierarchy(l)) {
1910 return true;
1911 }
1912 }
1913 return false;
1914}
1915
Robert Carr1f0a16a2016-10-24 16:27:39 -07001916void Layer::commitChildList() {
1917 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1918 const auto& child = mCurrentChildren[i];
1919 child->commitChildList();
1920 }
1921 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001922 mDrawingParent = mCurrentParent;
Robert Carr88b85e12022-03-21 15:47:35 -07001923 if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) {
1924 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1925 if (zOrderRelativeOf == nullptr) return;
1926 if (findInHierarchy(zOrderRelativeOf)) {
1927 ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(),
1928 zOrderRelativeOf->mName.c_str());
1929 ALOGE("Severing rel Z loop, potentially dangerous");
1930 mDrawingState.isRelativeOf = false;
Ady Abrahamd11bade2022-08-01 16:18:03 -07001931 zOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carr88b85e12022-03-21 15:47:35 -07001932 }
1933 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001934}
1935
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001936
chaviw3277faf2021-05-19 16:45:23 -05001937void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001938 mDrawingState.inputInfo = info;
Vishnu Nair07e2a482022-10-18 19:18:16 +00001939 mDrawingState.touchableRegionCrop =
1940 LayerHandle::getLayer(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07001941 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +00001942 mFlinger->mUpdateInputInfo = true;
Robert Carr720e5062018-07-30 17:45:14 -07001943 setTransactionFlags(eTransactionNeeded);
1944}
1945
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001946LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) {
chaviw08f3cb22020-01-13 13:17:21 -08001947 LayerProto* layerProto = layersProto.add_layers();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001948 writeToProtoDrawingState(layerProto);
chaviw08f3cb22020-01-13 13:17:21 -08001949 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1950
Vishnu Nair00b90132021-11-05 14:03:40 -07001951 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001952 ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread.
1953
Vishnu Nair60db8c02020-04-02 11:55:16 -07001954 // Only populate for the primary display.
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001955 if (const auto display = mFlinger->getDefaultDisplayDeviceLocked()) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05001956 const auto compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07001957 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001958 LayerProtoHelper::writeToProto(getVisibleRegion(display.get()),
1959 [&]() { return layerProto->mutable_visible_region(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07001960 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08001961 }
1962
chaviw08f3cb22020-01-13 13:17:21 -08001963 for (const sp<Layer>& layer : mDrawingChildren) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001964 layer->writeToProto(layersProto, traceFlags);
chaviw08f3cb22020-01-13 13:17:21 -08001965 }
chaviw6d89e2d2020-01-14 14:42:01 -08001966
1967 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08001968}
1969
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001970void Layer::writeToProtoDrawingState(LayerProto* layerInfo) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001971 const ui::Transform transform = getTransform();
Vishnu Naird37343b2022-01-12 16:18:56 -08001972 auto buffer = getExternalTexture();
Vishnu Nair00b90132021-11-05 14:03:40 -07001973 if (buffer != nullptr) {
Vishnu Naird37343b2022-01-12 16:18:56 -08001974 LayerProtoHelper::writeToProto(*buffer,
Vishnu Nair00b90132021-11-05 14:03:40 -07001975 [&]() { return layerInfo->mutable_active_buffer(); });
1976 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
1977 layerInfo->mutable_buffer_transform());
1978 }
1979 layerInfo->set_invalidate(contentDirty);
1980 layerInfo->set_is_protected(isProtected());
1981 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
1982 layerInfo->set_queued_frames(getQueuedFrameCount());
Vishnu Nair00b90132021-11-05 14:03:40 -07001983 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nair00b90132021-11-05 14:03:40 -07001984 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001985 layerInfo->set_corner_radius(
1986 (getRoundedCornerState().radius.x + getRoundedCornerState().radius.y) / 2.0);
Vishnu Nair00b90132021-11-05 14:03:40 -07001987 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
1988 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
1989 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
1990 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1991 [&]() { return layerInfo->mutable_position(); });
1992 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair00b90132021-11-05 14:03:40 -07001993 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1994 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07001995
Vishnu Nair00b90132021-11-05 14:03:40 -07001996 if (hasColorTransform()) {
1997 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07001998 }
1999
Vishnu Nair60db8c02020-04-02 11:55:16 -07002000 LayerProtoHelper::writeToProto(mSourceBounds,
2001 [&]() { return layerInfo->mutable_source_bounds(); });
2002 LayerProtoHelper::writeToProto(mScreenBounds,
2003 [&]() { return layerInfo->mutable_screen_bounds(); });
2004 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2005 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2006 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002007}
2008
2009void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002010 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002011 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2012 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002013 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002014
chaviw766c9c52021-02-10 17:36:47 -08002015 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002016
Vishnu Nair00b90132021-11-05 14:03:40 -07002017 layerInfo->set_id(sequence);
2018 layerInfo->set_name(getName().c_str());
2019 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002020
Vishnu Nair00b90132021-11-05 14:03:40 -07002021 for (const auto& child : children) {
2022 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002023 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002024
Vishnu Nair00b90132021-11-05 14:03:40 -07002025 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2026 sp<Layer> strongRelative = weakRelative.promote();
2027 if (strongRelative != nullptr) {
2028 layerInfo->add_relatives(strongRelative->sequence);
2029 }
2030 }
2031
Vishnu Nairea04b6f2022-08-19 21:28:17 +00002032 LayerProtoHelper::writeToProto(state.transparentRegionHint,
Vishnu Nair00b90132021-11-05 14:03:40 -07002033 [&]() { return layerInfo->mutable_transparent_region(); });
2034
2035 layerInfo->set_layer_stack(getLayerStack().id);
2036 layerInfo->set_z(state.z);
2037
2038 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2039 return layerInfo->mutable_requested_position();
2040 });
2041
Vishnu Nair00b90132021-11-05 14:03:40 -07002042 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2043
2044 layerInfo->set_is_opaque(isOpaque(state));
2045
2046 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2047 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2048 LayerProtoHelper::writeToProto(state.color,
2049 [&]() { return layerInfo->mutable_requested_color(); });
2050 layerInfo->set_flags(state.flags);
2051
2052 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2053 layerInfo->mutable_requested_transform());
2054
2055 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2056 if (parent != nullptr) {
2057 layerInfo->set_parent(parent->sequence);
2058 } else {
2059 layerInfo->set_parent(-1);
2060 }
2061
2062 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2063 if (zOrderRelativeOf != nullptr) {
2064 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2065 } else {
2066 layerInfo->set_z_order_relative_of(-1);
2067 }
2068
2069 layerInfo->set_is_relative_of(state.isRelativeOf);
2070
2071 layerInfo->set_owner_uid(mOwnerUid);
2072
Arthur Hung4cf2d8c2022-04-07 14:52:00 +00002073 if ((traceFlags & LayerTracing::TRACE_INPUT) && needsInputInfo()) {
chaviw3277faf2021-05-19 16:45:23 -05002074 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002075 if (useDrawing) {
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002076 info = fillInputInfo(
2077 InputDisplayArgs{.transform = &kIdentityTransform, .isSecure = true});
chaviw4c34a092020-07-08 11:30:06 -07002078 } else {
2079 info = state.inputInfo;
2080 }
2081
2082 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002083 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002084 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002085
Vishnu Nair00b90132021-11-05 14:03:40 -07002086 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002087 auto protoMap = layerInfo->mutable_metadata();
2088 for (const auto& entry : state.metadata.mMap) {
2089 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2090 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002091 }
Vishnu Naird2aaab12022-02-10 14:49:09 -08002092
2093 LayerProtoHelper::writeToProto(state.destinationFrame,
2094 [&]() { return layerInfo->mutable_destination_frame(); });
chaviw1d044282017-09-27 12:19:28 -07002095}
2096
Robert Carr2e102c92018-10-23 12:11:15 -07002097bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002098 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002099}
2100
Prabir Pradhan33da9462022-06-14 14:55:57 +00002101// Applies the given transform to the region, while protecting against overflows caused by any
2102// offsets. If applying the offset in the transform to any of the Rects in the region would result
2103// in an overflow, they are not added to the output Region.
2104static Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
2105 const std::string& debugWindowName) {
2106 // Round the translation using the same rounding strategy used by ui::Transform.
2107 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
2108 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
2109
2110 ui::Transform transformWithoutOffset = t;
2111 transformWithoutOffset.set(0.f, 0.f);
2112
2113 const Region transformed = transformWithoutOffset.transform(r);
2114
2115 // Apply the translation to each of the Rects in the region while discarding any that overflow.
2116 Region ret;
2117 for (const auto& rect : transformed) {
2118 Rect newRect;
2119 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
2120 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
2121 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
2122 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
2123 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
2124 debugWindowName.c_str());
2125 continue;
2126 }
2127 ret.orSelf(newRect);
2128 }
2129 return ret;
2130}
2131
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002132void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& screenToDisplay) {
2133 Rect tmpBounds = getInputBounds();
2134 if (!tmpBounds.isValid()) {
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08002135 info.touchableRegion.clear();
2136 // A layer could have invalid input bounds and still expect to receive touch input if it has
2137 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
2138 // correctly to determine the coordinate space for input events. Use an empty rect so that
2139 // the layer will receive input in its own layer space.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002140 tmpBounds = Rect::EMPTY_RECT;
chaviw7e72caf2020-12-02 16:50:43 -08002141 }
2142
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002143 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
2144 // frame and transform used for the layer, which determines the bounds and the coordinate space
2145 // within which the layer will receive input.
2146 //
2147 // The coordinate space within which each of the bounds are specified is explicitly documented
2148 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
2149 // Transform converts one coordinate space to another, which is apparent in its naming. For
2150 // example, "layerToDisplay" transforms layer space to display space.
2151 //
2152 // Coordinate space definitions:
2153 // - display: The display device's coordinate space. Correlates to pixels on the display.
2154 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
2155 // - layer: The coordinate space of this layer.
2156 // - input: The coordinate space in which this layer will receive input events. This could be
2157 // different than layer space if a surfaceInset is used, which changes the origin
2158 // of the input space.
2159 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
chaviw7e72caf2020-12-02 16:50:43 -08002160
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002161 // Clamp surface inset to the input bounds.
2162 const auto surfaceInset = static_cast<float>(info.surfaceInset);
2163 const float xSurfaceInset =
2164 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
2165 const float ySurfaceInset =
2166 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
chaviw1ff3d1e2020-07-01 15:53:47 -07002167
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002168 // Apply the insets to the input bounds.
2169 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
2170 inputBoundsInLayer.top + ySurfaceInset,
2171 inputBoundsInLayer.right - xSurfaceInset,
2172 inputBoundsInLayer.bottom - ySurfaceInset);
Ady Abraham282f1d72019-07-24 18:05:56 -07002173
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002174 // Crop the input bounds to ensure it is within the parent's bounds.
2175 const FloatRect croppedInsetBoundsInLayer = mBounds.intersect(insetBoundsInLayer);
Ady Abraham282f1d72019-07-24 18:05:56 -07002176
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002177 const ui::Transform layerToScreen = getInputTransform();
2178 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
Vishnu Nair8033a492018-12-05 07:27:23 -08002179
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002180 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
2181 info.frameLeft = roundedFrameInDisplay.left;
2182 info.frameTop = roundedFrameInDisplay.top;
2183 info.frameRight = roundedFrameInDisplay.right;
2184 info.frameBottom = roundedFrameInDisplay.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -07002185
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002186 ui::Transform inputToLayer;
2187 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
2188 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
chaviw39cfa2e2020-11-04 14:19:02 -08002189
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002190 // InputDispatcher expects a display-to-input transform.
2191 info.transform = inputToDisplay.inverse();
2192
2193 // The touchable region is specified in the input coordinate space. Change it to display space.
Prabir Pradhan33da9462022-06-14 14:55:57 +00002194 info.touchableRegion =
2195 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, mName);
chaviw7e72caf2020-12-02 16:50:43 -08002196}
2197
chaviw3277faf2021-05-19 16:45:23 -05002198void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002199 sp<Layer> p = sp<Layer>::fromExisting(this);
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002200 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002201 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002202 }
2203 if (p != nullptr) {
2204 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2205 }
2206}
2207
Vishnu Naira066d902021-09-13 18:40:17 -07002208gui::DropInputMode Layer::getDropInputMode() const {
2209 gui::DropInputMode mode = mDrawingState.dropInputMode;
2210 if (mode == gui::DropInputMode::ALL) {
2211 return mode;
2212 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002213 sp<Layer> parent = mDrawingParent.promote();
2214 if (parent) {
2215 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002216 if (parentMode != gui::DropInputMode::NONE) {
2217 return parentMode;
2218 }
2219 }
2220 return mode;
2221}
2222
2223void Layer::handleDropInputMode(gui::WindowInfo& info) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002224 if (mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Vishnu Naira066d902021-09-13 18:40:17 -07002225 return;
2226 }
2227
2228 // Check if we need to drop input unconditionally
2229 gui::DropInputMode dropInputMode = getDropInputMode();
2230 if (dropInputMode == gui::DropInputMode::ALL) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002231 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002232 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2233 return;
2234 }
2235
2236 // Check if we need to check if the window is obscured by parent
2237 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2238 return;
2239 }
2240
2241 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002242 sp<Layer> parent = mDrawingParent.promote();
2243 if (parent && parent->getAlpha() != 1.0_hf) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002244 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002245 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2246 static_cast<float>(getAlpha()));
2247 }
2248
2249 // Check if the parent has cropped the buffer
2250 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2251 if (!bufferSize.isValid()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002252 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002253 return;
2254 }
2255
2256 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2257 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2258 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2259 // match then the layer has not been cropped by its parents.
2260 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2261 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2262
2263 if (croppedByParent) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002264 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002265 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2266 getDebugName());
2267 } else {
2268 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2269 // input if the window is obscured. This check should be done in surfaceflinger but the
2270 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2271 // drop input events if the window is obscured.
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002272 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002273 }
2274}
2275
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002276WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) {
chaviw7e72caf2020-12-02 16:50:43 -08002277 if (!hasInputInfo()) {
2278 mDrawingState.inputInfo.name = getName();
2279 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2280 mDrawingState.inputInfo.ownerPid = mOwnerPid;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002281 mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002282 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002283 }
2284
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002285 const ui::Transform& displayTransform =
2286 displayArgs.transform != nullptr ? *displayArgs.transform : kIdentityTransform;
2287
chaviw3277faf2021-05-19 16:45:23 -05002288 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002289 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002290 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002291
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002292 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002293
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002294 if (displayArgs.transform == nullptr) {
2295 // Do not let the window receive touches if it is not associated with a valid display
2296 // transform. We still allow the window to receive keys and prevent ANRs.
2297 info.inputConfig |= WindowInfo::InputConfig::NOT_TOUCHABLE;
2298 }
2299
Robert Carr5dc426e2020-06-10 14:29:14 -07002300 // For compatibility reasons we let layers which can receive input
2301 // receive input before they have actually submitted a buffer. Because
2302 // of this we use canReceiveInput instead of isVisible to check the
2303 // policy-visibility, ignoring the buffer state. However for layers with
2304 // hasInputInfo()==false we can use the real visibility state.
2305 // We are just using these layers for occlusion detection in
2306 // InputDispatcher, and obviously if they aren't visible they can't occlude
2307 // anything.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002308 const bool visible = hasInputInfo() ? canReceiveInput() : isVisible();
2309 info.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
2310
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002311 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002312 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002313 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002314
Vishnu Nair16a938f2021-09-24 07:14:54 -07002315 // If the window will be blacked out on a display because the display does not have the secure
2316 // flag and the layer has the secure flag set, then drop input.
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002317 if (!displayArgs.isSecure && isSecure()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002318 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Nair16a938f2021-09-24 07:14:54 -07002319 }
2320
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002321 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2322 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002323 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2324 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002325 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002326 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002327 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002328 }
2329
Winson Chunga30f7c92021-06-29 15:42:56 -07002330 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2331 // if it was set by WM for a known system overlay
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002332 if (isTrustedOverlay()) {
2333 info.inputConfig |= WindowInfo::InputConfig::TRUSTED_OVERLAY;
2334 }
Winson Chunga30f7c92021-06-29 15:42:56 -07002335
chaviwaf87b3e2019-10-01 16:59:28 -07002336 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2337 // touches from going outside the cloned area.
2338 if (isClone()) {
chaviw8577ea82022-06-01 16:32:26 -05002339 info.inputConfig |= WindowInfo::InputConfig::CLONE;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002340 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2341 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002342 info.touchableRegion = info.touchableRegion.intersect(rect);
2343 }
2344 }
2345
Robert Carr720e5062018-07-30 17:45:14 -07002346 return info;
2347}
2348
chaviwaf87b3e2019-10-01 16:59:28 -07002349sp<Layer> Layer::getClonedRoot() {
2350 if (mClonedChild != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002351 return sp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002352 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002353 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002354 return nullptr;
2355 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002356 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002357}
2358
Robert Carredd13602020-04-13 17:24:34 -07002359bool Layer::hasInputInfo() const {
Linus Tufvessona1858822022-03-04 09:32:07 +00002360 return mDrawingState.inputInfo.token != nullptr ||
2361 mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Robert Carr720e5062018-07-30 17:45:14 -07002362}
2363
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002364compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002365 const DisplayDevice* display) const {
2366 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002367 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002368}
2369
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002370Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2371 const auto outputLayer = findOutputLayerForDisplay(display);
2372 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002373}
2374
chaviwb4c6e582019-08-16 14:35:07 -07002375void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002376 cloneDrawingState(clonedFrom.get());
chaviwb4c6e582019-08-16 14:35:07 -07002377 mClonedFrom = clonedFrom;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002378 mPremultipliedAlpha = clonedFrom->mPremultipliedAlpha;
2379 mPotentialCursor = clonedFrom->mPotentialCursor;
2380 mProtectedByApp = clonedFrom->mProtectedByApp;
2381 updateCloneBufferInfo();
2382}
2383
2384void Layer::updateCloneBufferInfo() {
2385 if (!isClone() || !isClonedFromAlive()) {
2386 return;
2387 }
2388
2389 sp<Layer> clonedFrom = getClonedFrom();
2390 mBufferInfo = clonedFrom->mBufferInfo;
2391 mSidebandStream = clonedFrom->mSidebandStream;
2392 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
2393 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
2394 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
2395
2396 // After buffer info is updated, the drawingState from the real layer needs to be copied into
2397 // the cloned. This is because some properties of drawingState can change when latchBuffer is
2398 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
2399 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
2400 // the cloned drawingState again.
2401 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
2402 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
2403 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
2404 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
2405
2406 cloneDrawingState(clonedFrom.get());
2407
2408 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
2409 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
2410 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
2411 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -07002412}
chaviw74b03172019-08-19 11:09:03 -07002413
2414void Layer::updateMirrorInfo() {
2415 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2416 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2417 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2418 // that case, we want to delete the reference to the clone since we want it to get
2419 // destroyed. The root, this layer, will still be around since the client can continue
2420 // to hold a reference, but no cloned layers will be displayed.
2421 mClonedChild = nullptr;
2422 return;
2423 }
2424
2425 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2426 // If the real layer exists and is in current state, add the clone as a child of the root.
2427 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2428 // copied to drawingState for the root layer. So the clonedChild is always removed from
2429 // drawingState and then needs to be added back each traversal.
2430 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2431 addChildToDrawing(mClonedChild);
2432 }
2433
2434 mClonedChild->updateClonedDrawingState(clonedLayersMap);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002435 mClonedChild->updateClonedChildren(sp<Layer>::fromExisting(this), clonedLayersMap);
chaviw74b03172019-08-19 11:09:03 -07002436 mClonedChild->updateClonedRelatives(clonedLayersMap);
2437}
2438
2439void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2440 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2441 // to the clone. If the real layer is no longer alive, continue traversing the children
2442 // since we may be able to pull out other children that are still alive.
2443 if (isClonedFromAlive()) {
2444 sp<Layer> clonedFrom = getClonedFrom();
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002445 cloneDrawingState(clonedFrom.get());
Ady Abrahamd11bade2022-08-01 16:18:03 -07002446 clonedLayersMap.emplace(clonedFrom, sp<Layer>::fromExisting(this));
chaviw74b03172019-08-19 11:09:03 -07002447 }
2448
2449 // The clone layer may have children in drawingState since they may have been created and
2450 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2451 // that already exist, since we can just re-use them.
2452 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2453 // not updated in the regular traversal. They are skipped since the root will lose the
2454 // reference to them when it copies its currentChildren to drawing.
2455 for (sp<Layer>& child : mDrawingChildren) {
2456 child->updateClonedDrawingState(clonedLayersMap);
2457 }
2458}
2459
2460void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2461 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2462 mDrawingChildren.clear();
2463
2464 if (!isClonedFromAlive()) {
2465 return;
2466 }
2467
2468 sp<Layer> clonedFrom = getClonedFrom();
2469 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2470 if (child == mirrorRoot) {
2471 // This is to avoid cyclical mirroring.
2472 continue;
2473 }
2474 sp<Layer> clonedChild = clonedLayersMap[child];
2475 if (clonedChild == nullptr) {
2476 clonedChild = child->createClone();
2477 clonedLayersMap[child] = clonedChild;
2478 }
2479 addChildToDrawing(clonedChild);
2480 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2481 }
2482}
2483
chaviwaf87b3e2019-10-01 16:59:28 -07002484void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2485 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2486 if (cropLayer != nullptr) {
2487 if (clonedLayersMap.count(cropLayer) == 0) {
2488 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2489 // self as crop layer to avoid going outside bounds.
Ady Abrahamd11bade2022-08-01 16:18:03 -07002490 mDrawingState.touchableRegionCrop = wp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002491 } else {
2492 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2493 mDrawingState.touchableRegionCrop = clonedCropLayer;
2494 }
2495 }
2496 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2497 // WM or the client.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002498 mDrawingState.inputInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, false);
chaviwaf87b3e2019-10-01 16:59:28 -07002499}
2500
2501void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002502 mDrawingState.zOrderRelativeOf = wp<Layer>();
chaviw74b03172019-08-19 11:09:03 -07002503 mDrawingState.zOrderRelatives.clear();
2504
2505 if (!isClonedFromAlive()) {
2506 return;
2507 }
2508
chaviwaf87b3e2019-10-01 16:59:28 -07002509 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002510 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002511 const sp<Layer>& relative = relativeWeak.promote();
2512 if (clonedLayersMap.count(relative) > 0) {
2513 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002514 mDrawingState.zOrderRelatives.add(clonedRelative);
2515 }
2516 }
2517
2518 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2519 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2520 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2521 // still traverse the children, but the layer with the missing relativeOf will not be shown
2522 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002523 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2524 if (clonedLayersMap.count(relativeOf) > 0) {
2525 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002526 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2527 }
2528
chaviwaf87b3e2019-10-01 16:59:28 -07002529 updateClonedInputInfo(clonedLayersMap);
2530
chaviw74b03172019-08-19 11:09:03 -07002531 for (sp<Layer>& child : mDrawingChildren) {
2532 child->updateClonedRelatives(clonedLayersMap);
2533 }
2534}
2535
2536void Layer::addChildToDrawing(const sp<Layer>& layer) {
2537 mDrawingChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002538 layer->mDrawingParent = sp<Layer>::fromExisting(this);
chaviw74b03172019-08-19 11:09:03 -07002539}
2540
Steven Thomas62a4cf82020-01-31 12:04:03 -08002541Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2542 switch (compatibility) {
2543 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2544 return FrameRateCompatibility::Default;
2545 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2546 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002547 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2548 return FrameRateCompatibility::Exact;
Andy Labrada096227e2022-06-15 16:58:11 +00002549 case ANATIVEWINDOW_FRAME_RATE_MIN:
2550 return FrameRateCompatibility::Min;
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002551 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
2552 return FrameRateCompatibility::NoVote;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002553 default:
2554 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2555 return FrameRateCompatibility::Default;
2556 }
2557}
2558
Marin Shalamanovc5986772021-03-16 16:09:49 +01002559scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2560 switch (strategy) {
2561 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2562 return Seamlessness::OnlySeamless;
2563 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2564 return Seamlessness::SeamedAndSeamless;
2565 default:
2566 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2567 return Seamlessness::Default;
2568 }
2569}
2570
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002571bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002572 const State& s(mDrawingState);
2573 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2574 return true;
2575 }
2576
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002577 sp<Layer> parent = mDrawingParent.promote();
2578 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002579}
2580
Robert Carr6a0382d2021-07-01 15:57:17 -07002581void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2582 mClonedChild = clonedChild;
2583 mHadClonedChild = true;
2584 mFlinger->mNumClones++;
2585}
2586
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002587bool Layer::setDropInputMode(gui::DropInputMode mode) {
2588 if (mDrawingState.dropInputMode == mode) {
2589 return false;
2590 }
2591 mDrawingState.dropInputMode = mode;
2592 return true;
2593}
2594
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002595void Layer::cloneDrawingState(const Layer* from) {
2596 mDrawingState = from->mDrawingState;
2597 // Skip callback info since they are not applicable for cloned layers.
2598 mDrawingState.releaseBufferListener = nullptr;
Vishnu Naircfb2d252023-01-19 04:44:02 +00002599 // TODO (b/238781169) currently broken for mirror layers because we do not
2600 // track release fences for mirror layers composed on other displays
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002601 mDrawingState.callbackHandles = {};
2602}
2603
Patrick Williamsbb25f802022-08-30 23:02:34 +00002604void Layer::callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
2605 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
2606 const sp<Fence>& releaseFence,
2607 uint32_t currentMaxAcquiredBufferCount) {
2608 if (!listener) {
2609 return;
2610 }
2611 ATRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, getDebugName(), framenumber);
Huihong Luoffee3bc2023-01-17 16:14:35 +00002612 listener->onReleaseBuffer({buffer->getId(), framenumber},
2613 releaseFence ? releaseFence : Fence::NO_FENCE,
2614 currentMaxAcquiredBufferCount);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002615}
2616
2617void Layer::onLayerDisplayed(ftl::SharedFuture<FenceResult> futureFenceResult) {
2618 // If we are displayed on multiple displays in a single composition cycle then we would
2619 // need to do careful tracking to enable the use of the mLastClientCompositionFence.
2620 // For example we can only use it if all the displays are client comp, and we need
2621 // to merge all the client comp fences. We could do this, but for now we just
2622 // disable the optimization when a layer is composed on multiple displays.
2623 if (mClearClientCompositionFenceOnLayerDisplayed) {
2624 mLastClientCompositionFence = nullptr;
2625 } else {
2626 mClearClientCompositionFenceOnLayerDisplayed = true;
2627 }
2628
2629 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
2630 // buffer that was presented on this layer. The first transaction that came in this frame that
2631 // replaced the previous buffer on this layer needs this release fence, because the fence will
2632 // let the client know when that previous buffer is removed from the screen.
2633 //
2634 // Every other transaction on this layer does not need a release fence because no other
2635 // Transactions that were set on this layer this frame are going to have their preceding buffer
2636 // removed from the display this frame.
2637 //
2638 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
2639 // buffer so it doesn't need a previous release fence because the layer still needs the previous
2640 // buffer. The second transaction contains a buffer so it needs a previous release fence because
2641 // the previous buffer will be released this frame. The third transaction also contains a
2642 // buffer. It replaces the buffer in the second transaction. The buffer in the second
2643 // transaction will now no longer be presented so it is released immediately and the third
2644 // transaction doesn't need a previous release fence.
2645 sp<CallbackHandle> ch;
2646 for (auto& handle : mDrawingState.callbackHandles) {
2647 if (handle->releasePreviousBuffer &&
2648 mDrawingState.releaseBufferEndpoint == handle->listener) {
2649 ch = handle;
2650 break;
2651 }
2652 }
2653
2654 // Prevent tracing the same release multiple times.
2655 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
2656 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
2657 }
2658
2659 if (ch != nullptr) {
2660 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2661 ch->previousReleaseFences.emplace_back(std::move(futureFenceResult));
2662 ch->name = mName;
2663 }
2664}
2665
2666void Layer::onSurfaceFrameCreated(
2667 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Vishnu Nair22491b82022-10-18 14:59:14 -07002668 if (!hasBufferOrSidebandStreamInDrawing()) {
2669 return;
2670 }
2671
Patrick Williamsbb25f802022-08-30 23:02:34 +00002672 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
2673 // Too many SurfaceFrames pending classification. The front of the deque is probably not
2674 // tracked by FrameTimeline and will never be presented. This will only result in a memory
2675 // leak.
2676 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
2677 mName.c_str());
2678 std::string miniDump = mPendingJankClassifications.front()->miniDump();
2679 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
2680 mPendingJankClassifications.pop_front();
2681 }
2682 mPendingJankClassifications.emplace_back(surfaceFrame);
2683}
2684
2685void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
2686 for (const auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair71fcf912022-10-18 09:14:20 -07002687 handle->transformHint = mSkipReportingTransformHint
2688 ? std::nullopt
2689 : std::make_optional<uint32_t>(mTransformHint);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002690 handle->dequeueReadyTime = dequeueReadyTime;
2691 handle->currentMaxAcquiredBufferCount =
2692 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
2693 ATRACE_FORMAT_INSTANT("releasePendingBuffer %s - %" PRIu64, getDebugName(),
2694 handle->previousReleaseCallbackId.framenumber);
2695 }
2696
2697 for (auto& handle : mDrawingState.callbackHandles) {
2698 if (handle->releasePreviousBuffer &&
2699 mDrawingState.releaseBufferEndpoint == handle->listener) {
2700 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2701 break;
2702 }
2703 }
2704
2705 std::vector<JankData> jankData;
2706 jankData.reserve(mPendingJankClassifications.size());
2707 while (!mPendingJankClassifications.empty() &&
2708 mPendingJankClassifications.front()->getJankType()) {
2709 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
2710 mPendingJankClassifications.front();
2711 mPendingJankClassifications.pop_front();
2712 jankData.emplace_back(
2713 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
2714 }
2715
2716 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(mDrawingState.callbackHandles,
2717 jankData);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002718 mDrawingState.callbackHandles = {};
2719}
2720
2721bool Layer::willPresentCurrentTransaction() const {
2722 // Returns true if the most recent Transaction applied to CurrentState will be presented.
2723 return (getSidebandStreamChanged() || getAutoRefresh() ||
2724 (mDrawingState.modified &&
2725 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
2726}
2727
2728bool Layer::setTransform(uint32_t transform) {
2729 if (mDrawingState.bufferTransform == transform) return false;
2730 mDrawingState.bufferTransform = transform;
2731 mDrawingState.modified = true;
2732 setTransactionFlags(eTransactionNeeded);
2733 return true;
2734}
2735
2736bool Layer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
2737 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
2738 mDrawingState.sequence++;
2739 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
2740 mDrawingState.modified = true;
2741 setTransactionFlags(eTransactionNeeded);
2742 return true;
2743}
2744
2745bool Layer::setBufferCrop(const Rect& bufferCrop) {
2746 if (mDrawingState.bufferCrop == bufferCrop) return false;
2747
2748 mDrawingState.sequence++;
2749 mDrawingState.bufferCrop = bufferCrop;
2750
2751 mDrawingState.modified = true;
2752 setTransactionFlags(eTransactionNeeded);
2753 return true;
2754}
2755
2756bool Layer::setDestinationFrame(const Rect& destinationFrame) {
2757 if (mDrawingState.destinationFrame == destinationFrame) return false;
2758
2759 mDrawingState.sequence++;
2760 mDrawingState.destinationFrame = destinationFrame;
2761
2762 mDrawingState.modified = true;
2763 setTransactionFlags(eTransactionNeeded);
2764 return true;
2765}
2766
2767// Translate destination frame into scale and position. If a destination frame is not set, use the
2768// provided scale and position
2769bool Layer::updateGeometry() {
2770 if ((mDrawingState.flags & layer_state_t::eIgnoreDestinationFrame) ||
2771 mDrawingState.destinationFrame.isEmpty()) {
2772 // If destination frame is not set, use the requested transform set via
2773 // Layer::setPosition and Layer::setMatrix.
2774 return assignTransform(&mDrawingState.transform, mRequestedTransform);
2775 }
2776
2777 Rect destRect = mDrawingState.destinationFrame;
2778 int32_t destW = destRect.width();
2779 int32_t destH = destRect.height();
2780 if (destRect.left < 0) {
2781 destRect.left = 0;
2782 destRect.right = destW;
2783 }
2784 if (destRect.top < 0) {
2785 destRect.top = 0;
2786 destRect.bottom = destH;
2787 }
2788
2789 if (!mDrawingState.buffer) {
2790 ui::Transform t;
2791 t.set(destRect.left, destRect.top);
2792 return assignTransform(&mDrawingState.transform, t);
2793 }
2794
2795 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
2796 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
2797 // Undo any transformations on the buffer.
2798 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
2799 std::swap(bufferWidth, bufferHeight);
2800 }
2801 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
2802 if (mDrawingState.transformToDisplayInverse) {
2803 if (invTransform & ui::Transform::ROT_90) {
2804 std::swap(bufferWidth, bufferHeight);
2805 }
2806 }
2807
2808 float sx = destW / static_cast<float>(bufferWidth);
2809 float sy = destH / static_cast<float>(bufferHeight);
2810 ui::Transform t;
2811 t.set(sx, 0, 0, sy);
2812 t.set(destRect.left, destRect.top);
2813 return assignTransform(&mDrawingState.transform, t);
2814}
2815
2816bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
2817 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
2818 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
2819 return false;
2820 }
2821
2822 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
2823
2824 mDrawingState.sequence++;
2825 mDrawingState.modified = true;
2826 setTransactionFlags(eTransactionNeeded);
2827
2828 return true;
2829}
2830
2831bool Layer::setPosition(float x, float y) {
2832 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
2833 return false;
2834 }
2835
2836 mRequestedTransform.set(x, y);
2837
2838 mDrawingState.sequence++;
2839 mDrawingState.modified = true;
2840 setTransactionFlags(eTransactionNeeded);
2841
2842 return true;
2843}
2844
2845bool Layer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
2846 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
2847 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
Brian Lindahl439afad2022-11-14 11:16:55 -07002848 const FrameTimelineInfo& info) {
Chavi Weingarten009619f2022-09-15 20:27:25 +00002849 ATRACE_FORMAT("setBuffer %s - hasBuffer=%s", getDebugName(), (buffer ? "true" : "false"));
Patrick Williamsbb25f802022-08-30 23:02:34 +00002850 if (!buffer) {
2851 return false;
2852 }
2853
2854 const bool frameNumberChanged =
2855 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
2856 const uint64_t frameNumber =
2857 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
Chavi Weingarten009619f2022-09-15 20:27:25 +00002858 ATRACE_FORMAT_INSTANT("setBuffer %s - %" PRIu64, getDebugName(), frameNumber);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002859
2860 if (mDrawingState.buffer) {
2861 mReleasePreviousBuffer = true;
2862 if (!mBufferInfo.mBuffer ||
2863 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
2864 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
2865 // If mDrawingState has a buffer, and we are about to update again
2866 // before swapping to drawing state, then the first buffer will be
2867 // dropped and we should decrement the pending buffer count and
2868 // call any release buffer callbacks if set.
2869 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2870 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2871 mDrawingState.acquireFence,
2872 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2873 mOwnerUid));
2874 decrementPendingBufferCount();
2875 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
2876 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
2877 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
2878 mDrawingState.bufferSurfaceFrameTX.reset();
2879 }
2880 } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) {
2881 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2882 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2883 mLastClientCompositionFence,
2884 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2885 mOwnerUid));
2886 mLastClientCompositionFence = nullptr;
2887 }
2888 }
2889
2890 mDrawingState.frameNumber = frameNumber;
2891 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
2892 mDrawingState.buffer = std::move(buffer);
2893 mDrawingState.clientCacheId = bufferData.cachedBuffer;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002894 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
2895 ? bufferData.acquireFence
2896 : Fence::NO_FENCE;
2897 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
2898 if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
2899 // We latched this buffer unsiganled, so we need to pass the acquire fence
2900 // on the callback instead of just the acquire time, since it's unknown at
2901 // this point.
2902 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence;
2903 } else {
2904 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime();
2905 }
2906
2907 mDrawingState.modified = true;
2908 setTransactionFlags(eTransactionNeeded);
2909
2910 const int32_t layerId = getSequence();
2911 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
2912 mOwnerUid, postTime, getGameMode());
2913 mDrawingState.desiredPresentTime = desiredPresentTime;
2914 mDrawingState.isAutoTimestamp = isAutoTimestamp;
2915
2916 const nsecs_t presentTime = [&] {
2917 if (!isAutoTimestamp) return desiredPresentTime;
2918
2919 const auto prediction =
2920 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
2921 if (prediction.has_value()) return prediction->presentTime;
2922
2923 return static_cast<nsecs_t>(0);
2924 }();
2925
2926 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
2927 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
2928
2929 setFrameTimelineVsyncForBufferTransaction(info, postTime);
2930
2931 if (dequeueTime && *dequeueTime != 0) {
2932 const uint64_t bufferId = mDrawingState.buffer->getId();
2933 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
2934 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
2935 FrameTracer::FrameEvent::DEQUEUE);
2936 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
2937 FrameTracer::FrameEvent::QUEUE);
2938 }
2939
2940 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
2941 return true;
2942}
2943
2944bool Layer::setDataspace(ui::Dataspace dataspace) {
2945 mDrawingState.dataspaceRequested = true;
2946 if (mDrawingState.dataspace == dataspace) return false;
2947 mDrawingState.dataspace = dataspace;
2948 mDrawingState.modified = true;
2949 setTransactionFlags(eTransactionNeeded);
2950 return true;
2951}
2952
2953bool Layer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
2954 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
2955 mDrawingState.hdrMetadata = hdrMetadata;
2956 mDrawingState.modified = true;
2957 setTransactionFlags(eTransactionNeeded);
2958 return true;
2959}
2960
2961bool Layer::setSurfaceDamageRegion(const Region& surfaceDamage) {
2962 mDrawingState.surfaceDamageRegion = surfaceDamage;
2963 mDrawingState.modified = true;
2964 setTransactionFlags(eTransactionNeeded);
2965 return true;
2966}
2967
2968bool Layer::setApi(int32_t api) {
2969 if (mDrawingState.api == api) return false;
2970 mDrawingState.api = api;
2971 mDrawingState.modified = true;
2972 setTransactionFlags(eTransactionNeeded);
2973 return true;
2974}
2975
2976bool Layer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
2977 if (mDrawingState.sidebandStream == sidebandStream) return false;
2978
2979 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
2980 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
2981 } else if (sidebandStream != nullptr) {
2982 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
2983 }
2984
2985 mDrawingState.sidebandStream = sidebandStream;
2986 mDrawingState.modified = true;
2987 setTransactionFlags(eTransactionNeeded);
2988 if (!mSidebandStreamChanged.exchange(true)) {
2989 // mSidebandStreamChanged was false
2990 mFlinger->onLayerUpdate();
2991 }
2992 return true;
2993}
2994
2995bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
2996 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
2997 if (handles.empty()) {
2998 mReleasePreviousBuffer = false;
2999 return false;
3000 }
3001
3002 const bool willPresent = willPresentCurrentTransaction();
3003
3004 for (const auto& handle : handles) {
3005 // If this transaction set a buffer on this layer, release its previous buffer
3006 handle->releasePreviousBuffer = mReleasePreviousBuffer;
3007
3008 // If this layer will be presented in this frame
3009 if (willPresent) {
3010 // If this transaction set an acquire fence on this layer, set its acquire time
3011 handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence;
3012 handle->frameNumber = mDrawingState.frameNumber;
3013
3014 // Store so latched time and release fence can be set
3015 mDrawingState.callbackHandles.push_back(handle);
3016
3017 } else { // If this layer will NOT need to be relatched and presented this frame
3018 // Notify the transaction completed thread this handle is done
3019 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
3020 }
3021 }
3022
3023 mReleasePreviousBuffer = false;
3024 mCallbackHandleAcquireTimeOrFence = -1;
3025
3026 return willPresent;
3027}
3028
3029Rect Layer::getBufferSize(const State& /*s*/) const {
3030 // for buffer state layers we use the display frame size as the buffer size.
3031
3032 if (mBufferInfo.mBuffer == nullptr) {
3033 return Rect::INVALID_RECT;
3034 }
3035
3036 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3037 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3038
3039 // Undo any transformations on the buffer and return the result.
3040 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
3041 std::swap(bufWidth, bufHeight);
3042 }
3043
3044 if (getTransformToDisplayInverse()) {
3045 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3046 if (invTransform & ui::Transform::ROT_90) {
3047 std::swap(bufWidth, bufHeight);
3048 }
3049 }
3050
3051 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
3052}
3053
3054FloatRect Layer::computeSourceBounds(const FloatRect& parentBounds) const {
3055 if (mBufferInfo.mBuffer == nullptr) {
3056 return parentBounds;
3057 }
3058
3059 return getBufferSize(getDrawingState()).toFloatRect();
3060}
3061
3062bool Layer::fenceHasSignaled() const {
3063 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
3064 return true;
3065 }
3066
3067 const bool fenceSignaled =
3068 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
3069 if (!fenceSignaled) {
3070 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
3071 TimeStats::LatchSkipReason::LateAcquire);
3072 }
3073
3074 return fenceSignaled;
3075}
3076
Vishnu Naire14c6b32022-08-06 04:20:15 +00003077bool Layer::onPreComposition(nsecs_t refreshStartTime) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003078 for (const auto& handle : mDrawingState.callbackHandles) {
3079 handle->refreshStartTime = refreshStartTime;
3080 }
3081 return hasReadyFrame();
3082}
3083
3084void Layer::setAutoRefresh(bool autoRefresh) {
3085 mDrawingState.autoRefresh = autoRefresh;
3086}
3087
3088bool Layer::latchSidebandStream(bool& recomputeVisibleRegions) {
3089 // 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 +00003090 auto* snapshot = editLayerSnapshot();
3091 snapshot->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003092
3093 if (mSidebandStreamChanged.exchange(false)) {
3094 const State& s(getDrawingState());
3095 // mSidebandStreamChanged was true
3096 mSidebandStream = s.sidebandStream;
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003097 snapshot->sidebandStream = mSidebandStream;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003098 if (mSidebandStream != nullptr) {
3099 setTransactionFlags(eTransactionNeeded);
3100 mFlinger->setTransactionFlags(eTraversalNeeded);
3101 }
3102 recomputeVisibleRegions = true;
3103
3104 return true;
3105 }
3106 return false;
3107}
3108
3109bool Layer::hasFrameUpdate() const {
3110 const State& c(getDrawingState());
3111 return (mDrawingStateModified || mDrawingState.modified) &&
3112 (c.buffer != nullptr || c.bgColorLayer != nullptr);
3113}
3114
3115void Layer::updateTexImage(nsecs_t latchTime) {
3116 const State& s(getDrawingState());
3117
3118 if (!s.buffer) {
3119 if (s.bgColorLayer) {
3120 for (auto& handle : mDrawingState.callbackHandles) {
3121 handle->latchTime = latchTime;
3122 }
3123 }
3124 return;
3125 }
3126
3127 for (auto& handle : mDrawingState.callbackHandles) {
3128 if (handle->frameNumber == mDrawingState.frameNumber) {
3129 handle->latchTime = latchTime;
3130 }
3131 }
3132
3133 const int32_t layerId = getSequence();
3134 const uint64_t bufferId = mDrawingState.buffer->getId();
3135 const uint64_t frameNumber = mDrawingState.frameNumber;
3136 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3137 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
3138 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
3139
3140 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
3141 FrameTracer::FrameEvent::ACQUIRE_FENCE);
3142 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
3143 FrameTracer::FrameEvent::LATCH);
3144
3145 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
3146 if (bufferSurfaceFrame != nullptr &&
3147 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
3148 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
3149 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
3150 // are processing the next state.
3151 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
3152 mDrawingState.acquireFenceTime->getSignalTime(),
3153 latchTime);
3154 mDrawingState.bufferSurfaceFrameTX.reset();
3155 }
3156
3157 std::deque<sp<CallbackHandle>> remainingHandles;
3158 mFlinger->getTransactionCallbackInvoker()
3159 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
3160 mDrawingState.callbackHandles = remainingHandles;
3161
3162 mDrawingStateModified = false;
3163}
3164
3165void Layer::gatherBufferInfo() {
3166 if (!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
3167 decrementPendingBufferCount();
3168 }
3169
3170 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
3171 mBufferInfo.mBuffer = mDrawingState.buffer;
3172 mBufferInfo.mFence = mDrawingState.acquireFence;
3173 mBufferInfo.mFrameNumber = mDrawingState.frameNumber;
3174 mBufferInfo.mPixelFormat =
3175 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat();
3176 mBufferInfo.mFrameLatencyNeeded = true;
3177 mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime;
3178 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3179 mBufferInfo.mFence = mDrawingState.acquireFence;
3180 mBufferInfo.mTransform = mDrawingState.bufferTransform;
3181 auto lastDataspace = mBufferInfo.mDataspace;
3182 mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace);
3183 if (lastDataspace != mBufferInfo.mDataspace) {
3184 mFlinger->mSomeDataspaceChanged = true;
3185 }
3186 mBufferInfo.mCrop = computeBufferCrop(mDrawingState);
3187 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
3188 mBufferInfo.mSurfaceDamage = mDrawingState.surfaceDamageRegion;
3189 mBufferInfo.mHdrMetadata = mDrawingState.hdrMetadata;
3190 mBufferInfo.mApi = mDrawingState.api;
3191 mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003192}
3193
3194Rect Layer::computeBufferCrop(const State& s) {
3195 if (s.buffer && !s.bufferCrop.isEmpty()) {
3196 Rect bufferCrop;
3197 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
3198 return bufferCrop;
3199 } else if (s.buffer) {
3200 return s.buffer->getBounds();
3201 } else {
3202 return s.bufferCrop;
3203 }
3204}
3205
3206sp<Layer> Layer::createClone() {
3207 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
3208 args.textureName = mTextureName;
Patrick Williams83f36b22022-09-14 17:57:35 +00003209 sp<Layer> layer = mFlinger->getFactory().createBufferStateLayer(args);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003210 layer->setInitialValuesForClone(sp<Layer>::fromExisting(this));
3211 return layer;
3212}
3213
3214bool Layer::bufferNeedsFiltering() const {
3215 const State& s(getDrawingState());
3216 if (!s.buffer) {
3217 return false;
3218 }
3219
3220 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
3221 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
3222
3223 // Undo any transformations on the buffer and return the result.
3224 if (s.bufferTransform & ui::Transform::ROT_90) {
3225 std::swap(bufferWidth, bufferHeight);
3226 }
3227
3228 if (s.transformToDisplayInverse) {
3229 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3230 if (invTransform & ui::Transform::ROT_90) {
3231 std::swap(bufferWidth, bufferHeight);
3232 }
3233 }
3234
3235 const Rect layerSize{getBounds()};
3236 int32_t layerWidth = layerSize.getWidth();
3237 int32_t layerHeight = layerSize.getHeight();
3238
3239 // Align the layer orientation with the buffer before comparism
3240 if (mTransformHint & ui::Transform::ROT_90) {
3241 std::swap(layerWidth, layerHeight);
3242 }
3243
3244 return layerWidth != bufferWidth || layerHeight != bufferHeight;
3245}
3246
3247void Layer::decrementPendingBufferCount() {
3248 int32_t pendingBuffers = --mPendingBufferTransactions;
3249 tracePendingBufferCount(pendingBuffers);
3250}
3251
3252void Layer::tracePendingBufferCount(int32_t pendingBuffers) {
3253 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
3254}
3255
3256/*
3257 * We don't want to send the layer's transform to input, but rather the
3258 * parent's transform. This is because Layer's transform is
3259 * information about how the buffer is placed on screen. The parent's
3260 * transform makes more sense to send since it's information about how the
3261 * layer is placed on screen. This transform is used by input to determine
3262 * how to go from screen space back to window space.
3263 */
3264ui::Transform Layer::getInputTransform() const {
3265 if (!hasBufferOrSidebandStream()) {
3266 return getTransform();
3267 }
3268 sp<Layer> parent = mDrawingParent.promote();
3269 if (parent == nullptr) {
3270 return ui::Transform();
3271 }
3272
3273 return parent->getTransform();
3274}
3275
3276/**
3277 * Similar to getInputTransform, we need to update the bounds to include the transform.
3278 * This is because bounds don't include the buffer transform, where the input assumes
3279 * that's already included.
3280 */
3281Rect Layer::getInputBounds() const {
3282 if (!hasBufferOrSidebandStream()) {
3283 return getCroppedBufferSize(getDrawingState());
3284 }
3285
3286 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
3287 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
3288 return bufferBounds;
3289 }
3290 return mDrawingState.transform.transform(bufferBounds);
3291}
3292
3293bool Layer::simpleBufferUpdate(const layer_state_t& s) const {
3294 const uint64_t requiredFlags = layer_state_t::eBufferChanged;
3295
3296 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
3297 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
3298 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
3299 layer_state_t::eLayerStackChanged | layer_state_t::eAutoRefreshChanged |
3300 layer_state_t::eReparent;
3301
3302 const uint64_t allowedFlags = layer_state_t::eHasListenerCallbacksChanged |
3303 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFrameRateChanged |
3304 layer_state_t::eSurfaceDamageRegionChanged | layer_state_t::eApiChanged |
3305 layer_state_t::eMetadataChanged | layer_state_t::eDropInputModeChanged |
3306 layer_state_t::eInputInfoChanged;
3307
3308 if ((s.what & requiredFlags) != requiredFlags) {
3309 ALOGV("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
3310 (s.what | requiredFlags) & ~s.what);
3311 return false;
3312 }
3313
3314 if (s.what & deniedFlags) {
3315 ALOGV("%s: false [has denied flags 0x%" PRIx64 "]", __func__, s.what & deniedFlags);
3316 return false;
3317 }
3318
3319 if (s.what & allowedFlags) {
3320 ALOGV("%s: [has allowed flags 0x%" PRIx64 "]", __func__, s.what & allowedFlags);
3321 }
3322
3323 if (s.what & layer_state_t::ePositionChanged) {
3324 if (mRequestedTransform.tx() != s.x || mRequestedTransform.ty() != s.y) {
3325 ALOGV("%s: false [ePositionChanged changed]", __func__);
3326 return false;
3327 }
3328 }
3329
3330 if (s.what & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003331 if (mDrawingState.color.a != s.color.a) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003332 ALOGV("%s: false [eAlphaChanged changed]", __func__);
3333 return false;
3334 }
3335 }
3336
3337 if (s.what & layer_state_t::eColorTransformChanged) {
3338 if (mDrawingState.colorTransform != s.colorTransform) {
3339 ALOGV("%s: false [eColorTransformChanged changed]", __func__);
3340 return false;
3341 }
3342 }
3343
3344 if (s.what & layer_state_t::eBackgroundColorChanged) {
3345 if (mDrawingState.bgColorLayer || s.bgColorAlpha != 0) {
3346 ALOGV("%s: false [eBackgroundColorChanged changed]", __func__);
3347 return false;
3348 }
3349 }
3350
3351 if (s.what & layer_state_t::eMatrixChanged) {
3352 if (mRequestedTransform.dsdx() != s.matrix.dsdx ||
3353 mRequestedTransform.dtdy() != s.matrix.dtdy ||
3354 mRequestedTransform.dtdx() != s.matrix.dtdx ||
3355 mRequestedTransform.dsdy() != s.matrix.dsdy) {
3356 ALOGV("%s: false [eMatrixChanged changed]", __func__);
3357 return false;
3358 }
3359 }
3360
3361 if (s.what & layer_state_t::eCornerRadiusChanged) {
3362 if (mDrawingState.cornerRadius != s.cornerRadius) {
3363 ALOGV("%s: false [eCornerRadiusChanged changed]", __func__);
3364 return false;
3365 }
3366 }
3367
3368 if (s.what & layer_state_t::eBackgroundBlurRadiusChanged) {
3369 if (mDrawingState.backgroundBlurRadius != static_cast<int>(s.backgroundBlurRadius)) {
3370 ALOGV("%s: false [eBackgroundBlurRadiusChanged changed]", __func__);
3371 return false;
3372 }
3373 }
3374
Vishnu Nairbbceb462022-10-10 04:52:13 +00003375 if (s.what & layer_state_t::eBufferTransformChanged) {
3376 if (mDrawingState.bufferTransform != s.bufferTransform) {
3377 ALOGV("%s: false [eBufferTransformChanged changed]", __func__);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003378 return false;
3379 }
3380 }
3381
3382 if (s.what & layer_state_t::eTransformToDisplayInverseChanged) {
3383 if (mDrawingState.transformToDisplayInverse != s.transformToDisplayInverse) {
3384 ALOGV("%s: false [eTransformToDisplayInverseChanged changed]", __func__);
3385 return false;
3386 }
3387 }
3388
3389 if (s.what & layer_state_t::eCropChanged) {
3390 if (mDrawingState.crop != s.crop) {
3391 ALOGV("%s: false [eCropChanged changed]", __func__);
3392 return false;
3393 }
3394 }
3395
3396 if (s.what & layer_state_t::eDataspaceChanged) {
3397 if (mDrawingState.dataspace != s.dataspace) {
3398 ALOGV("%s: false [eDataspaceChanged changed]", __func__);
3399 return false;
3400 }
3401 }
3402
3403 if (s.what & layer_state_t::eHdrMetadataChanged) {
3404 if (mDrawingState.hdrMetadata != s.hdrMetadata) {
3405 ALOGV("%s: false [eHdrMetadataChanged changed]", __func__);
3406 return false;
3407 }
3408 }
3409
3410 if (s.what & layer_state_t::eSidebandStreamChanged) {
3411 if (mDrawingState.sidebandStream != s.sidebandStream) {
3412 ALOGV("%s: false [eSidebandStreamChanged changed]", __func__);
3413 return false;
3414 }
3415 }
3416
3417 if (s.what & layer_state_t::eColorSpaceAgnosticChanged) {
3418 if (mDrawingState.colorSpaceAgnostic != s.colorSpaceAgnostic) {
3419 ALOGV("%s: false [eColorSpaceAgnosticChanged changed]", __func__);
3420 return false;
3421 }
3422 }
3423
3424 if (s.what & layer_state_t::eShadowRadiusChanged) {
3425 if (mDrawingState.shadowRadius != s.shadowRadius) {
3426 ALOGV("%s: false [eShadowRadiusChanged changed]", __func__);
3427 return false;
3428 }
3429 }
3430
3431 if (s.what & layer_state_t::eFixedTransformHintChanged) {
3432 if (mDrawingState.fixedTransformHint != s.fixedTransformHint) {
3433 ALOGV("%s: false [eFixedTransformHintChanged changed]", __func__);
3434 return false;
3435 }
3436 }
3437
3438 if (s.what & layer_state_t::eTrustedOverlayChanged) {
3439 if (mDrawingState.isTrustedOverlay != s.isTrustedOverlay) {
3440 ALOGV("%s: false [eTrustedOverlayChanged changed]", __func__);
3441 return false;
3442 }
3443 }
3444
3445 if (s.what & layer_state_t::eStretchChanged) {
3446 StretchEffect temp = s.stretchEffect;
3447 temp.sanitize();
3448 if (mDrawingState.stretchEffect != temp) {
3449 ALOGV("%s: false [eStretchChanged changed]", __func__);
3450 return false;
3451 }
3452 }
3453
3454 if (s.what & layer_state_t::eBufferCropChanged) {
3455 if (mDrawingState.bufferCrop != s.bufferCrop) {
3456 ALOGV("%s: false [eBufferCropChanged changed]", __func__);
3457 return false;
3458 }
3459 }
3460
3461 if (s.what & layer_state_t::eDestinationFrameChanged) {
3462 if (mDrawingState.destinationFrame != s.destinationFrame) {
3463 ALOGV("%s: false [eDestinationFrameChanged changed]", __func__);
3464 return false;
3465 }
3466 }
3467
3468 if (s.what & layer_state_t::eDimmingEnabledChanged) {
3469 if (mDrawingState.dimmingEnabled != s.dimmingEnabled) {
3470 ALOGV("%s: false [eDimmingEnabledChanged changed]", __func__);
3471 return false;
3472 }
3473 }
3474
3475 ALOGV("%s: true", __func__);
3476 return true;
3477}
3478
3479bool Layer::isHdrY410() const {
3480 // pixel format is HDR Y410 masquerading as RGBA_1010102
3481 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
3482 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
3483 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
3484}
3485
Vishnu Naire14c6b32022-08-06 04:20:15 +00003486sp<LayerFE> Layer::getCompositionEngineLayerFE() const {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003487 // There's no need to get a CE Layer if the layer isn't going to draw anything.
Vishnu Naire14c6b32022-08-06 04:20:15 +00003488 return hasSomethingToDraw() ? mLayerFE : nullptr;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003489}
3490
Vishnu Naire14c6b32022-08-06 04:20:15 +00003491const LayerSnapshot* Layer::getLayerSnapshot() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003492 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003493}
3494
Vishnu Naire14c6b32022-08-06 04:20:15 +00003495LayerSnapshot* Layer::editLayerSnapshot() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003496 return mSnapshot.get();
3497}
Vishnu Naire14c6b32022-08-06 04:20:15 +00003498
Patrick Williamsbb25f802022-08-30 23:02:34 +00003499const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003500 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003501}
3502
Patrick Williams7584c6a2022-10-29 02:10:58 +00003503sp<LayerFE> Layer::copyCompositionEngineLayerFE() const {
3504 auto result = mFlinger->getFactory().createLayerFE(mLayerFE->getDebugName());
3505 result->mSnapshot = std::make_unique<LayerSnapshot>(*mSnapshot);
3506 return result;
3507}
3508
Patrick Williamsbb25f802022-08-30 23:02:34 +00003509void Layer::useSurfaceDamage() {
3510 if (mFlinger->mForceFullDamage) {
3511 surfaceDamageRegion = Region::INVALID_REGION;
3512 } else {
3513 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
3514 }
3515}
3516
3517void Layer::useEmptyDamage() {
3518 surfaceDamageRegion.clear();
3519}
3520
3521bool Layer::isOpaque(const Layer::State& s) const {
3522 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
3523 // layer's opaque flag.
3524 if (!hasSomethingToDraw()) {
3525 return false;
3526 }
3527
3528 // if the layer has the opaque flag, then we're always opaque
3529 if ((s.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque) {
3530 return true;
3531 }
3532
3533 // If the buffer has no alpha channel, then we are opaque
Vishnu Nair8fc721b2022-12-22 20:06:32 +00003534 if (hasBufferOrSidebandStream() && LayerSnapshot::isOpaqueFormat(getPixelFormat())) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003535 return true;
3536 }
3537
3538 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
3539 return fillsColor() && getAlpha() == 1.0_hf;
3540}
3541
3542bool Layer::canReceiveInput() const {
3543 return !isHiddenByPolicy() && (mBufferInfo.mBuffer == nullptr || getAlpha() > 0.0f);
3544}
3545
3546bool Layer::isVisible() const {
3547 if (!hasSomethingToDraw()) {
3548 return false;
3549 }
3550
3551 if (isHiddenByPolicy()) {
3552 return false;
3553 }
3554
3555 return getAlpha() > 0.0f || hasBlur();
3556}
3557
3558void Layer::onPostComposition(const DisplayDevice* display,
3559 const std::shared_ptr<FenceTime>& glDoneFence,
3560 const std::shared_ptr<FenceTime>& presentFence,
3561 const CompositorTiming& compositorTiming) {
3562 // mFrameLatencyNeeded is true when a new frame was latched for the
3563 // composition.
3564 if (!mBufferInfo.mFrameLatencyNeeded) return;
3565
3566 for (const auto& handle : mDrawingState.callbackHandles) {
3567 handle->gpuCompositionDoneFence = glDoneFence;
3568 handle->compositorTiming = compositorTiming;
3569 }
3570
3571 // Update mFrameTracker.
3572 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
3573 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
3574
3575 const int32_t layerId = getSequence();
3576 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
3577
3578 const auto outputLayer = findOutputLayerForDisplay(display);
3579 if (outputLayer && outputLayer->requiresClientComposition()) {
3580 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
3581 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3582 clientCompositionTimestamp,
3583 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
3584 // Update the SurfaceFrames in the drawing state
3585 if (mDrawingState.bufferSurfaceFrameTX) {
3586 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
3587 }
3588 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
3589 surfaceFrame->setGpuComposition();
3590 }
3591 }
3592
3593 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
3594 if (frameReadyFence->isValid()) {
3595 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
3596 } else {
3597 // There was no fence for this frame, so assume that it was ready
3598 // to be presented at the desired present time.
3599 mFrameTracker.setFrameReadyTime(desiredPresentTime);
3600 }
3601
3602 if (display) {
Ady Abrahamace3d052022-11-17 16:25:05 -08003603 const Fps refreshRate = display->refreshRateSelector().getActiveMode().fps;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003604 const std::optional<Fps> renderRate =
3605 mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
3606
3607 const auto vote = frameRateToSetFrameRateVotePayload(mDrawingState.frameRate);
3608 const auto gameMode = getGameMode();
3609
3610 if (presentFence->isValid()) {
3611 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
3612 refreshRate, renderRate, vote, gameMode);
3613 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3614 presentFence,
3615 FrameTracer::FrameEvent::PRESENT_FENCE);
3616 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
3617 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
3618 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Dominik Laskowskidc2bb802022-09-28 16:02:59 -04003619 // The HWC doesn't support present fences, so use the present timestamp instead.
3620 const nsecs_t presentTimestamp =
3621 mFlinger->getHwComposer().getPresentTimestamp(*displayId);
3622
3623 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
3624 const nsecs_t vsyncPeriod = display->getVsyncPeriodFromHWC();
3625 const nsecs_t actualPresentTime = now - ((now - presentTimestamp) % vsyncPeriod);
3626
Patrick Williamsbb25f802022-08-30 23:02:34 +00003627 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
3628 refreshRate, renderRate, vote, gameMode);
3629 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(),
3630 mCurrentFrameNumber, actualPresentTime,
3631 FrameTracer::FrameEvent::PRESENT_FENCE);
3632 mFrameTracker.setActualPresentTime(actualPresentTime);
3633 }
3634 }
3635
3636 mFrameTracker.advanceFrame();
3637 mBufferInfo.mFrameLatencyNeeded = false;
3638}
3639
3640bool Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
3641 ATRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(),
3642 getDrawingState().frameNumber);
3643
3644 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
3645
3646 if (refreshRequired) {
3647 return refreshRequired;
3648 }
3649
3650 // If the head buffer's acquire fence hasn't signaled yet, return and
3651 // try again later
3652 if (!fenceHasSignaled()) {
3653 ATRACE_NAME("!fenceHasSignaled()");
3654 mFlinger->onLayerUpdate();
3655 return false;
3656 }
3657
3658 updateTexImage(latchTime);
3659 if (mDrawingState.buffer == nullptr) {
3660 return false;
3661 }
3662
3663 // Capture the old state of the layer for comparisons later
3664 BufferInfo oldBufferInfo = mBufferInfo;
3665 const bool oldOpacity = isOpaque(mDrawingState);
3666 mPreviousFrameNumber = mCurrentFrameNumber;
3667 mCurrentFrameNumber = mDrawingState.frameNumber;
3668 gatherBufferInfo();
3669
3670 if (oldBufferInfo.mBuffer == nullptr) {
3671 // the first time we receive a buffer, we need to trigger a
3672 // geometry invalidation.
3673 recomputeVisibleRegions = true;
3674 }
3675
3676 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
3677 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
3678 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
3679 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
3680 recomputeVisibleRegions = true;
3681 }
3682
3683 if (oldBufferInfo.mBuffer != nullptr) {
3684 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3685 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3686 if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
3687 bufHeight != oldBufferInfo.mBuffer->getHeight()) {
3688 recomputeVisibleRegions = true;
3689 }
3690 }
3691
3692 if (oldOpacity != isOpaque(mDrawingState)) {
3693 recomputeVisibleRegions = true;
3694 }
3695
3696 return true;
3697}
3698
3699bool Layer::hasReadyFrame() const {
3700 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
3701}
3702
3703bool Layer::isProtected() const {
3704 return (mBufferInfo.mBuffer != nullptr) &&
3705 (mBufferInfo.mBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
3706}
3707
Patrick Williamsbb25f802022-08-30 23:02:34 +00003708bool Layer::needsFiltering(const DisplayDevice* display) const {
3709 if (!hasBufferOrSidebandStream()) {
3710 return false;
3711 }
3712 const auto outputLayer = findOutputLayerForDisplay(display);
3713 if (outputLayer == nullptr) {
3714 return false;
3715 }
3716
3717 // We need filtering if the sourceCrop rectangle size does not match the
3718 // displayframe rectangle size (not a 1:1 render)
3719 const auto& compositionState = outputLayer->getState();
3720 const auto displayFrame = compositionState.displayFrame;
3721 const auto sourceCrop = compositionState.sourceCrop;
3722 return sourceCrop.getHeight() != displayFrame.getHeight() ||
3723 sourceCrop.getWidth() != displayFrame.getWidth();
3724}
3725
3726bool Layer::needsFilteringForScreenshots(const DisplayDevice* display,
3727 const ui::Transform& inverseParentTransform) const {
3728 if (!hasBufferOrSidebandStream()) {
3729 return false;
3730 }
3731 const auto outputLayer = findOutputLayerForDisplay(display);
3732 if (outputLayer == nullptr) {
3733 return false;
3734 }
3735
3736 // We need filtering if the sourceCrop rectangle size does not match the
3737 // viewport rectangle size (not a 1:1 render)
3738 const auto& compositionState = outputLayer->getState();
3739 const ui::Transform& displayTransform = display->getTransform();
3740 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
3741 // Undo the transformation of the displayFrame so that we're back into
3742 // layer-stack space.
3743 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
3744 const FloatRect sourceCrop = compositionState.sourceCrop;
3745
3746 int32_t frameHeight = frame.getHeight();
3747 int32_t frameWidth = frame.getWidth();
3748 // If the display transform had a rotational component then undo the
3749 // rotation so that the orientation matches the source crop.
3750 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
3751 std::swap(frameHeight, frameWidth);
3752 }
3753 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
3754}
3755
3756void Layer::latchAndReleaseBuffer() {
3757 if (hasReadyFrame()) {
3758 bool ignored = false;
3759 latchBuffer(ignored, systemTime());
3760 }
3761 releasePendingBuffer(systemTime());
3762}
3763
3764PixelFormat Layer::getPixelFormat() const {
3765 return mBufferInfo.mPixelFormat;
3766}
3767
3768bool Layer::getTransformToDisplayInverse() const {
3769 return mBufferInfo.mTransformToDisplayInverse;
3770}
3771
3772Rect Layer::getBufferCrop() const {
3773 // this is the crop rectangle that applies to the buffer
3774 // itself (as opposed to the window)
3775 if (!mBufferInfo.mCrop.isEmpty()) {
3776 // if the buffer crop is defined, we use that
3777 return mBufferInfo.mCrop;
3778 } else if (mBufferInfo.mBuffer != nullptr) {
3779 // otherwise we use the whole buffer
3780 return mBufferInfo.mBuffer->getBounds();
3781 } else {
3782 // if we don't have a buffer yet, we use an empty/invalid crop
3783 return Rect();
3784 }
3785}
3786
3787uint32_t Layer::getBufferTransform() const {
3788 return mBufferInfo.mTransform;
3789}
3790
3791ui::Dataspace Layer::getDataSpace() const {
3792 return mDrawingState.dataspaceRequested ? getRequestedDataSpace() : ui::Dataspace::UNKNOWN;
3793}
3794
3795ui::Dataspace Layer::getRequestedDataSpace() const {
3796 return hasBufferOrSidebandStream() ? mBufferInfo.mDataspace : mDrawingState.dataspace;
3797}
3798
3799ui::Dataspace Layer::translateDataspace(ui::Dataspace dataspace) {
3800 ui::Dataspace updatedDataspace = dataspace;
3801 // translate legacy dataspaces to modern dataspaces
3802 switch (dataspace) {
3803 case ui::Dataspace::SRGB:
3804 updatedDataspace = ui::Dataspace::V0_SRGB;
3805 break;
3806 case ui::Dataspace::SRGB_LINEAR:
3807 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
3808 break;
3809 case ui::Dataspace::JFIF:
3810 updatedDataspace = ui::Dataspace::V0_JFIF;
3811 break;
3812 case ui::Dataspace::BT601_625:
3813 updatedDataspace = ui::Dataspace::V0_BT601_625;
3814 break;
3815 case ui::Dataspace::BT601_525:
3816 updatedDataspace = ui::Dataspace::V0_BT601_525;
3817 break;
3818 case ui::Dataspace::BT709:
3819 updatedDataspace = ui::Dataspace::V0_BT709;
3820 break;
3821 default:
3822 break;
3823 }
3824
3825 return updatedDataspace;
3826}
3827
3828sp<GraphicBuffer> Layer::getBuffer() const {
3829 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
3830}
3831
Patrick Williamsbb25f802022-08-30 23:02:34 +00003832void Layer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
3833 mTransformHint = getFixedTransformHint();
3834 if (mTransformHint == ui::Transform::ROT_INVALID) {
3835 mTransformHint = displayTransformHint;
3836 }
Vishnu Nair71fcf912022-10-18 09:14:20 -07003837 mSkipReportingTransformHint = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003838}
3839
3840const std::shared_ptr<renderengine::ExternalTexture>& Layer::getExternalTexture() const {
3841 return mBufferInfo.mBuffer;
3842}
3843
3844bool Layer::setColor(const half3& color) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003845 if (mDrawingState.color.rgb == color) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003846 return false;
3847 }
3848
3849 mDrawingState.sequence++;
Vishnu Nairbbceb462022-10-10 04:52:13 +00003850 mDrawingState.color.rgb = color;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003851 mDrawingState.modified = true;
3852 setTransactionFlags(eTransactionNeeded);
3853 return true;
3854}
3855
3856bool Layer::fillsColor() const {
3857 return !hasBufferOrSidebandStream() && mDrawingState.color.r >= 0.0_hf &&
3858 mDrawingState.color.g >= 0.0_hf && mDrawingState.color.b >= 0.0_hf;
3859}
3860
3861bool Layer::hasBlur() const {
3862 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
3863}
3864
Vishnu Nairba354102022-08-01 00:14:18 +00003865void Layer::updateSnapshot(bool updateGeometry) {
3866 if (!getCompositionEngineLayerFE()) {
3867 return;
3868 }
3869
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003870 auto* snapshot = editLayerSnapshot();
Vishnu Nairba354102022-08-01 00:14:18 +00003871 if (updateGeometry) {
3872 prepareBasicGeometryCompositionState();
3873 prepareGeometryCompositionState();
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003874 snapshot->roundedCorner = getRoundedCornerState();
3875 snapshot->stretchEffect = getStretchEffect();
3876 snapshot->transformedBounds = mScreenBounds;
3877 if (mEffectiveShadowRadius > 0.f) {
3878 snapshot->shadowSettings = mFlinger->mDrawingState.globalShadowSettings;
3879
3880 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
3881 // it if transparent regions are present. This may not be necessary since shadows are
3882 // typically cast by layers without transparent regions.
3883 snapshot->shadowSettings.boundaries = mBounds;
3884
3885 const float casterAlpha = snapshot->alpha;
3886 const bool casterIsOpaque =
3887 ((mBufferInfo.mBuffer != nullptr) && isOpaque(mDrawingState));
3888
3889 // If the casting layer is translucent, we need to fill in the shadow underneath the
3890 // layer. Otherwise the generated shadow will only be shown around the casting layer.
3891 snapshot->shadowSettings.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
3892 snapshot->shadowSettings.ambientColor *= casterAlpha;
3893 snapshot->shadowSettings.spotColor *= casterAlpha;
3894 }
3895 snapshot->shadowSettings.length = mEffectiveShadowRadius;
Vishnu Nairba354102022-08-01 00:14:18 +00003896 }
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003897 snapshot->contentOpaque = isOpaque(mDrawingState);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00003898 snapshot->layerOpaqueFlagSet =
3899 (mDrawingState.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque;
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003900 snapshot->isHdrY410 = isHdrY410();
3901 snapshot->bufferNeedsFiltering = bufferNeedsFiltering();
3902 sp<Layer> p = mDrawingParent.promote();
3903 if (p != nullptr) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00003904 snapshot->parentTransform = p->getTransform();
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003905 } else {
Vishnu Nair8fc721b2022-12-22 20:06:32 +00003906 snapshot->parentTransform.reset();
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003907 }
3908 snapshot->bufferSize = getBufferSize(mDrawingState);
3909 snapshot->externalTexture = mBufferInfo.mBuffer;
Vishnu Naire14c6b32022-08-06 04:20:15 +00003910 snapshot->hasReadyFrame = hasReadyFrame();
Vishnu Nairba354102022-08-01 00:14:18 +00003911 preparePerFrameCompositionState();
3912}
3913
Vishnu Naire14c6b32022-08-06 04:20:15 +00003914void Layer::updateChildrenSnapshots(bool updateGeometry) {
3915 for (const sp<Layer>& child : mDrawingChildren) {
3916 child->updateSnapshot(updateGeometry);
3917 child->updateChildrenSnapshots(updateGeometry);
3918 }
3919}
3920
Vishnu Nair0a4fb002022-08-08 02:40:42 +00003921void Layer::updateMetadataSnapshot(const LayerMetadata& parentMetadata) {
3922 mSnapshot->layerMetadata = parentMetadata;
3923 mSnapshot->layerMetadata.merge(mDrawingState.metadata);
3924 for (const sp<Layer>& child : mDrawingChildren) {
3925 child->updateMetadataSnapshot(mSnapshot->layerMetadata);
3926 }
3927}
3928
3929void Layer::updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMetadata,
3930 std::unordered_set<Layer*>& visited) {
3931 if (visited.find(this) != visited.end()) {
3932 ALOGW("Cycle containing layer %s detected in z-order relatives", getDebugName());
3933 return;
3934 }
3935 visited.insert(this);
3936
3937 mSnapshot->relativeLayerMetadata = relativeLayerMetadata;
3938
3939 if (mDrawingState.zOrderRelatives.empty()) {
3940 return;
3941 }
3942 LayerMetadata childRelativeLayerMetadata = mSnapshot->relativeLayerMetadata;
3943 childRelativeLayerMetadata.merge(mSnapshot->layerMetadata);
3944 for (wp<Layer> weakRelative : mDrawingState.zOrderRelatives) {
3945 sp<Layer> relative = weakRelative.promote();
3946 if (!relative) {
3947 continue;
3948 }
3949 relative->updateRelativeMetadataSnapshot(childRelativeLayerMetadata, visited);
3950 }
3951}
3952
Patrick Williams377c0142022-10-13 17:33:38 +00003953LayerSnapshotGuard::LayerSnapshotGuard(Layer* layer) : mLayer(layer) {
Patrick Williams4da60852022-11-17 17:19:38 +00003954 if (mLayer) {
3955 mLayer->mLayerFE->mSnapshot = std::move(mLayer->mSnapshot);
3956 }
Patrick Williams377c0142022-10-13 17:33:38 +00003957}
3958
3959LayerSnapshotGuard::~LayerSnapshotGuard() {
3960 if (mLayer) {
3961 mLayer->mSnapshot = std::move(mLayer->mLayerFE->mSnapshot);
3962 }
3963}
3964
3965LayerSnapshotGuard::LayerSnapshotGuard(LayerSnapshotGuard&& other) : mLayer(other.mLayer) {
3966 other.mLayer = nullptr;
3967}
3968
3969LayerSnapshotGuard& LayerSnapshotGuard::operator=(LayerSnapshotGuard&& other) {
3970 mLayer = other.mLayer;
3971 other.mLayer = nullptr;
3972 return *this;
3973}
3974
Mathias Agopian13127d82013-03-05 17:47:11 -08003975// ---------------------------------------------------------------------------
3976
Marin Shalamanov46084422020-10-13 12:33:42 +02003977std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07003978 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
3979 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02003980}
3981
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07003982} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003983
3984#if defined(__gl_h_)
3985#error "don't include gl/gl.h in this file"
3986#endif
3987
3988#if defined(__gl2_h_)
3989#error "don't include gl2/gl2.h in this file"
3990#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08003991
3992// TODO(b/129481165): remove the #pragma below and fix conversion issues
3993#pragma clang diagnostic pop // ignored "-Wconversion"