blob: b47188fea49ceaf1cce14ba4e12f52d62c2b4e17 [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"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080073#include "LayerProtoHelper.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080075#include "TimeStats/TimeStats.h"
Robert Carr3e2a2992021-06-11 13:42:55 -070076#include "TunnelModeEnabledReporter.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070077
David Sodman41fdfc92017-11-06 16:09:56 -080078#define DEBUG_RESIZE 0
Patrick Williamsbb25f802022-08-30 23:02:34 +000079#define EARLY_RELEASE_ENABLED false
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010082namespace {
83constexpr int kDumpTableRowLength = 159;
Patrick Williamsbb25f802022-08-30 23:02:34 +000084
Prabir Pradhanda0f62c2022-07-22 19:53:04 +000085const ui::Transform kIdentityTransform;
Patrick Williamsbb25f802022-08-30 23:02:34 +000086
Patrick Williamsbb25f802022-08-30 23:02:34 +000087bool assignTransform(ui::Transform* dst, ui::Transform& from) {
88 if (*dst == from) {
89 return false;
90 }
91 *dst = from;
92 return true;
93}
94
95TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
96 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
97 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
98 const auto frameRateCompatibility = [frameRate] {
99 switch (frameRate.type) {
100 case Layer::FrameRateCompatibility::Default:
101 return FrameRateCompatibility::Default;
102 case Layer::FrameRateCompatibility::ExactOrMultiple:
103 return FrameRateCompatibility::ExactOrMultiple;
104 default:
105 return FrameRateCompatibility::Undefined;
106 }
107 }();
108
109 const auto seamlessness = [frameRate] {
110 switch (frameRate.seamlessness) {
111 case scheduler::Seamlessness::OnlySeamless:
112 return Seamlessness::ShouldBeSeamless;
113 case scheduler::Seamlessness::SeamedAndSeamless:
114 return Seamlessness::NotRequired;
115 default:
116 return Seamlessness::Undefined;
117 }
118 }();
119
120 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
121 .frameRateCompatibility = frameRateCompatibility,
122 .seamlessness = seamlessness};
123}
124
Marin Shalamanov1876e2e2020-12-04 13:23:59 +0100125} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700127using namespace ftl::flag_operators;
128
Yiwei Zhang5434a782018-12-05 18:06:32 -0800129using base::StringAppendF;
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800130using gui::GameMode;
131using gui::LayerMetadata;
chaviw3277faf2021-05-19 16:45:23 -0500132using gui::WindowInfo;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800133
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700134using PresentState = frametimeline::SurfaceFrame::PresentState;
135
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700136Layer::Layer(const LayerCreationArgs& args)
Vishnu Naircb8be502022-10-12 19:03:23 +0000137 : sequence(args.sequence),
Ady Abrahamd11bade2022-08-01 16:18:03 -0700138 mFlinger(sp<SurfaceFlinger>::fromExisting(args.flinger)),
Arthur Hung23e07502021-10-15 11:58:19 +0000139 mName(base::StringPrintf("%s#%d", args.name.c_str(), sequence)),
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700140 mClientRef(args.client),
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800141 mWindowType(static_cast<WindowInfo::Type>(
142 args.metadata.getInt32(gui::METADATA_WINDOW_TYPE, 0))),
Tianhao Yao67dd7122022-02-22 17:48:33 +0000143 mLayerCreationFlags(args.flags),
Patrick Williamsbb25f802022-08-30 23:02:34 +0000144 mBorderEnabled(false),
145 mTextureName(args.textureName),
Vishnu Naire14c6b32022-08-06 04:20:15 +0000146 mHwcSlotGenerator(sp<HwcSlotGenerator>::make()),
147 mLayerFE(args.flinger->getFactory().createLayerFE(mName)) {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000148 ALOGV("Creating Layer %s", getDebugName());
149
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700150 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700151 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
152 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
153 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -0700154 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
155 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Robert Carr6a160312021-05-17 12:08:20 -0700156 mDrawingState.flags = layerFlags;
Robert Carr6a160312021-05-17 12:08:20 -0700157 mDrawingState.crop.makeInvalid();
Robert Carr6a160312021-05-17 12:08:20 -0700158 mDrawingState.z = 0;
159 mDrawingState.color.a = 1.0f;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700160 mDrawingState.layerStack = ui::DEFAULT_LAYER_STACK;
Robert Carr6a160312021-05-17 12:08:20 -0700161 mDrawingState.sequence = 0;
Robert Carr6a160312021-05-17 12:08:20 -0700162 mDrawingState.transform.set(0, 0);
163 mDrawingState.frameNumber = 0;
164 mDrawingState.bufferTransform = 0;
165 mDrawingState.transformToDisplayInverse = false;
166 mDrawingState.crop.makeInvalid();
167 mDrawingState.acquireFence = sp<Fence>::make(-1);
168 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
169 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000170 mDrawingState.dataspaceRequested = false;
Robert Carr6a160312021-05-17 12:08:20 -0700171 mDrawingState.hdrMetadata.validTypes = 0;
172 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
173 mDrawingState.cornerRadius = 0.0f;
174 mDrawingState.backgroundBlurRadius = 0;
175 mDrawingState.api = -1;
176 mDrawingState.hasColorTransform = false;
177 mDrawingState.colorSpaceAgnostic = false;
178 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
179 mDrawingState.metadata = args.metadata;
180 mDrawingState.shadowRadius = 0.f;
181 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
182 mDrawingState.frameTimelineInfo = {};
183 mDrawingState.postTime = -1;
184 mDrawingState.destinationFrame.makeInvalid();
Robin Leeedb375d2021-09-10 12:03:42 +0000185 mDrawingState.isTrustedOverlay = false;
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -0700186 mDrawingState.dropInputMode = gui::DropInputMode::NONE;
Sally Qi81d95e62022-03-21 19:41:33 -0700187 mDrawingState.dimmingEnabled = true;
Andy Labrada096227e2022-06-15 16:58:11 +0000188 mDrawingState.defaultFrameRateCompatibility = FrameRateCompatibility::Default;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700189
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700190 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
191 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700192 mDrawingState.color.r = -1.0_hf;
193 mDrawingState.color.g = -1.0_hf;
194 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700195 }
Dominik Laskowski4376bd82022-07-07 11:50:20 -0700196
197 mFrameTracker.setDisplayRefreshPeriod(
198 args.flinger->mScheduler->getVsyncPeriodFromRefreshRateConfigs());
Robert Carr2e102c92018-10-23 12:11:15 -0700199
Vishnu Naircb8be502022-10-12 19:03:23 +0000200 mOwnerUid = args.ownerUid;
201 mOwnerPid = args.ownerPid;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000202
203 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
204 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
205 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
206 mDrawingState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000207
208 mSnapshot->sequence = sequence;
209 mSnapshot->name = getDebugName();
210 mSnapshot->textureName = mTextureName;
211 mSnapshot->premultipliedAlpha = mPremultipliedAlpha;
212 mSnapshot->transform = {};
Dominik Laskowski75848362019-11-11 17:57:20 -0800213}
214
215void Layer::onFirstRef() {
216 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700217}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700218
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700219Layer::~Layer() {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000220 // The original layer and the clone layer share the same texture and buffer. Therefore, only
221 // one of the layers, in this case the original layer, needs to handle the deletion. The
222 // original layer and the clone should be removed at the same time so there shouldn't be any
223 // issue with the clone layer trying to use the texture.
224 if (mBufferInfo.mBuffer != nullptr) {
225 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
226 mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
227 mBufferInfo.mFence,
228 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
229 mOwnerUid));
230 }
231 if (!isClone()) {
232 // The original layer and the clone layer share the same texture. Therefore, only one of
233 // the layers, in this case the original layer, needs to handle the deletion. The original
234 // layer and the clone should be removed at the same time so there shouldn't be any issue
235 // with the clone layer trying to use the deleted texture.
236 mFlinger->deleteTextureAsync(mTextureName);
237 }
238 const int32_t layerId = getSequence();
239 mFlinger->mTimeStats->onDestroy(layerId);
240 mFlinger->mFrameTracer->onDestroy(layerId);
241
David Sodman577c8962017-12-08 14:50:53 -0800242 sp<Client> c(mClientRef.promote());
243 if (c != 0) {
244 c->detachLayer(this);
245 }
246
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000247 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700248 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700249
250 if (mDrawingState.sidebandStream != nullptr) {
251 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
252 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700253 if (mHadClonedChild) {
254 mFlinger->mNumClones--;
255 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700256}
257
Mathias Agopian13127d82013-03-05 17:47:11 -0800258// ---------------------------------------------------------------------------
259// callbacks
260// ---------------------------------------------------------------------------
261
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700262void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700263 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700264 return;
265 }
Robert Carr2e102c92018-10-23 12:11:15 -0700266
Robert Carr6a160312021-05-17 12:08:20 -0700267 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700268 if (strongRelative == nullptr) {
269 setZOrderRelativeOf(nullptr);
270 return;
271 }
272
273 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700274 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700275 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800276 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700277 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700278}
279
280void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700281 if (!mRemovedFromDrawingState) {
282 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700283 mFlinger->mScheduler->deregisterLayer(this);
284 }
Rob Carr4bba3702018-10-08 21:53:30 +0000285
Ady Abrahamd11bade2022-08-01 16:18:03 -0700286 mFlinger->markLayerPendingRemovalLocked(sp<Layer>::fromExisting(this));
Chia-I Wuc6657022017-08-15 11:18:17 -0700287}
Chia-I Wu38512252017-05-17 14:36:16 -0700288
chaviw68d4dab2020-06-08 15:07:32 -0700289sp<Layer> Layer::getRootLayer() {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000290 sp<Layer> parent = getParent();
chaviw68d4dab2020-06-08 15:07:32 -0700291 if (parent == nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700292 return sp<Layer>::fromExisting(this);
chaviw68d4dab2020-06-08 15:07:32 -0700293 }
294 return parent->getRootLayer();
295}
296
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700297void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700298 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
299 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700300 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700301
302 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700303 layer->removeFromCurrentState();
304 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700305 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700306}
307
chaviw61626f22018-11-15 16:26:27 -0800308void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700309 if (mRemovedFromDrawingState) {
310 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700311 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700312 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700313 }
chaviw61626f22018-11-15 16:26:27 -0800314
315 for (const auto& child : mCurrentChildren) {
316 child->addToCurrentState();
317 }
318}
319
Mathias Agopian13127d82013-03-05 17:47:11 -0800320// ---------------------------------------------------------------------------
321// set-up
322// ---------------------------------------------------------------------------
323
chaviw13fdc492017-06-27 12:40:18 -0700324bool Layer::getPremultipledAlpha() const {
325 return mPremultipliedAlpha;
326}
327
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700328sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800329 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700330 if (mGetHandleCalled) {
331 ALOGE("Get handle called twice" );
332 return nullptr;
333 }
334 mGetHandleCalled = true;
Ady Abrahamd11bade2022-08-01 16:18:03 -0700335 return sp<Handle>::make(mFlinger, sp<Layer>::fromExisting(this));
Mathias Agopian13127d82013-03-05 17:47:11 -0800336}
337
338// ---------------------------------------------------------------------------
339// h/w composer set-up
340// ---------------------------------------------------------------------------
341
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700342static Rect reduce(const Rect& win, const Region& exclude) {
343 if (CC_LIKELY(exclude.isEmpty())) {
344 return win;
345 }
346 if (exclude.isRect()) {
347 return win.reduce(exclude.getBounds());
348 }
349 return Region(win).subtract(exclude).getBounds();
350}
351
Dan Stoza80d61162017-12-20 15:57:52 -0800352static FloatRect reduce(const FloatRect& win, const Region& exclude) {
353 if (CC_LIKELY(exclude.isEmpty())) {
354 return win;
355 }
356 // Convert through Rect (by rounding) for lack of FloatRegion
357 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
358}
359
Vishnu Nair4351ad52019-02-11 14:13:02 -0800360Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800361 if (!reduceTransparentRegion) {
362 return Rect{mScreenBounds};
363 }
364
365 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800366 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800367 // Transform to screen space.
368 bounds = t.transform(bounds);
369 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700370}
371
Vishnu Nair4351ad52019-02-11 14:13:02 -0800372FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000373 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800374 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700375}
376
Vishnu Nairf0c28512019-02-08 12:40:28 -0800377FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
378 // Subtract the transparent region and snap to the bounds.
379 return reduce(mBounds, activeTransparentRegion);
380}
381
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700382void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
383 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800384 const State& s(getDrawingState());
385
386 // Calculate effective layer transform
387 mEffectiveTransform = parentTransform * getActiveTransform(s);
388
Garfield Tan2c1782c2022-02-16 15:25:05 -0800389 if (CC_UNLIKELY(!isTransformValid())) {
390 ALOGW("Stop computing bounds for %s because it has invalid transformation.",
391 getDebugName());
392 return;
393 }
394
Vishnu Nair4351ad52019-02-11 14:13:02 -0800395 // Transform parent bounds to layer space
396 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
397
Vishnu Nairc652ff82019-03-15 12:48:54 -0700398 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800399 mSourceBounds = computeSourceBounds(parentBounds);
400
401 // Calculate bounds by croping diplay frame with layer crop and parent bounds
402 FloatRect bounds = mSourceBounds;
403 const Rect layerCrop = getCrop(s);
404 if (!layerCrop.isEmpty()) {
405 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
406 }
407 bounds = bounds.intersect(parentBounds);
408
409 mBounds = bounds;
410 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700411
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700412 // Use the layer's own shadow radius if set. Otherwise get the radius from
413 // parent.
414 if (s.shadowRadius > 0.f) {
415 mEffectiveShadowRadius = s.shadowRadius;
416 } else {
417 mEffectiveShadowRadius = parentShadowRadius;
418 }
419
420 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
421 // don't pass it to its children.
422 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
423
Vishnu Nair4351ad52019-02-11 14:13:02 -0800424 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700425 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800426 }
Vishnu Nairba354102022-08-01 00:14:18 +0000427
428 if (mPotentialCursor) {
429 prepareCursorCompositionState();
430 }
Vishnu Nair4351ad52019-02-11 14:13:02 -0800431}
432
Vishnu Nair60356342018-11-13 13:00:45 -0800433Rect Layer::getCroppedBufferSize(const State& s) const {
434 Rect size = getBufferSize(s);
435 Rect crop = getCrop(s);
436 if (!crop.isEmpty() && size.isValid()) {
437 size.intersect(crop, &size);
438 } else if (!crop.isEmpty()) {
439 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700440 }
Vishnu Nair60356342018-11-13 13:00:45 -0800441 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800442}
443
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700444void Layer::setupRoundedCornersCropCoordinates(Rect win,
445 const FloatRect& roundedCornersCrop) const {
446 // Translate win by the rounded corners rect coordinates, to have all values in
447 // layer coordinate space.
448 win.left -= roundedCornersCrop.left;
449 win.right -= roundedCornersCrop.left;
450 win.top -= roundedCornersCrop.top;
451 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700452}
453
Lloyd Piquede196652020-01-22 17:29:58 -0800454void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800455 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800456 const auto alpha = static_cast<float>(getAlpha());
457 const bool opaque = isOpaque(drawingState);
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700458 const bool usesRoundedCorners = hasRoundedCorners();
Lloyd Piquec6687342019-03-07 21:34:57 -0800459
460 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
461 if (!opaque || alpha != 1.0f) {
462 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
463 : Hwc2::IComposerClient::BlendMode::COVERAGE;
464 }
465
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000466 auto* snapshot = editLayerSnapshot();
467 snapshot->outputFilter = getOutputFilter();
468 snapshot->isVisible = isVisible();
469 snapshot->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
470 snapshot->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800471
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000472 snapshot->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800473 contentDirty = false;
474
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000475 snapshot->geomLayerBounds = mBounds;
476 snapshot->geomLayerTransform = getTransform();
477 snapshot->geomInverseLayerTransform = snapshot->geomLayerTransform.inverse();
478 snapshot->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800479
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();
581 snapshot->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
Patrick Williamsbb25f802022-08-30 23:02:34 +0000582 ? 0
583 : mBufferInfo.mBufferSlot;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000584 snapshot->acquireFence = mBufferInfo.mFence;
585 snapshot->frameNumber = mBufferInfo.mFrameNumber;
586 snapshot->sidebandStreamHasFrame = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000587}
588
589void Layer::preparePerFrameEffectsCompositionState() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000590 auto* snapshot = editLayerSnapshot();
591 snapshot->color = getColor();
592 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000593 aidl::android::hardware::graphics::composer3::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800594}
595
Lloyd Piquede196652020-01-22 17:29:58 -0800596void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800597 const State& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000598 auto* snapshot = editLayerSnapshot();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800599
600 // Apply the layer's transform, followed by the display's global transform
601 // Here we're guaranteed that the layer's transform preserves rects
602 Rect win = getCroppedBufferSize(drawingState);
603 // Subtract the transparent region and snap to the bounds
604 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
605 Rect frame(getTransform().transform(bounds));
606
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000607 snapshot->cursorFrame = frame;
Lloyd Piquede196652020-01-22 17:29:58 -0800608}
609
Lloyd Piquea83776c2019-01-29 18:42:32 -0800610const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700611 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800612}
613
Mathias Agopian13127d82013-03-05 17:47:11 -0800614// ---------------------------------------------------------------------------
615// drawing...
616// ---------------------------------------------------------------------------
617
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500618aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType(
619 const DisplayDevice& display) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700620 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800621 if (outputLayer == nullptr) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500622 return aidl::android::hardware::graphics::composer3::Composition::INVALID;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800623 }
624 if (outputLayer->getState().hwc) {
625 return (*outputLayer->getState().hwc).hwcCompositionType;
626 } else {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500627 return aidl::android::hardware::graphics::composer3::Composition::CLIENT;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800628 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629}
630
Mathias Agopian13127d82013-03-05 17:47:11 -0800631// ----------------------------------------------------------------------------
632// local state
633// ----------------------------------------------------------------------------
634
David Sodman41fdfc92017-11-06 16:09:56 -0800635bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800636 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700637 if (s.flags & layer_state_t::eLayerSecure) {
638 return true;
639 }
640
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000641 const auto p = mDrawingParent.promote();
Garfield Tande619fa2020-10-02 17:13:53 -0700642 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700643}
644
Mathias Agopian13127d82013-03-05 17:47:11 -0800645// ----------------------------------------------------------------------------
646// transaction
647// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800648
Marissa Wall61c58622018-07-18 10:12:20 -0700649uint32_t Layer::doTransaction(uint32_t flags) {
650 ATRACE_CALL();
651
Robert Carr0758e5d2021-03-11 22:15:04 -0800652 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700653 mDrawingStateModified = mDrawingState.modified;
654 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700655
Alec Mourib416efd2018-09-06 21:01:59 +0000656 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700657
Robert Carr6a160312021-05-17 12:08:20 -0700658 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800659 // invalidate and recompute the visible regions if needed
660 flags |= Layer::eVisibleRegion;
661 }
662
Robert Carr6a160312021-05-17 12:08:20 -0700663 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800664 // invalidate and recompute the visible regions if needed
Arthur Hung9ed43392022-05-27 06:31:57 +0000665 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800666 flags |= eVisibleRegion;
667 this->contentDirty = true;
668
669 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700670 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800671 }
672
Arthur Hung9ed43392022-05-27 06:31:57 +0000673 if (!mPotentialCursor && (flags & Layer::eVisibleRegion)) {
674 mFlinger->mUpdateInputInfo = true;
675 }
676
Robert Carr6a160312021-05-17 12:08:20 -0700677 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800678
Mathias Agopian13127d82013-03-05 17:47:11 -0800679 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680}
681
Robert Carr6a160312021-05-17 12:08:20 -0700682void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000683 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
684 // bufferSurfaceFrameTX will be presented in latchBuffer.
685 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
686 if (surfaceFrame->getPresentState() != PresentState::Presented) {
687 // With applyPendingStates, we could end up having presented surfaceframes from previous
688 // states
689 surfaceFrame->setPresentState(PresentState::Presented);
690 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
691 }
692 }
Robert Carr6a160312021-05-17 12:08:20 -0700693 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700694}
695
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700696uint32_t Layer::clearTransactionFlags(uint32_t mask) {
697 const auto flags = mTransactionFlags & mask;
698 mTransactionFlags &= ~mask;
699 return flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800700}
701
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700702void Layer::setTransactionFlags(uint32_t mask) {
703 mTransactionFlags |= mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800704}
705
Robert Carr1f0a16a2016-10-24 16:27:39 -0700706bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
707 ssize_t idx = mCurrentChildren.indexOf(childLayer);
708 if (idx < 0) {
709 return false;
710 }
711 if (childLayer->setLayer(z)) {
712 mCurrentChildren.removeAt(idx);
713 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800714 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700715 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800716 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700717}
718
Robert Carr503c7042017-09-27 15:06:08 -0700719bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
720 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
721 ssize_t idx = mCurrentChildren.indexOf(childLayer);
722 if (idx < 0) {
723 return false;
724 }
725 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
726 mCurrentChildren.removeAt(idx);
727 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800728 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700729 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800730 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700731}
732
Robert Carrae060832016-11-28 10:51:00 -0800733bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700734 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
735 mDrawingState.sequence++;
736 mDrawingState.z = z;
737 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700738
Robert Carra70e91c2021-06-11 13:59:52 -0700739 mFlinger->mSomeChildrenChanged = true;
740
Robert Carrdb66e622017-04-10 16:55:57 -0700741 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700742 if (mDrawingState.zOrderRelativeOf != nullptr) {
743 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700744 if (strongRelative != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700745 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700746 }
chaviw606e5cf2019-03-01 10:12:10 -0800747 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700748 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800749 setTransactionFlags(eTransactionNeeded);
750 return true;
751}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700752
Robert Carrdb66e622017-04-10 16:55:57 -0700753void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700754 mDrawingState.zOrderRelatives.remove(relative);
755 mDrawingState.sequence++;
756 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700757 setTransactionFlags(eTransactionNeeded);
758}
759
760void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700761 mDrawingState.zOrderRelatives.add(relative);
762 mDrawingState.modified = true;
763 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700764 setTransactionFlags(eTransactionNeeded);
765}
766
chaviw606e5cf2019-03-01 10:12:10 -0800767void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700768 mDrawingState.zOrderRelativeOf = relativeOf;
769 mDrawingState.sequence++;
770 mDrawingState.modified = true;
771 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700772
chaviw606e5cf2019-03-01 10:12:10 -0800773 setTransactionFlags(eTransactionNeeded);
774}
775
Robert Carr503d2bd2017-12-04 15:49:47 -0800776bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nairf9096652021-07-20 18:49:42 -0700777 sp<Layer> relative = fromHandle(relativeToHandle).promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700778 if (relative == nullptr) {
779 return false;
780 }
781
Robert Carr6a160312021-05-17 12:08:20 -0700782 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
783 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800784 return false;
785 }
786
Robert Carr88b85e12022-03-21 15:47:35 -0700787 if (CC_UNLIKELY(relative->usingRelativeZ(LayerVector::StateSet::Drawing)) &&
788 (relative->mDrawingState.zOrderRelativeOf == this)) {
789 ALOGE("Detected relative layer loop between %s and %s",
790 mName.c_str(), relative->mName.c_str());
791 ALOGE("Ignoring new call to set relative layer");
792 return false;
793 }
794
Robert Carra70e91c2021-06-11 13:59:52 -0700795 mFlinger->mSomeChildrenChanged = true;
796
Robert Carr6a160312021-05-17 12:08:20 -0700797 mDrawingState.sequence++;
798 mDrawingState.modified = true;
799 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700800
Robert Carr6a160312021-05-17 12:08:20 -0700801 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700802 if (oldZOrderRelativeOf != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700803 oldZOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
chaviw9ab4bd12017-11-03 13:11:00 -0700804 }
chaviw606e5cf2019-03-01 10:12:10 -0800805 setZOrderRelativeOf(relative);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700806 relative->addZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700807
808 setTransactionFlags(eTransactionNeeded);
809
810 return true;
811}
812
Winson Chunga30f7c92021-06-29 15:42:56 -0700813bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
814 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
815 mDrawingState.isTrustedOverlay = isTrustedOverlay;
816 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +0000817 mFlinger->mUpdateInputInfo = true;
Winson Chunga30f7c92021-06-29 15:42:56 -0700818 setTransactionFlags(eTransactionNeeded);
819 return true;
820}
821
822bool Layer::isTrustedOverlay() const {
823 if (getDrawingState().isTrustedOverlay) {
824 return true;
825 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000826 const auto& p = mDrawingParent.promote();
Winson Chunga30f7c92021-06-29 15:42:56 -0700827 return (p != nullptr) && p->isTrustedOverlay();
828}
829
Dan Stoza9e56aa02015-11-02 13:00:03 -0800830bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700831 if (mDrawingState.color.a == alpha) return false;
832 mDrawingState.sequence++;
833 mDrawingState.color.a = alpha;
834 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800835 setTransactionFlags(eTransactionNeeded);
836 return true;
837}
chaviw13fdc492017-06-27 12:40:18 -0700838
Valerie Haudd0b7572019-01-29 14:59:27 -0800839bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700840 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700841 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800842 }
Robert Carr6a160312021-05-17 12:08:20 -0700843 mDrawingState.sequence++;
844 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800845 setTransactionFlags(eTransactionNeeded);
846
Robert Carr6a160312021-05-17 12:08:20 -0700847 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800848 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800849 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700850 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700851 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800852 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700853 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700854
Valerie Haudd0b7572019-01-29 14:59:27 -0800855 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700856 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800857 mFlinger->mLayersAdded = true;
858 // set up SF to handle added color layer
859 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700860 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800861 }
862 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700863 } else if (mDrawingState.bgColorLayer && alpha == 0) {
864 mDrawingState.bgColorLayer->reparent(nullptr);
865 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800866 return true;
867 }
868
Robert Carr6a160312021-05-17 12:08:20 -0700869 mDrawingState.bgColorLayer->setColor(color);
870 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
871 mDrawingState.bgColorLayer->setAlpha(alpha);
872 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800873
chaviw13fdc492017-06-27 12:40:18 -0700874 return true;
875}
876
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700877bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700878 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700879
Robert Carr6a160312021-05-17 12:08:20 -0700880 mDrawingState.sequence++;
881 mDrawingState.cornerRadius = cornerRadius;
882 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700883 setTransactionFlags(eTransactionNeeded);
884 return true;
885}
886
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800887bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700888 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Vishnu Nairb801a982021-11-02 15:12:08 -0700889 // If we start or stop drawing blur then the layer's visibility state may change so increment
890 // the magic sequence number.
891 if (mDrawingState.backgroundBlurRadius == 0 || backgroundBlurRadius == 0) {
892 mDrawingState.sequence++;
893 }
Robert Carr6a160312021-05-17 12:08:20 -0700894 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
895 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800896 setTransactionFlags(eTransactionNeeded);
897 return true;
898}
Marissa Wall61c58622018-07-18 10:12:20 -0700899
Mathias Agopian13127d82013-03-05 17:47:11 -0800900bool Layer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000901 mDrawingState.sequence++;
902 mDrawingState.transparentRegionHint = transparent;
Robert Carr6a160312021-05-17 12:08:20 -0700903 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800904 setTransactionFlags(eTransactionNeeded);
905 return true;
906}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100907
Lucas Dupinc3800b82020-10-02 16:24:48 -0700908bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Vishnu Nairb801a982021-11-02 15:12:08 -0700909 // If we start or stop drawing blur then the layer's visibility state may change so increment
910 // the magic sequence number.
911 if (mDrawingState.blurRegions.size() == 0 || blurRegions.size() == 0) {
912 mDrawingState.sequence++;
913 }
Robert Carr6a160312021-05-17 12:08:20 -0700914 mDrawingState.blurRegions = blurRegions;
915 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700916 setTransactionFlags(eTransactionNeeded);
917 return true;
918}
919
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800920bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700921 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
922 if (mDrawingState.flags == newFlags) return false;
923 mDrawingState.sequence++;
924 mDrawingState.flags = newFlags;
925 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800926 setTransactionFlags(eTransactionNeeded);
927 return true;
928}
Robert Carr99e27f02016-06-16 15:18:02 -0700929
chaviw25714502021-02-11 10:01:08 -0800930bool Layer::setCrop(const Rect& crop) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000931 if (mDrawingState.crop == crop) return false;
Robert Carr6a160312021-05-17 12:08:20 -0700932 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700933 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700934
Robert Carr6a160312021-05-17 12:08:20 -0700935 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800936 setTransactionFlags(eTransactionNeeded);
937 return true;
938}
Robert Carr8d5227b2017-03-16 15:41:03 -0700939
Evan Roskyef876c92019-01-25 17:46:06 -0800940bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700941 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
942 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -0800943 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800944 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500945}
946
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700947bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -0700948 if (mDrawingState.layerStack == layerStack) return false;
949 mDrawingState.sequence++;
950 mDrawingState.layerStack = layerStack;
951 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800952 setTransactionFlags(eTransactionNeeded);
953 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700954}
955
Peiyong Linc502cb72019-03-01 15:00:23 -0800956bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -0700957 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800958 return false;
959 }
Robert Carr6a160312021-05-17 12:08:20 -0700960 mDrawingState.sequence++;
961 mDrawingState.colorSpaceAgnostic = agnostic;
962 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -0800963 setTransactionFlags(eTransactionNeeded);
964 return true;
965}
966
Sally Qi81d95e62022-03-21 19:41:33 -0700967bool Layer::setDimmingEnabled(const bool dimmingEnabled) {
968 if (mDrawingState.dimmingEnabled == dimmingEnabled) return false;
969
970 mDrawingState.sequence++;
971 mDrawingState.dimmingEnabled = dimmingEnabled;
972 mDrawingState.modified = true;
973 setTransactionFlags(eTransactionNeeded);
974 return true;
975}
976
Ana Krulecc84d09b2019-11-02 23:10:29 +0100977bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -0700978 if (mDrawingState.frameRateSelectionPriority == priority) return false;
979 mDrawingState.frameRateSelectionPriority = priority;
980 mDrawingState.sequence++;
981 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100982 setTransactionFlags(eTransactionNeeded);
983 return true;
984}
985
986int32_t Layer::getFrameRateSelectionPriority() const {
987 // Check if layer has priority set.
988 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
989 return mDrawingState.frameRateSelectionPriority;
990 }
991 // If not, search whether its parents have it set.
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000992 sp<Layer> parent = getParent();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100993 if (parent != nullptr) {
994 return parent->getFrameRateSelectionPriority();
995 }
996
997 return Layer::PRIORITY_UNSET;
998}
999
Andy Labrada096227e2022-06-15 16:58:11 +00001000bool Layer::setDefaultFrameRateCompatibility(FrameRateCompatibility compatibility) {
1001 if (mDrawingState.defaultFrameRateCompatibility == compatibility) return false;
1002 mDrawingState.defaultFrameRateCompatibility = compatibility;
1003 mDrawingState.modified = true;
1004 mFlinger->mScheduler->setDefaultFrameRateCompatibility(this);
1005 setTransactionFlags(eTransactionNeeded);
1006 return true;
1007}
1008
1009scheduler::LayerInfo::FrameRateCompatibility Layer::getDefaultFrameRateCompatibility() const {
1010 return mDrawingState.defaultFrameRateCompatibility;
1011}
1012
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001013bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1014 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1015};
1016
Vishnu Nair73908d12022-10-24 21:46:42 -07001017ui::LayerStack Layer::getLayerStack(LayerVector::StateSet state) const {
1018 bool useDrawing = state == LayerVector::StateSet::Drawing;
1019 const auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1020 if (parent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001021 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001022 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001023 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001024}
1025
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001026bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001027 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001028 return false;
1029 }
1030
Robert Carr6a160312021-05-17 12:08:20 -07001031 mDrawingState.sequence++;
1032 mDrawingState.shadowRadius = shadowRadius;
1033 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001034 setTransactionFlags(eTransactionNeeded);
1035 return true;
1036}
1037
Vishnu Nair6213bd92020-05-08 17:42:25 -07001038bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001039 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001040 return false;
1041 }
1042
Robert Carr6a160312021-05-17 12:08:20 -07001043 mDrawingState.sequence++;
1044 mDrawingState.fixedTransformHint = fixedTransformHint;
1045 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001046 setTransactionFlags(eTransactionNeeded);
1047 return true;
1048}
1049
John Reckcdb4ed72021-02-04 13:39:33 -05001050bool Layer::setStretchEffect(const StretchEffect& effect) {
1051 StretchEffect temp = effect;
1052 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001053 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001054 return false;
1055 }
Robert Carr6a160312021-05-17 12:08:20 -07001056 mDrawingState.sequence++;
1057 mDrawingState.stretchEffect = temp;
1058 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001059 setTransactionFlags(eTransactionNeeded);
1060 return true;
1061}
1062
John Reckc00c6692021-02-16 11:37:33 -05001063StretchEffect Layer::getStretchEffect() const {
1064 if (mDrawingState.stretchEffect.hasEffect()) {
1065 return mDrawingState.stretchEffect;
1066 }
1067
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001068 sp<Layer> parent = getParent();
John Reckc00c6692021-02-16 11:37:33 -05001069 if (parent != nullptr) {
1070 auto effect = parent->getStretchEffect();
1071 if (effect.hasEffect()) {
1072 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1073 return effect;
1074 }
1075 }
1076 return StretchEffect{};
1077}
1078
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001079bool Layer::enableBorder(bool shouldEnable, float width, const half4& color) {
1080 if (mBorderEnabled == shouldEnable && mBorderWidth == width && mBorderColor == color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00001081 return false;
1082 }
1083 mBorderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001084 mBorderWidth = width;
1085 mBorderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001086 return true;
1087}
1088
1089bool Layer::isBorderEnabled() {
1090 return mBorderEnabled;
1091}
1092
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001093float Layer::getBorderWidth() {
1094 return mBorderWidth;
1095}
1096
1097const half4& Layer::getBorderColor() {
1098 return mBorderColor;
1099}
1100
Ady Abrahama850c182021-08-04 13:04:37 -07001101bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1102 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1103 const auto frameRate = [&] {
1104 if (mDrawingState.frameRate.rate.isValid() ||
1105 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1106 return mDrawingState.frameRate;
1107 }
1108
1109 return parentFrameRate;
1110 }();
1111
1112 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1113
1114 // The frame rate is propagated to the children
1115 bool childrenHaveFrameRate = false;
1116 for (const sp<Layer>& child : mCurrentChildren) {
1117 childrenHaveFrameRate |=
1118 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1119 }
1120
1121 // If we don't have a valid frame rate, but the children do, we set this
1122 // layer as NoVote to allow the children to control the refresh rate
1123 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1124 childrenHaveFrameRate) {
1125 *transactionNeeded |=
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001126 setFrameRateForLayerTree(FrameRate(Fps(), FrameRateCompatibility::NoVote));
Ady Abrahama850c182021-08-04 13:04:37 -07001127 }
1128
1129 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1130 // the same reason we are allowing touch boost for those layers. See
1131 // RefreshRateConfigs::getBestRefreshRate for more details.
1132 const auto layerVotedWithDefaultCompatibility =
1133 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1134 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1135 const auto layerVotedWithExactCompatibility =
1136 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1137 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1138 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1139}
1140
Ady Abraham60e42ea2020-03-09 19:17:31 -07001141void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001142 const auto root = [&]() -> sp<Layer> {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001143 sp<Layer> layer = sp<Layer>::fromExisting(this);
Ady Abrahama850c182021-08-04 13:04:37 -07001144 while (auto parent = layer->getParent()) {
1145 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001146 }
Ady Abrahama850c182021-08-04 13:04:37 -07001147 return layer;
1148 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001149
Ady Abraham60e42ea2020-03-09 19:17:31 -07001150 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001151 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001152
Ady Abrahama850c182021-08-04 13:04:37 -07001153 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001154 if (transactionNeeded) {
1155 mFlinger->setTransactionFlags(eTraversalNeeded);
1156 }
1157}
1158
Ady Abraham71c437d2020-01-31 15:56:57 -08001159bool Layer::setFrameRate(FrameRate frameRate) {
Robert Carr6a160312021-05-17 12:08:20 -07001160 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001161 return false;
1162 }
1163
Robert Carr6a160312021-05-17 12:08:20 -07001164 mDrawingState.sequence++;
1165 mDrawingState.frameRate = frameRate;
1166 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001167
1168 updateTreeHasFrameRateVote();
1169
Steven Thomas3172e202020-01-06 19:25:30 -08001170 setTransactionFlags(eTransactionNeeded);
1171 return true;
1172}
1173
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001174void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1175 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001176 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001177
1178 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1179 // there are two transactions with the same token, the first one without a buffer and the
1180 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1181 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001182 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1183 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001184 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001185 mDrawingState.bufferSurfaceFrameTX = it->second;
1186 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1187 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1188 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001189 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001190 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001191 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1192 }
1193}
1194
1195void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1196 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001197 mDrawingState.frameTimelineInfo = info;
1198 mDrawingState.postTime = postTime;
1199 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001200 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001201
Robert Carr6a160312021-05-17 12:08:20 -07001202 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001203 bufferSurfaceFrameTX != nullptr) {
1204 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1205 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1206 // being used for BufferSurfaceFrame, don't create a new one.
1207 return;
1208 }
1209 }
1210 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1211 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1212 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001213 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1214 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001215 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001216 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001217 } else {
1218 if (it->second->getPresentState() == PresentState::Presented) {
1219 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1220 // have been from previous vsync.
1221 it->second = createSurfaceFrameForTransaction(info, postTime);
1222 }
1223 }
1224}
1225
1226void Layer::addSurfaceFrameDroppedForBuffer(
1227 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001228 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001229 surfaceFrame->setPresentState(PresentState::Dropped);
1230 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1231}
1232
1233void Layer::addSurfaceFramePresentedForBuffer(
1234 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1235 nsecs_t currentLatchTime) {
1236 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1237 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1238 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1239 mLastLatchTime = currentLatchTime;
1240}
1241
1242std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1243 const FrameTimelineInfo& info, nsecs_t postTime) {
1244 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001245 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1246 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001247 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001248 /*isBuffer*/ false, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001249 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001250 // For Transactions, the post time is considered to be both queue and acquire fence time.
1251 surfaceFrame->setActualQueueTime(postTime);
1252 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001253 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1254 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001255 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001256 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001257 onSurfaceFrameCreated(surfaceFrame);
1258 return surfaceFrame;
1259}
1260
1261std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1262 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1263 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001264 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001265 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001266 /*isBuffer*/ true, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001267 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001268 // For buffers, acquire fence time will set during latch.
1269 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001270 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1271 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001272 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001273 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001274 onSurfaceFrameCreated(surfaceFrame);
1275 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001276}
1277
Ady Abrahama850c182021-08-04 13:04:37 -07001278bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1279 if (mDrawingState.frameRateForLayerTree == frameRate) {
1280 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001281 }
1282
Ady Abrahama850c182021-08-04 13:04:37 -07001283 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001284
Ady Abrahama850c182021-08-04 13:04:37 -07001285 // TODO(b/195668952): we probably don't need to dirty visible regions here
1286 // or even store frameRateForLayerTree in mDrawingState
1287 mDrawingState.sequence++;
1288 mDrawingState.modified = true;
1289 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001290
Dominik Laskowski068173d2021-08-11 17:22:59 -07001291 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
1292 mFlinger->mScheduler->recordLayerHistory(this, systemTime(), LayerUpdateType::SetFrameRate);
Ady Abrahama850c182021-08-04 13:04:37 -07001293
1294 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001295}
1296
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001297Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1298 return getDrawingState().frameRateForLayerTree;
1299}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300
Robert Carr1f0a16a2016-10-24 16:27:39 -07001301bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001302 const State& s(mDrawingState);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001303 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001304 if (parent != nullptr && parent->isHiddenByPolicy()) {
1305 return true;
1306 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001307 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1308 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1309 if (zOrderRelativeOf != nullptr) {
1310 if (zOrderRelativeOf->isHiddenByPolicy()) {
1311 return true;
1312 }
1313 }
1314 }
Garfield Tan2c1782c2022-02-16 15:25:05 -08001315 if (CC_UNLIKELY(!isTransformValid())) {
1316 ALOGW("Hide layer %s because it has invalid transformation.", getDebugName());
1317 return true;
1318 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001319 return s.flags & layer_state_t::eLayerHidden;
1320}
1321
David Sodman41fdfc92017-11-06 16:09:56 -08001322uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001323 // TODO: should we do something special if mSecure is set?
1324 if (mProtectedByApp) {
1325 // need a hardware-protected path to external video sink
1326 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001327 }
Riley Andrews03414a12014-07-01 14:22:59 -07001328 if (mPotentialCursor) {
1329 usage |= GraphicBuffer::USAGE_CURSOR;
1330 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001331 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001332 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001333}
1334
Vishnu Nair71fcf912022-10-18 09:14:20 -07001335void Layer::skipReportingTransformHint() {
1336 mSkipReportingTransformHint = true;
1337}
1338
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001339void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1340 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1341 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001342 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001343
Vishnu Nair6213bd92020-05-08 17:42:25 -07001344 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001345}
1346
Mathias Agopian13127d82013-03-05 17:47:11 -08001347// ----------------------------------------------------------------------------
1348// debugging
1349// ----------------------------------------------------------------------------
1350
Marissa Wall61c58622018-07-18 10:12:20 -07001351// TODO(marissaw): add new layer state info to layer debugging
Huihong Luo05539a12022-02-23 10:29:40 -08001352gui::LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001353 using namespace std::string_literals;
1354
Huihong Luo05539a12022-02-23 10:29:40 -08001355 gui::LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001356 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001357 info.mName = getName();
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001358 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001359 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001360 info.mType = getType();
Lloyd Piquea2468662019-03-07 21:31:06 -08001361
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001362 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001363 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001364 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001365 info.mX = ds.transform.tx();
1366 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001367 info.mZ = ds.z;
chaviw25714502021-02-11 10:01:08 -08001368 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001369 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001370 info.mFlags = ds.flags;
1371 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001372 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001373 info.mMatrix[0][0] = ds.transform[0][0];
1374 info.mMatrix[0][1] = ds.transform[0][1];
1375 info.mMatrix[1][0] = ds.transform[1][0];
1376 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001377 {
chaviwd62d3062019-09-04 14:48:02 -07001378 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001379 if (buffer != 0) {
1380 info.mActiveBufferWidth = buffer->getWidth();
1381 info.mActiveBufferHeight = buffer->getHeight();
1382 info.mActiveBufferStride = buffer->getStride();
1383 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001384 } else {
1385 info.mActiveBufferWidth = 0;
1386 info.mActiveBufferHeight = 0;
1387 info.mActiveBufferStride = 0;
1388 info.mActiveBufferFormat = 0;
1389 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001390 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001391 info.mNumQueuedFrames = getQueuedFrameCount();
Kalle Raitaa099a242017-01-11 11:17:29 -08001392 info.mIsOpaque = isOpaque(ds);
1393 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001394 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001395 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001396}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001397
Yiwei Zhang5434a782018-12-05 18:06:32 -08001398void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001399 result.append(kDumpTableRowLength, '-');
1400 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001401 result.append(" Layer name\n");
1402 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001403 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001404 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001405 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001406 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001407 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001408 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1409 result.append(kDumpTableRowLength, '-');
1410 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001411}
1412
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001413void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1414 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001415 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001416 return;
1417 }
1418
Yiwei Zhang5434a782018-12-05 18:06:32 -08001419 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001420 if (mName.length() > 77) {
1421 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001422 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001423 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001424 shortened.append(mName, mName.length() - 36);
1425 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001426 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001427 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001428 }
1429
Yiwei Zhang5434a782018-12-05 18:06:32 -08001430 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001431
Alec Mourib416efd2018-09-06 21:01:59 +00001432 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001433 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001434
Chia-I Wu1e043612018-03-01 09:45:09 -08001435 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001436 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001437 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001438 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001439 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001440 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001441 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001442 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1443 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001444 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001445 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001446 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001447 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001448 const auto frameRate = getFrameRateForLayerTree();
1449 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1450 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001451 ftl::enum_string(frameRate.type).c_str(),
1452 ftl::enum_string(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001453 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001454 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001455 }
Dan Stozae22aec72016-08-01 13:20:59 -07001456
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001457 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1458 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1459
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001460 result.append(kDumpTableRowLength, '-');
1461 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001462}
Dan Stozae22aec72016-08-01 13:20:59 -07001463
Yiwei Zhang5434a782018-12-05 18:06:32 -08001464void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001465 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001466}
1467
Svetoslavd85084b2014-03-20 10:28:31 -07001468void Layer::clearFrameStats() {
1469 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001470}
1471
Jamie Gennis6547ff42013-07-16 20:12:42 -07001472void Layer::logFrameStats() {
1473 mFrameTracker.logAndResetStats(mName);
1474}
1475
Svetoslavd85084b2014-03-20 10:28:31 -07001476void Layer::getFrameStats(FrameStats* outStats) const {
1477 mFrameTracker.getStats(outStats);
1478}
1479
Vishnu Nair0f085c62019-08-30 08:49:12 -07001480void Layer::dumpCallingUidPid(std::string& result) const {
Vishnu Naircb8be502022-10-12 19:03:23 +00001481 StringAppendF(&result, "Layer %s (%s) ownerPid:%d ownerUid:%d\n", getName().c_str(), getType(),
1482 mOwnerPid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001483}
1484
Brian Anderson5ea5e592016-12-01 16:54:33 -08001485void Layer::onDisconnect() {
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001486 const int32_t layerId = getSequence();
1487 mFlinger->mTimeStats->onDestroy(layerId);
1488 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001489}
1490
Chia-I Wu98f1c102017-05-30 14:54:08 -07001491size_t Layer::getChildrenCount() const {
1492 size_t count = 0;
1493 for (const sp<Layer>& child : mCurrentChildren) {
1494 count += 1 + child->getChildrenCount();
1495 }
1496 return count;
1497}
1498
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001499void Layer::setGameModeForTree(GameMode gameMode) {
1500 const auto& currentState = getDrawingState();
Huihong Luod3d8f8e2022-03-08 14:48:46 -08001501 if (currentState.metadata.has(gui::METADATA_GAME_MODE)) {
1502 gameMode =
1503 static_cast<GameMode>(currentState.metadata.getInt32(gui::METADATA_GAME_MODE, 0));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001504 }
1505 setGameMode(gameMode);
1506 for (const sp<Layer>& child : mCurrentChildren) {
1507 child->setGameModeForTree(gameMode);
1508 }
1509}
1510
Robert Carr1f0a16a2016-10-24 16:27:39 -07001511void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001512 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001513 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001514
Robert Carr1f0a16a2016-10-24 16:27:39 -07001515 mCurrentChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07001516 layer->setParent(sp<Layer>::fromExisting(this));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001517 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001518 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001519}
1520
1521ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001522 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001523 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001524
Robert Carr1323c952019-01-28 18:13:27 -08001525 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001526 const auto removeResult = mCurrentChildren.remove(layer);
1527
1528 updateTreeHasFrameRateVote();
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001529 layer->setGameModeForTree(GameMode::Unsupported);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001530 layer->updateTreeHasFrameRateVote();
1531
1532 return removeResult;
1533}
1534
Robert Carr15eae092018-03-23 13:43:53 -07001535void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001536 for (const sp<Layer>& child : mDrawingChildren) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001537 child->mDrawingParent = newParent;
xinying1f4200e62022-04-26 14:41:30 +08001538 const float parentShadowRadius =
1539 newParent->canDrawShadows() ? 0.f : newParent->mEffectiveShadowRadius;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001540 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
xinying1f4200e62022-04-26 14:41:30 +08001541 parentShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001542 }
1543}
1544
chaviwf1961f72017-09-18 16:41:07 -07001545bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001546 sp<Layer> newParent;
1547 if (newParentHandle != nullptr) {
Vishnu Nairf9096652021-07-20 18:49:42 -07001548 newParent = fromHandle(newParentHandle).promote();
Robert Carr54cf5b12019-01-25 14:02:28 -08001549 if (newParent == nullptr) {
1550 ALOGE("Unable to promote Layer handle");
1551 return false;
1552 }
1553 if (newParent == this) {
1554 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1555 return false;
1556 }
1557 }
1558
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001559 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001560 if (parent != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001561 parent->removeChild(sp<Layer>::fromExisting(this));
chaviw06178942017-07-27 10:25:59 -07001562 }
1563
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001564 if (newParentHandle != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001565 newParent->addChild(sp<Layer>::fromExisting(this));
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001566 if (!newParent->isRemovedFromCurrentState()) {
1567 addToCurrentState();
1568 } else {
1569 onRemovedFromCurrentState();
1570 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001571 } else {
1572 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001573 }
1574
chaviw06178942017-07-27 10:25:59 -07001575 return true;
1576}
1577
Peiyong Lind3788632018-09-18 16:01:31 -07001578bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001579 static const mat4 identityMatrix = mat4();
1580
Robert Carr6a160312021-05-17 12:08:20 -07001581 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001582 return false;
1583 }
Robert Carr6a160312021-05-17 12:08:20 -07001584 ++mDrawingState.sequence;
1585 mDrawingState.colorTransform = matrix;
1586 mDrawingState.hasColorTransform = matrix != identityMatrix;
1587 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001588 setTransactionFlags(eTransactionNeeded);
1589 return true;
1590}
1591
chaviwf66724d2018-11-28 16:35:21 -08001592mat4 Layer::getColorTransform() const {
1593 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001594 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001595 colorTransform = parent->getColorTransform() * colorTransform;
1596 }
1597 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001598}
1599
1600bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001601 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001602 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001603 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1604 }
1605 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001606}
1607
Chia-I Wu11481472018-05-04 10:43:19 -07001608bool Layer::isLegacyDataSpace() const {
1609 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001610 return !(getDataSpace() &
1611 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1612 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001613}
1614
Robert Carr1f0a16a2016-10-24 16:27:39 -07001615void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001616 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001617}
1618
Robert Carr6a160312021-05-17 12:08:20 -07001619int32_t Layer::getZ(LayerVector::StateSet) const {
1620 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001621}
1622
Robert Carr1c5481e2019-07-01 14:42:27 -07001623bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001624 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001625 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001626 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001627}
1628
David Sodman41fdfc92017-11-06 16:09:56 -08001629__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001630 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001631 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1632 "makeTraversalList received invalid stateSet");
1633 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1634 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001635 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001636
Robert Carr29abff82017-12-04 13:51:20 -08001637 if (state.zOrderRelatives.size() == 0) {
1638 *outSkipRelativeZUsers = true;
1639 return children;
1640 }
1641
chaviwfd462612018-05-31 16:11:27 -07001642 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001643 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001644 sp<Layer> strongRelative = weakRelative.promote();
1645 if (strongRelative != nullptr) {
1646 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001647 }
1648 }
1649
Dan Stoza412903f2017-04-27 13:42:17 -07001650 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001651 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001652 continue;
1653 }
Robert Carrdb66e622017-04-10 16:55:57 -07001654 traverse.add(child);
1655 }
1656
1657 return traverse;
1658}
1659
Robert Carr1f0a16a2016-10-24 16:27:39 -07001660/**
Robert Carrdb66e622017-04-10 16:55:57 -07001661 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001662 */
Dan Stoza412903f2017-04-27 13:42:17 -07001663void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001664 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1665 // produce a new list for traversing, including our relatives, and not including our children
1666 // who are relatives of another surface. In the case that there are no relative Z,
1667 // makeTraversalList returns our children directly to avoid significant overhead.
1668 // However in this case we need to take the responsibility for filtering children which
1669 // are relatives of another surface here.
1670 bool skipRelativeZUsers = false;
1671 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001672
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001674 for (; i < list.size(); i++) {
1675 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001676 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1677 continue;
1678 }
1679
chaviw301b1d82019-11-06 13:15:09 -08001680 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001681 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001682 }
Dan Stoza412903f2017-04-27 13:42:17 -07001683 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001684 }
Robert Carr29abff82017-12-04 13:51:20 -08001685
Dan Stoza412903f2017-04-27 13:42:17 -07001686 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001687 for (; i < list.size(); i++) {
1688 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001689
Robert Carr29abff82017-12-04 13:51:20 -08001690 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1691 continue;
1692 }
Dan Stoza412903f2017-04-27 13:42:17 -07001693 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001694 }
1695}
1696
1697/**
Robert Carrdb66e622017-04-10 16:55:57 -07001698 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001699 */
Dan Stoza412903f2017-04-27 13:42:17 -07001700void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1701 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001702 // See traverseInZOrder for documentation.
1703 bool skipRelativeZUsers = false;
1704 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001705
Robert Carr1f0a16a2016-10-24 16:27:39 -07001706 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001707 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001708 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001709
1710 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1711 continue;
1712 }
1713
chaviw301b1d82019-11-06 13:15:09 -08001714 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001715 break;
1716 }
Dan Stoza412903f2017-04-27 13:42:17 -07001717 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001718 }
Dan Stoza412903f2017-04-27 13:42:17 -07001719 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001720 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001721 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001722
1723 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1724 continue;
1725 }
1726
Dan Stoza412903f2017-04-27 13:42:17 -07001727 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001728 }
1729}
1730
Edgar Arriaga844fa672020-01-16 14:21:42 -08001731void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1732 visitor(this);
1733 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001734 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001735 for (const sp<Layer>& child : children) {
1736 child->traverse(state, visitor);
1737 }
1738}
1739
chaviw4b129c22018-04-09 16:19:43 -07001740LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1741 const std::vector<Layer*>& layersInTree) {
1742 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1743 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001744 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1745 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001746 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001747
chaviwfd462612018-05-31 16:11:27 -07001748 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001749 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1750 sp<Layer> strongRelative = weakRelative.promote();
1751 // Only add relative layers that are also descendents of the top most parent of the tree.
1752 // If a relative layer is not a descendent, then it should be ignored.
1753 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1754 traverse.add(strongRelative);
1755 }
1756 }
1757
1758 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001759 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001760 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1761 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1762 // the child here since it won't be added later as a relative.
1763 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1764 childState.zOrderRelativeOf.promote().get())) {
1765 continue;
1766 }
1767 traverse.add(child);
1768 }
1769
1770 return traverse;
1771}
1772
1773void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1774 LayerVector::StateSet stateSet,
1775 const LayerVector::Visitor& visitor) {
1776 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001777
1778 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001779 for (; i < list.size(); i++) {
1780 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001781 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001782 break;
1783 }
chaviw4b129c22018-04-09 16:19:43 -07001784 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001785 }
chaviw4b129c22018-04-09 16:19:43 -07001786
chaviwa76b2712017-09-20 12:02:26 -07001787 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001788 for (; i < list.size(); i++) {
1789 const auto& relative = list[i];
1790 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001791 }
1792}
1793
chaviw4b129c22018-04-09 16:19:43 -07001794std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1795 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1796 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1797
1798 std::vector<Layer*> layersInTree = {this};
1799 for (size_t i = 0; i < children.size(); i++) {
1800 const auto& child = children[i];
1801 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1802 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1803 }
1804
1805 return layersInTree;
1806}
1807
1808void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1809 const LayerVector::Visitor& visitor) {
1810 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1811 std::sort(layersInTree.begin(), layersInTree.end());
1812 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1813}
1814
Peiyong Linefefaac2018-08-17 12:27:51 -07001815ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001816 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001817}
1818
Garfield Tan2c1782c2022-02-16 15:25:05 -08001819bool Layer::isTransformValid() const {
1820 float transformDet = getTransform().det();
1821 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
1822}
1823
chaviw13fdc492017-06-27 12:40:18 -07001824half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001825 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001826
chaviw13fdc492017-06-27 12:40:18 -07001827 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1828 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001829}
Robert Carr6452f122017-03-21 10:41:29 -07001830
Vishnu Nair6213bd92020-05-08 17:42:25 -07001831ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001832 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001833 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1834 return fixedTransformHint;
1835 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001836 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001837 if (!p) return fixedTransformHint;
1838 return p->getFixedTransformHint();
1839}
1840
chaviw13fdc492017-06-27 12:40:18 -07001841half4 Layer::getColor() const {
1842 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001843 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001844}
Robert Carr6452f122017-03-21 10:41:29 -07001845
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001846int32_t Layer::getBackgroundBlurRadius() const {
Vishnu Nair29810f72022-07-01 16:38:41 +00001847 if (getDrawingState().backgroundBlurRadius == 0) {
1848 return 0;
1849 }
Galia Peychevada7de0e2020-12-03 17:24:35 +01001850
Vishnu Nair29810f72022-07-01 16:38:41 +00001851 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001852 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1853 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001854}
1855
Galia Peychevae0acf382021-04-12 21:22:34 +02001856const std::vector<BlurRegion> Layer::getBlurRegions() const {
1857 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001858 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001859 for (auto& region : regionsCopy) {
1860 region.alpha = region.alpha * layerAlpha;
1861 }
1862 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001863}
1864
Vishnu Naire14c6b32022-08-06 04:20:15 +00001865RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001866 // Get parent settings
1867 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001868 const auto& parent = mDrawingParent.promote();
1869 if (parent != nullptr) {
1870 parentSettings = parent->getRoundedCornerState();
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001871 if (parentSettings.hasRoundedCorners()) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001872 ui::Transform t = getActiveTransform(getDrawingState());
1873 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001874 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001875 parentSettings.radius.x *= t.getScaleX();
1876 parentSettings.radius.y *= t.getScaleY();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001877 }
1878 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001879
1880 // Get layer settings
1881 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001882 const vec2 radius(getDrawingState().cornerRadius, getDrawingState().cornerRadius);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001883 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001884 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001885
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001886 if (layerSettingsValid && parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001887 // If the parent and the layer have rounded corner settings, use the parent settings if the
1888 // parent crop is entirely inside the layer crop.
1889 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1890 if (parentSettings.cropRect.left > layerCropRect.left &&
1891 parentSettings.cropRect.top > layerCropRect.top &&
1892 parentSettings.cropRect.right < layerCropRect.right &&
1893 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1894 return parentSettings;
1895 } else {
1896 return layerSettings;
1897 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001898 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001899 return layerSettings;
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001900 } else if (parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001901 return parentSettings;
1902 }
1903 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001904}
1905
Robert Carr88b85e12022-03-21 15:47:35 -07001906bool Layer::findInHierarchy(const sp<Layer>& l) {
1907 if (l == this) {
1908 return true;
1909 }
1910 for (auto& child : mDrawingChildren) {
1911 if (child->findInHierarchy(l)) {
1912 return true;
1913 }
1914 }
1915 return false;
1916}
1917
Robert Carr1f0a16a2016-10-24 16:27:39 -07001918void Layer::commitChildList() {
1919 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1920 const auto& child = mCurrentChildren[i];
1921 child->commitChildList();
1922 }
1923 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001924 mDrawingParent = mCurrentParent;
Robert Carr88b85e12022-03-21 15:47:35 -07001925 if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) {
1926 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1927 if (zOrderRelativeOf == nullptr) return;
1928 if (findInHierarchy(zOrderRelativeOf)) {
1929 ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(),
1930 zOrderRelativeOf->mName.c_str());
1931 ALOGE("Severing rel Z loop, potentially dangerous");
1932 mDrawingState.isRelativeOf = false;
Ady Abrahamd11bade2022-08-01 16:18:03 -07001933 zOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carr88b85e12022-03-21 15:47:35 -07001934 }
1935 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001936}
1937
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001938
chaviw3277faf2021-05-19 16:45:23 -05001939void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001940 mDrawingState.inputInfo = info;
Vishnu Nairf9096652021-07-20 18:49:42 -07001941 mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07001942 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +00001943 mFlinger->mUpdateInputInfo = true;
Robert Carr720e5062018-07-30 17:45:14 -07001944 setTransactionFlags(eTransactionNeeded);
1945}
1946
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001947LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) {
chaviw08f3cb22020-01-13 13:17:21 -08001948 LayerProto* layerProto = layersProto.add_layers();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001949 writeToProtoDrawingState(layerProto);
chaviw08f3cb22020-01-13 13:17:21 -08001950 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1951
Vishnu Nair00b90132021-11-05 14:03:40 -07001952 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001953 ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread.
1954
Vishnu Nair60db8c02020-04-02 11:55:16 -07001955 // Only populate for the primary display.
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001956 if (const auto display = mFlinger->getDefaultDisplayDeviceLocked()) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05001957 const auto compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07001958 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001959 LayerProtoHelper::writeToProto(getVisibleRegion(display.get()),
1960 [&]() { return layerProto->mutable_visible_region(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07001961 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08001962 }
1963
chaviw08f3cb22020-01-13 13:17:21 -08001964 for (const sp<Layer>& layer : mDrawingChildren) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001965 layer->writeToProto(layersProto, traceFlags);
chaviw08f3cb22020-01-13 13:17:21 -08001966 }
chaviw6d89e2d2020-01-14 14:42:01 -08001967
1968 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08001969}
1970
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001971void Layer::writeToProtoDrawingState(LayerProto* layerInfo) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001972 const ui::Transform transform = getTransform();
Vishnu Naird37343b2022-01-12 16:18:56 -08001973 auto buffer = getExternalTexture();
Vishnu Nair00b90132021-11-05 14:03:40 -07001974 if (buffer != nullptr) {
Vishnu Naird37343b2022-01-12 16:18:56 -08001975 LayerProtoHelper::writeToProto(*buffer,
Vishnu Nair00b90132021-11-05 14:03:40 -07001976 [&]() { return layerInfo->mutable_active_buffer(); });
1977 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
1978 layerInfo->mutable_buffer_transform());
1979 }
1980 layerInfo->set_invalidate(contentDirty);
1981 layerInfo->set_is_protected(isProtected());
1982 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
1983 layerInfo->set_queued_frames(getQueuedFrameCount());
Vishnu Nair00b90132021-11-05 14:03:40 -07001984 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nair00b90132021-11-05 14:03:40 -07001985 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001986 layerInfo->set_corner_radius(
1987 (getRoundedCornerState().radius.x + getRoundedCornerState().radius.y) / 2.0);
Vishnu Nair00b90132021-11-05 14:03:40 -07001988 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
1989 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
1990 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
1991 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1992 [&]() { return layerInfo->mutable_position(); });
1993 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair00b90132021-11-05 14:03:40 -07001994 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1995 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07001996
Vishnu Nair00b90132021-11-05 14:03:40 -07001997 if (hasColorTransform()) {
1998 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07001999 }
2000
Vishnu Nair60db8c02020-04-02 11:55:16 -07002001 LayerProtoHelper::writeToProto(mSourceBounds,
2002 [&]() { return layerInfo->mutable_source_bounds(); });
2003 LayerProtoHelper::writeToProto(mScreenBounds,
2004 [&]() { return layerInfo->mutable_screen_bounds(); });
2005 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2006 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2007 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002008}
2009
2010void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002011 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002012 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2013 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002014 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002015
chaviw766c9c52021-02-10 17:36:47 -08002016 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002017
Vishnu Nair00b90132021-11-05 14:03:40 -07002018 layerInfo->set_id(sequence);
2019 layerInfo->set_name(getName().c_str());
2020 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002021
Vishnu Nair00b90132021-11-05 14:03:40 -07002022 for (const auto& child : children) {
2023 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002024 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002025
Vishnu Nair00b90132021-11-05 14:03:40 -07002026 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2027 sp<Layer> strongRelative = weakRelative.promote();
2028 if (strongRelative != nullptr) {
2029 layerInfo->add_relatives(strongRelative->sequence);
2030 }
2031 }
2032
Vishnu Nairea04b6f2022-08-19 21:28:17 +00002033 LayerProtoHelper::writeToProto(state.transparentRegionHint,
Vishnu Nair00b90132021-11-05 14:03:40 -07002034 [&]() { return layerInfo->mutable_transparent_region(); });
2035
2036 layerInfo->set_layer_stack(getLayerStack().id);
2037 layerInfo->set_z(state.z);
2038
2039 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2040 return layerInfo->mutable_requested_position();
2041 });
2042
Vishnu Nair00b90132021-11-05 14:03:40 -07002043 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2044
2045 layerInfo->set_is_opaque(isOpaque(state));
2046
2047 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2048 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2049 LayerProtoHelper::writeToProto(state.color,
2050 [&]() { return layerInfo->mutable_requested_color(); });
2051 layerInfo->set_flags(state.flags);
2052
2053 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2054 layerInfo->mutable_requested_transform());
2055
2056 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2057 if (parent != nullptr) {
2058 layerInfo->set_parent(parent->sequence);
2059 } else {
2060 layerInfo->set_parent(-1);
2061 }
2062
2063 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2064 if (zOrderRelativeOf != nullptr) {
2065 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2066 } else {
2067 layerInfo->set_z_order_relative_of(-1);
2068 }
2069
2070 layerInfo->set_is_relative_of(state.isRelativeOf);
2071
2072 layerInfo->set_owner_uid(mOwnerUid);
2073
Arthur Hung4cf2d8c2022-04-07 14:52:00 +00002074 if ((traceFlags & LayerTracing::TRACE_INPUT) && needsInputInfo()) {
chaviw3277faf2021-05-19 16:45:23 -05002075 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002076 if (useDrawing) {
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002077 info = fillInputInfo(
2078 InputDisplayArgs{.transform = &kIdentityTransform, .isSecure = true});
chaviw4c34a092020-07-08 11:30:06 -07002079 } else {
2080 info = state.inputInfo;
2081 }
2082
2083 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002084 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002085 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002086
Vishnu Nair00b90132021-11-05 14:03:40 -07002087 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002088 auto protoMap = layerInfo->mutable_metadata();
2089 for (const auto& entry : state.metadata.mMap) {
2090 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2091 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002092 }
Vishnu Naird2aaab12022-02-10 14:49:09 -08002093
2094 LayerProtoHelper::writeToProto(state.destinationFrame,
2095 [&]() { return layerInfo->mutable_destination_frame(); });
chaviw1d044282017-09-27 12:19:28 -07002096}
2097
Robert Carr2e102c92018-10-23 12:11:15 -07002098bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002099 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002100}
2101
Prabir Pradhan33da9462022-06-14 14:55:57 +00002102// Applies the given transform to the region, while protecting against overflows caused by any
2103// offsets. If applying the offset in the transform to any of the Rects in the region would result
2104// in an overflow, they are not added to the output Region.
2105static Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
2106 const std::string& debugWindowName) {
2107 // Round the translation using the same rounding strategy used by ui::Transform.
2108 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
2109 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
2110
2111 ui::Transform transformWithoutOffset = t;
2112 transformWithoutOffset.set(0.f, 0.f);
2113
2114 const Region transformed = transformWithoutOffset.transform(r);
2115
2116 // Apply the translation to each of the Rects in the region while discarding any that overflow.
2117 Region ret;
2118 for (const auto& rect : transformed) {
2119 Rect newRect;
2120 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
2121 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
2122 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
2123 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
2124 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
2125 debugWindowName.c_str());
2126 continue;
2127 }
2128 ret.orSelf(newRect);
2129 }
2130 return ret;
2131}
2132
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002133void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& screenToDisplay) {
2134 Rect tmpBounds = getInputBounds();
2135 if (!tmpBounds.isValid()) {
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08002136 info.touchableRegion.clear();
2137 // A layer could have invalid input bounds and still expect to receive touch input if it has
2138 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
2139 // correctly to determine the coordinate space for input events. Use an empty rect so that
2140 // the layer will receive input in its own layer space.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002141 tmpBounds = Rect::EMPTY_RECT;
chaviw7e72caf2020-12-02 16:50:43 -08002142 }
2143
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002144 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
2145 // frame and transform used for the layer, which determines the bounds and the coordinate space
2146 // within which the layer will receive input.
2147 //
2148 // The coordinate space within which each of the bounds are specified is explicitly documented
2149 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
2150 // Transform converts one coordinate space to another, which is apparent in its naming. For
2151 // example, "layerToDisplay" transforms layer space to display space.
2152 //
2153 // Coordinate space definitions:
2154 // - display: The display device's coordinate space. Correlates to pixels on the display.
2155 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
2156 // - layer: The coordinate space of this layer.
2157 // - input: The coordinate space in which this layer will receive input events. This could be
2158 // different than layer space if a surfaceInset is used, which changes the origin
2159 // of the input space.
2160 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
chaviw7e72caf2020-12-02 16:50:43 -08002161
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002162 // Clamp surface inset to the input bounds.
2163 const auto surfaceInset = static_cast<float>(info.surfaceInset);
2164 const float xSurfaceInset =
2165 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
2166 const float ySurfaceInset =
2167 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
chaviw1ff3d1e2020-07-01 15:53:47 -07002168
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002169 // Apply the insets to the input bounds.
2170 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
2171 inputBoundsInLayer.top + ySurfaceInset,
2172 inputBoundsInLayer.right - xSurfaceInset,
2173 inputBoundsInLayer.bottom - ySurfaceInset);
Ady Abraham282f1d72019-07-24 18:05:56 -07002174
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002175 // Crop the input bounds to ensure it is within the parent's bounds.
2176 const FloatRect croppedInsetBoundsInLayer = mBounds.intersect(insetBoundsInLayer);
Ady Abraham282f1d72019-07-24 18:05:56 -07002177
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002178 const ui::Transform layerToScreen = getInputTransform();
2179 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
Vishnu Nair8033a492018-12-05 07:27:23 -08002180
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002181 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
2182 info.frameLeft = roundedFrameInDisplay.left;
2183 info.frameTop = roundedFrameInDisplay.top;
2184 info.frameRight = roundedFrameInDisplay.right;
2185 info.frameBottom = roundedFrameInDisplay.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -07002186
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002187 ui::Transform inputToLayer;
2188 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
2189 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
chaviw39cfa2e2020-11-04 14:19:02 -08002190
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002191 // InputDispatcher expects a display-to-input transform.
2192 info.transform = inputToDisplay.inverse();
2193
2194 // The touchable region is specified in the input coordinate space. Change it to display space.
Prabir Pradhan33da9462022-06-14 14:55:57 +00002195 info.touchableRegion =
2196 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, mName);
chaviw7e72caf2020-12-02 16:50:43 -08002197}
2198
chaviw3277faf2021-05-19 16:45:23 -05002199void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002200 sp<Layer> p = sp<Layer>::fromExisting(this);
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002201 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002202 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002203 }
2204 if (p != nullptr) {
2205 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2206 }
2207}
2208
Vishnu Naira066d902021-09-13 18:40:17 -07002209gui::DropInputMode Layer::getDropInputMode() const {
2210 gui::DropInputMode mode = mDrawingState.dropInputMode;
2211 if (mode == gui::DropInputMode::ALL) {
2212 return mode;
2213 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002214 sp<Layer> parent = mDrawingParent.promote();
2215 if (parent) {
2216 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002217 if (parentMode != gui::DropInputMode::NONE) {
2218 return parentMode;
2219 }
2220 }
2221 return mode;
2222}
2223
2224void Layer::handleDropInputMode(gui::WindowInfo& info) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002225 if (mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Vishnu Naira066d902021-09-13 18:40:17 -07002226 return;
2227 }
2228
2229 // Check if we need to drop input unconditionally
2230 gui::DropInputMode dropInputMode = getDropInputMode();
2231 if (dropInputMode == gui::DropInputMode::ALL) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002232 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002233 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2234 return;
2235 }
2236
2237 // Check if we need to check if the window is obscured by parent
2238 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2239 return;
2240 }
2241
2242 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002243 sp<Layer> parent = mDrawingParent.promote();
2244 if (parent && parent->getAlpha() != 1.0_hf) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002245 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002246 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2247 static_cast<float>(getAlpha()));
2248 }
2249
2250 // Check if the parent has cropped the buffer
2251 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2252 if (!bufferSize.isValid()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002253 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002254 return;
2255 }
2256
2257 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2258 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2259 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2260 // match then the layer has not been cropped by its parents.
2261 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2262 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2263
2264 if (croppedByParent) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002265 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002266 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2267 getDebugName());
2268 } else {
2269 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2270 // input if the window is obscured. This check should be done in surfaceflinger but the
2271 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2272 // drop input events if the window is obscured.
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002273 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002274 }
2275}
2276
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002277WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) {
chaviw7e72caf2020-12-02 16:50:43 -08002278 if (!hasInputInfo()) {
2279 mDrawingState.inputInfo.name = getName();
2280 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2281 mDrawingState.inputInfo.ownerPid = mOwnerPid;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002282 mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002283 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002284 }
2285
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002286 const ui::Transform& displayTransform =
2287 displayArgs.transform != nullptr ? *displayArgs.transform : kIdentityTransform;
2288
chaviw3277faf2021-05-19 16:45:23 -05002289 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002290 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002291 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002292
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002293 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002294
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002295 if (displayArgs.transform == nullptr) {
2296 // Do not let the window receive touches if it is not associated with a valid display
2297 // transform. We still allow the window to receive keys and prevent ANRs.
2298 info.inputConfig |= WindowInfo::InputConfig::NOT_TOUCHABLE;
2299 }
2300
Robert Carr5dc426e2020-06-10 14:29:14 -07002301 // For compatibility reasons we let layers which can receive input
2302 // receive input before they have actually submitted a buffer. Because
2303 // of this we use canReceiveInput instead of isVisible to check the
2304 // policy-visibility, ignoring the buffer state. However for layers with
2305 // hasInputInfo()==false we can use the real visibility state.
2306 // We are just using these layers for occlusion detection in
2307 // InputDispatcher, and obviously if they aren't visible they can't occlude
2308 // anything.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002309 const bool visible = hasInputInfo() ? canReceiveInput() : isVisible();
2310 info.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
2311
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002312 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002313 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002314 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002315
Vishnu Nair16a938f2021-09-24 07:14:54 -07002316 // If the window will be blacked out on a display because the display does not have the secure
2317 // flag and the layer has the secure flag set, then drop input.
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002318 if (!displayArgs.isSecure && isSecure()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002319 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Nair16a938f2021-09-24 07:14:54 -07002320 }
2321
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002322 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2323 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002324 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2325 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002326 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002327 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002328 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002329 }
2330
Winson Chunga30f7c92021-06-29 15:42:56 -07002331 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2332 // if it was set by WM for a known system overlay
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002333 if (isTrustedOverlay()) {
2334 info.inputConfig |= WindowInfo::InputConfig::TRUSTED_OVERLAY;
2335 }
Winson Chunga30f7c92021-06-29 15:42:56 -07002336
chaviwaf87b3e2019-10-01 16:59:28 -07002337 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2338 // touches from going outside the cloned area.
2339 if (isClone()) {
chaviw8577ea82022-06-01 16:32:26 -05002340 info.inputConfig |= WindowInfo::InputConfig::CLONE;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002341 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2342 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002343 info.touchableRegion = info.touchableRegion.intersect(rect);
2344 }
2345 }
2346
Robert Carr720e5062018-07-30 17:45:14 -07002347 return info;
2348}
2349
chaviwaf87b3e2019-10-01 16:59:28 -07002350sp<Layer> Layer::getClonedRoot() {
2351 if (mClonedChild != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002352 return sp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002353 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002354 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002355 return nullptr;
2356 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002357 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002358}
2359
Robert Carredd13602020-04-13 17:24:34 -07002360bool Layer::hasInputInfo() const {
Linus Tufvessona1858822022-03-04 09:32:07 +00002361 return mDrawingState.inputInfo.token != nullptr ||
2362 mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Robert Carr720e5062018-07-30 17:45:14 -07002363}
2364
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002365compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002366 const DisplayDevice* display) const {
2367 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002368 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002369}
2370
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002371Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2372 const auto outputLayer = findOutputLayerForDisplay(display);
2373 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002374}
2375
chaviwb4c6e582019-08-16 14:35:07 -07002376void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002377 cloneDrawingState(clonedFrom.get());
chaviwb4c6e582019-08-16 14:35:07 -07002378 mClonedFrom = clonedFrom;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002379 mPremultipliedAlpha = clonedFrom->mPremultipliedAlpha;
2380 mPotentialCursor = clonedFrom->mPotentialCursor;
2381 mProtectedByApp = clonedFrom->mProtectedByApp;
2382 updateCloneBufferInfo();
2383}
2384
2385void Layer::updateCloneBufferInfo() {
2386 if (!isClone() || !isClonedFromAlive()) {
2387 return;
2388 }
2389
2390 sp<Layer> clonedFrom = getClonedFrom();
2391 mBufferInfo = clonedFrom->mBufferInfo;
2392 mSidebandStream = clonedFrom->mSidebandStream;
2393 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
2394 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
2395 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
2396
2397 // After buffer info is updated, the drawingState from the real layer needs to be copied into
2398 // the cloned. This is because some properties of drawingState can change when latchBuffer is
2399 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
2400 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
2401 // the cloned drawingState again.
2402 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
2403 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
2404 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
2405 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
2406
2407 cloneDrawingState(clonedFrom.get());
2408
2409 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
2410 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
2411 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
2412 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -07002413}
chaviw74b03172019-08-19 11:09:03 -07002414
2415void Layer::updateMirrorInfo() {
2416 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2417 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2418 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2419 // that case, we want to delete the reference to the clone since we want it to get
2420 // destroyed. The root, this layer, will still be around since the client can continue
2421 // to hold a reference, but no cloned layers will be displayed.
2422 mClonedChild = nullptr;
2423 return;
2424 }
2425
2426 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2427 // If the real layer exists and is in current state, add the clone as a child of the root.
2428 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2429 // copied to drawingState for the root layer. So the clonedChild is always removed from
2430 // drawingState and then needs to be added back each traversal.
2431 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2432 addChildToDrawing(mClonedChild);
2433 }
2434
2435 mClonedChild->updateClonedDrawingState(clonedLayersMap);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002436 mClonedChild->updateClonedChildren(sp<Layer>::fromExisting(this), clonedLayersMap);
chaviw74b03172019-08-19 11:09:03 -07002437 mClonedChild->updateClonedRelatives(clonedLayersMap);
2438}
2439
2440void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2441 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2442 // to the clone. If the real layer is no longer alive, continue traversing the children
2443 // since we may be able to pull out other children that are still alive.
2444 if (isClonedFromAlive()) {
2445 sp<Layer> clonedFrom = getClonedFrom();
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002446 cloneDrawingState(clonedFrom.get());
Ady Abrahamd11bade2022-08-01 16:18:03 -07002447 clonedLayersMap.emplace(clonedFrom, sp<Layer>::fromExisting(this));
chaviw74b03172019-08-19 11:09:03 -07002448 }
2449
2450 // The clone layer may have children in drawingState since they may have been created and
2451 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2452 // that already exist, since we can just re-use them.
2453 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2454 // not updated in the regular traversal. They are skipped since the root will lose the
2455 // reference to them when it copies its currentChildren to drawing.
2456 for (sp<Layer>& child : mDrawingChildren) {
2457 child->updateClonedDrawingState(clonedLayersMap);
2458 }
2459}
2460
2461void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2462 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2463 mDrawingChildren.clear();
2464
2465 if (!isClonedFromAlive()) {
2466 return;
2467 }
2468
2469 sp<Layer> clonedFrom = getClonedFrom();
2470 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2471 if (child == mirrorRoot) {
2472 // This is to avoid cyclical mirroring.
2473 continue;
2474 }
2475 sp<Layer> clonedChild = clonedLayersMap[child];
2476 if (clonedChild == nullptr) {
2477 clonedChild = child->createClone();
2478 clonedLayersMap[child] = clonedChild;
2479 }
2480 addChildToDrawing(clonedChild);
2481 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2482 }
2483}
2484
chaviwaf87b3e2019-10-01 16:59:28 -07002485void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2486 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2487 if (cropLayer != nullptr) {
2488 if (clonedLayersMap.count(cropLayer) == 0) {
2489 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2490 // self as crop layer to avoid going outside bounds.
Ady Abrahamd11bade2022-08-01 16:18:03 -07002491 mDrawingState.touchableRegionCrop = wp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002492 } else {
2493 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2494 mDrawingState.touchableRegionCrop = clonedCropLayer;
2495 }
2496 }
2497 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2498 // WM or the client.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002499 mDrawingState.inputInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, false);
chaviwaf87b3e2019-10-01 16:59:28 -07002500}
2501
2502void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002503 mDrawingState.zOrderRelativeOf = wp<Layer>();
chaviw74b03172019-08-19 11:09:03 -07002504 mDrawingState.zOrderRelatives.clear();
2505
2506 if (!isClonedFromAlive()) {
2507 return;
2508 }
2509
chaviwaf87b3e2019-10-01 16:59:28 -07002510 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002511 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002512 const sp<Layer>& relative = relativeWeak.promote();
2513 if (clonedLayersMap.count(relative) > 0) {
2514 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002515 mDrawingState.zOrderRelatives.add(clonedRelative);
2516 }
2517 }
2518
2519 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2520 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2521 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2522 // still traverse the children, but the layer with the missing relativeOf will not be shown
2523 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002524 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2525 if (clonedLayersMap.count(relativeOf) > 0) {
2526 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002527 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2528 }
2529
chaviwaf87b3e2019-10-01 16:59:28 -07002530 updateClonedInputInfo(clonedLayersMap);
2531
chaviw74b03172019-08-19 11:09:03 -07002532 for (sp<Layer>& child : mDrawingChildren) {
2533 child->updateClonedRelatives(clonedLayersMap);
2534 }
2535}
2536
2537void Layer::addChildToDrawing(const sp<Layer>& layer) {
2538 mDrawingChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002539 layer->mDrawingParent = sp<Layer>::fromExisting(this);
chaviw74b03172019-08-19 11:09:03 -07002540}
2541
Steven Thomas62a4cf82020-01-31 12:04:03 -08002542Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2543 switch (compatibility) {
2544 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2545 return FrameRateCompatibility::Default;
2546 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2547 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002548 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2549 return FrameRateCompatibility::Exact;
Andy Labrada096227e2022-06-15 16:58:11 +00002550 case ANATIVEWINDOW_FRAME_RATE_MIN:
2551 return FrameRateCompatibility::Min;
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002552 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
2553 return FrameRateCompatibility::NoVote;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002554 default:
2555 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2556 return FrameRateCompatibility::Default;
2557 }
2558}
2559
Marin Shalamanovc5986772021-03-16 16:09:49 +01002560scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2561 switch (strategy) {
2562 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2563 return Seamlessness::OnlySeamless;
2564 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2565 return Seamlessness::SeamedAndSeamless;
2566 default:
2567 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2568 return Seamlessness::Default;
2569 }
2570}
2571
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002572bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002573 const State& s(mDrawingState);
2574 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2575 return true;
2576 }
2577
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002578 sp<Layer> parent = mDrawingParent.promote();
2579 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002580}
2581
Robert Carr6a0382d2021-07-01 15:57:17 -07002582void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2583 mClonedChild = clonedChild;
2584 mHadClonedChild = true;
2585 mFlinger->mNumClones++;
2586}
2587
Vishnu Nairf9096652021-07-20 18:49:42 -07002588const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle");
2589
2590wp<Layer> Layer::fromHandle(const sp<IBinder>& handleBinder) {
2591 if (handleBinder == nullptr) {
2592 return nullptr;
2593 }
2594
2595 BBinder* b = handleBinder->localBinder();
2596 if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) {
2597 return nullptr;
2598 }
2599
2600 // We can safely cast this binder since its local and we verified its interface descriptor.
Ady Abrahamd11bade2022-08-01 16:18:03 -07002601 sp<Handle> handle = sp<Handle>::cast(handleBinder);
Vishnu Nairf9096652021-07-20 18:49:42 -07002602 return handle->owner;
2603}
2604
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002605bool Layer::setDropInputMode(gui::DropInputMode mode) {
2606 if (mDrawingState.dropInputMode == mode) {
2607 return false;
2608 }
2609 mDrawingState.dropInputMode = mode;
2610 return true;
2611}
2612
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002613void Layer::cloneDrawingState(const Layer* from) {
2614 mDrawingState = from->mDrawingState;
2615 // Skip callback info since they are not applicable for cloned layers.
2616 mDrawingState.releaseBufferListener = nullptr;
2617 mDrawingState.callbackHandles = {};
2618}
2619
Patrick Williamsbb25f802022-08-30 23:02:34 +00002620void Layer::callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
2621 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
2622 const sp<Fence>& releaseFence,
2623 uint32_t currentMaxAcquiredBufferCount) {
2624 if (!listener) {
2625 return;
2626 }
2627 ATRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, getDebugName(), framenumber);
2628 listener->onReleaseBuffer({buffer->getId(), framenumber},
2629 releaseFence ? releaseFence : Fence::NO_FENCE,
2630 currentMaxAcquiredBufferCount);
2631}
2632
2633void Layer::onLayerDisplayed(ftl::SharedFuture<FenceResult> futureFenceResult) {
2634 // If we are displayed on multiple displays in a single composition cycle then we would
2635 // need to do careful tracking to enable the use of the mLastClientCompositionFence.
2636 // For example we can only use it if all the displays are client comp, and we need
2637 // to merge all the client comp fences. We could do this, but for now we just
2638 // disable the optimization when a layer is composed on multiple displays.
2639 if (mClearClientCompositionFenceOnLayerDisplayed) {
2640 mLastClientCompositionFence = nullptr;
2641 } else {
2642 mClearClientCompositionFenceOnLayerDisplayed = true;
2643 }
2644
2645 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
2646 // buffer that was presented on this layer. The first transaction that came in this frame that
2647 // replaced the previous buffer on this layer needs this release fence, because the fence will
2648 // let the client know when that previous buffer is removed from the screen.
2649 //
2650 // Every other transaction on this layer does not need a release fence because no other
2651 // Transactions that were set on this layer this frame are going to have their preceding buffer
2652 // removed from the display this frame.
2653 //
2654 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
2655 // buffer so it doesn't need a previous release fence because the layer still needs the previous
2656 // buffer. The second transaction contains a buffer so it needs a previous release fence because
2657 // the previous buffer will be released this frame. The third transaction also contains a
2658 // buffer. It replaces the buffer in the second transaction. The buffer in the second
2659 // transaction will now no longer be presented so it is released immediately and the third
2660 // transaction doesn't need a previous release fence.
2661 sp<CallbackHandle> ch;
2662 for (auto& handle : mDrawingState.callbackHandles) {
2663 if (handle->releasePreviousBuffer &&
2664 mDrawingState.releaseBufferEndpoint == handle->listener) {
2665 ch = handle;
2666 break;
2667 }
2668 }
2669
2670 // Prevent tracing the same release multiple times.
2671 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
2672 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
2673 }
2674
2675 if (ch != nullptr) {
2676 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2677 ch->previousReleaseFences.emplace_back(std::move(futureFenceResult));
2678 ch->name = mName;
2679 }
2680}
2681
2682void Layer::onSurfaceFrameCreated(
2683 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Vishnu Nair22491b82022-10-18 14:59:14 -07002684 if (!hasBufferOrSidebandStreamInDrawing()) {
2685 return;
2686 }
2687
Patrick Williamsbb25f802022-08-30 23:02:34 +00002688 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
2689 // Too many SurfaceFrames pending classification. The front of the deque is probably not
2690 // tracked by FrameTimeline and will never be presented. This will only result in a memory
2691 // leak.
2692 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
2693 mName.c_str());
2694 std::string miniDump = mPendingJankClassifications.front()->miniDump();
2695 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
2696 mPendingJankClassifications.pop_front();
2697 }
2698 mPendingJankClassifications.emplace_back(surfaceFrame);
2699}
2700
2701void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
2702 for (const auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair71fcf912022-10-18 09:14:20 -07002703 handle->transformHint = mSkipReportingTransformHint
2704 ? std::nullopt
2705 : std::make_optional<uint32_t>(mTransformHint);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002706 handle->dequeueReadyTime = dequeueReadyTime;
2707 handle->currentMaxAcquiredBufferCount =
2708 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
2709 ATRACE_FORMAT_INSTANT("releasePendingBuffer %s - %" PRIu64, getDebugName(),
2710 handle->previousReleaseCallbackId.framenumber);
2711 }
2712
2713 for (auto& handle : mDrawingState.callbackHandles) {
2714 if (handle->releasePreviousBuffer &&
2715 mDrawingState.releaseBufferEndpoint == handle->listener) {
2716 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2717 break;
2718 }
2719 }
2720
2721 std::vector<JankData> jankData;
2722 jankData.reserve(mPendingJankClassifications.size());
2723 while (!mPendingJankClassifications.empty() &&
2724 mPendingJankClassifications.front()->getJankType()) {
2725 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
2726 mPendingJankClassifications.front();
2727 mPendingJankClassifications.pop_front();
2728 jankData.emplace_back(
2729 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
2730 }
2731
2732 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(mDrawingState.callbackHandles,
2733 jankData);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002734 mDrawingState.callbackHandles = {};
2735}
2736
2737bool Layer::willPresentCurrentTransaction() const {
2738 // Returns true if the most recent Transaction applied to CurrentState will be presented.
2739 return (getSidebandStreamChanged() || getAutoRefresh() ||
2740 (mDrawingState.modified &&
2741 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
2742}
2743
2744bool Layer::setTransform(uint32_t transform) {
2745 if (mDrawingState.bufferTransform == transform) return false;
2746 mDrawingState.bufferTransform = transform;
2747 mDrawingState.modified = true;
2748 setTransactionFlags(eTransactionNeeded);
2749 return true;
2750}
2751
2752bool Layer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
2753 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
2754 mDrawingState.sequence++;
2755 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
2756 mDrawingState.modified = true;
2757 setTransactionFlags(eTransactionNeeded);
2758 return true;
2759}
2760
2761bool Layer::setBufferCrop(const Rect& bufferCrop) {
2762 if (mDrawingState.bufferCrop == bufferCrop) return false;
2763
2764 mDrawingState.sequence++;
2765 mDrawingState.bufferCrop = bufferCrop;
2766
2767 mDrawingState.modified = true;
2768 setTransactionFlags(eTransactionNeeded);
2769 return true;
2770}
2771
2772bool Layer::setDestinationFrame(const Rect& destinationFrame) {
2773 if (mDrawingState.destinationFrame == destinationFrame) return false;
2774
2775 mDrawingState.sequence++;
2776 mDrawingState.destinationFrame = destinationFrame;
2777
2778 mDrawingState.modified = true;
2779 setTransactionFlags(eTransactionNeeded);
2780 return true;
2781}
2782
2783// Translate destination frame into scale and position. If a destination frame is not set, use the
2784// provided scale and position
2785bool Layer::updateGeometry() {
2786 if ((mDrawingState.flags & layer_state_t::eIgnoreDestinationFrame) ||
2787 mDrawingState.destinationFrame.isEmpty()) {
2788 // If destination frame is not set, use the requested transform set via
2789 // Layer::setPosition and Layer::setMatrix.
2790 return assignTransform(&mDrawingState.transform, mRequestedTransform);
2791 }
2792
2793 Rect destRect = mDrawingState.destinationFrame;
2794 int32_t destW = destRect.width();
2795 int32_t destH = destRect.height();
2796 if (destRect.left < 0) {
2797 destRect.left = 0;
2798 destRect.right = destW;
2799 }
2800 if (destRect.top < 0) {
2801 destRect.top = 0;
2802 destRect.bottom = destH;
2803 }
2804
2805 if (!mDrawingState.buffer) {
2806 ui::Transform t;
2807 t.set(destRect.left, destRect.top);
2808 return assignTransform(&mDrawingState.transform, t);
2809 }
2810
2811 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
2812 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
2813 // Undo any transformations on the buffer.
2814 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
2815 std::swap(bufferWidth, bufferHeight);
2816 }
2817 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
2818 if (mDrawingState.transformToDisplayInverse) {
2819 if (invTransform & ui::Transform::ROT_90) {
2820 std::swap(bufferWidth, bufferHeight);
2821 }
2822 }
2823
2824 float sx = destW / static_cast<float>(bufferWidth);
2825 float sy = destH / static_cast<float>(bufferHeight);
2826 ui::Transform t;
2827 t.set(sx, 0, 0, sy);
2828 t.set(destRect.left, destRect.top);
2829 return assignTransform(&mDrawingState.transform, t);
2830}
2831
2832bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
2833 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
2834 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
2835 return false;
2836 }
2837
2838 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
2839
2840 mDrawingState.sequence++;
2841 mDrawingState.modified = true;
2842 setTransactionFlags(eTransactionNeeded);
2843
2844 return true;
2845}
2846
2847bool Layer::setPosition(float x, float y) {
2848 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
2849 return false;
2850 }
2851
2852 mRequestedTransform.set(x, y);
2853
2854 mDrawingState.sequence++;
2855 mDrawingState.modified = true;
2856 setTransactionFlags(eTransactionNeeded);
2857
2858 return true;
2859}
2860
2861bool Layer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
2862 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
2863 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
2864 const FrameTimelineInfo& info) {
Chavi Weingarten009619f2022-09-15 20:27:25 +00002865 ATRACE_FORMAT("setBuffer %s - hasBuffer=%s", getDebugName(), (buffer ? "true" : "false"));
Patrick Williamsbb25f802022-08-30 23:02:34 +00002866 if (!buffer) {
2867 return false;
2868 }
2869
2870 const bool frameNumberChanged =
2871 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
2872 const uint64_t frameNumber =
2873 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
Chavi Weingarten009619f2022-09-15 20:27:25 +00002874 ATRACE_FORMAT_INSTANT("setBuffer %s - %" PRIu64, getDebugName(), frameNumber);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002875
2876 if (mDrawingState.buffer) {
2877 mReleasePreviousBuffer = true;
2878 if (!mBufferInfo.mBuffer ||
2879 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
2880 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
2881 // If mDrawingState has a buffer, and we are about to update again
2882 // before swapping to drawing state, then the first buffer will be
2883 // dropped and we should decrement the pending buffer count and
2884 // call any release buffer callbacks if set.
2885 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2886 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2887 mDrawingState.acquireFence,
2888 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2889 mOwnerUid));
2890 decrementPendingBufferCount();
2891 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
2892 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
2893 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
2894 mDrawingState.bufferSurfaceFrameTX.reset();
2895 }
2896 } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) {
2897 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2898 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2899 mLastClientCompositionFence,
2900 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2901 mOwnerUid));
2902 mLastClientCompositionFence = nullptr;
2903 }
2904 }
2905
2906 mDrawingState.frameNumber = frameNumber;
2907 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
2908 mDrawingState.buffer = std::move(buffer);
2909 mDrawingState.clientCacheId = bufferData.cachedBuffer;
2910
2911 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
2912 ? bufferData.acquireFence
2913 : Fence::NO_FENCE;
2914 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
2915 if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
2916 // We latched this buffer unsiganled, so we need to pass the acquire fence
2917 // on the callback instead of just the acquire time, since it's unknown at
2918 // this point.
2919 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence;
2920 } else {
2921 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime();
2922 }
2923
2924 mDrawingState.modified = true;
2925 setTransactionFlags(eTransactionNeeded);
2926
2927 const int32_t layerId = getSequence();
2928 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
2929 mOwnerUid, postTime, getGameMode());
2930 mDrawingState.desiredPresentTime = desiredPresentTime;
2931 mDrawingState.isAutoTimestamp = isAutoTimestamp;
2932
2933 const nsecs_t presentTime = [&] {
2934 if (!isAutoTimestamp) return desiredPresentTime;
2935
2936 const auto prediction =
2937 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
2938 if (prediction.has_value()) return prediction->presentTime;
2939
2940 return static_cast<nsecs_t>(0);
2941 }();
2942
2943 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
2944 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
2945
2946 setFrameTimelineVsyncForBufferTransaction(info, postTime);
2947
2948 if (dequeueTime && *dequeueTime != 0) {
2949 const uint64_t bufferId = mDrawingState.buffer->getId();
2950 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
2951 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
2952 FrameTracer::FrameEvent::DEQUEUE);
2953 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
2954 FrameTracer::FrameEvent::QUEUE);
2955 }
2956
2957 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
2958 return true;
2959}
2960
2961bool Layer::setDataspace(ui::Dataspace dataspace) {
2962 mDrawingState.dataspaceRequested = true;
2963 if (mDrawingState.dataspace == dataspace) return false;
2964 mDrawingState.dataspace = dataspace;
2965 mDrawingState.modified = true;
2966 setTransactionFlags(eTransactionNeeded);
2967 return true;
2968}
2969
2970bool Layer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
2971 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
2972 mDrawingState.hdrMetadata = hdrMetadata;
2973 mDrawingState.modified = true;
2974 setTransactionFlags(eTransactionNeeded);
2975 return true;
2976}
2977
2978bool Layer::setSurfaceDamageRegion(const Region& surfaceDamage) {
2979 mDrawingState.surfaceDamageRegion = surfaceDamage;
2980 mDrawingState.modified = true;
2981 setTransactionFlags(eTransactionNeeded);
2982 return true;
2983}
2984
2985bool Layer::setApi(int32_t api) {
2986 if (mDrawingState.api == api) return false;
2987 mDrawingState.api = api;
2988 mDrawingState.modified = true;
2989 setTransactionFlags(eTransactionNeeded);
2990 return true;
2991}
2992
2993bool Layer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
2994 if (mDrawingState.sidebandStream == sidebandStream) return false;
2995
2996 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
2997 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
2998 } else if (sidebandStream != nullptr) {
2999 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
3000 }
3001
3002 mDrawingState.sidebandStream = sidebandStream;
3003 mDrawingState.modified = true;
3004 setTransactionFlags(eTransactionNeeded);
3005 if (!mSidebandStreamChanged.exchange(true)) {
3006 // mSidebandStreamChanged was false
3007 mFlinger->onLayerUpdate();
3008 }
3009 return true;
3010}
3011
3012bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
3013 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
3014 if (handles.empty()) {
3015 mReleasePreviousBuffer = false;
3016 return false;
3017 }
3018
3019 const bool willPresent = willPresentCurrentTransaction();
3020
3021 for (const auto& handle : handles) {
3022 // If this transaction set a buffer on this layer, release its previous buffer
3023 handle->releasePreviousBuffer = mReleasePreviousBuffer;
3024
3025 // If this layer will be presented in this frame
3026 if (willPresent) {
3027 // If this transaction set an acquire fence on this layer, set its acquire time
3028 handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence;
3029 handle->frameNumber = mDrawingState.frameNumber;
3030
3031 // Store so latched time and release fence can be set
3032 mDrawingState.callbackHandles.push_back(handle);
3033
3034 } else { // If this layer will NOT need to be relatched and presented this frame
3035 // Notify the transaction completed thread this handle is done
3036 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
3037 }
3038 }
3039
3040 mReleasePreviousBuffer = false;
3041 mCallbackHandleAcquireTimeOrFence = -1;
3042
3043 return willPresent;
3044}
3045
3046Rect Layer::getBufferSize(const State& /*s*/) const {
3047 // for buffer state layers we use the display frame size as the buffer size.
3048
3049 if (mBufferInfo.mBuffer == nullptr) {
3050 return Rect::INVALID_RECT;
3051 }
3052
3053 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3054 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3055
3056 // Undo any transformations on the buffer and return the result.
3057 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
3058 std::swap(bufWidth, bufHeight);
3059 }
3060
3061 if (getTransformToDisplayInverse()) {
3062 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3063 if (invTransform & ui::Transform::ROT_90) {
3064 std::swap(bufWidth, bufHeight);
3065 }
3066 }
3067
3068 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
3069}
3070
3071FloatRect Layer::computeSourceBounds(const FloatRect& parentBounds) const {
3072 if (mBufferInfo.mBuffer == nullptr) {
3073 return parentBounds;
3074 }
3075
3076 return getBufferSize(getDrawingState()).toFloatRect();
3077}
3078
3079bool Layer::fenceHasSignaled() const {
3080 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
3081 return true;
3082 }
3083
3084 const bool fenceSignaled =
3085 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
3086 if (!fenceSignaled) {
3087 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
3088 TimeStats::LatchSkipReason::LateAcquire);
3089 }
3090
3091 return fenceSignaled;
3092}
3093
Vishnu Naire14c6b32022-08-06 04:20:15 +00003094bool Layer::onPreComposition(nsecs_t refreshStartTime) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003095 for (const auto& handle : mDrawingState.callbackHandles) {
3096 handle->refreshStartTime = refreshStartTime;
3097 }
3098 return hasReadyFrame();
3099}
3100
3101void Layer::setAutoRefresh(bool autoRefresh) {
3102 mDrawingState.autoRefresh = autoRefresh;
3103}
3104
3105bool Layer::latchSidebandStream(bool& recomputeVisibleRegions) {
3106 // 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 +00003107 auto* snapshot = editLayerSnapshot();
3108 snapshot->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003109
3110 if (mSidebandStreamChanged.exchange(false)) {
3111 const State& s(getDrawingState());
3112 // mSidebandStreamChanged was true
3113 mSidebandStream = s.sidebandStream;
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003114 snapshot->sidebandStream = mSidebandStream;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003115 if (mSidebandStream != nullptr) {
3116 setTransactionFlags(eTransactionNeeded);
3117 mFlinger->setTransactionFlags(eTraversalNeeded);
3118 }
3119 recomputeVisibleRegions = true;
3120
3121 return true;
3122 }
3123 return false;
3124}
3125
3126bool Layer::hasFrameUpdate() const {
3127 const State& c(getDrawingState());
3128 return (mDrawingStateModified || mDrawingState.modified) &&
3129 (c.buffer != nullptr || c.bgColorLayer != nullptr);
3130}
3131
3132void Layer::updateTexImage(nsecs_t latchTime) {
3133 const State& s(getDrawingState());
3134
3135 if (!s.buffer) {
3136 if (s.bgColorLayer) {
3137 for (auto& handle : mDrawingState.callbackHandles) {
3138 handle->latchTime = latchTime;
3139 }
3140 }
3141 return;
3142 }
3143
3144 for (auto& handle : mDrawingState.callbackHandles) {
3145 if (handle->frameNumber == mDrawingState.frameNumber) {
3146 handle->latchTime = latchTime;
3147 }
3148 }
3149
3150 const int32_t layerId = getSequence();
3151 const uint64_t bufferId = mDrawingState.buffer->getId();
3152 const uint64_t frameNumber = mDrawingState.frameNumber;
3153 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3154 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
3155 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
3156
3157 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
3158 FrameTracer::FrameEvent::ACQUIRE_FENCE);
3159 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
3160 FrameTracer::FrameEvent::LATCH);
3161
3162 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
3163 if (bufferSurfaceFrame != nullptr &&
3164 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
3165 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
3166 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
3167 // are processing the next state.
3168 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
3169 mDrawingState.acquireFenceTime->getSignalTime(),
3170 latchTime);
3171 mDrawingState.bufferSurfaceFrameTX.reset();
3172 }
3173
3174 std::deque<sp<CallbackHandle>> remainingHandles;
3175 mFlinger->getTransactionCallbackInvoker()
3176 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
3177 mDrawingState.callbackHandles = remainingHandles;
3178
3179 mDrawingStateModified = false;
3180}
3181
3182void Layer::gatherBufferInfo() {
3183 if (!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
3184 decrementPendingBufferCount();
3185 }
3186
3187 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
3188 mBufferInfo.mBuffer = mDrawingState.buffer;
3189 mBufferInfo.mFence = mDrawingState.acquireFence;
3190 mBufferInfo.mFrameNumber = mDrawingState.frameNumber;
3191 mBufferInfo.mPixelFormat =
3192 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat();
3193 mBufferInfo.mFrameLatencyNeeded = true;
3194 mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime;
3195 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3196 mBufferInfo.mFence = mDrawingState.acquireFence;
3197 mBufferInfo.mTransform = mDrawingState.bufferTransform;
3198 auto lastDataspace = mBufferInfo.mDataspace;
3199 mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace);
3200 if (lastDataspace != mBufferInfo.mDataspace) {
3201 mFlinger->mSomeDataspaceChanged = true;
3202 }
3203 mBufferInfo.mCrop = computeBufferCrop(mDrawingState);
3204 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
3205 mBufferInfo.mSurfaceDamage = mDrawingState.surfaceDamageRegion;
3206 mBufferInfo.mHdrMetadata = mDrawingState.hdrMetadata;
3207 mBufferInfo.mApi = mDrawingState.api;
3208 mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse;
3209 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(mDrawingState.clientCacheId);
3210}
3211
3212Rect Layer::computeBufferCrop(const State& s) {
3213 if (s.buffer && !s.bufferCrop.isEmpty()) {
3214 Rect bufferCrop;
3215 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
3216 return bufferCrop;
3217 } else if (s.buffer) {
3218 return s.buffer->getBounds();
3219 } else {
3220 return s.bufferCrop;
3221 }
3222}
3223
3224sp<Layer> Layer::createClone() {
3225 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
3226 args.textureName = mTextureName;
Patrick Williams83f36b22022-09-14 17:57:35 +00003227 sp<Layer> layer = mFlinger->getFactory().createBufferStateLayer(args);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003228 layer->mHwcSlotGenerator = mHwcSlotGenerator;
3229 layer->setInitialValuesForClone(sp<Layer>::fromExisting(this));
3230 return layer;
3231}
3232
3233bool Layer::bufferNeedsFiltering() const {
3234 const State& s(getDrawingState());
3235 if (!s.buffer) {
3236 return false;
3237 }
3238
3239 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
3240 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
3241
3242 // Undo any transformations on the buffer and return the result.
3243 if (s.bufferTransform & ui::Transform::ROT_90) {
3244 std::swap(bufferWidth, bufferHeight);
3245 }
3246
3247 if (s.transformToDisplayInverse) {
3248 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3249 if (invTransform & ui::Transform::ROT_90) {
3250 std::swap(bufferWidth, bufferHeight);
3251 }
3252 }
3253
3254 const Rect layerSize{getBounds()};
3255 int32_t layerWidth = layerSize.getWidth();
3256 int32_t layerHeight = layerSize.getHeight();
3257
3258 // Align the layer orientation with the buffer before comparism
3259 if (mTransformHint & ui::Transform::ROT_90) {
3260 std::swap(layerWidth, layerHeight);
3261 }
3262
3263 return layerWidth != bufferWidth || layerHeight != bufferHeight;
3264}
3265
3266void Layer::decrementPendingBufferCount() {
3267 int32_t pendingBuffers = --mPendingBufferTransactions;
3268 tracePendingBufferCount(pendingBuffers);
3269}
3270
3271void Layer::tracePendingBufferCount(int32_t pendingBuffers) {
3272 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
3273}
3274
3275/*
3276 * We don't want to send the layer's transform to input, but rather the
3277 * parent's transform. This is because Layer's transform is
3278 * information about how the buffer is placed on screen. The parent's
3279 * transform makes more sense to send since it's information about how the
3280 * layer is placed on screen. This transform is used by input to determine
3281 * how to go from screen space back to window space.
3282 */
3283ui::Transform Layer::getInputTransform() const {
3284 if (!hasBufferOrSidebandStream()) {
3285 return getTransform();
3286 }
3287 sp<Layer> parent = mDrawingParent.promote();
3288 if (parent == nullptr) {
3289 return ui::Transform();
3290 }
3291
3292 return parent->getTransform();
3293}
3294
3295/**
3296 * Similar to getInputTransform, we need to update the bounds to include the transform.
3297 * This is because bounds don't include the buffer transform, where the input assumes
3298 * that's already included.
3299 */
3300Rect Layer::getInputBounds() const {
3301 if (!hasBufferOrSidebandStream()) {
3302 return getCroppedBufferSize(getDrawingState());
3303 }
3304
3305 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
3306 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
3307 return bufferBounds;
3308 }
3309 return mDrawingState.transform.transform(bufferBounds);
3310}
3311
3312bool Layer::simpleBufferUpdate(const layer_state_t& s) const {
3313 const uint64_t requiredFlags = layer_state_t::eBufferChanged;
3314
3315 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
3316 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
3317 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
3318 layer_state_t::eLayerStackChanged | layer_state_t::eAutoRefreshChanged |
3319 layer_state_t::eReparent;
3320
3321 const uint64_t allowedFlags = layer_state_t::eHasListenerCallbacksChanged |
3322 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFrameRateChanged |
3323 layer_state_t::eSurfaceDamageRegionChanged | layer_state_t::eApiChanged |
3324 layer_state_t::eMetadataChanged | layer_state_t::eDropInputModeChanged |
3325 layer_state_t::eInputInfoChanged;
3326
3327 if ((s.what & requiredFlags) != requiredFlags) {
3328 ALOGV("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
3329 (s.what | requiredFlags) & ~s.what);
3330 return false;
3331 }
3332
3333 if (s.what & deniedFlags) {
3334 ALOGV("%s: false [has denied flags 0x%" PRIx64 "]", __func__, s.what & deniedFlags);
3335 return false;
3336 }
3337
3338 if (s.what & allowedFlags) {
3339 ALOGV("%s: [has allowed flags 0x%" PRIx64 "]", __func__, s.what & allowedFlags);
3340 }
3341
3342 if (s.what & layer_state_t::ePositionChanged) {
3343 if (mRequestedTransform.tx() != s.x || mRequestedTransform.ty() != s.y) {
3344 ALOGV("%s: false [ePositionChanged changed]", __func__);
3345 return false;
3346 }
3347 }
3348
3349 if (s.what & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003350 if (mDrawingState.color.a != s.color.a) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003351 ALOGV("%s: false [eAlphaChanged changed]", __func__);
3352 return false;
3353 }
3354 }
3355
3356 if (s.what & layer_state_t::eColorTransformChanged) {
3357 if (mDrawingState.colorTransform != s.colorTransform) {
3358 ALOGV("%s: false [eColorTransformChanged changed]", __func__);
3359 return false;
3360 }
3361 }
3362
3363 if (s.what & layer_state_t::eBackgroundColorChanged) {
3364 if (mDrawingState.bgColorLayer || s.bgColorAlpha != 0) {
3365 ALOGV("%s: false [eBackgroundColorChanged changed]", __func__);
3366 return false;
3367 }
3368 }
3369
3370 if (s.what & layer_state_t::eMatrixChanged) {
3371 if (mRequestedTransform.dsdx() != s.matrix.dsdx ||
3372 mRequestedTransform.dtdy() != s.matrix.dtdy ||
3373 mRequestedTransform.dtdx() != s.matrix.dtdx ||
3374 mRequestedTransform.dsdy() != s.matrix.dsdy) {
3375 ALOGV("%s: false [eMatrixChanged changed]", __func__);
3376 return false;
3377 }
3378 }
3379
3380 if (s.what & layer_state_t::eCornerRadiusChanged) {
3381 if (mDrawingState.cornerRadius != s.cornerRadius) {
3382 ALOGV("%s: false [eCornerRadiusChanged changed]", __func__);
3383 return false;
3384 }
3385 }
3386
3387 if (s.what & layer_state_t::eBackgroundBlurRadiusChanged) {
3388 if (mDrawingState.backgroundBlurRadius != static_cast<int>(s.backgroundBlurRadius)) {
3389 ALOGV("%s: false [eBackgroundBlurRadiusChanged changed]", __func__);
3390 return false;
3391 }
3392 }
3393
Vishnu Nairbbceb462022-10-10 04:52:13 +00003394 if (s.what & layer_state_t::eBufferTransformChanged) {
3395 if (mDrawingState.bufferTransform != s.bufferTransform) {
3396 ALOGV("%s: false [eBufferTransformChanged changed]", __func__);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003397 return false;
3398 }
3399 }
3400
3401 if (s.what & layer_state_t::eTransformToDisplayInverseChanged) {
3402 if (mDrawingState.transformToDisplayInverse != s.transformToDisplayInverse) {
3403 ALOGV("%s: false [eTransformToDisplayInverseChanged changed]", __func__);
3404 return false;
3405 }
3406 }
3407
3408 if (s.what & layer_state_t::eCropChanged) {
3409 if (mDrawingState.crop != s.crop) {
3410 ALOGV("%s: false [eCropChanged changed]", __func__);
3411 return false;
3412 }
3413 }
3414
3415 if (s.what & layer_state_t::eDataspaceChanged) {
3416 if (mDrawingState.dataspace != s.dataspace) {
3417 ALOGV("%s: false [eDataspaceChanged changed]", __func__);
3418 return false;
3419 }
3420 }
3421
3422 if (s.what & layer_state_t::eHdrMetadataChanged) {
3423 if (mDrawingState.hdrMetadata != s.hdrMetadata) {
3424 ALOGV("%s: false [eHdrMetadataChanged changed]", __func__);
3425 return false;
3426 }
3427 }
3428
3429 if (s.what & layer_state_t::eSidebandStreamChanged) {
3430 if (mDrawingState.sidebandStream != s.sidebandStream) {
3431 ALOGV("%s: false [eSidebandStreamChanged changed]", __func__);
3432 return false;
3433 }
3434 }
3435
3436 if (s.what & layer_state_t::eColorSpaceAgnosticChanged) {
3437 if (mDrawingState.colorSpaceAgnostic != s.colorSpaceAgnostic) {
3438 ALOGV("%s: false [eColorSpaceAgnosticChanged changed]", __func__);
3439 return false;
3440 }
3441 }
3442
3443 if (s.what & layer_state_t::eShadowRadiusChanged) {
3444 if (mDrawingState.shadowRadius != s.shadowRadius) {
3445 ALOGV("%s: false [eShadowRadiusChanged changed]", __func__);
3446 return false;
3447 }
3448 }
3449
3450 if (s.what & layer_state_t::eFixedTransformHintChanged) {
3451 if (mDrawingState.fixedTransformHint != s.fixedTransformHint) {
3452 ALOGV("%s: false [eFixedTransformHintChanged changed]", __func__);
3453 return false;
3454 }
3455 }
3456
3457 if (s.what & layer_state_t::eTrustedOverlayChanged) {
3458 if (mDrawingState.isTrustedOverlay != s.isTrustedOverlay) {
3459 ALOGV("%s: false [eTrustedOverlayChanged changed]", __func__);
3460 return false;
3461 }
3462 }
3463
3464 if (s.what & layer_state_t::eStretchChanged) {
3465 StretchEffect temp = s.stretchEffect;
3466 temp.sanitize();
3467 if (mDrawingState.stretchEffect != temp) {
3468 ALOGV("%s: false [eStretchChanged changed]", __func__);
3469 return false;
3470 }
3471 }
3472
3473 if (s.what & layer_state_t::eBufferCropChanged) {
3474 if (mDrawingState.bufferCrop != s.bufferCrop) {
3475 ALOGV("%s: false [eBufferCropChanged changed]", __func__);
3476 return false;
3477 }
3478 }
3479
3480 if (s.what & layer_state_t::eDestinationFrameChanged) {
3481 if (mDrawingState.destinationFrame != s.destinationFrame) {
3482 ALOGV("%s: false [eDestinationFrameChanged changed]", __func__);
3483 return false;
3484 }
3485 }
3486
3487 if (s.what & layer_state_t::eDimmingEnabledChanged) {
3488 if (mDrawingState.dimmingEnabled != s.dimmingEnabled) {
3489 ALOGV("%s: false [eDimmingEnabledChanged changed]", __func__);
3490 return false;
3491 }
3492 }
3493
3494 ALOGV("%s: true", __func__);
3495 return true;
3496}
3497
3498bool Layer::isHdrY410() const {
3499 // pixel format is HDR Y410 masquerading as RGBA_1010102
3500 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
3501 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
3502 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
3503}
3504
Vishnu Naire14c6b32022-08-06 04:20:15 +00003505sp<LayerFE> Layer::getCompositionEngineLayerFE() const {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003506 // 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 +00003507 return hasSomethingToDraw() ? mLayerFE : nullptr;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003508}
3509
Vishnu Naire14c6b32022-08-06 04:20:15 +00003510const LayerSnapshot* Layer::getLayerSnapshot() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003511 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003512}
3513
Vishnu Naire14c6b32022-08-06 04:20:15 +00003514LayerSnapshot* Layer::editLayerSnapshot() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003515 return mSnapshot.get();
3516}
Vishnu Naire14c6b32022-08-06 04:20:15 +00003517
Patrick Williamsbb25f802022-08-30 23:02:34 +00003518const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003519 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003520}
3521
3522void Layer::useSurfaceDamage() {
3523 if (mFlinger->mForceFullDamage) {
3524 surfaceDamageRegion = Region::INVALID_REGION;
3525 } else {
3526 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
3527 }
3528}
3529
3530void Layer::useEmptyDamage() {
3531 surfaceDamageRegion.clear();
3532}
3533
3534bool Layer::isOpaque(const Layer::State& s) const {
3535 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
3536 // layer's opaque flag.
3537 if (!hasSomethingToDraw()) {
3538 return false;
3539 }
3540
3541 // if the layer has the opaque flag, then we're always opaque
3542 if ((s.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque) {
3543 return true;
3544 }
3545
3546 // If the buffer has no alpha channel, then we are opaque
3547 if (hasBufferOrSidebandStream() && isOpaqueFormat(getPixelFormat())) {
3548 return true;
3549 }
3550
3551 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
3552 return fillsColor() && getAlpha() == 1.0_hf;
3553}
3554
3555bool Layer::canReceiveInput() const {
3556 return !isHiddenByPolicy() && (mBufferInfo.mBuffer == nullptr || getAlpha() > 0.0f);
3557}
3558
3559bool Layer::isVisible() const {
3560 if (!hasSomethingToDraw()) {
3561 return false;
3562 }
3563
3564 if (isHiddenByPolicy()) {
3565 return false;
3566 }
3567
3568 return getAlpha() > 0.0f || hasBlur();
3569}
3570
3571void Layer::onPostComposition(const DisplayDevice* display,
3572 const std::shared_ptr<FenceTime>& glDoneFence,
3573 const std::shared_ptr<FenceTime>& presentFence,
3574 const CompositorTiming& compositorTiming) {
3575 // mFrameLatencyNeeded is true when a new frame was latched for the
3576 // composition.
3577 if (!mBufferInfo.mFrameLatencyNeeded) return;
3578
3579 for (const auto& handle : mDrawingState.callbackHandles) {
3580 handle->gpuCompositionDoneFence = glDoneFence;
3581 handle->compositorTiming = compositorTiming;
3582 }
3583
3584 // Update mFrameTracker.
3585 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
3586 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
3587
3588 const int32_t layerId = getSequence();
3589 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
3590
3591 const auto outputLayer = findOutputLayerForDisplay(display);
3592 if (outputLayer && outputLayer->requiresClientComposition()) {
3593 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
3594 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3595 clientCompositionTimestamp,
3596 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
3597 // Update the SurfaceFrames in the drawing state
3598 if (mDrawingState.bufferSurfaceFrameTX) {
3599 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
3600 }
3601 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
3602 surfaceFrame->setGpuComposition();
3603 }
3604 }
3605
3606 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
3607 if (frameReadyFence->isValid()) {
3608 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
3609 } else {
3610 // There was no fence for this frame, so assume that it was ready
3611 // to be presented at the desired present time.
3612 mFrameTracker.setFrameReadyTime(desiredPresentTime);
3613 }
3614
3615 if (display) {
Dominik Laskowskif8734e02022-08-26 09:06:59 -07003616 const Fps refreshRate = display->refreshRateConfigs().getActiveModePtr()->getFps();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003617 const std::optional<Fps> renderRate =
3618 mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
3619
3620 const auto vote = frameRateToSetFrameRateVotePayload(mDrawingState.frameRate);
3621 const auto gameMode = getGameMode();
3622
3623 if (presentFence->isValid()) {
3624 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
3625 refreshRate, renderRate, vote, gameMode);
3626 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3627 presentFence,
3628 FrameTracer::FrameEvent::PRESENT_FENCE);
3629 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
3630 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
3631 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Dominik Laskowskidc2bb802022-09-28 16:02:59 -04003632 // The HWC doesn't support present fences, so use the present timestamp instead.
3633 const nsecs_t presentTimestamp =
3634 mFlinger->getHwComposer().getPresentTimestamp(*displayId);
3635
3636 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
3637 const nsecs_t vsyncPeriod = display->getVsyncPeriodFromHWC();
3638 const nsecs_t actualPresentTime = now - ((now - presentTimestamp) % vsyncPeriod);
3639
Patrick Williamsbb25f802022-08-30 23:02:34 +00003640 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
3641 refreshRate, renderRate, vote, gameMode);
3642 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(),
3643 mCurrentFrameNumber, actualPresentTime,
3644 FrameTracer::FrameEvent::PRESENT_FENCE);
3645 mFrameTracker.setActualPresentTime(actualPresentTime);
3646 }
3647 }
3648
3649 mFrameTracker.advanceFrame();
3650 mBufferInfo.mFrameLatencyNeeded = false;
3651}
3652
3653bool Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
3654 ATRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(),
3655 getDrawingState().frameNumber);
3656
3657 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
3658
3659 if (refreshRequired) {
3660 return refreshRequired;
3661 }
3662
3663 // If the head buffer's acquire fence hasn't signaled yet, return and
3664 // try again later
3665 if (!fenceHasSignaled()) {
3666 ATRACE_NAME("!fenceHasSignaled()");
3667 mFlinger->onLayerUpdate();
3668 return false;
3669 }
3670
3671 updateTexImage(latchTime);
3672 if (mDrawingState.buffer == nullptr) {
3673 return false;
3674 }
3675
3676 // Capture the old state of the layer for comparisons later
3677 BufferInfo oldBufferInfo = mBufferInfo;
3678 const bool oldOpacity = isOpaque(mDrawingState);
3679 mPreviousFrameNumber = mCurrentFrameNumber;
3680 mCurrentFrameNumber = mDrawingState.frameNumber;
3681 gatherBufferInfo();
3682
3683 if (oldBufferInfo.mBuffer == nullptr) {
3684 // the first time we receive a buffer, we need to trigger a
3685 // geometry invalidation.
3686 recomputeVisibleRegions = true;
3687 }
3688
3689 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
3690 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
3691 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
3692 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
3693 recomputeVisibleRegions = true;
3694 }
3695
3696 if (oldBufferInfo.mBuffer != nullptr) {
3697 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3698 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3699 if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
3700 bufHeight != oldBufferInfo.mBuffer->getHeight()) {
3701 recomputeVisibleRegions = true;
3702 }
3703 }
3704
3705 if (oldOpacity != isOpaque(mDrawingState)) {
3706 recomputeVisibleRegions = true;
3707 }
3708
3709 return true;
3710}
3711
3712bool Layer::hasReadyFrame() const {
3713 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
3714}
3715
3716bool Layer::isProtected() const {
3717 return (mBufferInfo.mBuffer != nullptr) &&
3718 (mBufferInfo.mBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
3719}
3720
3721// As documented in libhardware header, formats in the range
3722// 0x100 - 0x1FF are specific to the HAL implementation, and
3723// are known to have no alpha channel
3724// TODO: move definition for device-specific range into
3725// hardware.h, instead of using hard-coded values here.
3726#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
3727
3728bool Layer::isOpaqueFormat(PixelFormat format) {
3729 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
3730 return true;
3731 }
3732 switch (format) {
3733 case PIXEL_FORMAT_RGBA_8888:
3734 case PIXEL_FORMAT_BGRA_8888:
3735 case PIXEL_FORMAT_RGBA_FP16:
3736 case PIXEL_FORMAT_RGBA_1010102:
3737 case PIXEL_FORMAT_R_8:
3738 return false;
3739 }
3740 // in all other case, we have no blending (also for unknown formats)
3741 return true;
3742}
3743
3744bool Layer::needsFiltering(const DisplayDevice* display) const {
3745 if (!hasBufferOrSidebandStream()) {
3746 return false;
3747 }
3748 const auto outputLayer = findOutputLayerForDisplay(display);
3749 if (outputLayer == nullptr) {
3750 return false;
3751 }
3752
3753 // We need filtering if the sourceCrop rectangle size does not match the
3754 // displayframe rectangle size (not a 1:1 render)
3755 const auto& compositionState = outputLayer->getState();
3756 const auto displayFrame = compositionState.displayFrame;
3757 const auto sourceCrop = compositionState.sourceCrop;
3758 return sourceCrop.getHeight() != displayFrame.getHeight() ||
3759 sourceCrop.getWidth() != displayFrame.getWidth();
3760}
3761
3762bool Layer::needsFilteringForScreenshots(const DisplayDevice* display,
3763 const ui::Transform& inverseParentTransform) const {
3764 if (!hasBufferOrSidebandStream()) {
3765 return false;
3766 }
3767 const auto outputLayer = findOutputLayerForDisplay(display);
3768 if (outputLayer == nullptr) {
3769 return false;
3770 }
3771
3772 // We need filtering if the sourceCrop rectangle size does not match the
3773 // viewport rectangle size (not a 1:1 render)
3774 const auto& compositionState = outputLayer->getState();
3775 const ui::Transform& displayTransform = display->getTransform();
3776 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
3777 // Undo the transformation of the displayFrame so that we're back into
3778 // layer-stack space.
3779 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
3780 const FloatRect sourceCrop = compositionState.sourceCrop;
3781
3782 int32_t frameHeight = frame.getHeight();
3783 int32_t frameWidth = frame.getWidth();
3784 // If the display transform had a rotational component then undo the
3785 // rotation so that the orientation matches the source crop.
3786 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
3787 std::swap(frameHeight, frameWidth);
3788 }
3789 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
3790}
3791
3792void Layer::latchAndReleaseBuffer() {
3793 if (hasReadyFrame()) {
3794 bool ignored = false;
3795 latchBuffer(ignored, systemTime());
3796 }
3797 releasePendingBuffer(systemTime());
3798}
3799
3800PixelFormat Layer::getPixelFormat() const {
3801 return mBufferInfo.mPixelFormat;
3802}
3803
3804bool Layer::getTransformToDisplayInverse() const {
3805 return mBufferInfo.mTransformToDisplayInverse;
3806}
3807
3808Rect Layer::getBufferCrop() const {
3809 // this is the crop rectangle that applies to the buffer
3810 // itself (as opposed to the window)
3811 if (!mBufferInfo.mCrop.isEmpty()) {
3812 // if the buffer crop is defined, we use that
3813 return mBufferInfo.mCrop;
3814 } else if (mBufferInfo.mBuffer != nullptr) {
3815 // otherwise we use the whole buffer
3816 return mBufferInfo.mBuffer->getBounds();
3817 } else {
3818 // if we don't have a buffer yet, we use an empty/invalid crop
3819 return Rect();
3820 }
3821}
3822
3823uint32_t Layer::getBufferTransform() const {
3824 return mBufferInfo.mTransform;
3825}
3826
3827ui::Dataspace Layer::getDataSpace() const {
3828 return mDrawingState.dataspaceRequested ? getRequestedDataSpace() : ui::Dataspace::UNKNOWN;
3829}
3830
3831ui::Dataspace Layer::getRequestedDataSpace() const {
3832 return hasBufferOrSidebandStream() ? mBufferInfo.mDataspace : mDrawingState.dataspace;
3833}
3834
3835ui::Dataspace Layer::translateDataspace(ui::Dataspace dataspace) {
3836 ui::Dataspace updatedDataspace = dataspace;
3837 // translate legacy dataspaces to modern dataspaces
3838 switch (dataspace) {
3839 case ui::Dataspace::SRGB:
3840 updatedDataspace = ui::Dataspace::V0_SRGB;
3841 break;
3842 case ui::Dataspace::SRGB_LINEAR:
3843 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
3844 break;
3845 case ui::Dataspace::JFIF:
3846 updatedDataspace = ui::Dataspace::V0_JFIF;
3847 break;
3848 case ui::Dataspace::BT601_625:
3849 updatedDataspace = ui::Dataspace::V0_BT601_625;
3850 break;
3851 case ui::Dataspace::BT601_525:
3852 updatedDataspace = ui::Dataspace::V0_BT601_525;
3853 break;
3854 case ui::Dataspace::BT709:
3855 updatedDataspace = ui::Dataspace::V0_BT709;
3856 break;
3857 default:
3858 break;
3859 }
3860
3861 return updatedDataspace;
3862}
3863
3864sp<GraphicBuffer> Layer::getBuffer() const {
3865 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
3866}
3867
Patrick Williamsbb25f802022-08-30 23:02:34 +00003868void Layer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
3869 mTransformHint = getFixedTransformHint();
3870 if (mTransformHint == ui::Transform::ROT_INVALID) {
3871 mTransformHint = displayTransformHint;
3872 }
Vishnu Nair71fcf912022-10-18 09:14:20 -07003873 mSkipReportingTransformHint = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003874}
3875
3876const std::shared_ptr<renderengine::ExternalTexture>& Layer::getExternalTexture() const {
3877 return mBufferInfo.mBuffer;
3878}
3879
3880bool Layer::setColor(const half3& color) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003881 if (mDrawingState.color.rgb == color) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003882 return false;
3883 }
3884
3885 mDrawingState.sequence++;
Vishnu Nairbbceb462022-10-10 04:52:13 +00003886 mDrawingState.color.rgb = color;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003887 mDrawingState.modified = true;
3888 setTransactionFlags(eTransactionNeeded);
3889 return true;
3890}
3891
3892bool Layer::fillsColor() const {
3893 return !hasBufferOrSidebandStream() && mDrawingState.color.r >= 0.0_hf &&
3894 mDrawingState.color.g >= 0.0_hf && mDrawingState.color.b >= 0.0_hf;
3895}
3896
3897bool Layer::hasBlur() const {
3898 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
3899}
3900
Vishnu Nairba354102022-08-01 00:14:18 +00003901void Layer::updateSnapshot(bool updateGeometry) {
3902 if (!getCompositionEngineLayerFE()) {
3903 return;
3904 }
3905
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003906 auto* snapshot = editLayerSnapshot();
Vishnu Nairba354102022-08-01 00:14:18 +00003907 if (updateGeometry) {
3908 prepareBasicGeometryCompositionState();
3909 prepareGeometryCompositionState();
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003910 snapshot->roundedCorner = getRoundedCornerState();
3911 snapshot->stretchEffect = getStretchEffect();
3912 snapshot->transformedBounds = mScreenBounds;
3913 if (mEffectiveShadowRadius > 0.f) {
3914 snapshot->shadowSettings = mFlinger->mDrawingState.globalShadowSettings;
3915
3916 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
3917 // it if transparent regions are present. This may not be necessary since shadows are
3918 // typically cast by layers without transparent regions.
3919 snapshot->shadowSettings.boundaries = mBounds;
3920
3921 const float casterAlpha = snapshot->alpha;
3922 const bool casterIsOpaque =
3923 ((mBufferInfo.mBuffer != nullptr) && isOpaque(mDrawingState));
3924
3925 // If the casting layer is translucent, we need to fill in the shadow underneath the
3926 // layer. Otherwise the generated shadow will only be shown around the casting layer.
3927 snapshot->shadowSettings.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
3928 snapshot->shadowSettings.ambientColor *= casterAlpha;
3929 snapshot->shadowSettings.spotColor *= casterAlpha;
3930 }
3931 snapshot->shadowSettings.length = mEffectiveShadowRadius;
Vishnu Nairba354102022-08-01 00:14:18 +00003932 }
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003933 snapshot->contentOpaque = isOpaque(mDrawingState);
3934 snapshot->isHdrY410 = isHdrY410();
3935 snapshot->bufferNeedsFiltering = bufferNeedsFiltering();
3936 sp<Layer> p = mDrawingParent.promote();
3937 if (p != nullptr) {
3938 snapshot->transform = p->getTransform();
3939 } else {
3940 snapshot->transform.reset();
3941 }
3942 snapshot->bufferSize = getBufferSize(mDrawingState);
3943 snapshot->externalTexture = mBufferInfo.mBuffer;
Vishnu Naire14c6b32022-08-06 04:20:15 +00003944 snapshot->hasReadyFrame = hasReadyFrame();
Vishnu Nairba354102022-08-01 00:14:18 +00003945 preparePerFrameCompositionState();
3946}
3947
Vishnu Naire14c6b32022-08-06 04:20:15 +00003948void Layer::updateChildrenSnapshots(bool updateGeometry) {
3949 for (const sp<Layer>& child : mDrawingChildren) {
3950 child->updateSnapshot(updateGeometry);
3951 child->updateChildrenSnapshots(updateGeometry);
3952 }
3953}
3954
Vishnu Nair0a4fb002022-08-08 02:40:42 +00003955void Layer::updateMetadataSnapshot(const LayerMetadata& parentMetadata) {
3956 mSnapshot->layerMetadata = parentMetadata;
3957 mSnapshot->layerMetadata.merge(mDrawingState.metadata);
3958 for (const sp<Layer>& child : mDrawingChildren) {
3959 child->updateMetadataSnapshot(mSnapshot->layerMetadata);
3960 }
3961}
3962
3963void Layer::updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMetadata,
3964 std::unordered_set<Layer*>& visited) {
3965 if (visited.find(this) != visited.end()) {
3966 ALOGW("Cycle containing layer %s detected in z-order relatives", getDebugName());
3967 return;
3968 }
3969 visited.insert(this);
3970
3971 mSnapshot->relativeLayerMetadata = relativeLayerMetadata;
3972
3973 if (mDrawingState.zOrderRelatives.empty()) {
3974 return;
3975 }
3976 LayerMetadata childRelativeLayerMetadata = mSnapshot->relativeLayerMetadata;
3977 childRelativeLayerMetadata.merge(mSnapshot->layerMetadata);
3978 for (wp<Layer> weakRelative : mDrawingState.zOrderRelatives) {
3979 sp<Layer> relative = weakRelative.promote();
3980 if (!relative) {
3981 continue;
3982 }
3983 relative->updateRelativeMetadataSnapshot(childRelativeLayerMetadata, visited);
3984 }
3985}
3986
Patrick Williams377c0142022-10-13 17:33:38 +00003987LayerSnapshotGuard::LayerSnapshotGuard(Layer* layer) : mLayer(layer) {
3988 if (mLayer) {
3989 mLayer->mLayerFE->mSnapshot = std::move(mLayer->mSnapshot);
3990 }
3991}
3992
3993LayerSnapshotGuard::~LayerSnapshotGuard() {
3994 if (mLayer) {
3995 mLayer->mSnapshot = std::move(mLayer->mLayerFE->mSnapshot);
3996 }
3997}
3998
3999LayerSnapshotGuard::LayerSnapshotGuard(LayerSnapshotGuard&& other) : mLayer(other.mLayer) {
4000 other.mLayer = nullptr;
4001}
4002
4003LayerSnapshotGuard& LayerSnapshotGuard::operator=(LayerSnapshotGuard&& other) {
4004 mLayer = other.mLayer;
4005 other.mLayer = nullptr;
4006 return *this;
4007}
4008
Mathias Agopian13127d82013-03-05 17:47:11 -08004009// ---------------------------------------------------------------------------
4010
Marin Shalamanov46084422020-10-13 12:33:42 +02004011std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07004012 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
4013 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02004014}
4015
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07004016} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07004017
4018#if defined(__gl_h_)
4019#error "don't include gl/gl.h in this file"
4020#endif
4021
4022#if defined(__gl2_h_)
4023#error "don't include gl2/gl2.h in this file"
4024#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08004025
4026// TODO(b/129481165): remove the #pragma below and fix conversion issues
4027#pragma clang diagnostic pop // ignored "-Wconversion"