blob: 8b7dc4dae4cb888662bb715d8f8d9300727cf103 [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
576 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800577 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000578 }
579
580 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800581 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
582 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000583
Lloyd Piquef16688f2019-02-19 17:47:57 -0800584 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700585 layerSettings.sourceDataspace = getDataSpace();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100586 if (!targetSettings.disableBlurs) {
587 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
588 layerSettings.blurRegions = getBlurRegions();
Derek Sollenberger39feade2021-04-27 16:08:40 -0400589 layerSettings.blurRegionTransform =
590 getActiveTransform(getDrawingState()).inverse().asMatrix4();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100591 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700592 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800593 // Record the name of the layer for debugging further down the stack.
594 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800595 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800596}
597
Vishnu Nair9b079a22020-01-21 14:36:08 -0800598std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareShadowClientComposition(
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200599 const LayerFE::LayerSettings& casterLayerSettings, const Rect& layerStackRect,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800600 ui::Dataspace outputDataspace) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200601 renderengine::ShadowSettings shadow = getShadowSettings(layerStackRect);
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800602 if (shadow.length <= 0.f) {
603 return {};
604 }
605
606 const float casterAlpha = casterLayerSettings.alpha;
607 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
608 casterLayerSettings.source.buffer.isOpaque);
609
Vishnu Nair9b079a22020-01-21 14:36:08 -0800610 compositionengine::LayerFE::LayerSettings shadowLayer = casterLayerSettings;
611
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800612 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800613 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800614
615 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
616 // Otherwise the generated shadow will only be shown around the casting layer.
617 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
618 shadowLayer.shadow.ambientColor *= casterAlpha;
619 shadowLayer.shadow.spotColor *= casterAlpha;
620 shadowLayer.sourceDataspace = outputDataspace;
621 shadowLayer.source.buffer.buffer = nullptr;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800622 shadowLayer.source.buffer.fence = nullptr;
623 shadowLayer.frameNumber = 0;
624 shadowLayer.bufferId = 0;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800625 shadowLayer.name = getName();
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800626
627 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
628 return {};
629 }
630
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800631 return shadowLayer;
632}
633
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800634void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
635 bool blackout) const {
636 layerSettings.source.buffer.buffer = nullptr;
637 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
638 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800639 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800640 layerSettings.frameNumber = 0;
641
642 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
643 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800644 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800645}
646
647std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
648 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
649 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
650 prepareClientComposition(targetSettings);
651 // Nothing to render.
652 if (!layerSettings) {
653 return {};
654 }
655
656 // HWC requests to clear this layer.
657 if (targetSettings.clearContent) {
658 prepareClearClientComposition(*layerSettings, false /* blackout */);
659 return {*layerSettings};
660 }
661
662 std::optional<compositionengine::LayerFE::LayerSettings> shadowSettings =
663 prepareShadowClientComposition(*layerSettings, targetSettings.viewport,
664 targetSettings.dataspace);
665 // There are no shadows to render.
666 if (!shadowSettings) {
667 return {*layerSettings};
668 }
669
670 // If the layer casts a shadow but the content casting the shadow is occluded, skip
671 // composing the non-shadow content and only draw the shadows.
672 if (targetSettings.realContentIsVisible) {
673 return {*shadowSettings, *layerSettings};
674 }
675
676 return {*shadowSettings};
677}
678
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700679Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
680 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800681 if (outputLayer == nullptr) {
682 return Hwc2::IComposerClient::Composition::INVALID;
683 }
684 if (outputLayer->getState().hwc) {
685 return (*outputLayer->getState().hwc).hwcCompositionType;
686 } else {
687 return Hwc2::IComposerClient::Composition::CLIENT;
688 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689}
690
Mathias Agopian13127d82013-03-05 17:47:11 -0800691// ----------------------------------------------------------------------------
692// local state
693// ----------------------------------------------------------------------------
694
David Sodman41fdfc92017-11-06 16:09:56 -0800695bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800696 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700697 if (s.flags & layer_state_t::eLayerSecure) {
698 return true;
699 }
700
701 const auto p = mDrawingParent.promote();
702 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700703}
704
Mathias Agopian13127d82013-03-05 17:47:11 -0800705// ----------------------------------------------------------------------------
706// transaction
707// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800708
Marissa Wall61c58622018-07-18 10:12:20 -0700709uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000710 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711
Marissa Wall61c58622018-07-18 10:12:20 -0700712 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
713 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700714
David Sodmaneb085e02017-10-05 18:49:04 -0700715 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700716 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000717 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800718 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
719 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
720 " requested={ wh={%4u,%4u} }}\n"
721 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
722 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700723 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700724 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
chaviw25714502021-02-11 10:01:08 -0800725 stateToCommit->crop.left, stateToCommit->crop.top, stateToCommit->crop.right,
726 stateToCommit->crop.bottom, stateToCommit->crop.getWidth(),
727 stateToCommit->crop.getHeight(), stateToCommit->requested_legacy.w,
728 stateToCommit->requested_legacy.h, s.active_legacy.w, s.active_legacy.h,
729 s.crop.left, s.crop.top, s.crop.right, s.crop.bottom, s.crop.getWidth(),
730 s.crop.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700732
Robert Carre392b552017-09-19 12:16:05 -0700733 // Don't let Layer::doTransaction update the drawing state
734 // if we have a pending resize, unless we are in fixed-size mode.
735 // the drawing state will be updated only once we receive a buffer
736 // with the correct size.
737 //
738 // In particular, we want to make sure the clip (which is part
739 // of the geometry state) is latched together with the size but is
740 // latched immediately when no resizing is involved.
741 //
742 // If a sideband stream is attached, however, we want to skip this
743 // optimization so that transactions aren't missed when a buffer
744 // never arrives
745 //
746 // In the case that we don't have a buffer we ignore other factors
747 // and avoid entering the resizePending state. At a high level the
748 // resizePending state is to avoid applying the state of the new buffer
749 // to the old buffer. However in the state where we don't have an old buffer
750 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700751 const bool resizePending =
752 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
753 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700754 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700755 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800756 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700757 flags |= eDontUpdateGeometryState;
758 }
759 }
760
Robert Carr7bf247e2017-05-18 14:02:49 -0700761 // Here we apply various requested geometry states, depending on our
762 // latching configuration. See Layer.h for a detailed discussion of
763 // how geometry latching is controlled.
764 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000765 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700766
Robert Carr7bf247e2017-05-18 14:02:49 -0700767 // There is an awkward asymmetry in the handling of the crop states in the position
768 // states, as can be seen below. Largely this arises from position and transform
769 // being stored in the same data structure while having different latching rules.
770 // b/38182305
771 //
Marissa Wall61c58622018-07-18 10:12:20 -0700772 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700773 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700774 editCurrentState.active_legacy = editCurrentState.requested_legacy;
775 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800776 }
777
Marissa Wall61c58622018-07-18 10:12:20 -0700778 return flags;
779}
780
781uint32_t Layer::doTransaction(uint32_t flags) {
782 ATRACE_CALL();
783
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800784 if (mChildrenChanged) {
785 flags |= eVisibleRegion;
786 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700787 }
788
Robert Carr0758e5d2021-03-11 22:15:04 -0800789 // TODO: This is unfortunate.
790 mCurrentStateModified = mCurrentState.modified;
791 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700792
Robert Carr0758e5d2021-03-11 22:15:04 -0800793 flags = doTransactionResize(flags, &mCurrentState);
Marissa Wall61c58622018-07-18 10:12:20 -0700794
Alec Mourib416efd2018-09-06 21:01:59 +0000795 const State& s(getDrawingState());
Robert Carr0758e5d2021-03-11 22:15:04 -0800796 State& c(getCurrentState());
Marissa Wall61c58622018-07-18 10:12:20 -0700797
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700798 // Translates dest frame into scale and position updates. This helps align geometry calculations
799 // for BufferStateLayer with other layers. This should ideally happen in the client once client
800 // has the display orientation details from WM.
801 updateGeometry();
802
chaviw4e765532021-04-30 12:11:39 -0500803 if (c.width != s.width || c.height != s.height || !(c.transform == s.transform)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800804 // invalidate and recompute the visible regions if needed
805 flags |= Layer::eVisibleRegion;
806 }
807
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700808 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800809 // invalidate and recompute the visible regions if needed
810 flags |= eVisibleRegion;
811 this->contentDirty = true;
812
813 // we may use linear filtering, if the matrix scales us
Marin Shalamanov043ba292020-09-10 13:35:20 +0200814 mNeedsFiltering = getActiveTransform(c).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800815 }
816
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800817 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700818 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800819 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700820 }
821
chaviw47dbd692020-10-26 17:50:12 -0700822 // Add the callbacks from the drawing state into the current state. This is so when the current
823 // state gets copied to drawing, we don't lose the callback handles that are still in drawing.
824 for (auto& handle : s.callbackHandles) {
825 c.callbackHandles.push_back(handle);
826 }
827
Vishnu Nair1506b182021-02-22 14:35:15 -0800828 // Allow BufferStateLayer to release any unlatched buffers in drawing state.
Alec Mouria90a5702021-04-16 16:36:21 +0000829 bufferMayChange(c.buffer->getBuffer());
Vishnu Nair1506b182021-02-22 14:35:15 -0800830
Mathias Agopian13127d82013-03-05 17:47:11 -0800831 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700832 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800833 mCurrentState.callbackHandles = {};
Robert Carra1257842020-01-31 13:48:28 -0800834
Mathias Agopian13127d82013-03-05 17:47:11 -0800835 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836}
837
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000838void Layer::commitTransaction(State& stateToCommit) {
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000839 if (auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
Alec Mouria90a5702021-04-16 16:36:21 +0000840 ((mDrawingState.buffer && stateToCommit.buffer &&
841 mDrawingState.buffer->getBuffer() != stateToCommit.buffer->getBuffer()) ||
842 (mDrawingState.buffer && !stateToCommit.buffer) ||
843 (!mDrawingState.buffer && stateToCommit.buffer)) &&
844 bufferSurfaceFrame != nullptr &&
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000845 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
846 // If the previous buffer was committed but not latched (refreshPending - happens during
847 // back to back invalidates), it gets silently dropped here. Mark the corresponding
848 // SurfaceFrame as dropped to prevent it from getting stuck in the pending classification
849 // list.
850 addSurfaceFrameDroppedForBuffer(bufferSurfaceFrame);
851 }
Ady Abraham59fd8ff2021-04-15 20:13:30 -0700852 const bool frameRateVoteChanged =
853 mDrawingState.frameRateForLayerTree != stateToCommit.frameRateForLayerTree;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800854 mDrawingState = stateToCommit;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000855
Ady Abraham59fd8ff2021-04-15 20:13:30 -0700856 if (frameRateVoteChanged) {
857 mFlinger->mScheduler->recordLayerHistory(this, systemTime(),
858 LayerHistory::LayerUpdateType::SetFrameRate);
859 }
860
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000861 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
862 // bufferSurfaceFrameTX will be presented in latchBuffer.
863 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
864 if (surfaceFrame->getPresentState() != PresentState::Presented) {
865 // With applyPendingStates, we could end up having presented surfaceframes from previous
866 // states
867 surfaceFrame->setPresentState(PresentState::Presented);
868 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
869 }
870 }
871 // Clear the surfaceFrames from the old state now that it has been copied into DrawingState.
872 stateToCommit.bufferSurfaceFrameTX.reset();
873 stateToCommit.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700874}
875
Mathias Agopian13127d82013-03-05 17:47:11 -0800876uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800878}
879
880uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800882}
883
chaviw214c89d2019-09-04 16:03:53 -0700884bool Layer::setPosition(float x, float y) {
chaviw4e765532021-04-30 12:11:39 -0500885 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800886 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700887
888 // We update the requested and active position simultaneously because
889 // we want to apply the position portion of the transform matrix immediately,
890 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
chaviw4e765532021-04-30 12:11:39 -0500891 mCurrentState.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700892 // Here we directly update the active state
893 // unlike other setters, because we store it within
894 // the transform, but use different latching rules.
895 // b/38182305
chaviw4e765532021-04-30 12:11:39 -0500896 mCurrentState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700897
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 setTransactionFlags(eTransactionNeeded);
900 return true;
901}
Robert Carr82364e32016-05-15 11:27:47 -0700902
Robert Carr1f0a16a2016-10-24 16:27:39 -0700903bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
904 ssize_t idx = mCurrentChildren.indexOf(childLayer);
905 if (idx < 0) {
906 return false;
907 }
908 if (childLayer->setLayer(z)) {
909 mCurrentChildren.removeAt(idx);
910 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800911 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700912 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800913 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700914}
915
Robert Carr503c7042017-09-27 15:06:08 -0700916bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
917 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
918 ssize_t idx = mCurrentChildren.indexOf(childLayer);
919 if (idx < 0) {
920 return false;
921 }
922 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
923 mCurrentChildren.removeAt(idx);
924 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800925 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700926 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800927 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700928}
929
Robert Carrae060832016-11-28 10:51:00 -0800930bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
932 mCurrentState.sequence++;
933 mCurrentState.z = z;
934 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700935
936 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800937 if (mCurrentState.zOrderRelativeOf != nullptr) {
938 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700939 if (strongRelative != nullptr) {
940 strongRelative->removeZOrderRelative(this);
941 }
chaviw606e5cf2019-03-01 10:12:10 -0800942 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700943 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800944 setTransactionFlags(eTransactionNeeded);
945 return true;
946}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700947
Robert Carrdb66e622017-04-10 16:55:57 -0700948void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800949 mCurrentState.zOrderRelatives.remove(relative);
950 mCurrentState.sequence++;
951 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700952 setTransactionFlags(eTransactionNeeded);
953}
954
955void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800956 mCurrentState.zOrderRelatives.add(relative);
957 mCurrentState.modified = true;
958 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700959 setTransactionFlags(eTransactionNeeded);
960}
961
chaviw606e5cf2019-03-01 10:12:10 -0800962void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
963 mCurrentState.zOrderRelativeOf = relativeOf;
964 mCurrentState.sequence++;
965 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700966 mCurrentState.isRelativeOf = relativeOf != nullptr;
967
chaviw606e5cf2019-03-01 10:12:10 -0800968 setTransactionFlags(eTransactionNeeded);
969}
970
Robert Carr503d2bd2017-12-04 15:49:47 -0800971bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700972 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
973 if (handle == nullptr) {
974 return false;
975 }
976 sp<Layer> relative = handle->owner.promote();
977 if (relative == nullptr) {
978 return false;
979 }
980
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800981 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
982 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800983 return false;
984 }
985
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800986 mCurrentState.sequence++;
987 mCurrentState.modified = true;
988 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700989
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800990 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700991 if (oldZOrderRelativeOf != nullptr) {
992 oldZOrderRelativeOf->removeZOrderRelative(this);
993 }
chaviw606e5cf2019-03-01 10:12:10 -0800994 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700995 relative->addZOrderRelative(this);
996
997 setTransactionFlags(eTransactionNeeded);
998
999 return true;
1000}
1001
Mathias Agopian13127d82013-03-05 17:47:11 -08001002bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001003 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001004 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001005 mCurrentState.requested_legacy.w = w;
1006 mCurrentState.requested_legacy.h = h;
1007 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001008 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001009
1010 // record the new size, from this point on, when the client request
1011 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001012 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001013 return true;
1014}
chaviw4e765532021-04-30 12:11:39 -05001015
Dan Stoza9e56aa02015-11-02 13:00:03 -08001016bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001017 if (mCurrentState.color.a == alpha) return false;
1018 mCurrentState.sequence++;
1019 mCurrentState.color.a = alpha;
1020 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001021 setTransactionFlags(eTransactionNeeded);
1022 return true;
1023}
chaviw13fdc492017-06-27 12:40:18 -07001024
Valerie Haudd0b7572019-01-29 14:59:27 -08001025bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1026 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001027 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001028 }
1029 mCurrentState.sequence++;
1030 mCurrentState.modified = true;
1031 setTransactionFlags(eTransactionNeeded);
1032
1033 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001034 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -08001035 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001036 std::string name = mName + "BackgroundColorLayer";
Vishnu Nairfa247b12020-02-11 08:58:26 -08001037 mCurrentState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001038 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1039 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001040
Valerie Haudd0b7572019-01-29 14:59:27 -08001041 // add to child list
1042 addChild(mCurrentState.bgColorLayer);
1043 mFlinger->mLayersAdded = true;
1044 // set up SF to handle added color layer
1045 if (isRemovedFromCurrentState()) {
1046 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1047 }
1048 mFlinger->setTransactionFlags(eTransactionNeeded);
1049 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1050 mCurrentState.bgColorLayer->reparent(nullptr);
1051 mCurrentState.bgColorLayer = nullptr;
1052 return true;
1053 }
1054
1055 mCurrentState.bgColorLayer->setColor(color);
1056 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1057 mCurrentState.bgColorLayer->setAlpha(alpha);
1058 mCurrentState.bgColorLayer->setDataspace(dataspace);
1059
chaviw13fdc492017-06-27 12:40:18 -07001060 return true;
1061}
1062
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001063bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001064 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001065
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001066 mCurrentState.sequence++;
1067 mCurrentState.cornerRadius = cornerRadius;
1068 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001069 setTransactionFlags(eTransactionNeeded);
1070 return true;
1071}
1072
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001073bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1074 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1075
1076 mCurrentState.sequence++;
1077 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1078 mCurrentState.modified = true;
1079 setTransactionFlags(eTransactionNeeded);
1080 return true;
1081}
1082
Robert Carrd4ae7f32018-06-07 16:10:57 -07001083bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1084 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001085 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001086 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1087
1088 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -08001089 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
1090 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -07001091 return false;
1092 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 mCurrentState.sequence++;
chaviw4e765532021-04-30 12:11:39 -05001094 mCurrentState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001095 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 setTransactionFlags(eTransactionNeeded);
1097 return true;
1098}
Marissa Wall61c58622018-07-18 10:12:20 -07001099
Mathias Agopian13127d82013-03-05 17:47:11 -08001100bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001101 mCurrentState.requestedTransparentRegion_legacy = transparent;
1102 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001103 setTransactionFlags(eTransactionNeeded);
1104 return true;
1105}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001106
Lucas Dupinc3800b82020-10-02 16:24:48 -07001107bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
1108 mCurrentState.sequence++;
1109 mCurrentState.blurRegions = blurRegions;
1110 mCurrentState.modified = true;
1111 setTransactionFlags(eTransactionNeeded);
1112 return true;
1113}
1114
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001115bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1117 if (mCurrentState.flags == newFlags) return false;
1118 mCurrentState.sequence++;
1119 mCurrentState.flags = newFlags;
1120 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001121 setTransactionFlags(eTransactionNeeded);
1122 return true;
1123}
Robert Carr99e27f02016-06-16 15:18:02 -07001124
chaviw25714502021-02-11 10:01:08 -08001125bool Layer::setCrop(const Rect& crop) {
1126 if (mCurrentState.requestedCrop == crop) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001127 mCurrentState.sequence++;
chaviw25714502021-02-11 10:01:08 -08001128 mCurrentState.requestedCrop = crop;
1129 mCurrentState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001130
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001131 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001132 setTransactionFlags(eTransactionNeeded);
1133 return true;
1134}
Robert Carr8d5227b2017-03-16 15:41:03 -07001135
Evan Roskyef876c92019-01-25 17:46:06 -08001136bool Layer::setMetadata(const LayerMetadata& data) {
1137 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001138 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001139 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001140 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001141}
1142
Mathias Agopian13127d82013-03-05 17:47:11 -08001143bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001144 if (mCurrentState.layerStack == layerStack) return false;
1145 mCurrentState.sequence++;
1146 mCurrentState.layerStack = layerStack;
1147 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001148 setTransactionFlags(eTransactionNeeded);
1149 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001150}
1151
Peiyong Linc502cb72019-03-01 15:00:23 -08001152bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1153 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1154 return false;
1155 }
1156 mCurrentState.sequence++;
1157 mCurrentState.colorSpaceAgnostic = agnostic;
1158 mCurrentState.modified = true;
1159 setTransactionFlags(eTransactionNeeded);
1160 return true;
1161}
1162
Ana Krulecc84d09b2019-11-02 23:10:29 +01001163bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1164 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1165 mCurrentState.frameRateSelectionPriority = priority;
1166 mCurrentState.sequence++;
1167 mCurrentState.modified = true;
1168 setTransactionFlags(eTransactionNeeded);
1169 return true;
1170}
1171
1172int32_t Layer::getFrameRateSelectionPriority() const {
1173 // Check if layer has priority set.
1174 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1175 return mDrawingState.frameRateSelectionPriority;
1176 }
1177 // If not, search whether its parents have it set.
1178 sp<Layer> parent = getParent();
1179 if (parent != nullptr) {
1180 return parent->getFrameRateSelectionPriority();
1181 }
1182
1183 return Layer::PRIORITY_UNSET;
1184}
1185
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001186bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1187 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1188};
1189
Robert Carr1f0a16a2016-10-24 16:27:39 -07001190uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001191 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001192 if (p == nullptr) {
1193 return getDrawingState().layerStack;
1194 }
1195 return p->getLayerStack();
1196}
1197
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001198bool Layer::setShadowRadius(float shadowRadius) {
1199 if (mCurrentState.shadowRadius == shadowRadius) {
1200 return false;
1201 }
1202
1203 mCurrentState.sequence++;
1204 mCurrentState.shadowRadius = shadowRadius;
1205 mCurrentState.modified = true;
1206 setTransactionFlags(eTransactionNeeded);
1207 return true;
1208}
1209
Vishnu Nair6213bd92020-05-08 17:42:25 -07001210bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
1211 if (mCurrentState.fixedTransformHint == fixedTransformHint) {
1212 return false;
1213 }
1214
1215 mCurrentState.sequence++;
1216 mCurrentState.fixedTransformHint = fixedTransformHint;
1217 mCurrentState.modified = true;
1218 setTransactionFlags(eTransactionNeeded);
1219 return true;
1220}
1221
John Reckcdb4ed72021-02-04 13:39:33 -05001222bool Layer::setStretchEffect(const StretchEffect& effect) {
1223 StretchEffect temp = effect;
1224 temp.sanitize();
1225 if (mCurrentState.stretchEffect == temp) {
1226 return false;
1227 }
1228 mCurrentState.sequence++;
1229 mCurrentState.stretchEffect = temp;
1230 mCurrentState.modified = true;
1231 setTransactionFlags(eTransactionNeeded);
1232 return true;
1233}
1234
John Reckc00c6692021-02-16 11:37:33 -05001235StretchEffect Layer::getStretchEffect() const {
1236 if (mDrawingState.stretchEffect.hasEffect()) {
1237 return mDrawingState.stretchEffect;
1238 }
1239
1240 sp<Layer> parent = getParent();
1241 if (parent != nullptr) {
1242 auto effect = parent->getStretchEffect();
1243 if (effect.hasEffect()) {
1244 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1245 return effect;
1246 }
1247 }
1248 return StretchEffect{};
1249}
1250
Ady Abraham60e42ea2020-03-09 19:17:31 -07001251void Layer::updateTreeHasFrameRateVote() {
1252 const auto traverseTree = [&](const LayerVector::Visitor& visitor) {
1253 auto parent = getParent();
1254 while (parent) {
1255 visitor(parent.get());
1256 parent = parent->getParent();
1257 }
1258
1259 traverse(LayerVector::StateSet::Current, visitor);
1260 };
1261
1262 // update parents and children about the vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001263 // First traverse the tree and count how many layers has votes.
Ady Abraham60e42ea2020-03-09 19:17:31 -07001264 int layersWithVote = 0;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001265 traverseTree([&layersWithVote](Layer* layer) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001266 const auto layerVotedWithDefaultCompatibility =
1267 layer->mCurrentState.frameRate.rate.isValid() &&
Ady Abraham414e8b52020-05-21 15:36:04 -07001268 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Default;
1269 const auto layerVotedWithNoVote =
1270 layer->mCurrentState.frameRate.type == FrameRateCompatibility::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001271 const auto layerVotedWithExactCompatibility =
1272 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Exact;
Ady Abraham414e8b52020-05-21 15:36:04 -07001273
1274 // We do not count layers that are ExactOrMultiple for the same reason
1275 // we are allowing touch boost for those layers. See
1276 // RefreshRateConfigs::getBestRefreshRate for more details.
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001277 if (layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1278 layerVotedWithExactCompatibility) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001279 layersWithVote++;
1280 }
1281 });
1282
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001283 // Now we can update the tree frame rate vote for each layer in the tree
1284 const bool treeHasFrameRateVote = layersWithVote > 0;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001285 bool transactionNeeded = false;
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001286 traverseTree([treeHasFrameRateVote, &transactionNeeded](Layer* layer) {
1287 transactionNeeded = layer->updateFrameRateForLayerTree(treeHasFrameRateVote);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001288 });
1289
1290 if (transactionNeeded) {
1291 mFlinger->setTransactionFlags(eTraversalNeeded);
1292 }
1293}
1294
Ady Abraham71c437d2020-01-31 15:56:57 -08001295bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001296 if (!mFlinger->useFrameRateApi) {
1297 return false;
1298 }
Steven Thomas3172e202020-01-06 19:25:30 -08001299 if (mCurrentState.frameRate == frameRate) {
1300 return false;
1301 }
1302
1303 mCurrentState.sequence++;
1304 mCurrentState.frameRate = frameRate;
1305 mCurrentState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001306
1307 updateTreeHasFrameRateVote();
1308
Steven Thomas3172e202020-01-06 19:25:30 -08001309 setTransactionFlags(eTransactionNeeded);
1310 return true;
1311}
1312
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001313void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1314 nsecs_t postTime) {
1315 mCurrentState.postTime = postTime;
1316
1317 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1318 // there are two transactions with the same token, the first one without a buffer and the
1319 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1320 // in that case.
1321 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1322 if (it != mCurrentState.bufferlessSurfaceFramesTX.end()) {
1323 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1324 mCurrentState.bufferSurfaceFrameTX = it->second;
1325 mCurrentState.bufferlessSurfaceFramesTX.erase(it);
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001326 mCurrentState.bufferSurfaceFrameTX->promoteToBuffer();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001327 mCurrentState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
1328 } else {
1329 mCurrentState.bufferSurfaceFrameTX =
1330 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1331 }
1332}
1333
1334void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1335 nsecs_t postTime) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001336 mCurrentState.frameTimelineInfo = info;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001337 mCurrentState.postTime = postTime;
1338 mCurrentState.modified = true;
1339 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001340
1341 if (const auto& bufferSurfaceFrameTX = mCurrentState.bufferSurfaceFrameTX;
1342 bufferSurfaceFrameTX != nullptr) {
1343 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1344 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1345 // being used for BufferSurfaceFrame, don't create a new one.
1346 return;
1347 }
1348 }
1349 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1350 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1351 // targeting different vsyncs).
1352 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1353 if (it == mCurrentState.bufferlessSurfaceFramesTX.end()) {
1354 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
1355 mCurrentState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
1356 } else {
1357 if (it->second->getPresentState() == PresentState::Presented) {
1358 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1359 // have been from previous vsync.
1360 it->second = createSurfaceFrameForTransaction(info, postTime);
1361 }
1362 }
1363}
1364
1365void Layer::addSurfaceFrameDroppedForBuffer(
1366 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001367 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001368 surfaceFrame->setPresentState(PresentState::Dropped);
1369 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1370}
1371
1372void Layer::addSurfaceFramePresentedForBuffer(
1373 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1374 nsecs_t currentLatchTime) {
1375 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1376 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1377 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1378 mLastLatchTime = currentLatchTime;
1379}
1380
1381std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1382 const FrameTimelineInfo& info, nsecs_t postTime) {
1383 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001384 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1385 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001386 mTransactionName,
1387 /*isBuffer*/ false);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001388 // For Transactions, the post time is considered to be both queue and acquire fence time.
1389 surfaceFrame->setActualQueueTime(postTime);
1390 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001391 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1392 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001393 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001394 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001395 onSurfaceFrameCreated(surfaceFrame);
1396 return surfaceFrame;
1397}
1398
1399std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1400 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1401 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001402 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001403 getSequence(), mName, debugName,
1404 /*isBuffer*/ true);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001405 // For buffers, acquire fence time will set during latch.
1406 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001407 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1408 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001409 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001410 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001411 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1412 onSurfaceFrameCreated(surfaceFrame);
1413 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001414}
1415
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001416bool Layer::updateFrameRateForLayerTree(bool treeHasFrameRateVote) {
1417 const auto updateCurrentState = [&](FrameRate frameRate) {
1418 if (mCurrentState.frameRateForLayerTree == frameRate) {
1419 return false;
1420 }
1421 mCurrentState.frameRateForLayerTree = frameRate;
1422 mCurrentState.sequence++;
1423 mCurrentState.modified = true;
1424 setTransactionFlags(eTransactionNeeded);
1425 return true;
1426 };
1427
1428 const auto frameRate = mCurrentState.frameRate;
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001429 if (frameRate.rate.isValid() || frameRate.type == FrameRateCompatibility::NoVote) {
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001430 return updateCurrentState(frameRate);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001431 }
1432
Ady Abrahamf467f892020-07-31 16:01:53 -07001433 // This layer doesn't have a frame rate. Check if its ancestors have a vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001434 for (sp<Layer> parent = getParent(); parent; parent = parent->getParent()) {
1435 if (parent->mCurrentState.frameRate.rate.isValid()) {
1436 return updateCurrentState(parent->mCurrentState.frameRate);
Ady Abrahamf467f892020-07-31 16:01:53 -07001437 }
1438 }
1439
1440 // This layer and its ancestors don't have a frame rate. If one of successors
1441 // has a vote, return a NoVote for successors to set the vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001442 if (treeHasFrameRateVote) {
1443 return updateCurrentState(FrameRate(Fps(0.0f), FrameRateCompatibility::NoVote));
Ady Abraham60e42ea2020-03-09 19:17:31 -07001444 }
1445
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001446 return updateCurrentState(frameRate);
Steven Thomas3172e202020-01-06 19:25:30 -08001447}
1448
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001449Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1450 return getDrawingState().frameRateForLayerTree;
1451}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452
Robert Carr1f0a16a2016-10-24 16:27:39 -07001453bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001454 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001455 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001456 if (parent != nullptr && parent->isHiddenByPolicy()) {
1457 return true;
1458 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001459 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1460 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1461 if (zOrderRelativeOf != nullptr) {
1462 if (zOrderRelativeOf->isHiddenByPolicy()) {
1463 return true;
1464 }
1465 }
1466 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001467 return s.flags & layer_state_t::eLayerHidden;
1468}
1469
David Sodman41fdfc92017-11-06 16:09:56 -08001470uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001471 // TODO: should we do something special if mSecure is set?
1472 if (mProtectedByApp) {
1473 // need a hardware-protected path to external video sink
1474 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001475 }
Riley Andrews03414a12014-07-01 14:22:59 -07001476 if (mPotentialCursor) {
1477 usage |= GraphicBuffer::USAGE_CURSOR;
1478 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001479 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001480 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001481}
1482
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001483void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1484 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1485 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001486 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001487
Vishnu Nair6213bd92020-05-08 17:42:25 -07001488 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001489}
1490
Mathias Agopian13127d82013-03-05 17:47:11 -08001491// ----------------------------------------------------------------------------
1492// debugging
1493// ----------------------------------------------------------------------------
1494
Marissa Wall61c58622018-07-18 10:12:20 -07001495// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001496LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001497 using namespace std::string_literals;
1498
Kalle Raitaa099a242017-01-11 11:17:29 -08001499 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001500 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001501 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001502 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001503 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001504 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001505 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001506
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001507 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001508 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001509 info.mLayerStack = getLayerStack();
chaviw4e765532021-04-30 12:11:39 -05001510 info.mX = ds.transform.tx();
1511 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001512 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001513 info.mWidth = ds.width;
1514 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001515 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001516 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001517 info.mFlags = ds.flags;
1518 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001519 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001520 info.mMatrix[0][0] = ds.transform[0][0];
1521 info.mMatrix[0][1] = ds.transform[0][1];
1522 info.mMatrix[1][0] = ds.transform[1][0];
1523 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001524 {
chaviwd62d3062019-09-04 14:48:02 -07001525 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001526 if (buffer != 0) {
1527 info.mActiveBufferWidth = buffer->getWidth();
1528 info.mActiveBufferHeight = buffer->getHeight();
1529 info.mActiveBufferStride = buffer->getStride();
1530 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001531 } else {
1532 info.mActiveBufferWidth = 0;
1533 info.mActiveBufferHeight = 0;
1534 info.mActiveBufferStride = 0;
1535 info.mActiveBufferFormat = 0;
1536 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001537 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001538 info.mNumQueuedFrames = getQueuedFrameCount();
1539 info.mRefreshPending = isBufferLatched();
1540 info.mIsOpaque = isOpaque(ds);
1541 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001542 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001543 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001544}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001545
Yiwei Zhang5434a782018-12-05 18:06:32 -08001546void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001547 result.append(kDumpTableRowLength, '-');
1548 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001549 result.append(" Layer name\n");
1550 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001551 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001552 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001553 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001554 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001555 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001556 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1557 result.append(kDumpTableRowLength, '-');
1558 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001559}
1560
1561std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1562 switch (compatibility) {
1563 case FrameRateCompatibility::Default:
1564 return "Default";
1565 case FrameRateCompatibility::ExactOrMultiple:
1566 return "ExactOrMultiple";
1567 case FrameRateCompatibility::NoVote:
1568 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001569 case FrameRateCompatibility::Exact:
1570 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001571 }
Dan Stozae22aec72016-08-01 13:20:59 -07001572}
1573
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001574void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1575 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001576 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001577 return;
1578 }
1579
Yiwei Zhang5434a782018-12-05 18:06:32 -08001580 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001581 if (mName.length() > 77) {
1582 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001583 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001584 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001585 shortened.append(mName, mName.length() - 36);
1586 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001587 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001588 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001589 }
1590
Yiwei Zhang5434a782018-12-05 18:06:32 -08001591 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001592
Alec Mourib416efd2018-09-06 21:01:59 +00001593 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001594 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001595
Chia-I Wu1e043612018-03-01 09:45:09 -08001596 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001597 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001598 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001599 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001600 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001601 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001602 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001603 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1604 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001605 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001606 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001607 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001608 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001609 const auto frameRate = getFrameRateForLayerTree();
1610 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1611 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1612 frameRateCompatibilityString(frameRate.type).c_str(),
1613 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001614 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001615 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001616 }
Dan Stozae22aec72016-08-01 13:20:59 -07001617
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001618 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1619 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1620
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001621 result.append(kDumpTableRowLength, '-');
1622 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001623}
Dan Stozae22aec72016-08-01 13:20:59 -07001624
Yiwei Zhang5434a782018-12-05 18:06:32 -08001625void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001626 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001627}
1628
Svetoslavd85084b2014-03-20 10:28:31 -07001629void Layer::clearFrameStats() {
1630 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001631}
1632
Jamie Gennis6547ff42013-07-16 20:12:42 -07001633void Layer::logFrameStats() {
1634 mFrameTracker.logAndResetStats(mName);
1635}
1636
Svetoslavd85084b2014-03-20 10:28:31 -07001637void Layer::getFrameStats(FrameStats* outStats) const {
1638 mFrameTracker.getStats(outStats);
1639}
1640
Yiwei Zhang5434a782018-12-05 18:06:32 -08001641void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001642 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001643 Mutex::Autolock lock(mFrameEventHistoryMutex);
1644 mFrameEventHistory.checkFencesForCompletion();
1645 mFrameEventHistory.dump(result);
1646}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001647
Vishnu Nair0f085c62019-08-30 08:49:12 -07001648void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001649 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1650 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001651}
1652
Brian Anderson5ea5e592016-12-01 16:54:33 -08001653void Layer::onDisconnect() {
1654 Mutex::Autolock lock(mFrameEventHistoryMutex);
1655 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001656 const int32_t layerId = getSequence();
1657 mFlinger->mTimeStats->onDestroy(layerId);
1658 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001659}
1660
Brian Anderson3890c392016-07-25 12:48:08 -07001661void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001662 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001663 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001664 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Alec Mouri9a29e672020-09-14 12:39:14 -07001665 getName().c_str(), mOwnerUid, newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001666 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1667 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001668 }
1669
Brian Andersond6927fb2016-07-23 23:37:30 -07001670 Mutex::Autolock lock(mFrameEventHistoryMutex);
1671 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001672 // If there are any unsignaled fences in the aquire timeline at this
1673 // point, the previously queued frame hasn't been latched yet. Go ahead
1674 // and try to get the signal time here so the syscall is taken out of
1675 // the main thread's critical path.
1676 mAcquireTimeline.updateSignalTimes();
1677 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001678 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001679 mFrameEventHistory.addQueue(*newTimestamps);
1680 }
1681
Brian Anderson3890c392016-07-25 12:48:08 -07001682 if (outDelta) {
1683 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001684 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001685}
Dan Stozae77c7662016-05-13 11:37:28 -07001686
Chia-I Wu98f1c102017-05-30 14:54:08 -07001687size_t Layer::getChildrenCount() const {
1688 size_t count = 0;
1689 for (const sp<Layer>& child : mCurrentChildren) {
1690 count += 1 + child->getChildrenCount();
1691 }
1692 return count;
1693}
1694
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001696 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001697 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001698
Robert Carr1f0a16a2016-10-24 16:27:39 -07001699 mCurrentChildren.add(layer);
1700 layer->setParent(this);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001701 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001702}
1703
1704ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001705 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001706 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001707
Robert Carr1323c952019-01-28 18:13:27 -08001708 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001709 const auto removeResult = mCurrentChildren.remove(layer);
1710
1711 updateTreeHasFrameRateVote();
1712 layer->updateTreeHasFrameRateVote();
1713
1714 return removeResult;
1715}
1716
Robert Carr15eae092018-03-23 13:43:53 -07001717void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001718 for (const sp<Layer>& child : mDrawingChildren) {
1719 child->mDrawingParent = newParent;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001720 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001721 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001722 }
1723}
1724
chaviwf1961f72017-09-18 16:41:07 -07001725bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001726 sp<Layer> newParent;
1727 if (newParentHandle != nullptr) {
1728 auto handle = static_cast<Handle*>(newParentHandle.get());
1729 newParent = handle->owner.promote();
1730 if (newParent == nullptr) {
1731 ALOGE("Unable to promote Layer handle");
1732 return false;
1733 }
1734 if (newParent == this) {
1735 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1736 return false;
1737 }
1738 }
1739
chaviwf1961f72017-09-18 16:41:07 -07001740 sp<Layer> parent = getParent();
1741 if (parent != nullptr) {
1742 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001743 }
1744
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001745 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001746 newParent->addChild(this);
1747 if (!newParent->isRemovedFromCurrentState()) {
1748 addToCurrentState();
1749 } else {
1750 onRemovedFromCurrentState();
1751 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001752 } else {
1753 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001754 }
1755
chaviw06178942017-07-27 10:25:59 -07001756 return true;
1757}
1758
Peiyong Lind3788632018-09-18 16:01:31 -07001759bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001760 static const mat4 identityMatrix = mat4();
1761
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001762 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001763 return false;
1764 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001765 ++mCurrentState.sequence;
1766 mCurrentState.colorTransform = matrix;
1767 mCurrentState.hasColorTransform = matrix != identityMatrix;
1768 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001769 setTransactionFlags(eTransactionNeeded);
1770 return true;
1771}
1772
chaviwf66724d2018-11-28 16:35:21 -08001773mat4 Layer::getColorTransform() const {
1774 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1775 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1776 colorTransform = parent->getColorTransform() * colorTransform;
1777 }
1778 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001779}
1780
1781bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001782 bool hasColorTransform = getDrawingState().hasColorTransform;
1783 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1784 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1785 }
1786 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001787}
1788
Chia-I Wu11481472018-05-04 10:43:19 -07001789bool Layer::isLegacyDataSpace() const {
1790 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001791 return !(getDataSpace() &
1792 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1793 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001794}
1795
Robert Carr1f0a16a2016-10-24 16:27:39 -07001796void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001797 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001798}
1799
chaviw301b1d82019-11-06 13:15:09 -08001800int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1801 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1802 const State& state = useDrawing ? mDrawingState : mCurrentState;
1803 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001804}
1805
Robert Carr1c5481e2019-07-01 14:42:27 -07001806bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001807 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001808 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001809 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001810}
1811
David Sodman41fdfc92017-11-06 16:09:56 -08001812__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001813 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001814 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1815 "makeTraversalList received invalid stateSet");
1816 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1817 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001818 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001819
Robert Carr29abff82017-12-04 13:51:20 -08001820 if (state.zOrderRelatives.size() == 0) {
1821 *outSkipRelativeZUsers = true;
1822 return children;
1823 }
1824
chaviwfd462612018-05-31 16:11:27 -07001825 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001826 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001827 sp<Layer> strongRelative = weakRelative.promote();
1828 if (strongRelative != nullptr) {
1829 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001830 }
1831 }
1832
Dan Stoza412903f2017-04-27 13:42:17 -07001833 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001834 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001835 continue;
1836 }
Robert Carrdb66e622017-04-10 16:55:57 -07001837 traverse.add(child);
1838 }
1839
1840 return traverse;
1841}
1842
Robert Carr1f0a16a2016-10-24 16:27:39 -07001843/**
Robert Carrdb66e622017-04-10 16:55:57 -07001844 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001845 */
Dan Stoza412903f2017-04-27 13:42:17 -07001846void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001847 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1848 // produce a new list for traversing, including our relatives, and not including our children
1849 // who are relatives of another surface. In the case that there are no relative Z,
1850 // makeTraversalList returns our children directly to avoid significant overhead.
1851 // However in this case we need to take the responsibility for filtering children which
1852 // are relatives of another surface here.
1853 bool skipRelativeZUsers = false;
1854 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001855
Robert Carr1f0a16a2016-10-24 16:27:39 -07001856 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001857 for (; i < list.size(); i++) {
1858 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001859 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1860 continue;
1861 }
1862
chaviw301b1d82019-11-06 13:15:09 -08001863 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001864 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001865 }
Dan Stoza412903f2017-04-27 13:42:17 -07001866 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867 }
Robert Carr29abff82017-12-04 13:51:20 -08001868
Dan Stoza412903f2017-04-27 13:42:17 -07001869 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001870 for (; i < list.size(); i++) {
1871 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001872
Robert Carr29abff82017-12-04 13:51:20 -08001873 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1874 continue;
1875 }
Dan Stoza412903f2017-04-27 13:42:17 -07001876 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001877 }
1878}
1879
1880/**
Robert Carrdb66e622017-04-10 16:55:57 -07001881 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001882 */
Dan Stoza412903f2017-04-27 13:42:17 -07001883void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1884 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001885 // See traverseInZOrder for documentation.
1886 bool skipRelativeZUsers = false;
1887 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001888
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001890 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001891 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001892
1893 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1894 continue;
1895 }
1896
chaviw301b1d82019-11-06 13:15:09 -08001897 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001898 break;
1899 }
Dan Stoza412903f2017-04-27 13:42:17 -07001900 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001901 }
Dan Stoza412903f2017-04-27 13:42:17 -07001902 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001903 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001904 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001905
1906 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1907 continue;
1908 }
1909
Dan Stoza412903f2017-04-27 13:42:17 -07001910 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001911 }
1912}
1913
Edgar Arriaga844fa672020-01-16 14:21:42 -08001914void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1915 visitor(this);
1916 const LayerVector& children =
1917 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1918 for (const sp<Layer>& child : children) {
1919 child->traverse(state, visitor);
1920 }
1921}
1922
chaviw4b129c22018-04-09 16:19:43 -07001923LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1924 const std::vector<Layer*>& layersInTree) {
1925 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1926 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001927 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1928 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001929 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001930
chaviwfd462612018-05-31 16:11:27 -07001931 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001932 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1933 sp<Layer> strongRelative = weakRelative.promote();
1934 // Only add relative layers that are also descendents of the top most parent of the tree.
1935 // If a relative layer is not a descendent, then it should be ignored.
1936 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1937 traverse.add(strongRelative);
1938 }
1939 }
1940
1941 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001942 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001943 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1944 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1945 // the child here since it won't be added later as a relative.
1946 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1947 childState.zOrderRelativeOf.promote().get())) {
1948 continue;
1949 }
1950 traverse.add(child);
1951 }
1952
1953 return traverse;
1954}
1955
1956void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1957 LayerVector::StateSet stateSet,
1958 const LayerVector::Visitor& visitor) {
1959 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001960
1961 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001962 for (; i < list.size(); i++) {
1963 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001964 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001965 break;
1966 }
chaviw4b129c22018-04-09 16:19:43 -07001967 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001968 }
chaviw4b129c22018-04-09 16:19:43 -07001969
chaviwa76b2712017-09-20 12:02:26 -07001970 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001971 for (; i < list.size(); i++) {
1972 const auto& relative = list[i];
1973 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001974 }
1975}
1976
chaviw4b129c22018-04-09 16:19:43 -07001977std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1978 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1979 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1980
1981 std::vector<Layer*> layersInTree = {this};
1982 for (size_t i = 0; i < children.size(); i++) {
1983 const auto& child = children[i];
1984 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1985 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1986 }
1987
1988 return layersInTree;
1989}
1990
1991void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1992 const LayerVector::Visitor& visitor) {
1993 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1994 std::sort(layersInTree.begin(), layersInTree.end());
1995 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1996}
1997
Peiyong Linefefaac2018-08-17 12:27:51 -07001998ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001999 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002000}
2001
chaviw13fdc492017-06-27 12:40:18 -07002002half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002003 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002004
chaviw13fdc492017-06-27 12:40:18 -07002005 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2006 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002007}
Robert Carr6452f122017-03-21 10:41:29 -07002008
Vishnu Nair6213bd92020-05-08 17:42:25 -07002009ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
2010 ui::Transform::RotationFlags fixedTransformHint = mCurrentState.fixedTransformHint;
2011 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
2012 return fixedTransformHint;
2013 }
2014 const auto& p = mCurrentParent.promote();
2015 if (!p) return fixedTransformHint;
2016 return p->getFixedTransformHint();
2017}
2018
chaviw13fdc492017-06-27 12:40:18 -07002019half4 Layer::getColor() const {
2020 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002021 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002022}
Robert Carr6452f122017-03-21 10:41:29 -07002023
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002024int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01002025 const auto& p = mDrawingParent.promote();
2026
2027 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2028 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002029}
2030
Galia Peychevae0acf382021-04-12 21:22:34 +02002031const std::vector<BlurRegion> Layer::getBlurRegions() const {
2032 auto regionsCopy(getDrawingState().blurRegions);
2033 int layerAlpha = getAlpha();
2034 for (auto& region : regionsCopy) {
2035 region.alpha = region.alpha * layerAlpha;
2036 }
2037 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07002038}
2039
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002040Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2041 const auto& p = mDrawingParent.promote();
2042 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00002043 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002044 if (parentState.radius > 0) {
2045 ui::Transform t = getActiveTransform(getDrawingState());
2046 t = t.inverse();
2047 parentState.cropRect = t.transform(parentState.cropRect);
2048 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2049 // but a transform matrix can define horizontal and vertical scales.
2050 // Let's take the average between both of them and pass into the shader, practically we
2051 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07002052 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
2053 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
2054 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002055 return parentState;
2056 }
2057 }
2058 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00002059 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
2060 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07002061 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002062}
2063
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002064renderengine::ShadowSettings Layer::getShadowSettings(const Rect& layerStackRect) const {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002065 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
2066
2067 // Shift the spot light x-position to the middle of the display and then
2068 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002069 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002070 state.lightPos.y -= mScreenBounds.top;
2071
2072 state.length = mEffectiveShadowRadius;
2073 return state;
2074}
2075
Robert Carr1f0a16a2016-10-24 16:27:39 -07002076void Layer::commitChildList() {
2077 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2078 const auto& child = mCurrentChildren[i];
2079 child->commitChildList();
2080 }
2081 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002082 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002083}
2084
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002085static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2086 if (weakBinderHandle == nullptr) {
2087 return nullptr;
2088 }
2089 sp<IBinder> binderHandle = weakBinderHandle.promote();
2090 if (binderHandle == nullptr) {
2091 return nullptr;
2092 }
2093 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2094 if (handle == nullptr) {
2095 return nullptr;
2096 }
2097 return handle->owner;
2098}
2099
Robert Carr720e5062018-07-30 17:45:14 -07002100void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002101 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002102 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002103 mCurrentState.modified = true;
2104 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002105 setTransactionFlags(eTransactionNeeded);
2106}
2107
Alec Mouri6b9e9912020-01-21 10:50:24 -08002108LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002109 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08002110 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002111 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002112 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2113
Vishnu Nair60db8c02020-04-02 11:55:16 -07002114 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2115 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002116 if (display) {
2117 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002118 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2119 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002120 }
2121
chaviw08f3cb22020-01-13 13:17:21 -08002122 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002123 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002124 }
chaviw6d89e2d2020-01-14 14:42:01 -08002125
2126 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002127}
2128
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002129void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002130 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002131 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002132
2133 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002134
chaviwd62d3062019-09-04 14:48:02 -07002135 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002136 if (buffer != nullptr) {
2137 LayerProtoHelper::writeToProto(buffer,
2138 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002139 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002140 layerInfo->mutable_buffer_transform());
2141 }
2142 layerInfo->set_invalidate(contentDirty);
2143 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002144 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002145 layerInfo->set_queued_frames(getQueuedFrameCount());
2146 layerInfo->set_refresh_pending(isBufferLatched());
2147 layerInfo->set_curr_frame(mCurrentFrameNumber);
2148 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2149
2150 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002151 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002152 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2153 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2154 [&]() { return layerInfo->mutable_position(); });
2155 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002156 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002157 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002158 [&]() { return layerInfo->mutable_visible_region(); });
2159 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002160 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2161 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002162
2163 if (hasColorTransform()) {
2164 LayerProtoHelper::writeToProto(getColorTransform(),
2165 layerInfo->mutable_color_transform());
2166 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002167 }
2168
Vishnu Nair60db8c02020-04-02 11:55:16 -07002169 LayerProtoHelper::writeToProto(mSourceBounds,
2170 [&]() { return layerInfo->mutable_source_bounds(); });
2171 LayerProtoHelper::writeToProto(mScreenBounds,
2172 [&]() { return layerInfo->mutable_screen_bounds(); });
2173 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2174 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2175 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002176}
2177
2178void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002179 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002180 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2181 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002182 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002183
chaviw766c9c52021-02-10 17:36:47 -08002184 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002185
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002186 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2187 layerInfo->set_id(sequence);
2188 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002189 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002190
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002191 for (const auto& child : children) {
2192 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002193 }
chaviw1d044282017-09-27 12:19:28 -07002194
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002195 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2196 sp<Layer> strongRelative = weakRelative.promote();
2197 if (strongRelative != nullptr) {
2198 layerInfo->add_relatives(strongRelative->sequence);
2199 }
chaviwadc40c22018-07-10 16:57:27 -07002200 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002201
2202 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2203 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002204
2205 layerInfo->set_layer_stack(getLayerStack());
2206 layerInfo->set_z(state.z);
2207
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002208 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2209 [&]() {
2210 return layerInfo->mutable_requested_position();
2211 });
2212
chaviw766c9c52021-02-10 17:36:47 -08002213 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002214 [&]() { return layerInfo->mutable_size(); });
2215
chaviw766c9c52021-02-10 17:36:47 -08002216 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002217
2218 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002219
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002220
2221 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2222 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2223 LayerProtoHelper::writeToProto(state.color,
2224 [&]() { return layerInfo->mutable_requested_color(); });
2225 layerInfo->set_flags(state.flags);
2226
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002227 LayerProtoHelper::writeToProto(requestedTransform,
2228 layerInfo->mutable_requested_transform());
2229
2230 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2231 if (parent != nullptr) {
2232 layerInfo->set_parent(parent->sequence);
2233 } else {
2234 layerInfo->set_parent(-1);
2235 }
2236
2237 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2238 if (zOrderRelativeOf != nullptr) {
2239 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2240 } else {
2241 layerInfo->set_z_order_relative_of(-1);
2242 }
chaviw08f3cb22020-01-13 13:17:21 -08002243
2244 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002245
2246 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002247 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002248
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002249 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw4c34a092020-07-08 11:30:06 -07002250 InputWindowInfo info;
2251 if (useDrawing) {
[1;3C2b9fc252021-02-04 16:16:50 -08002252 info = fillInputInfo({nullptr});
chaviw4c34a092020-07-08 11:30:06 -07002253 } else {
2254 info = state.inputInfo;
2255 }
2256
2257 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002258 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002259 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002260
2261 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2262 auto protoMap = layerInfo->mutable_metadata();
2263 for (const auto& entry : state.metadata.mMap) {
2264 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2265 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002266 }
chaviw1d044282017-09-27 12:19:28 -07002267}
2268
Robert Carr2e102c92018-10-23 12:11:15 -07002269bool Layer::isRemovedFromCurrentState() const {
2270 return mRemovedFromCurrentState;
2271}
2272
chaviw39d01472021-04-08 14:26:24 -05002273ui::Transform Layer::getInputTransform() const {
2274 return getTransform();
2275}
2276
2277Rect Layer::getInputBounds() const {
2278 return getCroppedBufferSize(getDrawingState());
2279}
2280
[1;3C2b9fc252021-02-04 16:16:50 -08002281void Layer::fillInputFrameInfo(InputWindowInfo& info, const ui::Transform& toPhysicalDisplay) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002282 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002283 // If this is a portal window, set the touchableRegion to the layerBounds.
2284 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002285 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002286 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002287 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002288 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002289 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002290
chaviw7e72caf2020-12-02 16:50:43 -08002291 if (!layerBounds.isValid()) {
2292 // If the layer bounds is empty, set the frame to empty and clear the transform
2293 info.frameLeft = 0;
2294 info.frameTop = 0;
2295 info.frameRight = 0;
2296 info.frameBottom = 0;
2297 info.transform.reset();
2298 return;
2299 }
2300
chaviw39d01472021-04-08 14:26:24 -05002301 ui::Transform layerToDisplay = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002302 // Transform that takes window coordinates to unrotated display coordinates
2303 ui::Transform t = toPhysicalDisplay * layerToDisplay;
chaviw7e72caf2020-12-02 16:50:43 -08002304 int32_t xSurfaceInset = info.surfaceInset;
2305 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002306 // Bring screenBounds into unrotated space
2307 Rect screenBounds = toPhysicalDisplay.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002308
chaviw1ff3d1e2020-07-01 15:53:47 -07002309 const float xScale = t.getScaleX();
2310 const float yScale = t.getScaleY();
2311 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002312 xSurfaceInset = std::round(xSurfaceInset * xScale);
2313 ySurfaceInset = std::round(ySurfaceInset * yScale);
2314 }
2315
Peiyong Lin74861ad2020-10-14 11:55:33 -07002316 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002317 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002318
2319 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002320 xSurfaceInset = (xSurfaceInset >= 0)
2321 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2322 : 0;
2323 ySurfaceInset = (ySurfaceInset >= 0)
2324 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2325 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002326
Robert Carra7242302020-09-01 18:26:29 -07002327 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2328 // in the overflow scenario?
2329 {
2330 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002331 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2332 transformedLayerBounds.left = tmp;
2333 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2334 transformedLayerBounds.right = tmp;
2335 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2336 transformedLayerBounds.top = tmp;
2337 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2338 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002339 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002340
chaviw44a6d2b2020-09-08 17:14:16 -07002341 // Compute the correct transform to send to input. This will allow it to transform the
2342 // input coordinates from display space into window space. Therefore, it needs to use the
2343 // final layer frame to create the inverse transform. Since surface insets are added later,
2344 // along with the overflow, the best way to ensure we get the correct transform is to use
2345 // the final frame calculated.
2346 // 1. Take the original transform set on the window and get the inverse transform. This is
2347 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002348 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002349 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2350 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2351 // be the new translation values for the transform.
2352 // 3. Update the translation of the original transform to the new translation values.
2353 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2354 // window space.
2355 ui::Transform inverseTransform = t.inverse();
2356 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2357 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002358 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002359 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002360 info.transform = inputTransform.inverse();
2361
chaviw39cfa2e2020-11-04 14:19:02 -08002362 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2363 // The frame should be the area the user sees on screen since it's used for occlusion
2364 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002365 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2366 info.frameLeft = transformedLayerBounds.left;
2367 info.frameTop = transformedLayerBounds.top;
2368 info.frameRight = transformedLayerBounds.right;
2369 info.frameBottom = transformedLayerBounds.bottom;
2370
Vishnu Nair8033a492018-12-05 07:27:23 -08002371 // Position the touchable region relative to frame screen location and restrict it to frame
2372 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002373 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002374}
2375
[1;3C2b9fc252021-02-04 16:16:50 -08002376InputWindowInfo Layer::fillInputInfo(const sp<DisplayDevice>& display) {
chaviw7e72caf2020-12-02 16:50:43 -08002377 if (!hasInputInfo()) {
2378 mDrawingState.inputInfo.name = getName();
2379 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2380 mDrawingState.inputInfo.ownerPid = mOwnerPid;
2381 mDrawingState.inputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
2382 mDrawingState.inputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
2383 mDrawingState.inputInfo.displayId = getLayerStack();
2384 }
2385
2386 InputWindowInfo info = mDrawingState.inputInfo;
2387 info.id = sequence;
2388
2389 if (info.displayId == ADISPLAY_ID_NONE) {
2390 info.displayId = getLayerStack();
2391 }
2392
[1;3C2b9fc252021-02-04 16:16:50 -08002393 // Transform that goes from "logical(rotated)" display to physical/unrotated display.
2394 // This is for when inputflinger operates in physical display-space.
2395 ui::Transform toPhysicalDisplay;
2396 if (display) {
2397 toPhysicalDisplay = display->getTransform();
Evan Rosky84f07f02021-04-16 10:42:42 -07002398 info.displayWidth = display->getWidth();
2399 info.displayHeight = display->getHeight();
[1;3C2b9fc252021-02-04 16:16:50 -08002400 }
2401 fillInputFrameInfo(info, toPhysicalDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08002402
Robert Carr5dc426e2020-06-10 14:29:14 -07002403 // For compatibility reasons we let layers which can receive input
2404 // receive input before they have actually submitted a buffer. Because
2405 // of this we use canReceiveInput instead of isVisible to check the
2406 // policy-visibility, ignoring the buffer state. However for layers with
2407 // hasInputInfo()==false we can use the real visibility state.
2408 // We are just using these layers for occlusion detection in
2409 // InputDispatcher, and obviously if they aren't visible they can't occlude
2410 // anything.
2411 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002412 info.alpha = getAlpha();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002413
2414 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2415 if (info.replaceTouchableRegionWithCrop) {
2416 if (cropLayer == nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002417 info.touchableRegion = Region(toPhysicalDisplay.transform(Rect{mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002418 } else {
[1;3C2b9fc252021-02-04 16:16:50 -08002419 info.touchableRegion =
2420 Region(toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002421 }
2422 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002423 info.touchableRegion = info.touchableRegion.intersect(
2424 toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002425 }
2426
chaviwaf87b3e2019-10-01 16:59:28 -07002427 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2428 // touches from going outside the cloned area.
2429 if (isClone()) {
2430 sp<Layer> clonedRoot = getClonedRoot();
2431 if (clonedRoot != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002432 Rect rect = toPhysicalDisplay.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002433 info.touchableRegion = info.touchableRegion.intersect(rect);
2434 }
2435 }
2436
Robert Carr720e5062018-07-30 17:45:14 -07002437 return info;
2438}
2439
chaviwaf87b3e2019-10-01 16:59:28 -07002440sp<Layer> Layer::getClonedRoot() {
2441 if (mClonedChild != nullptr) {
2442 return this;
2443 }
2444 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2445 return nullptr;
2446 }
2447 return mDrawingParent.promote()->getClonedRoot();
2448}
2449
Robert Carredd13602020-04-13 17:24:34 -07002450bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002451 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002452}
2453
chaviw3e727cd2019-01-31 13:41:05 -08002454bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002455 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002456}
2457
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002458compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002459 const DisplayDevice* display) const {
2460 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002461 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002462}
2463
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002464Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2465 const auto outputLayer = findOutputLayerForDisplay(display);
2466 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002467}
2468
chaviwb4c6e582019-08-16 14:35:07 -07002469void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2470 // copy drawing state from cloned layer
2471 mDrawingState = clonedFrom->mDrawingState;
2472 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002473}
chaviw74b03172019-08-19 11:09:03 -07002474
2475void Layer::updateMirrorInfo() {
2476 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2477 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2478 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2479 // that case, we want to delete the reference to the clone since we want it to get
2480 // destroyed. The root, this layer, will still be around since the client can continue
2481 // to hold a reference, but no cloned layers will be displayed.
2482 mClonedChild = nullptr;
2483 return;
2484 }
2485
2486 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2487 // If the real layer exists and is in current state, add the clone as a child of the root.
2488 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2489 // copied to drawingState for the root layer. So the clonedChild is always removed from
2490 // drawingState and then needs to be added back each traversal.
2491 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2492 addChildToDrawing(mClonedChild);
2493 }
2494
2495 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2496 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2497 mClonedChild->updateClonedRelatives(clonedLayersMap);
2498}
2499
2500void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2501 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2502 // to the clone. If the real layer is no longer alive, continue traversing the children
2503 // since we may be able to pull out other children that are still alive.
2504 if (isClonedFromAlive()) {
2505 sp<Layer> clonedFrom = getClonedFrom();
2506 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002507 clonedLayersMap.emplace(clonedFrom, this);
2508 }
2509
2510 // The clone layer may have children in drawingState since they may have been created and
2511 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2512 // that already exist, since we can just re-use them.
2513 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2514 // not updated in the regular traversal. They are skipped since the root will lose the
2515 // reference to them when it copies its currentChildren to drawing.
2516 for (sp<Layer>& child : mDrawingChildren) {
2517 child->updateClonedDrawingState(clonedLayersMap);
2518 }
2519}
2520
2521void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2522 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2523 mDrawingChildren.clear();
2524
2525 if (!isClonedFromAlive()) {
2526 return;
2527 }
2528
2529 sp<Layer> clonedFrom = getClonedFrom();
2530 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2531 if (child == mirrorRoot) {
2532 // This is to avoid cyclical mirroring.
2533 continue;
2534 }
2535 sp<Layer> clonedChild = clonedLayersMap[child];
2536 if (clonedChild == nullptr) {
2537 clonedChild = child->createClone();
2538 clonedLayersMap[child] = clonedChild;
2539 }
2540 addChildToDrawing(clonedChild);
2541 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2542 }
2543}
2544
chaviwaf87b3e2019-10-01 16:59:28 -07002545void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2546 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2547 if (cropLayer != nullptr) {
2548 if (clonedLayersMap.count(cropLayer) == 0) {
2549 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2550 // self as crop layer to avoid going outside bounds.
2551 mDrawingState.touchableRegionCrop = this;
2552 } else {
2553 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2554 mDrawingState.touchableRegionCrop = clonedCropLayer;
2555 }
2556 }
2557 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2558 // WM or the client.
Michael Wright44753b12020-07-08 13:48:11 +01002559 mDrawingState.inputInfo.flags &= ~InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002560}
2561
2562void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002563 mDrawingState.zOrderRelativeOf = nullptr;
2564 mDrawingState.zOrderRelatives.clear();
2565
2566 if (!isClonedFromAlive()) {
2567 return;
2568 }
2569
chaviwaf87b3e2019-10-01 16:59:28 -07002570 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002571 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002572 const sp<Layer>& relative = relativeWeak.promote();
2573 if (clonedLayersMap.count(relative) > 0) {
2574 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002575 mDrawingState.zOrderRelatives.add(clonedRelative);
2576 }
2577 }
2578
2579 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2580 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2581 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2582 // still traverse the children, but the layer with the missing relativeOf will not be shown
2583 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002584 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2585 if (clonedLayersMap.count(relativeOf) > 0) {
2586 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002587 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2588 }
2589
chaviwaf87b3e2019-10-01 16:59:28 -07002590 updateClonedInputInfo(clonedLayersMap);
2591
chaviw74b03172019-08-19 11:09:03 -07002592 for (sp<Layer>& child : mDrawingChildren) {
2593 child->updateClonedRelatives(clonedLayersMap);
2594 }
2595}
2596
2597void Layer::addChildToDrawing(const sp<Layer>& layer) {
2598 mDrawingChildren.add(layer);
2599 layer->mDrawingParent = this;
2600}
2601
Steven Thomas62a4cf82020-01-31 12:04:03 -08002602Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2603 switch (compatibility) {
2604 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2605 return FrameRateCompatibility::Default;
2606 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2607 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002608 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2609 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002610 default:
2611 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2612 return FrameRateCompatibility::Default;
2613 }
2614}
2615
Marin Shalamanovc5986772021-03-16 16:09:49 +01002616scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2617 switch (strategy) {
2618 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2619 return Seamlessness::OnlySeamless;
2620 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2621 return Seamlessness::SeamedAndSeamless;
2622 default:
2623 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2624 return Seamlessness::Default;
2625 }
2626}
2627
chaviwc5676c62020-09-18 15:01:04 -07002628bool Layer::getPrimaryDisplayOnly() const {
2629 const State& s(mDrawingState);
2630 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2631 return true;
2632 }
2633
2634 sp<Layer> parent = mDrawingParent.promote();
2635 return parent == nullptr ? false : parent->getPrimaryDisplayOnly();
2636}
2637
Mathias Agopian13127d82013-03-05 17:47:11 -08002638// ---------------------------------------------------------------------------
2639
Marin Shalamanov46084422020-10-13 12:33:42 +02002640std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2641 return stream << "{rate=" << rate.rate
2642 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002643 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002644}
2645
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002646}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002647
2648#if defined(__gl_h_)
2649#error "don't include gl/gl.h in this file"
2650#endif
2651
2652#if defined(__gl2_h_)
2653#error "don't include gl2/gl2.h in this file"
2654#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002655
2656// TODO(b/129481165): remove the #pragma below and fix conversion issues
2657#pragma clang diagnostic pop // ignored "-Wconversion"