blob: def271196dc7e29cf084af8bb4632153cd28a54c [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;
Ady Abraham60e42ea2020-03-09 19:17:31 -0700132 mCurrentState.treeHasFrameRateVote = false;
Vishnu Nair6213bd92020-05-08 17:42:25 -0700133 mCurrentState.fixedTransformHint = ui::Transform::ROT_INVALID;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000134 mCurrentState.frameTimelineInfo = {};
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700135 mCurrentState.postTime = -1;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700136 mCurrentState.destinationFrame.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700137
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700138 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
139 // Set an invalid color so there is no color fill.
140 mCurrentState.color.r = -1.0_hf;
141 mCurrentState.color.g = -1.0_hf;
142 mCurrentState.color.b = -1.0_hf;
143 }
144
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700145 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800146 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700147
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800148 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700149 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800150 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200151 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700152
Vishnu Nair0f085c62019-08-30 08:49:12 -0700153 mCallingPid = args.callingPid;
154 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700155
156 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
157 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
158 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700159 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700160 } else {
161 // A create layer request from a non system request cannot specify the owner uid
162 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700163 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700164 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800165}
166
167void Layer::onFirstRef() {
168 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700169}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700170
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700171Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800172 sp<Client> c(mClientRef.promote());
173 if (c != 0) {
174 c->detachLayer(this);
175 }
176
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000177 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700178 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700179}
180
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700181LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
182 uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700183 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700184 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700185 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700186 w(w),
187 h(h),
188 flags(flags),
189 metadata(std::move(metadata)) {
190 IPCThreadState* ipc = IPCThreadState::self();
191 callingPid = ipc->getCallingPid();
192 callingUid = ipc->getCallingUid();
193}
194
Mathias Agopian13127d82013-03-05 17:47:11 -0800195// ---------------------------------------------------------------------------
196// callbacks
197// ---------------------------------------------------------------------------
198
David Sodmaneb085e02017-10-05 18:49:04 -0700199/*
200 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
201 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800202 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700203 */
David Sodmaneb085e02017-10-05 18:49:04 -0700204void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800205
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700206void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
207 if (mCurrentState.zOrderRelativeOf == nullptr) {
208 return;
209 }
Robert Carr2e102c92018-10-23 12:11:15 -0700210
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700211 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
212 if (strongRelative == nullptr) {
213 setZOrderRelativeOf(nullptr);
214 return;
215 }
216
217 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
218 strongRelative->removeZOrderRelative(this);
219 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800220 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700221 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700222}
223
224void Layer::removeFromCurrentState() {
225 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000226
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800227 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700228}
Chia-I Wu38512252017-05-17 14:36:16 -0700229
chaviw68d4dab2020-06-08 15:07:32 -0700230sp<Layer> Layer::getRootLayer() {
231 sp<Layer> parent = getParent();
232 if (parent == nullptr) {
233 return this;
234 }
235 return parent->getRootLayer();
236}
237
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700238void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700239 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
240 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700241 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700242
243 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700244 layer->removeFromCurrentState();
245 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700246 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700247}
248
chaviw61626f22018-11-15 16:26:27 -0800249void Layer::addToCurrentState() {
250 mRemovedFromCurrentState = false;
251
252 for (const auto& child : mCurrentChildren) {
253 child->addToCurrentState();
254 }
255}
256
Mathias Agopian13127d82013-03-05 17:47:11 -0800257// ---------------------------------------------------------------------------
258// set-up
259// ---------------------------------------------------------------------------
260
chaviw13fdc492017-06-27 12:40:18 -0700261bool Layer::getPremultipledAlpha() const {
262 return mPremultipliedAlpha;
263}
264
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700265sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800266 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700267 if (mGetHandleCalled) {
268 ALOGE("Get handle called twice" );
269 return nullptr;
270 }
271 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700272 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800273}
274
275// ---------------------------------------------------------------------------
276// h/w composer set-up
277// ---------------------------------------------------------------------------
278
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700279static Rect reduce(const Rect& win, const Region& exclude) {
280 if (CC_LIKELY(exclude.isEmpty())) {
281 return win;
282 }
283 if (exclude.isRect()) {
284 return win.reduce(exclude.getBounds());
285 }
286 return Region(win).subtract(exclude).getBounds();
287}
288
Dan Stoza80d61162017-12-20 15:57:52 -0800289static FloatRect reduce(const FloatRect& win, const Region& exclude) {
290 if (CC_LIKELY(exclude.isEmpty())) {
291 return win;
292 }
293 // Convert through Rect (by rounding) for lack of FloatRegion
294 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
295}
296
Vishnu Nair4351ad52019-02-11 14:13:02 -0800297Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800298 if (!reduceTransparentRegion) {
299 return Rect{mScreenBounds};
300 }
301
302 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800303 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800304 // Transform to screen space.
305 bounds = t.transform(bounds);
306 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700307}
308
Vishnu Nair4351ad52019-02-11 14:13:02 -0800309FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000310 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800311 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700312}
313
Vishnu Nairf0c28512019-02-08 12:40:28 -0800314FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
315 // Subtract the transparent region and snap to the bounds.
316 return reduce(mBounds, activeTransparentRegion);
317}
318
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700319void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
320 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800321 const State& s(getDrawingState());
322
323 // Calculate effective layer transform
324 mEffectiveTransform = parentTransform * getActiveTransform(s);
325
326 // Transform parent bounds to layer space
327 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
328
Vishnu Nairc652ff82019-03-15 12:48:54 -0700329 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800330 mSourceBounds = computeSourceBounds(parentBounds);
331
332 // Calculate bounds by croping diplay frame with layer crop and parent bounds
333 FloatRect bounds = mSourceBounds;
334 const Rect layerCrop = getCrop(s);
335 if (!layerCrop.isEmpty()) {
336 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
337 }
338 bounds = bounds.intersect(parentBounds);
339
340 mBounds = bounds;
341 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700342
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700343 // Use the layer's own shadow radius if set. Otherwise get the radius from
344 // parent.
345 if (s.shadowRadius > 0.f) {
346 mEffectiveShadowRadius = s.shadowRadius;
347 } else {
348 mEffectiveShadowRadius = parentShadowRadius;
349 }
350
351 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
352 // don't pass it to its children.
353 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
354
Vishnu Nair4351ad52019-02-11 14:13:02 -0800355 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700356 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800357 }
358}
359
Vishnu Nair60356342018-11-13 13:00:45 -0800360Rect Layer::getCroppedBufferSize(const State& s) const {
361 Rect size = getBufferSize(s);
362 Rect crop = getCrop(s);
363 if (!crop.isEmpty() && size.isValid()) {
364 size.intersect(crop, &size);
365 } else if (!crop.isEmpty()) {
366 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700367 }
Vishnu Nair60356342018-11-13 13:00:45 -0800368 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800369}
370
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700371void Layer::setupRoundedCornersCropCoordinates(Rect win,
372 const FloatRect& roundedCornersCrop) const {
373 // Translate win by the rounded corners rect coordinates, to have all values in
374 // layer coordinate space.
375 win.left -= roundedCornersCrop.left;
376 win.right -= roundedCornersCrop.left;
377 win.top -= roundedCornersCrop.top;
378 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700379}
380
Lloyd Piquede196652020-01-22 17:29:58 -0800381void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800382 const auto& drawingState{getDrawingState()};
383 const uint32_t layerStack = getLayerStack();
384 const auto alpha = static_cast<float>(getAlpha());
385 const bool opaque = isOpaque(drawingState);
386 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
387
388 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
389 if (!opaque || alpha != 1.0f) {
390 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
391 : Hwc2::IComposerClient::BlendMode::COVERAGE;
392 }
393
Lloyd Piquede196652020-01-22 17:29:58 -0800394 auto* compositionState = editCompositionState();
395 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800396 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800397 compositionState->internalOnly = getPrimaryDisplayOnly();
398 compositionState->isVisible = isVisible();
399 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
400 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800401
Lloyd Piquede196652020-01-22 17:29:58 -0800402 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800403 contentDirty = false;
404
Lloyd Piquede196652020-01-22 17:29:58 -0800405 compositionState->geomLayerBounds = mBounds;
406 compositionState->geomLayerTransform = getTransform();
407 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
408 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800409
Lloyd Piquede196652020-01-22 17:29:58 -0800410 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
411 compositionState->alpha = alpha;
412 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700413 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500414 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800415}
416
Lloyd Piquede196652020-01-22 17:29:58 -0800417void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800418 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800419
Lloyd Piquea83776c2019-01-29 18:42:32 -0800420 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
421 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700422 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400423 if (parent.get()) {
424 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800425 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
426 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700427 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800428 type = parentType;
429 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700430 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400431 }
432
Lloyd Piquede196652020-01-22 17:29:58 -0800433 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700434
Lloyd Piquede196652020-01-22 17:29:58 -0800435 compositionState->geomBufferSize = getBufferSize(drawingState);
436 compositionState->geomContentCrop = getBufferCrop();
437 compositionState->geomCrop = getCrop(drawingState);
438 compositionState->geomBufferTransform = getBufferTransform();
439 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
440 compositionState->geomUsesSourceCrop = usesSourceCrop();
441 compositionState->isSecure = isSecure();
442
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800443 compositionState->metadata.clear();
444 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
445 for (const auto& [key, mandatory] : supportedMetadata) {
446 const auto& genericLayerMetadataCompatibilityMap =
447 mFlinger->getGenericLayerMetadataKeyMap();
448 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
449 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
450 continue;
451 }
452 const uint32_t id = compatIter->second;
453
454 auto it = drawingState.metadata.mMap.find(id);
455 if (it == std::end(drawingState.metadata.mMap)) {
456 continue;
457 }
458
459 compositionState->metadata
460 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
461 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800462}
David Sodmanba340492018-08-05 21:51:33 -0700463
Lloyd Piquede196652020-01-22 17:29:58 -0800464void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800465 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800466 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800467
Lloyd Piquede196652020-01-22 17:29:58 -0800468 compositionState->forceClientComposition = false;
469
470 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
471 compositionState->dataspace = getDataSpace();
472 compositionState->colorTransform = getColorTransform();
473 compositionState->colorTransformIsIdentity = !hasColorTransform();
474 compositionState->surfaceDamage = surfaceDamageRegion;
475 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800476
477 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700478
Lloyd Piquede196652020-01-22 17:29:58 -0800479 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800480 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800481
482 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400483 // Rounded corners no longer force client composition, since we may use a
484 // hole punch so that the layer will appear to have rounded corners.
485 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500486 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800487 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800488 }
489}
490
Lloyd Piquede196652020-01-22 17:29:58 -0800491void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800492 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800493 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800494
495 // Apply the layer's transform, followed by the display's global transform
496 // Here we're guaranteed that the layer's transform preserves rects
497 Rect win = getCroppedBufferSize(drawingState);
498 // Subtract the transparent region and snap to the bounds
499 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
500 Rect frame(getTransform().transform(bounds));
501
Lloyd Piquede196652020-01-22 17:29:58 -0800502 compositionState->cursorFrame = frame;
503}
504
505sp<compositionengine::LayerFE> Layer::asLayerFE() const {
506 return const_cast<compositionengine::LayerFE*>(
507 static_cast<const compositionengine::LayerFE*>(this));
508}
509
510sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
511 return nullptr;
512}
513
514compositionengine::LayerFECompositionState* Layer::editCompositionState() {
515 return nullptr;
516}
517
518const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
519 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800520}
521
Lloyd Piquef16688f2019-02-19 17:47:57 -0800522bool Layer::onPreComposition(nsecs_t) {
523 return false;
524}
525
Lloyd Piquede196652020-01-22 17:29:58 -0800526void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800527 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800528
Lloyd Piquec6687342019-03-07 21:34:57 -0800529 switch (subset) {
530 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800531 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800532 break;
533
534 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800535 prepareBasicGeometryCompositionState();
536 prepareGeometryCompositionState();
537 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800538 break;
539
540 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800541 preparePerFrameCompositionState();
542 break;
543
544 case StateSubset::Cursor:
545 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800546 break;
547 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800548}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700549
Lloyd Piquea83776c2019-01-29 18:42:32 -0800550const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700551 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800552}
553
Mathias Agopian13127d82013-03-05 17:47:11 -0800554// ---------------------------------------------------------------------------
555// drawing...
556// ---------------------------------------------------------------------------
557
Vishnu Nair9b079a22020-01-21 14:36:08 -0800558std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100559 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800560 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800561 return {};
562 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800563
Vishnu Nair4351ad52019-02-11 14:13:02 -0800564 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000565 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800566
567 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800568 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700569 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000570
571 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800572 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000573 }
574
575 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800576 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
577 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000578
Lloyd Piquef16688f2019-02-19 17:47:57 -0800579 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700580 layerSettings.sourceDataspace = getDataSpace();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100581 if (!targetSettings.disableBlurs) {
582 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
583 layerSettings.blurRegions = getBlurRegions();
Derek Sollenberger39feade2021-04-27 16:08:40 -0400584 layerSettings.blurRegionTransform =
585 getActiveTransform(getDrawingState()).inverse().asMatrix4();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100586 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700587 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800588 // Record the name of the layer for debugging further down the stack.
589 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800590 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800591}
592
Vishnu Nair9b079a22020-01-21 14:36:08 -0800593std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareShadowClientComposition(
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200594 const LayerFE::LayerSettings& casterLayerSettings, const Rect& layerStackRect,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800595 ui::Dataspace outputDataspace) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200596 renderengine::ShadowSettings shadow = getShadowSettings(layerStackRect);
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800597 if (shadow.length <= 0.f) {
598 return {};
599 }
600
601 const float casterAlpha = casterLayerSettings.alpha;
602 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
603 casterLayerSettings.source.buffer.isOpaque);
604
Vishnu Nair9b079a22020-01-21 14:36:08 -0800605 compositionengine::LayerFE::LayerSettings shadowLayer = casterLayerSettings;
606
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800607 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800608 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800609
610 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
611 // Otherwise the generated shadow will only be shown around the casting layer.
612 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
613 shadowLayer.shadow.ambientColor *= casterAlpha;
614 shadowLayer.shadow.spotColor *= casterAlpha;
615 shadowLayer.sourceDataspace = outputDataspace;
616 shadowLayer.source.buffer.buffer = nullptr;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800617 shadowLayer.source.buffer.fence = nullptr;
618 shadowLayer.frameNumber = 0;
619 shadowLayer.bufferId = 0;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800620 shadowLayer.name = getName();
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800621
622 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
623 return {};
624 }
625
626 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800627 const FloatRect& cornerRadiusCropRect = shadowLayer.geometry.roundedCornersCrop;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800628 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
629
630 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
631 // radius accordingly.
632 if (casterCornerRadius > 0.f) {
633 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
634 std::abs(cornerRadiusCropRect.left - casterRect.left));
635 if (cropRectOffset > casterCornerRadius) {
636 casterCornerRadius = 0;
637 } else {
638 casterCornerRadius -= cropRectOffset;
639 }
640 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
641 }
642
643 return shadowLayer;
644}
645
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800646void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
647 bool blackout) const {
648 layerSettings.source.buffer.buffer = nullptr;
649 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
650 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800651 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800652 layerSettings.frameNumber = 0;
653
654 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
655 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800656 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800657}
658
659std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
660 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
661 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
662 prepareClientComposition(targetSettings);
663 // Nothing to render.
664 if (!layerSettings) {
665 return {};
666 }
667
668 // HWC requests to clear this layer.
669 if (targetSettings.clearContent) {
670 prepareClearClientComposition(*layerSettings, false /* blackout */);
671 return {*layerSettings};
672 }
673
674 std::optional<compositionengine::LayerFE::LayerSettings> shadowSettings =
675 prepareShadowClientComposition(*layerSettings, targetSettings.viewport,
676 targetSettings.dataspace);
677 // There are no shadows to render.
678 if (!shadowSettings) {
679 return {*layerSettings};
680 }
681
682 // If the layer casts a shadow but the content casting the shadow is occluded, skip
683 // composing the non-shadow content and only draw the shadows.
684 if (targetSettings.realContentIsVisible) {
685 return {*shadowSettings, *layerSettings};
686 }
687
688 return {*shadowSettings};
689}
690
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700691Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
692 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800693 if (outputLayer == nullptr) {
694 return Hwc2::IComposerClient::Composition::INVALID;
695 }
696 if (outputLayer->getState().hwc) {
697 return (*outputLayer->getState().hwc).hwcCompositionType;
698 } else {
699 return Hwc2::IComposerClient::Composition::CLIENT;
700 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800701}
702
Mathias Agopian13127d82013-03-05 17:47:11 -0800703// ----------------------------------------------------------------------------
704// local state
705// ----------------------------------------------------------------------------
706
David Sodman41fdfc92017-11-06 16:09:56 -0800707bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800708 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700709 if (s.flags & layer_state_t::eLayerSecure) {
710 return true;
711 }
712
713 const auto p = mDrawingParent.promote();
714 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700715}
716
Mathias Agopian13127d82013-03-05 17:47:11 -0800717// ----------------------------------------------------------------------------
718// transaction
719// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800720
Marissa Wall61c58622018-07-18 10:12:20 -0700721uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000722 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723
Marissa Wall61c58622018-07-18 10:12:20 -0700724 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
725 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700726
David Sodmaneb085e02017-10-05 18:49:04 -0700727 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700728 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000729 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800730 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
731 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
732 " requested={ wh={%4u,%4u} }}\n"
733 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
734 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700735 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700736 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
chaviw25714502021-02-11 10:01:08 -0800737 stateToCommit->crop.left, stateToCommit->crop.top, stateToCommit->crop.right,
738 stateToCommit->crop.bottom, stateToCommit->crop.getWidth(),
739 stateToCommit->crop.getHeight(), stateToCommit->requested_legacy.w,
740 stateToCommit->requested_legacy.h, s.active_legacy.w, s.active_legacy.h,
741 s.crop.left, s.crop.top, s.crop.right, s.crop.bottom, s.crop.getWidth(),
742 s.crop.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700744
Robert Carre392b552017-09-19 12:16:05 -0700745 // Don't let Layer::doTransaction update the drawing state
746 // if we have a pending resize, unless we are in fixed-size mode.
747 // the drawing state will be updated only once we receive a buffer
748 // with the correct size.
749 //
750 // In particular, we want to make sure the clip (which is part
751 // of the geometry state) is latched together with the size but is
752 // latched immediately when no resizing is involved.
753 //
754 // If a sideband stream is attached, however, we want to skip this
755 // optimization so that transactions aren't missed when a buffer
756 // never arrives
757 //
758 // In the case that we don't have a buffer we ignore other factors
759 // and avoid entering the resizePending state. At a high level the
760 // resizePending state is to avoid applying the state of the new buffer
761 // to the old buffer. However in the state where we don't have an old buffer
762 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700763 const bool resizePending =
764 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
765 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700766 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700767 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800768 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700769 flags |= eDontUpdateGeometryState;
770 }
771 }
772
Robert Carr7bf247e2017-05-18 14:02:49 -0700773 // Here we apply various requested geometry states, depending on our
774 // latching configuration. See Layer.h for a detailed discussion of
775 // how geometry latching is controlled.
776 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000777 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700778
Robert Carr7bf247e2017-05-18 14:02:49 -0700779 // There is an awkward asymmetry in the handling of the crop states in the position
780 // states, as can be seen below. Largely this arises from position and transform
781 // being stored in the same data structure while having different latching rules.
782 // b/38182305
783 //
Marissa Wall61c58622018-07-18 10:12:20 -0700784 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700785 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700786 editCurrentState.active_legacy = editCurrentState.requested_legacy;
787 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800788 }
789
Marissa Wall61c58622018-07-18 10:12:20 -0700790 return flags;
791}
792
793uint32_t Layer::doTransaction(uint32_t flags) {
794 ATRACE_CALL();
795
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800796 if (mChildrenChanged) {
797 flags |= eVisibleRegion;
798 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700799 }
800
Robert Carr0758e5d2021-03-11 22:15:04 -0800801 // TODO: This is unfortunate.
802 mCurrentStateModified = mCurrentState.modified;
803 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700804
Robert Carr0758e5d2021-03-11 22:15:04 -0800805 flags = doTransactionResize(flags, &mCurrentState);
Marissa Wall61c58622018-07-18 10:12:20 -0700806
Alec Mourib416efd2018-09-06 21:01:59 +0000807 const State& s(getDrawingState());
Robert Carr0758e5d2021-03-11 22:15:04 -0800808 State& c(getCurrentState());
Marissa Wall61c58622018-07-18 10:12:20 -0700809
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700810 // Translates dest frame into scale and position updates. This helps align geometry calculations
811 // for BufferStateLayer with other layers. This should ideally happen in the client once client
812 // has the display orientation details from WM.
813 updateGeometry();
814
chaviw4e765532021-04-30 12:11:39 -0500815 if (c.width != s.width || c.height != s.height || !(c.transform == s.transform)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800816 // invalidate and recompute the visible regions if needed
817 flags |= Layer::eVisibleRegion;
818 }
819
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700820 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800821 // invalidate and recompute the visible regions if needed
822 flags |= eVisibleRegion;
823 this->contentDirty = true;
824
825 // we may use linear filtering, if the matrix scales us
Marin Shalamanov043ba292020-09-10 13:35:20 +0200826 mNeedsFiltering = getActiveTransform(c).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800827 }
828
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800829 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700830 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800831 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700832 }
833
chaviw47dbd692020-10-26 17:50:12 -0700834 // Add the callbacks from the drawing state into the current state. This is so when the current
835 // state gets copied to drawing, we don't lose the callback handles that are still in drawing.
836 for (auto& handle : s.callbackHandles) {
837 c.callbackHandles.push_back(handle);
838 }
839
Vishnu Nair1506b182021-02-22 14:35:15 -0800840 // Allow BufferStateLayer to release any unlatched buffers in drawing state.
Alec Mouria90a5702021-04-16 16:36:21 +0000841 bufferMayChange(c.buffer->getBuffer());
Vishnu Nair1506b182021-02-22 14:35:15 -0800842
Mathias Agopian13127d82013-03-05 17:47:11 -0800843 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700844 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800845 mCurrentState.callbackHandles = {};
Robert Carra1257842020-01-31 13:48:28 -0800846
Mathias Agopian13127d82013-03-05 17:47:11 -0800847 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848}
849
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000850void Layer::commitTransaction(State& stateToCommit) {
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000851 if (auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
Alec Mouria90a5702021-04-16 16:36:21 +0000852 ((mDrawingState.buffer && stateToCommit.buffer &&
853 mDrawingState.buffer->getBuffer() != stateToCommit.buffer->getBuffer()) ||
854 (mDrawingState.buffer && !stateToCommit.buffer) ||
855 (!mDrawingState.buffer && stateToCommit.buffer)) &&
856 bufferSurfaceFrame != nullptr &&
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000857 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
858 // If the previous buffer was committed but not latched (refreshPending - happens during
859 // back to back invalidates), it gets silently dropped here. Mark the corresponding
860 // SurfaceFrame as dropped to prevent it from getting stuck in the pending classification
861 // list.
862 addSurfaceFrameDroppedForBuffer(bufferSurfaceFrame);
863 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800864 mDrawingState = stateToCommit;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000865
866 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
867 // bufferSurfaceFrameTX will be presented in latchBuffer.
868 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
869 if (surfaceFrame->getPresentState() != PresentState::Presented) {
870 // With applyPendingStates, we could end up having presented surfaceframes from previous
871 // states
872 surfaceFrame->setPresentState(PresentState::Presented);
873 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
874 }
875 }
876 // Clear the surfaceFrames from the old state now that it has been copied into DrawingState.
877 stateToCommit.bufferSurfaceFrameTX.reset();
878 stateToCommit.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700879}
880
Mathias Agopian13127d82013-03-05 17:47:11 -0800881uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800882 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800883}
884
885uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800886 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800887}
888
chaviw214c89d2019-09-04 16:03:53 -0700889bool Layer::setPosition(float x, float y) {
chaviw4e765532021-04-30 12:11:39 -0500890 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700892
893 // We update the requested and active position simultaneously because
894 // we want to apply the position portion of the transform matrix immediately,
895 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
chaviw4e765532021-04-30 12:11:39 -0500896 mCurrentState.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700897 // Here we directly update the active state
898 // unlike other setters, because we store it within
899 // the transform, but use different latching rules.
900 // b/38182305
chaviw4e765532021-04-30 12:11:39 -0500901 mCurrentState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700902
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800903 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800904 setTransactionFlags(eTransactionNeeded);
905 return true;
906}
Robert Carr82364e32016-05-15 11:27:47 -0700907
Robert Carr1f0a16a2016-10-24 16:27:39 -0700908bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
909 ssize_t idx = mCurrentChildren.indexOf(childLayer);
910 if (idx < 0) {
911 return false;
912 }
913 if (childLayer->setLayer(z)) {
914 mCurrentChildren.removeAt(idx);
915 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800916 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700917 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800918 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700919}
920
Robert Carr503c7042017-09-27 15:06:08 -0700921bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
922 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
923 ssize_t idx = mCurrentChildren.indexOf(childLayer);
924 if (idx < 0) {
925 return false;
926 }
927 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
928 mCurrentChildren.removeAt(idx);
929 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800930 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700931 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800932 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700933}
934
Robert Carrae060832016-11-28 10:51:00 -0800935bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800936 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
937 mCurrentState.sequence++;
938 mCurrentState.z = z;
939 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700940
941 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 if (mCurrentState.zOrderRelativeOf != nullptr) {
943 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700944 if (strongRelative != nullptr) {
945 strongRelative->removeZOrderRelative(this);
946 }
chaviw606e5cf2019-03-01 10:12:10 -0800947 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700948 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800949 setTransactionFlags(eTransactionNeeded);
950 return true;
951}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700952
Robert Carrdb66e622017-04-10 16:55:57 -0700953void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800954 mCurrentState.zOrderRelatives.remove(relative);
955 mCurrentState.sequence++;
956 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700957 setTransactionFlags(eTransactionNeeded);
958}
959
960void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800961 mCurrentState.zOrderRelatives.add(relative);
962 mCurrentState.modified = true;
963 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700964 setTransactionFlags(eTransactionNeeded);
965}
966
chaviw606e5cf2019-03-01 10:12:10 -0800967void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
968 mCurrentState.zOrderRelativeOf = relativeOf;
969 mCurrentState.sequence++;
970 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700971 mCurrentState.isRelativeOf = relativeOf != nullptr;
972
chaviw606e5cf2019-03-01 10:12:10 -0800973 setTransactionFlags(eTransactionNeeded);
974}
975
Robert Carr503d2bd2017-12-04 15:49:47 -0800976bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700977 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
978 if (handle == nullptr) {
979 return false;
980 }
981 sp<Layer> relative = handle->owner.promote();
982 if (relative == nullptr) {
983 return false;
984 }
985
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800986 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
987 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800988 return false;
989 }
990
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800991 mCurrentState.sequence++;
992 mCurrentState.modified = true;
993 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700994
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800995 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700996 if (oldZOrderRelativeOf != nullptr) {
997 oldZOrderRelativeOf->removeZOrderRelative(this);
998 }
chaviw606e5cf2019-03-01 10:12:10 -0800999 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001000 relative->addZOrderRelative(this);
1001
1002 setTransactionFlags(eTransactionNeeded);
1003
1004 return true;
1005}
1006
Mathias Agopian13127d82013-03-05 17:47:11 -08001007bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001008 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001009 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001010 mCurrentState.requested_legacy.w = w;
1011 mCurrentState.requested_legacy.h = h;
1012 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001013 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001014
1015 // record the new size, from this point on, when the client request
1016 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001017 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001018 return true;
1019}
chaviw4e765532021-04-30 12:11:39 -05001020
Dan Stoza9e56aa02015-11-02 13:00:03 -08001021bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001022 if (mCurrentState.color.a == alpha) return false;
1023 mCurrentState.sequence++;
1024 mCurrentState.color.a = alpha;
1025 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001026 setTransactionFlags(eTransactionNeeded);
1027 return true;
1028}
chaviw13fdc492017-06-27 12:40:18 -07001029
Valerie Haudd0b7572019-01-29 14:59:27 -08001030bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1031 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001032 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001033 }
1034 mCurrentState.sequence++;
1035 mCurrentState.modified = true;
1036 setTransactionFlags(eTransactionNeeded);
1037
1038 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001039 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -08001040 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001041 std::string name = mName + "BackgroundColorLayer";
Vishnu Nairfa247b12020-02-11 08:58:26 -08001042 mCurrentState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001043 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1044 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001045
Valerie Haudd0b7572019-01-29 14:59:27 -08001046 // add to child list
1047 addChild(mCurrentState.bgColorLayer);
1048 mFlinger->mLayersAdded = true;
1049 // set up SF to handle added color layer
1050 if (isRemovedFromCurrentState()) {
1051 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1052 }
1053 mFlinger->setTransactionFlags(eTransactionNeeded);
1054 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1055 mCurrentState.bgColorLayer->reparent(nullptr);
1056 mCurrentState.bgColorLayer = nullptr;
1057 return true;
1058 }
1059
1060 mCurrentState.bgColorLayer->setColor(color);
1061 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1062 mCurrentState.bgColorLayer->setAlpha(alpha);
1063 mCurrentState.bgColorLayer->setDataspace(dataspace);
1064
chaviw13fdc492017-06-27 12:40:18 -07001065 return true;
1066}
1067
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001068bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001069 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001070
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001071 mCurrentState.sequence++;
1072 mCurrentState.cornerRadius = cornerRadius;
1073 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001074 setTransactionFlags(eTransactionNeeded);
1075 return true;
1076}
1077
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001078bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1079 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1080
1081 mCurrentState.sequence++;
1082 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1083 mCurrentState.modified = true;
1084 setTransactionFlags(eTransactionNeeded);
1085 return true;
1086}
1087
Robert Carrd4ae7f32018-06-07 16:10:57 -07001088bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1089 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001090 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001091 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1092
1093 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -08001094 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
1095 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -07001096 return false;
1097 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001098 mCurrentState.sequence++;
chaviw4e765532021-04-30 12:11:39 -05001099 mCurrentState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001100 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001101 setTransactionFlags(eTransactionNeeded);
1102 return true;
1103}
Marissa Wall61c58622018-07-18 10:12:20 -07001104
Mathias Agopian13127d82013-03-05 17:47:11 -08001105bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001106 mCurrentState.requestedTransparentRegion_legacy = transparent;
1107 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 setTransactionFlags(eTransactionNeeded);
1109 return true;
1110}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001111
Lucas Dupinc3800b82020-10-02 16:24:48 -07001112bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
1113 mCurrentState.sequence++;
1114 mCurrentState.blurRegions = blurRegions;
1115 mCurrentState.modified = true;
1116 setTransactionFlags(eTransactionNeeded);
1117 return true;
1118}
1119
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001120bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001121 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1122 if (mCurrentState.flags == newFlags) return false;
1123 mCurrentState.sequence++;
1124 mCurrentState.flags = newFlags;
1125 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001126 setTransactionFlags(eTransactionNeeded);
1127 return true;
1128}
Robert Carr99e27f02016-06-16 15:18:02 -07001129
chaviw25714502021-02-11 10:01:08 -08001130bool Layer::setCrop(const Rect& crop) {
1131 if (mCurrentState.requestedCrop == crop) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001132 mCurrentState.sequence++;
chaviw25714502021-02-11 10:01:08 -08001133 mCurrentState.requestedCrop = crop;
1134 mCurrentState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001135
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001136 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001137 setTransactionFlags(eTransactionNeeded);
1138 return true;
1139}
Robert Carr8d5227b2017-03-16 15:41:03 -07001140
Evan Roskyef876c92019-01-25 17:46:06 -08001141bool Layer::setMetadata(const LayerMetadata& data) {
1142 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001143 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001144 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001145 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001146}
1147
Mathias Agopian13127d82013-03-05 17:47:11 -08001148bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001149 if (mCurrentState.layerStack == layerStack) return false;
1150 mCurrentState.sequence++;
1151 mCurrentState.layerStack = layerStack;
1152 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001153 setTransactionFlags(eTransactionNeeded);
1154 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001155}
1156
Peiyong Linc502cb72019-03-01 15:00:23 -08001157bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1158 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1159 return false;
1160 }
1161 mCurrentState.sequence++;
1162 mCurrentState.colorSpaceAgnostic = agnostic;
1163 mCurrentState.modified = true;
1164 setTransactionFlags(eTransactionNeeded);
1165 return true;
1166}
1167
Ana Krulecc84d09b2019-11-02 23:10:29 +01001168bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1169 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1170 mCurrentState.frameRateSelectionPriority = priority;
1171 mCurrentState.sequence++;
1172 mCurrentState.modified = true;
1173 setTransactionFlags(eTransactionNeeded);
1174 return true;
1175}
1176
1177int32_t Layer::getFrameRateSelectionPriority() const {
1178 // Check if layer has priority set.
1179 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1180 return mDrawingState.frameRateSelectionPriority;
1181 }
1182 // If not, search whether its parents have it set.
1183 sp<Layer> parent = getParent();
1184 if (parent != nullptr) {
1185 return parent->getFrameRateSelectionPriority();
1186 }
1187
1188 return Layer::PRIORITY_UNSET;
1189}
1190
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001191bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1192 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1193};
1194
Robert Carr1f0a16a2016-10-24 16:27:39 -07001195uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001196 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001197 if (p == nullptr) {
1198 return getDrawingState().layerStack;
1199 }
1200 return p->getLayerStack();
1201}
1202
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001203bool Layer::setShadowRadius(float shadowRadius) {
1204 if (mCurrentState.shadowRadius == shadowRadius) {
1205 return false;
1206 }
1207
1208 mCurrentState.sequence++;
1209 mCurrentState.shadowRadius = shadowRadius;
1210 mCurrentState.modified = true;
1211 setTransactionFlags(eTransactionNeeded);
1212 return true;
1213}
1214
Vishnu Nair6213bd92020-05-08 17:42:25 -07001215bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
1216 if (mCurrentState.fixedTransformHint == fixedTransformHint) {
1217 return false;
1218 }
1219
1220 mCurrentState.sequence++;
1221 mCurrentState.fixedTransformHint = fixedTransformHint;
1222 mCurrentState.modified = true;
1223 setTransactionFlags(eTransactionNeeded);
1224 return true;
1225}
1226
John Reckcdb4ed72021-02-04 13:39:33 -05001227bool Layer::setStretchEffect(const StretchEffect& effect) {
1228 StretchEffect temp = effect;
1229 temp.sanitize();
1230 if (mCurrentState.stretchEffect == temp) {
1231 return false;
1232 }
1233 mCurrentState.sequence++;
1234 mCurrentState.stretchEffect = temp;
1235 mCurrentState.modified = true;
1236 setTransactionFlags(eTransactionNeeded);
1237 return true;
1238}
1239
John Reckc00c6692021-02-16 11:37:33 -05001240StretchEffect Layer::getStretchEffect() const {
1241 if (mDrawingState.stretchEffect.hasEffect()) {
1242 return mDrawingState.stretchEffect;
1243 }
1244
1245 sp<Layer> parent = getParent();
1246 if (parent != nullptr) {
1247 auto effect = parent->getStretchEffect();
1248 if (effect.hasEffect()) {
1249 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1250 return effect;
1251 }
1252 }
1253 return StretchEffect{};
1254}
1255
Ady Abraham60e42ea2020-03-09 19:17:31 -07001256void Layer::updateTreeHasFrameRateVote() {
1257 const auto traverseTree = [&](const LayerVector::Visitor& visitor) {
1258 auto parent = getParent();
1259 while (parent) {
1260 visitor(parent.get());
1261 parent = parent->getParent();
1262 }
1263
1264 traverse(LayerVector::StateSet::Current, visitor);
1265 };
1266
1267 // update parents and children about the vote
Ady Abrahamf467f892020-07-31 16:01:53 -07001268 // First traverse the tree and count how many layers has votes. In addition
1269 // activate the layers in Scheduler's LayerHistory for it to check for changes
Ady Abraham60e42ea2020-03-09 19:17:31 -07001270 int layersWithVote = 0;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001271 traverseTree([&layersWithVote](Layer* layer) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001272 const auto layerVotedWithDefaultCompatibility =
1273 layer->mCurrentState.frameRate.rate.isValid() &&
Ady Abraham414e8b52020-05-21 15:36:04 -07001274 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Default;
1275 const auto layerVotedWithNoVote =
1276 layer->mCurrentState.frameRate.type == FrameRateCompatibility::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001277 const auto layerVotedWithExactCompatibility =
1278 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Exact;
Ady Abraham414e8b52020-05-21 15:36:04 -07001279
1280 // We do not count layers that are ExactOrMultiple for the same reason
1281 // we are allowing touch boost for those layers. See
1282 // RefreshRateConfigs::getBestRefreshRate for more details.
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001283 if (layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1284 layerVotedWithExactCompatibility) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001285 layersWithVote++;
1286 }
1287 });
1288
1289 // Now update the other layers
1290 bool transactionNeeded = false;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001291 traverseTree([layersWithVote, &transactionNeeded, this](Layer* layer) {
1292 const bool treeHasFrameRateVote = layersWithVote > 0;
1293 if (layer->mCurrentState.treeHasFrameRateVote != treeHasFrameRateVote) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001294 layer->mCurrentState.sequence++;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001295 layer->mCurrentState.treeHasFrameRateVote = treeHasFrameRateVote;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001296 layer->mCurrentState.modified = true;
1297 layer->setTransactionFlags(eTransactionNeeded);
1298 transactionNeeded = true;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001299
1300 mFlinger->mScheduler->recordLayerHistory(layer, systemTime(),
1301 LayerHistory::LayerUpdateType::SetFrameRate);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001302 }
1303 });
1304
1305 if (transactionNeeded) {
1306 mFlinger->setTransactionFlags(eTraversalNeeded);
1307 }
1308}
1309
Ady Abraham71c437d2020-01-31 15:56:57 -08001310bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001311 if (!mFlinger->useFrameRateApi) {
1312 return false;
1313 }
Steven Thomas3172e202020-01-06 19:25:30 -08001314 if (mCurrentState.frameRate == frameRate) {
1315 return false;
1316 }
1317
1318 mCurrentState.sequence++;
1319 mCurrentState.frameRate = frameRate;
1320 mCurrentState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001321
1322 updateTreeHasFrameRateVote();
1323
Steven Thomas3172e202020-01-06 19:25:30 -08001324 setTransactionFlags(eTransactionNeeded);
1325 return true;
1326}
1327
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001328void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1329 nsecs_t postTime) {
1330 mCurrentState.postTime = postTime;
1331
1332 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1333 // there are two transactions with the same token, the first one without a buffer and the
1334 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1335 // in that case.
1336 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1337 if (it != mCurrentState.bufferlessSurfaceFramesTX.end()) {
1338 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1339 mCurrentState.bufferSurfaceFrameTX = it->second;
1340 mCurrentState.bufferlessSurfaceFramesTX.erase(it);
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001341 mCurrentState.bufferSurfaceFrameTX->promoteToBuffer();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001342 mCurrentState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
1343 } else {
1344 mCurrentState.bufferSurfaceFrameTX =
1345 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1346 }
1347}
1348
1349void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1350 nsecs_t postTime) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001351 mCurrentState.frameTimelineInfo = info;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001352 mCurrentState.postTime = postTime;
1353 mCurrentState.modified = true;
1354 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001355
1356 if (const auto& bufferSurfaceFrameTX = mCurrentState.bufferSurfaceFrameTX;
1357 bufferSurfaceFrameTX != nullptr) {
1358 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1359 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1360 // being used for BufferSurfaceFrame, don't create a new one.
1361 return;
1362 }
1363 }
1364 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1365 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1366 // targeting different vsyncs).
1367 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1368 if (it == mCurrentState.bufferlessSurfaceFramesTX.end()) {
1369 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
1370 mCurrentState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
1371 } else {
1372 if (it->second->getPresentState() == PresentState::Presented) {
1373 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1374 // have been from previous vsync.
1375 it->second = createSurfaceFrameForTransaction(info, postTime);
1376 }
1377 }
1378}
1379
1380void Layer::addSurfaceFrameDroppedForBuffer(
1381 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001382 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001383 surfaceFrame->setPresentState(PresentState::Dropped);
1384 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1385}
1386
1387void Layer::addSurfaceFramePresentedForBuffer(
1388 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1389 nsecs_t currentLatchTime) {
1390 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1391 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1392 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1393 mLastLatchTime = currentLatchTime;
1394}
1395
1396std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1397 const FrameTimelineInfo& info, nsecs_t postTime) {
1398 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001399 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1400 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001401 mTransactionName,
1402 /*isBuffer*/ false);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001403 // For Transactions, the post time is considered to be both queue and acquire fence time.
1404 surfaceFrame->setActualQueueTime(postTime);
1405 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001406 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1407 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001408 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001409 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001410 onSurfaceFrameCreated(surfaceFrame);
1411 return surfaceFrame;
1412}
1413
1414std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1415 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1416 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001417 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001418 getSequence(), mName, debugName,
1419 /*isBuffer*/ true);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001420 // For buffers, acquire fence time will set during latch.
1421 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001422 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1423 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001424 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001425 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001426 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1427 onSurfaceFrameCreated(surfaceFrame);
1428 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001429}
1430
Ady Abraham60e42ea2020-03-09 19:17:31 -07001431Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1432 const auto frameRate = getDrawingState().frameRate;
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001433 if (frameRate.rate.isValid() || frameRate.type == FrameRateCompatibility::NoVote) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001434 return frameRate;
1435 }
1436
Ady Abrahamf467f892020-07-31 16:01:53 -07001437 // This layer doesn't have a frame rate. Check if its ancestors have a vote
1438 if (sp<Layer> parent = getParent(); parent) {
1439 if (const auto parentFrameRate = parent->getFrameRateForLayerTree();
1440 parentFrameRate.rate.isValid()) {
1441 return parentFrameRate;
1442 }
1443 }
1444
1445 // This layer and its ancestors don't have a frame rate. If one of successors
1446 // has a vote, return a NoVote for successors to set the vote
Ady Abraham60e42ea2020-03-09 19:17:31 -07001447 if (getDrawingState().treeHasFrameRateVote) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001448 return {Fps(0.0f), FrameRateCompatibility::NoVote};
Ady Abraham60e42ea2020-03-09 19:17:31 -07001449 }
1450
1451 return frameRate;
Steven Thomas3172e202020-01-06 19:25:30 -08001452}
1453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454// ----------------------------------------------------------------------------
1455// pageflip handling...
1456// ----------------------------------------------------------------------------
1457
Robert Carr1f0a16a2016-10-24 16:27:39 -07001458bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001459 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001460 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001461 if (parent != nullptr && parent->isHiddenByPolicy()) {
1462 return true;
1463 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001464 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1465 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1466 if (zOrderRelativeOf != nullptr) {
1467 if (zOrderRelativeOf->isHiddenByPolicy()) {
1468 return true;
1469 }
1470 }
1471 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001472 return s.flags & layer_state_t::eLayerHidden;
1473}
1474
David Sodman41fdfc92017-11-06 16:09:56 -08001475uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001476 // TODO: should we do something special if mSecure is set?
1477 if (mProtectedByApp) {
1478 // need a hardware-protected path to external video sink
1479 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001480 }
Riley Andrews03414a12014-07-01 14:22:59 -07001481 if (mPotentialCursor) {
1482 usage |= GraphicBuffer::USAGE_CURSOR;
1483 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001484 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001485 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001486}
1487
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001488void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1489 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1490 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001491 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001492
Vishnu Nair6213bd92020-05-08 17:42:25 -07001493 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001494}
1495
Mathias Agopian13127d82013-03-05 17:47:11 -08001496// ----------------------------------------------------------------------------
1497// debugging
1498// ----------------------------------------------------------------------------
1499
Marissa Wall61c58622018-07-18 10:12:20 -07001500// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001501LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001502 using namespace std::string_literals;
1503
Kalle Raitaa099a242017-01-11 11:17:29 -08001504 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001505 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001506 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001507 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001508 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001509 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001510 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001511
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001512 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001513 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001514 info.mLayerStack = getLayerStack();
chaviw4e765532021-04-30 12:11:39 -05001515 info.mX = ds.transform.tx();
1516 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001517 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001518 info.mWidth = ds.width;
1519 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001520 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001521 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001522 info.mFlags = ds.flags;
1523 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001524 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001525 info.mMatrix[0][0] = ds.transform[0][0];
1526 info.mMatrix[0][1] = ds.transform[0][1];
1527 info.mMatrix[1][0] = ds.transform[1][0];
1528 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001529 {
chaviwd62d3062019-09-04 14:48:02 -07001530 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001531 if (buffer != 0) {
1532 info.mActiveBufferWidth = buffer->getWidth();
1533 info.mActiveBufferHeight = buffer->getHeight();
1534 info.mActiveBufferStride = buffer->getStride();
1535 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001536 } else {
1537 info.mActiveBufferWidth = 0;
1538 info.mActiveBufferHeight = 0;
1539 info.mActiveBufferStride = 0;
1540 info.mActiveBufferFormat = 0;
1541 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001542 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001543 info.mNumQueuedFrames = getQueuedFrameCount();
1544 info.mRefreshPending = isBufferLatched();
1545 info.mIsOpaque = isOpaque(ds);
1546 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001547 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001548 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001549}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001550
Yiwei Zhang5434a782018-12-05 18:06:32 -08001551void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001552 result.append(kDumpTableRowLength, '-');
1553 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001554 result.append(" Layer name\n");
1555 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001556 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001557 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001558 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001559 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001560 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001561 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1562 result.append(kDumpTableRowLength, '-');
1563 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001564}
1565
1566std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1567 switch (compatibility) {
1568 case FrameRateCompatibility::Default:
1569 return "Default";
1570 case FrameRateCompatibility::ExactOrMultiple:
1571 return "ExactOrMultiple";
1572 case FrameRateCompatibility::NoVote:
1573 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001574 case FrameRateCompatibility::Exact:
1575 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001576 }
Dan Stozae22aec72016-08-01 13:20:59 -07001577}
1578
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001579void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1580 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001581 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001582 return;
1583 }
1584
Yiwei Zhang5434a782018-12-05 18:06:32 -08001585 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001586 if (mName.length() > 77) {
1587 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001588 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001589 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001590 shortened.append(mName, mName.length() - 36);
1591 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001592 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001593 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001594 }
1595
Yiwei Zhang5434a782018-12-05 18:06:32 -08001596 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001597
Alec Mourib416efd2018-09-06 21:01:59 +00001598 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001599 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001600
Chia-I Wu1e043612018-03-01 09:45:09 -08001601 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001602 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001603 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001604 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001605 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001606 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001607 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001608 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1609 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001610 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001611 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001612 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001613 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001614 const auto frameRate = getFrameRateForLayerTree();
1615 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1616 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1617 frameRateCompatibilityString(frameRate.type).c_str(),
1618 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001619 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001620 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001621 }
Dan Stozae22aec72016-08-01 13:20:59 -07001622
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001623 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1624 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1625
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001626 result.append(kDumpTableRowLength, '-');
1627 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001628}
Dan Stozae22aec72016-08-01 13:20:59 -07001629
Yiwei Zhang5434a782018-12-05 18:06:32 -08001630void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001631 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001632}
1633
Svetoslavd85084b2014-03-20 10:28:31 -07001634void Layer::clearFrameStats() {
1635 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001636}
1637
Jamie Gennis6547ff42013-07-16 20:12:42 -07001638void Layer::logFrameStats() {
1639 mFrameTracker.logAndResetStats(mName);
1640}
1641
Svetoslavd85084b2014-03-20 10:28:31 -07001642void Layer::getFrameStats(FrameStats* outStats) const {
1643 mFrameTracker.getStats(outStats);
1644}
1645
Yiwei Zhang5434a782018-12-05 18:06:32 -08001646void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001647 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001648 Mutex::Autolock lock(mFrameEventHistoryMutex);
1649 mFrameEventHistory.checkFencesForCompletion();
1650 mFrameEventHistory.dump(result);
1651}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001652
Vishnu Nair0f085c62019-08-30 08:49:12 -07001653void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001654 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1655 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001656}
1657
Brian Anderson5ea5e592016-12-01 16:54:33 -08001658void Layer::onDisconnect() {
1659 Mutex::Autolock lock(mFrameEventHistoryMutex);
1660 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001661 const int32_t layerId = getSequence();
1662 mFlinger->mTimeStats->onDestroy(layerId);
1663 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001664}
1665
Brian Anderson3890c392016-07-25 12:48:08 -07001666void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001667 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001668 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001669 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Alec Mouri9a29e672020-09-14 12:39:14 -07001670 getName().c_str(), mOwnerUid, newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001671 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1672 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001673 }
1674
Brian Andersond6927fb2016-07-23 23:37:30 -07001675 Mutex::Autolock lock(mFrameEventHistoryMutex);
1676 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001677 // If there are any unsignaled fences in the aquire timeline at this
1678 // point, the previously queued frame hasn't been latched yet. Go ahead
1679 // and try to get the signal time here so the syscall is taken out of
1680 // the main thread's critical path.
1681 mAcquireTimeline.updateSignalTimes();
1682 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001683 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001684 mFrameEventHistory.addQueue(*newTimestamps);
1685 }
1686
Brian Anderson3890c392016-07-25 12:48:08 -07001687 if (outDelta) {
1688 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001689 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001690}
Dan Stozae77c7662016-05-13 11:37:28 -07001691
Chia-I Wu98f1c102017-05-30 14:54:08 -07001692size_t Layer::getChildrenCount() const {
1693 size_t count = 0;
1694 for (const sp<Layer>& child : mCurrentChildren) {
1695 count += 1 + child->getChildrenCount();
1696 }
1697 return count;
1698}
1699
Robert Carr1f0a16a2016-10-24 16:27:39 -07001700void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001701 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001702 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001703
Robert Carr1f0a16a2016-10-24 16:27:39 -07001704 mCurrentChildren.add(layer);
1705 layer->setParent(this);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001706 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001707}
1708
1709ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001710 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001711 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001712
Robert Carr1323c952019-01-28 18:13:27 -08001713 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001714 const auto removeResult = mCurrentChildren.remove(layer);
1715
1716 updateTreeHasFrameRateVote();
1717 layer->updateTreeHasFrameRateVote();
1718
1719 return removeResult;
1720}
1721
Robert Carr15eae092018-03-23 13:43:53 -07001722void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001723 for (const sp<Layer>& child : mDrawingChildren) {
1724 child->mDrawingParent = newParent;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001725 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001726 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001727 }
1728}
1729
chaviwf1961f72017-09-18 16:41:07 -07001730bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001731 sp<Layer> newParent;
1732 if (newParentHandle != nullptr) {
1733 auto handle = static_cast<Handle*>(newParentHandle.get());
1734 newParent = handle->owner.promote();
1735 if (newParent == nullptr) {
1736 ALOGE("Unable to promote Layer handle");
1737 return false;
1738 }
1739 if (newParent == this) {
1740 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1741 return false;
1742 }
1743 }
1744
chaviwf1961f72017-09-18 16:41:07 -07001745 sp<Layer> parent = getParent();
1746 if (parent != nullptr) {
1747 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001748 }
1749
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001750 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001751 newParent->addChild(this);
1752 if (!newParent->isRemovedFromCurrentState()) {
1753 addToCurrentState();
1754 } else {
1755 onRemovedFromCurrentState();
1756 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001757 } else {
1758 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001759 }
1760
chaviw06178942017-07-27 10:25:59 -07001761 return true;
1762}
1763
Peiyong Lind3788632018-09-18 16:01:31 -07001764bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001765 static const mat4 identityMatrix = mat4();
1766
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001767 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001768 return false;
1769 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001770 ++mCurrentState.sequence;
1771 mCurrentState.colorTransform = matrix;
1772 mCurrentState.hasColorTransform = matrix != identityMatrix;
1773 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001774 setTransactionFlags(eTransactionNeeded);
1775 return true;
1776}
1777
chaviwf66724d2018-11-28 16:35:21 -08001778mat4 Layer::getColorTransform() const {
1779 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1780 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1781 colorTransform = parent->getColorTransform() * colorTransform;
1782 }
1783 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001784}
1785
1786bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001787 bool hasColorTransform = getDrawingState().hasColorTransform;
1788 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1789 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1790 }
1791 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001792}
1793
Chia-I Wu11481472018-05-04 10:43:19 -07001794bool Layer::isLegacyDataSpace() const {
1795 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001796 return !(getDataSpace() &
1797 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1798 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001799}
1800
Robert Carr1f0a16a2016-10-24 16:27:39 -07001801void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001802 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001803}
1804
chaviw301b1d82019-11-06 13:15:09 -08001805int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1806 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1807 const State& state = useDrawing ? mDrawingState : mCurrentState;
1808 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809}
1810
Robert Carr1c5481e2019-07-01 14:42:27 -07001811bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001812 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001813 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001814 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001815}
1816
David Sodman41fdfc92017-11-06 16:09:56 -08001817__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001818 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001819 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1820 "makeTraversalList received invalid stateSet");
1821 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1822 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001823 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001824
Robert Carr29abff82017-12-04 13:51:20 -08001825 if (state.zOrderRelatives.size() == 0) {
1826 *outSkipRelativeZUsers = true;
1827 return children;
1828 }
1829
chaviwfd462612018-05-31 16:11:27 -07001830 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001831 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001832 sp<Layer> strongRelative = weakRelative.promote();
1833 if (strongRelative != nullptr) {
1834 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001835 }
1836 }
1837
Dan Stoza412903f2017-04-27 13:42:17 -07001838 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001839 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001840 continue;
1841 }
Robert Carrdb66e622017-04-10 16:55:57 -07001842 traverse.add(child);
1843 }
1844
1845 return traverse;
1846}
1847
Robert Carr1f0a16a2016-10-24 16:27:39 -07001848/**
Robert Carrdb66e622017-04-10 16:55:57 -07001849 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001850 */
Dan Stoza412903f2017-04-27 13:42:17 -07001851void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001852 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1853 // produce a new list for traversing, including our relatives, and not including our children
1854 // who are relatives of another surface. In the case that there are no relative Z,
1855 // makeTraversalList returns our children directly to avoid significant overhead.
1856 // However in this case we need to take the responsibility for filtering children which
1857 // are relatives of another surface here.
1858 bool skipRelativeZUsers = false;
1859 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001860
Robert Carr1f0a16a2016-10-24 16:27:39 -07001861 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001862 for (; i < list.size(); i++) {
1863 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001864 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1865 continue;
1866 }
1867
chaviw301b1d82019-11-06 13:15:09 -08001868 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001869 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001870 }
Dan Stoza412903f2017-04-27 13:42:17 -07001871 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001872 }
Robert Carr29abff82017-12-04 13:51:20 -08001873
Dan Stoza412903f2017-04-27 13:42:17 -07001874 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001875 for (; i < list.size(); i++) {
1876 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001877
Robert Carr29abff82017-12-04 13:51:20 -08001878 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1879 continue;
1880 }
Dan Stoza412903f2017-04-27 13:42:17 -07001881 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001882 }
1883}
1884
1885/**
Robert Carrdb66e622017-04-10 16:55:57 -07001886 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001887 */
Dan Stoza412903f2017-04-27 13:42:17 -07001888void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1889 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001890 // See traverseInZOrder for documentation.
1891 bool skipRelativeZUsers = false;
1892 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001893
Robert Carr1f0a16a2016-10-24 16:27:39 -07001894 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001895 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001896 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001897
1898 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1899 continue;
1900 }
1901
chaviw301b1d82019-11-06 13:15:09 -08001902 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001903 break;
1904 }
Dan Stoza412903f2017-04-27 13:42:17 -07001905 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001906 }
Dan Stoza412903f2017-04-27 13:42:17 -07001907 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001908 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001909 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001910
1911 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1912 continue;
1913 }
1914
Dan Stoza412903f2017-04-27 13:42:17 -07001915 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001916 }
1917}
1918
Edgar Arriaga844fa672020-01-16 14:21:42 -08001919void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1920 visitor(this);
1921 const LayerVector& children =
1922 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1923 for (const sp<Layer>& child : children) {
1924 child->traverse(state, visitor);
1925 }
1926}
1927
chaviw4b129c22018-04-09 16:19:43 -07001928LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1929 const std::vector<Layer*>& layersInTree) {
1930 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1931 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001932 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1933 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001934 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001935
chaviwfd462612018-05-31 16:11:27 -07001936 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001937 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1938 sp<Layer> strongRelative = weakRelative.promote();
1939 // Only add relative layers that are also descendents of the top most parent of the tree.
1940 // If a relative layer is not a descendent, then it should be ignored.
1941 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1942 traverse.add(strongRelative);
1943 }
1944 }
1945
1946 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001947 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001948 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1949 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1950 // the child here since it won't be added later as a relative.
1951 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1952 childState.zOrderRelativeOf.promote().get())) {
1953 continue;
1954 }
1955 traverse.add(child);
1956 }
1957
1958 return traverse;
1959}
1960
1961void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1962 LayerVector::StateSet stateSet,
1963 const LayerVector::Visitor& visitor) {
1964 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001965
1966 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001967 for (; i < list.size(); i++) {
1968 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001969 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001970 break;
1971 }
chaviw4b129c22018-04-09 16:19:43 -07001972 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001973 }
chaviw4b129c22018-04-09 16:19:43 -07001974
chaviwa76b2712017-09-20 12:02:26 -07001975 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001976 for (; i < list.size(); i++) {
1977 const auto& relative = list[i];
1978 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001979 }
1980}
1981
chaviw4b129c22018-04-09 16:19:43 -07001982std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1983 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1984 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1985
1986 std::vector<Layer*> layersInTree = {this};
1987 for (size_t i = 0; i < children.size(); i++) {
1988 const auto& child = children[i];
1989 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1990 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1991 }
1992
1993 return layersInTree;
1994}
1995
1996void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1997 const LayerVector::Visitor& visitor) {
1998 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1999 std::sort(layersInTree.begin(), layersInTree.end());
2000 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2001}
2002
Peiyong Linefefaac2018-08-17 12:27:51 -07002003ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002004 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002005}
2006
chaviw13fdc492017-06-27 12:40:18 -07002007half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002008 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002009
chaviw13fdc492017-06-27 12:40:18 -07002010 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2011 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002012}
Robert Carr6452f122017-03-21 10:41:29 -07002013
Vishnu Nair6213bd92020-05-08 17:42:25 -07002014ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
2015 ui::Transform::RotationFlags fixedTransformHint = mCurrentState.fixedTransformHint;
2016 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
2017 return fixedTransformHint;
2018 }
2019 const auto& p = mCurrentParent.promote();
2020 if (!p) return fixedTransformHint;
2021 return p->getFixedTransformHint();
2022}
2023
chaviw13fdc492017-06-27 12:40:18 -07002024half4 Layer::getColor() const {
2025 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002026 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002027}
Robert Carr6452f122017-03-21 10:41:29 -07002028
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002029int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01002030 const auto& p = mDrawingParent.promote();
2031
2032 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2033 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002034}
2035
Galia Peychevae0acf382021-04-12 21:22:34 +02002036const std::vector<BlurRegion> Layer::getBlurRegions() const {
2037 auto regionsCopy(getDrawingState().blurRegions);
2038 int layerAlpha = getAlpha();
2039 for (auto& region : regionsCopy) {
2040 region.alpha = region.alpha * layerAlpha;
2041 }
2042 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07002043}
2044
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002045Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2046 const auto& p = mDrawingParent.promote();
2047 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00002048 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002049 if (parentState.radius > 0) {
2050 ui::Transform t = getActiveTransform(getDrawingState());
2051 t = t.inverse();
2052 parentState.cropRect = t.transform(parentState.cropRect);
2053 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2054 // but a transform matrix can define horizontal and vertical scales.
2055 // Let's take the average between both of them and pass into the shader, practically we
2056 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07002057 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
2058 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
2059 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002060 return parentState;
2061 }
2062 }
2063 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00002064 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
2065 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07002066 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002067}
2068
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002069renderengine::ShadowSettings Layer::getShadowSettings(const Rect& layerStackRect) const {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002070 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
2071
2072 // Shift the spot light x-position to the middle of the display and then
2073 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002074 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002075 state.lightPos.y -= mScreenBounds.top;
2076
2077 state.length = mEffectiveShadowRadius;
2078 return state;
2079}
2080
Robert Carr1f0a16a2016-10-24 16:27:39 -07002081void Layer::commitChildList() {
2082 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2083 const auto& child = mCurrentChildren[i];
2084 child->commitChildList();
2085 }
2086 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002087 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002088}
2089
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002090static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2091 if (weakBinderHandle == nullptr) {
2092 return nullptr;
2093 }
2094 sp<IBinder> binderHandle = weakBinderHandle.promote();
2095 if (binderHandle == nullptr) {
2096 return nullptr;
2097 }
2098 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2099 if (handle == nullptr) {
2100 return nullptr;
2101 }
2102 return handle->owner;
2103}
2104
Robert Carr720e5062018-07-30 17:45:14 -07002105void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002106 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002107 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002108 mCurrentState.modified = true;
2109 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002110 setTransactionFlags(eTransactionNeeded);
2111}
2112
Alec Mouri6b9e9912020-01-21 10:50:24 -08002113LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002114 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08002115 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002116 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002117 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2118
Vishnu Nair60db8c02020-04-02 11:55:16 -07002119 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2120 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002121 if (display) {
2122 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002123 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2124 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002125 }
2126
chaviw08f3cb22020-01-13 13:17:21 -08002127 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002128 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002129 }
chaviw6d89e2d2020-01-14 14:42:01 -08002130
2131 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002132}
2133
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002134void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002135 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002136 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002137
2138 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002139
chaviwd62d3062019-09-04 14:48:02 -07002140 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002141 if (buffer != nullptr) {
2142 LayerProtoHelper::writeToProto(buffer,
2143 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002144 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002145 layerInfo->mutable_buffer_transform());
2146 }
2147 layerInfo->set_invalidate(contentDirty);
2148 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002149 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002150 layerInfo->set_queued_frames(getQueuedFrameCount());
2151 layerInfo->set_refresh_pending(isBufferLatched());
2152 layerInfo->set_curr_frame(mCurrentFrameNumber);
2153 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2154
2155 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002156 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002157 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2158 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2159 [&]() { return layerInfo->mutable_position(); });
2160 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002161 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002162 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002163 [&]() { return layerInfo->mutable_visible_region(); });
2164 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002165 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2166 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002167
2168 if (hasColorTransform()) {
2169 LayerProtoHelper::writeToProto(getColorTransform(),
2170 layerInfo->mutable_color_transform());
2171 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002172 }
2173
Vishnu Nair60db8c02020-04-02 11:55:16 -07002174 LayerProtoHelper::writeToProto(mSourceBounds,
2175 [&]() { return layerInfo->mutable_source_bounds(); });
2176 LayerProtoHelper::writeToProto(mScreenBounds,
2177 [&]() { return layerInfo->mutable_screen_bounds(); });
2178 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2179 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2180 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002181}
2182
2183void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002184 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002185 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2186 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002187 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002188
chaviw766c9c52021-02-10 17:36:47 -08002189 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002190
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002191 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2192 layerInfo->set_id(sequence);
2193 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002194 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002195
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002196 for (const auto& child : children) {
2197 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002198 }
chaviw1d044282017-09-27 12:19:28 -07002199
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002200 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2201 sp<Layer> strongRelative = weakRelative.promote();
2202 if (strongRelative != nullptr) {
2203 layerInfo->add_relatives(strongRelative->sequence);
2204 }
chaviwadc40c22018-07-10 16:57:27 -07002205 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002206
2207 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2208 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002209
2210 layerInfo->set_layer_stack(getLayerStack());
2211 layerInfo->set_z(state.z);
2212
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002213 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2214 [&]() {
2215 return layerInfo->mutable_requested_position();
2216 });
2217
chaviw766c9c52021-02-10 17:36:47 -08002218 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002219 [&]() { return layerInfo->mutable_size(); });
2220
chaviw766c9c52021-02-10 17:36:47 -08002221 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002222
2223 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002224
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002225
2226 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2227 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2228 LayerProtoHelper::writeToProto(state.color,
2229 [&]() { return layerInfo->mutable_requested_color(); });
2230 layerInfo->set_flags(state.flags);
2231
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002232 LayerProtoHelper::writeToProto(requestedTransform,
2233 layerInfo->mutable_requested_transform());
2234
2235 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2236 if (parent != nullptr) {
2237 layerInfo->set_parent(parent->sequence);
2238 } else {
2239 layerInfo->set_parent(-1);
2240 }
2241
2242 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2243 if (zOrderRelativeOf != nullptr) {
2244 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2245 } else {
2246 layerInfo->set_z_order_relative_of(-1);
2247 }
chaviw08f3cb22020-01-13 13:17:21 -08002248
2249 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002250
2251 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002252 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002253
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002254 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw4c34a092020-07-08 11:30:06 -07002255 InputWindowInfo info;
2256 if (useDrawing) {
[1;3C2b9fc252021-02-04 16:16:50 -08002257 info = fillInputInfo({nullptr});
chaviw4c34a092020-07-08 11:30:06 -07002258 } else {
2259 info = state.inputInfo;
2260 }
2261
2262 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002263 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002264 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002265
2266 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2267 auto protoMap = layerInfo->mutable_metadata();
2268 for (const auto& entry : state.metadata.mMap) {
2269 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2270 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002271 }
chaviw1d044282017-09-27 12:19:28 -07002272}
2273
Robert Carr2e102c92018-10-23 12:11:15 -07002274bool Layer::isRemovedFromCurrentState() const {
2275 return mRemovedFromCurrentState;
2276}
2277
chaviw39d01472021-04-08 14:26:24 -05002278ui::Transform Layer::getInputTransform() const {
2279 return getTransform();
2280}
2281
2282Rect Layer::getInputBounds() const {
2283 return getCroppedBufferSize(getDrawingState());
2284}
2285
[1;3C2b9fc252021-02-04 16:16:50 -08002286void Layer::fillInputFrameInfo(InputWindowInfo& info, const ui::Transform& toPhysicalDisplay) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002287 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002288 // If this is a portal window, set the touchableRegion to the layerBounds.
2289 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002290 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002291 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002292 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002293 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002294 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002295
chaviw7e72caf2020-12-02 16:50:43 -08002296 if (!layerBounds.isValid()) {
2297 // If the layer bounds is empty, set the frame to empty and clear the transform
2298 info.frameLeft = 0;
2299 info.frameTop = 0;
2300 info.frameRight = 0;
2301 info.frameBottom = 0;
2302 info.transform.reset();
2303 return;
2304 }
2305
chaviw39d01472021-04-08 14:26:24 -05002306 ui::Transform layerToDisplay = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002307 // Transform that takes window coordinates to unrotated display coordinates
2308 ui::Transform t = toPhysicalDisplay * layerToDisplay;
chaviw7e72caf2020-12-02 16:50:43 -08002309 int32_t xSurfaceInset = info.surfaceInset;
2310 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002311 // Bring screenBounds into unrotated space
2312 Rect screenBounds = toPhysicalDisplay.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002313
chaviw1ff3d1e2020-07-01 15:53:47 -07002314 const float xScale = t.getScaleX();
2315 const float yScale = t.getScaleY();
2316 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002317 xSurfaceInset = std::round(xSurfaceInset * xScale);
2318 ySurfaceInset = std::round(ySurfaceInset * yScale);
2319 }
2320
Peiyong Lin74861ad2020-10-14 11:55:33 -07002321 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002322 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002323
2324 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002325 xSurfaceInset = (xSurfaceInset >= 0)
2326 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2327 : 0;
2328 ySurfaceInset = (ySurfaceInset >= 0)
2329 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2330 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002331
Robert Carra7242302020-09-01 18:26:29 -07002332 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2333 // in the overflow scenario?
2334 {
2335 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002336 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2337 transformedLayerBounds.left = tmp;
2338 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2339 transformedLayerBounds.right = tmp;
2340 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2341 transformedLayerBounds.top = tmp;
2342 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2343 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002344 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002345
chaviw44a6d2b2020-09-08 17:14:16 -07002346 // Compute the correct transform to send to input. This will allow it to transform the
2347 // input coordinates from display space into window space. Therefore, it needs to use the
2348 // final layer frame to create the inverse transform. Since surface insets are added later,
2349 // along with the overflow, the best way to ensure we get the correct transform is to use
2350 // the final frame calculated.
2351 // 1. Take the original transform set on the window and get the inverse transform. This is
2352 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002353 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002354 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2355 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2356 // be the new translation values for the transform.
2357 // 3. Update the translation of the original transform to the new translation values.
2358 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2359 // window space.
2360 ui::Transform inverseTransform = t.inverse();
2361 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2362 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002363 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002364 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002365 info.transform = inputTransform.inverse();
2366
chaviw39cfa2e2020-11-04 14:19:02 -08002367 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2368 // The frame should be the area the user sees on screen since it's used for occlusion
2369 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002370 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2371 info.frameLeft = transformedLayerBounds.left;
2372 info.frameTop = transformedLayerBounds.top;
2373 info.frameRight = transformedLayerBounds.right;
2374 info.frameBottom = transformedLayerBounds.bottom;
2375
Vishnu Nair8033a492018-12-05 07:27:23 -08002376 // Position the touchable region relative to frame screen location and restrict it to frame
2377 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002378 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002379}
2380
[1;3C2b9fc252021-02-04 16:16:50 -08002381InputWindowInfo Layer::fillInputInfo(const sp<DisplayDevice>& display) {
chaviw7e72caf2020-12-02 16:50:43 -08002382 if (!hasInputInfo()) {
2383 mDrawingState.inputInfo.name = getName();
2384 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2385 mDrawingState.inputInfo.ownerPid = mOwnerPid;
2386 mDrawingState.inputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
2387 mDrawingState.inputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
2388 mDrawingState.inputInfo.displayId = getLayerStack();
2389 }
2390
2391 InputWindowInfo info = mDrawingState.inputInfo;
2392 info.id = sequence;
2393
2394 if (info.displayId == ADISPLAY_ID_NONE) {
2395 info.displayId = getLayerStack();
2396 }
2397
[1;3C2b9fc252021-02-04 16:16:50 -08002398 // Transform that goes from "logical(rotated)" display to physical/unrotated display.
2399 // This is for when inputflinger operates in physical display-space.
2400 ui::Transform toPhysicalDisplay;
2401 if (display) {
2402 toPhysicalDisplay = display->getTransform();
Evan Rosky84f07f02021-04-16 10:42:42 -07002403 info.displayWidth = display->getWidth();
2404 info.displayHeight = display->getHeight();
[1;3C2b9fc252021-02-04 16:16:50 -08002405 }
2406 fillInputFrameInfo(info, toPhysicalDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08002407
Robert Carr5dc426e2020-06-10 14:29:14 -07002408 // For compatibility reasons we let layers which can receive input
2409 // receive input before they have actually submitted a buffer. Because
2410 // of this we use canReceiveInput instead of isVisible to check the
2411 // policy-visibility, ignoring the buffer state. However for layers with
2412 // hasInputInfo()==false we can use the real visibility state.
2413 // We are just using these layers for occlusion detection in
2414 // InputDispatcher, and obviously if they aren't visible they can't occlude
2415 // anything.
2416 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002417 info.alpha = getAlpha();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002418
2419 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2420 if (info.replaceTouchableRegionWithCrop) {
2421 if (cropLayer == nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002422 info.touchableRegion = Region(toPhysicalDisplay.transform(Rect{mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002423 } else {
[1;3C2b9fc252021-02-04 16:16:50 -08002424 info.touchableRegion =
2425 Region(toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002426 }
2427 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002428 info.touchableRegion = info.touchableRegion.intersect(
2429 toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002430 }
2431
chaviwaf87b3e2019-10-01 16:59:28 -07002432 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2433 // touches from going outside the cloned area.
2434 if (isClone()) {
2435 sp<Layer> clonedRoot = getClonedRoot();
2436 if (clonedRoot != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002437 Rect rect = toPhysicalDisplay.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002438 info.touchableRegion = info.touchableRegion.intersect(rect);
2439 }
2440 }
2441
Robert Carr720e5062018-07-30 17:45:14 -07002442 return info;
2443}
2444
chaviwaf87b3e2019-10-01 16:59:28 -07002445sp<Layer> Layer::getClonedRoot() {
2446 if (mClonedChild != nullptr) {
2447 return this;
2448 }
2449 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2450 return nullptr;
2451 }
2452 return mDrawingParent.promote()->getClonedRoot();
2453}
2454
Robert Carredd13602020-04-13 17:24:34 -07002455bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002456 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002457}
2458
chaviw3e727cd2019-01-31 13:41:05 -08002459bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002460 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002461}
2462
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002463compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002464 const DisplayDevice* display) const {
2465 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002466 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002467}
2468
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002469Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2470 const auto outputLayer = findOutputLayerForDisplay(display);
2471 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002472}
2473
chaviwb4c6e582019-08-16 14:35:07 -07002474void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2475 // copy drawing state from cloned layer
2476 mDrawingState = clonedFrom->mDrawingState;
2477 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002478}
chaviw74b03172019-08-19 11:09:03 -07002479
2480void Layer::updateMirrorInfo() {
2481 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2482 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2483 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2484 // that case, we want to delete the reference to the clone since we want it to get
2485 // destroyed. The root, this layer, will still be around since the client can continue
2486 // to hold a reference, but no cloned layers will be displayed.
2487 mClonedChild = nullptr;
2488 return;
2489 }
2490
2491 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2492 // If the real layer exists and is in current state, add the clone as a child of the root.
2493 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2494 // copied to drawingState for the root layer. So the clonedChild is always removed from
2495 // drawingState and then needs to be added back each traversal.
2496 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2497 addChildToDrawing(mClonedChild);
2498 }
2499
2500 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2501 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2502 mClonedChild->updateClonedRelatives(clonedLayersMap);
2503}
2504
2505void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2506 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2507 // to the clone. If the real layer is no longer alive, continue traversing the children
2508 // since we may be able to pull out other children that are still alive.
2509 if (isClonedFromAlive()) {
2510 sp<Layer> clonedFrom = getClonedFrom();
2511 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002512 clonedLayersMap.emplace(clonedFrom, this);
2513 }
2514
2515 // The clone layer may have children in drawingState since they may have been created and
2516 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2517 // that already exist, since we can just re-use them.
2518 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2519 // not updated in the regular traversal. They are skipped since the root will lose the
2520 // reference to them when it copies its currentChildren to drawing.
2521 for (sp<Layer>& child : mDrawingChildren) {
2522 child->updateClonedDrawingState(clonedLayersMap);
2523 }
2524}
2525
2526void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2527 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2528 mDrawingChildren.clear();
2529
2530 if (!isClonedFromAlive()) {
2531 return;
2532 }
2533
2534 sp<Layer> clonedFrom = getClonedFrom();
2535 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2536 if (child == mirrorRoot) {
2537 // This is to avoid cyclical mirroring.
2538 continue;
2539 }
2540 sp<Layer> clonedChild = clonedLayersMap[child];
2541 if (clonedChild == nullptr) {
2542 clonedChild = child->createClone();
2543 clonedLayersMap[child] = clonedChild;
2544 }
2545 addChildToDrawing(clonedChild);
2546 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2547 }
2548}
2549
chaviwaf87b3e2019-10-01 16:59:28 -07002550void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2551 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2552 if (cropLayer != nullptr) {
2553 if (clonedLayersMap.count(cropLayer) == 0) {
2554 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2555 // self as crop layer to avoid going outside bounds.
2556 mDrawingState.touchableRegionCrop = this;
2557 } else {
2558 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2559 mDrawingState.touchableRegionCrop = clonedCropLayer;
2560 }
2561 }
2562 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2563 // WM or the client.
Michael Wright44753b12020-07-08 13:48:11 +01002564 mDrawingState.inputInfo.flags &= ~InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002565}
2566
2567void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002568 mDrawingState.zOrderRelativeOf = nullptr;
2569 mDrawingState.zOrderRelatives.clear();
2570
2571 if (!isClonedFromAlive()) {
2572 return;
2573 }
2574
chaviwaf87b3e2019-10-01 16:59:28 -07002575 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002576 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002577 const sp<Layer>& relative = relativeWeak.promote();
2578 if (clonedLayersMap.count(relative) > 0) {
2579 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002580 mDrawingState.zOrderRelatives.add(clonedRelative);
2581 }
2582 }
2583
2584 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2585 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2586 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2587 // still traverse the children, but the layer with the missing relativeOf will not be shown
2588 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002589 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2590 if (clonedLayersMap.count(relativeOf) > 0) {
2591 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002592 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2593 }
2594
chaviwaf87b3e2019-10-01 16:59:28 -07002595 updateClonedInputInfo(clonedLayersMap);
2596
chaviw74b03172019-08-19 11:09:03 -07002597 for (sp<Layer>& child : mDrawingChildren) {
2598 child->updateClonedRelatives(clonedLayersMap);
2599 }
2600}
2601
2602void Layer::addChildToDrawing(const sp<Layer>& layer) {
2603 mDrawingChildren.add(layer);
2604 layer->mDrawingParent = this;
2605}
2606
Steven Thomas62a4cf82020-01-31 12:04:03 -08002607Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2608 switch (compatibility) {
2609 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2610 return FrameRateCompatibility::Default;
2611 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2612 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002613 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2614 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002615 default:
2616 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2617 return FrameRateCompatibility::Default;
2618 }
2619}
2620
Marin Shalamanovc5986772021-03-16 16:09:49 +01002621scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2622 switch (strategy) {
2623 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2624 return Seamlessness::OnlySeamless;
2625 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2626 return Seamlessness::SeamedAndSeamless;
2627 default:
2628 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2629 return Seamlessness::Default;
2630 }
2631}
2632
chaviwc5676c62020-09-18 15:01:04 -07002633bool Layer::getPrimaryDisplayOnly() const {
2634 const State& s(mDrawingState);
2635 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2636 return true;
2637 }
2638
2639 sp<Layer> parent = mDrawingParent.promote();
2640 return parent == nullptr ? false : parent->getPrimaryDisplayOnly();
2641}
2642
Mathias Agopian13127d82013-03-05 17:47:11 -08002643// ---------------------------------------------------------------------------
2644
Marin Shalamanov46084422020-10-13 12:33:42 +02002645std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2646 return stream << "{rate=" << rate.rate
2647 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002648 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002649}
2650
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002651}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002652
2653#if defined(__gl_h_)
2654#error "don't include gl/gl.h in this file"
2655#endif
2656
2657#if defined(__gl2_h_)
2658#error "don't include gl2/gl2.h in this file"
2659#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002660
2661// TODO(b/129481165): remove the #pragma below and fix conversion issues
2662#pragma clang diagnostic pop // ignored "-Wconversion"