blob: 4beb5262d42023b99e024dad6f2e01b7adc3ad5d [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"
Michael Wright44753b12020-07-08 13:48:11 +010073#include "input/InputWindow.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070074
David Sodman41fdfc92017-11-06 16:09:56 -080075#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010078namespace {
79constexpr int kDumpTableRowLength = 159;
80} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
Yiwei Zhang5434a782018-12-05 18:06:32 -080082using base::StringAppendF;
Michael Wright44753b12020-07-08 13:48:11 +010083using namespace android::flag_operators;
Ady Abraham22c7b5c2020-09-22 19:33:40 -070084using PresentState = frametimeline::SurfaceFrame::PresentState;
Yiwei Zhang5434a782018-12-05 18:06:32 -080085
Lloyd Piquef1c675b2018-09-12 20:45:39 -070086std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080087
Lloyd Pique42ab75e2018-09-12 20:46:03 -070088Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070089 : mFlinger(args.flinger),
90 mName(args.name),
91 mClientRef(args.client),
Michael Wright44753b12020-07-08 13:48:11 +010092 mWindowType(static_cast<InputWindowInfo::Type>(
93 args.metadata.getInt32(METADATA_WINDOW_TYPE, 0))) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070094 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070095 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
96 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
97 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -070098 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
99 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700100
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800101 mCurrentState.active_legacy.w = args.w;
102 mCurrentState.active_legacy.h = args.h;
103 mCurrentState.flags = layerFlags;
104 mCurrentState.active_legacy.transform.set(0, 0);
chaviw25714502021-02-11 10:01:08 -0800105 mCurrentState.crop.makeInvalid();
106 mCurrentState.requestedCrop = mCurrentState.crop;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 mCurrentState.z = 0;
108 mCurrentState.color.a = 1.0f;
109 mCurrentState.layerStack = 0;
110 mCurrentState.sequence = 0;
111 mCurrentState.requested_legacy = mCurrentState.active_legacy;
chaviw766c9c52021-02-10 17:36:47 -0800112 mCurrentState.width = UINT32_MAX;
113 mCurrentState.height = UINT32_MAX;
114 mCurrentState.transform.set(0, 0);
Valerie Hau134651a2020-01-28 16:21:22 -0800115 mCurrentState.frameNumber = 0;
chaviw766c9c52021-02-10 17:36:47 -0800116 mCurrentState.bufferTransform = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 mCurrentState.transformToDisplayInverse = false;
118 mCurrentState.crop.makeInvalid();
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700119 mCurrentState.acquireFence = sp<Fence>::make(-1);
120 mCurrentState.acquireFenceTime = std::make_shared<FenceTime>(mCurrentState.acquireFence);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800121 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
122 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700123 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800124 mCurrentState.cornerRadius = 0.0f;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800125 mCurrentState.backgroundBlurRadius = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126 mCurrentState.api = -1;
127 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800128 mCurrentState.colorSpaceAgnostic = false;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100129 mCurrentState.frameRateSelectionPriority = PRIORITY_UNSET;
Evan Roskya1f1e152019-01-24 16:17:46 -0800130 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800131 mCurrentState.shadowRadius = 0.f;
Vishnu Nair6213bd92020-05-08 17:42:25 -0700132 mCurrentState.fixedTransformHint = ui::Transform::ROT_INVALID;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000133 mCurrentState.frameTimelineInfo = {};
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700134 mCurrentState.postTime = -1;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700135 mCurrentState.destinationFrame.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700136
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700137 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
138 // Set an invalid color so there is no color fill.
139 mCurrentState.color.r = -1.0_hf;
140 mCurrentState.color.g = -1.0_hf;
141 mCurrentState.color.b = -1.0_hf;
142 }
143
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700144 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700146
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800147 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700148 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800149 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200150 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700151
Vishnu Nair0f085c62019-08-30 08:49:12 -0700152 mCallingPid = args.callingPid;
153 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700154
155 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
156 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
157 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700158 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700159 } else {
160 // A create layer request from a non system request cannot specify the owner uid
161 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700162 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700163 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800164}
165
166void Layer::onFirstRef() {
167 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700168}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700170Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800171 sp<Client> c(mClientRef.promote());
172 if (c != 0) {
173 c->detachLayer(this);
174 }
175
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000176 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700177 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700178}
179
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700180LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
181 uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700182 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700183 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700184 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700185 w(w),
186 h(h),
187 flags(flags),
188 metadata(std::move(metadata)) {
189 IPCThreadState* ipc = IPCThreadState::self();
190 callingPid = ipc->getCallingPid();
191 callingUid = ipc->getCallingUid();
192}
193
Mathias Agopian13127d82013-03-05 17:47:11 -0800194// ---------------------------------------------------------------------------
195// callbacks
196// ---------------------------------------------------------------------------
197
David Sodmaneb085e02017-10-05 18:49:04 -0700198/*
199 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
200 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800201 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700202 */
David Sodmaneb085e02017-10-05 18:49:04 -0700203void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800204
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700205void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
206 if (mCurrentState.zOrderRelativeOf == nullptr) {
207 return;
208 }
Robert Carr2e102c92018-10-23 12:11:15 -0700209
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700210 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
211 if (strongRelative == nullptr) {
212 setZOrderRelativeOf(nullptr);
213 return;
214 }
215
216 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
217 strongRelative->removeZOrderRelative(this);
218 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800219 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700220 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700221}
222
223void Layer::removeFromCurrentState() {
Ady Abrahambe09aad2021-05-03 18:59:38 -0700224 if (!mRemovedFromCurrentState) {
225 mRemovedFromCurrentState = true;
226 mFlinger->mScheduler->deregisterLayer(this);
227 }
Rob Carr4bba3702018-10-08 21:53:30 +0000228
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800229 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700230}
Chia-I Wu38512252017-05-17 14:36:16 -0700231
chaviw68d4dab2020-06-08 15:07:32 -0700232sp<Layer> Layer::getRootLayer() {
233 sp<Layer> parent = getParent();
234 if (parent == nullptr) {
235 return this;
236 }
237 return parent->getRootLayer();
238}
239
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700240void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700241 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
242 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700243 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700244
245 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700246 layer->removeFromCurrentState();
247 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700248 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700249}
250
chaviw61626f22018-11-15 16:26:27 -0800251void Layer::addToCurrentState() {
Ady Abrahambe09aad2021-05-03 18:59:38 -0700252 if (mRemovedFromCurrentState) {
253 mRemovedFromCurrentState = false;
254 mFlinger->mScheduler->registerLayer(this);
255 }
chaviw61626f22018-11-15 16:26:27 -0800256
257 for (const auto& child : mCurrentChildren) {
258 child->addToCurrentState();
259 }
260}
261
Mathias Agopian13127d82013-03-05 17:47:11 -0800262// ---------------------------------------------------------------------------
263// set-up
264// ---------------------------------------------------------------------------
265
chaviw13fdc492017-06-27 12:40:18 -0700266bool Layer::getPremultipledAlpha() const {
267 return mPremultipliedAlpha;
268}
269
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700270sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800271 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700272 if (mGetHandleCalled) {
273 ALOGE("Get handle called twice" );
274 return nullptr;
275 }
276 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700277 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800278}
279
280// ---------------------------------------------------------------------------
281// h/w composer set-up
282// ---------------------------------------------------------------------------
283
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700284static Rect reduce(const Rect& win, const Region& exclude) {
285 if (CC_LIKELY(exclude.isEmpty())) {
286 return win;
287 }
288 if (exclude.isRect()) {
289 return win.reduce(exclude.getBounds());
290 }
291 return Region(win).subtract(exclude).getBounds();
292}
293
Dan Stoza80d61162017-12-20 15:57:52 -0800294static FloatRect reduce(const FloatRect& win, const Region& exclude) {
295 if (CC_LIKELY(exclude.isEmpty())) {
296 return win;
297 }
298 // Convert through Rect (by rounding) for lack of FloatRegion
299 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
300}
301
Vishnu Nair4351ad52019-02-11 14:13:02 -0800302Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800303 if (!reduceTransparentRegion) {
304 return Rect{mScreenBounds};
305 }
306
307 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800308 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800309 // Transform to screen space.
310 bounds = t.transform(bounds);
311 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700312}
313
Vishnu Nair4351ad52019-02-11 14:13:02 -0800314FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000315 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800316 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700317}
318
Vishnu Nairf0c28512019-02-08 12:40:28 -0800319FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
320 // Subtract the transparent region and snap to the bounds.
321 return reduce(mBounds, activeTransparentRegion);
322}
323
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700324void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
325 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800326 const State& s(getDrawingState());
327
328 // Calculate effective layer transform
329 mEffectiveTransform = parentTransform * getActiveTransform(s);
330
331 // Transform parent bounds to layer space
332 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
333
Vishnu Nairc652ff82019-03-15 12:48:54 -0700334 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800335 mSourceBounds = computeSourceBounds(parentBounds);
336
337 // Calculate bounds by croping diplay frame with layer crop and parent bounds
338 FloatRect bounds = mSourceBounds;
339 const Rect layerCrop = getCrop(s);
340 if (!layerCrop.isEmpty()) {
341 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
342 }
343 bounds = bounds.intersect(parentBounds);
344
345 mBounds = bounds;
346 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700347
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700348 // Use the layer's own shadow radius if set. Otherwise get the radius from
349 // parent.
350 if (s.shadowRadius > 0.f) {
351 mEffectiveShadowRadius = s.shadowRadius;
352 } else {
353 mEffectiveShadowRadius = parentShadowRadius;
354 }
355
356 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
357 // don't pass it to its children.
358 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
359
Vishnu Nair4351ad52019-02-11 14:13:02 -0800360 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700361 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800362 }
363}
364
Vishnu Nair60356342018-11-13 13:00:45 -0800365Rect Layer::getCroppedBufferSize(const State& s) const {
366 Rect size = getBufferSize(s);
367 Rect crop = getCrop(s);
368 if (!crop.isEmpty() && size.isValid()) {
369 size.intersect(crop, &size);
370 } else if (!crop.isEmpty()) {
371 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700372 }
Vishnu Nair60356342018-11-13 13:00:45 -0800373 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800374}
375
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700376void Layer::setupRoundedCornersCropCoordinates(Rect win,
377 const FloatRect& roundedCornersCrop) const {
378 // Translate win by the rounded corners rect coordinates, to have all values in
379 // layer coordinate space.
380 win.left -= roundedCornersCrop.left;
381 win.right -= roundedCornersCrop.left;
382 win.top -= roundedCornersCrop.top;
383 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700384}
385
Lloyd Piquede196652020-01-22 17:29:58 -0800386void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800387 const auto& drawingState{getDrawingState()};
388 const uint32_t layerStack = getLayerStack();
389 const auto alpha = static_cast<float>(getAlpha());
390 const bool opaque = isOpaque(drawingState);
391 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
392
393 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
394 if (!opaque || alpha != 1.0f) {
395 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
396 : Hwc2::IComposerClient::BlendMode::COVERAGE;
397 }
398
Lloyd Piquede196652020-01-22 17:29:58 -0800399 auto* compositionState = editCompositionState();
400 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800401 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800402 compositionState->internalOnly = getPrimaryDisplayOnly();
403 compositionState->isVisible = isVisible();
404 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
405 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800406
Lloyd Piquede196652020-01-22 17:29:58 -0800407 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800408 contentDirty = false;
409
Lloyd Piquede196652020-01-22 17:29:58 -0800410 compositionState->geomLayerBounds = mBounds;
411 compositionState->geomLayerTransform = getTransform();
412 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
413 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800414
Lloyd Piquede196652020-01-22 17:29:58 -0800415 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
416 compositionState->alpha = alpha;
417 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700418 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500419 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800420}
421
Lloyd Piquede196652020-01-22 17:29:58 -0800422void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800423 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800424
Lloyd Piquea83776c2019-01-29 18:42:32 -0800425 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
426 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700427 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400428 if (parent.get()) {
429 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800430 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
431 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700432 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800433 type = parentType;
434 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700435 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400436 }
437
Lloyd Piquede196652020-01-22 17:29:58 -0800438 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700439
Lloyd Piquede196652020-01-22 17:29:58 -0800440 compositionState->geomBufferSize = getBufferSize(drawingState);
441 compositionState->geomContentCrop = getBufferCrop();
442 compositionState->geomCrop = getCrop(drawingState);
443 compositionState->geomBufferTransform = getBufferTransform();
444 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
445 compositionState->geomUsesSourceCrop = usesSourceCrop();
446 compositionState->isSecure = isSecure();
447
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800448 compositionState->metadata.clear();
449 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
450 for (const auto& [key, mandatory] : supportedMetadata) {
451 const auto& genericLayerMetadataCompatibilityMap =
452 mFlinger->getGenericLayerMetadataKeyMap();
453 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
454 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
455 continue;
456 }
457 const uint32_t id = compatIter->second;
458
459 auto it = drawingState.metadata.mMap.find(id);
460 if (it == std::end(drawingState.metadata.mMap)) {
461 continue;
462 }
463
464 compositionState->metadata
465 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
466 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800467}
David Sodmanba340492018-08-05 21:51:33 -0700468
Lloyd Piquede196652020-01-22 17:29:58 -0800469void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800470 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800471 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800472
Lloyd Piquede196652020-01-22 17:29:58 -0800473 compositionState->forceClientComposition = false;
474
475 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
476 compositionState->dataspace = getDataSpace();
477 compositionState->colorTransform = getColorTransform();
478 compositionState->colorTransformIsIdentity = !hasColorTransform();
479 compositionState->surfaceDamage = surfaceDamageRegion;
480 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800481
482 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700483
Lloyd Piquede196652020-01-22 17:29:58 -0800484 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800485 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800486
487 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400488 // Rounded corners no longer force client composition, since we may use a
489 // hole punch so that the layer will appear to have rounded corners.
490 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500491 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800492 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800493 }
494}
495
Lloyd Piquede196652020-01-22 17:29:58 -0800496void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800497 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800498 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800499
500 // Apply the layer's transform, followed by the display's global transform
501 // Here we're guaranteed that the layer's transform preserves rects
502 Rect win = getCroppedBufferSize(drawingState);
503 // Subtract the transparent region and snap to the bounds
504 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
505 Rect frame(getTransform().transform(bounds));
506
Lloyd Piquede196652020-01-22 17:29:58 -0800507 compositionState->cursorFrame = frame;
508}
509
510sp<compositionengine::LayerFE> Layer::asLayerFE() const {
511 return const_cast<compositionengine::LayerFE*>(
512 static_cast<const compositionengine::LayerFE*>(this));
513}
514
515sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
516 return nullptr;
517}
518
519compositionengine::LayerFECompositionState* Layer::editCompositionState() {
520 return nullptr;
521}
522
523const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
524 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800525}
526
Lloyd Piquef16688f2019-02-19 17:47:57 -0800527bool Layer::onPreComposition(nsecs_t) {
528 return false;
529}
530
Lloyd Piquede196652020-01-22 17:29:58 -0800531void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800532 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800533
Lloyd Piquec6687342019-03-07 21:34:57 -0800534 switch (subset) {
535 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800536 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800537 break;
538
539 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800540 prepareBasicGeometryCompositionState();
541 prepareGeometryCompositionState();
542 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800543 break;
544
545 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800546 preparePerFrameCompositionState();
547 break;
548
549 case StateSubset::Cursor:
550 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800551 break;
552 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800553}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700554
Lloyd Piquea83776c2019-01-29 18:42:32 -0800555const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700556 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800557}
558
Mathias Agopian13127d82013-03-05 17:47:11 -0800559// ---------------------------------------------------------------------------
560// drawing...
561// ---------------------------------------------------------------------------
562
Vishnu Nair9b079a22020-01-21 14:36:08 -0800563std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100564 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800565 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800566 return {};
567 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800568
Vishnu Nair4351ad52019-02-11 14:13:02 -0800569 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000570 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800571
572 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800573 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700574 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000575
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400576 // skip drawing content if the targetSettings indicate the content will be occluded
Lucas Dupin0fe42e52021-05-27 17:27:15 -0700577 layerSettings.skipContentDraw =
578 layerSettings.skipContentDraw || !targetSettings.realContentIsVisible;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400579
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000580 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800581 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000582 }
583
584 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800585 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
586 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000587
Lloyd Piquef16688f2019-02-19 17:47:57 -0800588 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700589 layerSettings.sourceDataspace = getDataSpace();
Alec Mourif54453c2021-05-13 16:28:28 -0700590 switch (targetSettings.blurSetting) {
591 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Enabled:
592 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
593 layerSettings.blurRegions = getBlurRegions();
594 layerSettings.blurRegionTransform =
595 getActiveTransform(getDrawingState()).inverse().asMatrix4();
596 break;
597 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BackgroundBlurOnly:
598 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
599 break;
600 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BlurRegionsOnly:
601 layerSettings.blurRegions = getBlurRegions();
602 layerSettings.blurRegionTransform =
603 getActiveTransform(getDrawingState()).inverse().asMatrix4();
604 break;
605 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled:
606 default:
607 break;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100608 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700609 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800610 // Record the name of the layer for debugging further down the stack.
611 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800612 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800613}
614
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800615void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
616 bool blackout) const {
617 layerSettings.source.buffer.buffer = nullptr;
618 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
619 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800620 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800621 layerSettings.frameNumber = 0;
622
623 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
624 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800625 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800626}
627
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400628// TODO(b/188891810): This method now only ever returns 0 or 1 layers so we should return
629// std::optional instead of a vector. Additionally, we should consider removing
630// this method entirely in favor of calling prepareClientComposition directly.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800631std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
632 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
633 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
634 prepareClientComposition(targetSettings);
635 // Nothing to render.
636 if (!layerSettings) {
637 return {};
638 }
639
640 // HWC requests to clear this layer.
641 if (targetSettings.clearContent) {
642 prepareClearClientComposition(*layerSettings, false /* blackout */);
643 return {*layerSettings};
644 }
645
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400646 // set the shadow for the layer if needed
647 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800648
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400649 return {*layerSettings};
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800650}
651
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700652Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
653 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800654 if (outputLayer == nullptr) {
655 return Hwc2::IComposerClient::Composition::INVALID;
656 }
657 if (outputLayer->getState().hwc) {
658 return (*outputLayer->getState().hwc).hwcCompositionType;
659 } else {
660 return Hwc2::IComposerClient::Composition::CLIENT;
661 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800662}
663
Mathias Agopian13127d82013-03-05 17:47:11 -0800664// ----------------------------------------------------------------------------
665// local state
666// ----------------------------------------------------------------------------
667
David Sodman41fdfc92017-11-06 16:09:56 -0800668bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800669 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700670 if (s.flags & layer_state_t::eLayerSecure) {
671 return true;
672 }
673
674 const auto p = mDrawingParent.promote();
675 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700676}
677
Mathias Agopian13127d82013-03-05 17:47:11 -0800678// ----------------------------------------------------------------------------
679// transaction
680// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800681
Marissa Wall61c58622018-07-18 10:12:20 -0700682uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000683 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684
Marissa Wall61c58622018-07-18 10:12:20 -0700685 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
686 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700687
David Sodmaneb085e02017-10-05 18:49:04 -0700688 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700689 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000690 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800691 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
692 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
693 " requested={ wh={%4u,%4u} }}\n"
694 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
695 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700696 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700697 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
chaviw25714502021-02-11 10:01:08 -0800698 stateToCommit->crop.left, stateToCommit->crop.top, stateToCommit->crop.right,
699 stateToCommit->crop.bottom, stateToCommit->crop.getWidth(),
700 stateToCommit->crop.getHeight(), stateToCommit->requested_legacy.w,
701 stateToCommit->requested_legacy.h, s.active_legacy.w, s.active_legacy.h,
702 s.crop.left, s.crop.top, s.crop.right, s.crop.bottom, s.crop.getWidth(),
703 s.crop.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700705
Robert Carre392b552017-09-19 12:16:05 -0700706 // Don't let Layer::doTransaction update the drawing state
707 // if we have a pending resize, unless we are in fixed-size mode.
708 // the drawing state will be updated only once we receive a buffer
709 // with the correct size.
710 //
711 // In particular, we want to make sure the clip (which is part
712 // of the geometry state) is latched together with the size but is
713 // latched immediately when no resizing is involved.
714 //
715 // If a sideband stream is attached, however, we want to skip this
716 // optimization so that transactions aren't missed when a buffer
717 // never arrives
718 //
719 // In the case that we don't have a buffer we ignore other factors
720 // and avoid entering the resizePending state. At a high level the
721 // resizePending state is to avoid applying the state of the new buffer
722 // to the old buffer. However in the state where we don't have an old buffer
723 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700724 const bool resizePending =
725 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
726 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700727 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700728 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800729 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700730 flags |= eDontUpdateGeometryState;
731 }
732 }
733
Robert Carr7bf247e2017-05-18 14:02:49 -0700734 // Here we apply various requested geometry states, depending on our
735 // latching configuration. See Layer.h for a detailed discussion of
736 // how geometry latching is controlled.
737 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000738 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700739
Robert Carr7bf247e2017-05-18 14:02:49 -0700740 // There is an awkward asymmetry in the handling of the crop states in the position
741 // states, as can be seen below. Largely this arises from position and transform
742 // being stored in the same data structure while having different latching rules.
743 // b/38182305
744 //
Marissa Wall61c58622018-07-18 10:12:20 -0700745 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700746 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700747 editCurrentState.active_legacy = editCurrentState.requested_legacy;
748 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800749 }
750
Marissa Wall61c58622018-07-18 10:12:20 -0700751 return flags;
752}
753
754uint32_t Layer::doTransaction(uint32_t flags) {
755 ATRACE_CALL();
756
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800757 if (mChildrenChanged) {
758 flags |= eVisibleRegion;
759 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700760 }
761
Robert Carr0758e5d2021-03-11 22:15:04 -0800762 // TODO: This is unfortunate.
763 mCurrentStateModified = mCurrentState.modified;
764 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700765
Robert Carr0758e5d2021-03-11 22:15:04 -0800766 flags = doTransactionResize(flags, &mCurrentState);
Marissa Wall61c58622018-07-18 10:12:20 -0700767
Alec Mourib416efd2018-09-06 21:01:59 +0000768 const State& s(getDrawingState());
Robert Carr0758e5d2021-03-11 22:15:04 -0800769 State& c(getCurrentState());
Marissa Wall61c58622018-07-18 10:12:20 -0700770
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700771 // Translates dest frame into scale and position updates. This helps align geometry calculations
772 // for BufferStateLayer with other layers. This should ideally happen in the client once client
773 // has the display orientation details from WM.
774 updateGeometry();
775
chaviw4e765532021-04-30 12:11:39 -0500776 if (c.width != s.width || c.height != s.height || !(c.transform == s.transform)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800777 // invalidate and recompute the visible regions if needed
778 flags |= Layer::eVisibleRegion;
779 }
780
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700781 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800782 // invalidate and recompute the visible regions if needed
783 flags |= eVisibleRegion;
784 this->contentDirty = true;
785
786 // we may use linear filtering, if the matrix scales us
Marin Shalamanov043ba292020-09-10 13:35:20 +0200787 mNeedsFiltering = getActiveTransform(c).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800788 }
789
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800790 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700791 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800792 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700793 }
794
chaviw47dbd692020-10-26 17:50:12 -0700795 // Add the callbacks from the drawing state into the current state. This is so when the current
796 // state gets copied to drawing, we don't lose the callback handles that are still in drawing.
797 for (auto& handle : s.callbackHandles) {
798 c.callbackHandles.push_back(handle);
799 }
800
Vishnu Nair1506b182021-02-22 14:35:15 -0800801 // Allow BufferStateLayer to release any unlatched buffers in drawing state.
Alec Mouria90a5702021-04-16 16:36:21 +0000802 bufferMayChange(c.buffer->getBuffer());
Vishnu Nair1506b182021-02-22 14:35:15 -0800803
Mathias Agopian13127d82013-03-05 17:47:11 -0800804 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700805 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800806 mCurrentState.callbackHandles = {};
Robert Carra1257842020-01-31 13:48:28 -0800807
Mathias Agopian13127d82013-03-05 17:47:11 -0800808 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809}
810
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000811void Layer::commitTransaction(State& stateToCommit) {
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000812 if (auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
Alec Mouria90a5702021-04-16 16:36:21 +0000813 ((mDrawingState.buffer && stateToCommit.buffer &&
814 mDrawingState.buffer->getBuffer() != stateToCommit.buffer->getBuffer()) ||
815 (mDrawingState.buffer && !stateToCommit.buffer) ||
816 (!mDrawingState.buffer && stateToCommit.buffer)) &&
817 bufferSurfaceFrame != nullptr &&
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000818 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
819 // If the previous buffer was committed but not latched (refreshPending - happens during
820 // back to back invalidates), it gets silently dropped here. Mark the corresponding
821 // SurfaceFrame as dropped to prevent it from getting stuck in the pending classification
822 // list.
823 addSurfaceFrameDroppedForBuffer(bufferSurfaceFrame);
824 }
Ady Abraham59fd8ff2021-04-15 20:13:30 -0700825 const bool frameRateVoteChanged =
826 mDrawingState.frameRateForLayerTree != stateToCommit.frameRateForLayerTree;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800827 mDrawingState = stateToCommit;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000828
Ady Abraham59fd8ff2021-04-15 20:13:30 -0700829 if (frameRateVoteChanged) {
830 mFlinger->mScheduler->recordLayerHistory(this, systemTime(),
831 LayerHistory::LayerUpdateType::SetFrameRate);
832 }
833
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000834 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
835 // bufferSurfaceFrameTX will be presented in latchBuffer.
836 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
837 if (surfaceFrame->getPresentState() != PresentState::Presented) {
838 // With applyPendingStates, we could end up having presented surfaceframes from previous
839 // states
840 surfaceFrame->setPresentState(PresentState::Presented);
841 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
842 }
843 }
844 // Clear the surfaceFrames from the old state now that it has been copied into DrawingState.
845 stateToCommit.bufferSurfaceFrameTX.reset();
846 stateToCommit.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700847}
848
Mathias Agopian13127d82013-03-05 17:47:11 -0800849uint32_t Layer::getTransactionFlags(uint32_t flags) {
Robert Carr38d25002021-06-11 14:30:09 -0700850 auto ret = mTransactionFlags & flags;
851 mTransactionFlags &= ~flags;
852 return ret;
Mathias Agopian13127d82013-03-05 17:47:11 -0800853}
854
855uint32_t Layer::setTransactionFlags(uint32_t flags) {
Robert Carr38d25002021-06-11 14:30:09 -0700856 return mTransactionFlags |= flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800857}
858
chaviw214c89d2019-09-04 16:03:53 -0700859bool Layer::setPosition(float x, float y) {
chaviw4e765532021-04-30 12:11:39 -0500860 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700862
863 // We update the requested and active position simultaneously because
864 // we want to apply the position portion of the transform matrix immediately,
865 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
chaviw4e765532021-04-30 12:11:39 -0500866 mCurrentState.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700867 // Here we directly update the active state
868 // unlike other setters, because we store it within
869 // the transform, but use different latching rules.
870 // b/38182305
chaviw4e765532021-04-30 12:11:39 -0500871 mCurrentState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700872
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800874 setTransactionFlags(eTransactionNeeded);
875 return true;
876}
Robert Carr82364e32016-05-15 11:27:47 -0700877
Robert Carr1f0a16a2016-10-24 16:27:39 -0700878bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
879 ssize_t idx = mCurrentChildren.indexOf(childLayer);
880 if (idx < 0) {
881 return false;
882 }
883 if (childLayer->setLayer(z)) {
884 mCurrentChildren.removeAt(idx);
885 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800886 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700887 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800888 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700889}
890
Robert Carr503c7042017-09-27 15:06:08 -0700891bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
892 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
893 ssize_t idx = mCurrentChildren.indexOf(childLayer);
894 if (idx < 0) {
895 return false;
896 }
897 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
898 mCurrentChildren.removeAt(idx);
899 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800900 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700901 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800902 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700903}
904
Robert Carrae060832016-11-28 10:51:00 -0800905bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800906 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
907 mCurrentState.sequence++;
908 mCurrentState.z = z;
909 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700910
911 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800912 if (mCurrentState.zOrderRelativeOf != nullptr) {
913 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700914 if (strongRelative != nullptr) {
915 strongRelative->removeZOrderRelative(this);
916 }
chaviw606e5cf2019-03-01 10:12:10 -0800917 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700918 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800919 setTransactionFlags(eTransactionNeeded);
920 return true;
921}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700922
Robert Carrdb66e622017-04-10 16:55:57 -0700923void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800924 mCurrentState.zOrderRelatives.remove(relative);
925 mCurrentState.sequence++;
926 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700927 setTransactionFlags(eTransactionNeeded);
928}
929
930void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 mCurrentState.zOrderRelatives.add(relative);
932 mCurrentState.modified = true;
933 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700934 setTransactionFlags(eTransactionNeeded);
935}
936
chaviw606e5cf2019-03-01 10:12:10 -0800937void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
938 mCurrentState.zOrderRelativeOf = relativeOf;
939 mCurrentState.sequence++;
940 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700941 mCurrentState.isRelativeOf = relativeOf != nullptr;
942
chaviw606e5cf2019-03-01 10:12:10 -0800943 setTransactionFlags(eTransactionNeeded);
944}
945
Robert Carr503d2bd2017-12-04 15:49:47 -0800946bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700947 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
948 if (handle == nullptr) {
949 return false;
950 }
951 sp<Layer> relative = handle->owner.promote();
952 if (relative == nullptr) {
953 return false;
954 }
955
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800956 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
957 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800958 return false;
959 }
960
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800961 mCurrentState.sequence++;
962 mCurrentState.modified = true;
963 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700964
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800965 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700966 if (oldZOrderRelativeOf != nullptr) {
967 oldZOrderRelativeOf->removeZOrderRelative(this);
968 }
chaviw606e5cf2019-03-01 10:12:10 -0800969 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700970 relative->addZOrderRelative(this);
971
972 setTransactionFlags(eTransactionNeeded);
973
974 return true;
975}
976
Mathias Agopian13127d82013-03-05 17:47:11 -0800977bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800978 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700979 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800980 mCurrentState.requested_legacy.w = w;
981 mCurrentState.requested_legacy.h = h;
982 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800983 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700984
985 // record the new size, from this point on, when the client request
986 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800987 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800988 return true;
989}
chaviw4e765532021-04-30 12:11:39 -0500990
Dan Stoza9e56aa02015-11-02 13:00:03 -0800991bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800992 if (mCurrentState.color.a == alpha) return false;
993 mCurrentState.sequence++;
994 mCurrentState.color.a = alpha;
995 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800996 setTransactionFlags(eTransactionNeeded);
997 return true;
998}
chaviw13fdc492017-06-27 12:40:18 -0700999
Valerie Haudd0b7572019-01-29 14:59:27 -08001000bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1001 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001002 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001003 }
1004 mCurrentState.sequence++;
1005 mCurrentState.modified = true;
1006 setTransactionFlags(eTransactionNeeded);
1007
1008 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001009 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -08001010 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001011 std::string name = mName + "BackgroundColorLayer";
Vishnu Nairfa247b12020-02-11 08:58:26 -08001012 mCurrentState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001013 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1014 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001015
Valerie Haudd0b7572019-01-29 14:59:27 -08001016 // add to child list
1017 addChild(mCurrentState.bgColorLayer);
1018 mFlinger->mLayersAdded = true;
1019 // set up SF to handle added color layer
1020 if (isRemovedFromCurrentState()) {
1021 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1022 }
1023 mFlinger->setTransactionFlags(eTransactionNeeded);
1024 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1025 mCurrentState.bgColorLayer->reparent(nullptr);
1026 mCurrentState.bgColorLayer = nullptr;
1027 return true;
1028 }
1029
1030 mCurrentState.bgColorLayer->setColor(color);
1031 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1032 mCurrentState.bgColorLayer->setAlpha(alpha);
1033 mCurrentState.bgColorLayer->setDataspace(dataspace);
1034
chaviw13fdc492017-06-27 12:40:18 -07001035 return true;
1036}
1037
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001038bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001039 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001040
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001041 mCurrentState.sequence++;
1042 mCurrentState.cornerRadius = cornerRadius;
1043 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001044 setTransactionFlags(eTransactionNeeded);
1045 return true;
1046}
1047
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001048bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1049 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1050
1051 mCurrentState.sequence++;
1052 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1053 mCurrentState.modified = true;
1054 setTransactionFlags(eTransactionNeeded);
1055 return true;
1056}
1057
Robert Carrd4ae7f32018-06-07 16:10:57 -07001058bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1059 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001060 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001061 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1062
1063 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -08001064 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
1065 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -07001066 return false;
1067 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001068 mCurrentState.sequence++;
chaviw4e765532021-04-30 12:11:39 -05001069 mCurrentState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001070 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 setTransactionFlags(eTransactionNeeded);
1072 return true;
1073}
Marissa Wall61c58622018-07-18 10:12:20 -07001074
Mathias Agopian13127d82013-03-05 17:47:11 -08001075bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001076 mCurrentState.requestedTransparentRegion_legacy = transparent;
1077 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001078 setTransactionFlags(eTransactionNeeded);
1079 return true;
1080}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001081
Lucas Dupinc3800b82020-10-02 16:24:48 -07001082bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
1083 mCurrentState.sequence++;
1084 mCurrentState.blurRegions = blurRegions;
1085 mCurrentState.modified = true;
1086 setTransactionFlags(eTransactionNeeded);
1087 return true;
1088}
1089
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001090bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001091 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1092 if (mCurrentState.flags == newFlags) return false;
1093 mCurrentState.sequence++;
1094 mCurrentState.flags = newFlags;
1095 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 setTransactionFlags(eTransactionNeeded);
1097 return true;
1098}
Robert Carr99e27f02016-06-16 15:18:02 -07001099
chaviw25714502021-02-11 10:01:08 -08001100bool Layer::setCrop(const Rect& crop) {
1101 if (mCurrentState.requestedCrop == crop) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001102 mCurrentState.sequence++;
chaviw25714502021-02-11 10:01:08 -08001103 mCurrentState.requestedCrop = crop;
1104 mCurrentState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001105
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001106 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001107 setTransactionFlags(eTransactionNeeded);
1108 return true;
1109}
Robert Carr8d5227b2017-03-16 15:41:03 -07001110
Evan Roskyef876c92019-01-25 17:46:06 -08001111bool Layer::setMetadata(const LayerMetadata& data) {
1112 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001114 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001115 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001116}
1117
Mathias Agopian13127d82013-03-05 17:47:11 -08001118bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 if (mCurrentState.layerStack == layerStack) return false;
1120 mCurrentState.sequence++;
1121 mCurrentState.layerStack = layerStack;
1122 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001123 setTransactionFlags(eTransactionNeeded);
1124 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001125}
1126
Peiyong Linc502cb72019-03-01 15:00:23 -08001127bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1128 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1129 return false;
1130 }
1131 mCurrentState.sequence++;
1132 mCurrentState.colorSpaceAgnostic = agnostic;
1133 mCurrentState.modified = true;
1134 setTransactionFlags(eTransactionNeeded);
1135 return true;
1136}
1137
Ana Krulecc84d09b2019-11-02 23:10:29 +01001138bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1139 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1140 mCurrentState.frameRateSelectionPriority = priority;
1141 mCurrentState.sequence++;
1142 mCurrentState.modified = true;
1143 setTransactionFlags(eTransactionNeeded);
1144 return true;
1145}
1146
1147int32_t Layer::getFrameRateSelectionPriority() const {
1148 // Check if layer has priority set.
1149 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1150 return mDrawingState.frameRateSelectionPriority;
1151 }
1152 // If not, search whether its parents have it set.
1153 sp<Layer> parent = getParent();
1154 if (parent != nullptr) {
1155 return parent->getFrameRateSelectionPriority();
1156 }
1157
1158 return Layer::PRIORITY_UNSET;
1159}
1160
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001161bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1162 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1163};
1164
Robert Carr1f0a16a2016-10-24 16:27:39 -07001165uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001166 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001167 if (p == nullptr) {
1168 return getDrawingState().layerStack;
1169 }
1170 return p->getLayerStack();
1171}
1172
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001173bool Layer::setShadowRadius(float shadowRadius) {
1174 if (mCurrentState.shadowRadius == shadowRadius) {
1175 return false;
1176 }
1177
1178 mCurrentState.sequence++;
1179 mCurrentState.shadowRadius = shadowRadius;
1180 mCurrentState.modified = true;
1181 setTransactionFlags(eTransactionNeeded);
1182 return true;
1183}
1184
Vishnu Nair6213bd92020-05-08 17:42:25 -07001185bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
1186 if (mCurrentState.fixedTransformHint == fixedTransformHint) {
1187 return false;
1188 }
1189
1190 mCurrentState.sequence++;
1191 mCurrentState.fixedTransformHint = fixedTransformHint;
1192 mCurrentState.modified = true;
1193 setTransactionFlags(eTransactionNeeded);
1194 return true;
1195}
1196
John Reckcdb4ed72021-02-04 13:39:33 -05001197bool Layer::setStretchEffect(const StretchEffect& effect) {
1198 StretchEffect temp = effect;
1199 temp.sanitize();
1200 if (mCurrentState.stretchEffect == temp) {
1201 return false;
1202 }
1203 mCurrentState.sequence++;
1204 mCurrentState.stretchEffect = temp;
1205 mCurrentState.modified = true;
1206 setTransactionFlags(eTransactionNeeded);
1207 return true;
1208}
1209
John Reckc00c6692021-02-16 11:37:33 -05001210StretchEffect Layer::getStretchEffect() const {
1211 if (mDrawingState.stretchEffect.hasEffect()) {
1212 return mDrawingState.stretchEffect;
1213 }
1214
1215 sp<Layer> parent = getParent();
1216 if (parent != nullptr) {
1217 auto effect = parent->getStretchEffect();
1218 if (effect.hasEffect()) {
1219 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1220 return effect;
1221 }
1222 }
1223 return StretchEffect{};
1224}
1225
Ady Abraham60e42ea2020-03-09 19:17:31 -07001226void Layer::updateTreeHasFrameRateVote() {
1227 const auto traverseTree = [&](const LayerVector::Visitor& visitor) {
1228 auto parent = getParent();
1229 while (parent) {
1230 visitor(parent.get());
1231 parent = parent->getParent();
1232 }
1233
1234 traverse(LayerVector::StateSet::Current, visitor);
1235 };
1236
1237 // update parents and children about the vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001238 // First traverse the tree and count how many layers has votes.
Ady Abraham60e42ea2020-03-09 19:17:31 -07001239 int layersWithVote = 0;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001240 traverseTree([&layersWithVote](Layer* layer) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001241 const auto layerVotedWithDefaultCompatibility =
1242 layer->mCurrentState.frameRate.rate.isValid() &&
Ady Abraham414e8b52020-05-21 15:36:04 -07001243 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Default;
1244 const auto layerVotedWithNoVote =
1245 layer->mCurrentState.frameRate.type == FrameRateCompatibility::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001246 const auto layerVotedWithExactCompatibility =
1247 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Exact;
Ady Abraham414e8b52020-05-21 15:36:04 -07001248
1249 // We do not count layers that are ExactOrMultiple for the same reason
1250 // we are allowing touch boost for those layers. See
1251 // RefreshRateConfigs::getBestRefreshRate for more details.
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001252 if (layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1253 layerVotedWithExactCompatibility) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001254 layersWithVote++;
1255 }
1256 });
1257
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001258 // Now we can update the tree frame rate vote for each layer in the tree
1259 const bool treeHasFrameRateVote = layersWithVote > 0;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001260 bool transactionNeeded = false;
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001261 traverseTree([treeHasFrameRateVote, &transactionNeeded](Layer* layer) {
1262 transactionNeeded = layer->updateFrameRateForLayerTree(treeHasFrameRateVote);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001263 });
1264
1265 if (transactionNeeded) {
1266 mFlinger->setTransactionFlags(eTraversalNeeded);
1267 }
1268}
1269
Ady Abraham71c437d2020-01-31 15:56:57 -08001270bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001271 if (!mFlinger->useFrameRateApi) {
1272 return false;
1273 }
Steven Thomas3172e202020-01-06 19:25:30 -08001274 if (mCurrentState.frameRate == frameRate) {
1275 return false;
1276 }
1277
1278 mCurrentState.sequence++;
1279 mCurrentState.frameRate = frameRate;
1280 mCurrentState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001281
1282 updateTreeHasFrameRateVote();
1283
Steven Thomas3172e202020-01-06 19:25:30 -08001284 setTransactionFlags(eTransactionNeeded);
1285 return true;
1286}
1287
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001288void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1289 nsecs_t postTime) {
1290 mCurrentState.postTime = postTime;
1291
1292 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1293 // there are two transactions with the same token, the first one without a buffer and the
1294 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1295 // in that case.
1296 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1297 if (it != mCurrentState.bufferlessSurfaceFramesTX.end()) {
1298 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1299 mCurrentState.bufferSurfaceFrameTX = it->second;
1300 mCurrentState.bufferlessSurfaceFramesTX.erase(it);
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001301 mCurrentState.bufferSurfaceFrameTX->promoteToBuffer();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001302 mCurrentState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
1303 } else {
1304 mCurrentState.bufferSurfaceFrameTX =
1305 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1306 }
1307}
1308
1309void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1310 nsecs_t postTime) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001311 mCurrentState.frameTimelineInfo = info;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001312 mCurrentState.postTime = postTime;
1313 mCurrentState.modified = true;
1314 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001315
1316 if (const auto& bufferSurfaceFrameTX = mCurrentState.bufferSurfaceFrameTX;
1317 bufferSurfaceFrameTX != nullptr) {
1318 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1319 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1320 // being used for BufferSurfaceFrame, don't create a new one.
1321 return;
1322 }
1323 }
1324 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1325 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1326 // targeting different vsyncs).
1327 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1328 if (it == mCurrentState.bufferlessSurfaceFramesTX.end()) {
1329 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
1330 mCurrentState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
1331 } else {
1332 if (it->second->getPresentState() == PresentState::Presented) {
1333 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1334 // have been from previous vsync.
1335 it->second = createSurfaceFrameForTransaction(info, postTime);
1336 }
1337 }
1338}
1339
1340void Layer::addSurfaceFrameDroppedForBuffer(
1341 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001342 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001343 surfaceFrame->setPresentState(PresentState::Dropped);
1344 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1345}
1346
1347void Layer::addSurfaceFramePresentedForBuffer(
1348 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1349 nsecs_t currentLatchTime) {
1350 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1351 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1352 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1353 mLastLatchTime = currentLatchTime;
1354}
1355
1356std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1357 const FrameTimelineInfo& info, nsecs_t postTime) {
1358 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001359 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1360 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001361 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001362 /*isBuffer*/ false, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001363 // For Transactions, the post time is considered to be both queue and acquire fence time.
1364 surfaceFrame->setActualQueueTime(postTime);
1365 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001366 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1367 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001368 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001369 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001370 onSurfaceFrameCreated(surfaceFrame);
1371 return surfaceFrame;
1372}
1373
1374std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1375 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1376 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001377 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001378 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001379 /*isBuffer*/ true, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001380 // For buffers, acquire fence time will set during latch.
1381 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001382 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1383 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001384 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001385 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001386 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1387 onSurfaceFrameCreated(surfaceFrame);
1388 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001389}
1390
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001391bool Layer::updateFrameRateForLayerTree(bool treeHasFrameRateVote) {
1392 const auto updateCurrentState = [&](FrameRate frameRate) {
1393 if (mCurrentState.frameRateForLayerTree == frameRate) {
1394 return false;
1395 }
1396 mCurrentState.frameRateForLayerTree = frameRate;
1397 mCurrentState.sequence++;
1398 mCurrentState.modified = true;
1399 setTransactionFlags(eTransactionNeeded);
1400 return true;
1401 };
1402
1403 const auto frameRate = mCurrentState.frameRate;
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001404 if (frameRate.rate.isValid() || frameRate.type == FrameRateCompatibility::NoVote) {
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001405 return updateCurrentState(frameRate);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001406 }
1407
Ady Abrahamf467f892020-07-31 16:01:53 -07001408 // This layer doesn't have a frame rate. Check if its ancestors have a vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001409 for (sp<Layer> parent = getParent(); parent; parent = parent->getParent()) {
1410 if (parent->mCurrentState.frameRate.rate.isValid()) {
1411 return updateCurrentState(parent->mCurrentState.frameRate);
Ady Abrahamf467f892020-07-31 16:01:53 -07001412 }
1413 }
1414
1415 // This layer and its ancestors don't have a frame rate. If one of successors
1416 // has a vote, return a NoVote for successors to set the vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001417 if (treeHasFrameRateVote) {
1418 return updateCurrentState(FrameRate(Fps(0.0f), FrameRateCompatibility::NoVote));
Ady Abraham60e42ea2020-03-09 19:17:31 -07001419 }
1420
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001421 return updateCurrentState(frameRate);
Steven Thomas3172e202020-01-06 19:25:30 -08001422}
1423
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001424Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1425 return getDrawingState().frameRateForLayerTree;
1426}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427
Robert Carr1f0a16a2016-10-24 16:27:39 -07001428bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001429 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001430 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001431 if (parent != nullptr && parent->isHiddenByPolicy()) {
1432 return true;
1433 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001434 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1435 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1436 if (zOrderRelativeOf != nullptr) {
1437 if (zOrderRelativeOf->isHiddenByPolicy()) {
1438 return true;
1439 }
1440 }
1441 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001442 return s.flags & layer_state_t::eLayerHidden;
1443}
1444
David Sodman41fdfc92017-11-06 16:09:56 -08001445uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001446 // TODO: should we do something special if mSecure is set?
1447 if (mProtectedByApp) {
1448 // need a hardware-protected path to external video sink
1449 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001450 }
Riley Andrews03414a12014-07-01 14:22:59 -07001451 if (mPotentialCursor) {
1452 usage |= GraphicBuffer::USAGE_CURSOR;
1453 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001454 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001455 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001456}
1457
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001458void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1459 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1460 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001461 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001462
Vishnu Nair6213bd92020-05-08 17:42:25 -07001463 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001464}
1465
Mathias Agopian13127d82013-03-05 17:47:11 -08001466// ----------------------------------------------------------------------------
1467// debugging
1468// ----------------------------------------------------------------------------
1469
Marissa Wall61c58622018-07-18 10:12:20 -07001470// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001471LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001472 using namespace std::string_literals;
1473
Kalle Raitaa099a242017-01-11 11:17:29 -08001474 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001475 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001476 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001477 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001478 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001479 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001480 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001481
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001482 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001483 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001484 info.mLayerStack = getLayerStack();
chaviw4e765532021-04-30 12:11:39 -05001485 info.mX = ds.transform.tx();
1486 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001487 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001488 info.mWidth = ds.width;
1489 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001490 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001491 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001492 info.mFlags = ds.flags;
1493 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001494 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001495 info.mMatrix[0][0] = ds.transform[0][0];
1496 info.mMatrix[0][1] = ds.transform[0][1];
1497 info.mMatrix[1][0] = ds.transform[1][0];
1498 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001499 {
chaviwd62d3062019-09-04 14:48:02 -07001500 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001501 if (buffer != 0) {
1502 info.mActiveBufferWidth = buffer->getWidth();
1503 info.mActiveBufferHeight = buffer->getHeight();
1504 info.mActiveBufferStride = buffer->getStride();
1505 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001506 } else {
1507 info.mActiveBufferWidth = 0;
1508 info.mActiveBufferHeight = 0;
1509 info.mActiveBufferStride = 0;
1510 info.mActiveBufferFormat = 0;
1511 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001512 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001513 info.mNumQueuedFrames = getQueuedFrameCount();
1514 info.mRefreshPending = isBufferLatched();
1515 info.mIsOpaque = isOpaque(ds);
1516 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001517 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001518 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001519}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001520
Yiwei Zhang5434a782018-12-05 18:06:32 -08001521void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001522 result.append(kDumpTableRowLength, '-');
1523 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001524 result.append(" Layer name\n");
1525 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001526 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001527 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001528 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001529 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001530 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001531 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1532 result.append(kDumpTableRowLength, '-');
1533 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001534}
1535
1536std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1537 switch (compatibility) {
1538 case FrameRateCompatibility::Default:
1539 return "Default";
1540 case FrameRateCompatibility::ExactOrMultiple:
1541 return "ExactOrMultiple";
1542 case FrameRateCompatibility::NoVote:
1543 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001544 case FrameRateCompatibility::Exact:
1545 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001546 }
Dan Stozae22aec72016-08-01 13:20:59 -07001547}
1548
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001549void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1550 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001551 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001552 return;
1553 }
1554
Yiwei Zhang5434a782018-12-05 18:06:32 -08001555 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001556 if (mName.length() > 77) {
1557 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001558 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001559 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001560 shortened.append(mName, mName.length() - 36);
1561 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001562 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001563 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001564 }
1565
Yiwei Zhang5434a782018-12-05 18:06:32 -08001566 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001567
Alec Mourib416efd2018-09-06 21:01:59 +00001568 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001569 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001570
Chia-I Wu1e043612018-03-01 09:45:09 -08001571 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001572 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001573 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001574 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001575 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001576 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001577 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001578 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1579 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001580 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001581 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001582 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001583 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001584 const auto frameRate = getFrameRateForLayerTree();
1585 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1586 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1587 frameRateCompatibilityString(frameRate.type).c_str(),
1588 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001589 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001590 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001591 }
Dan Stozae22aec72016-08-01 13:20:59 -07001592
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001593 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1594 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1595
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001596 result.append(kDumpTableRowLength, '-');
1597 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001598}
Dan Stozae22aec72016-08-01 13:20:59 -07001599
Yiwei Zhang5434a782018-12-05 18:06:32 -08001600void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001601 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001602}
1603
Svetoslavd85084b2014-03-20 10:28:31 -07001604void Layer::clearFrameStats() {
1605 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001606}
1607
Jamie Gennis6547ff42013-07-16 20:12:42 -07001608void Layer::logFrameStats() {
1609 mFrameTracker.logAndResetStats(mName);
1610}
1611
Svetoslavd85084b2014-03-20 10:28:31 -07001612void Layer::getFrameStats(FrameStats* outStats) const {
1613 mFrameTracker.getStats(outStats);
1614}
1615
Yiwei Zhang5434a782018-12-05 18:06:32 -08001616void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001617 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001618 Mutex::Autolock lock(mFrameEventHistoryMutex);
1619 mFrameEventHistory.checkFencesForCompletion();
1620 mFrameEventHistory.dump(result);
1621}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001622
Vishnu Nair0f085c62019-08-30 08:49:12 -07001623void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001624 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1625 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001626}
1627
Brian Anderson5ea5e592016-12-01 16:54:33 -08001628void Layer::onDisconnect() {
1629 Mutex::Autolock lock(mFrameEventHistoryMutex);
1630 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001631 const int32_t layerId = getSequence();
1632 mFlinger->mTimeStats->onDestroy(layerId);
1633 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001634}
1635
Brian Anderson3890c392016-07-25 12:48:08 -07001636void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001637 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001638 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001639 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001640 getName().c_str(), mOwnerUid, newTimestamps->postedTime,
1641 getGameMode());
Yiwei Zhang487340f2019-11-18 17:42:12 -08001642 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1643 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001644 }
1645
Brian Andersond6927fb2016-07-23 23:37:30 -07001646 Mutex::Autolock lock(mFrameEventHistoryMutex);
1647 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001648 // If there are any unsignaled fences in the aquire timeline at this
1649 // point, the previously queued frame hasn't been latched yet. Go ahead
1650 // and try to get the signal time here so the syscall is taken out of
1651 // the main thread's critical path.
1652 mAcquireTimeline.updateSignalTimes();
1653 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001654 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001655 mFrameEventHistory.addQueue(*newTimestamps);
1656 }
1657
Brian Anderson3890c392016-07-25 12:48:08 -07001658 if (outDelta) {
1659 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001660 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001661}
Dan Stozae77c7662016-05-13 11:37:28 -07001662
Chia-I Wu98f1c102017-05-30 14:54:08 -07001663size_t Layer::getChildrenCount() const {
1664 size_t count = 0;
1665 for (const sp<Layer>& child : mCurrentChildren) {
1666 count += 1 + child->getChildrenCount();
1667 }
1668 return count;
1669}
1670
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001671void Layer::setGameModeForTree(int parentGameMode) {
1672 int gameMode = parentGameMode;
1673 auto& currentState = getCurrentState();
1674 if (currentState.metadata.has(METADATA_GAME_MODE)) {
1675 gameMode = currentState.metadata.getInt32(METADATA_GAME_MODE, 0);
1676 }
1677 setGameMode(gameMode);
1678 for (const sp<Layer>& child : mCurrentChildren) {
1679 child->setGameModeForTree(gameMode);
1680 }
1681}
1682
Robert Carr1f0a16a2016-10-24 16:27:39 -07001683void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001684 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001685 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001686
Robert Carr1f0a16a2016-10-24 16:27:39 -07001687 mCurrentChildren.add(layer);
1688 layer->setParent(this);
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001689 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001690 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001691}
1692
1693ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001694 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001695 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001696
Robert Carr1323c952019-01-28 18:13:27 -08001697 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001698 const auto removeResult = mCurrentChildren.remove(layer);
1699
1700 updateTreeHasFrameRateVote();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001701 layer->setGameModeForTree(0);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001702 layer->updateTreeHasFrameRateVote();
1703
1704 return removeResult;
1705}
1706
Robert Carr15eae092018-03-23 13:43:53 -07001707void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001708 for (const sp<Layer>& child : mDrawingChildren) {
1709 child->mDrawingParent = newParent;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001710 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001711 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001712 }
1713}
1714
chaviwf1961f72017-09-18 16:41:07 -07001715bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001716 sp<Layer> newParent;
1717 if (newParentHandle != nullptr) {
1718 auto handle = static_cast<Handle*>(newParentHandle.get());
1719 newParent = handle->owner.promote();
1720 if (newParent == nullptr) {
1721 ALOGE("Unable to promote Layer handle");
1722 return false;
1723 }
1724 if (newParent == this) {
1725 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1726 return false;
1727 }
1728 }
1729
chaviwf1961f72017-09-18 16:41:07 -07001730 sp<Layer> parent = getParent();
1731 if (parent != nullptr) {
1732 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001733 }
1734
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001735 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001736 newParent->addChild(this);
1737 if (!newParent->isRemovedFromCurrentState()) {
1738 addToCurrentState();
1739 } else {
1740 onRemovedFromCurrentState();
1741 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001742 } else {
1743 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001744 }
1745
chaviw06178942017-07-27 10:25:59 -07001746 return true;
1747}
1748
Peiyong Lind3788632018-09-18 16:01:31 -07001749bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001750 static const mat4 identityMatrix = mat4();
1751
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001752 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001753 return false;
1754 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001755 ++mCurrentState.sequence;
1756 mCurrentState.colorTransform = matrix;
1757 mCurrentState.hasColorTransform = matrix != identityMatrix;
1758 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001759 setTransactionFlags(eTransactionNeeded);
1760 return true;
1761}
1762
chaviwf66724d2018-11-28 16:35:21 -08001763mat4 Layer::getColorTransform() const {
1764 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1765 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1766 colorTransform = parent->getColorTransform() * colorTransform;
1767 }
1768 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001769}
1770
1771bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001772 bool hasColorTransform = getDrawingState().hasColorTransform;
1773 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1774 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1775 }
1776 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001777}
1778
Chia-I Wu11481472018-05-04 10:43:19 -07001779bool Layer::isLegacyDataSpace() const {
1780 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001781 return !(getDataSpace() &
1782 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1783 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001784}
1785
Robert Carr1f0a16a2016-10-24 16:27:39 -07001786void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001787 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001788}
1789
chaviw301b1d82019-11-06 13:15:09 -08001790int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1791 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1792 const State& state = useDrawing ? mDrawingState : mCurrentState;
1793 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001794}
1795
Robert Carr1c5481e2019-07-01 14:42:27 -07001796bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001797 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001798 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001799 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001800}
1801
David Sodman41fdfc92017-11-06 16:09:56 -08001802__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001803 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001804 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1805 "makeTraversalList received invalid stateSet");
1806 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1807 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001808 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001809
Robert Carr29abff82017-12-04 13:51:20 -08001810 if (state.zOrderRelatives.size() == 0) {
1811 *outSkipRelativeZUsers = true;
1812 return children;
1813 }
1814
chaviwfd462612018-05-31 16:11:27 -07001815 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001816 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001817 sp<Layer> strongRelative = weakRelative.promote();
1818 if (strongRelative != nullptr) {
1819 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001820 }
1821 }
1822
Dan Stoza412903f2017-04-27 13:42:17 -07001823 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001824 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001825 continue;
1826 }
Robert Carrdb66e622017-04-10 16:55:57 -07001827 traverse.add(child);
1828 }
1829
1830 return traverse;
1831}
1832
Robert Carr1f0a16a2016-10-24 16:27:39 -07001833/**
Robert Carrdb66e622017-04-10 16:55:57 -07001834 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001835 */
Dan Stoza412903f2017-04-27 13:42:17 -07001836void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001837 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1838 // produce a new list for traversing, including our relatives, and not including our children
1839 // who are relatives of another surface. In the case that there are no relative Z,
1840 // makeTraversalList returns our children directly to avoid significant overhead.
1841 // However in this case we need to take the responsibility for filtering children which
1842 // are relatives of another surface here.
1843 bool skipRelativeZUsers = false;
1844 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001845
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001847 for (; i < list.size(); i++) {
1848 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001849 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1850 continue;
1851 }
1852
chaviw301b1d82019-11-06 13:15:09 -08001853 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001854 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001855 }
Dan Stoza412903f2017-04-27 13:42:17 -07001856 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001857 }
Robert Carr29abff82017-12-04 13:51:20 -08001858
Dan Stoza412903f2017-04-27 13:42:17 -07001859 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001860 for (; i < list.size(); i++) {
1861 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001862
Robert Carr29abff82017-12-04 13:51:20 -08001863 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1864 continue;
1865 }
Dan Stoza412903f2017-04-27 13:42:17 -07001866 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867 }
1868}
1869
1870/**
Robert Carrdb66e622017-04-10 16:55:57 -07001871 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001872 */
Dan Stoza412903f2017-04-27 13:42:17 -07001873void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1874 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001875 // See traverseInZOrder for documentation.
1876 bool skipRelativeZUsers = false;
1877 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001878
Robert Carr1f0a16a2016-10-24 16:27:39 -07001879 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001880 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001881 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001882
1883 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1884 continue;
1885 }
1886
chaviw301b1d82019-11-06 13:15:09 -08001887 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001888 break;
1889 }
Dan Stoza412903f2017-04-27 13:42:17 -07001890 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001891 }
Dan Stoza412903f2017-04-27 13:42:17 -07001892 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001893 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001894 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001895
1896 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1897 continue;
1898 }
1899
Dan Stoza412903f2017-04-27 13:42:17 -07001900 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001901 }
1902}
1903
Edgar Arriaga844fa672020-01-16 14:21:42 -08001904void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1905 visitor(this);
1906 const LayerVector& children =
1907 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1908 for (const sp<Layer>& child : children) {
1909 child->traverse(state, visitor);
1910 }
1911}
1912
chaviw4b129c22018-04-09 16:19:43 -07001913LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1914 const std::vector<Layer*>& layersInTree) {
1915 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1916 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001917 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1918 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001919 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001920
chaviwfd462612018-05-31 16:11:27 -07001921 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001922 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1923 sp<Layer> strongRelative = weakRelative.promote();
1924 // Only add relative layers that are also descendents of the top most parent of the tree.
1925 // If a relative layer is not a descendent, then it should be ignored.
1926 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1927 traverse.add(strongRelative);
1928 }
1929 }
1930
1931 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001932 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001933 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1934 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1935 // the child here since it won't be added later as a relative.
1936 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1937 childState.zOrderRelativeOf.promote().get())) {
1938 continue;
1939 }
1940 traverse.add(child);
1941 }
1942
1943 return traverse;
1944}
1945
1946void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1947 LayerVector::StateSet stateSet,
1948 const LayerVector::Visitor& visitor) {
1949 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001950
1951 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001952 for (; i < list.size(); i++) {
1953 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001954 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001955 break;
1956 }
chaviw4b129c22018-04-09 16:19:43 -07001957 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001958 }
chaviw4b129c22018-04-09 16:19:43 -07001959
chaviwa76b2712017-09-20 12:02:26 -07001960 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001961 for (; i < list.size(); i++) {
1962 const auto& relative = list[i];
1963 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001964 }
1965}
1966
chaviw4b129c22018-04-09 16:19:43 -07001967std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1968 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1969 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1970
1971 std::vector<Layer*> layersInTree = {this};
1972 for (size_t i = 0; i < children.size(); i++) {
1973 const auto& child = children[i];
1974 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1975 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1976 }
1977
1978 return layersInTree;
1979}
1980
1981void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1982 const LayerVector::Visitor& visitor) {
1983 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1984 std::sort(layersInTree.begin(), layersInTree.end());
1985 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1986}
1987
Peiyong Linefefaac2018-08-17 12:27:51 -07001988ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001989 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001990}
1991
chaviw13fdc492017-06-27 12:40:18 -07001992half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001993 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001994
chaviw13fdc492017-06-27 12:40:18 -07001995 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1996 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001997}
Robert Carr6452f122017-03-21 10:41:29 -07001998
Vishnu Nair6213bd92020-05-08 17:42:25 -07001999ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
2000 ui::Transform::RotationFlags fixedTransformHint = mCurrentState.fixedTransformHint;
2001 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
2002 return fixedTransformHint;
2003 }
2004 const auto& p = mCurrentParent.promote();
2005 if (!p) return fixedTransformHint;
2006 return p->getFixedTransformHint();
2007}
2008
chaviw13fdc492017-06-27 12:40:18 -07002009half4 Layer::getColor() const {
2010 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002011 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002012}
Robert Carr6452f122017-03-21 10:41:29 -07002013
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002014int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01002015 const auto& p = mDrawingParent.promote();
2016
2017 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2018 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002019}
2020
Galia Peychevae0acf382021-04-12 21:22:34 +02002021const std::vector<BlurRegion> Layer::getBlurRegions() const {
2022 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02002023 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02002024 for (auto& region : regionsCopy) {
2025 region.alpha = region.alpha * layerAlpha;
2026 }
2027 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07002028}
2029
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002030Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2031 const auto& p = mDrawingParent.promote();
2032 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00002033 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002034 if (parentState.radius > 0) {
2035 ui::Transform t = getActiveTransform(getDrawingState());
2036 t = t.inverse();
2037 parentState.cropRect = t.transform(parentState.cropRect);
2038 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2039 // but a transform matrix can define horizontal and vertical scales.
2040 // Let's take the average between both of them and pass into the shader, practically we
2041 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07002042 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
2043 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
2044 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002045 return parentState;
2046 }
2047 }
2048 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00002049 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
2050 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07002051 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002052}
2053
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04002054void Layer::prepareShadowClientComposition(LayerFE::LayerSettings& caster,
2055 const Rect& layerStackRect) {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002056 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
2057
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04002058 // Note: this preserves existing behavior of shadowing the entire layer and not cropping it if
2059 // transparent regions are present. This may not be necessary since shadows are only cast by
2060 // SurfaceFlinger's EffectLayers, which do not typically use transparent regions.
2061 state.boundaries = mBounds;
2062
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002063 // Shift the spot light x-position to the middle of the display and then
2064 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002065 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002066 state.lightPos.y -= mScreenBounds.top;
2067
2068 state.length = mEffectiveShadowRadius;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04002069
2070 if (state.length > 0.f) {
2071 const float casterAlpha = caster.alpha;
2072 const bool casterIsOpaque =
2073 ((caster.source.buffer.buffer != nullptr) && caster.source.buffer.isOpaque);
2074
2075 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
2076 // Otherwise the generated shadow will only be shown around the casting layer.
2077 state.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
2078 state.ambientColor *= casterAlpha;
2079 state.spotColor *= casterAlpha;
2080
2081 if (state.ambientColor.a > 0.f && state.spotColor.a > 0.f) {
2082 caster.shadow = state;
2083 }
2084 }
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002085}
2086
Robert Carr1f0a16a2016-10-24 16:27:39 -07002087void Layer::commitChildList() {
2088 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2089 const auto& child = mCurrentChildren[i];
2090 child->commitChildList();
2091 }
2092 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002093 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002094}
2095
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002096static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2097 if (weakBinderHandle == nullptr) {
2098 return nullptr;
2099 }
2100 sp<IBinder> binderHandle = weakBinderHandle.promote();
2101 if (binderHandle == nullptr) {
2102 return nullptr;
2103 }
2104 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2105 if (handle == nullptr) {
2106 return nullptr;
2107 }
2108 return handle->owner;
2109}
2110
Robert Carr720e5062018-07-30 17:45:14 -07002111void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002112 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002113 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002114 mCurrentState.modified = true;
2115 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002116 setTransactionFlags(eTransactionNeeded);
2117}
2118
Alec Mouri6b9e9912020-01-21 10:50:24 -08002119LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002120 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08002121 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002122 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002123 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2124
Vishnu Nair60db8c02020-04-02 11:55:16 -07002125 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2126 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002127 if (display) {
2128 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002129 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2130 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002131 }
2132
chaviw08f3cb22020-01-13 13:17:21 -08002133 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002134 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002135 }
chaviw6d89e2d2020-01-14 14:42:01 -08002136
2137 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002138}
2139
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002140void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002141 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002142 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002143
2144 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002145
chaviwd62d3062019-09-04 14:48:02 -07002146 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002147 if (buffer != nullptr) {
2148 LayerProtoHelper::writeToProto(buffer,
2149 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002150 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002151 layerInfo->mutable_buffer_transform());
2152 }
2153 layerInfo->set_invalidate(contentDirty);
2154 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002155 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002156 layerInfo->set_queued_frames(getQueuedFrameCount());
2157 layerInfo->set_refresh_pending(isBufferLatched());
2158 layerInfo->set_curr_frame(mCurrentFrameNumber);
2159 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2160
2161 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002162 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002163 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2164 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2165 [&]() { return layerInfo->mutable_position(); });
2166 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002167 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002168 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002169 [&]() { return layerInfo->mutable_visible_region(); });
2170 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002171 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2172 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002173
2174 if (hasColorTransform()) {
2175 LayerProtoHelper::writeToProto(getColorTransform(),
2176 layerInfo->mutable_color_transform());
2177 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002178 }
2179
Vishnu Nair60db8c02020-04-02 11:55:16 -07002180 LayerProtoHelper::writeToProto(mSourceBounds,
2181 [&]() { return layerInfo->mutable_source_bounds(); });
2182 LayerProtoHelper::writeToProto(mScreenBounds,
2183 [&]() { return layerInfo->mutable_screen_bounds(); });
2184 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2185 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2186 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002187}
2188
2189void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002190 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002191 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2192 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002193 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002194
chaviw766c9c52021-02-10 17:36:47 -08002195 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002196
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002197 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2198 layerInfo->set_id(sequence);
2199 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002200 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002201
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002202 for (const auto& child : children) {
2203 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002204 }
chaviw1d044282017-09-27 12:19:28 -07002205
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002206 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2207 sp<Layer> strongRelative = weakRelative.promote();
2208 if (strongRelative != nullptr) {
2209 layerInfo->add_relatives(strongRelative->sequence);
2210 }
chaviwadc40c22018-07-10 16:57:27 -07002211 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002212
2213 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2214 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002215
2216 layerInfo->set_layer_stack(getLayerStack());
2217 layerInfo->set_z(state.z);
2218
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002219 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2220 [&]() {
2221 return layerInfo->mutable_requested_position();
2222 });
2223
chaviw766c9c52021-02-10 17:36:47 -08002224 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002225 [&]() { return layerInfo->mutable_size(); });
2226
chaviw766c9c52021-02-10 17:36:47 -08002227 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002228
2229 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002230
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002231
2232 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2233 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2234 LayerProtoHelper::writeToProto(state.color,
2235 [&]() { return layerInfo->mutable_requested_color(); });
2236 layerInfo->set_flags(state.flags);
2237
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002238 LayerProtoHelper::writeToProto(requestedTransform,
2239 layerInfo->mutable_requested_transform());
2240
2241 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2242 if (parent != nullptr) {
2243 layerInfo->set_parent(parent->sequence);
2244 } else {
2245 layerInfo->set_parent(-1);
2246 }
2247
2248 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2249 if (zOrderRelativeOf != nullptr) {
2250 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2251 } else {
2252 layerInfo->set_z_order_relative_of(-1);
2253 }
chaviw08f3cb22020-01-13 13:17:21 -08002254
2255 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002256
2257 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002258 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002259
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002260 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw4c34a092020-07-08 11:30:06 -07002261 InputWindowInfo info;
2262 if (useDrawing) {
[1;3C2b9fc252021-02-04 16:16:50 -08002263 info = fillInputInfo({nullptr});
chaviw4c34a092020-07-08 11:30:06 -07002264 } else {
2265 info = state.inputInfo;
2266 }
2267
2268 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002269 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002270 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002271
2272 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2273 auto protoMap = layerInfo->mutable_metadata();
2274 for (const auto& entry : state.metadata.mMap) {
2275 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2276 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002277 }
chaviw1d044282017-09-27 12:19:28 -07002278}
2279
Robert Carr2e102c92018-10-23 12:11:15 -07002280bool Layer::isRemovedFromCurrentState() const {
2281 return mRemovedFromCurrentState;
2282}
2283
chaviw39d01472021-04-08 14:26:24 -05002284ui::Transform Layer::getInputTransform() const {
2285 return getTransform();
2286}
2287
2288Rect Layer::getInputBounds() const {
2289 return getCroppedBufferSize(getDrawingState());
2290}
2291
[1;3C2b9fc252021-02-04 16:16:50 -08002292void Layer::fillInputFrameInfo(InputWindowInfo& info, const ui::Transform& toPhysicalDisplay) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002293 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002294 // If this is a portal window, set the touchableRegion to the layerBounds.
2295 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002296 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002297 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002298 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002299 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002300 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002301
chaviw7e72caf2020-12-02 16:50:43 -08002302 if (!layerBounds.isValid()) {
2303 // If the layer bounds is empty, set the frame to empty and clear the transform
2304 info.frameLeft = 0;
2305 info.frameTop = 0;
2306 info.frameRight = 0;
2307 info.frameBottom = 0;
2308 info.transform.reset();
2309 return;
2310 }
2311
chaviw39d01472021-04-08 14:26:24 -05002312 ui::Transform layerToDisplay = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002313 // Transform that takes window coordinates to unrotated display coordinates
2314 ui::Transform t = toPhysicalDisplay * layerToDisplay;
chaviw7e72caf2020-12-02 16:50:43 -08002315 int32_t xSurfaceInset = info.surfaceInset;
2316 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002317 // Bring screenBounds into unrotated space
2318 Rect screenBounds = toPhysicalDisplay.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002319
chaviw1ff3d1e2020-07-01 15:53:47 -07002320 const float xScale = t.getScaleX();
2321 const float yScale = t.getScaleY();
2322 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002323 xSurfaceInset = std::round(xSurfaceInset * xScale);
2324 ySurfaceInset = std::round(ySurfaceInset * yScale);
2325 }
2326
Peiyong Lin74861ad2020-10-14 11:55:33 -07002327 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002328 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002329
2330 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002331 xSurfaceInset = (xSurfaceInset >= 0)
2332 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2333 : 0;
2334 ySurfaceInset = (ySurfaceInset >= 0)
2335 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2336 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002337
Robert Carra7242302020-09-01 18:26:29 -07002338 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2339 // in the overflow scenario?
2340 {
2341 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002342 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2343 transformedLayerBounds.left = tmp;
2344 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2345 transformedLayerBounds.right = tmp;
2346 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2347 transformedLayerBounds.top = tmp;
2348 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2349 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002350 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002351
chaviw44a6d2b2020-09-08 17:14:16 -07002352 // Compute the correct transform to send to input. This will allow it to transform the
2353 // input coordinates from display space into window space. Therefore, it needs to use the
2354 // final layer frame to create the inverse transform. Since surface insets are added later,
2355 // along with the overflow, the best way to ensure we get the correct transform is to use
2356 // the final frame calculated.
2357 // 1. Take the original transform set on the window and get the inverse transform. This is
2358 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002359 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002360 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2361 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2362 // be the new translation values for the transform.
2363 // 3. Update the translation of the original transform to the new translation values.
2364 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2365 // window space.
2366 ui::Transform inverseTransform = t.inverse();
2367 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2368 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002369 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002370 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002371 info.transform = inputTransform.inverse();
2372
chaviw39cfa2e2020-11-04 14:19:02 -08002373 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2374 // The frame should be the area the user sees on screen since it's used for occlusion
2375 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002376 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2377 info.frameLeft = transformedLayerBounds.left;
2378 info.frameTop = transformedLayerBounds.top;
2379 info.frameRight = transformedLayerBounds.right;
2380 info.frameBottom = transformedLayerBounds.bottom;
2381
Vishnu Nair8033a492018-12-05 07:27:23 -08002382 // Position the touchable region relative to frame screen location and restrict it to frame
2383 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002384 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002385}
2386
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002387void Layer::fillTouchOcclusionMode(InputWindowInfo& info) {
2388 sp<Layer> p = this;
2389 while (p != nullptr && !p->hasInputInfo()) {
2390 p = p->mDrawingParent.promote();
2391 }
2392 if (p != nullptr) {
2393 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2394 }
2395}
2396
[1;3C2b9fc252021-02-04 16:16:50 -08002397InputWindowInfo Layer::fillInputInfo(const sp<DisplayDevice>& display) {
chaviw7e72caf2020-12-02 16:50:43 -08002398 if (!hasInputInfo()) {
2399 mDrawingState.inputInfo.name = getName();
2400 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2401 mDrawingState.inputInfo.ownerPid = mOwnerPid;
2402 mDrawingState.inputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
2403 mDrawingState.inputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
2404 mDrawingState.inputInfo.displayId = getLayerStack();
2405 }
2406
2407 InputWindowInfo info = mDrawingState.inputInfo;
2408 info.id = sequence;
2409
2410 if (info.displayId == ADISPLAY_ID_NONE) {
2411 info.displayId = getLayerStack();
2412 }
2413
[1;3C2b9fc252021-02-04 16:16:50 -08002414 // Transform that goes from "logical(rotated)" display to physical/unrotated display.
2415 // This is for when inputflinger operates in physical display-space.
2416 ui::Transform toPhysicalDisplay;
2417 if (display) {
2418 toPhysicalDisplay = display->getTransform();
Evan Rosky84f07f02021-04-16 10:42:42 -07002419 info.displayWidth = display->getWidth();
2420 info.displayHeight = display->getHeight();
[1;3C2b9fc252021-02-04 16:16:50 -08002421 }
2422 fillInputFrameInfo(info, toPhysicalDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08002423
Robert Carr5dc426e2020-06-10 14:29:14 -07002424 // For compatibility reasons we let layers which can receive input
2425 // receive input before they have actually submitted a buffer. Because
2426 // of this we use canReceiveInput instead of isVisible to check the
2427 // policy-visibility, ignoring the buffer state. However for layers with
2428 // hasInputInfo()==false we can use the real visibility state.
2429 // We are just using these layers for occlusion detection in
2430 // InputDispatcher, and obviously if they aren't visible they can't occlude
2431 // anything.
2432 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002433 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002434 fillTouchOcclusionMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002435
2436 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2437 if (info.replaceTouchableRegionWithCrop) {
2438 if (cropLayer == nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002439 info.touchableRegion = Region(toPhysicalDisplay.transform(Rect{mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002440 } else {
[1;3C2b9fc252021-02-04 16:16:50 -08002441 info.touchableRegion =
2442 Region(toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002443 }
2444 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002445 info.touchableRegion = info.touchableRegion.intersect(
2446 toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002447 }
2448
chaviwaf87b3e2019-10-01 16:59:28 -07002449 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2450 // touches from going outside the cloned area.
2451 if (isClone()) {
2452 sp<Layer> clonedRoot = getClonedRoot();
2453 if (clonedRoot != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002454 Rect rect = toPhysicalDisplay.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002455 info.touchableRegion = info.touchableRegion.intersect(rect);
2456 }
2457 }
2458
Robert Carr720e5062018-07-30 17:45:14 -07002459 return info;
2460}
2461
chaviwaf87b3e2019-10-01 16:59:28 -07002462sp<Layer> Layer::getClonedRoot() {
2463 if (mClonedChild != nullptr) {
2464 return this;
2465 }
2466 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2467 return nullptr;
2468 }
2469 return mDrawingParent.promote()->getClonedRoot();
2470}
2471
Robert Carredd13602020-04-13 17:24:34 -07002472bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002473 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002474}
2475
chaviw3e727cd2019-01-31 13:41:05 -08002476bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002477 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002478}
2479
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002480compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002481 const DisplayDevice* display) const {
2482 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002483 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002484}
2485
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002486Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2487 const auto outputLayer = findOutputLayerForDisplay(display);
2488 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002489}
2490
chaviwb4c6e582019-08-16 14:35:07 -07002491void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2492 // copy drawing state from cloned layer
2493 mDrawingState = clonedFrom->mDrawingState;
2494 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002495}
chaviw74b03172019-08-19 11:09:03 -07002496
2497void Layer::updateMirrorInfo() {
2498 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2499 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2500 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2501 // that case, we want to delete the reference to the clone since we want it to get
2502 // destroyed. The root, this layer, will still be around since the client can continue
2503 // to hold a reference, but no cloned layers will be displayed.
2504 mClonedChild = nullptr;
2505 return;
2506 }
2507
2508 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2509 // If the real layer exists and is in current state, add the clone as a child of the root.
2510 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2511 // copied to drawingState for the root layer. So the clonedChild is always removed from
2512 // drawingState and then needs to be added back each traversal.
2513 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2514 addChildToDrawing(mClonedChild);
2515 }
2516
2517 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2518 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2519 mClonedChild->updateClonedRelatives(clonedLayersMap);
2520}
2521
2522void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2523 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2524 // to the clone. If the real layer is no longer alive, continue traversing the children
2525 // since we may be able to pull out other children that are still alive.
2526 if (isClonedFromAlive()) {
2527 sp<Layer> clonedFrom = getClonedFrom();
2528 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002529 clonedLayersMap.emplace(clonedFrom, this);
2530 }
2531
2532 // The clone layer may have children in drawingState since they may have been created and
2533 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2534 // that already exist, since we can just re-use them.
2535 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2536 // not updated in the regular traversal. They are skipped since the root will lose the
2537 // reference to them when it copies its currentChildren to drawing.
2538 for (sp<Layer>& child : mDrawingChildren) {
2539 child->updateClonedDrawingState(clonedLayersMap);
2540 }
2541}
2542
2543void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2544 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2545 mDrawingChildren.clear();
2546
2547 if (!isClonedFromAlive()) {
2548 return;
2549 }
2550
2551 sp<Layer> clonedFrom = getClonedFrom();
2552 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2553 if (child == mirrorRoot) {
2554 // This is to avoid cyclical mirroring.
2555 continue;
2556 }
2557 sp<Layer> clonedChild = clonedLayersMap[child];
2558 if (clonedChild == nullptr) {
2559 clonedChild = child->createClone();
2560 clonedLayersMap[child] = clonedChild;
2561 }
2562 addChildToDrawing(clonedChild);
2563 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2564 }
2565}
2566
chaviwaf87b3e2019-10-01 16:59:28 -07002567void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2568 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2569 if (cropLayer != nullptr) {
2570 if (clonedLayersMap.count(cropLayer) == 0) {
2571 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2572 // self as crop layer to avoid going outside bounds.
2573 mDrawingState.touchableRegionCrop = this;
2574 } else {
2575 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2576 mDrawingState.touchableRegionCrop = clonedCropLayer;
2577 }
2578 }
2579 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2580 // WM or the client.
Michael Wright44753b12020-07-08 13:48:11 +01002581 mDrawingState.inputInfo.flags &= ~InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002582}
2583
2584void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002585 mDrawingState.zOrderRelativeOf = nullptr;
2586 mDrawingState.zOrderRelatives.clear();
2587
2588 if (!isClonedFromAlive()) {
2589 return;
2590 }
2591
chaviwaf87b3e2019-10-01 16:59:28 -07002592 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002593 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002594 const sp<Layer>& relative = relativeWeak.promote();
2595 if (clonedLayersMap.count(relative) > 0) {
2596 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002597 mDrawingState.zOrderRelatives.add(clonedRelative);
2598 }
2599 }
2600
2601 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2602 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2603 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2604 // still traverse the children, but the layer with the missing relativeOf will not be shown
2605 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002606 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2607 if (clonedLayersMap.count(relativeOf) > 0) {
2608 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002609 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2610 }
2611
chaviwaf87b3e2019-10-01 16:59:28 -07002612 updateClonedInputInfo(clonedLayersMap);
2613
chaviw74b03172019-08-19 11:09:03 -07002614 for (sp<Layer>& child : mDrawingChildren) {
2615 child->updateClonedRelatives(clonedLayersMap);
2616 }
2617}
2618
2619void Layer::addChildToDrawing(const sp<Layer>& layer) {
2620 mDrawingChildren.add(layer);
2621 layer->mDrawingParent = this;
2622}
2623
Steven Thomas62a4cf82020-01-31 12:04:03 -08002624Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2625 switch (compatibility) {
2626 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2627 return FrameRateCompatibility::Default;
2628 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2629 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002630 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2631 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002632 default:
2633 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2634 return FrameRateCompatibility::Default;
2635 }
2636}
2637
Marin Shalamanovc5986772021-03-16 16:09:49 +01002638scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2639 switch (strategy) {
2640 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2641 return Seamlessness::OnlySeamless;
2642 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2643 return Seamlessness::SeamedAndSeamless;
2644 default:
2645 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2646 return Seamlessness::Default;
2647 }
2648}
2649
chaviwc5676c62020-09-18 15:01:04 -07002650bool Layer::getPrimaryDisplayOnly() const {
2651 const State& s(mDrawingState);
2652 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2653 return true;
2654 }
2655
2656 sp<Layer> parent = mDrawingParent.promote();
2657 return parent == nullptr ? false : parent->getPrimaryDisplayOnly();
2658}
2659
Mathias Agopian13127d82013-03-05 17:47:11 -08002660// ---------------------------------------------------------------------------
2661
Marin Shalamanov46084422020-10-13 12:33:42 +02002662std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2663 return stream << "{rate=" << rate.rate
2664 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002665 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002666}
2667
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002668}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002669
2670#if defined(__gl_h_)
2671#error "don't include gl/gl.h in this file"
2672#endif
2673
2674#if defined(__gl2_h_)
2675#error "don't include gl2/gl2.h in this file"
2676#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002677
2678// TODO(b/129481165): remove the #pragma below and fix conversion issues
2679#pragma clang diagnostic pop // ignored "-Wconversion"