blob: b7abd95af88434523c7770ded28d88b494bce8b4 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Dan Stoza9e56aa02015-11-02 13:00:03 -080021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080024#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Alec Mourie60041e2019-06-14 18:59:51 -070026#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
[1;3C2b9fc252021-02-04 16:16:50 -080028#include <android-base/properties.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Steven Thomas62a4cf82020-01-31 12:04:03 -080030#include <android/native_window.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070031#include <binder/IPCThreadState.h>
Patrick Williamsbb25f802022-08-30 23:02:34 +000032#include <compositionengine/CompositionEngine.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/Display.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080034#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <compositionengine/OutputLayer.h>
36#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070039#include <cutils/properties.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070040#include <ftl/enum.h>
Dominik Laskowski298b08e2022-02-15 13:45:02 -080041#include <ftl/fake_guard.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <gui/BufferItem.h>
43#include <gui/LayerDebugInfo.h>
44#include <gui/Surface.h>
Patrick Williamsbb25f802022-08-30 23:02:34 +000045#include <gui/TraceUtils.h>
Alec Mourie60041e2019-06-14 18:59:51 -070046#include <math.h>
chaviw250bcbb2020-08-05 11:17:54 -070047#include <private/android_filesystem_config.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080048#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070049#include <stdint.h>
50#include <stdlib.h>
51#include <sys/types.h>
Alec Mouridda07d92022-04-25 22:39:25 +000052#include <system/graphics-base-v1.0.h>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070053#include <ui/DataspaceUtils.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080054#include <ui/DebugUtils.h>
55#include <ui/GraphicBuffer.h>
56#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include <utils/Errors.h>
58#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080059#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080061#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Alec Mourie60041e2019-06-14 18:59:51 -070063#include <algorithm>
64#include <mutex>
Vishnu Nair71fcf912022-10-18 09:14:20 -070065#include <optional>
Alec Mourie60041e2019-06-14 18:59:51 -070066#include <sstream>
67
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070068#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080069#include "DisplayHardware/HWComposer.h"
Ady Abraham22c7b5c2020-09-22 19:33:40 -070070#include "FrameTimeline.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070071#include "FrameTracer/FrameTracer.h"
Vishnu Naircb8be502022-10-12 19:03:23 +000072#include "FrontEnd/LayerCreationArgs.h"
Vishnu Nair07e2a482022-10-18 19:18:16 +000073#include "FrontEnd/LayerHandle.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080074#include "LayerProtoHelper.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080076#include "TimeStats/TimeStats.h"
Robert Carr3e2a2992021-06-11 13:42:55 -070077#include "TunnelModeEnabledReporter.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070078
David Sodman41fdfc92017-11-06 16:09:56 -080079#define DEBUG_RESIZE 0
Patrick Williamsbb25f802022-08-30 23:02:34 +000080#define EARLY_RELEASE_ENABLED false
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010083namespace {
84constexpr int kDumpTableRowLength = 159;
Patrick Williamsbb25f802022-08-30 23:02:34 +000085
Prabir Pradhanda0f62c2022-07-22 19:53:04 +000086const ui::Transform kIdentityTransform;
Patrick Williamsbb25f802022-08-30 23:02:34 +000087
Patrick Williamsbb25f802022-08-30 23:02:34 +000088bool assignTransform(ui::Transform* dst, ui::Transform& from) {
89 if (*dst == from) {
90 return false;
91 }
92 *dst = from;
93 return true;
94}
95
96TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
97 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
98 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
99 const auto frameRateCompatibility = [frameRate] {
100 switch (frameRate.type) {
101 case Layer::FrameRateCompatibility::Default:
102 return FrameRateCompatibility::Default;
103 case Layer::FrameRateCompatibility::ExactOrMultiple:
104 return FrameRateCompatibility::ExactOrMultiple;
105 default:
106 return FrameRateCompatibility::Undefined;
107 }
108 }();
109
110 const auto seamlessness = [frameRate] {
111 switch (frameRate.seamlessness) {
112 case scheduler::Seamlessness::OnlySeamless:
113 return Seamlessness::ShouldBeSeamless;
114 case scheduler::Seamlessness::SeamedAndSeamless:
115 return Seamlessness::NotRequired;
116 default:
117 return Seamlessness::Undefined;
118 }
119 }();
120
121 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
122 .frameRateCompatibility = frameRateCompatibility,
123 .seamlessness = seamlessness};
124}
125
Marin Shalamanov1876e2e2020-12-04 13:23:59 +0100126} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700128using namespace ftl::flag_operators;
129
Yiwei Zhang5434a782018-12-05 18:06:32 -0800130using base::StringAppendF;
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800131using gui::GameMode;
132using gui::LayerMetadata;
chaviw3277faf2021-05-19 16:45:23 -0500133using gui::WindowInfo;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800134
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700135using PresentState = frametimeline::SurfaceFrame::PresentState;
136
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700137Layer::Layer(const LayerCreationArgs& args)
Vishnu Naircb8be502022-10-12 19:03:23 +0000138 : sequence(args.sequence),
Ady Abrahamd11bade2022-08-01 16:18:03 -0700139 mFlinger(sp<SurfaceFlinger>::fromExisting(args.flinger)),
Arthur Hung23e07502021-10-15 11:58:19 +0000140 mName(base::StringPrintf("%s#%d", args.name.c_str(), sequence)),
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700141 mClientRef(args.client),
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800142 mWindowType(static_cast<WindowInfo::Type>(
143 args.metadata.getInt32(gui::METADATA_WINDOW_TYPE, 0))),
Tianhao Yao67dd7122022-02-22 17:48:33 +0000144 mLayerCreationFlags(args.flags),
Patrick Williamsbb25f802022-08-30 23:02:34 +0000145 mBorderEnabled(false),
146 mTextureName(args.textureName),
Vishnu Naire14c6b32022-08-06 04:20:15 +0000147 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
Dominik Laskowski596a2562022-10-28 11:26:12 -0400197 mFrameTracker.setDisplayRefreshPeriod(args.flinger->mScheduler->getLeaderVsyncPeriod());
Robert Carr2e102c92018-10-23 12:11:15 -0700198
Vishnu Naircb8be502022-10-12 19:03:23 +0000199 mOwnerUid = args.ownerUid;
200 mOwnerPid = args.ownerPid;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000201
202 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
203 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
204 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
205 mDrawingState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000206
207 mSnapshot->sequence = sequence;
208 mSnapshot->name = getDebugName();
209 mSnapshot->textureName = mTextureName;
210 mSnapshot->premultipliedAlpha = mPremultipliedAlpha;
211 mSnapshot->transform = {};
Dominik Laskowski75848362019-11-11 17:57:20 -0800212}
213
214void Layer::onFirstRef() {
215 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700216}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700217
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700218Layer::~Layer() {
Patrick Williamsbb25f802022-08-30 23:02:34 +0000219 // The original layer and the clone layer share the same texture and buffer. Therefore, only
220 // one of the layers, in this case the original layer, needs to handle the deletion. The
221 // original layer and the clone should be removed at the same time so there shouldn't be any
222 // issue with the clone layer trying to use the texture.
223 if (mBufferInfo.mBuffer != nullptr) {
224 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
225 mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
226 mBufferInfo.mFence,
227 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
228 mOwnerUid));
229 }
230 if (!isClone()) {
231 // The original layer and the clone layer share the same texture. Therefore, only one of
232 // the layers, in this case the original layer, needs to handle the deletion. The original
233 // layer and the clone should be removed at the same time so there shouldn't be any issue
234 // with the clone layer trying to use the deleted texture.
235 mFlinger->deleteTextureAsync(mTextureName);
236 }
237 const int32_t layerId = getSequence();
238 mFlinger->mTimeStats->onDestroy(layerId);
239 mFlinger->mFrameTracer->onDestroy(layerId);
240
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000241 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700242 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700243
244 if (mDrawingState.sidebandStream != nullptr) {
245 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
246 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700247 if (mHadClonedChild) {
248 mFlinger->mNumClones--;
249 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700250}
251
Mathias Agopian13127d82013-03-05 17:47:11 -0800252// ---------------------------------------------------------------------------
253// callbacks
254// ---------------------------------------------------------------------------
255
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700256void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700257 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700258 return;
259 }
Robert Carr2e102c92018-10-23 12:11:15 -0700260
Robert Carr6a160312021-05-17 12:08:20 -0700261 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700262 if (strongRelative == nullptr) {
263 setZOrderRelativeOf(nullptr);
264 return;
265 }
266
267 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700268 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700269 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800270 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700271 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700272}
273
274void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700275 if (!mRemovedFromDrawingState) {
276 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700277 mFlinger->mScheduler->deregisterLayer(this);
278 }
Rob Carr4bba3702018-10-08 21:53:30 +0000279
Ady Abrahamd11bade2022-08-01 16:18:03 -0700280 mFlinger->markLayerPendingRemovalLocked(sp<Layer>::fromExisting(this));
Chia-I Wuc6657022017-08-15 11:18:17 -0700281}
Chia-I Wu38512252017-05-17 14:36:16 -0700282
chaviw68d4dab2020-06-08 15:07:32 -0700283sp<Layer> Layer::getRootLayer() {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000284 sp<Layer> parent = getParent();
chaviw68d4dab2020-06-08 15:07:32 -0700285 if (parent == nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700286 return sp<Layer>::fromExisting(this);
chaviw68d4dab2020-06-08 15:07:32 -0700287 }
288 return parent->getRootLayer();
289}
290
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700291void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700292 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
293 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700294 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700295
296 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700297 layer->removeFromCurrentState();
298 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700299 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700300}
301
chaviw61626f22018-11-15 16:26:27 -0800302void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700303 if (mRemovedFromDrawingState) {
304 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700305 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700306 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700307 }
chaviw61626f22018-11-15 16:26:27 -0800308
309 for (const auto& child : mCurrentChildren) {
310 child->addToCurrentState();
311 }
312}
313
Mathias Agopian13127d82013-03-05 17:47:11 -0800314// ---------------------------------------------------------------------------
315// set-up
316// ---------------------------------------------------------------------------
317
chaviw13fdc492017-06-27 12:40:18 -0700318bool Layer::getPremultipledAlpha() const {
319 return mPremultipliedAlpha;
320}
321
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700322sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800323 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700324 if (mGetHandleCalled) {
325 ALOGE("Get handle called twice" );
326 return nullptr;
327 }
328 mGetHandleCalled = true;
Vishnu Nair07e2a482022-10-18 19:18:16 +0000329 return sp<LayerHandle>::make(mFlinger, sp<Layer>::fromExisting(this));
Mathias Agopian13127d82013-03-05 17:47:11 -0800330}
331
332// ---------------------------------------------------------------------------
333// h/w composer set-up
334// ---------------------------------------------------------------------------
335
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700336static Rect reduce(const Rect& win, const Region& exclude) {
337 if (CC_LIKELY(exclude.isEmpty())) {
338 return win;
339 }
340 if (exclude.isRect()) {
341 return win.reduce(exclude.getBounds());
342 }
343 return Region(win).subtract(exclude).getBounds();
344}
345
Dan Stoza80d61162017-12-20 15:57:52 -0800346static FloatRect reduce(const FloatRect& win, const Region& exclude) {
347 if (CC_LIKELY(exclude.isEmpty())) {
348 return win;
349 }
350 // Convert through Rect (by rounding) for lack of FloatRegion
351 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
352}
353
Vishnu Nair4351ad52019-02-11 14:13:02 -0800354Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800355 if (!reduceTransparentRegion) {
356 return Rect{mScreenBounds};
357 }
358
359 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800360 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800361 // Transform to screen space.
362 bounds = t.transform(bounds);
363 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700364}
365
Vishnu Nair4351ad52019-02-11 14:13:02 -0800366FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000367 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800368 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700369}
370
Vishnu Nairf0c28512019-02-08 12:40:28 -0800371FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
372 // Subtract the transparent region and snap to the bounds.
373 return reduce(mBounds, activeTransparentRegion);
374}
375
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700376void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
377 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800378 const State& s(getDrawingState());
379
380 // Calculate effective layer transform
381 mEffectiveTransform = parentTransform * getActiveTransform(s);
382
Garfield Tan2c1782c2022-02-16 15:25:05 -0800383 if (CC_UNLIKELY(!isTransformValid())) {
384 ALOGW("Stop computing bounds for %s because it has invalid transformation.",
385 getDebugName());
386 return;
387 }
388
Vishnu Nair4351ad52019-02-11 14:13:02 -0800389 // Transform parent bounds to layer space
390 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
391
Vishnu Nairc652ff82019-03-15 12:48:54 -0700392 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800393 mSourceBounds = computeSourceBounds(parentBounds);
394
395 // Calculate bounds by croping diplay frame with layer crop and parent bounds
396 FloatRect bounds = mSourceBounds;
397 const Rect layerCrop = getCrop(s);
398 if (!layerCrop.isEmpty()) {
399 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
400 }
401 bounds = bounds.intersect(parentBounds);
402
403 mBounds = bounds;
404 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700405
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700406 // Use the layer's own shadow radius if set. Otherwise get the radius from
407 // parent.
408 if (s.shadowRadius > 0.f) {
409 mEffectiveShadowRadius = s.shadowRadius;
410 } else {
411 mEffectiveShadowRadius = parentShadowRadius;
412 }
413
414 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
415 // don't pass it to its children.
416 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
417
Vishnu Nair4351ad52019-02-11 14:13:02 -0800418 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700419 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800420 }
Vishnu Nairba354102022-08-01 00:14:18 +0000421
422 if (mPotentialCursor) {
423 prepareCursorCompositionState();
424 }
Vishnu Nair4351ad52019-02-11 14:13:02 -0800425}
426
Vishnu Nair60356342018-11-13 13:00:45 -0800427Rect Layer::getCroppedBufferSize(const State& s) const {
428 Rect size = getBufferSize(s);
429 Rect crop = getCrop(s);
430 if (!crop.isEmpty() && size.isValid()) {
431 size.intersect(crop, &size);
432 } else if (!crop.isEmpty()) {
433 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700434 }
Vishnu Nair60356342018-11-13 13:00:45 -0800435 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800436}
437
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700438void Layer::setupRoundedCornersCropCoordinates(Rect win,
439 const FloatRect& roundedCornersCrop) const {
440 // Translate win by the rounded corners rect coordinates, to have all values in
441 // layer coordinate space.
442 win.left -= roundedCornersCrop.left;
443 win.right -= roundedCornersCrop.left;
444 win.top -= roundedCornersCrop.top;
445 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700446}
447
Lloyd Piquede196652020-01-22 17:29:58 -0800448void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800449 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800450 const auto alpha = static_cast<float>(getAlpha());
451 const bool opaque = isOpaque(drawingState);
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700452 const bool usesRoundedCorners = hasRoundedCorners();
Lloyd Piquec6687342019-03-07 21:34:57 -0800453
454 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
455 if (!opaque || alpha != 1.0f) {
456 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
457 : Hwc2::IComposerClient::BlendMode::COVERAGE;
458 }
459
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000460 auto* snapshot = editLayerSnapshot();
461 snapshot->outputFilter = getOutputFilter();
462 snapshot->isVisible = isVisible();
463 snapshot->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
464 snapshot->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800465
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000466 snapshot->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800467 contentDirty = false;
468
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000469 snapshot->geomLayerBounds = mBounds;
470 snapshot->geomLayerTransform = getTransform();
471 snapshot->geomInverseLayerTransform = snapshot->geomLayerTransform.inverse();
472 snapshot->transparentRegionHint = getActiveTransparentRegion(drawingState);
Vishnu Naire7613db2022-11-04 00:07:06 -0700473 snapshot->blurRegionTransform = getActiveTransform(drawingState).inverse();
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000474 snapshot->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
475 snapshot->alpha = alpha;
476 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
477 snapshot->blurRegions = drawingState.blurRegions;
478 snapshot->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800479}
480
Lloyd Piquede196652020-01-22 17:29:58 -0800481void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800482 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000483 auto* snapshot = editLayerSnapshot();
David Sodman15094112018-10-11 09:39:37 -0700484
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000485 snapshot->geomBufferSize = getBufferSize(drawingState);
486 snapshot->geomContentCrop = getBufferCrop();
487 snapshot->geomCrop = getCrop(drawingState);
488 snapshot->geomBufferTransform = getBufferTransform();
489 snapshot->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
490 snapshot->geomUsesSourceCrop = usesSourceCrop();
491 snapshot->isSecure = isSecure();
Lloyd Piquede196652020-01-22 17:29:58 -0800492
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000493 snapshot->metadata.clear();
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800494 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
495 for (const auto& [key, mandatory] : supportedMetadata) {
496 const auto& genericLayerMetadataCompatibilityMap =
497 mFlinger->getGenericLayerMetadataKeyMap();
498 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
499 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
500 continue;
501 }
502 const uint32_t id = compatIter->second;
503
504 auto it = drawingState.metadata.mMap.find(id);
505 if (it == std::end(drawingState.metadata.mMap)) {
506 continue;
507 }
508
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000509 snapshot->metadata.emplace(key,
510 compositionengine::GenericLayerMetadataEntry{mandatory,
511 it->second});
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800512 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800513}
David Sodmanba340492018-08-05 21:51:33 -0700514
Lloyd Piquede196652020-01-22 17:29:58 -0800515void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800516 const auto& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000517 auto* snapshot = editLayerSnapshot();
Lloyd Piquef5275482019-01-29 18:42:42 -0800518
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000519 snapshot->forceClientComposition = false;
Lloyd Piquede196652020-01-22 17:29:58 -0800520
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000521 snapshot->isColorspaceAgnostic = isColorSpaceAgnostic();
522 snapshot->dataspace = getDataSpace();
523 snapshot->colorTransform = getColorTransform();
524 snapshot->colorTransformIsIdentity = !hasColorTransform();
525 snapshot->surfaceDamage = surfaceDamageRegion;
526 snapshot->hasProtectedContent = isProtected();
527 snapshot->dimmingEnabled = isDimmingEnabled();
Lloyd Pique688abd42019-02-15 15:42:24 -0800528
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700529 const bool usesRoundedCorners = hasRoundedCorners();
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700530
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000531 snapshot->isOpaque = isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800532
533 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400534 // Rounded corners no longer force client composition, since we may use a
535 // hole punch so that the layer will appear to have rounded corners.
536 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000537 snapshot->stretchEffect.hasEffect()) {
538 snapshot->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800539 }
Vishnu Nairb801a982021-11-02 15:12:08 -0700540 // If there are no visible region changes, we still need to update blur parameters.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000541 snapshot->blurRegions = drawingState.blurRegions;
542 snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400543
544 // Layer framerate is used in caching decisions.
545 // Retrieve it from the scheduler which maintains an instance of LayerHistory, and store it in
546 // LayerFECompositionState where it would be visible to Flattener.
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000547 snapshot->fps = mFlinger->getLayerFramerate(systemTime(), getSequence());
Patrick Williamsbb25f802022-08-30 23:02:34 +0000548
549 if (hasBufferOrSidebandStream()) {
550 preparePerFrameBufferCompositionState();
551 } else {
552 preparePerFrameEffectsCompositionState();
553 }
554}
555
556void Layer::preparePerFrameBufferCompositionState() {
557 // Sideband layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000558 auto* snapshot = editLayerSnapshot();
559 if (snapshot->sidebandStream.get() && !snapshot->sidebandStreamHasFrame) {
560 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000561 aidl::android::hardware::graphics::composer3::Composition::SIDEBAND;
562 return;
563 } else if ((mDrawingState.flags & layer_state_t::eLayerIsDisplayDecoration) != 0) {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000564 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000565 aidl::android::hardware::graphics::composer3::Composition::DISPLAY_DECORATION;
566 } else {
567 // Normal buffer layers
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000568 snapshot->hdrMetadata = mBufferInfo.mHdrMetadata;
569 snapshot->compositionType = mPotentialCursor
Patrick Williamsbb25f802022-08-30 23:02:34 +0000570 ? aidl::android::hardware::graphics::composer3::Composition::CURSOR
571 : aidl::android::hardware::graphics::composer3::Composition::DEVICE;
572 }
573
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000574 snapshot->buffer = getBuffer();
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000575 snapshot->acquireFence = mBufferInfo.mFence;
576 snapshot->frameNumber = mBufferInfo.mFrameNumber;
577 snapshot->sidebandStreamHasFrame = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +0000578}
579
580void Layer::preparePerFrameEffectsCompositionState() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000581 auto* snapshot = editLayerSnapshot();
582 snapshot->color = getColor();
583 snapshot->compositionType =
Patrick Williamsbb25f802022-08-30 23:02:34 +0000584 aidl::android::hardware::graphics::composer3::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800585}
586
Lloyd Piquede196652020-01-22 17:29:58 -0800587void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800588 const State& drawingState{getDrawingState()};
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000589 auto* snapshot = editLayerSnapshot();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800590
591 // Apply the layer's transform, followed by the display's global transform
592 // Here we're guaranteed that the layer's transform preserves rects
593 Rect win = getCroppedBufferSize(drawingState);
594 // Subtract the transparent region and snap to the bounds
595 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
596 Rect frame(getTransform().transform(bounds));
597
Vishnu Nairbedb44b2022-08-02 21:47:40 +0000598 snapshot->cursorFrame = frame;
Lloyd Piquede196652020-01-22 17:29:58 -0800599}
600
Lloyd Piquea83776c2019-01-29 18:42:32 -0800601const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700602 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800603}
604
Mathias Agopian13127d82013-03-05 17:47:11 -0800605// ---------------------------------------------------------------------------
606// drawing...
607// ---------------------------------------------------------------------------
608
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500609aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType(
610 const DisplayDevice& display) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700611 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800612 if (outputLayer == nullptr) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500613 return aidl::android::hardware::graphics::composer3::Composition::INVALID;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800614 }
615 if (outputLayer->getState().hwc) {
616 return (*outputLayer->getState().hwc).hwcCompositionType;
617 } else {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500618 return aidl::android::hardware::graphics::composer3::Composition::CLIENT;
Alec Mouri6b9e9912020-01-21 10:50:24 -0800619 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620}
621
Mathias Agopian13127d82013-03-05 17:47:11 -0800622// ----------------------------------------------------------------------------
623// local state
624// ----------------------------------------------------------------------------
625
David Sodman41fdfc92017-11-06 16:09:56 -0800626bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800627 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700628 if (s.flags & layer_state_t::eLayerSecure) {
629 return true;
630 }
631
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000632 const auto p = mDrawingParent.promote();
Garfield Tande619fa2020-10-02 17:13:53 -0700633 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700634}
635
Mathias Agopian13127d82013-03-05 17:47:11 -0800636// ----------------------------------------------------------------------------
637// transaction
638// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800639
Marissa Wall61c58622018-07-18 10:12:20 -0700640uint32_t Layer::doTransaction(uint32_t flags) {
641 ATRACE_CALL();
642
Robert Carr0758e5d2021-03-11 22:15:04 -0800643 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700644 mDrawingStateModified = mDrawingState.modified;
645 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700646
Alec Mourib416efd2018-09-06 21:01:59 +0000647 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700648
Robert Carr6a160312021-05-17 12:08:20 -0700649 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800650 // invalidate and recompute the visible regions if needed
651 flags |= Layer::eVisibleRegion;
652 }
653
Robert Carr6a160312021-05-17 12:08:20 -0700654 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800655 // invalidate and recompute the visible regions if needed
Arthur Hung9ed43392022-05-27 06:31:57 +0000656 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800657 flags |= eVisibleRegion;
658 this->contentDirty = true;
659
660 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700661 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800662 }
663
Arthur Hung9ed43392022-05-27 06:31:57 +0000664 if (!mPotentialCursor && (flags & Layer::eVisibleRegion)) {
665 mFlinger->mUpdateInputInfo = true;
666 }
667
Robert Carr6a160312021-05-17 12:08:20 -0700668 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800669
Mathias Agopian13127d82013-03-05 17:47:11 -0800670 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671}
672
Robert Carr6a160312021-05-17 12:08:20 -0700673void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000674 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
675 // bufferSurfaceFrameTX will be presented in latchBuffer.
676 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
677 if (surfaceFrame->getPresentState() != PresentState::Presented) {
678 // With applyPendingStates, we could end up having presented surfaceframes from previous
679 // states
680 surfaceFrame->setPresentState(PresentState::Presented);
681 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
682 }
683 }
Robert Carr6a160312021-05-17 12:08:20 -0700684 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700685}
686
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700687uint32_t Layer::clearTransactionFlags(uint32_t mask) {
688 const auto flags = mTransactionFlags & mask;
689 mTransactionFlags &= ~mask;
690 return flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800691}
692
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700693void Layer::setTransactionFlags(uint32_t mask) {
694 mTransactionFlags |= mask;
Mathias Agopian13127d82013-03-05 17:47:11 -0800695}
696
Robert Carr1f0a16a2016-10-24 16:27:39 -0700697bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
698 ssize_t idx = mCurrentChildren.indexOf(childLayer);
699 if (idx < 0) {
700 return false;
701 }
702 if (childLayer->setLayer(z)) {
703 mCurrentChildren.removeAt(idx);
704 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800705 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700706 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800707 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700708}
709
Robert Carr503c7042017-09-27 15:06:08 -0700710bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
711 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
712 ssize_t idx = mCurrentChildren.indexOf(childLayer);
713 if (idx < 0) {
714 return false;
715 }
716 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
717 mCurrentChildren.removeAt(idx);
718 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800719 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700720 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800721 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700722}
723
Robert Carrae060832016-11-28 10:51:00 -0800724bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700725 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
726 mDrawingState.sequence++;
727 mDrawingState.z = z;
728 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700729
Robert Carra70e91c2021-06-11 13:59:52 -0700730 mFlinger->mSomeChildrenChanged = true;
731
Robert Carrdb66e622017-04-10 16:55:57 -0700732 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700733 if (mDrawingState.zOrderRelativeOf != nullptr) {
734 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700735 if (strongRelative != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700736 strongRelative->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700737 }
chaviw606e5cf2019-03-01 10:12:10 -0800738 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700739 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800740 setTransactionFlags(eTransactionNeeded);
741 return true;
742}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700743
Robert Carrdb66e622017-04-10 16:55:57 -0700744void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700745 mDrawingState.zOrderRelatives.remove(relative);
746 mDrawingState.sequence++;
747 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700748 setTransactionFlags(eTransactionNeeded);
749}
750
751void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700752 mDrawingState.zOrderRelatives.add(relative);
753 mDrawingState.modified = true;
754 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700755 setTransactionFlags(eTransactionNeeded);
756}
757
chaviw606e5cf2019-03-01 10:12:10 -0800758void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700759 mDrawingState.zOrderRelativeOf = relativeOf;
760 mDrawingState.sequence++;
761 mDrawingState.modified = true;
762 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700763
chaviw606e5cf2019-03-01 10:12:10 -0800764 setTransactionFlags(eTransactionNeeded);
765}
766
Robert Carr503d2bd2017-12-04 15:49:47 -0800767bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nair07e2a482022-10-18 19:18:16 +0000768 sp<Layer> relative = LayerHandle::getLayer(relativeToHandle);
Robert Carrdb66e622017-04-10 16:55:57 -0700769 if (relative == nullptr) {
770 return false;
771 }
772
Robert Carr6a160312021-05-17 12:08:20 -0700773 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
774 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800775 return false;
776 }
777
Robert Carr88b85e12022-03-21 15:47:35 -0700778 if (CC_UNLIKELY(relative->usingRelativeZ(LayerVector::StateSet::Drawing)) &&
779 (relative->mDrawingState.zOrderRelativeOf == this)) {
780 ALOGE("Detected relative layer loop between %s and %s",
781 mName.c_str(), relative->mName.c_str());
782 ALOGE("Ignoring new call to set relative layer");
783 return false;
784 }
785
Robert Carra70e91c2021-06-11 13:59:52 -0700786 mFlinger->mSomeChildrenChanged = true;
787
Robert Carr6a160312021-05-17 12:08:20 -0700788 mDrawingState.sequence++;
789 mDrawingState.modified = true;
790 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700791
Robert Carr6a160312021-05-17 12:08:20 -0700792 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700793 if (oldZOrderRelativeOf != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700794 oldZOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
chaviw9ab4bd12017-11-03 13:11:00 -0700795 }
chaviw606e5cf2019-03-01 10:12:10 -0800796 setZOrderRelativeOf(relative);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700797 relative->addZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carrdb66e622017-04-10 16:55:57 -0700798
799 setTransactionFlags(eTransactionNeeded);
800
801 return true;
802}
803
Winson Chunga30f7c92021-06-29 15:42:56 -0700804bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
805 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
806 mDrawingState.isTrustedOverlay = isTrustedOverlay;
807 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +0000808 mFlinger->mUpdateInputInfo = true;
Winson Chunga30f7c92021-06-29 15:42:56 -0700809 setTransactionFlags(eTransactionNeeded);
810 return true;
811}
812
813bool Layer::isTrustedOverlay() const {
814 if (getDrawingState().isTrustedOverlay) {
815 return true;
816 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000817 const auto& p = mDrawingParent.promote();
Winson Chunga30f7c92021-06-29 15:42:56 -0700818 return (p != nullptr) && p->isTrustedOverlay();
819}
820
Dan Stoza9e56aa02015-11-02 13:00:03 -0800821bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700822 if (mDrawingState.color.a == alpha) return false;
823 mDrawingState.sequence++;
824 mDrawingState.color.a = alpha;
825 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800826 setTransactionFlags(eTransactionNeeded);
827 return true;
828}
chaviw13fdc492017-06-27 12:40:18 -0700829
Valerie Haudd0b7572019-01-29 14:59:27 -0800830bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700831 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700832 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800833 }
Robert Carr6a160312021-05-17 12:08:20 -0700834 mDrawingState.sequence++;
835 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800836 setTransactionFlags(eTransactionNeeded);
837
Robert Carr6a160312021-05-17 12:08:20 -0700838 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800839 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800840 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700841 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700842 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800843 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700844 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700845
Valerie Haudd0b7572019-01-29 14:59:27 -0800846 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700847 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800848 mFlinger->mLayersAdded = true;
849 // set up SF to handle added color layer
850 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700851 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800852 }
853 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700854 } else if (mDrawingState.bgColorLayer && alpha == 0) {
855 mDrawingState.bgColorLayer->reparent(nullptr);
856 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800857 return true;
858 }
859
Robert Carr6a160312021-05-17 12:08:20 -0700860 mDrawingState.bgColorLayer->setColor(color);
861 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
862 mDrawingState.bgColorLayer->setAlpha(alpha);
863 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800864
chaviw13fdc492017-06-27 12:40:18 -0700865 return true;
866}
867
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700868bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700869 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700870
Robert Carr6a160312021-05-17 12:08:20 -0700871 mDrawingState.sequence++;
872 mDrawingState.cornerRadius = cornerRadius;
873 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700874 setTransactionFlags(eTransactionNeeded);
875 return true;
876}
877
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800878bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700879 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Vishnu Nairb801a982021-11-02 15:12:08 -0700880 // If we start or stop drawing blur then the layer's visibility state may change so increment
881 // the magic sequence number.
882 if (mDrawingState.backgroundBlurRadius == 0 || backgroundBlurRadius == 0) {
883 mDrawingState.sequence++;
884 }
Robert Carr6a160312021-05-17 12:08:20 -0700885 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
886 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800887 setTransactionFlags(eTransactionNeeded);
888 return true;
889}
Marissa Wall61c58622018-07-18 10:12:20 -0700890
Mathias Agopian13127d82013-03-05 17:47:11 -0800891bool Layer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000892 mDrawingState.sequence++;
893 mDrawingState.transparentRegionHint = transparent;
Robert Carr6a160312021-05-17 12:08:20 -0700894 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800895 setTransactionFlags(eTransactionNeeded);
896 return true;
897}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100898
Lucas Dupinc3800b82020-10-02 16:24:48 -0700899bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Vishnu Nairb801a982021-11-02 15:12:08 -0700900 // If we start or stop drawing blur then the layer's visibility state may change so increment
901 // the magic sequence number.
902 if (mDrawingState.blurRegions.size() == 0 || blurRegions.size() == 0) {
903 mDrawingState.sequence++;
904 }
Robert Carr6a160312021-05-17 12:08:20 -0700905 mDrawingState.blurRegions = blurRegions;
906 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700907 setTransactionFlags(eTransactionNeeded);
908 return true;
909}
910
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800911bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700912 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
913 if (mDrawingState.flags == newFlags) return false;
914 mDrawingState.sequence++;
915 mDrawingState.flags = newFlags;
916 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800917 setTransactionFlags(eTransactionNeeded);
918 return true;
919}
Robert Carr99e27f02016-06-16 15:18:02 -0700920
chaviw25714502021-02-11 10:01:08 -0800921bool Layer::setCrop(const Rect& crop) {
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000922 if (mDrawingState.crop == crop) return false;
Robert Carr6a160312021-05-17 12:08:20 -0700923 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700924 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700925
Robert Carr6a160312021-05-17 12:08:20 -0700926 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800927 setTransactionFlags(eTransactionNeeded);
928 return true;
929}
Robert Carr8d5227b2017-03-16 15:41:03 -0700930
Evan Roskyef876c92019-01-25 17:46:06 -0800931bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700932 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
933 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -0800934 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800935 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500936}
937
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700938bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -0700939 if (mDrawingState.layerStack == layerStack) return false;
940 mDrawingState.sequence++;
941 mDrawingState.layerStack = layerStack;
942 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800943 setTransactionFlags(eTransactionNeeded);
944 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700945}
946
Peiyong Linc502cb72019-03-01 15:00:23 -0800947bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -0700948 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800949 return false;
950 }
Robert Carr6a160312021-05-17 12:08:20 -0700951 mDrawingState.sequence++;
952 mDrawingState.colorSpaceAgnostic = agnostic;
953 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -0800954 setTransactionFlags(eTransactionNeeded);
955 return true;
956}
957
Sally Qi81d95e62022-03-21 19:41:33 -0700958bool Layer::setDimmingEnabled(const bool dimmingEnabled) {
959 if (mDrawingState.dimmingEnabled == dimmingEnabled) return false;
960
961 mDrawingState.sequence++;
962 mDrawingState.dimmingEnabled = dimmingEnabled;
963 mDrawingState.modified = true;
964 setTransactionFlags(eTransactionNeeded);
965 return true;
966}
967
Ana Krulecc84d09b2019-11-02 23:10:29 +0100968bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -0700969 if (mDrawingState.frameRateSelectionPriority == priority) return false;
970 mDrawingState.frameRateSelectionPriority = priority;
971 mDrawingState.sequence++;
972 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100973 setTransactionFlags(eTransactionNeeded);
974 return true;
975}
976
977int32_t Layer::getFrameRateSelectionPriority() const {
978 // Check if layer has priority set.
979 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
980 return mDrawingState.frameRateSelectionPriority;
981 }
982 // If not, search whether its parents have it set.
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000983 sp<Layer> parent = getParent();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100984 if (parent != nullptr) {
985 return parent->getFrameRateSelectionPriority();
986 }
987
988 return Layer::PRIORITY_UNSET;
989}
990
Andy Labrada096227e2022-06-15 16:58:11 +0000991bool Layer::setDefaultFrameRateCompatibility(FrameRateCompatibility compatibility) {
992 if (mDrawingState.defaultFrameRateCompatibility == compatibility) return false;
993 mDrawingState.defaultFrameRateCompatibility = compatibility;
994 mDrawingState.modified = true;
995 mFlinger->mScheduler->setDefaultFrameRateCompatibility(this);
996 setTransactionFlags(eTransactionNeeded);
997 return true;
998}
999
1000scheduler::LayerInfo::FrameRateCompatibility Layer::getDefaultFrameRateCompatibility() const {
1001 return mDrawingState.defaultFrameRateCompatibility;
1002}
1003
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001004bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1005 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1006};
1007
Vishnu Nair73908d12022-10-24 21:46:42 -07001008ui::LayerStack Layer::getLayerStack(LayerVector::StateSet state) const {
1009 bool useDrawing = state == LayerVector::StateSet::Drawing;
1010 const auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1011 if (parent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001012 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001013 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001014 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001015}
1016
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001017bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001018 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001019 return false;
1020 }
1021
Robert Carr6a160312021-05-17 12:08:20 -07001022 mDrawingState.sequence++;
1023 mDrawingState.shadowRadius = shadowRadius;
1024 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001025 setTransactionFlags(eTransactionNeeded);
1026 return true;
1027}
1028
Vishnu Nair6213bd92020-05-08 17:42:25 -07001029bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001030 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001031 return false;
1032 }
1033
Robert Carr6a160312021-05-17 12:08:20 -07001034 mDrawingState.sequence++;
1035 mDrawingState.fixedTransformHint = fixedTransformHint;
1036 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001037 setTransactionFlags(eTransactionNeeded);
1038 return true;
1039}
1040
John Reckcdb4ed72021-02-04 13:39:33 -05001041bool Layer::setStretchEffect(const StretchEffect& effect) {
1042 StretchEffect temp = effect;
1043 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001044 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001045 return false;
1046 }
Robert Carr6a160312021-05-17 12:08:20 -07001047 mDrawingState.sequence++;
1048 mDrawingState.stretchEffect = temp;
1049 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001050 setTransactionFlags(eTransactionNeeded);
1051 return true;
1052}
1053
John Reckc00c6692021-02-16 11:37:33 -05001054StretchEffect Layer::getStretchEffect() const {
1055 if (mDrawingState.stretchEffect.hasEffect()) {
1056 return mDrawingState.stretchEffect;
1057 }
1058
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001059 sp<Layer> parent = getParent();
John Reckc00c6692021-02-16 11:37:33 -05001060 if (parent != nullptr) {
1061 auto effect = parent->getStretchEffect();
1062 if (effect.hasEffect()) {
1063 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1064 return effect;
1065 }
1066 }
1067 return StretchEffect{};
1068}
1069
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001070bool Layer::enableBorder(bool shouldEnable, float width, const half4& color) {
1071 if (mBorderEnabled == shouldEnable && mBorderWidth == width && mBorderColor == color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00001072 return false;
1073 }
1074 mBorderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001075 mBorderWidth = width;
1076 mBorderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001077 return true;
1078}
1079
1080bool Layer::isBorderEnabled() {
1081 return mBorderEnabled;
1082}
1083
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001084float Layer::getBorderWidth() {
1085 return mBorderWidth;
1086}
1087
1088const half4& Layer::getBorderColor() {
1089 return mBorderColor;
1090}
1091
Ady Abrahama850c182021-08-04 13:04:37 -07001092bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1093 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1094 const auto frameRate = [&] {
1095 if (mDrawingState.frameRate.rate.isValid() ||
1096 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1097 return mDrawingState.frameRate;
1098 }
1099
1100 return parentFrameRate;
1101 }();
1102
1103 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1104
1105 // The frame rate is propagated to the children
1106 bool childrenHaveFrameRate = false;
1107 for (const sp<Layer>& child : mCurrentChildren) {
1108 childrenHaveFrameRate |=
1109 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1110 }
1111
1112 // If we don't have a valid frame rate, but the children do, we set this
1113 // layer as NoVote to allow the children to control the refresh rate
1114 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1115 childrenHaveFrameRate) {
1116 *transactionNeeded |=
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001117 setFrameRateForLayerTree(FrameRate(Fps(), FrameRateCompatibility::NoVote));
Ady Abrahama850c182021-08-04 13:04:37 -07001118 }
1119
1120 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1121 // the same reason we are allowing touch boost for those layers. See
Ady Abraham68636062022-11-16 17:07:25 -08001122 // RefreshRateSelector::rankFrameRates for details.
Ady Abrahama850c182021-08-04 13:04:37 -07001123 const auto layerVotedWithDefaultCompatibility =
1124 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1125 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1126 const auto layerVotedWithExactCompatibility =
1127 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1128 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1129 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1130}
1131
Ady Abraham60e42ea2020-03-09 19:17:31 -07001132void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001133 const auto root = [&]() -> sp<Layer> {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001134 sp<Layer> layer = sp<Layer>::fromExisting(this);
Ady Abrahama850c182021-08-04 13:04:37 -07001135 while (auto parent = layer->getParent()) {
1136 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001137 }
Ady Abrahama850c182021-08-04 13:04:37 -07001138 return layer;
1139 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001140
Ady Abraham60e42ea2020-03-09 19:17:31 -07001141 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001142 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001143
Ady Abrahama850c182021-08-04 13:04:37 -07001144 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001145 if (transactionNeeded) {
1146 mFlinger->setTransactionFlags(eTraversalNeeded);
1147 }
1148}
1149
Ady Abraham71c437d2020-01-31 15:56:57 -08001150bool Layer::setFrameRate(FrameRate frameRate) {
Robert Carr6a160312021-05-17 12:08:20 -07001151 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001152 return false;
1153 }
1154
Robert Carr6a160312021-05-17 12:08:20 -07001155 mDrawingState.sequence++;
1156 mDrawingState.frameRate = frameRate;
1157 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001158
1159 updateTreeHasFrameRateVote();
1160
Steven Thomas3172e202020-01-06 19:25:30 -08001161 setTransactionFlags(eTransactionNeeded);
1162 return true;
1163}
1164
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001165void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1166 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001167 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001168
1169 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1170 // there are two transactions with the same token, the first one without a buffer and the
1171 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1172 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001173 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1174 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001175 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001176 mDrawingState.bufferSurfaceFrameTX = it->second;
1177 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1178 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1179 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001180 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001181 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001182 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1183 }
1184}
1185
1186void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1187 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001188 mDrawingState.frameTimelineInfo = info;
1189 mDrawingState.postTime = postTime;
1190 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001191 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001192
Robert Carr6a160312021-05-17 12:08:20 -07001193 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001194 bufferSurfaceFrameTX != nullptr) {
1195 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1196 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1197 // being used for BufferSurfaceFrame, don't create a new one.
1198 return;
1199 }
1200 }
1201 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1202 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1203 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001204 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1205 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001206 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001207 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001208 } else {
1209 if (it->second->getPresentState() == PresentState::Presented) {
1210 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1211 // have been from previous vsync.
1212 it->second = createSurfaceFrameForTransaction(info, postTime);
1213 }
1214 }
1215}
1216
1217void Layer::addSurfaceFrameDroppedForBuffer(
1218 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001219 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001220 surfaceFrame->setPresentState(PresentState::Dropped);
1221 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1222}
1223
1224void Layer::addSurfaceFramePresentedForBuffer(
1225 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1226 nsecs_t currentLatchTime) {
1227 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1228 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1229 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1230 mLastLatchTime = currentLatchTime;
1231}
1232
1233std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1234 const FrameTimelineInfo& info, nsecs_t postTime) {
1235 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001236 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1237 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001238 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001239 /*isBuffer*/ false, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001240 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001241 // For Transactions, the post time is considered to be both queue and acquire fence time.
1242 surfaceFrame->setActualQueueTime(postTime);
1243 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001244 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1245 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001246 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001247 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001248 onSurfaceFrameCreated(surfaceFrame);
1249 return surfaceFrame;
1250}
1251
1252std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1253 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1254 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001255 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001256 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001257 /*isBuffer*/ true, getGameMode());
Rachel Leeed511ef2021-10-11 15:09:51 -07001258 surfaceFrame->setActualStartTime(info.startTimeNanos);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001259 // For buffers, acquire fence time will set during latch.
1260 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001261 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1262 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001263 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001264 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001265 onSurfaceFrameCreated(surfaceFrame);
1266 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001267}
1268
Ady Abrahama850c182021-08-04 13:04:37 -07001269bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1270 if (mDrawingState.frameRateForLayerTree == frameRate) {
1271 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001272 }
1273
Ady Abrahama850c182021-08-04 13:04:37 -07001274 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001275
Ady Abrahama850c182021-08-04 13:04:37 -07001276 // TODO(b/195668952): we probably don't need to dirty visible regions here
1277 // or even store frameRateForLayerTree in mDrawingState
1278 mDrawingState.sequence++;
1279 mDrawingState.modified = true;
1280 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001281
Dominik Laskowski068173d2021-08-11 17:22:59 -07001282 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
1283 mFlinger->mScheduler->recordLayerHistory(this, systemTime(), LayerUpdateType::SetFrameRate);
Ady Abrahama850c182021-08-04 13:04:37 -07001284
1285 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001286}
1287
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001288Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1289 return getDrawingState().frameRateForLayerTree;
1290}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291
Robert Carr1f0a16a2016-10-24 16:27:39 -07001292bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001293 const State& s(mDrawingState);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001294 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001295 if (parent != nullptr && parent->isHiddenByPolicy()) {
1296 return true;
1297 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001298 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1299 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1300 if (zOrderRelativeOf != nullptr) {
1301 if (zOrderRelativeOf->isHiddenByPolicy()) {
1302 return true;
1303 }
1304 }
1305 }
Garfield Tan2c1782c2022-02-16 15:25:05 -08001306 if (CC_UNLIKELY(!isTransformValid())) {
1307 ALOGW("Hide layer %s because it has invalid transformation.", getDebugName());
1308 return true;
1309 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001310 return s.flags & layer_state_t::eLayerHidden;
1311}
1312
David Sodman41fdfc92017-11-06 16:09:56 -08001313uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001314 // TODO: should we do something special if mSecure is set?
1315 if (mProtectedByApp) {
1316 // need a hardware-protected path to external video sink
1317 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001318 }
Riley Andrews03414a12014-07-01 14:22:59 -07001319 if (mPotentialCursor) {
1320 usage |= GraphicBuffer::USAGE_CURSOR;
1321 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001322 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001323 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001324}
1325
Vishnu Nair71fcf912022-10-18 09:14:20 -07001326void Layer::skipReportingTransformHint() {
1327 mSkipReportingTransformHint = true;
1328}
1329
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001330void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1331 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1332 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001333 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001334
Vishnu Nair6213bd92020-05-08 17:42:25 -07001335 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001336}
1337
Mathias Agopian13127d82013-03-05 17:47:11 -08001338// ----------------------------------------------------------------------------
1339// debugging
1340// ----------------------------------------------------------------------------
1341
Marissa Wall61c58622018-07-18 10:12:20 -07001342// TODO(marissaw): add new layer state info to layer debugging
Huihong Luo05539a12022-02-23 10:29:40 -08001343gui::LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001344 using namespace std::string_literals;
1345
Huihong Luo05539a12022-02-23 10:29:40 -08001346 gui::LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001347 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001348 info.mName = getName();
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001349 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001350 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001351 info.mType = getType();
Lloyd Piquea2468662019-03-07 21:31:06 -08001352
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001353 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001354 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001355 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001356 info.mX = ds.transform.tx();
1357 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001358 info.mZ = ds.z;
chaviw25714502021-02-11 10:01:08 -08001359 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001360 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001361 info.mFlags = ds.flags;
1362 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001363 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001364 info.mMatrix[0][0] = ds.transform[0][0];
1365 info.mMatrix[0][1] = ds.transform[0][1];
1366 info.mMatrix[1][0] = ds.transform[1][0];
1367 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001368 {
chaviwd62d3062019-09-04 14:48:02 -07001369 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001370 if (buffer != 0) {
1371 info.mActiveBufferWidth = buffer->getWidth();
1372 info.mActiveBufferHeight = buffer->getHeight();
1373 info.mActiveBufferStride = buffer->getStride();
1374 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001375 } else {
1376 info.mActiveBufferWidth = 0;
1377 info.mActiveBufferHeight = 0;
1378 info.mActiveBufferStride = 0;
1379 info.mActiveBufferFormat = 0;
1380 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001381 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001382 info.mNumQueuedFrames = getQueuedFrameCount();
Kalle Raitaa099a242017-01-11 11:17:29 -08001383 info.mIsOpaque = isOpaque(ds);
1384 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001385 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001386 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001387}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001388
Yiwei Zhang5434a782018-12-05 18:06:32 -08001389void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001390 result.append(kDumpTableRowLength, '-');
1391 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001392 result.append(" Layer name\n");
1393 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001394 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001395 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001396 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001397 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001398 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001399 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1400 result.append(kDumpTableRowLength, '-');
1401 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001402}
1403
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001404void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1405 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001406 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001407 return;
1408 }
1409
Yiwei Zhang5434a782018-12-05 18:06:32 -08001410 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001411 if (mName.length() > 77) {
1412 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001413 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001414 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001415 shortened.append(mName, mName.length() - 36);
1416 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001417 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001418 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001419 }
1420
Yiwei Zhang5434a782018-12-05 18:06:32 -08001421 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001422
Alec Mourib416efd2018-09-06 21:01:59 +00001423 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001424 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001425
Chia-I Wu1e043612018-03-01 09:45:09 -08001426 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001427 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001428 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001429 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001430 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001431 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001432 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001433 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1434 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001435 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001436 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001437 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001438 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001439 const auto frameRate = getFrameRateForLayerTree();
1440 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1441 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001442 ftl::enum_string(frameRate.type).c_str(),
1443 ftl::enum_string(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001444 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001445 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001446 }
Dan Stozae22aec72016-08-01 13:20:59 -07001447
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001448 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1449 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1450
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001451 result.append(kDumpTableRowLength, '-');
1452 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001453}
Dan Stozae22aec72016-08-01 13:20:59 -07001454
Yiwei Zhang5434a782018-12-05 18:06:32 -08001455void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001456 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001457}
1458
Svetoslavd85084b2014-03-20 10:28:31 -07001459void Layer::clearFrameStats() {
1460 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001461}
1462
Jamie Gennis6547ff42013-07-16 20:12:42 -07001463void Layer::logFrameStats() {
1464 mFrameTracker.logAndResetStats(mName);
1465}
1466
Svetoslavd85084b2014-03-20 10:28:31 -07001467void Layer::getFrameStats(FrameStats* outStats) const {
1468 mFrameTracker.getStats(outStats);
1469}
1470
Vishnu Nair76554eb2022-12-09 03:38:36 +00001471void Layer::dumpOffscreenDebugInfo(std::string& result) const {
1472 std::string hasBuffer = hasBufferOrSidebandStream() ? " (contains buffer)" : "";
1473 StringAppendF(&result, "Layer %s%s pid:%d uid:%d\n", getName().c_str(), hasBuffer.c_str(),
Vishnu Naircb8be502022-10-12 19:03:23 +00001474 mOwnerPid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001475}
1476
Brian Anderson5ea5e592016-12-01 16:54:33 -08001477void Layer::onDisconnect() {
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001478 const int32_t layerId = getSequence();
1479 mFlinger->mTimeStats->onDestroy(layerId);
1480 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001481}
1482
Chia-I Wu98f1c102017-05-30 14:54:08 -07001483size_t Layer::getChildrenCount() const {
1484 size_t count = 0;
1485 for (const sp<Layer>& child : mCurrentChildren) {
1486 count += 1 + child->getChildrenCount();
1487 }
1488 return count;
1489}
1490
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001491void Layer::setGameModeForTree(GameMode gameMode) {
1492 const auto& currentState = getDrawingState();
Huihong Luod3d8f8e2022-03-08 14:48:46 -08001493 if (currentState.metadata.has(gui::METADATA_GAME_MODE)) {
1494 gameMode =
1495 static_cast<GameMode>(currentState.metadata.getInt32(gui::METADATA_GAME_MODE, 0));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001496 }
1497 setGameMode(gameMode);
1498 for (const sp<Layer>& child : mCurrentChildren) {
1499 child->setGameModeForTree(gameMode);
1500 }
1501}
1502
Robert Carr1f0a16a2016-10-24 16:27:39 -07001503void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001504 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001505 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001506
Robert Carr1f0a16a2016-10-24 16:27:39 -07001507 mCurrentChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07001508 layer->setParent(sp<Layer>::fromExisting(this));
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001509 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001510 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001511}
1512
1513ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001514 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001515 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001516
Robert Carr1323c952019-01-28 18:13:27 -08001517 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001518 const auto removeResult = mCurrentChildren.remove(layer);
1519
1520 updateTreeHasFrameRateVote();
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07001521 layer->setGameModeForTree(GameMode::Unsupported);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001522 layer->updateTreeHasFrameRateVote();
1523
1524 return removeResult;
1525}
1526
Robert Carr15eae092018-03-23 13:43:53 -07001527void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001528 for (const sp<Layer>& child : mDrawingChildren) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001529 child->mDrawingParent = newParent;
xinying1f4200e62022-04-26 14:41:30 +08001530 const float parentShadowRadius =
1531 newParent->canDrawShadows() ? 0.f : newParent->mEffectiveShadowRadius;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001532 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
xinying1f4200e62022-04-26 14:41:30 +08001533 parentShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001534 }
1535}
1536
chaviwf1961f72017-09-18 16:41:07 -07001537bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001538 sp<Layer> newParent;
1539 if (newParentHandle != nullptr) {
Vishnu Nair07e2a482022-10-18 19:18:16 +00001540 newParent = LayerHandle::getLayer(newParentHandle);
Robert Carr54cf5b12019-01-25 14:02:28 -08001541 if (newParent == nullptr) {
1542 ALOGE("Unable to promote Layer handle");
1543 return false;
1544 }
1545 if (newParent == this) {
1546 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1547 return false;
1548 }
1549 }
1550
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001551 sp<Layer> parent = getParent();
chaviwf1961f72017-09-18 16:41:07 -07001552 if (parent != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001553 parent->removeChild(sp<Layer>::fromExisting(this));
chaviw06178942017-07-27 10:25:59 -07001554 }
1555
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001556 if (newParentHandle != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07001557 newParent->addChild(sp<Layer>::fromExisting(this));
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001558 if (!newParent->isRemovedFromCurrentState()) {
1559 addToCurrentState();
1560 } else {
1561 onRemovedFromCurrentState();
1562 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001563 } else {
1564 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001565 }
1566
chaviw06178942017-07-27 10:25:59 -07001567 return true;
1568}
1569
Peiyong Lind3788632018-09-18 16:01:31 -07001570bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001571 static const mat4 identityMatrix = mat4();
1572
Robert Carr6a160312021-05-17 12:08:20 -07001573 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001574 return false;
1575 }
Robert Carr6a160312021-05-17 12:08:20 -07001576 ++mDrawingState.sequence;
1577 mDrawingState.colorTransform = matrix;
1578 mDrawingState.hasColorTransform = matrix != identityMatrix;
1579 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001580 setTransactionFlags(eTransactionNeeded);
1581 return true;
1582}
1583
chaviwf66724d2018-11-28 16:35:21 -08001584mat4 Layer::getColorTransform() const {
1585 mat4 colorTransform = mat4(getDrawingState().colorTransform);
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001586 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001587 colorTransform = parent->getColorTransform() * colorTransform;
1588 }
1589 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001590}
1591
1592bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001593 bool hasColorTransform = getDrawingState().hasColorTransform;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001594 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
chaviwf66724d2018-11-28 16:35:21 -08001595 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1596 }
1597 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001598}
1599
Chia-I Wu11481472018-05-04 10:43:19 -07001600bool Layer::isLegacyDataSpace() const {
1601 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001602 return !(getDataSpace() &
1603 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1604 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001605}
1606
Robert Carr1f0a16a2016-10-24 16:27:39 -07001607void Layer::setParent(const sp<Layer>& layer) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001608 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609}
1610
Robert Carr6a160312021-05-17 12:08:20 -07001611int32_t Layer::getZ(LayerVector::StateSet) const {
1612 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001613}
1614
Robert Carr1c5481e2019-07-01 14:42:27 -07001615bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001616 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001617 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001618 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001619}
1620
David Sodman41fdfc92017-11-06 16:09:56 -08001621__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001622 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001623 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1624 "makeTraversalList received invalid stateSet");
1625 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1626 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001627 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001628
Robert Carr29abff82017-12-04 13:51:20 -08001629 if (state.zOrderRelatives.size() == 0) {
1630 *outSkipRelativeZUsers = true;
1631 return children;
1632 }
1633
chaviwfd462612018-05-31 16:11:27 -07001634 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001635 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001636 sp<Layer> strongRelative = weakRelative.promote();
1637 if (strongRelative != nullptr) {
1638 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001639 }
1640 }
1641
Dan Stoza412903f2017-04-27 13:42:17 -07001642 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001643 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001644 continue;
1645 }
Robert Carrdb66e622017-04-10 16:55:57 -07001646 traverse.add(child);
1647 }
1648
1649 return traverse;
1650}
1651
Robert Carr1f0a16a2016-10-24 16:27:39 -07001652/**
Robert Carrdb66e622017-04-10 16:55:57 -07001653 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 */
Dan Stoza412903f2017-04-27 13:42:17 -07001655void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001656 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1657 // produce a new list for traversing, including our relatives, and not including our children
1658 // who are relatives of another surface. In the case that there are no relative Z,
1659 // makeTraversalList returns our children directly to avoid significant overhead.
1660 // However in this case we need to take the responsibility for filtering children which
1661 // are relatives of another surface here.
1662 bool skipRelativeZUsers = false;
1663 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001664
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001666 for (; i < list.size(); i++) {
1667 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001668 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1669 continue;
1670 }
1671
chaviw301b1d82019-11-06 13:15:09 -08001672 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001674 }
Dan Stoza412903f2017-04-27 13:42:17 -07001675 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 }
Robert Carr29abff82017-12-04 13:51:20 -08001677
Dan Stoza412903f2017-04-27 13:42:17 -07001678 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001679 for (; i < list.size(); i++) {
1680 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001681
Robert Carr29abff82017-12-04 13:51:20 -08001682 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1683 continue;
1684 }
Dan Stoza412903f2017-04-27 13:42:17 -07001685 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001686 }
1687}
1688
1689/**
Robert Carrdb66e622017-04-10 16:55:57 -07001690 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001691 */
Dan Stoza412903f2017-04-27 13:42:17 -07001692void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1693 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001694 // See traverseInZOrder for documentation.
1695 bool skipRelativeZUsers = false;
1696 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001697
Robert Carr1f0a16a2016-10-24 16:27:39 -07001698 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001699 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001700 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001701
1702 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1703 continue;
1704 }
1705
chaviw301b1d82019-11-06 13:15:09 -08001706 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001707 break;
1708 }
Dan Stoza412903f2017-04-27 13:42:17 -07001709 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710 }
Dan Stoza412903f2017-04-27 13:42:17 -07001711 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001712 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001713 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001714
1715 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1716 continue;
1717 }
1718
Dan Stoza412903f2017-04-27 13:42:17 -07001719 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001720 }
1721}
1722
Edgar Arriaga844fa672020-01-16 14:21:42 -08001723void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1724 visitor(this);
1725 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001726 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001727 for (const sp<Layer>& child : children) {
1728 child->traverse(state, visitor);
1729 }
1730}
1731
chaviw4b129c22018-04-09 16:19:43 -07001732LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1733 const std::vector<Layer*>& layersInTree) {
1734 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1735 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001736 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1737 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001738 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001739
chaviwfd462612018-05-31 16:11:27 -07001740 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001741 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1742 sp<Layer> strongRelative = weakRelative.promote();
1743 // Only add relative layers that are also descendents of the top most parent of the tree.
1744 // If a relative layer is not a descendent, then it should be ignored.
1745 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1746 traverse.add(strongRelative);
1747 }
1748 }
1749
1750 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001751 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001752 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1753 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1754 // the child here since it won't be added later as a relative.
1755 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1756 childState.zOrderRelativeOf.promote().get())) {
1757 continue;
1758 }
1759 traverse.add(child);
1760 }
1761
1762 return traverse;
1763}
1764
1765void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1766 LayerVector::StateSet stateSet,
1767 const LayerVector::Visitor& visitor) {
1768 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001769
1770 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001771 for (; i < list.size(); i++) {
1772 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001773 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001774 break;
1775 }
chaviw4b129c22018-04-09 16:19:43 -07001776 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001777 }
chaviw4b129c22018-04-09 16:19:43 -07001778
chaviwa76b2712017-09-20 12:02:26 -07001779 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001780 for (; i < list.size(); i++) {
1781 const auto& relative = list[i];
1782 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001783 }
1784}
1785
chaviw4b129c22018-04-09 16:19:43 -07001786std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1787 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1788 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1789
1790 std::vector<Layer*> layersInTree = {this};
1791 for (size_t i = 0; i < children.size(); i++) {
1792 const auto& child = children[i];
1793 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1794 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1795 }
1796
1797 return layersInTree;
1798}
1799
1800void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1801 const LayerVector::Visitor& visitor) {
1802 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1803 std::sort(layersInTree.begin(), layersInTree.end());
1804 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1805}
1806
Peiyong Linefefaac2018-08-17 12:27:51 -07001807ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001808 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809}
1810
Garfield Tan2c1782c2022-02-16 15:25:05 -08001811bool Layer::isTransformValid() const {
1812 float transformDet = getTransform().det();
1813 return transformDet != 0 && !isinf(transformDet) && !isnan(transformDet);
1814}
1815
chaviw13fdc492017-06-27 12:40:18 -07001816half Layer::getAlpha() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001817 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001818
chaviw13fdc492017-06-27 12:40:18 -07001819 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1820 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001821}
Robert Carr6452f122017-03-21 10:41:29 -07001822
Vishnu Nair6213bd92020-05-08 17:42:25 -07001823ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001824 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001825 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1826 return fixedTransformHint;
1827 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001828 const auto& p = mCurrentParent.promote();
Vishnu Nair6213bd92020-05-08 17:42:25 -07001829 if (!p) return fixedTransformHint;
1830 return p->getFixedTransformHint();
1831}
1832
chaviw13fdc492017-06-27 12:40:18 -07001833half4 Layer::getColor() const {
1834 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001835 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001836}
Robert Carr6452f122017-03-21 10:41:29 -07001837
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001838int32_t Layer::getBackgroundBlurRadius() const {
Vishnu Nair29810f72022-07-01 16:38:41 +00001839 if (getDrawingState().backgroundBlurRadius == 0) {
1840 return 0;
1841 }
Galia Peychevada7de0e2020-12-03 17:24:35 +01001842
Vishnu Nair29810f72022-07-01 16:38:41 +00001843 const auto& p = mDrawingParent.promote();
Galia Peychevada7de0e2020-12-03 17:24:35 +01001844 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1845 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001846}
1847
Galia Peychevae0acf382021-04-12 21:22:34 +02001848const std::vector<BlurRegion> Layer::getBlurRegions() const {
1849 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001850 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001851 for (auto& region : regionsCopy) {
1852 region.alpha = region.alpha * layerAlpha;
1853 }
1854 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001855}
1856
Vishnu Naire14c6b32022-08-06 04:20:15 +00001857RoundedCornerState Layer::getRoundedCornerState() const {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001858 // Get parent settings
1859 RoundedCornerState parentSettings;
Rob Carrc6d2d2b2021-10-25 16:51:49 +00001860 const auto& parent = mDrawingParent.promote();
1861 if (parent != nullptr) {
1862 parentSettings = parent->getRoundedCornerState();
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001863 if (parentSettings.hasRoundedCorners()) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001864 ui::Transform t = getActiveTransform(getDrawingState());
1865 t = t.inverse();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001866 parentSettings.cropRect = t.transform(parentSettings.cropRect);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001867 parentSettings.radius.x *= t.getScaleX();
1868 parentSettings.radius.y *= t.getScaleY();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001869 }
1870 }
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001871
1872 // Get layer settings
1873 Rect layerCropRect = getCroppedBufferSize(getDrawingState());
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001874 const vec2 radius(getDrawingState().cornerRadius, getDrawingState().cornerRadius);
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001875 RoundedCornerState layerSettings(layerCropRect.toFloatRect(), radius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001876 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && layerCropRect.isValid();
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001877
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001878 if (layerSettingsValid && parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001879 // If the parent and the layer have rounded corner settings, use the parent settings if the
1880 // parent crop is entirely inside the layer crop.
1881 // This has limitations and cause rendering artifacts. See b/200300845 for correct fix.
1882 if (parentSettings.cropRect.left > layerCropRect.left &&
1883 parentSettings.cropRect.top > layerCropRect.top &&
1884 parentSettings.cropRect.right < layerCropRect.right &&
1885 parentSettings.cropRect.bottom < layerCropRect.bottom) {
1886 return parentSettings;
1887 } else {
1888 return layerSettings;
1889 }
Vishnu Nairb1845592021-10-07 12:17:57 -07001890 } else if (layerSettingsValid) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001891 return layerSettings;
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001892 } else if (parentSettings.hasRoundedCorners()) {
Vishnu Nair9dbf8e82021-09-16 16:24:47 -07001893 return parentSettings;
1894 }
1895 return {};
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001896}
1897
Robert Carr88b85e12022-03-21 15:47:35 -07001898bool Layer::findInHierarchy(const sp<Layer>& l) {
1899 if (l == this) {
1900 return true;
1901 }
1902 for (auto& child : mDrawingChildren) {
1903 if (child->findInHierarchy(l)) {
1904 return true;
1905 }
1906 }
1907 return false;
1908}
1909
Robert Carr1f0a16a2016-10-24 16:27:39 -07001910void Layer::commitChildList() {
1911 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1912 const auto& child = mCurrentChildren[i];
1913 child->commitChildList();
1914 }
1915 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001916 mDrawingParent = mCurrentParent;
Robert Carr88b85e12022-03-21 15:47:35 -07001917 if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) {
1918 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1919 if (zOrderRelativeOf == nullptr) return;
1920 if (findInHierarchy(zOrderRelativeOf)) {
1921 ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(),
1922 zOrderRelativeOf->mName.c_str());
1923 ALOGE("Severing rel Z loop, potentially dangerous");
1924 mDrawingState.isRelativeOf = false;
Ady Abrahamd11bade2022-08-01 16:18:03 -07001925 zOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this));
Robert Carr88b85e12022-03-21 15:47:35 -07001926 }
1927 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001928}
1929
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001930
chaviw3277faf2021-05-19 16:45:23 -05001931void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001932 mDrawingState.inputInfo = info;
Vishnu Nair07e2a482022-10-18 19:18:16 +00001933 mDrawingState.touchableRegionCrop =
1934 LayerHandle::getLayer(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07001935 mDrawingState.modified = true;
Arthur Hung9ed43392022-05-27 06:31:57 +00001936 mFlinger->mUpdateInputInfo = true;
Robert Carr720e5062018-07-30 17:45:14 -07001937 setTransactionFlags(eTransactionNeeded);
1938}
1939
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001940LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) {
chaviw08f3cb22020-01-13 13:17:21 -08001941 LayerProto* layerProto = layersProto.add_layers();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001942 writeToProtoDrawingState(layerProto);
chaviw08f3cb22020-01-13 13:17:21 -08001943 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1944
Vishnu Nair00b90132021-11-05 14:03:40 -07001945 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001946 ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread.
1947
Vishnu Nair60db8c02020-04-02 11:55:16 -07001948 // Only populate for the primary display.
Dominik Laskowski298b08e2022-02-15 13:45:02 -08001949 if (const auto display = mFlinger->getDefaultDisplayDeviceLocked()) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05001950 const auto compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07001951 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001952 LayerProtoHelper::writeToProto(getVisibleRegion(display.get()),
1953 [&]() { return layerProto->mutable_visible_region(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07001954 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08001955 }
1956
chaviw08f3cb22020-01-13 13:17:21 -08001957 for (const sp<Layer>& layer : mDrawingChildren) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001958 layer->writeToProto(layersProto, traceFlags);
chaviw08f3cb22020-01-13 13:17:21 -08001959 }
chaviw6d89e2d2020-01-14 14:42:01 -08001960
1961 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08001962}
1963
Vishnu Naird8f5e9f2022-02-03 10:23:28 -08001964void Layer::writeToProtoDrawingState(LayerProto* layerInfo) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001965 const ui::Transform transform = getTransform();
Vishnu Naird37343b2022-01-12 16:18:56 -08001966 auto buffer = getExternalTexture();
Vishnu Nair00b90132021-11-05 14:03:40 -07001967 if (buffer != nullptr) {
Vishnu Naird37343b2022-01-12 16:18:56 -08001968 LayerProtoHelper::writeToProto(*buffer,
Vishnu Nair00b90132021-11-05 14:03:40 -07001969 [&]() { return layerInfo->mutable_active_buffer(); });
1970 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(getBufferTransform()),
1971 layerInfo->mutable_buffer_transform());
1972 }
1973 layerInfo->set_invalidate(contentDirty);
1974 layerInfo->set_is_protected(isProtected());
1975 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
1976 layerInfo->set_queued_frames(getQueuedFrameCount());
Vishnu Nair00b90132021-11-05 14:03:40 -07001977 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nair00b90132021-11-05 14:03:40 -07001978 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nair50c0afe2022-07-11 15:04:07 -07001979 layerInfo->set_corner_radius(
1980 (getRoundedCornerState().radius.x + getRoundedCornerState().radius.y) / 2.0);
Vishnu Nair00b90132021-11-05 14:03:40 -07001981 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
1982 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
1983 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
1984 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1985 [&]() { return layerInfo->mutable_position(); });
1986 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair00b90132021-11-05 14:03:40 -07001987 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1988 [&]() { return layerInfo->mutable_damage_region(); });
Vishnu Nair8406fd72019-07-30 11:29:31 -07001989
Vishnu Nair00b90132021-11-05 14:03:40 -07001990 if (hasColorTransform()) {
1991 LayerProtoHelper::writeToProto(getColorTransform(), layerInfo->mutable_color_transform());
Vishnu Nair8406fd72019-07-30 11:29:31 -07001992 }
1993
Vishnu Nair60db8c02020-04-02 11:55:16 -07001994 LayerProtoHelper::writeToProto(mSourceBounds,
1995 [&]() { return layerInfo->mutable_source_bounds(); });
1996 LayerProtoHelper::writeToProto(mScreenBounds,
1997 [&]() { return layerInfo->mutable_screen_bounds(); });
1998 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
1999 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2000 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002001}
2002
2003void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002004 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002005 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2006 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002007 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002008
chaviw766c9c52021-02-10 17:36:47 -08002009 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002010
Vishnu Nair00b90132021-11-05 14:03:40 -07002011 layerInfo->set_id(sequence);
2012 layerInfo->set_name(getName().c_str());
2013 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002014
Vishnu Nair00b90132021-11-05 14:03:40 -07002015 for (const auto& child : children) {
2016 layerInfo->add_children(child->sequence);
chaviwadc40c22018-07-10 16:57:27 -07002017 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002018
Vishnu Nair00b90132021-11-05 14:03:40 -07002019 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2020 sp<Layer> strongRelative = weakRelative.promote();
2021 if (strongRelative != nullptr) {
2022 layerInfo->add_relatives(strongRelative->sequence);
2023 }
2024 }
2025
Vishnu Nairea04b6f2022-08-19 21:28:17 +00002026 LayerProtoHelper::writeToProto(state.transparentRegionHint,
Vishnu Nair00b90132021-11-05 14:03:40 -07002027 [&]() { return layerInfo->mutable_transparent_region(); });
2028
2029 layerInfo->set_layer_stack(getLayerStack().id);
2030 layerInfo->set_z(state.z);
2031
2032 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2033 return layerInfo->mutable_requested_position();
2034 });
2035
Vishnu Nair00b90132021-11-05 14:03:40 -07002036 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
2037
2038 layerInfo->set_is_opaque(isOpaque(state));
2039
2040 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2041 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2042 LayerProtoHelper::writeToProto(state.color,
2043 [&]() { return layerInfo->mutable_requested_color(); });
2044 layerInfo->set_flags(state.flags);
2045
2046 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
2047 layerInfo->mutable_requested_transform());
2048
2049 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2050 if (parent != nullptr) {
2051 layerInfo->set_parent(parent->sequence);
2052 } else {
2053 layerInfo->set_parent(-1);
2054 }
2055
2056 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2057 if (zOrderRelativeOf != nullptr) {
2058 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2059 } else {
2060 layerInfo->set_z_order_relative_of(-1);
2061 }
2062
2063 layerInfo->set_is_relative_of(state.isRelativeOf);
2064
2065 layerInfo->set_owner_uid(mOwnerUid);
2066
Arthur Hung4cf2d8c2022-04-07 14:52:00 +00002067 if ((traceFlags & LayerTracing::TRACE_INPUT) && needsInputInfo()) {
chaviw3277faf2021-05-19 16:45:23 -05002068 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002069 if (useDrawing) {
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002070 info = fillInputInfo(
2071 InputDisplayArgs{.transform = &kIdentityTransform, .isSecure = true});
chaviw4c34a092020-07-08 11:30:06 -07002072 } else {
2073 info = state.inputInfo;
2074 }
2075
2076 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002077 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002078 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002079
Vishnu Nair00b90132021-11-05 14:03:40 -07002080 if (traceFlags & LayerTracing::TRACE_EXTRA) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002081 auto protoMap = layerInfo->mutable_metadata();
2082 for (const auto& entry : state.metadata.mMap) {
2083 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2084 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002085 }
Vishnu Naird2aaab12022-02-10 14:49:09 -08002086
2087 LayerProtoHelper::writeToProto(state.destinationFrame,
2088 [&]() { return layerInfo->mutable_destination_frame(); });
chaviw1d044282017-09-27 12:19:28 -07002089}
2090
Robert Carr2e102c92018-10-23 12:11:15 -07002091bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002092 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002093}
2094
Prabir Pradhan33da9462022-06-14 14:55:57 +00002095// Applies the given transform to the region, while protecting against overflows caused by any
2096// offsets. If applying the offset in the transform to any of the Rects in the region would result
2097// in an overflow, they are not added to the output Region.
2098static Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
2099 const std::string& debugWindowName) {
2100 // Round the translation using the same rounding strategy used by ui::Transform.
2101 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
2102 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
2103
2104 ui::Transform transformWithoutOffset = t;
2105 transformWithoutOffset.set(0.f, 0.f);
2106
2107 const Region transformed = transformWithoutOffset.transform(r);
2108
2109 // Apply the translation to each of the Rects in the region while discarding any that overflow.
2110 Region ret;
2111 for (const auto& rect : transformed) {
2112 Rect newRect;
2113 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
2114 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
2115 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
2116 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
2117 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
2118 debugWindowName.c_str());
2119 continue;
2120 }
2121 ret.orSelf(newRect);
2122 }
2123 return ret;
2124}
2125
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002126void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& screenToDisplay) {
2127 Rect tmpBounds = getInputBounds();
2128 if (!tmpBounds.isValid()) {
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08002129 info.touchableRegion.clear();
2130 // A layer could have invalid input bounds and still expect to receive touch input if it has
2131 // replaceTouchableRegionWithCrop. For that case, the input transform needs to be calculated
2132 // correctly to determine the coordinate space for input events. Use an empty rect so that
2133 // the layer will receive input in its own layer space.
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002134 tmpBounds = Rect::EMPTY_RECT;
chaviw7e72caf2020-12-02 16:50:43 -08002135 }
2136
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002137 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
2138 // frame and transform used for the layer, which determines the bounds and the coordinate space
2139 // within which the layer will receive input.
2140 //
2141 // The coordinate space within which each of the bounds are specified is explicitly documented
2142 // in the variable name. For example "inputBoundsInLayer" is specified in layer space. A
2143 // Transform converts one coordinate space to another, which is apparent in its naming. For
2144 // example, "layerToDisplay" transforms layer space to display space.
2145 //
2146 // Coordinate space definitions:
2147 // - display: The display device's coordinate space. Correlates to pixels on the display.
2148 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
2149 // - layer: The coordinate space of this layer.
2150 // - input: The coordinate space in which this layer will receive input events. This could be
2151 // different than layer space if a surfaceInset is used, which changes the origin
2152 // of the input space.
2153 const FloatRect inputBoundsInLayer = tmpBounds.toFloatRect();
chaviw7e72caf2020-12-02 16:50:43 -08002154
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002155 // Clamp surface inset to the input bounds.
2156 const auto surfaceInset = static_cast<float>(info.surfaceInset);
2157 const float xSurfaceInset =
2158 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getWidth() / 2.f));
2159 const float ySurfaceInset =
2160 std::max(0.f, std::min(surfaceInset, inputBoundsInLayer.getHeight() / 2.f));
chaviw1ff3d1e2020-07-01 15:53:47 -07002161
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002162 // Apply the insets to the input bounds.
2163 const FloatRect insetBoundsInLayer(inputBoundsInLayer.left + xSurfaceInset,
2164 inputBoundsInLayer.top + ySurfaceInset,
2165 inputBoundsInLayer.right - xSurfaceInset,
2166 inputBoundsInLayer.bottom - ySurfaceInset);
Ady Abraham282f1d72019-07-24 18:05:56 -07002167
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002168 // Crop the input bounds to ensure it is within the parent's bounds.
2169 const FloatRect croppedInsetBoundsInLayer = mBounds.intersect(insetBoundsInLayer);
Ady Abraham282f1d72019-07-24 18:05:56 -07002170
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002171 const ui::Transform layerToScreen = getInputTransform();
2172 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
Vishnu Nair8033a492018-12-05 07:27:23 -08002173
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002174 const Rect roundedFrameInDisplay{layerToDisplay.transform(croppedInsetBoundsInLayer)};
2175 info.frameLeft = roundedFrameInDisplay.left;
2176 info.frameTop = roundedFrameInDisplay.top;
2177 info.frameRight = roundedFrameInDisplay.right;
2178 info.frameBottom = roundedFrameInDisplay.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -07002179
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002180 ui::Transform inputToLayer;
2181 inputToLayer.set(insetBoundsInLayer.left, insetBoundsInLayer.top);
2182 const ui::Transform inputToDisplay = layerToDisplay * inputToLayer;
chaviw39cfa2e2020-11-04 14:19:02 -08002183
Prabir Pradhanc9589c12021-09-22 06:11:43 -07002184 // InputDispatcher expects a display-to-input transform.
2185 info.transform = inputToDisplay.inverse();
2186
2187 // The touchable region is specified in the input coordinate space. Change it to display space.
Prabir Pradhan33da9462022-06-14 14:55:57 +00002188 info.touchableRegion =
2189 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, mName);
chaviw7e72caf2020-12-02 16:50:43 -08002190}
2191
chaviw3277faf2021-05-19 16:45:23 -05002192void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002193 sp<Layer> p = sp<Layer>::fromExisting(this);
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002194 while (p != nullptr && !p->hasInputInfo()) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002195 p = p->mDrawingParent.promote();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002196 }
2197 if (p != nullptr) {
2198 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2199 }
2200}
2201
Vishnu Naira066d902021-09-13 18:40:17 -07002202gui::DropInputMode Layer::getDropInputMode() const {
2203 gui::DropInputMode mode = mDrawingState.dropInputMode;
2204 if (mode == gui::DropInputMode::ALL) {
2205 return mode;
2206 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002207 sp<Layer> parent = mDrawingParent.promote();
2208 if (parent) {
2209 gui::DropInputMode parentMode = parent->getDropInputMode();
Vishnu Naira066d902021-09-13 18:40:17 -07002210 if (parentMode != gui::DropInputMode::NONE) {
2211 return parentMode;
2212 }
2213 }
2214 return mode;
2215}
2216
2217void Layer::handleDropInputMode(gui::WindowInfo& info) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002218 if (mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Vishnu Naira066d902021-09-13 18:40:17 -07002219 return;
2220 }
2221
2222 // Check if we need to drop input unconditionally
2223 gui::DropInputMode dropInputMode = getDropInputMode();
2224 if (dropInputMode == gui::DropInputMode::ALL) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002225 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002226 ALOGV("Dropping input for %s as requested by policy.", getDebugName());
2227 return;
2228 }
2229
2230 // Check if we need to check if the window is obscured by parent
2231 if (dropInputMode != gui::DropInputMode::OBSCURED) {
2232 return;
2233 }
2234
2235 // Check if the parent has set an alpha on the layer
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002236 sp<Layer> parent = mDrawingParent.promote();
2237 if (parent && parent->getAlpha() != 1.0_hf) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002238 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002239 ALOGV("Dropping input for %s as requested by policy because alpha=%f", getDebugName(),
2240 static_cast<float>(getAlpha()));
2241 }
2242
2243 // Check if the parent has cropped the buffer
2244 Rect bufferSize = getCroppedBufferSize(getDrawingState());
2245 if (!bufferSize.isValid()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002246 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002247 return;
2248 }
2249
2250 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
2251 // To check if the layer has been cropped, we take the buffer bounds, apply the local
2252 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
2253 // match then the layer has not been cropped by its parents.
2254 Rect bufferInScreenSpace(getTransform().transform(bufferSize));
2255 bool croppedByParent = bufferInScreenSpace != Rect{mScreenBounds};
2256
2257 if (croppedByParent) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002258 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Naira066d902021-09-13 18:40:17 -07002259 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
2260 getDebugName());
2261 } else {
2262 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
2263 // input if the window is obscured. This check should be done in surfaceflinger but the
2264 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
2265 // drop input events if the window is obscured.
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002266 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
Vishnu Naira066d902021-09-13 18:40:17 -07002267 }
2268}
2269
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002270WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) {
chaviw7e72caf2020-12-02 16:50:43 -08002271 if (!hasInputInfo()) {
2272 mDrawingState.inputInfo.name = getName();
2273 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2274 mDrawingState.inputInfo.ownerPid = mOwnerPid;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002275 mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002276 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002277 }
2278
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002279 const ui::Transform& displayTransform =
2280 displayArgs.transform != nullptr ? *displayArgs.transform : kIdentityTransform;
2281
chaviw3277faf2021-05-19 16:45:23 -05002282 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002283 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002284 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002285
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002286 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002287
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002288 if (displayArgs.transform == nullptr) {
2289 // Do not let the window receive touches if it is not associated with a valid display
2290 // transform. We still allow the window to receive keys and prevent ANRs.
2291 info.inputConfig |= WindowInfo::InputConfig::NOT_TOUCHABLE;
2292 }
2293
Robert Carr5dc426e2020-06-10 14:29:14 -07002294 // For compatibility reasons we let layers which can receive input
2295 // receive input before they have actually submitted a buffer. Because
2296 // of this we use canReceiveInput instead of isVisible to check the
2297 // policy-visibility, ignoring the buffer state. However for layers with
2298 // hasInputInfo()==false we can use the real visibility state.
2299 // We are just using these layers for occlusion detection in
2300 // InputDispatcher, and obviously if they aren't visible they can't occlude
2301 // anything.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002302 const bool visible = hasInputInfo() ? canReceiveInput() : isVisible();
2303 info.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
2304
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002305 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002306 fillTouchOcclusionMode(info);
Vishnu Naira066d902021-09-13 18:40:17 -07002307 handleDropInputMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002308
Vishnu Nair16a938f2021-09-24 07:14:54 -07002309 // If the window will be blacked out on a display because the display does not have the secure
2310 // flag and the layer has the secure flag set, then drop input.
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00002311 if (!displayArgs.isSecure && isSecure()) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08002312 info.inputConfig |= WindowInfo::InputConfig::DROP_INPUT;
Vishnu Nair16a938f2021-09-24 07:14:54 -07002313 }
2314
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002315 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2316 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002317 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2318 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002319 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002320 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002321 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002322 }
2323
Winson Chunga30f7c92021-06-29 15:42:56 -07002324 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2325 // if it was set by WM for a known system overlay
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002326 if (isTrustedOverlay()) {
2327 info.inputConfig |= WindowInfo::InputConfig::TRUSTED_OVERLAY;
2328 }
Winson Chunga30f7c92021-06-29 15:42:56 -07002329
chaviwaf87b3e2019-10-01 16:59:28 -07002330 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2331 // touches from going outside the cloned area.
2332 if (isClone()) {
chaviw8577ea82022-06-01 16:32:26 -05002333 info.inputConfig |= WindowInfo::InputConfig::CLONE;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002334 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2335 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002336 info.touchableRegion = info.touchableRegion.intersect(rect);
2337 }
2338 }
2339
Robert Carr720e5062018-07-30 17:45:14 -07002340 return info;
2341}
2342
chaviwaf87b3e2019-10-01 16:59:28 -07002343sp<Layer> Layer::getClonedRoot() {
2344 if (mClonedChild != nullptr) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002345 return sp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002346 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002347 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
chaviwaf87b3e2019-10-01 16:59:28 -07002348 return nullptr;
2349 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002350 return mDrawingParent.promote()->getClonedRoot();
chaviwaf87b3e2019-10-01 16:59:28 -07002351}
2352
Robert Carredd13602020-04-13 17:24:34 -07002353bool Layer::hasInputInfo() const {
Linus Tufvessona1858822022-03-04 09:32:07 +00002354 return mDrawingState.inputInfo.token != nullptr ||
2355 mDrawingState.inputInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Robert Carr720e5062018-07-30 17:45:14 -07002356}
2357
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002358compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002359 const DisplayDevice* display) const {
2360 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002361 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002362}
2363
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002364Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2365 const auto outputLayer = findOutputLayerForDisplay(display);
2366 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002367}
2368
chaviwb4c6e582019-08-16 14:35:07 -07002369void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002370 cloneDrawingState(clonedFrom.get());
chaviwb4c6e582019-08-16 14:35:07 -07002371 mClonedFrom = clonedFrom;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002372 mPremultipliedAlpha = clonedFrom->mPremultipliedAlpha;
2373 mPotentialCursor = clonedFrom->mPotentialCursor;
2374 mProtectedByApp = clonedFrom->mProtectedByApp;
2375 updateCloneBufferInfo();
2376}
2377
2378void Layer::updateCloneBufferInfo() {
2379 if (!isClone() || !isClonedFromAlive()) {
2380 return;
2381 }
2382
2383 sp<Layer> clonedFrom = getClonedFrom();
2384 mBufferInfo = clonedFrom->mBufferInfo;
2385 mSidebandStream = clonedFrom->mSidebandStream;
2386 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
2387 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
2388 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
2389
2390 // After buffer info is updated, the drawingState from the real layer needs to be copied into
2391 // the cloned. This is because some properties of drawingState can change when latchBuffer is
2392 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
2393 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
2394 // the cloned drawingState again.
2395 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
2396 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
2397 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
2398 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
2399
2400 cloneDrawingState(clonedFrom.get());
2401
2402 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
2403 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
2404 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
2405 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -07002406}
chaviw74b03172019-08-19 11:09:03 -07002407
2408void Layer::updateMirrorInfo() {
2409 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2410 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2411 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2412 // that case, we want to delete the reference to the clone since we want it to get
2413 // destroyed. The root, this layer, will still be around since the client can continue
2414 // to hold a reference, but no cloned layers will be displayed.
2415 mClonedChild = nullptr;
2416 return;
2417 }
2418
2419 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2420 // If the real layer exists and is in current state, add the clone as a child of the root.
2421 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2422 // copied to drawingState for the root layer. So the clonedChild is always removed from
2423 // drawingState and then needs to be added back each traversal.
2424 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2425 addChildToDrawing(mClonedChild);
2426 }
2427
2428 mClonedChild->updateClonedDrawingState(clonedLayersMap);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002429 mClonedChild->updateClonedChildren(sp<Layer>::fromExisting(this), clonedLayersMap);
chaviw74b03172019-08-19 11:09:03 -07002430 mClonedChild->updateClonedRelatives(clonedLayersMap);
2431}
2432
2433void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2434 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2435 // to the clone. If the real layer is no longer alive, continue traversing the children
2436 // since we may be able to pull out other children that are still alive.
2437 if (isClonedFromAlive()) {
2438 sp<Layer> clonedFrom = getClonedFrom();
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002439 cloneDrawingState(clonedFrom.get());
Ady Abrahamd11bade2022-08-01 16:18:03 -07002440 clonedLayersMap.emplace(clonedFrom, sp<Layer>::fromExisting(this));
chaviw74b03172019-08-19 11:09:03 -07002441 }
2442
2443 // The clone layer may have children in drawingState since they may have been created and
2444 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2445 // that already exist, since we can just re-use them.
2446 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2447 // not updated in the regular traversal. They are skipped since the root will lose the
2448 // reference to them when it copies its currentChildren to drawing.
2449 for (sp<Layer>& child : mDrawingChildren) {
2450 child->updateClonedDrawingState(clonedLayersMap);
2451 }
2452}
2453
2454void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2455 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2456 mDrawingChildren.clear();
2457
2458 if (!isClonedFromAlive()) {
2459 return;
2460 }
2461
2462 sp<Layer> clonedFrom = getClonedFrom();
2463 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2464 if (child == mirrorRoot) {
2465 // This is to avoid cyclical mirroring.
2466 continue;
2467 }
2468 sp<Layer> clonedChild = clonedLayersMap[child];
2469 if (clonedChild == nullptr) {
2470 clonedChild = child->createClone();
2471 clonedLayersMap[child] = clonedChild;
2472 }
2473 addChildToDrawing(clonedChild);
2474 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2475 }
2476}
2477
chaviwaf87b3e2019-10-01 16:59:28 -07002478void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2479 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2480 if (cropLayer != nullptr) {
2481 if (clonedLayersMap.count(cropLayer) == 0) {
2482 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2483 // self as crop layer to avoid going outside bounds.
Ady Abrahamd11bade2022-08-01 16:18:03 -07002484 mDrawingState.touchableRegionCrop = wp<Layer>::fromExisting(this);
chaviwaf87b3e2019-10-01 16:59:28 -07002485 } else {
2486 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2487 mDrawingState.touchableRegionCrop = clonedCropLayer;
2488 }
2489 }
2490 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2491 // WM or the client.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002492 mDrawingState.inputInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, false);
chaviwaf87b3e2019-10-01 16:59:28 -07002493}
2494
2495void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
Ady Abrahamd11bade2022-08-01 16:18:03 -07002496 mDrawingState.zOrderRelativeOf = wp<Layer>();
chaviw74b03172019-08-19 11:09:03 -07002497 mDrawingState.zOrderRelatives.clear();
2498
2499 if (!isClonedFromAlive()) {
2500 return;
2501 }
2502
chaviwaf87b3e2019-10-01 16:59:28 -07002503 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002504 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002505 const sp<Layer>& relative = relativeWeak.promote();
2506 if (clonedLayersMap.count(relative) > 0) {
2507 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002508 mDrawingState.zOrderRelatives.add(clonedRelative);
2509 }
2510 }
2511
2512 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2513 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2514 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2515 // still traverse the children, but the layer with the missing relativeOf will not be shown
2516 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002517 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2518 if (clonedLayersMap.count(relativeOf) > 0) {
2519 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002520 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2521 }
2522
chaviwaf87b3e2019-10-01 16:59:28 -07002523 updateClonedInputInfo(clonedLayersMap);
2524
chaviw74b03172019-08-19 11:09:03 -07002525 for (sp<Layer>& child : mDrawingChildren) {
2526 child->updateClonedRelatives(clonedLayersMap);
2527 }
2528}
2529
2530void Layer::addChildToDrawing(const sp<Layer>& layer) {
2531 mDrawingChildren.add(layer);
Ady Abrahamd11bade2022-08-01 16:18:03 -07002532 layer->mDrawingParent = sp<Layer>::fromExisting(this);
chaviw74b03172019-08-19 11:09:03 -07002533}
2534
Steven Thomas62a4cf82020-01-31 12:04:03 -08002535Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2536 switch (compatibility) {
2537 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2538 return FrameRateCompatibility::Default;
2539 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2540 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002541 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2542 return FrameRateCompatibility::Exact;
Andy Labrada096227e2022-06-15 16:58:11 +00002543 case ANATIVEWINDOW_FRAME_RATE_MIN:
2544 return FrameRateCompatibility::Min;
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002545 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
2546 return FrameRateCompatibility::NoVote;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002547 default:
2548 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2549 return FrameRateCompatibility::Default;
2550 }
2551}
2552
Marin Shalamanovc5986772021-03-16 16:09:49 +01002553scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2554 switch (strategy) {
2555 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2556 return Seamlessness::OnlySeamless;
2557 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2558 return Seamlessness::SeamedAndSeamless;
2559 default:
2560 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2561 return Seamlessness::Default;
2562 }
2563}
2564
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002565bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002566 const State& s(mDrawingState);
2567 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2568 return true;
2569 }
2570
Rob Carrc6d2d2b2021-10-25 16:51:49 +00002571 sp<Layer> parent = mDrawingParent.promote();
2572 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002573}
2574
Robert Carr6a0382d2021-07-01 15:57:17 -07002575void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2576 mClonedChild = clonedChild;
2577 mHadClonedChild = true;
2578 mFlinger->mNumClones++;
2579}
2580
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002581bool Layer::setDropInputMode(gui::DropInputMode mode) {
2582 if (mDrawingState.dropInputMode == mode) {
2583 return false;
2584 }
2585 mDrawingState.dropInputMode = mode;
2586 return true;
2587}
2588
Vishnu Nair3bb11d02021-11-26 09:24:11 -08002589void Layer::cloneDrawingState(const Layer* from) {
2590 mDrawingState = from->mDrawingState;
2591 // Skip callback info since they are not applicable for cloned layers.
2592 mDrawingState.releaseBufferListener = nullptr;
2593 mDrawingState.callbackHandles = {};
2594}
2595
Patrick Williamsbb25f802022-08-30 23:02:34 +00002596void Layer::callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
2597 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
2598 const sp<Fence>& releaseFence,
2599 uint32_t currentMaxAcquiredBufferCount) {
2600 if (!listener) {
2601 return;
2602 }
2603 ATRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, getDebugName(), framenumber);
Huihong Luoc50b4982021-11-24 12:34:52 -08002604 std::optional<os::ParcelFileDescriptor> fenceFd;
2605 if (releaseFence) {
2606 fenceFd = os::ParcelFileDescriptor(base::unique_fd(::dup(releaseFence->get())));
2607 }
2608 listener->onReleaseBuffer({buffer->getId(), framenumber}, fenceFd,
2609 static_cast<int32_t>(currentMaxAcquiredBufferCount));
Patrick Williamsbb25f802022-08-30 23:02:34 +00002610}
2611
2612void Layer::onLayerDisplayed(ftl::SharedFuture<FenceResult> futureFenceResult) {
2613 // If we are displayed on multiple displays in a single composition cycle then we would
2614 // need to do careful tracking to enable the use of the mLastClientCompositionFence.
2615 // For example we can only use it if all the displays are client comp, and we need
2616 // to merge all the client comp fences. We could do this, but for now we just
2617 // disable the optimization when a layer is composed on multiple displays.
2618 if (mClearClientCompositionFenceOnLayerDisplayed) {
2619 mLastClientCompositionFence = nullptr;
2620 } else {
2621 mClearClientCompositionFenceOnLayerDisplayed = true;
2622 }
2623
2624 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
2625 // buffer that was presented on this layer. The first transaction that came in this frame that
2626 // replaced the previous buffer on this layer needs this release fence, because the fence will
2627 // let the client know when that previous buffer is removed from the screen.
2628 //
2629 // Every other transaction on this layer does not need a release fence because no other
2630 // Transactions that were set on this layer this frame are going to have their preceding buffer
2631 // removed from the display this frame.
2632 //
2633 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
2634 // buffer so it doesn't need a previous release fence because the layer still needs the previous
2635 // buffer. The second transaction contains a buffer so it needs a previous release fence because
2636 // the previous buffer will be released this frame. The third transaction also contains a
2637 // buffer. It replaces the buffer in the second transaction. The buffer in the second
2638 // transaction will now no longer be presented so it is released immediately and the third
2639 // transaction doesn't need a previous release fence.
2640 sp<CallbackHandle> ch;
2641 for (auto& handle : mDrawingState.callbackHandles) {
2642 if (handle->releasePreviousBuffer &&
2643 mDrawingState.releaseBufferEndpoint == handle->listener) {
2644 ch = handle;
2645 break;
2646 }
2647 }
2648
2649 // Prevent tracing the same release multiple times.
2650 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
2651 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
2652 }
2653
2654 if (ch != nullptr) {
2655 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2656 ch->previousReleaseFences.emplace_back(std::move(futureFenceResult));
2657 ch->name = mName;
2658 }
2659}
2660
2661void Layer::onSurfaceFrameCreated(
2662 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Vishnu Nair22491b82022-10-18 14:59:14 -07002663 if (!hasBufferOrSidebandStreamInDrawing()) {
2664 return;
2665 }
2666
Patrick Williamsbb25f802022-08-30 23:02:34 +00002667 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
2668 // Too many SurfaceFrames pending classification. The front of the deque is probably not
2669 // tracked by FrameTimeline and will never be presented. This will only result in a memory
2670 // leak.
2671 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
2672 mName.c_str());
2673 std::string miniDump = mPendingJankClassifications.front()->miniDump();
2674 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
2675 mPendingJankClassifications.pop_front();
2676 }
2677 mPendingJankClassifications.emplace_back(surfaceFrame);
2678}
2679
2680void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
2681 for (const auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair71fcf912022-10-18 09:14:20 -07002682 handle->transformHint = mSkipReportingTransformHint
2683 ? std::nullopt
2684 : std::make_optional<uint32_t>(mTransformHint);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002685 handle->dequeueReadyTime = dequeueReadyTime;
2686 handle->currentMaxAcquiredBufferCount =
2687 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
2688 ATRACE_FORMAT_INSTANT("releasePendingBuffer %s - %" PRIu64, getDebugName(),
2689 handle->previousReleaseCallbackId.framenumber);
2690 }
2691
2692 for (auto& handle : mDrawingState.callbackHandles) {
2693 if (handle->releasePreviousBuffer &&
2694 mDrawingState.releaseBufferEndpoint == handle->listener) {
2695 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
2696 break;
2697 }
2698 }
2699
2700 std::vector<JankData> jankData;
2701 jankData.reserve(mPendingJankClassifications.size());
2702 while (!mPendingJankClassifications.empty() &&
2703 mPendingJankClassifications.front()->getJankType()) {
2704 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
2705 mPendingJankClassifications.front();
2706 mPendingJankClassifications.pop_front();
2707 jankData.emplace_back(
2708 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
2709 }
2710
2711 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(mDrawingState.callbackHandles,
2712 jankData);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002713 mDrawingState.callbackHandles = {};
2714}
2715
2716bool Layer::willPresentCurrentTransaction() const {
2717 // Returns true if the most recent Transaction applied to CurrentState will be presented.
2718 return (getSidebandStreamChanged() || getAutoRefresh() ||
2719 (mDrawingState.modified &&
2720 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
2721}
2722
2723bool Layer::setTransform(uint32_t transform) {
2724 if (mDrawingState.bufferTransform == transform) return false;
2725 mDrawingState.bufferTransform = transform;
2726 mDrawingState.modified = true;
2727 setTransactionFlags(eTransactionNeeded);
2728 return true;
2729}
2730
2731bool Layer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
2732 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
2733 mDrawingState.sequence++;
2734 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
2735 mDrawingState.modified = true;
2736 setTransactionFlags(eTransactionNeeded);
2737 return true;
2738}
2739
2740bool Layer::setBufferCrop(const Rect& bufferCrop) {
2741 if (mDrawingState.bufferCrop == bufferCrop) return false;
2742
2743 mDrawingState.sequence++;
2744 mDrawingState.bufferCrop = bufferCrop;
2745
2746 mDrawingState.modified = true;
2747 setTransactionFlags(eTransactionNeeded);
2748 return true;
2749}
2750
2751bool Layer::setDestinationFrame(const Rect& destinationFrame) {
2752 if (mDrawingState.destinationFrame == destinationFrame) return false;
2753
2754 mDrawingState.sequence++;
2755 mDrawingState.destinationFrame = destinationFrame;
2756
2757 mDrawingState.modified = true;
2758 setTransactionFlags(eTransactionNeeded);
2759 return true;
2760}
2761
2762// Translate destination frame into scale and position. If a destination frame is not set, use the
2763// provided scale and position
2764bool Layer::updateGeometry() {
2765 if ((mDrawingState.flags & layer_state_t::eIgnoreDestinationFrame) ||
2766 mDrawingState.destinationFrame.isEmpty()) {
2767 // If destination frame is not set, use the requested transform set via
2768 // Layer::setPosition and Layer::setMatrix.
2769 return assignTransform(&mDrawingState.transform, mRequestedTransform);
2770 }
2771
2772 Rect destRect = mDrawingState.destinationFrame;
2773 int32_t destW = destRect.width();
2774 int32_t destH = destRect.height();
2775 if (destRect.left < 0) {
2776 destRect.left = 0;
2777 destRect.right = destW;
2778 }
2779 if (destRect.top < 0) {
2780 destRect.top = 0;
2781 destRect.bottom = destH;
2782 }
2783
2784 if (!mDrawingState.buffer) {
2785 ui::Transform t;
2786 t.set(destRect.left, destRect.top);
2787 return assignTransform(&mDrawingState.transform, t);
2788 }
2789
2790 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
2791 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
2792 // Undo any transformations on the buffer.
2793 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
2794 std::swap(bufferWidth, bufferHeight);
2795 }
2796 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
2797 if (mDrawingState.transformToDisplayInverse) {
2798 if (invTransform & ui::Transform::ROT_90) {
2799 std::swap(bufferWidth, bufferHeight);
2800 }
2801 }
2802
2803 float sx = destW / static_cast<float>(bufferWidth);
2804 float sy = destH / static_cast<float>(bufferHeight);
2805 ui::Transform t;
2806 t.set(sx, 0, 0, sy);
2807 t.set(destRect.left, destRect.top);
2808 return assignTransform(&mDrawingState.transform, t);
2809}
2810
2811bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
2812 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
2813 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
2814 return false;
2815 }
2816
2817 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
2818
2819 mDrawingState.sequence++;
2820 mDrawingState.modified = true;
2821 setTransactionFlags(eTransactionNeeded);
2822
2823 return true;
2824}
2825
2826bool Layer::setPosition(float x, float y) {
2827 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
2828 return false;
2829 }
2830
2831 mRequestedTransform.set(x, y);
2832
2833 mDrawingState.sequence++;
2834 mDrawingState.modified = true;
2835 setTransactionFlags(eTransactionNeeded);
2836
2837 return true;
2838}
2839
2840bool Layer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
2841 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
2842 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
Brian Lindahl439afad2022-11-14 11:16:55 -07002843 const FrameTimelineInfo& info) {
Chavi Weingarten009619f2022-09-15 20:27:25 +00002844 ATRACE_FORMAT("setBuffer %s - hasBuffer=%s", getDebugName(), (buffer ? "true" : "false"));
Patrick Williamsbb25f802022-08-30 23:02:34 +00002845 if (!buffer) {
2846 return false;
2847 }
2848
2849 const bool frameNumberChanged =
2850 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
2851 const uint64_t frameNumber =
2852 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
Chavi Weingarten009619f2022-09-15 20:27:25 +00002853 ATRACE_FORMAT_INSTANT("setBuffer %s - %" PRIu64, getDebugName(), frameNumber);
Patrick Williamsbb25f802022-08-30 23:02:34 +00002854
2855 if (mDrawingState.buffer) {
2856 mReleasePreviousBuffer = true;
2857 if (!mBufferInfo.mBuffer ||
2858 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
2859 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
2860 // If mDrawingState has a buffer, and we are about to update again
2861 // before swapping to drawing state, then the first buffer will be
2862 // dropped and we should decrement the pending buffer count and
2863 // call any release buffer callbacks if set.
2864 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2865 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2866 mDrawingState.acquireFence,
2867 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2868 mOwnerUid));
2869 decrementPendingBufferCount();
2870 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
2871 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
2872 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
2873 mDrawingState.bufferSurfaceFrameTX.reset();
2874 }
2875 } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) {
2876 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
2877 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
2878 mLastClientCompositionFence,
2879 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
2880 mOwnerUid));
2881 mLastClientCompositionFence = nullptr;
2882 }
2883 }
2884
2885 mDrawingState.frameNumber = frameNumber;
2886 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
2887 mDrawingState.buffer = std::move(buffer);
2888 mDrawingState.clientCacheId = bufferData.cachedBuffer;
Patrick Williamsbb25f802022-08-30 23:02:34 +00002889 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
2890 ? bufferData.acquireFence
2891 : Fence::NO_FENCE;
2892 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
2893 if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
2894 // We latched this buffer unsiganled, so we need to pass the acquire fence
2895 // on the callback instead of just the acquire time, since it's unknown at
2896 // this point.
2897 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence;
2898 } else {
2899 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime();
2900 }
2901
2902 mDrawingState.modified = true;
2903 setTransactionFlags(eTransactionNeeded);
2904
2905 const int32_t layerId = getSequence();
2906 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
2907 mOwnerUid, postTime, getGameMode());
2908 mDrawingState.desiredPresentTime = desiredPresentTime;
2909 mDrawingState.isAutoTimestamp = isAutoTimestamp;
2910
2911 const nsecs_t presentTime = [&] {
2912 if (!isAutoTimestamp) return desiredPresentTime;
2913
2914 const auto prediction =
2915 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
2916 if (prediction.has_value()) return prediction->presentTime;
2917
2918 return static_cast<nsecs_t>(0);
2919 }();
2920
2921 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
2922 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
2923
2924 setFrameTimelineVsyncForBufferTransaction(info, postTime);
2925
2926 if (dequeueTime && *dequeueTime != 0) {
2927 const uint64_t bufferId = mDrawingState.buffer->getId();
2928 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
2929 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
2930 FrameTracer::FrameEvent::DEQUEUE);
2931 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
2932 FrameTracer::FrameEvent::QUEUE);
2933 }
2934
2935 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
2936 return true;
2937}
2938
2939bool Layer::setDataspace(ui::Dataspace dataspace) {
2940 mDrawingState.dataspaceRequested = true;
2941 if (mDrawingState.dataspace == dataspace) return false;
2942 mDrawingState.dataspace = dataspace;
2943 mDrawingState.modified = true;
2944 setTransactionFlags(eTransactionNeeded);
2945 return true;
2946}
2947
2948bool Layer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
2949 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
2950 mDrawingState.hdrMetadata = hdrMetadata;
2951 mDrawingState.modified = true;
2952 setTransactionFlags(eTransactionNeeded);
2953 return true;
2954}
2955
2956bool Layer::setSurfaceDamageRegion(const Region& surfaceDamage) {
2957 mDrawingState.surfaceDamageRegion = surfaceDamage;
2958 mDrawingState.modified = true;
2959 setTransactionFlags(eTransactionNeeded);
2960 return true;
2961}
2962
2963bool Layer::setApi(int32_t api) {
2964 if (mDrawingState.api == api) return false;
2965 mDrawingState.api = api;
2966 mDrawingState.modified = true;
2967 setTransactionFlags(eTransactionNeeded);
2968 return true;
2969}
2970
2971bool Layer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
2972 if (mDrawingState.sidebandStream == sidebandStream) return false;
2973
2974 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
2975 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
2976 } else if (sidebandStream != nullptr) {
2977 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
2978 }
2979
2980 mDrawingState.sidebandStream = sidebandStream;
2981 mDrawingState.modified = true;
2982 setTransactionFlags(eTransactionNeeded);
2983 if (!mSidebandStreamChanged.exchange(true)) {
2984 // mSidebandStreamChanged was false
2985 mFlinger->onLayerUpdate();
2986 }
2987 return true;
2988}
2989
2990bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
2991 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
2992 if (handles.empty()) {
2993 mReleasePreviousBuffer = false;
2994 return false;
2995 }
2996
2997 const bool willPresent = willPresentCurrentTransaction();
2998
2999 for (const auto& handle : handles) {
3000 // If this transaction set a buffer on this layer, release its previous buffer
3001 handle->releasePreviousBuffer = mReleasePreviousBuffer;
3002
3003 // If this layer will be presented in this frame
3004 if (willPresent) {
3005 // If this transaction set an acquire fence on this layer, set its acquire time
3006 handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence;
3007 handle->frameNumber = mDrawingState.frameNumber;
3008
3009 // Store so latched time and release fence can be set
3010 mDrawingState.callbackHandles.push_back(handle);
3011
3012 } else { // If this layer will NOT need to be relatched and presented this frame
3013 // Notify the transaction completed thread this handle is done
3014 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
3015 }
3016 }
3017
3018 mReleasePreviousBuffer = false;
3019 mCallbackHandleAcquireTimeOrFence = -1;
3020
3021 return willPresent;
3022}
3023
3024Rect Layer::getBufferSize(const State& /*s*/) const {
3025 // for buffer state layers we use the display frame size as the buffer size.
3026
3027 if (mBufferInfo.mBuffer == nullptr) {
3028 return Rect::INVALID_RECT;
3029 }
3030
3031 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3032 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3033
3034 // Undo any transformations on the buffer and return the result.
3035 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
3036 std::swap(bufWidth, bufHeight);
3037 }
3038
3039 if (getTransformToDisplayInverse()) {
3040 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3041 if (invTransform & ui::Transform::ROT_90) {
3042 std::swap(bufWidth, bufHeight);
3043 }
3044 }
3045
3046 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
3047}
3048
3049FloatRect Layer::computeSourceBounds(const FloatRect& parentBounds) const {
3050 if (mBufferInfo.mBuffer == nullptr) {
3051 return parentBounds;
3052 }
3053
3054 return getBufferSize(getDrawingState()).toFloatRect();
3055}
3056
3057bool Layer::fenceHasSignaled() const {
3058 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
3059 return true;
3060 }
3061
3062 const bool fenceSignaled =
3063 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
3064 if (!fenceSignaled) {
3065 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
3066 TimeStats::LatchSkipReason::LateAcquire);
3067 }
3068
3069 return fenceSignaled;
3070}
3071
Vishnu Naire14c6b32022-08-06 04:20:15 +00003072bool Layer::onPreComposition(nsecs_t refreshStartTime) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003073 for (const auto& handle : mDrawingState.callbackHandles) {
3074 handle->refreshStartTime = refreshStartTime;
3075 }
3076 return hasReadyFrame();
3077}
3078
3079void Layer::setAutoRefresh(bool autoRefresh) {
3080 mDrawingState.autoRefresh = autoRefresh;
3081}
3082
3083bool Layer::latchSidebandStream(bool& recomputeVisibleRegions) {
3084 // 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 +00003085 auto* snapshot = editLayerSnapshot();
3086 snapshot->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003087
3088 if (mSidebandStreamChanged.exchange(false)) {
3089 const State& s(getDrawingState());
3090 // mSidebandStreamChanged was true
3091 mSidebandStream = s.sidebandStream;
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003092 snapshot->sidebandStream = mSidebandStream;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003093 if (mSidebandStream != nullptr) {
3094 setTransactionFlags(eTransactionNeeded);
3095 mFlinger->setTransactionFlags(eTraversalNeeded);
3096 }
3097 recomputeVisibleRegions = true;
3098
3099 return true;
3100 }
3101 return false;
3102}
3103
3104bool Layer::hasFrameUpdate() const {
3105 const State& c(getDrawingState());
3106 return (mDrawingStateModified || mDrawingState.modified) &&
3107 (c.buffer != nullptr || c.bgColorLayer != nullptr);
3108}
3109
3110void Layer::updateTexImage(nsecs_t latchTime) {
3111 const State& s(getDrawingState());
3112
3113 if (!s.buffer) {
3114 if (s.bgColorLayer) {
3115 for (auto& handle : mDrawingState.callbackHandles) {
3116 handle->latchTime = latchTime;
3117 }
3118 }
3119 return;
3120 }
3121
3122 for (auto& handle : mDrawingState.callbackHandles) {
3123 if (handle->frameNumber == mDrawingState.frameNumber) {
3124 handle->latchTime = latchTime;
3125 }
3126 }
3127
3128 const int32_t layerId = getSequence();
3129 const uint64_t bufferId = mDrawingState.buffer->getId();
3130 const uint64_t frameNumber = mDrawingState.frameNumber;
3131 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3132 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
3133 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
3134
3135 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
3136 FrameTracer::FrameEvent::ACQUIRE_FENCE);
3137 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
3138 FrameTracer::FrameEvent::LATCH);
3139
3140 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
3141 if (bufferSurfaceFrame != nullptr &&
3142 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
3143 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
3144 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
3145 // are processing the next state.
3146 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
3147 mDrawingState.acquireFenceTime->getSignalTime(),
3148 latchTime);
3149 mDrawingState.bufferSurfaceFrameTX.reset();
3150 }
3151
3152 std::deque<sp<CallbackHandle>> remainingHandles;
3153 mFlinger->getTransactionCallbackInvoker()
3154 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
3155 mDrawingState.callbackHandles = remainingHandles;
3156
3157 mDrawingStateModified = false;
3158}
3159
3160void Layer::gatherBufferInfo() {
3161 if (!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
3162 decrementPendingBufferCount();
3163 }
3164
3165 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
3166 mBufferInfo.mBuffer = mDrawingState.buffer;
3167 mBufferInfo.mFence = mDrawingState.acquireFence;
3168 mBufferInfo.mFrameNumber = mDrawingState.frameNumber;
3169 mBufferInfo.mPixelFormat =
3170 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat();
3171 mBufferInfo.mFrameLatencyNeeded = true;
3172 mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime;
3173 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
3174 mBufferInfo.mFence = mDrawingState.acquireFence;
3175 mBufferInfo.mTransform = mDrawingState.bufferTransform;
3176 auto lastDataspace = mBufferInfo.mDataspace;
3177 mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace);
3178 if (lastDataspace != mBufferInfo.mDataspace) {
3179 mFlinger->mSomeDataspaceChanged = true;
3180 }
3181 mBufferInfo.mCrop = computeBufferCrop(mDrawingState);
3182 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
3183 mBufferInfo.mSurfaceDamage = mDrawingState.surfaceDamageRegion;
3184 mBufferInfo.mHdrMetadata = mDrawingState.hdrMetadata;
3185 mBufferInfo.mApi = mDrawingState.api;
3186 mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003187}
3188
3189Rect Layer::computeBufferCrop(const State& s) {
3190 if (s.buffer && !s.bufferCrop.isEmpty()) {
3191 Rect bufferCrop;
3192 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
3193 return bufferCrop;
3194 } else if (s.buffer) {
3195 return s.buffer->getBounds();
3196 } else {
3197 return s.bufferCrop;
3198 }
3199}
3200
3201sp<Layer> Layer::createClone() {
3202 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
3203 args.textureName = mTextureName;
Patrick Williams83f36b22022-09-14 17:57:35 +00003204 sp<Layer> layer = mFlinger->getFactory().createBufferStateLayer(args);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003205 layer->setInitialValuesForClone(sp<Layer>::fromExisting(this));
3206 return layer;
3207}
3208
3209bool Layer::bufferNeedsFiltering() const {
3210 const State& s(getDrawingState());
3211 if (!s.buffer) {
3212 return false;
3213 }
3214
3215 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
3216 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
3217
3218 // Undo any transformations on the buffer and return the result.
3219 if (s.bufferTransform & ui::Transform::ROT_90) {
3220 std::swap(bufferWidth, bufferHeight);
3221 }
3222
3223 if (s.transformToDisplayInverse) {
3224 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
3225 if (invTransform & ui::Transform::ROT_90) {
3226 std::swap(bufferWidth, bufferHeight);
3227 }
3228 }
3229
3230 const Rect layerSize{getBounds()};
3231 int32_t layerWidth = layerSize.getWidth();
3232 int32_t layerHeight = layerSize.getHeight();
3233
3234 // Align the layer orientation with the buffer before comparism
3235 if (mTransformHint & ui::Transform::ROT_90) {
3236 std::swap(layerWidth, layerHeight);
3237 }
3238
3239 return layerWidth != bufferWidth || layerHeight != bufferHeight;
3240}
3241
3242void Layer::decrementPendingBufferCount() {
3243 int32_t pendingBuffers = --mPendingBufferTransactions;
3244 tracePendingBufferCount(pendingBuffers);
3245}
3246
3247void Layer::tracePendingBufferCount(int32_t pendingBuffers) {
3248 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
3249}
3250
3251/*
3252 * We don't want to send the layer's transform to input, but rather the
3253 * parent's transform. This is because Layer's transform is
3254 * information about how the buffer is placed on screen. The parent's
3255 * transform makes more sense to send since it's information about how the
3256 * layer is placed on screen. This transform is used by input to determine
3257 * how to go from screen space back to window space.
3258 */
3259ui::Transform Layer::getInputTransform() const {
3260 if (!hasBufferOrSidebandStream()) {
3261 return getTransform();
3262 }
3263 sp<Layer> parent = mDrawingParent.promote();
3264 if (parent == nullptr) {
3265 return ui::Transform();
3266 }
3267
3268 return parent->getTransform();
3269}
3270
3271/**
3272 * Similar to getInputTransform, we need to update the bounds to include the transform.
3273 * This is because bounds don't include the buffer transform, where the input assumes
3274 * that's already included.
3275 */
3276Rect Layer::getInputBounds() const {
3277 if (!hasBufferOrSidebandStream()) {
3278 return getCroppedBufferSize(getDrawingState());
3279 }
3280
3281 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
3282 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
3283 return bufferBounds;
3284 }
3285 return mDrawingState.transform.transform(bufferBounds);
3286}
3287
3288bool Layer::simpleBufferUpdate(const layer_state_t& s) const {
3289 const uint64_t requiredFlags = layer_state_t::eBufferChanged;
3290
3291 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
3292 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
3293 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
3294 layer_state_t::eLayerStackChanged | layer_state_t::eAutoRefreshChanged |
3295 layer_state_t::eReparent;
3296
3297 const uint64_t allowedFlags = layer_state_t::eHasListenerCallbacksChanged |
3298 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFrameRateChanged |
3299 layer_state_t::eSurfaceDamageRegionChanged | layer_state_t::eApiChanged |
3300 layer_state_t::eMetadataChanged | layer_state_t::eDropInputModeChanged |
3301 layer_state_t::eInputInfoChanged;
3302
3303 if ((s.what & requiredFlags) != requiredFlags) {
3304 ALOGV("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
3305 (s.what | requiredFlags) & ~s.what);
3306 return false;
3307 }
3308
3309 if (s.what & deniedFlags) {
3310 ALOGV("%s: false [has denied flags 0x%" PRIx64 "]", __func__, s.what & deniedFlags);
3311 return false;
3312 }
3313
3314 if (s.what & allowedFlags) {
3315 ALOGV("%s: [has allowed flags 0x%" PRIx64 "]", __func__, s.what & allowedFlags);
3316 }
3317
3318 if (s.what & layer_state_t::ePositionChanged) {
3319 if (mRequestedTransform.tx() != s.x || mRequestedTransform.ty() != s.y) {
3320 ALOGV("%s: false [ePositionChanged changed]", __func__);
3321 return false;
3322 }
3323 }
3324
3325 if (s.what & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003326 if (mDrawingState.color.a != s.color.a) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003327 ALOGV("%s: false [eAlphaChanged changed]", __func__);
3328 return false;
3329 }
3330 }
3331
3332 if (s.what & layer_state_t::eColorTransformChanged) {
3333 if (mDrawingState.colorTransform != s.colorTransform) {
3334 ALOGV("%s: false [eColorTransformChanged changed]", __func__);
3335 return false;
3336 }
3337 }
3338
3339 if (s.what & layer_state_t::eBackgroundColorChanged) {
3340 if (mDrawingState.bgColorLayer || s.bgColorAlpha != 0) {
3341 ALOGV("%s: false [eBackgroundColorChanged changed]", __func__);
3342 return false;
3343 }
3344 }
3345
3346 if (s.what & layer_state_t::eMatrixChanged) {
3347 if (mRequestedTransform.dsdx() != s.matrix.dsdx ||
3348 mRequestedTransform.dtdy() != s.matrix.dtdy ||
3349 mRequestedTransform.dtdx() != s.matrix.dtdx ||
3350 mRequestedTransform.dsdy() != s.matrix.dsdy) {
3351 ALOGV("%s: false [eMatrixChanged changed]", __func__);
3352 return false;
3353 }
3354 }
3355
3356 if (s.what & layer_state_t::eCornerRadiusChanged) {
3357 if (mDrawingState.cornerRadius != s.cornerRadius) {
3358 ALOGV("%s: false [eCornerRadiusChanged changed]", __func__);
3359 return false;
3360 }
3361 }
3362
3363 if (s.what & layer_state_t::eBackgroundBlurRadiusChanged) {
3364 if (mDrawingState.backgroundBlurRadius != static_cast<int>(s.backgroundBlurRadius)) {
3365 ALOGV("%s: false [eBackgroundBlurRadiusChanged changed]", __func__);
3366 return false;
3367 }
3368 }
3369
Vishnu Nairbbceb462022-10-10 04:52:13 +00003370 if (s.what & layer_state_t::eBufferTransformChanged) {
3371 if (mDrawingState.bufferTransform != s.bufferTransform) {
3372 ALOGV("%s: false [eBufferTransformChanged changed]", __func__);
Patrick Williamsbb25f802022-08-30 23:02:34 +00003373 return false;
3374 }
3375 }
3376
3377 if (s.what & layer_state_t::eTransformToDisplayInverseChanged) {
3378 if (mDrawingState.transformToDisplayInverse != s.transformToDisplayInverse) {
3379 ALOGV("%s: false [eTransformToDisplayInverseChanged changed]", __func__);
3380 return false;
3381 }
3382 }
3383
3384 if (s.what & layer_state_t::eCropChanged) {
3385 if (mDrawingState.crop != s.crop) {
3386 ALOGV("%s: false [eCropChanged changed]", __func__);
3387 return false;
3388 }
3389 }
3390
3391 if (s.what & layer_state_t::eDataspaceChanged) {
3392 if (mDrawingState.dataspace != s.dataspace) {
3393 ALOGV("%s: false [eDataspaceChanged changed]", __func__);
3394 return false;
3395 }
3396 }
3397
3398 if (s.what & layer_state_t::eHdrMetadataChanged) {
3399 if (mDrawingState.hdrMetadata != s.hdrMetadata) {
3400 ALOGV("%s: false [eHdrMetadataChanged changed]", __func__);
3401 return false;
3402 }
3403 }
3404
3405 if (s.what & layer_state_t::eSidebandStreamChanged) {
3406 if (mDrawingState.sidebandStream != s.sidebandStream) {
3407 ALOGV("%s: false [eSidebandStreamChanged changed]", __func__);
3408 return false;
3409 }
3410 }
3411
3412 if (s.what & layer_state_t::eColorSpaceAgnosticChanged) {
3413 if (mDrawingState.colorSpaceAgnostic != s.colorSpaceAgnostic) {
3414 ALOGV("%s: false [eColorSpaceAgnosticChanged changed]", __func__);
3415 return false;
3416 }
3417 }
3418
3419 if (s.what & layer_state_t::eShadowRadiusChanged) {
3420 if (mDrawingState.shadowRadius != s.shadowRadius) {
3421 ALOGV("%s: false [eShadowRadiusChanged changed]", __func__);
3422 return false;
3423 }
3424 }
3425
3426 if (s.what & layer_state_t::eFixedTransformHintChanged) {
3427 if (mDrawingState.fixedTransformHint != s.fixedTransformHint) {
3428 ALOGV("%s: false [eFixedTransformHintChanged changed]", __func__);
3429 return false;
3430 }
3431 }
3432
3433 if (s.what & layer_state_t::eTrustedOverlayChanged) {
3434 if (mDrawingState.isTrustedOverlay != s.isTrustedOverlay) {
3435 ALOGV("%s: false [eTrustedOverlayChanged changed]", __func__);
3436 return false;
3437 }
3438 }
3439
3440 if (s.what & layer_state_t::eStretchChanged) {
3441 StretchEffect temp = s.stretchEffect;
3442 temp.sanitize();
3443 if (mDrawingState.stretchEffect != temp) {
3444 ALOGV("%s: false [eStretchChanged changed]", __func__);
3445 return false;
3446 }
3447 }
3448
3449 if (s.what & layer_state_t::eBufferCropChanged) {
3450 if (mDrawingState.bufferCrop != s.bufferCrop) {
3451 ALOGV("%s: false [eBufferCropChanged changed]", __func__);
3452 return false;
3453 }
3454 }
3455
3456 if (s.what & layer_state_t::eDestinationFrameChanged) {
3457 if (mDrawingState.destinationFrame != s.destinationFrame) {
3458 ALOGV("%s: false [eDestinationFrameChanged changed]", __func__);
3459 return false;
3460 }
3461 }
3462
3463 if (s.what & layer_state_t::eDimmingEnabledChanged) {
3464 if (mDrawingState.dimmingEnabled != s.dimmingEnabled) {
3465 ALOGV("%s: false [eDimmingEnabledChanged changed]", __func__);
3466 return false;
3467 }
3468 }
3469
3470 ALOGV("%s: true", __func__);
3471 return true;
3472}
3473
3474bool Layer::isHdrY410() const {
3475 // pixel format is HDR Y410 masquerading as RGBA_1010102
3476 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
3477 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
3478 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
3479}
3480
Vishnu Naire14c6b32022-08-06 04:20:15 +00003481sp<LayerFE> Layer::getCompositionEngineLayerFE() const {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003482 // 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 +00003483 return hasSomethingToDraw() ? mLayerFE : nullptr;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003484}
3485
Vishnu Naire14c6b32022-08-06 04:20:15 +00003486const LayerSnapshot* Layer::getLayerSnapshot() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003487 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003488}
3489
Vishnu Naire14c6b32022-08-06 04:20:15 +00003490LayerSnapshot* Layer::editLayerSnapshot() {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003491 return mSnapshot.get();
3492}
Vishnu Naire14c6b32022-08-06 04:20:15 +00003493
Patrick Williamsbb25f802022-08-30 23:02:34 +00003494const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003495 return mSnapshot.get();
Patrick Williamsbb25f802022-08-30 23:02:34 +00003496}
3497
Patrick Williams7584c6a2022-10-29 02:10:58 +00003498sp<LayerFE> Layer::copyCompositionEngineLayerFE() const {
3499 auto result = mFlinger->getFactory().createLayerFE(mLayerFE->getDebugName());
3500 result->mSnapshot = std::make_unique<LayerSnapshot>(*mSnapshot);
3501 return result;
3502}
3503
Patrick Williamsbb25f802022-08-30 23:02:34 +00003504void Layer::useSurfaceDamage() {
3505 if (mFlinger->mForceFullDamage) {
3506 surfaceDamageRegion = Region::INVALID_REGION;
3507 } else {
3508 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
3509 }
3510}
3511
3512void Layer::useEmptyDamage() {
3513 surfaceDamageRegion.clear();
3514}
3515
3516bool Layer::isOpaque(const Layer::State& s) const {
3517 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
3518 // layer's opaque flag.
3519 if (!hasSomethingToDraw()) {
3520 return false;
3521 }
3522
3523 // if the layer has the opaque flag, then we're always opaque
3524 if ((s.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque) {
3525 return true;
3526 }
3527
3528 // If the buffer has no alpha channel, then we are opaque
3529 if (hasBufferOrSidebandStream() && isOpaqueFormat(getPixelFormat())) {
3530 return true;
3531 }
3532
3533 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
3534 return fillsColor() && getAlpha() == 1.0_hf;
3535}
3536
3537bool Layer::canReceiveInput() const {
3538 return !isHiddenByPolicy() && (mBufferInfo.mBuffer == nullptr || getAlpha() > 0.0f);
3539}
3540
3541bool Layer::isVisible() const {
3542 if (!hasSomethingToDraw()) {
3543 return false;
3544 }
3545
3546 if (isHiddenByPolicy()) {
3547 return false;
3548 }
3549
3550 return getAlpha() > 0.0f || hasBlur();
3551}
3552
3553void Layer::onPostComposition(const DisplayDevice* display,
3554 const std::shared_ptr<FenceTime>& glDoneFence,
3555 const std::shared_ptr<FenceTime>& presentFence,
3556 const CompositorTiming& compositorTiming) {
3557 // mFrameLatencyNeeded is true when a new frame was latched for the
3558 // composition.
3559 if (!mBufferInfo.mFrameLatencyNeeded) return;
3560
3561 for (const auto& handle : mDrawingState.callbackHandles) {
3562 handle->gpuCompositionDoneFence = glDoneFence;
3563 handle->compositorTiming = compositorTiming;
3564 }
3565
3566 // Update mFrameTracker.
3567 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
3568 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
3569
3570 const int32_t layerId = getSequence();
3571 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
3572
3573 const auto outputLayer = findOutputLayerForDisplay(display);
3574 if (outputLayer && outputLayer->requiresClientComposition()) {
3575 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
3576 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3577 clientCompositionTimestamp,
3578 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
3579 // Update the SurfaceFrames in the drawing state
3580 if (mDrawingState.bufferSurfaceFrameTX) {
3581 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
3582 }
3583 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
3584 surfaceFrame->setGpuComposition();
3585 }
3586 }
3587
3588 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
3589 if (frameReadyFence->isValid()) {
3590 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
3591 } else {
3592 // There was no fence for this frame, so assume that it was ready
3593 // to be presented at the desired present time.
3594 mFrameTracker.setFrameReadyTime(desiredPresentTime);
3595 }
3596
3597 if (display) {
Ady Abrahamace3d052022-11-17 16:25:05 -08003598 const Fps refreshRate = display->refreshRateSelector().getActiveMode().fps;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003599 const std::optional<Fps> renderRate =
3600 mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
3601
3602 const auto vote = frameRateToSetFrameRateVotePayload(mDrawingState.frameRate);
3603 const auto gameMode = getGameMode();
3604
3605 if (presentFence->isValid()) {
3606 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
3607 refreshRate, renderRate, vote, gameMode);
3608 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
3609 presentFence,
3610 FrameTracer::FrameEvent::PRESENT_FENCE);
3611 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
3612 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
3613 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Dominik Laskowskidc2bb802022-09-28 16:02:59 -04003614 // The HWC doesn't support present fences, so use the present timestamp instead.
3615 const nsecs_t presentTimestamp =
3616 mFlinger->getHwComposer().getPresentTimestamp(*displayId);
3617
3618 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
3619 const nsecs_t vsyncPeriod = display->getVsyncPeriodFromHWC();
3620 const nsecs_t actualPresentTime = now - ((now - presentTimestamp) % vsyncPeriod);
3621
Patrick Williamsbb25f802022-08-30 23:02:34 +00003622 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
3623 refreshRate, renderRate, vote, gameMode);
3624 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(),
3625 mCurrentFrameNumber, actualPresentTime,
3626 FrameTracer::FrameEvent::PRESENT_FENCE);
3627 mFrameTracker.setActualPresentTime(actualPresentTime);
3628 }
3629 }
3630
3631 mFrameTracker.advanceFrame();
3632 mBufferInfo.mFrameLatencyNeeded = false;
3633}
3634
3635bool Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
3636 ATRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(),
3637 getDrawingState().frameNumber);
3638
3639 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
3640
3641 if (refreshRequired) {
3642 return refreshRequired;
3643 }
3644
3645 // If the head buffer's acquire fence hasn't signaled yet, return and
3646 // try again later
3647 if (!fenceHasSignaled()) {
3648 ATRACE_NAME("!fenceHasSignaled()");
3649 mFlinger->onLayerUpdate();
3650 return false;
3651 }
3652
3653 updateTexImage(latchTime);
3654 if (mDrawingState.buffer == nullptr) {
3655 return false;
3656 }
3657
3658 // Capture the old state of the layer for comparisons later
3659 BufferInfo oldBufferInfo = mBufferInfo;
3660 const bool oldOpacity = isOpaque(mDrawingState);
3661 mPreviousFrameNumber = mCurrentFrameNumber;
3662 mCurrentFrameNumber = mDrawingState.frameNumber;
3663 gatherBufferInfo();
3664
3665 if (oldBufferInfo.mBuffer == nullptr) {
3666 // the first time we receive a buffer, we need to trigger a
3667 // geometry invalidation.
3668 recomputeVisibleRegions = true;
3669 }
3670
3671 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
3672 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
3673 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
3674 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
3675 recomputeVisibleRegions = true;
3676 }
3677
3678 if (oldBufferInfo.mBuffer != nullptr) {
3679 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
3680 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
3681 if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
3682 bufHeight != oldBufferInfo.mBuffer->getHeight()) {
3683 recomputeVisibleRegions = true;
3684 }
3685 }
3686
3687 if (oldOpacity != isOpaque(mDrawingState)) {
3688 recomputeVisibleRegions = true;
3689 }
3690
3691 return true;
3692}
3693
3694bool Layer::hasReadyFrame() const {
3695 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
3696}
3697
3698bool Layer::isProtected() const {
3699 return (mBufferInfo.mBuffer != nullptr) &&
3700 (mBufferInfo.mBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
3701}
3702
3703// As documented in libhardware header, formats in the range
3704// 0x100 - 0x1FF are specific to the HAL implementation, and
3705// are known to have no alpha channel
3706// TODO: move definition for device-specific range into
3707// hardware.h, instead of using hard-coded values here.
3708#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
3709
3710bool Layer::isOpaqueFormat(PixelFormat format) {
3711 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
3712 return true;
3713 }
3714 switch (format) {
3715 case PIXEL_FORMAT_RGBA_8888:
3716 case PIXEL_FORMAT_BGRA_8888:
3717 case PIXEL_FORMAT_RGBA_FP16:
3718 case PIXEL_FORMAT_RGBA_1010102:
3719 case PIXEL_FORMAT_R_8:
3720 return false;
3721 }
3722 // in all other case, we have no blending (also for unknown formats)
3723 return true;
3724}
3725
3726bool Layer::needsFiltering(const DisplayDevice* display) const {
3727 if (!hasBufferOrSidebandStream()) {
3728 return false;
3729 }
3730 const auto outputLayer = findOutputLayerForDisplay(display);
3731 if (outputLayer == nullptr) {
3732 return false;
3733 }
3734
3735 // We need filtering if the sourceCrop rectangle size does not match the
3736 // displayframe rectangle size (not a 1:1 render)
3737 const auto& compositionState = outputLayer->getState();
3738 const auto displayFrame = compositionState.displayFrame;
3739 const auto sourceCrop = compositionState.sourceCrop;
3740 return sourceCrop.getHeight() != displayFrame.getHeight() ||
3741 sourceCrop.getWidth() != displayFrame.getWidth();
3742}
3743
3744bool Layer::needsFilteringForScreenshots(const DisplayDevice* display,
3745 const ui::Transform& inverseParentTransform) const {
3746 if (!hasBufferOrSidebandStream()) {
3747 return false;
3748 }
3749 const auto outputLayer = findOutputLayerForDisplay(display);
3750 if (outputLayer == nullptr) {
3751 return false;
3752 }
3753
3754 // We need filtering if the sourceCrop rectangle size does not match the
3755 // viewport rectangle size (not a 1:1 render)
3756 const auto& compositionState = outputLayer->getState();
3757 const ui::Transform& displayTransform = display->getTransform();
3758 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
3759 // Undo the transformation of the displayFrame so that we're back into
3760 // layer-stack space.
3761 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
3762 const FloatRect sourceCrop = compositionState.sourceCrop;
3763
3764 int32_t frameHeight = frame.getHeight();
3765 int32_t frameWidth = frame.getWidth();
3766 // If the display transform had a rotational component then undo the
3767 // rotation so that the orientation matches the source crop.
3768 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
3769 std::swap(frameHeight, frameWidth);
3770 }
3771 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
3772}
3773
3774void Layer::latchAndReleaseBuffer() {
3775 if (hasReadyFrame()) {
3776 bool ignored = false;
3777 latchBuffer(ignored, systemTime());
3778 }
3779 releasePendingBuffer(systemTime());
3780}
3781
3782PixelFormat Layer::getPixelFormat() const {
3783 return mBufferInfo.mPixelFormat;
3784}
3785
3786bool Layer::getTransformToDisplayInverse() const {
3787 return mBufferInfo.mTransformToDisplayInverse;
3788}
3789
3790Rect Layer::getBufferCrop() const {
3791 // this is the crop rectangle that applies to the buffer
3792 // itself (as opposed to the window)
3793 if (!mBufferInfo.mCrop.isEmpty()) {
3794 // if the buffer crop is defined, we use that
3795 return mBufferInfo.mCrop;
3796 } else if (mBufferInfo.mBuffer != nullptr) {
3797 // otherwise we use the whole buffer
3798 return mBufferInfo.mBuffer->getBounds();
3799 } else {
3800 // if we don't have a buffer yet, we use an empty/invalid crop
3801 return Rect();
3802 }
3803}
3804
3805uint32_t Layer::getBufferTransform() const {
3806 return mBufferInfo.mTransform;
3807}
3808
3809ui::Dataspace Layer::getDataSpace() const {
3810 return mDrawingState.dataspaceRequested ? getRequestedDataSpace() : ui::Dataspace::UNKNOWN;
3811}
3812
3813ui::Dataspace Layer::getRequestedDataSpace() const {
3814 return hasBufferOrSidebandStream() ? mBufferInfo.mDataspace : mDrawingState.dataspace;
3815}
3816
3817ui::Dataspace Layer::translateDataspace(ui::Dataspace dataspace) {
3818 ui::Dataspace updatedDataspace = dataspace;
3819 // translate legacy dataspaces to modern dataspaces
3820 switch (dataspace) {
3821 case ui::Dataspace::SRGB:
3822 updatedDataspace = ui::Dataspace::V0_SRGB;
3823 break;
3824 case ui::Dataspace::SRGB_LINEAR:
3825 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
3826 break;
3827 case ui::Dataspace::JFIF:
3828 updatedDataspace = ui::Dataspace::V0_JFIF;
3829 break;
3830 case ui::Dataspace::BT601_625:
3831 updatedDataspace = ui::Dataspace::V0_BT601_625;
3832 break;
3833 case ui::Dataspace::BT601_525:
3834 updatedDataspace = ui::Dataspace::V0_BT601_525;
3835 break;
3836 case ui::Dataspace::BT709:
3837 updatedDataspace = ui::Dataspace::V0_BT709;
3838 break;
3839 default:
3840 break;
3841 }
3842
3843 return updatedDataspace;
3844}
3845
3846sp<GraphicBuffer> Layer::getBuffer() const {
3847 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
3848}
3849
Patrick Williamsbb25f802022-08-30 23:02:34 +00003850void Layer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
3851 mTransformHint = getFixedTransformHint();
3852 if (mTransformHint == ui::Transform::ROT_INVALID) {
3853 mTransformHint = displayTransformHint;
3854 }
Vishnu Nair71fcf912022-10-18 09:14:20 -07003855 mSkipReportingTransformHint = false;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003856}
3857
3858const std::shared_ptr<renderengine::ExternalTexture>& Layer::getExternalTexture() const {
3859 return mBufferInfo.mBuffer;
3860}
3861
3862bool Layer::setColor(const half3& color) {
Vishnu Nairbbceb462022-10-10 04:52:13 +00003863 if (mDrawingState.color.rgb == color) {
Patrick Williamsbb25f802022-08-30 23:02:34 +00003864 return false;
3865 }
3866
3867 mDrawingState.sequence++;
Vishnu Nairbbceb462022-10-10 04:52:13 +00003868 mDrawingState.color.rgb = color;
Patrick Williamsbb25f802022-08-30 23:02:34 +00003869 mDrawingState.modified = true;
3870 setTransactionFlags(eTransactionNeeded);
3871 return true;
3872}
3873
3874bool Layer::fillsColor() const {
3875 return !hasBufferOrSidebandStream() && mDrawingState.color.r >= 0.0_hf &&
3876 mDrawingState.color.g >= 0.0_hf && mDrawingState.color.b >= 0.0_hf;
3877}
3878
3879bool Layer::hasBlur() const {
3880 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
3881}
3882
Vishnu Nairba354102022-08-01 00:14:18 +00003883void Layer::updateSnapshot(bool updateGeometry) {
3884 if (!getCompositionEngineLayerFE()) {
3885 return;
3886 }
3887
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003888 auto* snapshot = editLayerSnapshot();
Vishnu Nairba354102022-08-01 00:14:18 +00003889 if (updateGeometry) {
3890 prepareBasicGeometryCompositionState();
3891 prepareGeometryCompositionState();
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003892 snapshot->roundedCorner = getRoundedCornerState();
3893 snapshot->stretchEffect = getStretchEffect();
3894 snapshot->transformedBounds = mScreenBounds;
3895 if (mEffectiveShadowRadius > 0.f) {
3896 snapshot->shadowSettings = mFlinger->mDrawingState.globalShadowSettings;
3897
3898 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
3899 // it if transparent regions are present. This may not be necessary since shadows are
3900 // typically cast by layers without transparent regions.
3901 snapshot->shadowSettings.boundaries = mBounds;
3902
3903 const float casterAlpha = snapshot->alpha;
3904 const bool casterIsOpaque =
3905 ((mBufferInfo.mBuffer != nullptr) && isOpaque(mDrawingState));
3906
3907 // If the casting layer is translucent, we need to fill in the shadow underneath the
3908 // layer. Otherwise the generated shadow will only be shown around the casting layer.
3909 snapshot->shadowSettings.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
3910 snapshot->shadowSettings.ambientColor *= casterAlpha;
3911 snapshot->shadowSettings.spotColor *= casterAlpha;
3912 }
3913 snapshot->shadowSettings.length = mEffectiveShadowRadius;
Vishnu Nairba354102022-08-01 00:14:18 +00003914 }
Vishnu Nairbedb44b2022-08-02 21:47:40 +00003915 snapshot->contentOpaque = isOpaque(mDrawingState);
3916 snapshot->isHdrY410 = isHdrY410();
3917 snapshot->bufferNeedsFiltering = bufferNeedsFiltering();
3918 sp<Layer> p = mDrawingParent.promote();
3919 if (p != nullptr) {
3920 snapshot->transform = p->getTransform();
3921 } else {
3922 snapshot->transform.reset();
3923 }
3924 snapshot->bufferSize = getBufferSize(mDrawingState);
3925 snapshot->externalTexture = mBufferInfo.mBuffer;
Vishnu Naire14c6b32022-08-06 04:20:15 +00003926 snapshot->hasReadyFrame = hasReadyFrame();
Vishnu Nairba354102022-08-01 00:14:18 +00003927 preparePerFrameCompositionState();
3928}
3929
Vishnu Naire14c6b32022-08-06 04:20:15 +00003930void Layer::updateChildrenSnapshots(bool updateGeometry) {
3931 for (const sp<Layer>& child : mDrawingChildren) {
3932 child->updateSnapshot(updateGeometry);
3933 child->updateChildrenSnapshots(updateGeometry);
3934 }
3935}
3936
Vishnu Nair0a4fb002022-08-08 02:40:42 +00003937void Layer::updateMetadataSnapshot(const LayerMetadata& parentMetadata) {
3938 mSnapshot->layerMetadata = parentMetadata;
3939 mSnapshot->layerMetadata.merge(mDrawingState.metadata);
3940 for (const sp<Layer>& child : mDrawingChildren) {
3941 child->updateMetadataSnapshot(mSnapshot->layerMetadata);
3942 }
3943}
3944
3945void Layer::updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMetadata,
3946 std::unordered_set<Layer*>& visited) {
3947 if (visited.find(this) != visited.end()) {
3948 ALOGW("Cycle containing layer %s detected in z-order relatives", getDebugName());
3949 return;
3950 }
3951 visited.insert(this);
3952
3953 mSnapshot->relativeLayerMetadata = relativeLayerMetadata;
3954
3955 if (mDrawingState.zOrderRelatives.empty()) {
3956 return;
3957 }
3958 LayerMetadata childRelativeLayerMetadata = mSnapshot->relativeLayerMetadata;
3959 childRelativeLayerMetadata.merge(mSnapshot->layerMetadata);
3960 for (wp<Layer> weakRelative : mDrawingState.zOrderRelatives) {
3961 sp<Layer> relative = weakRelative.promote();
3962 if (!relative) {
3963 continue;
3964 }
3965 relative->updateRelativeMetadataSnapshot(childRelativeLayerMetadata, visited);
3966 }
3967}
3968
Patrick Williams377c0142022-10-13 17:33:38 +00003969LayerSnapshotGuard::LayerSnapshotGuard(Layer* layer) : mLayer(layer) {
Patrick Williams4da60852022-11-17 17:19:38 +00003970 if (mLayer) {
3971 mLayer->mLayerFE->mSnapshot = std::move(mLayer->mSnapshot);
3972 }
Patrick Williams377c0142022-10-13 17:33:38 +00003973}
3974
3975LayerSnapshotGuard::~LayerSnapshotGuard() {
3976 if (mLayer) {
3977 mLayer->mSnapshot = std::move(mLayer->mLayerFE->mSnapshot);
3978 }
3979}
3980
3981LayerSnapshotGuard::LayerSnapshotGuard(LayerSnapshotGuard&& other) : mLayer(other.mLayer) {
3982 other.mLayer = nullptr;
3983}
3984
3985LayerSnapshotGuard& LayerSnapshotGuard::operator=(LayerSnapshotGuard&& other) {
3986 mLayer = other.mLayer;
3987 other.mLayer = nullptr;
3988 return *this;
3989}
3990
Mathias Agopian13127d82013-03-05 17:47:11 -08003991// ---------------------------------------------------------------------------
3992
Marin Shalamanov46084422020-10-13 12:33:42 +02003993std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07003994 return stream << "{rate=" << rate.rate << " type=" << ftl::enum_string(rate.type)
3995 << " seamlessness=" << ftl::enum_string(rate.seamlessness) << '}';
Marin Shalamanov46084422020-10-13 12:33:42 +02003996}
3997
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -07003998} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003999
4000#if defined(__gl_h_)
4001#error "don't include gl/gl.h in this file"
4002#endif
4003
4004#if defined(__gl2_h_)
4005#error "don't include gl2/gl2.h in this file"
4006#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08004007
4008// TODO(b/129481165): remove the #pragma below and fix conversion issues
4009#pragma clang diagnostic pop // ignored "-Wconversion"