blob: a207d928e287ad2b4bf0b3dfa742d2cfefe0c0f1 [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>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/Display.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080033#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/OutputLayer.h>
35#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070042#include <math.h>
chaviw250bcbb2020-08-05 11:17:54 -070043#include <private/android_filesystem_config.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080044#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070045#include <stdint.h>
46#include <stdlib.h>
47#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080048#include <ui/DebugUtils.h>
49#include <ui/GraphicBuffer.h>
50#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/Errors.h>
52#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080053#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Alec Mourie60041e2019-06-14 18:59:51 -070057#include <algorithm>
58#include <mutex>
59#include <sstream>
60
Yiwei Zhang60d1a192018-03-07 14:52:28 -080061#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080064#include "DisplayHardware/HWComposer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080065#include "EffectLayer.h"
Ady Abraham22c7b5c2020-09-22 19:33:40 -070066#include "FrameTimeline.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070067#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080068#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070069#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070070#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080072#include "TimeStats/TimeStats.h"
Robert Carr3e2a2992021-06-11 13:42:55 -070073#include "TunnelModeEnabledReporter.h"
Michael Wright44753b12020-07-08 13:48:11 +010074#include "input/InputWindow.h"
Ramakant Singh54ec1eb2020-03-26 15:35:26 +053075#include "QtiGralloc.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070076
David Sodman41fdfc92017-11-06 16:09:56 -080077#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078
Ramakant Singh54ec1eb2020-03-26 15:35:26 +053079using android::hardware::graphics::common::V1_0::BufferUsage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010081namespace {
82constexpr int kDumpTableRowLength = 159;
83} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084
Yiwei Zhang5434a782018-12-05 18:06:32 -080085using base::StringAppendF;
Michael Wright44753b12020-07-08 13:48:11 +010086using namespace android::flag_operators;
Ady Abraham22c7b5c2020-09-22 19:33:40 -070087using PresentState = frametimeline::SurfaceFrame::PresentState;
Yiwei Zhang5434a782018-12-05 18:06:32 -080088
Lloyd Piquef1c675b2018-09-12 20:45:39 -070089std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080090
Lloyd Pique42ab75e2018-09-12 20:46:03 -070091Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070092 : mFlinger(args.flinger),
93 mName(args.name),
94 mClientRef(args.client),
Michael Wright44753b12020-07-08 13:48:11 +010095 mWindowType(static_cast<InputWindowInfo::Type>(
96 args.metadata.getInt32(METADATA_WINDOW_TYPE, 0))) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070097 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070098 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
99 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
100 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -0700101 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
102 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700103
Robert Carr6a160312021-05-17 12:08:20 -0700104 mDrawingState.active_legacy.w = args.w;
105 mDrawingState.active_legacy.h = args.h;
106 mDrawingState.flags = layerFlags;
107 mDrawingState.active_legacy.transform.set(0, 0);
108 mDrawingState.crop.makeInvalid();
109 mDrawingState.requestedCrop = mDrawingState.crop;
110 mDrawingState.z = 0;
111 mDrawingState.color.a = 1.0f;
112 mDrawingState.layerStack = 0;
113 mDrawingState.sequence = 0;
114 mDrawingState.requested_legacy = mDrawingState.active_legacy;
115 mDrawingState.width = UINT32_MAX;
116 mDrawingState.height = UINT32_MAX;
117 mDrawingState.transform.set(0, 0);
118 mDrawingState.frameNumber = 0;
119 mDrawingState.bufferTransform = 0;
120 mDrawingState.transformToDisplayInverse = false;
121 mDrawingState.crop.makeInvalid();
122 mDrawingState.acquireFence = sp<Fence>::make(-1);
123 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
124 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
125 mDrawingState.hdrMetadata.validTypes = 0;
126 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
127 mDrawingState.cornerRadius = 0.0f;
128 mDrawingState.backgroundBlurRadius = 0;
129 mDrawingState.api = -1;
130 mDrawingState.hasColorTransform = false;
131 mDrawingState.colorSpaceAgnostic = false;
132 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
133 mDrawingState.metadata = args.metadata;
134 mDrawingState.shadowRadius = 0.f;
135 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
136 mDrawingState.frameTimelineInfo = {};
137 mDrawingState.postTime = -1;
Vishnu Nair2e5b3262022-01-27 17:13:18 +0000138 mDrawingState.dropInputMode = gui::DropInputMode::NONE;
Robert Carr6a160312021-05-17 12:08:20 -0700139 mDrawingState.destinationFrame.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700140
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700141 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
142 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700143 mDrawingState.color.r = -1.0_hf;
144 mDrawingState.color.g = -1.0_hf;
145 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700146 }
147
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800148 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700149 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800150 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200151 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700152
Vishnu Nair0f085c62019-08-30 08:49:12 -0700153 mCallingPid = args.callingPid;
154 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700155
156 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
157 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
158 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700159 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700160 } else {
161 // A create layer request from a non system request cannot specify the owner uid
162 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700163 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700164 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800165}
166
167void Layer::onFirstRef() {
168 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700169}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700170
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700171Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800172 sp<Client> c(mClientRef.promote());
173 if (c != 0) {
174 c->detachLayer(this);
175 }
176
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000177 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700178 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700179
180 if (mDrawingState.sidebandStream != nullptr) {
181 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
182 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700183 if (mHadClonedChild) {
184 mFlinger->mNumClones--;
185 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700186}
187
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700188LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
189 uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700190 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700191 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700192 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700193 w(w),
194 h(h),
195 flags(flags),
196 metadata(std::move(metadata)) {
197 IPCThreadState* ipc = IPCThreadState::self();
198 callingPid = ipc->getCallingPid();
199 callingUid = ipc->getCallingUid();
200}
201
Mathias Agopian13127d82013-03-05 17:47:11 -0800202// ---------------------------------------------------------------------------
203// callbacks
204// ---------------------------------------------------------------------------
205
David Sodmaneb085e02017-10-05 18:49:04 -0700206/*
207 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
208 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800209 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700210 */
David Sodmaneb085e02017-10-05 18:49:04 -0700211void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800212
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700213void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700214 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700215 return;
216 }
Robert Carr2e102c92018-10-23 12:11:15 -0700217
Robert Carr6a160312021-05-17 12:08:20 -0700218 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700219 if (strongRelative == nullptr) {
220 setZOrderRelativeOf(nullptr);
221 return;
222 }
223
224 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
225 strongRelative->removeZOrderRelative(this);
226 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800227 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700228 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700229}
230
231void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700232 if (!mRemovedFromDrawingState) {
233 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700234 mFlinger->mScheduler->deregisterLayer(this);
235 }
Rob Carr4bba3702018-10-08 21:53:30 +0000236
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800237 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700238}
Chia-I Wu38512252017-05-17 14:36:16 -0700239
chaviw68d4dab2020-06-08 15:07:32 -0700240sp<Layer> Layer::getRootLayer() {
241 sp<Layer> parent = getParent();
242 if (parent == nullptr) {
243 return this;
244 }
245 return parent->getRootLayer();
246}
247
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700248void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700249 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
250 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700251 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700252
253 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700254 layer->removeFromCurrentState();
255 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700256 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700257}
258
chaviw61626f22018-11-15 16:26:27 -0800259void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700260 if (mRemovedFromDrawingState) {
261 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700262 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700263 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700264 }
chaviw61626f22018-11-15 16:26:27 -0800265
266 for (const auto& child : mCurrentChildren) {
267 child->addToCurrentState();
268 }
269}
270
Mathias Agopian13127d82013-03-05 17:47:11 -0800271// ---------------------------------------------------------------------------
272// set-up
273// ---------------------------------------------------------------------------
274
chaviw13fdc492017-06-27 12:40:18 -0700275bool Layer::getPremultipledAlpha() const {
276 return mPremultipliedAlpha;
277}
278
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700279sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800280 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700281 if (mGetHandleCalled) {
282 ALOGE("Get handle called twice" );
283 return nullptr;
284 }
285 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700286 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800287}
288
289// ---------------------------------------------------------------------------
290// h/w composer set-up
291// ---------------------------------------------------------------------------
292
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700293static Rect reduce(const Rect& win, const Region& exclude) {
294 if (CC_LIKELY(exclude.isEmpty())) {
295 return win;
296 }
297 if (exclude.isRect()) {
298 return win.reduce(exclude.getBounds());
299 }
300 return Region(win).subtract(exclude).getBounds();
301}
302
Dan Stoza80d61162017-12-20 15:57:52 -0800303static FloatRect reduce(const FloatRect& win, const Region& exclude) {
304 if (CC_LIKELY(exclude.isEmpty())) {
305 return win;
306 }
307 // Convert through Rect (by rounding) for lack of FloatRegion
308 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
309}
310
Vishnu Nair4351ad52019-02-11 14:13:02 -0800311Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800312 if (!reduceTransparentRegion) {
313 return Rect{mScreenBounds};
314 }
315
316 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800317 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800318 // Transform to screen space.
319 bounds = t.transform(bounds);
320 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700321}
322
Vishnu Nair4351ad52019-02-11 14:13:02 -0800323FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000324 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800325 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700326}
327
Vishnu Nairf0c28512019-02-08 12:40:28 -0800328FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
329 // Subtract the transparent region and snap to the bounds.
330 return reduce(mBounds, activeTransparentRegion);
331}
332
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700333void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
334 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800335 const State& s(getDrawingState());
336
337 // Calculate effective layer transform
338 mEffectiveTransform = parentTransform * getActiveTransform(s);
339
340 // Transform parent bounds to layer space
341 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
342
Vishnu Nairc652ff82019-03-15 12:48:54 -0700343 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800344 mSourceBounds = computeSourceBounds(parentBounds);
345
346 // Calculate bounds by croping diplay frame with layer crop and parent bounds
347 FloatRect bounds = mSourceBounds;
348 const Rect layerCrop = getCrop(s);
349 if (!layerCrop.isEmpty()) {
350 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
351 }
352 bounds = bounds.intersect(parentBounds);
353
354 mBounds = bounds;
355 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700356
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700357 // Use the layer's own shadow radius if set. Otherwise get the radius from
358 // parent.
359 if (s.shadowRadius > 0.f) {
360 mEffectiveShadowRadius = s.shadowRadius;
361 } else {
362 mEffectiveShadowRadius = parentShadowRadius;
363 }
364
365 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
366 // don't pass it to its children.
367 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
368
Vishnu Nair4351ad52019-02-11 14:13:02 -0800369 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700370 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800371 }
372}
373
Vishnu Nair60356342018-11-13 13:00:45 -0800374Rect Layer::getCroppedBufferSize(const State& s) const {
375 Rect size = getBufferSize(s);
376 Rect crop = getCrop(s);
377 if (!crop.isEmpty() && size.isValid()) {
378 size.intersect(crop, &size);
379 } else if (!crop.isEmpty()) {
380 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700381 }
Vishnu Nair60356342018-11-13 13:00:45 -0800382 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800383}
384
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700385void Layer::setupRoundedCornersCropCoordinates(Rect win,
386 const FloatRect& roundedCornersCrop) const {
387 // Translate win by the rounded corners rect coordinates, to have all values in
388 // layer coordinate space.
389 win.left -= roundedCornersCrop.left;
390 win.right -= roundedCornersCrop.left;
391 win.top -= roundedCornersCrop.top;
392 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700393}
394
Lloyd Piquede196652020-01-22 17:29:58 -0800395void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800396 const auto& drawingState{getDrawingState()};
397 const uint32_t layerStack = getLayerStack();
398 const auto alpha = static_cast<float>(getAlpha());
399 const bool opaque = isOpaque(drawingState);
400 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
401
402 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
403 if (!opaque || alpha != 1.0f) {
404 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
405 : Hwc2::IComposerClient::BlendMode::COVERAGE;
406 }
407
Lloyd Piquede196652020-01-22 17:29:58 -0800408 auto* compositionState = editCompositionState();
409 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800410 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800411 compositionState->internalOnly = getPrimaryDisplayOnly();
412 compositionState->isVisible = isVisible();
413 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
414 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800415
Lloyd Piquede196652020-01-22 17:29:58 -0800416 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800417 contentDirty = false;
418
Lloyd Piquede196652020-01-22 17:29:58 -0800419 compositionState->geomLayerBounds = mBounds;
420 compositionState->geomLayerTransform = getTransform();
421 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
422 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800423
Lloyd Piquede196652020-01-22 17:29:58 -0800424 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
425 compositionState->alpha = alpha;
426 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700427 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500428 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800429}
430
Lloyd Piquede196652020-01-22 17:29:58 -0800431void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800432 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800433
Lloyd Piquea83776c2019-01-29 18:42:32 -0800434 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
435 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700436 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400437 if (parent.get()) {
438 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800439 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
440 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700441 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800442 type = parentType;
443 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700444 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400445 }
446
Lloyd Piquede196652020-01-22 17:29:58 -0800447 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700448
Lloyd Piquede196652020-01-22 17:29:58 -0800449 compositionState->geomBufferSize = getBufferSize(drawingState);
450 compositionState->geomContentCrop = getBufferCrop();
451 compositionState->geomCrop = getCrop(drawingState);
452 compositionState->geomBufferTransform = getBufferTransform();
453 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
454 compositionState->geomUsesSourceCrop = usesSourceCrop();
455 compositionState->isSecure = isSecure();
Ramakant Singh54ec1eb2020-03-26 15:35:26 +0530456 compositionState->isSecureDisplay = isSecureDisplay();
457 compositionState->isSecureCamera = isSecureCamera();
Ramakant Singh3e6a04a2020-05-30 20:37:11 +0530458 compositionState->isScreenshot = isScreenshot();
Lloyd Piquede196652020-01-22 17:29:58 -0800459
Ray Zhang49d5d0d2020-03-27 19:53:33 +0800460 compositionState->layerClass = mLayerClass;
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800461
462 compositionState->metadata.clear();
463 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
464 for (const auto& [key, mandatory] : supportedMetadata) {
465 const auto& genericLayerMetadataCompatibilityMap =
466 mFlinger->getGenericLayerMetadataKeyMap();
467 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
468 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
469 continue;
470 }
471 const uint32_t id = compatIter->second;
472
473 auto it = drawingState.metadata.mMap.find(id);
474 if (it == std::end(drawingState.metadata.mMap)) {
475 continue;
476 }
477
478 compositionState->metadata
479 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
480 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800481}
David Sodmanba340492018-08-05 21:51:33 -0700482
Lloyd Piquede196652020-01-22 17:29:58 -0800483void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800484 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800485 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800486
Lloyd Piquede196652020-01-22 17:29:58 -0800487 compositionState->forceClientComposition = false;
488
489 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
490 compositionState->dataspace = getDataSpace();
491 compositionState->colorTransform = getColorTransform();
492 compositionState->colorTransformIsIdentity = !hasColorTransform();
493 compositionState->surfaceDamage = surfaceDamageRegion;
494 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800495
496 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700497
Lloyd Piquede196652020-01-22 17:29:58 -0800498 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800499 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800500
501 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400502 // Rounded corners no longer force client composition, since we may use a
503 // hole punch so that the layer will appear to have rounded corners.
504 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500505 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800506 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800507 }
508}
509
Lloyd Piquede196652020-01-22 17:29:58 -0800510void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800511 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800512 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800513
514 // Apply the layer's transform, followed by the display's global transform
515 // Here we're guaranteed that the layer's transform preserves rects
516 Rect win = getCroppedBufferSize(drawingState);
517 // Subtract the transparent region and snap to the bounds
518 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
519 Rect frame(getTransform().transform(bounds));
520
Lloyd Piquede196652020-01-22 17:29:58 -0800521 compositionState->cursorFrame = frame;
522}
523
524sp<compositionengine::LayerFE> Layer::asLayerFE() const {
525 return const_cast<compositionengine::LayerFE*>(
526 static_cast<const compositionengine::LayerFE*>(this));
527}
528
529sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
530 return nullptr;
531}
532
533compositionengine::LayerFECompositionState* Layer::editCompositionState() {
534 return nullptr;
535}
536
537const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
538 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800539}
540
Lloyd Piquef16688f2019-02-19 17:47:57 -0800541bool Layer::onPreComposition(nsecs_t) {
542 return false;
543}
544
Lloyd Piquede196652020-01-22 17:29:58 -0800545void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800546 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800547
Lloyd Piquec6687342019-03-07 21:34:57 -0800548 switch (subset) {
549 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800550 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800551 break;
552
553 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800554 prepareBasicGeometryCompositionState();
555 prepareGeometryCompositionState();
556 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800557 break;
558
559 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800560 preparePerFrameCompositionState();
561 break;
562
563 case StateSubset::Cursor:
564 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800565 break;
566 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800567}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700568
Lloyd Piquea83776c2019-01-29 18:42:32 -0800569const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700570 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800571}
572
Mathias Agopian13127d82013-03-05 17:47:11 -0800573// ---------------------------------------------------------------------------
574// drawing...
575// ---------------------------------------------------------------------------
576
Vishnu Nair9b079a22020-01-21 14:36:08 -0800577std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100578 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800579 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800580 return {};
581 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800582
Vishnu Nair4351ad52019-02-11 14:13:02 -0800583 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000584 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800585
586 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800587 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700588 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000589
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400590 // skip drawing content if the targetSettings indicate the content will be occluded
Ady Abrahambaa6cf52021-07-13 14:02:19 -0700591 const bool drawContent = targetSettings.realContentIsVisible || targetSettings.clearContent;
592 layerSettings.skipContentDraw = !drawContent;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400593
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000594 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800595 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000596 }
597
598 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800599 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
600 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000601
Lloyd Piquef16688f2019-02-19 17:47:57 -0800602 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700603 layerSettings.sourceDataspace = getDataSpace();
Alec Mourif54453c2021-05-13 16:28:28 -0700604 switch (targetSettings.blurSetting) {
605 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Enabled:
606 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
607 layerSettings.blurRegions = getBlurRegions();
608 layerSettings.blurRegionTransform =
609 getActiveTransform(getDrawingState()).inverse().asMatrix4();
610 break;
611 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BackgroundBlurOnly:
612 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
613 break;
614 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BlurRegionsOnly:
615 layerSettings.blurRegions = getBlurRegions();
616 layerSettings.blurRegionTransform =
617 getActiveTransform(getDrawingState()).inverse().asMatrix4();
618 break;
619 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled:
620 default:
621 break;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100622 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700623 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800624 // Record the name of the layer for debugging further down the stack.
625 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800626 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800627}
628
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800629void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
630 bool blackout) const {
631 layerSettings.source.buffer.buffer = nullptr;
632 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
633 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800634 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800635 layerSettings.frameNumber = 0;
636
637 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
638 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800639 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800640}
641
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400642// TODO(b/188891810): This method now only ever returns 0 or 1 layers so we should return
643// std::optional instead of a vector. Additionally, we should consider removing
644// this method entirely in favor of calling prepareClientComposition directly.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800645std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
646 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
647 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
648 prepareClientComposition(targetSettings);
649 // Nothing to render.
650 if (!layerSettings) {
651 return {};
652 }
653
654 // HWC requests to clear this layer.
655 if (targetSettings.clearContent) {
656 prepareClearClientComposition(*layerSettings, false /* blackout */);
657 return {*layerSettings};
658 }
659
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400660 // set the shadow for the layer if needed
661 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800662
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400663 return {*layerSettings};
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800664}
665
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700666Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
667 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800668 if (outputLayer == nullptr) {
669 return Hwc2::IComposerClient::Composition::INVALID;
670 }
671 if (outputLayer->getState().hwc) {
672 return (*outputLayer->getState().hwc).hwcCompositionType;
673 } else {
674 return Hwc2::IComposerClient::Composition::CLIENT;
675 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676}
677
Mathias Agopian13127d82013-03-05 17:47:11 -0800678// ----------------------------------------------------------------------------
679// local state
680// ----------------------------------------------------------------------------
681
David Sodman41fdfc92017-11-06 16:09:56 -0800682bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800683 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700684 if (s.flags & layer_state_t::eLayerSecure) {
685 return true;
686 }
687
688 const auto p = mDrawingParent.promote();
689 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700690}
691
Ramakant Singh54ec1eb2020-03-26 15:35:26 +0530692bool Layer::isSecureDisplay() const {
693 sp<const GraphicBuffer> buffer = getBuffer();
694 return buffer && (buffer->getUsage() & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY);
695}
696
697bool Layer::isSecureCamera() const {
698 sp<const GraphicBuffer> buffer = getBuffer();
699 bool protected_buffer = buffer && (buffer->getUsage() & BufferUsage::PROTECTED);
700 bool camera_output = buffer && (buffer->getUsage() & BufferUsage::CAMERA_OUTPUT);
701 return protected_buffer && camera_output;
702}
Ramakant Singh3e6a04a2020-05-30 20:37:11 +0530703
704bool Layer::isScreenshot() const {
Mathew Joseph Karimpanal1370fca2021-11-22 14:37:38 +0530705 return (isScreenshotName(getName()));
Ramakant Singh3e6a04a2020-05-30 20:37:11 +0530706}
Mathew Joseph Karimpanal1370fca2021-11-22 14:37:38 +0530707
708bool Layer::isScreenshotName(std::string layer_name) {
709 return ((layer_name.find("ScreenshotSurface") != std::string::npos) ||
710 (layer_name.find("RotationLayer") != std::string::npos) ||
711 (layer_name.find("BackColorSurface") != std::string::npos));
712}
713
Mathias Agopian13127d82013-03-05 17:47:11 -0800714// ----------------------------------------------------------------------------
715// transaction
716// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800717
Marissa Wall61c58622018-07-18 10:12:20 -0700718uint32_t Layer::doTransaction(uint32_t flags) {
719 ATRACE_CALL();
720
Robert Carr0758e5d2021-03-11 22:15:04 -0800721 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700722 mDrawingStateModified = mDrawingState.modified;
723 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700724
Alec Mourib416efd2018-09-06 21:01:59 +0000725 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700726
Robert Carr6a160312021-05-17 12:08:20 -0700727 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800728 // invalidate and recompute the visible regions if needed
729 flags |= Layer::eVisibleRegion;
730 }
731
Robert Carr6a160312021-05-17 12:08:20 -0700732 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800733 // invalidate and recompute the visible regions if needed
Robert Carr6a160312021-05-17 12:08:20 -0700734 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800735 flags |= eVisibleRegion;
736 this->contentDirty = true;
737
738 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700739 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800740 }
741
Robert Carr6a160312021-05-17 12:08:20 -0700742 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800743
Mathias Agopian13127d82013-03-05 17:47:11 -0800744 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745}
746
Robert Carr6a160312021-05-17 12:08:20 -0700747void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000748 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
749 // bufferSurfaceFrameTX will be presented in latchBuffer.
750 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
751 if (surfaceFrame->getPresentState() != PresentState::Presented) {
752 // With applyPendingStates, we could end up having presented surfaceframes from previous
753 // states
754 surfaceFrame->setPresentState(PresentState::Presented);
755 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
756 }
757 }
Robert Carr6a160312021-05-17 12:08:20 -0700758 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700759}
760
Mathias Agopian13127d82013-03-05 17:47:11 -0800761uint32_t Layer::getTransactionFlags(uint32_t flags) {
Robert Carr38d25002021-06-11 14:30:09 -0700762 auto ret = mTransactionFlags & flags;
763 mTransactionFlags &= ~flags;
764 return ret;
Mathias Agopian13127d82013-03-05 17:47:11 -0800765}
766
767uint32_t Layer::setTransactionFlags(uint32_t flags) {
Robert Carr38d25002021-06-11 14:30:09 -0700768 return mTransactionFlags |= flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800769}
770
chaviw214c89d2019-09-04 16:03:53 -0700771bool Layer::setPosition(float x, float y) {
Robert Carr6a160312021-05-17 12:08:20 -0700772 if (mDrawingState.transform.tx() == x && mDrawingState.transform.ty() == y) return false;
773 mDrawingState.sequence++;
774 mDrawingState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700775
Robert Carr6a160312021-05-17 12:08:20 -0700776 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800777 setTransactionFlags(eTransactionNeeded);
778 return true;
779}
Robert Carr82364e32016-05-15 11:27:47 -0700780
Robert Carr1f0a16a2016-10-24 16:27:39 -0700781bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
782 ssize_t idx = mCurrentChildren.indexOf(childLayer);
783 if (idx < 0) {
784 return false;
785 }
786 if (childLayer->setLayer(z)) {
787 mCurrentChildren.removeAt(idx);
788 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800789 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700790 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800791 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700792}
793
Robert Carr503c7042017-09-27 15:06:08 -0700794bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
795 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
796 ssize_t idx = mCurrentChildren.indexOf(childLayer);
797 if (idx < 0) {
798 return false;
799 }
800 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
801 mCurrentChildren.removeAt(idx);
802 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800803 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700804 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800805 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700806}
807
Robert Carrae060832016-11-28 10:51:00 -0800808bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700809 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
810 mDrawingState.sequence++;
811 mDrawingState.z = z;
812 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700813
Robert Carra70e91c2021-06-11 13:59:52 -0700814 mFlinger->mSomeChildrenChanged = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700815
816 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700817 if (mDrawingState.zOrderRelativeOf != nullptr) {
818 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700819 if (strongRelative != nullptr) {
820 strongRelative->removeZOrderRelative(this);
821 }
chaviw606e5cf2019-03-01 10:12:10 -0800822 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700823 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800824 setTransactionFlags(eTransactionNeeded);
825 return true;
826}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700827
Robert Carrdb66e622017-04-10 16:55:57 -0700828void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700829 mDrawingState.zOrderRelatives.remove(relative);
830 mDrawingState.sequence++;
831 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700832 setTransactionFlags(eTransactionNeeded);
833}
834
835void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700836 mDrawingState.zOrderRelatives.add(relative);
837 mDrawingState.modified = true;
838 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700839 setTransactionFlags(eTransactionNeeded);
840}
841
chaviw606e5cf2019-03-01 10:12:10 -0800842void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700843 mDrawingState.zOrderRelativeOf = relativeOf;
844 mDrawingState.sequence++;
845 mDrawingState.modified = true;
846 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700847
chaviw606e5cf2019-03-01 10:12:10 -0800848 setTransactionFlags(eTransactionNeeded);
849}
850
Robert Carr503d2bd2017-12-04 15:49:47 -0800851bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Naira8c7c542021-07-20 18:49:42 -0700852 sp<Layer> relative = fromHandle(relativeToHandle).promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700853 if (relative == nullptr) {
854 return false;
855 }
856
Robert Carr6a160312021-05-17 12:08:20 -0700857 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
858 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800859 return false;
860 }
861
Robert Carra70e91c2021-06-11 13:59:52 -0700862 mFlinger->mSomeChildrenChanged = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700863
Robert Carr6a160312021-05-17 12:08:20 -0700864 mDrawingState.sequence++;
865 mDrawingState.modified = true;
866 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700867
Robert Carr6a160312021-05-17 12:08:20 -0700868 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700869 if (oldZOrderRelativeOf != nullptr) {
870 oldZOrderRelativeOf->removeZOrderRelative(this);
871 }
chaviw606e5cf2019-03-01 10:12:10 -0800872 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700873 relative->addZOrderRelative(this);
874
875 setTransactionFlags(eTransactionNeeded);
876
877 return true;
878}
879
Winson Chunga30f7c92021-06-29 15:42:56 -0700880bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
881 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
882 mDrawingState.isTrustedOverlay = isTrustedOverlay;
883 mDrawingState.modified = true;
884 mFlinger->mInputInfoChanged = true;
885 setTransactionFlags(eTransactionNeeded);
886 return true;
887}
888
889bool Layer::isTrustedOverlay() const {
890 if (getDrawingState().isTrustedOverlay) {
891 return true;
892 }
893 const auto& p = mDrawingParent.promote();
894 return (p != nullptr) && p->isTrustedOverlay();
895}
896
Mathias Agopian13127d82013-03-05 17:47:11 -0800897bool Layer::setSize(uint32_t w, uint32_t h) {
Robert Carr6a160312021-05-17 12:08:20 -0700898 if (mDrawingState.requested_legacy.w == w && mDrawingState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700899 return false;
Robert Carr6a160312021-05-17 12:08:20 -0700900 mDrawingState.requested_legacy.w = w;
901 mDrawingState.requested_legacy.h = h;
902 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800903 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700904
905 // record the new size, from this point on, when the client request
906 // a buffer, it'll get the new size.
Robert Carr6a160312021-05-17 12:08:20 -0700907 setDefaultBufferSize(mDrawingState.requested_legacy.w, mDrawingState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800908 return true;
909}
chaviw4e765532021-04-30 12:11:39 -0500910
Dan Stoza9e56aa02015-11-02 13:00:03 -0800911bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700912 if (mDrawingState.color.a == alpha) return false;
913 mDrawingState.sequence++;
914 mDrawingState.color.a = alpha;
915 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800916 setTransactionFlags(eTransactionNeeded);
917 return true;
918}
chaviw13fdc492017-06-27 12:40:18 -0700919
Valerie Haudd0b7572019-01-29 14:59:27 -0800920bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700921 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700922 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800923 }
Robert Carr6a160312021-05-17 12:08:20 -0700924 mDrawingState.sequence++;
925 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800926 setTransactionFlags(eTransactionNeeded);
927
Robert Carr6a160312021-05-17 12:08:20 -0700928 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800929 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800930 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700931 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700932 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700933 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
934 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700935
Valerie Haudd0b7572019-01-29 14:59:27 -0800936 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700937 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800938 mFlinger->mLayersAdded = true;
939 // set up SF to handle added color layer
940 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700941 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800942 }
943 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700944 } else if (mDrawingState.bgColorLayer && alpha == 0) {
945 mDrawingState.bgColorLayer->reparent(nullptr);
946 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800947 return true;
948 }
949
Robert Carr6a160312021-05-17 12:08:20 -0700950 mDrawingState.bgColorLayer->setColor(color);
951 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
952 mDrawingState.bgColorLayer->setAlpha(alpha);
953 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800954
chaviw13fdc492017-06-27 12:40:18 -0700955 return true;
956}
957
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700958bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700959 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700960
Robert Carr6a160312021-05-17 12:08:20 -0700961 mDrawingState.sequence++;
962 mDrawingState.cornerRadius = cornerRadius;
963 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700964 setTransactionFlags(eTransactionNeeded);
965 return true;
966}
967
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800968bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700969 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800970
Robert Carr6a160312021-05-17 12:08:20 -0700971 mDrawingState.sequence++;
972 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
973 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800974 setTransactionFlags(eTransactionNeeded);
975 return true;
976}
977
Robert Carrd4ae7f32018-06-07 16:10:57 -0700978bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
979 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700980 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -0700981 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
982
983 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -0800984 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
985 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -0700986 return false;
987 }
Robert Carr6a160312021-05-17 12:08:20 -0700988 mDrawingState.sequence++;
989 mDrawingState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
990 mDrawingState.modified = true;
991
Mathias Agopian13127d82013-03-05 17:47:11 -0800992 setTransactionFlags(eTransactionNeeded);
993 return true;
994}
Marissa Wall61c58622018-07-18 10:12:20 -0700995
Mathias Agopian13127d82013-03-05 17:47:11 -0800996bool Layer::setTransparentRegionHint(const Region& transparent) {
Robert Carr6a160312021-05-17 12:08:20 -0700997 mDrawingState.requestedTransparentRegion_legacy = transparent;
998 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800999 setTransactionFlags(eTransactionNeeded);
1000 return true;
1001}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001002
Lucas Dupinc3800b82020-10-02 16:24:48 -07001003bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Robert Carr6a160312021-05-17 12:08:20 -07001004 mDrawingState.blurRegions = blurRegions;
1005 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001006 setTransactionFlags(eTransactionNeeded);
1007 return true;
1008}
1009
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001010bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -07001011 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
1012 if (mDrawingState.flags == newFlags) return false;
1013 mDrawingState.sequence++;
1014 mDrawingState.flags = newFlags;
1015 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001016 setTransactionFlags(eTransactionNeeded);
1017 return true;
1018}
Robert Carr99e27f02016-06-16 15:18:02 -07001019
chaviw25714502021-02-11 10:01:08 -08001020bool Layer::setCrop(const Rect& crop) {
Robert Carr6a160312021-05-17 12:08:20 -07001021 if (mDrawingState.requestedCrop == crop) return false;
1022 mDrawingState.sequence++;
1023 mDrawingState.requestedCrop = crop;
1024 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001025
Robert Carr6a160312021-05-17 12:08:20 -07001026 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001027 setTransactionFlags(eTransactionNeeded);
1028 return true;
1029}
Robert Carr8d5227b2017-03-16 15:41:03 -07001030
Evan Roskyef876c92019-01-25 17:46:06 -08001031bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -07001032 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
1033 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001034 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001035 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001036}
1037
Mathias Agopian13127d82013-03-05 17:47:11 -08001038bool Layer::setLayerStack(uint32_t layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -07001039 if (mDrawingState.layerStack == layerStack) return false;
1040 mDrawingState.sequence++;
1041 mDrawingState.layerStack = layerStack;
1042 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001043 setTransactionFlags(eTransactionNeeded);
1044 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001045}
1046
Peiyong Linc502cb72019-03-01 15:00:23 -08001047bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -07001048 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -08001049 return false;
1050 }
Robert Carr6a160312021-05-17 12:08:20 -07001051 mDrawingState.sequence++;
1052 mDrawingState.colorSpaceAgnostic = agnostic;
1053 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -08001054 setTransactionFlags(eTransactionNeeded);
1055 return true;
1056}
1057
Ana Krulecc84d09b2019-11-02 23:10:29 +01001058bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -07001059 if (mDrawingState.frameRateSelectionPriority == priority) return false;
1060 mDrawingState.frameRateSelectionPriority = priority;
1061 mDrawingState.sequence++;
1062 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +01001063 setTransactionFlags(eTransactionNeeded);
1064 return true;
1065}
1066
1067int32_t Layer::getFrameRateSelectionPriority() const {
1068 // Check if layer has priority set.
1069 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
Rajat Yadav70b5c422021-04-08 17:45:47 +05301070 mPriority = mDrawingState.frameRateSelectionPriority;
1071 return mPriority;
Ana Krulecc84d09b2019-11-02 23:10:29 +01001072 }
1073 // If not, search whether its parents have it set.
1074 sp<Layer> parent = getParent();
1075 if (parent != nullptr) {
Rajat Yadav70b5c422021-04-08 17:45:47 +05301076 mPriority = parent->getFrameRateSelectionPriority();
1077 return mPriority;
Ana Krulecc84d09b2019-11-02 23:10:29 +01001078 }
1079
1080 return Layer::PRIORITY_UNSET;
1081}
1082
Rajat Yadav70b5c422021-04-08 17:45:47 +05301083int32_t Layer::getPriority() {
1084 return mPriority;
1085}
1086
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001087bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1088 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1089};
1090
Robert Carr1f0a16a2016-10-24 16:27:39 -07001091uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001092 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001093 if (p == nullptr) {
1094 return getDrawingState().layerStack;
1095 }
1096 return p->getLayerStack();
1097}
1098
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001099bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001100 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001101 return false;
1102 }
1103
Robert Carr6a160312021-05-17 12:08:20 -07001104 mDrawingState.sequence++;
1105 mDrawingState.shadowRadius = shadowRadius;
1106 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001107 setTransactionFlags(eTransactionNeeded);
1108 return true;
1109}
1110
Vishnu Nair6213bd92020-05-08 17:42:25 -07001111bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001112 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001113 return false;
1114 }
1115
Robert Carr6a160312021-05-17 12:08:20 -07001116 mDrawingState.sequence++;
1117 mDrawingState.fixedTransformHint = fixedTransformHint;
1118 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001119 setTransactionFlags(eTransactionNeeded);
1120 return true;
1121}
1122
John Reckcdb4ed72021-02-04 13:39:33 -05001123bool Layer::setStretchEffect(const StretchEffect& effect) {
1124 StretchEffect temp = effect;
1125 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001126 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001127 return false;
1128 }
Robert Carr6a160312021-05-17 12:08:20 -07001129 mDrawingState.sequence++;
1130 mDrawingState.stretchEffect = temp;
1131 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001132 setTransactionFlags(eTransactionNeeded);
1133 return true;
1134}
1135
John Reckc00c6692021-02-16 11:37:33 -05001136StretchEffect Layer::getStretchEffect() const {
1137 if (mDrawingState.stretchEffect.hasEffect()) {
1138 return mDrawingState.stretchEffect;
1139 }
1140
1141 sp<Layer> parent = getParent();
1142 if (parent != nullptr) {
1143 auto effect = parent->getStretchEffect();
1144 if (effect.hasEffect()) {
1145 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1146 return effect;
1147 }
1148 }
1149 return StretchEffect{};
1150}
1151
Ady Abrahama850c182021-08-04 13:04:37 -07001152bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1153 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1154 const auto frameRate = [&] {
1155 if (mDrawingState.frameRate.rate.isValid() ||
1156 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1157 return mDrawingState.frameRate;
1158 }
1159
1160 return parentFrameRate;
1161 }();
1162
1163 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1164
1165 // The frame rate is propagated to the children
1166 bool childrenHaveFrameRate = false;
1167 for (const sp<Layer>& child : mCurrentChildren) {
1168 childrenHaveFrameRate |=
1169 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1170 }
1171
1172 // If we don't have a valid frame rate, but the children do, we set this
1173 // layer as NoVote to allow the children to control the refresh rate
1174 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1175 childrenHaveFrameRate) {
1176 *transactionNeeded |=
1177 setFrameRateForLayerTree(FrameRate(Fps(0.0f), FrameRateCompatibility::NoVote));
1178 }
1179
1180 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1181 // the same reason we are allowing touch boost for those layers. See
1182 // RefreshRateConfigs::getBestRefreshRate for more details.
1183 const auto layerVotedWithDefaultCompatibility =
1184 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1185 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1186 const auto layerVotedWithExactCompatibility =
1187 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1188 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1189 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1190}
1191
Ady Abraham60e42ea2020-03-09 19:17:31 -07001192void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001193 const auto root = [&]() -> sp<Layer> {
1194 sp<Layer> layer = this;
1195 while (auto parent = layer->getParent()) {
1196 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001197 }
Ady Abrahama850c182021-08-04 13:04:37 -07001198 return layer;
1199 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001200
Ady Abraham60e42ea2020-03-09 19:17:31 -07001201 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001202 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001203
Ady Abrahama850c182021-08-04 13:04:37 -07001204 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001205 if (transactionNeeded) {
1206 mFlinger->setTransactionFlags(eTraversalNeeded);
1207 }
1208}
1209
Ady Abraham71c437d2020-01-31 15:56:57 -08001210bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001211 if (!mFlinger->useFrameRateApi) {
1212 return false;
1213 }
Robert Carr6a160312021-05-17 12:08:20 -07001214 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001215 return false;
1216 }
1217
Robert Carr6a160312021-05-17 12:08:20 -07001218 mDrawingState.sequence++;
1219 mDrawingState.frameRate = frameRate;
1220 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001221
1222 updateTreeHasFrameRateVote();
1223
Steven Thomas3172e202020-01-06 19:25:30 -08001224 setTransactionFlags(eTransactionNeeded);
1225 return true;
1226}
1227
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001228void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1229 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001230 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001231
1232 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1233 // there are two transactions with the same token, the first one without a buffer and the
1234 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1235 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001236 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1237 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001238 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001239 mDrawingState.bufferSurfaceFrameTX = it->second;
1240 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1241 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1242 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001243 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001244 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001245 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1246 }
1247}
1248
1249void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1250 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001251 mDrawingState.frameTimelineInfo = info;
1252 mDrawingState.postTime = postTime;
1253 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001254 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001255
Robert Carr6a160312021-05-17 12:08:20 -07001256 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001257 bufferSurfaceFrameTX != nullptr) {
1258 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1259 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1260 // being used for BufferSurfaceFrame, don't create a new one.
1261 return;
1262 }
1263 }
1264 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1265 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1266 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001267 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1268 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001269 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001270 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001271 } else {
1272 if (it->second->getPresentState() == PresentState::Presented) {
1273 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1274 // have been from previous vsync.
1275 it->second = createSurfaceFrameForTransaction(info, postTime);
1276 }
1277 }
1278}
1279
1280void Layer::addSurfaceFrameDroppedForBuffer(
1281 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001282 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001283 surfaceFrame->setPresentState(PresentState::Dropped);
1284 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1285}
1286
1287void Layer::addSurfaceFramePresentedForBuffer(
1288 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1289 nsecs_t currentLatchTime) {
1290 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1291 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1292 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1293 mLastLatchTime = currentLatchTime;
1294}
1295
1296std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1297 const FrameTimelineInfo& info, nsecs_t postTime) {
1298 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001299 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1300 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001301 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001302 /*isBuffer*/ false, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001303 // For Transactions, the post time is considered to be both queue and acquire fence time.
1304 surfaceFrame->setActualQueueTime(postTime);
1305 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001306 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1307 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001308 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001309 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001310 onSurfaceFrameCreated(surfaceFrame);
1311 return surfaceFrame;
1312}
1313
1314std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1315 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1316 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001317 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001318 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001319 /*isBuffer*/ true, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001320 // For buffers, acquire fence time will set during latch.
1321 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001322 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1323 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001324 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001325 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001326 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1327 onSurfaceFrameCreated(surfaceFrame);
1328 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001329}
1330
Ady Abrahama850c182021-08-04 13:04:37 -07001331bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1332 if (mDrawingState.frameRateForLayerTree == frameRate) {
1333 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001334 }
1335
Ady Abrahama850c182021-08-04 13:04:37 -07001336 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001337
Ady Abrahama850c182021-08-04 13:04:37 -07001338 // TODO(b/195668952): we probably don't need to dirty visible regions here
1339 // or even store frameRateForLayerTree in mDrawingState
1340 mDrawingState.sequence++;
1341 mDrawingState.modified = true;
1342 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001343
Ady Abrahama850c182021-08-04 13:04:37 -07001344 mFlinger->mScheduler->recordLayerHistory(this, systemTime(),
1345 LayerHistory::LayerUpdateType::SetFrameRate);
1346
1347 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001348}
1349
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001350Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1351 return getDrawingState().frameRateForLayerTree;
1352}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353
Robert Carr1f0a16a2016-10-24 16:27:39 -07001354bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001355 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001356 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001357 if (parent != nullptr && parent->isHiddenByPolicy()) {
1358 return true;
1359 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001360 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1361 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1362 if (zOrderRelativeOf != nullptr) {
1363 if (zOrderRelativeOf->isHiddenByPolicy()) {
1364 return true;
1365 }
1366 }
1367 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001368 return s.flags & layer_state_t::eLayerHidden;
1369}
1370
David Sodman41fdfc92017-11-06 16:09:56 -08001371uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001372 // TODO: should we do something special if mSecure is set?
1373 if (mProtectedByApp) {
1374 // need a hardware-protected path to external video sink
1375 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001376 }
Riley Andrews03414a12014-07-01 14:22:59 -07001377 if (mPotentialCursor) {
1378 usage |= GraphicBuffer::USAGE_CURSOR;
1379 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001380 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001381 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001382}
1383
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001384void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1385 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1386 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001387 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001388
Vishnu Nair6213bd92020-05-08 17:42:25 -07001389 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001390}
1391
Mathias Agopian13127d82013-03-05 17:47:11 -08001392// ----------------------------------------------------------------------------
1393// debugging
1394// ----------------------------------------------------------------------------
1395
Marissa Wall61c58622018-07-18 10:12:20 -07001396// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001397LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001398 using namespace std::string_literals;
1399
Kalle Raitaa099a242017-01-11 11:17:29 -08001400 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001401 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001402 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001403 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001404 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001405 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001406 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001407
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001408 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001409 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001410 info.mLayerStack = getLayerStack();
chaviw4e765532021-04-30 12:11:39 -05001411 info.mX = ds.transform.tx();
1412 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001413 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001414 info.mWidth = ds.width;
1415 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001416 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001417 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001418 info.mFlags = ds.flags;
1419 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001420 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001421 info.mMatrix[0][0] = ds.transform[0][0];
1422 info.mMatrix[0][1] = ds.transform[0][1];
1423 info.mMatrix[1][0] = ds.transform[1][0];
1424 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001425 {
chaviwd62d3062019-09-04 14:48:02 -07001426 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001427 if (buffer != 0) {
1428 info.mActiveBufferWidth = buffer->getWidth();
1429 info.mActiveBufferHeight = buffer->getHeight();
1430 info.mActiveBufferStride = buffer->getStride();
1431 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001432 } else {
1433 info.mActiveBufferWidth = 0;
1434 info.mActiveBufferHeight = 0;
1435 info.mActiveBufferStride = 0;
1436 info.mActiveBufferFormat = 0;
1437 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001438 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001439 info.mNumQueuedFrames = getQueuedFrameCount();
1440 info.mRefreshPending = isBufferLatched();
1441 info.mIsOpaque = isOpaque(ds);
1442 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001443 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001444 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001445}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001446
Yiwei Zhang5434a782018-12-05 18:06:32 -08001447void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001448 result.append(kDumpTableRowLength, '-');
1449 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001450 result.append(" Layer name\n");
1451 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001452 result.append(" Window Type | ");
Ray Zhang49d5d0d2020-03-27 19:53:33 +08001453 result.append(" Layer Class | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001454 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001455 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001456 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001457 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001458 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1459 result.append(kDumpTableRowLength, '-');
1460 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001461}
1462
1463std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1464 switch (compatibility) {
1465 case FrameRateCompatibility::Default:
1466 return "Default";
1467 case FrameRateCompatibility::ExactOrMultiple:
1468 return "ExactOrMultiple";
1469 case FrameRateCompatibility::NoVote:
1470 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001471 case FrameRateCompatibility::Exact:
1472 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001473 }
Dan Stozae22aec72016-08-01 13:20:59 -07001474}
1475
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001476void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1477 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001478 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001479 return;
1480 }
1481
Yiwei Zhang5434a782018-12-05 18:06:32 -08001482 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001483 if (mName.length() > 77) {
1484 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001485 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001486 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001487 shortened.append(mName, mName.length() - 36);
1488 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001489 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001490 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001491 }
1492
Yiwei Zhang5434a782018-12-05 18:06:32 -08001493 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001494
Alec Mourib416efd2018-09-06 21:01:59 +00001495 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001496 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001497
Chia-I Wu1e043612018-03-01 09:45:09 -08001498 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001499 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001500 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001501 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001502 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001503 StringAppendF(&result, " %10d | ", mWindowType);
Ray Zhang49d5d0d2020-03-27 19:53:33 +08001504 StringAppendF(&result, " %10d | ", mLayerClass);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001505 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001506 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1507 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001508 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001509 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001510 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001511 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001512 const auto frameRate = getFrameRateForLayerTree();
1513 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1514 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1515 frameRateCompatibilityString(frameRate.type).c_str(),
1516 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001517 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001518 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001519 }
Dan Stozae22aec72016-08-01 13:20:59 -07001520
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001521 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1522 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1523
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001524 result.append(kDumpTableRowLength, '-');
1525 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001526}
Dan Stozae22aec72016-08-01 13:20:59 -07001527
Yiwei Zhang5434a782018-12-05 18:06:32 -08001528void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001529 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001530}
1531
Svetoslavd85084b2014-03-20 10:28:31 -07001532void Layer::clearFrameStats() {
1533 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001534}
1535
Jamie Gennis6547ff42013-07-16 20:12:42 -07001536void Layer::logFrameStats() {
1537 mFrameTracker.logAndResetStats(mName);
1538}
1539
Svetoslavd85084b2014-03-20 10:28:31 -07001540void Layer::getFrameStats(FrameStats* outStats) const {
1541 mFrameTracker.getStats(outStats);
1542}
1543
Yiwei Zhang5434a782018-12-05 18:06:32 -08001544void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001545 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001546 Mutex::Autolock lock(mFrameEventHistoryMutex);
1547 mFrameEventHistory.checkFencesForCompletion();
1548 mFrameEventHistory.dump(result);
1549}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001550
Vishnu Nair0f085c62019-08-30 08:49:12 -07001551void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001552 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1553 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001554}
1555
Brian Anderson5ea5e592016-12-01 16:54:33 -08001556void Layer::onDisconnect() {
1557 Mutex::Autolock lock(mFrameEventHistoryMutex);
1558 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001559 const int32_t layerId = getSequence();
1560 mFlinger->mTimeStats->onDestroy(layerId);
1561 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001562}
1563
Brian Anderson3890c392016-07-25 12:48:08 -07001564void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001565 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001566 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001567 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001568 getName().c_str(), mOwnerUid, newTimestamps->postedTime,
1569 getGameMode());
Yiwei Zhang487340f2019-11-18 17:42:12 -08001570 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1571 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001572 }
1573
Brian Andersond6927fb2016-07-23 23:37:30 -07001574 Mutex::Autolock lock(mFrameEventHistoryMutex);
1575 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001576 // If there are any unsignaled fences in the aquire timeline at this
1577 // point, the previously queued frame hasn't been latched yet. Go ahead
1578 // and try to get the signal time here so the syscall is taken out of
1579 // the main thread's critical path.
1580 mAcquireTimeline.updateSignalTimes();
1581 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001582 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001583 mFrameEventHistory.addQueue(*newTimestamps);
1584 }
1585
Brian Anderson3890c392016-07-25 12:48:08 -07001586 if (outDelta) {
1587 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001588 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001589}
Dan Stozae77c7662016-05-13 11:37:28 -07001590
Chia-I Wu98f1c102017-05-30 14:54:08 -07001591size_t Layer::getChildrenCount() const {
1592 size_t count = 0;
1593 for (const sp<Layer>& child : mCurrentChildren) {
1594 count += 1 + child->getChildrenCount();
1595 }
1596 return count;
1597}
1598
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001599void Layer::setGameModeForTree(int parentGameMode) {
1600 int gameMode = parentGameMode;
Robert Carr6a160312021-05-17 12:08:20 -07001601 auto& currentState = getDrawingState();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001602 if (currentState.metadata.has(METADATA_GAME_MODE)) {
1603 gameMode = currentState.metadata.getInt32(METADATA_GAME_MODE, 0);
1604 }
1605 setGameMode(gameMode);
1606 for (const sp<Layer>& child : mCurrentChildren) {
1607 child->setGameModeForTree(gameMode);
1608 }
1609}
1610
Robert Carr1f0a16a2016-10-24 16:27:39 -07001611void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001612 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001613 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001614
Robert Carr1f0a16a2016-10-24 16:27:39 -07001615 mCurrentChildren.add(layer);
1616 layer->setParent(this);
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001617 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001618 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001619}
1620
1621ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001622 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001623 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001624
Robert Carr1323c952019-01-28 18:13:27 -08001625 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001626 const auto removeResult = mCurrentChildren.remove(layer);
1627
1628 updateTreeHasFrameRateVote();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001629 layer->setGameModeForTree(0);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001630 layer->updateTreeHasFrameRateVote();
1631
1632 return removeResult;
1633}
1634
Robert Carr15eae092018-03-23 13:43:53 -07001635void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001636 for (const sp<Layer>& child : mDrawingChildren) {
1637 child->mDrawingParent = newParent;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001638 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001639 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001640 }
1641}
1642
chaviwf1961f72017-09-18 16:41:07 -07001643bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001644 sp<Layer> newParent;
1645 if (newParentHandle != nullptr) {
Vishnu Naira8c7c542021-07-20 18:49:42 -07001646 newParent = fromHandle(newParentHandle).promote();
Robert Carr54cf5b12019-01-25 14:02:28 -08001647 if (newParent == nullptr) {
1648 ALOGE("Unable to promote Layer handle");
1649 return false;
1650 }
1651 if (newParent == this) {
1652 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1653 return false;
1654 }
1655 }
1656
chaviwf1961f72017-09-18 16:41:07 -07001657 sp<Layer> parent = getParent();
1658 if (parent != nullptr) {
1659 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001660 }
1661
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001662 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001663 newParent->addChild(this);
1664 if (!newParent->isRemovedFromCurrentState()) {
1665 addToCurrentState();
1666 } else {
1667 onRemovedFromCurrentState();
1668 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001669 } else {
1670 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001671 }
1672
chaviw06178942017-07-27 10:25:59 -07001673 return true;
1674}
1675
Peiyong Lind3788632018-09-18 16:01:31 -07001676bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001677 static const mat4 identityMatrix = mat4();
1678
Robert Carr6a160312021-05-17 12:08:20 -07001679 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001680 return false;
1681 }
Robert Carr6a160312021-05-17 12:08:20 -07001682 ++mDrawingState.sequence;
1683 mDrawingState.colorTransform = matrix;
1684 mDrawingState.hasColorTransform = matrix != identityMatrix;
1685 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001686 setTransactionFlags(eTransactionNeeded);
1687 return true;
1688}
1689
chaviwf66724d2018-11-28 16:35:21 -08001690mat4 Layer::getColorTransform() const {
1691 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1692 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1693 colorTransform = parent->getColorTransform() * colorTransform;
1694 }
1695 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001696}
1697
1698bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001699 bool hasColorTransform = getDrawingState().hasColorTransform;
1700 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1701 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1702 }
1703 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001704}
1705
Chia-I Wu11481472018-05-04 10:43:19 -07001706bool Layer::isLegacyDataSpace() const {
1707 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001708 return !(getDataSpace() &
1709 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1710 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001711}
1712
Robert Carr1f0a16a2016-10-24 16:27:39 -07001713void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001714 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001715}
1716
Robert Carr6a160312021-05-17 12:08:20 -07001717int32_t Layer::getZ(LayerVector::StateSet) const {
1718 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001719}
1720
Robert Carr1c5481e2019-07-01 14:42:27 -07001721bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001722 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001723 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001724 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001725}
1726
David Sodman41fdfc92017-11-06 16:09:56 -08001727__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001728 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001729 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1730 "makeTraversalList received invalid stateSet");
1731 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1732 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001733 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001734
Robert Carr29abff82017-12-04 13:51:20 -08001735 if (state.zOrderRelatives.size() == 0) {
1736 *outSkipRelativeZUsers = true;
1737 return children;
1738 }
1739
chaviwfd462612018-05-31 16:11:27 -07001740 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001741 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001742 sp<Layer> strongRelative = weakRelative.promote();
1743 if (strongRelative != nullptr) {
1744 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001745 }
1746 }
1747
Dan Stoza412903f2017-04-27 13:42:17 -07001748 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001749 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001750 continue;
1751 }
Robert Carrdb66e622017-04-10 16:55:57 -07001752 traverse.add(child);
1753 }
1754
1755 return traverse;
1756}
1757
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758/**
Robert Carrdb66e622017-04-10 16:55:57 -07001759 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001760 */
Dan Stoza412903f2017-04-27 13:42:17 -07001761void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001762 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1763 // produce a new list for traversing, including our relatives, and not including our children
1764 // who are relatives of another surface. In the case that there are no relative Z,
1765 // makeTraversalList returns our children directly to avoid significant overhead.
1766 // However in this case we need to take the responsibility for filtering children which
1767 // are relatives of another surface here.
1768 bool skipRelativeZUsers = false;
1769 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001770
Robert Carr1f0a16a2016-10-24 16:27:39 -07001771 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001772 for (; i < list.size(); i++) {
1773 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001774 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1775 continue;
1776 }
1777
chaviw301b1d82019-11-06 13:15:09 -08001778 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001780 }
Dan Stoza412903f2017-04-27 13:42:17 -07001781 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001782 }
Robert Carr29abff82017-12-04 13:51:20 -08001783
Dan Stoza412903f2017-04-27 13:42:17 -07001784 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001785 for (; i < list.size(); i++) {
1786 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001787
Robert Carr29abff82017-12-04 13:51:20 -08001788 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1789 continue;
1790 }
Dan Stoza412903f2017-04-27 13:42:17 -07001791 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001792 }
1793}
1794
1795/**
Robert Carrdb66e622017-04-10 16:55:57 -07001796 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001797 */
Dan Stoza412903f2017-04-27 13:42:17 -07001798void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1799 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001800 // See traverseInZOrder for documentation.
1801 bool skipRelativeZUsers = false;
1802 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001803
Robert Carr1f0a16a2016-10-24 16:27:39 -07001804 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001805 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001806 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001807
1808 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1809 continue;
1810 }
1811
chaviw301b1d82019-11-06 13:15:09 -08001812 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001813 break;
1814 }
Dan Stoza412903f2017-04-27 13:42:17 -07001815 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816 }
Dan Stoza412903f2017-04-27 13:42:17 -07001817 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001818 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001819 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001820
1821 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1822 continue;
1823 }
1824
Dan Stoza412903f2017-04-27 13:42:17 -07001825 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001826 }
1827}
1828
Edgar Arriaga844fa672020-01-16 14:21:42 -08001829void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1830 visitor(this);
1831 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001832 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001833 for (const sp<Layer>& child : children) {
1834 child->traverse(state, visitor);
1835 }
1836}
1837
chaviw4b129c22018-04-09 16:19:43 -07001838LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1839 const std::vector<Layer*>& layersInTree) {
1840 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1841 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001842 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1843 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001844 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001845
chaviwfd462612018-05-31 16:11:27 -07001846 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001847 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1848 sp<Layer> strongRelative = weakRelative.promote();
1849 // Only add relative layers that are also descendents of the top most parent of the tree.
1850 // If a relative layer is not a descendent, then it should be ignored.
1851 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1852 traverse.add(strongRelative);
1853 }
1854 }
1855
1856 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001857 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001858 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1859 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1860 // the child here since it won't be added later as a relative.
1861 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1862 childState.zOrderRelativeOf.promote().get())) {
1863 continue;
1864 }
1865 traverse.add(child);
1866 }
1867
1868 return traverse;
1869}
1870
1871void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1872 LayerVector::StateSet stateSet,
1873 const LayerVector::Visitor& visitor) {
1874 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001875
1876 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001877 for (; i < list.size(); i++) {
1878 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001879 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001880 break;
1881 }
chaviw4b129c22018-04-09 16:19:43 -07001882 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001883 }
chaviw4b129c22018-04-09 16:19:43 -07001884
chaviwa76b2712017-09-20 12:02:26 -07001885 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001886 for (; i < list.size(); i++) {
1887 const auto& relative = list[i];
1888 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001889 }
1890}
1891
chaviw4b129c22018-04-09 16:19:43 -07001892std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1893 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1894 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1895
1896 std::vector<Layer*> layersInTree = {this};
1897 for (size_t i = 0; i < children.size(); i++) {
1898 const auto& child = children[i];
1899 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1900 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1901 }
1902
1903 return layersInTree;
1904}
1905
1906void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1907 const LayerVector::Visitor& visitor) {
1908 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1909 std::sort(layersInTree.begin(), layersInTree.end());
1910 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1911}
1912
Peiyong Linefefaac2018-08-17 12:27:51 -07001913ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001914 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001915}
1916
chaviw13fdc492017-06-27 12:40:18 -07001917half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001918 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001919
chaviw13fdc492017-06-27 12:40:18 -07001920 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1921 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001922}
Robert Carr6452f122017-03-21 10:41:29 -07001923
Vishnu Nair6213bd92020-05-08 17:42:25 -07001924ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001925 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001926 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1927 return fixedTransformHint;
1928 }
1929 const auto& p = mCurrentParent.promote();
1930 if (!p) return fixedTransformHint;
1931 return p->getFixedTransformHint();
1932}
1933
chaviw13fdc492017-06-27 12:40:18 -07001934half4 Layer::getColor() const {
1935 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001936 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001937}
Robert Carr6452f122017-03-21 10:41:29 -07001938
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001939int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01001940 const auto& p = mDrawingParent.promote();
1941
1942 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1943 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001944}
1945
Galia Peychevae0acf382021-04-12 21:22:34 +02001946const std::vector<BlurRegion> Layer::getBlurRegions() const {
1947 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001948 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001949 for (auto& region : regionsCopy) {
1950 region.alpha = region.alpha * layerAlpha;
1951 }
1952 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001953}
1954
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001955Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1956 const auto& p = mDrawingParent.promote();
1957 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001958 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001959 if (parentState.radius > 0) {
1960 ui::Transform t = getActiveTransform(getDrawingState());
1961 t = t.inverse();
1962 parentState.cropRect = t.transform(parentState.cropRect);
1963 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1964 // but a transform matrix can define horizontal and vertical scales.
1965 // Let's take the average between both of them and pass into the shader, practically we
1966 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07001967 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
1968 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
1969 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001970 return parentState;
1971 }
1972 }
1973 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001974 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
1975 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001976 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001977}
1978
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001979void Layer::prepareShadowClientComposition(LayerFE::LayerSettings& caster,
1980 const Rect& layerStackRect) {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001981 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1982
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001983 // Note: this preserves existing behavior of shadowing the entire layer and not cropping it if
1984 // transparent regions are present. This may not be necessary since shadows are only cast by
1985 // SurfaceFlinger's EffectLayers, which do not typically use transparent regions.
1986 state.boundaries = mBounds;
1987
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001988 // Shift the spot light x-position to the middle of the display and then
1989 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001990 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001991 state.lightPos.y -= mScreenBounds.top;
1992
1993 state.length = mEffectiveShadowRadius;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001994
1995 if (state.length > 0.f) {
1996 const float casterAlpha = caster.alpha;
1997 const bool casterIsOpaque =
1998 ((caster.source.buffer.buffer != nullptr) && caster.source.buffer.isOpaque);
1999
2000 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
2001 // Otherwise the generated shadow will only be shown around the casting layer.
2002 state.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
2003 state.ambientColor *= casterAlpha;
2004 state.spotColor *= casterAlpha;
2005
2006 if (state.ambientColor.a > 0.f && state.spotColor.a > 0.f) {
2007 caster.shadow = state;
2008 }
2009 }
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002010}
2011
Robert Carr1f0a16a2016-10-24 16:27:39 -07002012void Layer::commitChildList() {
2013 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2014 const auto& child = mCurrentChildren[i];
2015 child->commitChildList();
2016 }
2017 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002018 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002019}
2020
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002021
Robert Carr720e5062018-07-30 17:45:14 -07002022void Layer::setInputInfo(const InputWindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07002023 mDrawingState.inputInfo = info;
Vishnu Naira8c7c542021-07-20 18:49:42 -07002024 mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07002025 mDrawingState.modified = true;
Robert Carrbf14dbb2021-06-11 13:26:56 -07002026 mFlinger->mInputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002027 setTransactionFlags(eTransactionNeeded);
2028}
2029
Alec Mouri6b9e9912020-01-21 10:50:24 -08002030LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002031 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08002032 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002033 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002034 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2035
Vishnu Nair60db8c02020-04-02 11:55:16 -07002036 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2037 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002038 if (display) {
2039 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002040 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2041 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002042 }
2043
chaviw08f3cb22020-01-13 13:17:21 -08002044 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002045 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002046 }
chaviw6d89e2d2020-01-14 14:42:01 -08002047
2048 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002049}
2050
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002051void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002052 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002053 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002054
2055 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002056
chaviwd62d3062019-09-04 14:48:02 -07002057 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002058 if (buffer != nullptr) {
2059 LayerProtoHelper::writeToProto(buffer,
2060 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002061 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002062 layerInfo->mutable_buffer_transform());
2063 }
2064 layerInfo->set_invalidate(contentDirty);
2065 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002066 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002067 layerInfo->set_queued_frames(getQueuedFrameCount());
2068 layerInfo->set_refresh_pending(isBufferLatched());
2069 layerInfo->set_curr_frame(mCurrentFrameNumber);
2070 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2071
2072 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002073 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Winson Chunga30f7c92021-06-29 15:42:56 -07002074 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002075 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2076 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2077 [&]() { return layerInfo->mutable_position(); });
2078 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002079 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002080 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002081 [&]() { return layerInfo->mutable_visible_region(); });
2082 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002083 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2084 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002085
2086 if (hasColorTransform()) {
2087 LayerProtoHelper::writeToProto(getColorTransform(),
2088 layerInfo->mutable_color_transform());
2089 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002090 }
2091
Vishnu Nair60db8c02020-04-02 11:55:16 -07002092 LayerProtoHelper::writeToProto(mSourceBounds,
2093 [&]() { return layerInfo->mutable_source_bounds(); });
2094 LayerProtoHelper::writeToProto(mScreenBounds,
2095 [&]() { return layerInfo->mutable_screen_bounds(); });
2096 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2097 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2098 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002099}
2100
2101void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002102 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002103 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2104 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002105 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002106
chaviw766c9c52021-02-10 17:36:47 -08002107 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002108
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002109 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2110 layerInfo->set_id(sequence);
2111 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002112 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002113
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002114 for (const auto& child : children) {
2115 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002116 }
chaviw1d044282017-09-27 12:19:28 -07002117
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002118 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2119 sp<Layer> strongRelative = weakRelative.promote();
2120 if (strongRelative != nullptr) {
2121 layerInfo->add_relatives(strongRelative->sequence);
2122 }
chaviwadc40c22018-07-10 16:57:27 -07002123 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002124
2125 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2126 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002127
2128 layerInfo->set_layer_stack(getLayerStack());
2129 layerInfo->set_z(state.z);
2130
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002131 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2132 [&]() {
2133 return layerInfo->mutable_requested_position();
2134 });
2135
chaviw766c9c52021-02-10 17:36:47 -08002136 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002137 [&]() { return layerInfo->mutable_size(); });
2138
chaviw766c9c52021-02-10 17:36:47 -08002139 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002140
2141 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002142
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002143
2144 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2145 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2146 LayerProtoHelper::writeToProto(state.color,
2147 [&]() { return layerInfo->mutable_requested_color(); });
2148 layerInfo->set_flags(state.flags);
2149
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002150 LayerProtoHelper::writeToProto(requestedTransform,
2151 layerInfo->mutable_requested_transform());
2152
2153 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2154 if (parent != nullptr) {
2155 layerInfo->set_parent(parent->sequence);
2156 } else {
2157 layerInfo->set_parent(-1);
2158 }
2159
2160 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2161 if (zOrderRelativeOf != nullptr) {
2162 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2163 } else {
2164 layerInfo->set_z_order_relative_of(-1);
2165 }
chaviw08f3cb22020-01-13 13:17:21 -08002166
2167 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002168
2169 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002170 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002171
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002172 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw4c34a092020-07-08 11:30:06 -07002173 InputWindowInfo info;
2174 if (useDrawing) {
[1;3C2b9fc252021-02-04 16:16:50 -08002175 info = fillInputInfo({nullptr});
chaviw4c34a092020-07-08 11:30:06 -07002176 } else {
2177 info = state.inputInfo;
2178 }
2179
2180 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002181 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002182 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002183
2184 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2185 auto protoMap = layerInfo->mutable_metadata();
2186 for (const auto& entry : state.metadata.mMap) {
2187 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2188 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002189 }
chaviw1d044282017-09-27 12:19:28 -07002190}
2191
Robert Carr2e102c92018-10-23 12:11:15 -07002192bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002193 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002194}
2195
chaviw39d01472021-04-08 14:26:24 -05002196ui::Transform Layer::getInputTransform() const {
2197 return getTransform();
2198}
2199
2200Rect Layer::getInputBounds() const {
2201 return getCroppedBufferSize(getDrawingState());
2202}
2203
[1;3C2b9fc252021-02-04 16:16:50 -08002204void Layer::fillInputFrameInfo(InputWindowInfo& info, const ui::Transform& toPhysicalDisplay) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002205 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002206 // If this is a portal window, set the touchableRegion to the layerBounds.
2207 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002208 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002209 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002210 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002211 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002212 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002213
chaviw7e72caf2020-12-02 16:50:43 -08002214 if (!layerBounds.isValid()) {
2215 // If the layer bounds is empty, set the frame to empty and clear the transform
2216 info.frameLeft = 0;
2217 info.frameTop = 0;
2218 info.frameRight = 0;
2219 info.frameBottom = 0;
2220 info.transform.reset();
2221 return;
2222 }
2223
chaviw39d01472021-04-08 14:26:24 -05002224 ui::Transform layerToDisplay = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002225 // Transform that takes window coordinates to unrotated display coordinates
2226 ui::Transform t = toPhysicalDisplay * layerToDisplay;
chaviw7e72caf2020-12-02 16:50:43 -08002227 int32_t xSurfaceInset = info.surfaceInset;
2228 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002229 // Bring screenBounds into unrotated space
2230 Rect screenBounds = toPhysicalDisplay.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002231
chaviw1ff3d1e2020-07-01 15:53:47 -07002232 const float xScale = t.getScaleX();
2233 const float yScale = t.getScaleY();
2234 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002235 xSurfaceInset = std::round(xSurfaceInset * xScale);
2236 ySurfaceInset = std::round(ySurfaceInset * yScale);
2237 }
2238
Peiyong Lin74861ad2020-10-14 11:55:33 -07002239 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002240 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002241
2242 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002243 xSurfaceInset = (xSurfaceInset >= 0)
2244 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2245 : 0;
2246 ySurfaceInset = (ySurfaceInset >= 0)
2247 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2248 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002249
Robert Carra7242302020-09-01 18:26:29 -07002250 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2251 // in the overflow scenario?
2252 {
2253 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002254 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2255 transformedLayerBounds.left = tmp;
2256 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2257 transformedLayerBounds.right = tmp;
2258 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2259 transformedLayerBounds.top = tmp;
2260 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2261 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002262 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002263
chaviw44a6d2b2020-09-08 17:14:16 -07002264 // Compute the correct transform to send to input. This will allow it to transform the
2265 // input coordinates from display space into window space. Therefore, it needs to use the
2266 // final layer frame to create the inverse transform. Since surface insets are added later,
2267 // along with the overflow, the best way to ensure we get the correct transform is to use
2268 // the final frame calculated.
2269 // 1. Take the original transform set on the window and get the inverse transform. This is
2270 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002271 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002272 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2273 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2274 // be the new translation values for the transform.
2275 // 3. Update the translation of the original transform to the new translation values.
2276 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2277 // window space.
2278 ui::Transform inverseTransform = t.inverse();
2279 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2280 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002281 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002282 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002283 info.transform = inputTransform.inverse();
2284
chaviw39cfa2e2020-11-04 14:19:02 -08002285 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2286 // The frame should be the area the user sees on screen since it's used for occlusion
2287 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002288 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2289 info.frameLeft = transformedLayerBounds.left;
2290 info.frameTop = transformedLayerBounds.top;
2291 info.frameRight = transformedLayerBounds.right;
2292 info.frameBottom = transformedLayerBounds.bottom;
2293
Vishnu Nair8033a492018-12-05 07:27:23 -08002294 // Position the touchable region relative to frame screen location and restrict it to frame
2295 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002296 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002297}
2298
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002299void Layer::fillTouchOcclusionMode(InputWindowInfo& info) {
2300 sp<Layer> p = this;
2301 while (p != nullptr && !p->hasInputInfo()) {
2302 p = p->mDrawingParent.promote();
2303 }
2304 if (p != nullptr) {
2305 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2306 }
2307}
2308
[1;3C2b9fc252021-02-04 16:16:50 -08002309InputWindowInfo Layer::fillInputInfo(const sp<DisplayDevice>& display) {
chaviw7e72caf2020-12-02 16:50:43 -08002310 if (!hasInputInfo()) {
2311 mDrawingState.inputInfo.name = getName();
2312 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2313 mDrawingState.inputInfo.ownerPid = mOwnerPid;
2314 mDrawingState.inputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
2315 mDrawingState.inputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
2316 mDrawingState.inputInfo.displayId = getLayerStack();
2317 }
2318
2319 InputWindowInfo info = mDrawingState.inputInfo;
2320 info.id = sequence;
2321
2322 if (info.displayId == ADISPLAY_ID_NONE) {
2323 info.displayId = getLayerStack();
2324 }
2325
[1;3C2b9fc252021-02-04 16:16:50 -08002326 // Transform that goes from "logical(rotated)" display to physical/unrotated display.
2327 // This is for when inputflinger operates in physical display-space.
2328 ui::Transform toPhysicalDisplay;
2329 if (display) {
2330 toPhysicalDisplay = display->getTransform();
Evan Rosky84f07f02021-04-16 10:42:42 -07002331 info.displayWidth = display->getWidth();
2332 info.displayHeight = display->getHeight();
[1;3C2b9fc252021-02-04 16:16:50 -08002333 }
2334 fillInputFrameInfo(info, toPhysicalDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08002335
Robert Carr5dc426e2020-06-10 14:29:14 -07002336 // For compatibility reasons we let layers which can receive input
2337 // receive input before they have actually submitted a buffer. Because
2338 // of this we use canReceiveInput instead of isVisible to check the
2339 // policy-visibility, ignoring the buffer state. However for layers with
2340 // hasInputInfo()==false we can use the real visibility state.
2341 // We are just using these layers for occlusion detection in
2342 // InputDispatcher, and obviously if they aren't visible they can't occlude
2343 // anything.
2344 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002345 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002346 fillTouchOcclusionMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002347
2348 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2349 if (info.replaceTouchableRegionWithCrop) {
2350 if (cropLayer == nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002351 info.touchableRegion = Region(toPhysicalDisplay.transform(Rect{mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002352 } else {
[1;3C2b9fc252021-02-04 16:16:50 -08002353 info.touchableRegion =
2354 Region(toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002355 }
2356 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002357 info.touchableRegion = info.touchableRegion.intersect(
2358 toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002359 }
2360
Winson Chunga30f7c92021-06-29 15:42:56 -07002361 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2362 // if it was set by WM for a known system overlay
2363 info.trustedOverlay = info.trustedOverlay || isTrustedOverlay();
2364
2365
chaviwaf87b3e2019-10-01 16:59:28 -07002366 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2367 // touches from going outside the cloned area.
2368 if (isClone()) {
2369 sp<Layer> clonedRoot = getClonedRoot();
2370 if (clonedRoot != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002371 Rect rect = toPhysicalDisplay.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002372 info.touchableRegion = info.touchableRegion.intersect(rect);
2373 }
2374 }
2375
Robert Carr720e5062018-07-30 17:45:14 -07002376 return info;
2377}
2378
chaviwaf87b3e2019-10-01 16:59:28 -07002379sp<Layer> Layer::getClonedRoot() {
2380 if (mClonedChild != nullptr) {
2381 return this;
2382 }
2383 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2384 return nullptr;
2385 }
2386 return mDrawingParent.promote()->getClonedRoot();
2387}
2388
Robert Carredd13602020-04-13 17:24:34 -07002389bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002390 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002391}
2392
chaviw3e727cd2019-01-31 13:41:05 -08002393bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002394 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002395}
2396
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002397compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002398 const DisplayDevice* display) const {
2399 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002400 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002401}
2402
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002403Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2404 const auto outputLayer = findOutputLayerForDisplay(display);
2405 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002406}
2407
chaviwb4c6e582019-08-16 14:35:07 -07002408void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2409 // copy drawing state from cloned layer
2410 mDrawingState = clonedFrom->mDrawingState;
2411 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002412}
chaviw74b03172019-08-19 11:09:03 -07002413
2414void Layer::updateMirrorInfo() {
2415 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2416 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2417 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2418 // that case, we want to delete the reference to the clone since we want it to get
2419 // destroyed. The root, this layer, will still be around since the client can continue
2420 // to hold a reference, but no cloned layers will be displayed.
2421 mClonedChild = nullptr;
2422 return;
2423 }
2424
2425 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2426 // If the real layer exists and is in current state, add the clone as a child of the root.
2427 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2428 // copied to drawingState for the root layer. So the clonedChild is always removed from
2429 // drawingState and then needs to be added back each traversal.
2430 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2431 addChildToDrawing(mClonedChild);
2432 }
2433
2434 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2435 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2436 mClonedChild->updateClonedRelatives(clonedLayersMap);
2437}
2438
2439void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2440 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2441 // to the clone. If the real layer is no longer alive, continue traversing the children
2442 // since we may be able to pull out other children that are still alive.
2443 if (isClonedFromAlive()) {
2444 sp<Layer> clonedFrom = getClonedFrom();
2445 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002446 clonedLayersMap.emplace(clonedFrom, this);
2447 }
2448
2449 // The clone layer may have children in drawingState since they may have been created and
2450 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2451 // that already exist, since we can just re-use them.
2452 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2453 // not updated in the regular traversal. They are skipped since the root will lose the
2454 // reference to them when it copies its currentChildren to drawing.
2455 for (sp<Layer>& child : mDrawingChildren) {
2456 child->updateClonedDrawingState(clonedLayersMap);
2457 }
2458}
2459
2460void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2461 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2462 mDrawingChildren.clear();
2463
2464 if (!isClonedFromAlive()) {
2465 return;
2466 }
2467
2468 sp<Layer> clonedFrom = getClonedFrom();
2469 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2470 if (child == mirrorRoot) {
2471 // This is to avoid cyclical mirroring.
2472 continue;
2473 }
2474 sp<Layer> clonedChild = clonedLayersMap[child];
2475 if (clonedChild == nullptr) {
2476 clonedChild = child->createClone();
2477 clonedLayersMap[child] = clonedChild;
2478 }
2479 addChildToDrawing(clonedChild);
2480 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2481 }
2482}
2483
chaviwaf87b3e2019-10-01 16:59:28 -07002484void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2485 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2486 if (cropLayer != nullptr) {
2487 if (clonedLayersMap.count(cropLayer) == 0) {
2488 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2489 // self as crop layer to avoid going outside bounds.
2490 mDrawingState.touchableRegionCrop = this;
2491 } else {
2492 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2493 mDrawingState.touchableRegionCrop = clonedCropLayer;
2494 }
2495 }
2496 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2497 // WM or the client.
Michael Wright44753b12020-07-08 13:48:11 +01002498 mDrawingState.inputInfo.flags &= ~InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002499}
2500
2501void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002502 mDrawingState.zOrderRelativeOf = nullptr;
2503 mDrawingState.zOrderRelatives.clear();
2504
2505 if (!isClonedFromAlive()) {
2506 return;
2507 }
2508
chaviwaf87b3e2019-10-01 16:59:28 -07002509 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002510 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002511 const sp<Layer>& relative = relativeWeak.promote();
2512 if (clonedLayersMap.count(relative) > 0) {
2513 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002514 mDrawingState.zOrderRelatives.add(clonedRelative);
2515 }
2516 }
2517
2518 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2519 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2520 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2521 // still traverse the children, but the layer with the missing relativeOf will not be shown
2522 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002523 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2524 if (clonedLayersMap.count(relativeOf) > 0) {
2525 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002526 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2527 }
2528
chaviwaf87b3e2019-10-01 16:59:28 -07002529 updateClonedInputInfo(clonedLayersMap);
2530
chaviw74b03172019-08-19 11:09:03 -07002531 for (sp<Layer>& child : mDrawingChildren) {
2532 child->updateClonedRelatives(clonedLayersMap);
2533 }
2534}
2535
2536void Layer::addChildToDrawing(const sp<Layer>& layer) {
2537 mDrawingChildren.add(layer);
2538 layer->mDrawingParent = this;
2539}
2540
Steven Thomas62a4cf82020-01-31 12:04:03 -08002541Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2542 switch (compatibility) {
2543 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2544 return FrameRateCompatibility::Default;
2545 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2546 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002547 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2548 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002549 default:
2550 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2551 return FrameRateCompatibility::Default;
2552 }
2553}
2554
Vishnu Nair2e5b3262022-01-27 17:13:18 +00002555bool Layer::setDropInputMode(gui::DropInputMode mode) {
2556 if (mDrawingState.dropInputMode == mode) {
2557 return false;
2558 }
2559 mDrawingState.dropInputMode = mode;
2560 mDrawingState.modified = true;
2561 mFlinger->mInputInfoChanged = true;
2562 setTransactionFlags(eTransactionNeeded);
2563 return true;
2564}
2565
Marin Shalamanovc5986772021-03-16 16:09:49 +01002566scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2567 switch (strategy) {
2568 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2569 return Seamlessness::OnlySeamless;
2570 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2571 return Seamlessness::SeamedAndSeamless;
2572 default:
2573 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2574 return Seamlessness::Default;
2575 }
2576}
2577
chaviwc5676c62020-09-18 15:01:04 -07002578bool Layer::getPrimaryDisplayOnly() const {
2579 const State& s(mDrawingState);
2580 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2581 return true;
2582 }
2583
2584 sp<Layer> parent = mDrawingParent.promote();
2585 return parent == nullptr ? false : parent->getPrimaryDisplayOnly();
2586}
2587
binzhang174ac1e2021-06-28 14:17:33 +08002588nsecs_t Layer::getPreviousGfxInfo() {
2589 return mFrameTracker.getPreviousGfxInfo();
2590}
2591
Robert Carr6a0382d2021-07-01 15:57:17 -07002592void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2593 mClonedChild = clonedChild;
2594 mHadClonedChild = true;
2595 mFlinger->mNumClones++;
2596}
2597
Vishnu Naira8c7c542021-07-20 18:49:42 -07002598const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle");
2599
2600wp<Layer> Layer::fromHandle(const sp<IBinder>& handleBinder) {
2601 if (handleBinder == nullptr) {
2602 return nullptr;
2603 }
2604
2605 BBinder* b = handleBinder->localBinder();
2606 if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) {
2607 return nullptr;
2608 }
2609
2610 // We can safely cast this binder since its local and we verified its interface descriptor.
2611 sp<Handle> handle = static_cast<Handle*>(handleBinder.get());
2612 return handle->owner;
2613}
2614
Mathias Agopian13127d82013-03-05 17:47:11 -08002615// ---------------------------------------------------------------------------
2616
Marin Shalamanov46084422020-10-13 12:33:42 +02002617std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2618 return stream << "{rate=" << rate.rate
2619 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002620 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002621}
2622
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002624
2625#if defined(__gl_h_)
2626#error "don't include gl/gl.h in this file"
2627#endif
2628
2629#if defined(__gl2_h_)
2630#error "don't include gl2/gl2.h in this file"
2631#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002632
2633// TODO(b/129481165): remove the #pragma below and fix conversion issues
2634#pragma clang diagnostic pop // ignored "-Wconversion"