blob: b45f2bcd924dc39754df47bf4942634f5d5d457a [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;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700136
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700137 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
138 // Set an invalid color so there is no color fill.
139 mCurrentState.color.r = -1.0_hf;
140 mCurrentState.color.g = -1.0_hf;
141 mCurrentState.color.b = -1.0_hf;
142 }
143
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700144 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700146
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800147 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700148 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800149 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200150 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700151
Vishnu Nair0f085c62019-08-30 08:49:12 -0700152 mCallingPid = args.callingPid;
153 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700154
155 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
156 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
157 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700158 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700159 } else {
160 // A create layer request from a non system request cannot specify the owner uid
161 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700162 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700163 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800164}
165
166void Layer::onFirstRef() {
167 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700168}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700170Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800171 sp<Client> c(mClientRef.promote());
172 if (c != 0) {
173 c->detachLayer(this);
174 }
175
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000176 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700177 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700178}
179
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700180LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
181 uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700182 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700183 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700184 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700185 w(w),
186 h(h),
187 flags(flags),
188 metadata(std::move(metadata)) {
189 IPCThreadState* ipc = IPCThreadState::self();
190 callingPid = ipc->getCallingPid();
191 callingUid = ipc->getCallingUid();
192}
193
Mathias Agopian13127d82013-03-05 17:47:11 -0800194// ---------------------------------------------------------------------------
195// callbacks
196// ---------------------------------------------------------------------------
197
David Sodmaneb085e02017-10-05 18:49:04 -0700198/*
199 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
200 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800201 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700202 */
David Sodmaneb085e02017-10-05 18:49:04 -0700203void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800204
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700205void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
206 if (mCurrentState.zOrderRelativeOf == nullptr) {
207 return;
208 }
Robert Carr2e102c92018-10-23 12:11:15 -0700209
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700210 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
211 if (strongRelative == nullptr) {
212 setZOrderRelativeOf(nullptr);
213 return;
214 }
215
216 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
217 strongRelative->removeZOrderRelative(this);
218 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800219 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700220 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700221}
222
223void Layer::removeFromCurrentState() {
224 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000225
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800226 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700227}
Chia-I Wu38512252017-05-17 14:36:16 -0700228
chaviw68d4dab2020-06-08 15:07:32 -0700229sp<Layer> Layer::getRootLayer() {
230 sp<Layer> parent = getParent();
231 if (parent == nullptr) {
232 return this;
233 }
234 return parent->getRootLayer();
235}
236
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700237void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700238 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
239 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700240 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700241
242 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700243 layer->removeFromCurrentState();
244 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700245 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700246}
247
chaviw61626f22018-11-15 16:26:27 -0800248void Layer::addToCurrentState() {
249 mRemovedFromCurrentState = false;
250
251 for (const auto& child : mCurrentChildren) {
252 child->addToCurrentState();
253 }
254}
255
Mathias Agopian13127d82013-03-05 17:47:11 -0800256// ---------------------------------------------------------------------------
257// set-up
258// ---------------------------------------------------------------------------
259
chaviw13fdc492017-06-27 12:40:18 -0700260bool Layer::getPremultipledAlpha() const {
261 return mPremultipliedAlpha;
262}
263
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700264sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800265 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700266 if (mGetHandleCalled) {
267 ALOGE("Get handle called twice" );
268 return nullptr;
269 }
270 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700271 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800272}
273
274// ---------------------------------------------------------------------------
275// h/w composer set-up
276// ---------------------------------------------------------------------------
277
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700278static Rect reduce(const Rect& win, const Region& exclude) {
279 if (CC_LIKELY(exclude.isEmpty())) {
280 return win;
281 }
282 if (exclude.isRect()) {
283 return win.reduce(exclude.getBounds());
284 }
285 return Region(win).subtract(exclude).getBounds();
286}
287
Dan Stoza80d61162017-12-20 15:57:52 -0800288static FloatRect reduce(const FloatRect& win, const Region& exclude) {
289 if (CC_LIKELY(exclude.isEmpty())) {
290 return win;
291 }
292 // Convert through Rect (by rounding) for lack of FloatRegion
293 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
294}
295
Vishnu Nair4351ad52019-02-11 14:13:02 -0800296Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800297 if (!reduceTransparentRegion) {
298 return Rect{mScreenBounds};
299 }
300
301 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800302 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800303 // Transform to screen space.
304 bounds = t.transform(bounds);
305 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700306}
307
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000309 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800310 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700311}
312
Vishnu Nairf0c28512019-02-08 12:40:28 -0800313FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
314 // Subtract the transparent region and snap to the bounds.
315 return reduce(mBounds, activeTransparentRegion);
316}
317
Vishnu Nairc652ff82019-03-15 12:48:54 -0700318ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800319 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
320 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700321 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700322 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700323 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800324 }
325
Marissa Wall290ad082019-03-06 13:23:47 -0800326 // If the layer is a buffer state layer, the active width and height
327 // could be infinite. In that case, return the effective transform.
328 const uint32_t activeWidth = getActiveWidth(getDrawingState());
329 const uint32_t activeHeight = getActiveHeight(getDrawingState());
330 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700331 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800332 }
333
chaviwd62d3062019-09-04 14:48:02 -0700334 int bufferWidth = getBuffer()->getWidth();
335 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700336
chaviw4244e032019-09-04 11:27:49 -0700337 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700338 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800339 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700340
Marissa Wall290ad082019-03-06 13:23:47 -0800341 float sx = activeWidth / static_cast<float>(bufferWidth);
342 float sy = activeHeight / static_cast<float>(bufferHeight);
343
Vishnu Nair4351ad52019-02-11 14:13:02 -0800344 ui::Transform extraParentScaling;
345 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700346 return extraParentScaling;
347}
348
349ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
350 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
351 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700352 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700353 return mEffectiveTransform;
354 }
355 return mEffectiveTransform * bufferScaleTransform;
356}
357
358FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
359 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
360 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700361 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700362 return mBounds;
363 }
364 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800365}
366
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700367void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
368 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800369 const State& s(getDrawingState());
370
371 // Calculate effective layer transform
372 mEffectiveTransform = parentTransform * getActiveTransform(s);
373
374 // Transform parent bounds to layer space
375 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
376
Vishnu Nairc652ff82019-03-15 12:48:54 -0700377 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800378 mSourceBounds = computeSourceBounds(parentBounds);
379
380 // Calculate bounds by croping diplay frame with layer crop and parent bounds
381 FloatRect bounds = mSourceBounds;
382 const Rect layerCrop = getCrop(s);
383 if (!layerCrop.isEmpty()) {
384 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
385 }
386 bounds = bounds.intersect(parentBounds);
387
388 mBounds = bounds;
389 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700390
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700391 // Use the layer's own shadow radius if set. Otherwise get the radius from
392 // parent.
393 if (s.shadowRadius > 0.f) {
394 mEffectiveShadowRadius = s.shadowRadius;
395 } else {
396 mEffectiveShadowRadius = parentShadowRadius;
397 }
398
399 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
400 // don't pass it to its children.
401 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
402
Vishnu Nairc652ff82019-03-15 12:48:54 -0700403 // Add any buffer scaling to the layer's children.
404 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800405 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700406 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700407 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800408 }
409}
410
Vishnu Nair60356342018-11-13 13:00:45 -0800411Rect Layer::getCroppedBufferSize(const State& s) const {
412 Rect size = getBufferSize(s);
413 Rect crop = getCrop(s);
414 if (!crop.isEmpty() && size.isValid()) {
415 size.intersect(crop, &size);
416 } else if (!crop.isEmpty()) {
417 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700418 }
Vishnu Nair60356342018-11-13 13:00:45 -0800419 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800420}
421
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700422void Layer::setupRoundedCornersCropCoordinates(Rect win,
423 const FloatRect& roundedCornersCrop) const {
424 // Translate win by the rounded corners rect coordinates, to have all values in
425 // layer coordinate space.
426 win.left -= roundedCornersCrop.left;
427 win.right -= roundedCornersCrop.left;
428 win.top -= roundedCornersCrop.top;
429 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700430}
431
Lloyd Piquede196652020-01-22 17:29:58 -0800432void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800433 const auto& drawingState{getDrawingState()};
434 const uint32_t layerStack = getLayerStack();
435 const auto alpha = static_cast<float>(getAlpha());
436 const bool opaque = isOpaque(drawingState);
437 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
438
439 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
440 if (!opaque || alpha != 1.0f) {
441 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
442 : Hwc2::IComposerClient::BlendMode::COVERAGE;
443 }
444
Lloyd Piquede196652020-01-22 17:29:58 -0800445 auto* compositionState = editCompositionState();
446 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800447 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800448 compositionState->internalOnly = getPrimaryDisplayOnly();
449 compositionState->isVisible = isVisible();
450 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
451 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800452
Lloyd Piquede196652020-01-22 17:29:58 -0800453 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800454 contentDirty = false;
455
Lloyd Piquede196652020-01-22 17:29:58 -0800456 compositionState->geomLayerBounds = mBounds;
457 compositionState->geomLayerTransform = getTransform();
458 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
459 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800460
Lloyd Piquede196652020-01-22 17:29:58 -0800461 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
462 compositionState->alpha = alpha;
463 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700464 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500465 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800466}
467
Lloyd Piquede196652020-01-22 17:29:58 -0800468void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800469 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800470
Lloyd Piquea83776c2019-01-29 18:42:32 -0800471 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
472 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700473 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400474 if (parent.get()) {
475 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800476 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
477 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700478 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800479 type = parentType;
480 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700481 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400482 }
483
Lloyd Piquede196652020-01-22 17:29:58 -0800484 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700485
Lloyd Piquede196652020-01-22 17:29:58 -0800486 compositionState->geomBufferSize = getBufferSize(drawingState);
487 compositionState->geomContentCrop = getBufferCrop();
488 compositionState->geomCrop = getCrop(drawingState);
489 compositionState->geomBufferTransform = getBufferTransform();
490 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
491 compositionState->geomUsesSourceCrop = usesSourceCrop();
492 compositionState->isSecure = isSecure();
493
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800494 compositionState->metadata.clear();
495 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
496 for (const auto& [key, mandatory] : supportedMetadata) {
497 const auto& genericLayerMetadataCompatibilityMap =
498 mFlinger->getGenericLayerMetadataKeyMap();
499 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
500 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
501 continue;
502 }
503 const uint32_t id = compatIter->second;
504
505 auto it = drawingState.metadata.mMap.find(id);
506 if (it == std::end(drawingState.metadata.mMap)) {
507 continue;
508 }
509
510 compositionState->metadata
511 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
512 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800513}
David Sodmanba340492018-08-05 21:51:33 -0700514
Lloyd Piquede196652020-01-22 17:29:58 -0800515void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800516 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800517 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800518
Lloyd Piquede196652020-01-22 17:29:58 -0800519 compositionState->forceClientComposition = false;
520
521 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
522 compositionState->dataspace = getDataSpace();
523 compositionState->colorTransform = getColorTransform();
524 compositionState->colorTransformIsIdentity = !hasColorTransform();
525 compositionState->surfaceDamage = surfaceDamageRegion;
526 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800527
528 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700529
Lloyd Piquede196652020-01-22 17:29:58 -0800530 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800531 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800532
533 // Force client composition for special cases known only to the front-end.
John Reckcdb4ed72021-02-04 13:39:33 -0500534 if (isHdrY410() || usesRoundedCorners || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500535 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800536 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800537 }
538}
539
Lloyd Piquede196652020-01-22 17:29:58 -0800540void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800541 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800542 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800543
544 // Apply the layer's transform, followed by the display's global transform
545 // Here we're guaranteed that the layer's transform preserves rects
546 Rect win = getCroppedBufferSize(drawingState);
547 // Subtract the transparent region and snap to the bounds
548 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
549 Rect frame(getTransform().transform(bounds));
550
Lloyd Piquede196652020-01-22 17:29:58 -0800551 compositionState->cursorFrame = frame;
552}
553
554sp<compositionengine::LayerFE> Layer::asLayerFE() const {
555 return const_cast<compositionengine::LayerFE*>(
556 static_cast<const compositionengine::LayerFE*>(this));
557}
558
559sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
560 return nullptr;
561}
562
563compositionengine::LayerFECompositionState* Layer::editCompositionState() {
564 return nullptr;
565}
566
567const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
568 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800569}
570
Lloyd Piquef16688f2019-02-19 17:47:57 -0800571bool Layer::onPreComposition(nsecs_t) {
572 return false;
573}
574
Lloyd Piquede196652020-01-22 17:29:58 -0800575void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800576 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800577
Lloyd Piquec6687342019-03-07 21:34:57 -0800578 switch (subset) {
579 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800580 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800581 break;
582
583 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800584 prepareBasicGeometryCompositionState();
585 prepareGeometryCompositionState();
586 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800587 break;
588
589 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800590 preparePerFrameCompositionState();
591 break;
592
593 case StateSubset::Cursor:
594 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800595 break;
596 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800597}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700598
Lloyd Piquea83776c2019-01-29 18:42:32 -0800599const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700600 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800601}
602
Mathias Agopian13127d82013-03-05 17:47:11 -0800603// ---------------------------------------------------------------------------
604// drawing...
605// ---------------------------------------------------------------------------
606
Vishnu Nair9b079a22020-01-21 14:36:08 -0800607std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100608 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800609 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800610 return {};
611 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800612
Vishnu Nair4351ad52019-02-11 14:13:02 -0800613 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000614 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800615
616 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800617 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700618 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000619
620 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800621 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000622 }
623
624 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800625 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
626 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000627
Lloyd Piquef16688f2019-02-19 17:47:57 -0800628 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700629 layerSettings.sourceDataspace = getDataSpace();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100630 if (!targetSettings.disableBlurs) {
631 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
632 layerSettings.blurRegions = getBlurRegions();
633 }
John Reckcdb4ed72021-02-04 13:39:33 -0500634 layerSettings.stretchEffect = getDrawingState().stretchEffect;
Ana Krulec6eab17a2020-12-09 15:52:36 -0800635 // Record the name of the layer for debugging further down the stack.
636 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800637 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800638}
639
Vishnu Nair9b079a22020-01-21 14:36:08 -0800640std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareShadowClientComposition(
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200641 const LayerFE::LayerSettings& casterLayerSettings, const Rect& layerStackRect,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800642 ui::Dataspace outputDataspace) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200643 renderengine::ShadowSettings shadow = getShadowSettings(layerStackRect);
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800644 if (shadow.length <= 0.f) {
645 return {};
646 }
647
648 const float casterAlpha = casterLayerSettings.alpha;
649 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
650 casterLayerSettings.source.buffer.isOpaque);
651
Vishnu Nair9b079a22020-01-21 14:36:08 -0800652 compositionengine::LayerFE::LayerSettings shadowLayer = casterLayerSettings;
653
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800654 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800655 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800656
657 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
658 // Otherwise the generated shadow will only be shown around the casting layer.
659 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
660 shadowLayer.shadow.ambientColor *= casterAlpha;
661 shadowLayer.shadow.spotColor *= casterAlpha;
662 shadowLayer.sourceDataspace = outputDataspace;
663 shadowLayer.source.buffer.buffer = nullptr;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800664 shadowLayer.source.buffer.fence = nullptr;
665 shadowLayer.frameNumber = 0;
666 shadowLayer.bufferId = 0;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800667 shadowLayer.name = getName();
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800668
669 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
670 return {};
671 }
672
673 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800674 const FloatRect& cornerRadiusCropRect = shadowLayer.geometry.roundedCornersCrop;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800675 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
676
677 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
678 // radius accordingly.
679 if (casterCornerRadius > 0.f) {
680 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
681 std::abs(cornerRadiusCropRect.left - casterRect.left));
682 if (cropRectOffset > casterCornerRadius) {
683 casterCornerRadius = 0;
684 } else {
685 casterCornerRadius -= cropRectOffset;
686 }
687 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
688 }
689
690 return shadowLayer;
691}
692
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800693void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
694 bool blackout) const {
695 layerSettings.source.buffer.buffer = nullptr;
696 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
697 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800698 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800699 layerSettings.frameNumber = 0;
700
701 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
702 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800703 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800704}
705
706std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
707 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
708 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
709 prepareClientComposition(targetSettings);
710 // Nothing to render.
711 if (!layerSettings) {
712 return {};
713 }
714
715 // HWC requests to clear this layer.
716 if (targetSettings.clearContent) {
717 prepareClearClientComposition(*layerSettings, false /* blackout */);
718 return {*layerSettings};
719 }
720
721 std::optional<compositionengine::LayerFE::LayerSettings> shadowSettings =
722 prepareShadowClientComposition(*layerSettings, targetSettings.viewport,
723 targetSettings.dataspace);
724 // There are no shadows to render.
725 if (!shadowSettings) {
726 return {*layerSettings};
727 }
728
729 // If the layer casts a shadow but the content casting the shadow is occluded, skip
730 // composing the non-shadow content and only draw the shadows.
731 if (targetSettings.realContentIsVisible) {
732 return {*shadowSettings, *layerSettings};
733 }
734
735 return {*shadowSettings};
736}
737
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700738Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
739 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800740 if (outputLayer == nullptr) {
741 return Hwc2::IComposerClient::Composition::INVALID;
742 }
743 if (outputLayer->getState().hwc) {
744 return (*outputLayer->getState().hwc).hwcCompositionType;
745 } else {
746 return Hwc2::IComposerClient::Composition::CLIENT;
747 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748}
749
Mathias Agopian13127d82013-03-05 17:47:11 -0800750// ----------------------------------------------------------------------------
751// local state
752// ----------------------------------------------------------------------------
753
David Sodman41fdfc92017-11-06 16:09:56 -0800754bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800755 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700756 if (s.flags & layer_state_t::eLayerSecure) {
757 return true;
758 }
759
760 const auto p = mDrawingParent.promote();
761 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700762}
763
Mathias Agopian13127d82013-03-05 17:47:11 -0800764// ----------------------------------------------------------------------------
765// transaction
766// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800767
Marissa Wall61c58622018-07-18 10:12:20 -0700768uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000769 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770
Marissa Wall61c58622018-07-18 10:12:20 -0700771 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
772 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700773
David Sodmaneb085e02017-10-05 18:49:04 -0700774 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700775 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000776 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800777 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
778 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
779 " requested={ wh={%4u,%4u} }}\n"
780 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
781 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700782 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700783 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
chaviw25714502021-02-11 10:01:08 -0800784 stateToCommit->crop.left, stateToCommit->crop.top, stateToCommit->crop.right,
785 stateToCommit->crop.bottom, stateToCommit->crop.getWidth(),
786 stateToCommit->crop.getHeight(), stateToCommit->requested_legacy.w,
787 stateToCommit->requested_legacy.h, s.active_legacy.w, s.active_legacy.h,
788 s.crop.left, s.crop.top, s.crop.right, s.crop.bottom, s.crop.getWidth(),
789 s.crop.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700791
Robert Carre392b552017-09-19 12:16:05 -0700792 // Don't let Layer::doTransaction update the drawing state
793 // if we have a pending resize, unless we are in fixed-size mode.
794 // the drawing state will be updated only once we receive a buffer
795 // with the correct size.
796 //
797 // In particular, we want to make sure the clip (which is part
798 // of the geometry state) is latched together with the size but is
799 // latched immediately when no resizing is involved.
800 //
801 // If a sideband stream is attached, however, we want to skip this
802 // optimization so that transactions aren't missed when a buffer
803 // never arrives
804 //
805 // In the case that we don't have a buffer we ignore other factors
806 // and avoid entering the resizePending state. At a high level the
807 // resizePending state is to avoid applying the state of the new buffer
808 // to the old buffer. However in the state where we don't have an old buffer
809 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700810 const bool resizePending =
811 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
812 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700813 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700814 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800815 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700816 flags |= eDontUpdateGeometryState;
817 }
818 }
819
Robert Carr7bf247e2017-05-18 14:02:49 -0700820 // Here we apply various requested geometry states, depending on our
821 // latching configuration. See Layer.h for a detailed discussion of
822 // how geometry latching is controlled.
823 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000824 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700825
Robert Carr7bf247e2017-05-18 14:02:49 -0700826 // There is an awkward asymmetry in the handling of the crop states in the position
827 // states, as can be seen below. Largely this arises from position and transform
828 // being stored in the same data structure while having different latching rules.
829 // b/38182305
830 //
Marissa Wall61c58622018-07-18 10:12:20 -0700831 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700832 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700833 editCurrentState.active_legacy = editCurrentState.requested_legacy;
834 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800835 }
836
Marissa Wall61c58622018-07-18 10:12:20 -0700837 return flags;
838}
839
840uint32_t Layer::doTransaction(uint32_t flags) {
841 ATRACE_CALL();
842
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800843 if (mChildrenChanged) {
844 flags |= eVisibleRegion;
845 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700846 }
847
Robert Carr0758e5d2021-03-11 22:15:04 -0800848 // TODO: This is unfortunate.
849 mCurrentStateModified = mCurrentState.modified;
850 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700851
Robert Carr0758e5d2021-03-11 22:15:04 -0800852 flags = doTransactionResize(flags, &mCurrentState);
Marissa Wall61c58622018-07-18 10:12:20 -0700853
Alec Mourib416efd2018-09-06 21:01:59 +0000854 const State& s(getDrawingState());
Robert Carr0758e5d2021-03-11 22:15:04 -0800855 State& c(getCurrentState());
Marissa Wall61c58622018-07-18 10:12:20 -0700856
857 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800858 // invalidate and recompute the visible regions if needed
859 flags |= Layer::eVisibleRegion;
860 }
861
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700862 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800863 // invalidate and recompute the visible regions if needed
864 flags |= eVisibleRegion;
865 this->contentDirty = true;
866
867 // we may use linear filtering, if the matrix scales us
Marin Shalamanov043ba292020-09-10 13:35:20 +0200868 mNeedsFiltering = getActiveTransform(c).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800869 }
870
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800871 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700872 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700874 }
875
chaviw47dbd692020-10-26 17:50:12 -0700876 // Add the callbacks from the drawing state into the current state. This is so when the current
877 // state gets copied to drawing, we don't lose the callback handles that are still in drawing.
878 for (auto& handle : s.callbackHandles) {
879 c.callbackHandles.push_back(handle);
880 }
881
Vishnu Nair1506b182021-02-22 14:35:15 -0800882 // Allow BufferStateLayer to release any unlatched buffers in drawing state.
Alec Mouri617752f2021-04-15 16:27:01 +0000883 bufferMayChange(c.buffer);
Vishnu Nair1506b182021-02-22 14:35:15 -0800884
Mathias Agopian13127d82013-03-05 17:47:11 -0800885 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700886 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800887 mCurrentState.callbackHandles = {};
Robert Carra1257842020-01-31 13:48:28 -0800888
Mathias Agopian13127d82013-03-05 17:47:11 -0800889 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890}
891
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000892void Layer::commitTransaction(State& stateToCommit) {
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000893 if (auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
Alec Mouri617752f2021-04-15 16:27:01 +0000894 mDrawingState.buffer != stateToCommit.buffer && bufferSurfaceFrame != nullptr &&
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000895 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
896 // If the previous buffer was committed but not latched (refreshPending - happens during
897 // back to back invalidates), it gets silently dropped here. Mark the corresponding
898 // SurfaceFrame as dropped to prevent it from getting stuck in the pending classification
899 // list.
900 addSurfaceFrameDroppedForBuffer(bufferSurfaceFrame);
901 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800902 mDrawingState = stateToCommit;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000903
904 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
905 // bufferSurfaceFrameTX will be presented in latchBuffer.
906 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
907 if (surfaceFrame->getPresentState() != PresentState::Presented) {
908 // With applyPendingStates, we could end up having presented surfaceframes from previous
909 // states
910 surfaceFrame->setPresentState(PresentState::Presented);
911 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
912 }
913 }
914 // Clear the surfaceFrames from the old state now that it has been copied into DrawingState.
915 stateToCommit.bufferSurfaceFrameTX.reset();
916 stateToCommit.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700917}
918
Mathias Agopian13127d82013-03-05 17:47:11 -0800919uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800920 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800921}
922
923uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800924 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800925}
926
chaviw214c89d2019-09-04 16:03:53 -0700927bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800928 if (mCurrentState.requested_legacy.transform.tx() == x &&
929 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800930 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700932
933 // We update the requested and active position simultaneously because
934 // we want to apply the position portion of the transform matrix immediately,
935 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800936 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700937 // Here we directly update the active state
938 // unlike other setters, because we store it within
939 // the transform, but use different latching rules.
940 // b/38182305
941 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700942
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800943 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800944 setTransactionFlags(eTransactionNeeded);
945 return true;
946}
Robert Carr82364e32016-05-15 11:27:47 -0700947
Robert Carr1f0a16a2016-10-24 16:27:39 -0700948bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
949 ssize_t idx = mCurrentChildren.indexOf(childLayer);
950 if (idx < 0) {
951 return false;
952 }
953 if (childLayer->setLayer(z)) {
954 mCurrentChildren.removeAt(idx);
955 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800956 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700957 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800958 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700959}
960
Robert Carr503c7042017-09-27 15:06:08 -0700961bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
962 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
963 ssize_t idx = mCurrentChildren.indexOf(childLayer);
964 if (idx < 0) {
965 return false;
966 }
967 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
968 mCurrentChildren.removeAt(idx);
969 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800970 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700971 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800972 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700973}
974
Robert Carrae060832016-11-28 10:51:00 -0800975bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800976 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
977 mCurrentState.sequence++;
978 mCurrentState.z = z;
979 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700980
981 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800982 if (mCurrentState.zOrderRelativeOf != nullptr) {
983 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700984 if (strongRelative != nullptr) {
985 strongRelative->removeZOrderRelative(this);
986 }
chaviw606e5cf2019-03-01 10:12:10 -0800987 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700988 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800989 setTransactionFlags(eTransactionNeeded);
990 return true;
991}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700992
Robert Carrdb66e622017-04-10 16:55:57 -0700993void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800994 mCurrentState.zOrderRelatives.remove(relative);
995 mCurrentState.sequence++;
996 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700997 setTransactionFlags(eTransactionNeeded);
998}
999
1000void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001001 mCurrentState.zOrderRelatives.add(relative);
1002 mCurrentState.modified = true;
1003 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001004 setTransactionFlags(eTransactionNeeded);
1005}
1006
chaviw606e5cf2019-03-01 10:12:10 -08001007void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1008 mCurrentState.zOrderRelativeOf = relativeOf;
1009 mCurrentState.sequence++;
1010 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -07001011 mCurrentState.isRelativeOf = relativeOf != nullptr;
1012
chaviw606e5cf2019-03-01 10:12:10 -08001013 setTransactionFlags(eTransactionNeeded);
1014}
1015
Robert Carr503d2bd2017-12-04 15:49:47 -08001016bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001017 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1018 if (handle == nullptr) {
1019 return false;
1020 }
1021 sp<Layer> relative = handle->owner.promote();
1022 if (relative == nullptr) {
1023 return false;
1024 }
1025
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001026 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1027 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001028 return false;
1029 }
1030
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001031 mCurrentState.sequence++;
1032 mCurrentState.modified = true;
1033 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001034
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001035 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001036 if (oldZOrderRelativeOf != nullptr) {
1037 oldZOrderRelativeOf->removeZOrderRelative(this);
1038 }
chaviw606e5cf2019-03-01 10:12:10 -08001039 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001040 relative->addZOrderRelative(this);
1041
1042 setTransactionFlags(eTransactionNeeded);
1043
1044 return true;
1045}
1046
Mathias Agopian13127d82013-03-05 17:47:11 -08001047bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001048 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001049 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001050 mCurrentState.requested_legacy.w = w;
1051 mCurrentState.requested_legacy.h = h;
1052 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001054
1055 // record the new size, from this point on, when the client request
1056 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001057 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001058 return true;
1059}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001060bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001061 if (mCurrentState.color.a == alpha) return false;
1062 mCurrentState.sequence++;
1063 mCurrentState.color.a = alpha;
1064 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 setTransactionFlags(eTransactionNeeded);
1066 return true;
1067}
chaviw13fdc492017-06-27 12:40:18 -07001068
Valerie Haudd0b7572019-01-29 14:59:27 -08001069bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1070 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001071 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001072 }
1073 mCurrentState.sequence++;
1074 mCurrentState.modified = true;
1075 setTransactionFlags(eTransactionNeeded);
1076
1077 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001078 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -08001079 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001080 std::string name = mName + "BackgroundColorLayer";
Vishnu Nairfa247b12020-02-11 08:58:26 -08001081 mCurrentState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001082 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1083 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001084
Valerie Haudd0b7572019-01-29 14:59:27 -08001085 // add to child list
1086 addChild(mCurrentState.bgColorLayer);
1087 mFlinger->mLayersAdded = true;
1088 // set up SF to handle added color layer
1089 if (isRemovedFromCurrentState()) {
1090 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1091 }
1092 mFlinger->setTransactionFlags(eTransactionNeeded);
1093 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1094 mCurrentState.bgColorLayer->reparent(nullptr);
1095 mCurrentState.bgColorLayer = nullptr;
1096 return true;
1097 }
1098
1099 mCurrentState.bgColorLayer->setColor(color);
1100 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1101 mCurrentState.bgColorLayer->setAlpha(alpha);
1102 mCurrentState.bgColorLayer->setDataspace(dataspace);
1103
chaviw13fdc492017-06-27 12:40:18 -07001104 return true;
1105}
1106
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001107bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001109
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001110 mCurrentState.sequence++;
1111 mCurrentState.cornerRadius = cornerRadius;
1112 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001113 setTransactionFlags(eTransactionNeeded);
1114 return true;
1115}
1116
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001117bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1118 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1119
1120 mCurrentState.sequence++;
1121 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1122 mCurrentState.modified = true;
1123 setTransactionFlags(eTransactionNeeded);
1124 return true;
1125}
1126
Robert Carrd4ae7f32018-06-07 16:10:57 -07001127bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1128 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001129 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001130 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1131
1132 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -08001133 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
1134 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -07001135 return false;
1136 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001137 mCurrentState.sequence++;
1138 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1139 matrix.dsdy);
1140 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001141 setTransactionFlags(eTransactionNeeded);
1142 return true;
1143}
Marissa Wall61c58622018-07-18 10:12:20 -07001144
Mathias Agopian13127d82013-03-05 17:47:11 -08001145bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001146 mCurrentState.requestedTransparentRegion_legacy = transparent;
1147 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001148 setTransactionFlags(eTransactionNeeded);
1149 return true;
1150}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001151
Lucas Dupinc3800b82020-10-02 16:24:48 -07001152bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
1153 mCurrentState.sequence++;
1154 mCurrentState.blurRegions = blurRegions;
1155 mCurrentState.modified = true;
1156 setTransactionFlags(eTransactionNeeded);
1157 return true;
1158}
1159
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001160bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001161 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1162 if (mCurrentState.flags == newFlags) return false;
1163 mCurrentState.sequence++;
1164 mCurrentState.flags = newFlags;
1165 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001166 setTransactionFlags(eTransactionNeeded);
1167 return true;
1168}
Robert Carr99e27f02016-06-16 15:18:02 -07001169
chaviw25714502021-02-11 10:01:08 -08001170bool Layer::setCrop(const Rect& crop) {
1171 if (mCurrentState.requestedCrop == crop) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001172 mCurrentState.sequence++;
chaviw25714502021-02-11 10:01:08 -08001173 mCurrentState.requestedCrop = crop;
1174 mCurrentState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001175
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001176 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001177 setTransactionFlags(eTransactionNeeded);
1178 return true;
1179}
Robert Carr8d5227b2017-03-16 15:41:03 -07001180
Evan Roskyef876c92019-01-25 17:46:06 -08001181bool Layer::setMetadata(const LayerMetadata& data) {
1182 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001183 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001184 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001185 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001186}
1187
Mathias Agopian13127d82013-03-05 17:47:11 -08001188bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001189 if (mCurrentState.layerStack == layerStack) return false;
1190 mCurrentState.sequence++;
1191 mCurrentState.layerStack = layerStack;
1192 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001193 setTransactionFlags(eTransactionNeeded);
1194 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001195}
1196
Peiyong Linc502cb72019-03-01 15:00:23 -08001197bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1198 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1199 return false;
1200 }
1201 mCurrentState.sequence++;
1202 mCurrentState.colorSpaceAgnostic = agnostic;
1203 mCurrentState.modified = true;
1204 setTransactionFlags(eTransactionNeeded);
1205 return true;
1206}
1207
Ana Krulecc84d09b2019-11-02 23:10:29 +01001208bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1209 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1210 mCurrentState.frameRateSelectionPriority = priority;
1211 mCurrentState.sequence++;
1212 mCurrentState.modified = true;
1213 setTransactionFlags(eTransactionNeeded);
1214 return true;
1215}
1216
1217int32_t Layer::getFrameRateSelectionPriority() const {
1218 // Check if layer has priority set.
1219 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1220 return mDrawingState.frameRateSelectionPriority;
1221 }
1222 // If not, search whether its parents have it set.
1223 sp<Layer> parent = getParent();
1224 if (parent != nullptr) {
1225 return parent->getFrameRateSelectionPriority();
1226 }
1227
1228 return Layer::PRIORITY_UNSET;
1229}
1230
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001231bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1232 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1233};
1234
Robert Carr1f0a16a2016-10-24 16:27:39 -07001235uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001236 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001237 if (p == nullptr) {
1238 return getDrawingState().layerStack;
1239 }
1240 return p->getLayerStack();
1241}
1242
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001243bool Layer::setShadowRadius(float shadowRadius) {
1244 if (mCurrentState.shadowRadius == shadowRadius) {
1245 return false;
1246 }
1247
1248 mCurrentState.sequence++;
1249 mCurrentState.shadowRadius = shadowRadius;
1250 mCurrentState.modified = true;
1251 setTransactionFlags(eTransactionNeeded);
1252 return true;
1253}
1254
Vishnu Nair6213bd92020-05-08 17:42:25 -07001255bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
1256 if (mCurrentState.fixedTransformHint == fixedTransformHint) {
1257 return false;
1258 }
1259
1260 mCurrentState.sequence++;
1261 mCurrentState.fixedTransformHint = fixedTransformHint;
1262 mCurrentState.modified = true;
1263 setTransactionFlags(eTransactionNeeded);
1264 return true;
1265}
1266
John Reckcdb4ed72021-02-04 13:39:33 -05001267bool Layer::setStretchEffect(const StretchEffect& effect) {
1268 StretchEffect temp = effect;
1269 temp.sanitize();
1270 if (mCurrentState.stretchEffect == temp) {
1271 return false;
1272 }
1273 mCurrentState.sequence++;
1274 mCurrentState.stretchEffect = temp;
1275 mCurrentState.modified = true;
1276 setTransactionFlags(eTransactionNeeded);
1277 return true;
1278}
1279
John Reckc00c6692021-02-16 11:37:33 -05001280StretchEffect Layer::getStretchEffect() const {
1281 if (mDrawingState.stretchEffect.hasEffect()) {
1282 return mDrawingState.stretchEffect;
1283 }
1284
1285 sp<Layer> parent = getParent();
1286 if (parent != nullptr) {
1287 auto effect = parent->getStretchEffect();
1288 if (effect.hasEffect()) {
1289 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1290 return effect;
1291 }
1292 }
1293 return StretchEffect{};
1294}
1295
Ady Abraham60e42ea2020-03-09 19:17:31 -07001296void Layer::updateTreeHasFrameRateVote() {
1297 const auto traverseTree = [&](const LayerVector::Visitor& visitor) {
1298 auto parent = getParent();
1299 while (parent) {
1300 visitor(parent.get());
1301 parent = parent->getParent();
1302 }
1303
1304 traverse(LayerVector::StateSet::Current, visitor);
1305 };
1306
1307 // update parents and children about the vote
Ady Abrahamf467f892020-07-31 16:01:53 -07001308 // First traverse the tree and count how many layers has votes. In addition
1309 // activate the layers in Scheduler's LayerHistory for it to check for changes
Ady Abraham60e42ea2020-03-09 19:17:31 -07001310 int layersWithVote = 0;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001311 traverseTree([&layersWithVote](Layer* layer) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001312 const auto layerVotedWithDefaultCompatibility =
1313 layer->mCurrentState.frameRate.rate.isValid() &&
Ady Abraham414e8b52020-05-21 15:36:04 -07001314 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Default;
1315 const auto layerVotedWithNoVote =
1316 layer->mCurrentState.frameRate.type == FrameRateCompatibility::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001317 const auto layerVotedWithExactCompatibility =
1318 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Exact;
Ady Abraham414e8b52020-05-21 15:36:04 -07001319
1320 // We do not count layers that are ExactOrMultiple for the same reason
1321 // we are allowing touch boost for those layers. See
1322 // RefreshRateConfigs::getBestRefreshRate for more details.
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001323 if (layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1324 layerVotedWithExactCompatibility) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001325 layersWithVote++;
1326 }
1327 });
1328
1329 // Now update the other layers
1330 bool transactionNeeded = false;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001331 traverseTree([layersWithVote, &transactionNeeded, this](Layer* layer) {
1332 const bool treeHasFrameRateVote = layersWithVote > 0;
1333 if (layer->mCurrentState.treeHasFrameRateVote != treeHasFrameRateVote) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001334 layer->mCurrentState.sequence++;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001335 layer->mCurrentState.treeHasFrameRateVote = treeHasFrameRateVote;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001336 layer->mCurrentState.modified = true;
1337 layer->setTransactionFlags(eTransactionNeeded);
1338 transactionNeeded = true;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001339
1340 mFlinger->mScheduler->recordLayerHistory(layer, systemTime(),
1341 LayerHistory::LayerUpdateType::SetFrameRate);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001342 }
1343 });
1344
1345 if (transactionNeeded) {
1346 mFlinger->setTransactionFlags(eTraversalNeeded);
1347 }
1348}
1349
Ady Abraham71c437d2020-01-31 15:56:57 -08001350bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001351 if (!mFlinger->useFrameRateApi) {
1352 return false;
1353 }
Steven Thomas3172e202020-01-06 19:25:30 -08001354 if (mCurrentState.frameRate == frameRate) {
1355 return false;
1356 }
1357
1358 mCurrentState.sequence++;
1359 mCurrentState.frameRate = frameRate;
1360 mCurrentState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001361
1362 updateTreeHasFrameRateVote();
1363
Steven Thomas3172e202020-01-06 19:25:30 -08001364 setTransactionFlags(eTransactionNeeded);
1365 return true;
1366}
1367
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001368void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1369 nsecs_t postTime) {
1370 mCurrentState.postTime = postTime;
1371
1372 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1373 // there are two transactions with the same token, the first one without a buffer and the
1374 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1375 // in that case.
1376 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1377 if (it != mCurrentState.bufferlessSurfaceFramesTX.end()) {
1378 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1379 mCurrentState.bufferSurfaceFrameTX = it->second;
1380 mCurrentState.bufferlessSurfaceFramesTX.erase(it);
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001381 mCurrentState.bufferSurfaceFrameTX->promoteToBuffer();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001382 mCurrentState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
1383 } else {
1384 mCurrentState.bufferSurfaceFrameTX =
1385 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1386 }
1387}
1388
1389void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1390 nsecs_t postTime) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001391 mCurrentState.frameTimelineInfo = info;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001392 mCurrentState.postTime = postTime;
1393 mCurrentState.modified = true;
1394 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001395
1396 if (const auto& bufferSurfaceFrameTX = mCurrentState.bufferSurfaceFrameTX;
1397 bufferSurfaceFrameTX != nullptr) {
1398 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1399 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1400 // being used for BufferSurfaceFrame, don't create a new one.
1401 return;
1402 }
1403 }
1404 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1405 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1406 // targeting different vsyncs).
1407 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1408 if (it == mCurrentState.bufferlessSurfaceFramesTX.end()) {
1409 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
1410 mCurrentState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
1411 } else {
1412 if (it->second->getPresentState() == PresentState::Presented) {
1413 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1414 // have been from previous vsync.
1415 it->second = createSurfaceFrameForTransaction(info, postTime);
1416 }
1417 }
1418}
1419
1420void Layer::addSurfaceFrameDroppedForBuffer(
1421 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001422 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001423 surfaceFrame->setPresentState(PresentState::Dropped);
1424 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1425}
1426
1427void Layer::addSurfaceFramePresentedForBuffer(
1428 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1429 nsecs_t currentLatchTime) {
1430 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1431 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1432 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1433 mLastLatchTime = currentLatchTime;
1434}
1435
1436std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1437 const FrameTimelineInfo& info, nsecs_t postTime) {
1438 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001439 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1440 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001441 mTransactionName,
1442 /*isBuffer*/ false);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001443 // For Transactions, the post time is considered to be both queue and acquire fence time.
1444 surfaceFrame->setActualQueueTime(postTime);
1445 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001446 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1447 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001448 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001449 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001450 onSurfaceFrameCreated(surfaceFrame);
1451 return surfaceFrame;
1452}
1453
1454std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1455 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1456 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001457 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001458 getSequence(), mName, debugName,
1459 /*isBuffer*/ true);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001460 // For buffers, acquire fence time will set during latch.
1461 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001462 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1463 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001464 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001465 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001466 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1467 onSurfaceFrameCreated(surfaceFrame);
1468 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001469}
1470
Ady Abraham60e42ea2020-03-09 19:17:31 -07001471Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1472 const auto frameRate = getDrawingState().frameRate;
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001473 if (frameRate.rate.isValid() || frameRate.type == FrameRateCompatibility::NoVote) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001474 return frameRate;
1475 }
1476
Ady Abrahamf467f892020-07-31 16:01:53 -07001477 // This layer doesn't have a frame rate. Check if its ancestors have a vote
1478 if (sp<Layer> parent = getParent(); parent) {
1479 if (const auto parentFrameRate = parent->getFrameRateForLayerTree();
1480 parentFrameRate.rate.isValid()) {
1481 return parentFrameRate;
1482 }
1483 }
1484
1485 // This layer and its ancestors don't have a frame rate. If one of successors
1486 // has a vote, return a NoVote for successors to set the vote
Ady Abraham60e42ea2020-03-09 19:17:31 -07001487 if (getDrawingState().treeHasFrameRateVote) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001488 return {Fps(0.0f), FrameRateCompatibility::NoVote};
Ady Abraham60e42ea2020-03-09 19:17:31 -07001489 }
1490
1491 return frameRate;
Steven Thomas3172e202020-01-06 19:25:30 -08001492}
1493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494// ----------------------------------------------------------------------------
1495// pageflip handling...
1496// ----------------------------------------------------------------------------
1497
Robert Carr1f0a16a2016-10-24 16:27:39 -07001498bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001499 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001500 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001501 if (parent != nullptr && parent->isHiddenByPolicy()) {
1502 return true;
1503 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001504 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1505 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1506 if (zOrderRelativeOf != nullptr) {
1507 if (zOrderRelativeOf->isHiddenByPolicy()) {
1508 return true;
1509 }
1510 }
1511 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001512 return s.flags & layer_state_t::eLayerHidden;
1513}
1514
David Sodman41fdfc92017-11-06 16:09:56 -08001515uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001516 // TODO: should we do something special if mSecure is set?
1517 if (mProtectedByApp) {
1518 // need a hardware-protected path to external video sink
1519 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001520 }
Riley Andrews03414a12014-07-01 14:22:59 -07001521 if (mPotentialCursor) {
1522 usage |= GraphicBuffer::USAGE_CURSOR;
1523 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001524 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001525 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001526}
1527
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001528void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1529 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1530 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001531 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001532
Vishnu Nair6213bd92020-05-08 17:42:25 -07001533 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001534}
1535
Mathias Agopian13127d82013-03-05 17:47:11 -08001536// ----------------------------------------------------------------------------
1537// debugging
1538// ----------------------------------------------------------------------------
1539
Marissa Wall61c58622018-07-18 10:12:20 -07001540// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001541LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001542 using namespace std::string_literals;
1543
Kalle Raitaa099a242017-01-11 11:17:29 -08001544 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001545 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001546 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001547 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001548 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001549 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001550 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001551
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001552 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001553 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001554 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001555 info.mX = ds.active_legacy.transform.tx();
1556 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001557 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001558 info.mWidth = ds.active_legacy.w;
1559 info.mHeight = ds.active_legacy.h;
chaviw25714502021-02-11 10:01:08 -08001560 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001561 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001562 info.mFlags = ds.flags;
1563 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001564 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001565 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1566 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1567 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1568 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001569 {
chaviwd62d3062019-09-04 14:48:02 -07001570 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001571 if (buffer != 0) {
1572 info.mActiveBufferWidth = buffer->getWidth();
1573 info.mActiveBufferHeight = buffer->getHeight();
1574 info.mActiveBufferStride = buffer->getStride();
1575 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001576 } else {
1577 info.mActiveBufferWidth = 0;
1578 info.mActiveBufferHeight = 0;
1579 info.mActiveBufferStride = 0;
1580 info.mActiveBufferFormat = 0;
1581 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001582 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001583 info.mNumQueuedFrames = getQueuedFrameCount();
1584 info.mRefreshPending = isBufferLatched();
1585 info.mIsOpaque = isOpaque(ds);
1586 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001587 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001588 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001589}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001590
Yiwei Zhang5434a782018-12-05 18:06:32 -08001591void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001592 result.append(kDumpTableRowLength, '-');
1593 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001594 result.append(" Layer name\n");
1595 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001596 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001597 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001598 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001599 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001600 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001601 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1602 result.append(kDumpTableRowLength, '-');
1603 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001604}
1605
1606std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1607 switch (compatibility) {
1608 case FrameRateCompatibility::Default:
1609 return "Default";
1610 case FrameRateCompatibility::ExactOrMultiple:
1611 return "ExactOrMultiple";
1612 case FrameRateCompatibility::NoVote:
1613 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001614 case FrameRateCompatibility::Exact:
1615 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001616 }
Dan Stozae22aec72016-08-01 13:20:59 -07001617}
1618
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001619void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1620 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001621 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001622 return;
1623 }
1624
Yiwei Zhang5434a782018-12-05 18:06:32 -08001625 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001626 if (mName.length() > 77) {
1627 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001628 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001629 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001630 shortened.append(mName, mName.length() - 36);
1631 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001632 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001633 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001634 }
1635
Yiwei Zhang5434a782018-12-05 18:06:32 -08001636 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001637
Alec Mourib416efd2018-09-06 21:01:59 +00001638 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001639 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001640
Chia-I Wu1e043612018-03-01 09:45:09 -08001641 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001642 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001643 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001644 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001645 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001646 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001647 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001648 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1649 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001650 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001651 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001652 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001653 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001654 const auto frameRate = getFrameRateForLayerTree();
1655 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1656 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1657 frameRateCompatibilityString(frameRate.type).c_str(),
1658 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001659 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001660 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001661 }
Dan Stozae22aec72016-08-01 13:20:59 -07001662
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001663 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1664 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1665
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001666 result.append(kDumpTableRowLength, '-');
1667 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001668}
Dan Stozae22aec72016-08-01 13:20:59 -07001669
Yiwei Zhang5434a782018-12-05 18:06:32 -08001670void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001671 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001672}
1673
Svetoslavd85084b2014-03-20 10:28:31 -07001674void Layer::clearFrameStats() {
1675 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001676}
1677
Jamie Gennis6547ff42013-07-16 20:12:42 -07001678void Layer::logFrameStats() {
1679 mFrameTracker.logAndResetStats(mName);
1680}
1681
Svetoslavd85084b2014-03-20 10:28:31 -07001682void Layer::getFrameStats(FrameStats* outStats) const {
1683 mFrameTracker.getStats(outStats);
1684}
1685
Yiwei Zhang5434a782018-12-05 18:06:32 -08001686void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001687 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001688 Mutex::Autolock lock(mFrameEventHistoryMutex);
1689 mFrameEventHistory.checkFencesForCompletion();
1690 mFrameEventHistory.dump(result);
1691}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001692
Vishnu Nair0f085c62019-08-30 08:49:12 -07001693void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001694 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1695 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001696}
1697
Brian Anderson5ea5e592016-12-01 16:54:33 -08001698void Layer::onDisconnect() {
1699 Mutex::Autolock lock(mFrameEventHistoryMutex);
1700 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001701 const int32_t layerId = getSequence();
1702 mFlinger->mTimeStats->onDestroy(layerId);
1703 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001704}
1705
Brian Anderson3890c392016-07-25 12:48:08 -07001706void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001707 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001708 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001709 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Alec Mouri9a29e672020-09-14 12:39:14 -07001710 getName().c_str(), mOwnerUid, newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001711 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1712 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001713 }
1714
Brian Andersond6927fb2016-07-23 23:37:30 -07001715 Mutex::Autolock lock(mFrameEventHistoryMutex);
1716 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001717 // If there are any unsignaled fences in the aquire timeline at this
1718 // point, the previously queued frame hasn't been latched yet. Go ahead
1719 // and try to get the signal time here so the syscall is taken out of
1720 // the main thread's critical path.
1721 mAcquireTimeline.updateSignalTimes();
1722 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001723 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001724 mFrameEventHistory.addQueue(*newTimestamps);
1725 }
1726
Brian Anderson3890c392016-07-25 12:48:08 -07001727 if (outDelta) {
1728 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001729 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001730}
Dan Stozae77c7662016-05-13 11:37:28 -07001731
Chia-I Wu98f1c102017-05-30 14:54:08 -07001732size_t Layer::getChildrenCount() const {
1733 size_t count = 0;
1734 for (const sp<Layer>& child : mCurrentChildren) {
1735 count += 1 + child->getChildrenCount();
1736 }
1737 return count;
1738}
1739
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001741 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001742 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001743
Robert Carr1f0a16a2016-10-24 16:27:39 -07001744 mCurrentChildren.add(layer);
1745 layer->setParent(this);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001746 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001747}
1748
1749ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001750 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001751 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001752
Robert Carr1323c952019-01-28 18:13:27 -08001753 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001754 const auto removeResult = mCurrentChildren.remove(layer);
1755
1756 updateTreeHasFrameRateVote();
1757 layer->updateTreeHasFrameRateVote();
1758
1759 return removeResult;
1760}
1761
Robert Carr15eae092018-03-23 13:43:53 -07001762void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001763 for (const sp<Layer>& child : mDrawingChildren) {
1764 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001765 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001766 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1767 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001768 }
1769}
1770
chaviwf1961f72017-09-18 16:41:07 -07001771bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001772 sp<Layer> newParent;
1773 if (newParentHandle != nullptr) {
1774 auto handle = static_cast<Handle*>(newParentHandle.get());
1775 newParent = handle->owner.promote();
1776 if (newParent == nullptr) {
1777 ALOGE("Unable to promote Layer handle");
1778 return false;
1779 }
1780 if (newParent == this) {
1781 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1782 return false;
1783 }
1784 }
1785
chaviwf1961f72017-09-18 16:41:07 -07001786 sp<Layer> parent = getParent();
1787 if (parent != nullptr) {
1788 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001789 }
1790
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001791 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001792 newParent->addChild(this);
1793 if (!newParent->isRemovedFromCurrentState()) {
1794 addToCurrentState();
1795 } else {
1796 onRemovedFromCurrentState();
1797 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001798 } else {
1799 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001800 }
1801
chaviw06178942017-07-27 10:25:59 -07001802 return true;
1803}
1804
Peiyong Lind3788632018-09-18 16:01:31 -07001805bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001806 static const mat4 identityMatrix = mat4();
1807
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001808 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001809 return false;
1810 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001811 ++mCurrentState.sequence;
1812 mCurrentState.colorTransform = matrix;
1813 mCurrentState.hasColorTransform = matrix != identityMatrix;
1814 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001815 setTransactionFlags(eTransactionNeeded);
1816 return true;
1817}
1818
chaviwf66724d2018-11-28 16:35:21 -08001819mat4 Layer::getColorTransform() const {
1820 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1821 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1822 colorTransform = parent->getColorTransform() * colorTransform;
1823 }
1824 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001825}
1826
1827bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001828 bool hasColorTransform = getDrawingState().hasColorTransform;
1829 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1830 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1831 }
1832 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001833}
1834
Chia-I Wu11481472018-05-04 10:43:19 -07001835bool Layer::isLegacyDataSpace() const {
1836 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001837 return !(getDataSpace() &
1838 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1839 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001840}
1841
Robert Carr1f0a16a2016-10-24 16:27:39 -07001842void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001843 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844}
1845
chaviw301b1d82019-11-06 13:15:09 -08001846int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1847 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1848 const State& state = useDrawing ? mDrawingState : mCurrentState;
1849 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001850}
1851
Robert Carr1c5481e2019-07-01 14:42:27 -07001852bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001853 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001854 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001855 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001856}
1857
David Sodman41fdfc92017-11-06 16:09:56 -08001858__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001859 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001860 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1861 "makeTraversalList received invalid stateSet");
1862 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1863 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001864 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001865
Robert Carr29abff82017-12-04 13:51:20 -08001866 if (state.zOrderRelatives.size() == 0) {
1867 *outSkipRelativeZUsers = true;
1868 return children;
1869 }
1870
chaviwfd462612018-05-31 16:11:27 -07001871 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001872 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001873 sp<Layer> strongRelative = weakRelative.promote();
1874 if (strongRelative != nullptr) {
1875 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001876 }
1877 }
1878
Dan Stoza412903f2017-04-27 13:42:17 -07001879 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001880 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001881 continue;
1882 }
Robert Carrdb66e622017-04-10 16:55:57 -07001883 traverse.add(child);
1884 }
1885
1886 return traverse;
1887}
1888
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889/**
Robert Carrdb66e622017-04-10 16:55:57 -07001890 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001891 */
Dan Stoza412903f2017-04-27 13:42:17 -07001892void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001893 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1894 // produce a new list for traversing, including our relatives, and not including our children
1895 // who are relatives of another surface. In the case that there are no relative Z,
1896 // makeTraversalList returns our children directly to avoid significant overhead.
1897 // However in this case we need to take the responsibility for filtering children which
1898 // are relatives of another surface here.
1899 bool skipRelativeZUsers = false;
1900 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001901
Robert Carr1f0a16a2016-10-24 16:27:39 -07001902 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001903 for (; i < list.size(); i++) {
1904 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001905 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1906 continue;
1907 }
1908
chaviw301b1d82019-11-06 13:15:09 -08001909 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001910 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001911 }
Dan Stoza412903f2017-04-27 13:42:17 -07001912 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001913 }
Robert Carr29abff82017-12-04 13:51:20 -08001914
Dan Stoza412903f2017-04-27 13:42:17 -07001915 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001916 for (; i < list.size(); i++) {
1917 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001918
Robert Carr29abff82017-12-04 13:51:20 -08001919 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1920 continue;
1921 }
Dan Stoza412903f2017-04-27 13:42:17 -07001922 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001923 }
1924}
1925
1926/**
Robert Carrdb66e622017-04-10 16:55:57 -07001927 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001928 */
Dan Stoza412903f2017-04-27 13:42:17 -07001929void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1930 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001931 // See traverseInZOrder for documentation.
1932 bool skipRelativeZUsers = false;
1933 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001934
Robert Carr1f0a16a2016-10-24 16:27:39 -07001935 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001936 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001937 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001938
1939 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1940 continue;
1941 }
1942
chaviw301b1d82019-11-06 13:15:09 -08001943 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001944 break;
1945 }
Dan Stoza412903f2017-04-27 13:42:17 -07001946 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001947 }
Dan Stoza412903f2017-04-27 13:42:17 -07001948 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001949 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001950 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001951
1952 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1953 continue;
1954 }
1955
Dan Stoza412903f2017-04-27 13:42:17 -07001956 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001957 }
1958}
1959
Edgar Arriaga844fa672020-01-16 14:21:42 -08001960void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1961 visitor(this);
1962 const LayerVector& children =
1963 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1964 for (const sp<Layer>& child : children) {
1965 child->traverse(state, visitor);
1966 }
1967}
1968
chaviw4b129c22018-04-09 16:19:43 -07001969LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1970 const std::vector<Layer*>& layersInTree) {
1971 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1972 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001973 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1974 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001975 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001976
chaviwfd462612018-05-31 16:11:27 -07001977 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001978 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1979 sp<Layer> strongRelative = weakRelative.promote();
1980 // Only add relative layers that are also descendents of the top most parent of the tree.
1981 // If a relative layer is not a descendent, then it should be ignored.
1982 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1983 traverse.add(strongRelative);
1984 }
1985 }
1986
1987 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001988 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001989 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1990 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1991 // the child here since it won't be added later as a relative.
1992 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1993 childState.zOrderRelativeOf.promote().get())) {
1994 continue;
1995 }
1996 traverse.add(child);
1997 }
1998
1999 return traverse;
2000}
2001
2002void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
2003 LayerVector::StateSet stateSet,
2004 const LayerVector::Visitor& visitor) {
2005 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07002006
2007 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07002008 for (; i < list.size(); i++) {
2009 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08002010 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07002011 break;
2012 }
chaviw4b129c22018-04-09 16:19:43 -07002013 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002014 }
chaviw4b129c22018-04-09 16:19:43 -07002015
chaviwa76b2712017-09-20 12:02:26 -07002016 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07002017 for (; i < list.size(); i++) {
2018 const auto& relative = list[i];
2019 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002020 }
2021}
2022
chaviw4b129c22018-04-09 16:19:43 -07002023std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
2024 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2025 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2026
2027 std::vector<Layer*> layersInTree = {this};
2028 for (size_t i = 0; i < children.size(); i++) {
2029 const auto& child = children[i];
2030 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
2031 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
2032 }
2033
2034 return layersInTree;
2035}
2036
2037void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2038 const LayerVector::Visitor& visitor) {
2039 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
2040 std::sort(layersInTree.begin(), layersInTree.end());
2041 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2042}
2043
Peiyong Linefefaac2018-08-17 12:27:51 -07002044ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002045 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002046}
2047
chaviw13fdc492017-06-27 12:40:18 -07002048half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002049 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002050
chaviw13fdc492017-06-27 12:40:18 -07002051 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2052 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002053}
Robert Carr6452f122017-03-21 10:41:29 -07002054
Vishnu Nair6213bd92020-05-08 17:42:25 -07002055ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
2056 ui::Transform::RotationFlags fixedTransformHint = mCurrentState.fixedTransformHint;
2057 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
2058 return fixedTransformHint;
2059 }
2060 const auto& p = mCurrentParent.promote();
2061 if (!p) return fixedTransformHint;
2062 return p->getFixedTransformHint();
2063}
2064
chaviw13fdc492017-06-27 12:40:18 -07002065half4 Layer::getColor() const {
2066 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002067 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002068}
Robert Carr6452f122017-03-21 10:41:29 -07002069
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002070int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01002071 const auto& p = mDrawingParent.promote();
2072
2073 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2074 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002075}
2076
Galia Peychevae0acf382021-04-12 21:22:34 +02002077const std::vector<BlurRegion> Layer::getBlurRegions() const {
2078 auto regionsCopy(getDrawingState().blurRegions);
2079 int layerAlpha = getAlpha();
2080 for (auto& region : regionsCopy) {
2081 region.alpha = region.alpha * layerAlpha;
2082 }
2083 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07002084}
2085
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002086Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2087 const auto& p = mDrawingParent.promote();
2088 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00002089 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002090 if (parentState.radius > 0) {
2091 ui::Transform t = getActiveTransform(getDrawingState());
2092 t = t.inverse();
2093 parentState.cropRect = t.transform(parentState.cropRect);
2094 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2095 // but a transform matrix can define horizontal and vertical scales.
2096 // Let's take the average between both of them and pass into the shader, practically we
2097 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07002098 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
2099 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
2100 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002101 return parentState;
2102 }
2103 }
2104 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00002105 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
2106 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07002107 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002108}
2109
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002110renderengine::ShadowSettings Layer::getShadowSettings(const Rect& layerStackRect) const {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002111 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
2112
2113 // Shift the spot light x-position to the middle of the display and then
2114 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002115 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002116 state.lightPos.y -= mScreenBounds.top;
2117
2118 state.length = mEffectiveShadowRadius;
2119 return state;
2120}
2121
Robert Carr1f0a16a2016-10-24 16:27:39 -07002122void Layer::commitChildList() {
2123 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2124 const auto& child = mCurrentChildren[i];
2125 child->commitChildList();
2126 }
2127 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002128 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002129}
2130
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002131static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2132 if (weakBinderHandle == nullptr) {
2133 return nullptr;
2134 }
2135 sp<IBinder> binderHandle = weakBinderHandle.promote();
2136 if (binderHandle == nullptr) {
2137 return nullptr;
2138 }
2139 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2140 if (handle == nullptr) {
2141 return nullptr;
2142 }
2143 return handle->owner;
2144}
2145
Robert Carr720e5062018-07-30 17:45:14 -07002146void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002147 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002148 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002149 mCurrentState.modified = true;
2150 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002151 setTransactionFlags(eTransactionNeeded);
2152}
2153
Alec Mouri6b9e9912020-01-21 10:50:24 -08002154LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002155 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08002156 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002157 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002158 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2159
Vishnu Nair60db8c02020-04-02 11:55:16 -07002160 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2161 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002162 if (display) {
2163 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002164 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2165 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002166 }
2167
chaviw08f3cb22020-01-13 13:17:21 -08002168 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002169 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002170 }
chaviw6d89e2d2020-01-14 14:42:01 -08002171
2172 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002173}
2174
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002175void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002176 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002177 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002178
2179 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002180
chaviwd62d3062019-09-04 14:48:02 -07002181 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002182 if (buffer != nullptr) {
2183 LayerProtoHelper::writeToProto(buffer,
2184 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002185 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002186 layerInfo->mutable_buffer_transform());
2187 }
2188 layerInfo->set_invalidate(contentDirty);
2189 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002190 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002191 layerInfo->set_queued_frames(getQueuedFrameCount());
2192 layerInfo->set_refresh_pending(isBufferLatched());
2193 layerInfo->set_curr_frame(mCurrentFrameNumber);
2194 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2195
2196 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002197 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002198 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2199 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2200 [&]() { return layerInfo->mutable_position(); });
2201 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002202 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002203 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002204 [&]() { return layerInfo->mutable_visible_region(); });
2205 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002206 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2207 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002208
2209 if (hasColorTransform()) {
2210 LayerProtoHelper::writeToProto(getColorTransform(),
2211 layerInfo->mutable_color_transform());
2212 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002213 }
2214
Vishnu Nair60db8c02020-04-02 11:55:16 -07002215 LayerProtoHelper::writeToProto(mSourceBounds,
2216 [&]() { return layerInfo->mutable_source_bounds(); });
2217 LayerProtoHelper::writeToProto(mScreenBounds,
2218 [&]() { return layerInfo->mutable_screen_bounds(); });
2219 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2220 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2221 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002222}
2223
2224void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002225 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002226 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2227 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002228 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002229
chaviw766c9c52021-02-10 17:36:47 -08002230 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002231
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002232 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2233 layerInfo->set_id(sequence);
2234 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002235 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002236
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002237 for (const auto& child : children) {
2238 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002239 }
chaviw1d044282017-09-27 12:19:28 -07002240
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002241 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2242 sp<Layer> strongRelative = weakRelative.promote();
2243 if (strongRelative != nullptr) {
2244 layerInfo->add_relatives(strongRelative->sequence);
2245 }
chaviwadc40c22018-07-10 16:57:27 -07002246 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002247
2248 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2249 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002250
2251 layerInfo->set_layer_stack(getLayerStack());
2252 layerInfo->set_z(state.z);
2253
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002254 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2255 [&]() {
2256 return layerInfo->mutable_requested_position();
2257 });
2258
chaviw766c9c52021-02-10 17:36:47 -08002259 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002260 [&]() { return layerInfo->mutable_size(); });
2261
chaviw766c9c52021-02-10 17:36:47 -08002262 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002263
2264 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002265
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002266
2267 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2268 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2269 LayerProtoHelper::writeToProto(state.color,
2270 [&]() { return layerInfo->mutable_requested_color(); });
2271 layerInfo->set_flags(state.flags);
2272
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002273 LayerProtoHelper::writeToProto(requestedTransform,
2274 layerInfo->mutable_requested_transform());
2275
2276 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2277 if (parent != nullptr) {
2278 layerInfo->set_parent(parent->sequence);
2279 } else {
2280 layerInfo->set_parent(-1);
2281 }
2282
2283 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2284 if (zOrderRelativeOf != nullptr) {
2285 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2286 } else {
2287 layerInfo->set_z_order_relative_of(-1);
2288 }
chaviw08f3cb22020-01-13 13:17:21 -08002289
2290 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002291
2292 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002293 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002294
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002295 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw4c34a092020-07-08 11:30:06 -07002296 InputWindowInfo info;
2297 if (useDrawing) {
[1;3C2b9fc252021-02-04 16:16:50 -08002298 info = fillInputInfo({nullptr});
chaviw4c34a092020-07-08 11:30:06 -07002299 } else {
2300 info = state.inputInfo;
2301 }
2302
2303 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002304 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002305 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002306
2307 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2308 auto protoMap = layerInfo->mutable_metadata();
2309 for (const auto& entry : state.metadata.mMap) {
2310 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2311 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002312 }
chaviw1d044282017-09-27 12:19:28 -07002313}
2314
Robert Carr2e102c92018-10-23 12:11:15 -07002315bool Layer::isRemovedFromCurrentState() const {
2316 return mRemovedFromCurrentState;
2317}
2318
chaviw39d01472021-04-08 14:26:24 -05002319ui::Transform Layer::getInputTransform() const {
2320 return getTransform();
2321}
2322
2323Rect Layer::getInputBounds() const {
2324 return getCroppedBufferSize(getDrawingState());
2325}
2326
[1;3C2b9fc252021-02-04 16:16:50 -08002327void Layer::fillInputFrameInfo(InputWindowInfo& info, const ui::Transform& toPhysicalDisplay) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002328 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002329 // If this is a portal window, set the touchableRegion to the layerBounds.
2330 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002331 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002332 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002333 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002334 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002335 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002336
chaviw7e72caf2020-12-02 16:50:43 -08002337 if (!layerBounds.isValid()) {
2338 // If the layer bounds is empty, set the frame to empty and clear the transform
2339 info.frameLeft = 0;
2340 info.frameTop = 0;
2341 info.frameRight = 0;
2342 info.frameBottom = 0;
2343 info.transform.reset();
2344 return;
2345 }
2346
chaviw39d01472021-04-08 14:26:24 -05002347 ui::Transform layerToDisplay = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002348 // Transform that takes window coordinates to unrotated display coordinates
2349 ui::Transform t = toPhysicalDisplay * layerToDisplay;
chaviw7e72caf2020-12-02 16:50:43 -08002350 int32_t xSurfaceInset = info.surfaceInset;
2351 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002352 // Bring screenBounds into unrotated space
2353 Rect screenBounds = toPhysicalDisplay.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002354
chaviw1ff3d1e2020-07-01 15:53:47 -07002355 const float xScale = t.getScaleX();
2356 const float yScale = t.getScaleY();
2357 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002358 xSurfaceInset = std::round(xSurfaceInset * xScale);
2359 ySurfaceInset = std::round(ySurfaceInset * yScale);
2360 }
2361
Peiyong Lin74861ad2020-10-14 11:55:33 -07002362 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002363 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002364
2365 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002366 xSurfaceInset = (xSurfaceInset >= 0)
2367 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2368 : 0;
2369 ySurfaceInset = (ySurfaceInset >= 0)
2370 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2371 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002372
Robert Carra7242302020-09-01 18:26:29 -07002373 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2374 // in the overflow scenario?
2375 {
2376 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002377 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2378 transformedLayerBounds.left = tmp;
2379 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2380 transformedLayerBounds.right = tmp;
2381 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2382 transformedLayerBounds.top = tmp;
2383 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2384 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002385 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002386
chaviw44a6d2b2020-09-08 17:14:16 -07002387 // Compute the correct transform to send to input. This will allow it to transform the
2388 // input coordinates from display space into window space. Therefore, it needs to use the
2389 // final layer frame to create the inverse transform. Since surface insets are added later,
2390 // along with the overflow, the best way to ensure we get the correct transform is to use
2391 // the final frame calculated.
2392 // 1. Take the original transform set on the window and get the inverse transform. This is
2393 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002394 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002395 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2396 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2397 // be the new translation values for the transform.
2398 // 3. Update the translation of the original transform to the new translation values.
2399 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2400 // window space.
2401 ui::Transform inverseTransform = t.inverse();
2402 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2403 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002404 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002405 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002406 info.transform = inputTransform.inverse();
2407
chaviw39cfa2e2020-11-04 14:19:02 -08002408 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2409 // The frame should be the area the user sees on screen since it's used for occlusion
2410 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002411 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2412 info.frameLeft = transformedLayerBounds.left;
2413 info.frameTop = transformedLayerBounds.top;
2414 info.frameRight = transformedLayerBounds.right;
2415 info.frameBottom = transformedLayerBounds.bottom;
2416
Vishnu Nair8033a492018-12-05 07:27:23 -08002417 // Position the touchable region relative to frame screen location and restrict it to frame
2418 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002419 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002420}
2421
[1;3C2b9fc252021-02-04 16:16:50 -08002422InputWindowInfo Layer::fillInputInfo(const sp<DisplayDevice>& display) {
chaviw7e72caf2020-12-02 16:50:43 -08002423 if (!hasInputInfo()) {
2424 mDrawingState.inputInfo.name = getName();
2425 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2426 mDrawingState.inputInfo.ownerPid = mOwnerPid;
2427 mDrawingState.inputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
2428 mDrawingState.inputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
2429 mDrawingState.inputInfo.displayId = getLayerStack();
2430 }
2431
2432 InputWindowInfo info = mDrawingState.inputInfo;
2433 info.id = sequence;
2434
2435 if (info.displayId == ADISPLAY_ID_NONE) {
2436 info.displayId = getLayerStack();
2437 }
2438
[1;3C2b9fc252021-02-04 16:16:50 -08002439 // Transform that goes from "logical(rotated)" display to physical/unrotated display.
2440 // This is for when inputflinger operates in physical display-space.
2441 ui::Transform toPhysicalDisplay;
2442 if (display) {
2443 toPhysicalDisplay = display->getTransform();
2444 }
2445 fillInputFrameInfo(info, toPhysicalDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08002446
Robert Carr5dc426e2020-06-10 14:29:14 -07002447 // For compatibility reasons we let layers which can receive input
2448 // receive input before they have actually submitted a buffer. Because
2449 // of this we use canReceiveInput instead of isVisible to check the
2450 // policy-visibility, ignoring the buffer state. However for layers with
2451 // hasInputInfo()==false we can use the real visibility state.
2452 // We are just using these layers for occlusion detection in
2453 // InputDispatcher, and obviously if they aren't visible they can't occlude
2454 // anything.
2455 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002456 info.alpha = getAlpha();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002457
2458 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2459 if (info.replaceTouchableRegionWithCrop) {
2460 if (cropLayer == nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002461 info.touchableRegion = Region(toPhysicalDisplay.transform(Rect{mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002462 } else {
[1;3C2b9fc252021-02-04 16:16:50 -08002463 info.touchableRegion =
2464 Region(toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002465 }
2466 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002467 info.touchableRegion = info.touchableRegion.intersect(
2468 toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002469 }
2470
chaviwaf87b3e2019-10-01 16:59:28 -07002471 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2472 // touches from going outside the cloned area.
2473 if (isClone()) {
2474 sp<Layer> clonedRoot = getClonedRoot();
2475 if (clonedRoot != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002476 Rect rect = toPhysicalDisplay.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002477 info.touchableRegion = info.touchableRegion.intersect(rect);
2478 }
2479 }
2480
Robert Carr720e5062018-07-30 17:45:14 -07002481 return info;
2482}
2483
chaviwaf87b3e2019-10-01 16:59:28 -07002484sp<Layer> Layer::getClonedRoot() {
2485 if (mClonedChild != nullptr) {
2486 return this;
2487 }
2488 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2489 return nullptr;
2490 }
2491 return mDrawingParent.promote()->getClonedRoot();
2492}
2493
Robert Carredd13602020-04-13 17:24:34 -07002494bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002495 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002496}
2497
chaviw3e727cd2019-01-31 13:41:05 -08002498bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002499 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002500}
2501
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002502compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002503 const DisplayDevice* display) const {
2504 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002505 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002506}
2507
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002508Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2509 const auto outputLayer = findOutputLayerForDisplay(display);
2510 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002511}
2512
chaviwb4c6e582019-08-16 14:35:07 -07002513void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2514 // copy drawing state from cloned layer
2515 mDrawingState = clonedFrom->mDrawingState;
2516 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002517}
chaviw74b03172019-08-19 11:09:03 -07002518
2519void Layer::updateMirrorInfo() {
2520 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2521 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2522 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2523 // that case, we want to delete the reference to the clone since we want it to get
2524 // destroyed. The root, this layer, will still be around since the client can continue
2525 // to hold a reference, but no cloned layers will be displayed.
2526 mClonedChild = nullptr;
2527 return;
2528 }
2529
2530 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2531 // If the real layer exists and is in current state, add the clone as a child of the root.
2532 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2533 // copied to drawingState for the root layer. So the clonedChild is always removed from
2534 // drawingState and then needs to be added back each traversal.
2535 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2536 addChildToDrawing(mClonedChild);
2537 }
2538
2539 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2540 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2541 mClonedChild->updateClonedRelatives(clonedLayersMap);
2542}
2543
2544void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2545 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2546 // to the clone. If the real layer is no longer alive, continue traversing the children
2547 // since we may be able to pull out other children that are still alive.
2548 if (isClonedFromAlive()) {
2549 sp<Layer> clonedFrom = getClonedFrom();
2550 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002551 clonedLayersMap.emplace(clonedFrom, this);
2552 }
2553
2554 // The clone layer may have children in drawingState since they may have been created and
2555 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2556 // that already exist, since we can just re-use them.
2557 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2558 // not updated in the regular traversal. They are skipped since the root will lose the
2559 // reference to them when it copies its currentChildren to drawing.
2560 for (sp<Layer>& child : mDrawingChildren) {
2561 child->updateClonedDrawingState(clonedLayersMap);
2562 }
2563}
2564
2565void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2566 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2567 mDrawingChildren.clear();
2568
2569 if (!isClonedFromAlive()) {
2570 return;
2571 }
2572
2573 sp<Layer> clonedFrom = getClonedFrom();
2574 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2575 if (child == mirrorRoot) {
2576 // This is to avoid cyclical mirroring.
2577 continue;
2578 }
2579 sp<Layer> clonedChild = clonedLayersMap[child];
2580 if (clonedChild == nullptr) {
2581 clonedChild = child->createClone();
2582 clonedLayersMap[child] = clonedChild;
2583 }
2584 addChildToDrawing(clonedChild);
2585 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2586 }
2587}
2588
chaviwaf87b3e2019-10-01 16:59:28 -07002589void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2590 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2591 if (cropLayer != nullptr) {
2592 if (clonedLayersMap.count(cropLayer) == 0) {
2593 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2594 // self as crop layer to avoid going outside bounds.
2595 mDrawingState.touchableRegionCrop = this;
2596 } else {
2597 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2598 mDrawingState.touchableRegionCrop = clonedCropLayer;
2599 }
2600 }
2601 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2602 // WM or the client.
Michael Wright44753b12020-07-08 13:48:11 +01002603 mDrawingState.inputInfo.flags &= ~InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002604}
2605
2606void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002607 mDrawingState.zOrderRelativeOf = nullptr;
2608 mDrawingState.zOrderRelatives.clear();
2609
2610 if (!isClonedFromAlive()) {
2611 return;
2612 }
2613
chaviwaf87b3e2019-10-01 16:59:28 -07002614 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002615 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002616 const sp<Layer>& relative = relativeWeak.promote();
2617 if (clonedLayersMap.count(relative) > 0) {
2618 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002619 mDrawingState.zOrderRelatives.add(clonedRelative);
2620 }
2621 }
2622
2623 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2624 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2625 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2626 // still traverse the children, but the layer with the missing relativeOf will not be shown
2627 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002628 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2629 if (clonedLayersMap.count(relativeOf) > 0) {
2630 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002631 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2632 }
2633
chaviwaf87b3e2019-10-01 16:59:28 -07002634 updateClonedInputInfo(clonedLayersMap);
2635
chaviw74b03172019-08-19 11:09:03 -07002636 for (sp<Layer>& child : mDrawingChildren) {
2637 child->updateClonedRelatives(clonedLayersMap);
2638 }
2639}
2640
2641void Layer::addChildToDrawing(const sp<Layer>& layer) {
2642 mDrawingChildren.add(layer);
2643 layer->mDrawingParent = this;
2644}
2645
Steven Thomas62a4cf82020-01-31 12:04:03 -08002646Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2647 switch (compatibility) {
2648 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2649 return FrameRateCompatibility::Default;
2650 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2651 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002652 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2653 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002654 default:
2655 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2656 return FrameRateCompatibility::Default;
2657 }
2658}
2659
Marin Shalamanovc5986772021-03-16 16:09:49 +01002660scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2661 switch (strategy) {
2662 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2663 return Seamlessness::OnlySeamless;
2664 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2665 return Seamlessness::SeamedAndSeamless;
2666 default:
2667 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2668 return Seamlessness::Default;
2669 }
2670}
2671
chaviwc5676c62020-09-18 15:01:04 -07002672bool Layer::getPrimaryDisplayOnly() const {
2673 const State& s(mDrawingState);
2674 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2675 return true;
2676 }
2677
2678 sp<Layer> parent = mDrawingParent.promote();
2679 return parent == nullptr ? false : parent->getPrimaryDisplayOnly();
2680}
2681
Mathias Agopian13127d82013-03-05 17:47:11 -08002682// ---------------------------------------------------------------------------
2683
Marin Shalamanov46084422020-10-13 12:33:42 +02002684std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2685 return stream << "{rate=" << rate.rate
2686 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002687 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002688}
2689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002690}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002691
2692#if defined(__gl_h_)
2693#error "don't include gl/gl.h in this file"
2694#endif
2695
2696#if defined(__gl2_h_)
2697#error "don't include gl2/gl2.h in this file"
2698#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002699
2700// TODO(b/129481165): remove the #pragma below and fix conversion issues
2701#pragma clang diagnostic pop // ignored "-Wconversion"